summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2021-05-01 08:42:28 -0400
committerWilliam Wilgus <me.theuser@yahoo.com>2021-05-04 01:52:08 +0000
commit489a5f3ff72802fac20ca7459620101cd263bd1a (patch)
tree9410ca4697575cfc67a698d072aa8dd61245c833
parent4f83e66cd4e00bfa225f54b2c314a3d42d09ca7a (diff)
downloadrockbox-489a5f3ff72802fac20ca7459620101cd263bd1a.tar.gz
rockbox-489a5f3ff72802fac20ca7459620101cd263bd1a.zip
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
-rw-r--r--apps/plugins/lua/include_lua/create_kbd_layout.lua81
-rw-r--r--apps/plugins/lua/lua.make2
-rw-r--r--apps/plugins/lua/rocklib.c12
-rw-r--r--apps/plugins/lua_scripts/print_lua_func.lua41
4 files changed, 115 insertions, 21 deletions
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 @@
1--create keyboard layout
2--BILGUS 4/2021
3local function encode_short(n)
4 return string.char(bit.band(0x00FF, n), bit.rshift(bit.band(0xFF00, n), 8))
5end
6
7function utf8decode(str)
8 local INVALID = 0xfffd
9 local t = {}
10 local function check_char(c)
11 local tail = false
12 local code
13 c = string.byte(c)
14 if (c <= 0x7f) or (c >= 0xc2) then
15 -- Start of new character
16 if (c < 0x80) then -- U-00000000 - U-0000007F, 1 string.byte
17 code = c;
18 elseif (c < 0xe0) then -- U-00000080 - U-000007FF, 2 string.bytes
19 tail = 1;
20 code = bit.band(c, 0x1f)
21 elseif (c < 0xf0) then -- U-00000800 - U-0000FFFF, 3 string.bytes
22 tail = 2;
23 code = bit.band(c, 0x0f)
24 elseif (c < 0xf5) then -- U-00010000 - U-001FFFFF, 4 string.bytes
25 tail = 3;
26 code = bit.band(c, 0x07)
27 else
28 -- Invalid size
29 code = 0xfffd;
30 end
31
32 while tail and c ~= 0 do
33 tail = tail - 1
34 if bit.band(c, 0xc0) == 0x80 then
35 -- Valid continuation character
36 code = bit.bor(bit.lshift(code, 6),bit.band(c, 0x3f))
37 else
38 -- Invalid continuation char
39 code = INVALID;
40 break;
41 end
42 end
43 else
44 -- Invalid UTF-8 char
45 code = INVALID;
46 end
47 -- currently we don't support chars above U-FFFF
48 t[#t + 1 ] = encode_short((code < 0x10000) and code or 0xfffd)
49 end
50 str:gsub(".", check_char) -- run check function for every char
51 return table.concat(t)
52end
53
54function create_keyboard_layout(s_layout)
55 local insert = table.insert
56 lines = {}
57
58 local t={}
59 for str in string.gmatch(s_layout, "([^\n]+)") do
60 local len = string.len(str)
61 lines[#lines + 1] =
62 table.concat({encode_short(len), utf8decode(str)})
63 end
64 lines[#lines + 1] = encode_short(0xFEFF)
65
66 return table.concat(lines)
67end
68
69--[[
70local name = "Test_KBD_LAYOUT_" .. tostring(1)
71local test = create_keyboard_layout("ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n0123456789")
72local file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag
73file:write(test)-- write the layout to the file now
74file:close()
75
76if not file then
77 rb.splash(rb.HZ, "Error opening /" .. name)
78 return
79end
80rb.kbd_input(name, test)
81]]
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 \
22 math_ex.lua print.lua timer.lua playlist.lua pcm.lua \ 22 math_ex.lua print.lua timer.lua playlist.lua pcm.lua \
23 sound.lua rbcompat.lua rbsettings.lua poly_points.lua \ 23 sound.lua rbcompat.lua rbsettings.lua poly_points.lua \
24 printtable.lua printmenus.lua printsubmenu.lua \ 24 printtable.lua printmenus.lua printsubmenu.lua \
25 menubuttons.lua menucoresettings.lua) 25 menubuttons.lua menucoresettings.lua create_kbd_layout.lua)
26 26
27ifndef APP_TYPE 27ifndef APP_TYPE
28 ROCKS += $(LUA_BUILDDIR)/lua.rock 28 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)
143 143
144RB_WRAP(kbd_input) 144RB_WRAP(kbd_input)
145{ 145{
146 /*kbd_input(text, layout)*
147 note: layout needs special formatting
148 see includes/create_kbd_layout.lua
149 or lib/kbd_helper.c
150 */
146 luaL_Buffer b; 151 luaL_Buffer b;
147 luaL_buffinit(L, &b); 152 luaL_buffinit(L, &b);
148 153
149 const char *input = lua_tostring(L, 1); 154 const char *input = lua_tostring(L, 1);
155 size_t layout_len;
156 const char *layout = lua_tolstring(L, 2, &layout_len);
150 char *buffer = luaL_prepbuffer(&b); 157 char *buffer = luaL_prepbuffer(&b);
151 158
152 if(input != NULL) 159 if(input != NULL)
@@ -154,7 +161,10 @@ RB_WRAP(kbd_input)
154 else 161 else
155 buffer[0] = '\0'; 162 buffer[0] = '\0';
156 163
157 if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE, NULL)) 164 if(layout_len <= 1 || (unsigned short)layout[layout_len - 1] != 0xFFFE)
165 layout = NULL;
166
167 if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE, (unsigned short *)layout))
158 { 168 {
159 luaL_addstring(&b, buffer); 169 luaL_addstring(&b, buffer);
160 luaL_pushresult(&b); 170 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 @@
1
1--RB LUA show all global variables; BILGUS 2--RB LUA show all global variables; BILGUS
2require "actions" 3if not ... then --if executed directly this is nil
3require "audio" 4 require "actions"
4require "buttons" 5 require "audio"
5require "color" 6 require "buttons"
6require "draw" 7 require "color"
7require "draw_floodfill" 8 require "draw"
8require "draw_poly" 9 require "draw_floodfill"
9require "draw_text" 10 require "draw_poly"
10 11 require "draw_text"
11require "image" 12
12require "image_save" 13 require "image"
13 14 require "image_save"
14require "lcd" 15
15require "math_ex" 16 require "lcd"
16require "pcm" 17 require "math_ex"
17require "playlist" 18 require "pcm"
18require "print" 19 require "playlist"
19--require "settings" --uses a lot of memory 20 require "print"
20require "sound" 21 --require "settings" --uses a lot of memory
22 require "sound"
23end
21collectgarbage("collect") 24collectgarbage("collect")
22 25
23local sDumpFile = "/rb-lua_functions.txt" 26local sDumpFile = "/rb-lua_functions.txt"