From 489a5f3ff72802fac20ca7459620101cd263bd1a Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Sat, 1 May 2021 08:42:28 -0400 Subject: lua add ability to use custom kbd layouts bring custom keyboard layouts to lua conversion to the proper format requires create_kbd_layout.lua just pass a lua string with your desired layout Change-Id: I14a392410846311a4f3cf8dda0e88d39834d0418 --- apps/plugins/lua/include_lua/create_kbd_layout.lua | 81 ++++++++++++++++++++++ apps/plugins/lua/lua.make | 2 +- apps/plugins/lua/rocklib.c | 12 +++- apps/plugins/lua_scripts/print_lua_func.lua | 41 ++++++----- 4 files changed, 115 insertions(+), 21 deletions(-) create mode 100644 apps/plugins/lua/include_lua/create_kbd_layout.lua (limited to 'apps/plugins') diff --git a/apps/plugins/lua/include_lua/create_kbd_layout.lua b/apps/plugins/lua/include_lua/create_kbd_layout.lua new file mode 100644 index 0000000000..bb3ab297ca --- /dev/null +++ b/apps/plugins/lua/include_lua/create_kbd_layout.lua @@ -0,0 +1,81 @@ +--create keyboard layout +--BILGUS 4/2021 +local function encode_short(n) + return string.char(bit.band(0x00FF, n), bit.rshift(bit.band(0xFF00, n), 8)) +end + +function utf8decode(str) + local INVALID = 0xfffd + local t = {} + local function check_char(c) + local tail = false + local code + c = string.byte(c) + if (c <= 0x7f) or (c >= 0xc2) then + -- Start of new character + if (c < 0x80) then -- U-00000000 - U-0000007F, 1 string.byte + code = c; + elseif (c < 0xe0) then -- U-00000080 - U-000007FF, 2 string.bytes + tail = 1; + code = bit.band(c, 0x1f) + elseif (c < 0xf0) then -- U-00000800 - U-0000FFFF, 3 string.bytes + tail = 2; + code = bit.band(c, 0x0f) + elseif (c < 0xf5) then -- U-00010000 - U-001FFFFF, 4 string.bytes + tail = 3; + code = bit.band(c, 0x07) + else + -- Invalid size + code = 0xfffd; + end + + while tail and c ~= 0 do + tail = tail - 1 + if bit.band(c, 0xc0) == 0x80 then + -- Valid continuation character + code = bit.bor(bit.lshift(code, 6),bit.band(c, 0x3f)) + else + -- Invalid continuation char + code = INVALID; + break; + end + end + else + -- Invalid UTF-8 char + code = INVALID; + end + -- currently we don't support chars above U-FFFF + t[#t + 1 ] = encode_short((code < 0x10000) and code or 0xfffd) + end + str:gsub(".", check_char) -- run check function for every char + return table.concat(t) +end + +function create_keyboard_layout(s_layout) + local insert = table.insert + lines = {} + + local t={} + for str in string.gmatch(s_layout, "([^\n]+)") do + local len = string.len(str) + lines[#lines + 1] = + table.concat({encode_short(len), utf8decode(str)}) + end + lines[#lines + 1] = encode_short(0xFEFF) + + return table.concat(lines) +end + +--[[ +local name = "Test_KBD_LAYOUT_" .. tostring(1) +local test = create_keyboard_layout("ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n0123456789") +local file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag +file:write(test)-- write the layout to the file now +file:close() + +if not file then + rb.splash(rb.HZ, "Error opening /" .. name) + return +end +rb.kbd_input(name, test) +]] diff --git a/apps/plugins/lua/lua.make b/apps/plugins/lua/lua.make index f6dba72aed..eada6b370f 100644 --- a/apps/plugins/lua/lua.make +++ b/apps/plugins/lua/lua.make @@ -22,7 +22,7 @@ LUA_INCLUDELIST := $(addprefix $(LUA_BUILDDIR)/,audio.lua blit.lua color.lua \ math_ex.lua print.lua timer.lua playlist.lua pcm.lua \ sound.lua rbcompat.lua rbsettings.lua poly_points.lua \ printtable.lua printmenus.lua printsubmenu.lua \ - menubuttons.lua menucoresettings.lua) + menubuttons.lua menucoresettings.lua create_kbd_layout.lua) ifndef APP_TYPE ROCKS += $(LUA_BUILDDIR)/lua.rock diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index 81b6f4ce2a..050fbc73b2 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c @@ -143,10 +143,17 @@ RB_WRAP(touchscreen_mode) RB_WRAP(kbd_input) { + /*kbd_input(text, layout)* + note: layout needs special formatting + see includes/create_kbd_layout.lua + or lib/kbd_helper.c + */ luaL_Buffer b; luaL_buffinit(L, &b); const char *input = lua_tostring(L, 1); + size_t layout_len; + const char *layout = lua_tolstring(L, 2, &layout_len); char *buffer = luaL_prepbuffer(&b); if(input != NULL) @@ -154,7 +161,10 @@ RB_WRAP(kbd_input) else buffer[0] = '\0'; - if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE, NULL)) + if(layout_len <= 1 || (unsigned short)layout[layout_len - 1] != 0xFFFE) + layout = NULL; + + if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE, (unsigned short *)layout)) { luaL_addstring(&b, buffer); luaL_pushresult(&b); diff --git a/apps/plugins/lua_scripts/print_lua_func.lua b/apps/plugins/lua_scripts/print_lua_func.lua index ef0290bbd8..8cfbcf0e8e 100644 --- a/apps/plugins/lua_scripts/print_lua_func.lua +++ b/apps/plugins/lua_scripts/print_lua_func.lua @@ -1,23 +1,26 @@ + --RB LUA show all global variables; BILGUS -require "actions" -require "audio" -require "buttons" -require "color" -require "draw" -require "draw_floodfill" -require "draw_poly" -require "draw_text" - -require "image" -require "image_save" - -require "lcd" -require "math_ex" -require "pcm" -require "playlist" -require "print" ---require "settings" --uses a lot of memory -require "sound" +if not ... then --if executed directly this is nil + require "actions" + require "audio" + require "buttons" + require "color" + require "draw" + require "draw_floodfill" + require "draw_poly" + require "draw_text" + + require "image" + require "image_save" + + require "lcd" + require "math_ex" + require "pcm" + require "playlist" + require "print" + --require "settings" --uses a lot of memory + require "sound" +end collectgarbage("collect") local sDumpFile = "/rb-lua_functions.txt" -- cgit v1.2.3