mirror of
https://github.com/Rockbox/rockbox.git
synced 2025-10-14 02:27:39 -04:00
lua fix yellow and add temploader
temp loader allows some lua requires to be loaded and later garbage collected unfortunately the module needs to be formatted in such a way to pass back a call table in order to keep the functions within from being garbage collected too early BE AWARE this bypasses the module loader which would allow code reuse so if you aren't careful this memory saving tool could spell disaster for free RAM if you load the same code multiple times Change-Id: I0b6f81e481b8c779edbd620c8403794f8353926f
This commit is contained in:
parent
489a5f3ff7
commit
9b2f23319c
4 changed files with 104 additions and 60 deletions
|
@ -1,10 +1,14 @@
|
||||||
--create keyboard layout
|
--create keyboard layout
|
||||||
--BILGUS 4/2021
|
--BILGUS 4/2021
|
||||||
|
-- kbdlayout = require("create_kbd_layout")
|
||||||
|
-- local layout = kbdlayout.create_keyboard_layout("abcd")
|
||||||
|
local _kbdlayout = {} do
|
||||||
|
|
||||||
local function encode_short(n)
|
local function encode_short(n)
|
||||||
return string.char(bit.band(0x00FF, n), bit.rshift(bit.band(0xFF00, n), 8))
|
return string.char(bit.band(0x00FF, n), bit.rshift(bit.band(0xFF00, n), 8))
|
||||||
end
|
end
|
||||||
|
|
||||||
function utf8decode(str)
|
local function utf8decode(str)
|
||||||
local INVALID = 0xfffd
|
local INVALID = 0xfffd
|
||||||
local t = {}
|
local t = {}
|
||||||
local function check_char(c)
|
local function check_char(c)
|
||||||
|
@ -51,7 +55,7 @@ function utf8decode(str)
|
||||||
return table.concat(t)
|
return table.concat(t)
|
||||||
end
|
end
|
||||||
|
|
||||||
function create_keyboard_layout(s_layout)
|
local function create_keyboard_layout(s_layout)
|
||||||
local insert = table.insert
|
local insert = table.insert
|
||||||
lines = {}
|
lines = {}
|
||||||
|
|
||||||
|
@ -65,10 +69,14 @@ function create_keyboard_layout(s_layout)
|
||||||
|
|
||||||
return table.concat(lines)
|
return table.concat(lines)
|
||||||
end
|
end
|
||||||
|
_kbdlayout.create_keyboard_layout = create_keyboard_layout
|
||||||
|
_kbdlayout.utf8decode = utf8decode
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
local name = "Test_KBD_LAYOUT_" .. tostring(1)
|
local name = "Test_KBD_LAYOUT_" .. tostring(1)
|
||||||
local test = create_keyboard_layout("ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n0123456789")
|
local test = _kbdlayout.create_keyboard_layout("ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n0123456789")
|
||||||
local file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag
|
local file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag
|
||||||
file:write(test)-- write the layout to the file now
|
file:write(test)-- write the layout to the file now
|
||||||
file:close()
|
file:close()
|
||||||
|
@ -79,3 +87,4 @@ if not file then
|
||||||
end
|
end
|
||||||
rb.kbd_input(name, test)
|
rb.kbd_input(name, test)
|
||||||
]]
|
]]
|
||||||
|
return _kbdlayout
|
||||||
|
|
30
apps/plugins/lua/include_lua/temploader.lua
Normal file
30
apps/plugins/lua/include_lua/temploader.lua
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
--[[
|
||||||
|
temp loader allows some lua requires to be loaded and later garbage collected
|
||||||
|
unfortunately the module needs to be formatted in such a way to pass back a
|
||||||
|
call table in order to keep the functions within from being garbage collected
|
||||||
|
too early
|
||||||
|
|
||||||
|
BE AWARE this bypasses the module loader which would allow code reuse
|
||||||
|
so if you aren't careful this memory saving tool could spell disaster
|
||||||
|
for free RAM if you load the same code multiple times
|
||||||
|
--]]
|
||||||
|
|
||||||
|
|
||||||
|
local function tempload(modulename)
|
||||||
|
--http://lua-users.org/wiki/LuaModulesLoader
|
||||||
|
local errmsg = ""
|
||||||
|
-- Find source
|
||||||
|
local modulepath = string.gsub(modulename, "%.", "/")
|
||||||
|
for path in string.gmatch(package.path, "([^;]+)") do
|
||||||
|
local filename = string.gsub(path, "%?", modulepath)
|
||||||
|
local file = io.open(filename, "r")
|
||||||
|
if file then
|
||||||
|
-- Compile and return the module
|
||||||
|
return assert(loadstring(assert(file:read("*a")), filename))()
|
||||||
|
end
|
||||||
|
errmsg = errmsg.."\n\tno file '"..filename.."' (temp loader)"
|
||||||
|
end
|
||||||
|
return errmsg
|
||||||
|
end
|
||||||
|
|
||||||
|
return tempload
|
|
@ -22,7 +22,8 @@ LUA_INCLUDELIST := $(addprefix $(LUA_BUILDDIR)/,audio.lua blit.lua color.lua \
|
||||||
math_ex.lua print.lua timer.lua playlist.lua pcm.lua \
|
math_ex.lua print.lua timer.lua playlist.lua pcm.lua \
|
||||||
sound.lua rbcompat.lua rbsettings.lua poly_points.lua \
|
sound.lua rbcompat.lua rbsettings.lua poly_points.lua \
|
||||||
printtable.lua printmenus.lua printsubmenu.lua \
|
printtable.lua printmenus.lua printsubmenu.lua \
|
||||||
menubuttons.lua menucoresettings.lua create_kbd_layout.lua)
|
menubuttons.lua menucoresettings.lua create_kbd_layout.lua \
|
||||||
|
temploader.lua)
|
||||||
|
|
||||||
ifndef APP_TYPE
|
ifndef APP_TYPE
|
||||||
ROCKS += $(LUA_BUILDDIR)/lua.rock
|
ROCKS += $(LUA_BUILDDIR)/lua.rock
|
||||||
|
|
|
@ -153,7 +153,7 @@ RB_WRAP(kbd_input)
|
||||||
|
|
||||||
const char *input = lua_tostring(L, 1);
|
const char *input = lua_tostring(L, 1);
|
||||||
size_t layout_len;
|
size_t layout_len;
|
||||||
const char *layout = lua_tolstring(L, 2, &layout_len);
|
const unsigned char *layout = lua_tolstring(L, 2, &layout_len);
|
||||||
char *buffer = luaL_prepbuffer(&b);
|
char *buffer = luaL_prepbuffer(&b);
|
||||||
|
|
||||||
if(input != NULL)
|
if(input != NULL)
|
||||||
|
@ -161,8 +161,12 @@ RB_WRAP(kbd_input)
|
||||||
else
|
else
|
||||||
buffer[0] = '\0';
|
buffer[0] = '\0';
|
||||||
|
|
||||||
if(layout_len <= 1 || (unsigned short)layout[layout_len - 1] != 0xFFFE)
|
if(layout_len <= 2 ||
|
||||||
|
layout[layout_len - 1] != 0xFE ||
|
||||||
|
layout[layout_len - 2] != 0xFF)
|
||||||
|
{
|
||||||
layout = NULL;
|
layout = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE, (unsigned short *)layout))
|
if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE, (unsigned short *)layout))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue