diff options
author | William Wilgus <wilgus.william@gmail.com> | 2021-05-01 08:42:28 -0400 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2021-05-04 01:52:08 +0000 |
commit | 489a5f3ff72802fac20ca7459620101cd263bd1a (patch) | |
tree | 9410ca4697575cfc67a698d072aa8dd61245c833 /apps/plugins/lua | |
parent | 4f83e66cd4e00bfa225f54b2c314a3d42d09ca7a (diff) | |
download | rockbox-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
Diffstat (limited to 'apps/plugins/lua')
-rw-r--r-- | apps/plugins/lua/include_lua/create_kbd_layout.lua | 81 | ||||
-rw-r--r-- | apps/plugins/lua/lua.make | 2 | ||||
-rw-r--r-- | apps/plugins/lua/rocklib.c | 12 |
3 files changed, 93 insertions, 2 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 | ||
3 | local function encode_short(n) | ||
4 | return string.char(bit.band(0x00FF, n), bit.rshift(bit.band(0xFF00, n), 8)) | ||
5 | end | ||
6 | |||
7 | function 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) | ||
52 | end | ||
53 | |||
54 | function 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) | ||
67 | end | ||
68 | |||
69 | --[[ | ||
70 | local name = "Test_KBD_LAYOUT_" .. tostring(1) | ||
71 | local test = create_keyboard_layout("ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n0123456789") | ||
72 | local file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag | ||
73 | file:write(test)-- write the layout to the file now | ||
74 | file:close() | ||
75 | |||
76 | if not file then | ||
77 | rb.splash(rb.HZ, "Error opening /" .. name) | ||
78 | return | ||
79 | end | ||
80 | rb.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 | ||
27 | ifndef APP_TYPE | 27 | ifndef 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 | ||
144 | RB_WRAP(kbd_input) | 144 | RB_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); |