diff options
-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 | ||||
-rw-r--r-- | apps/plugins/lua_scripts/print_lua_func.lua | 41 |
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 | ||
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); |
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 |
2 | require "actions" | 3 | if not ... then --if executed directly this is nil |
3 | require "audio" | 4 | require "actions" |
4 | require "buttons" | 5 | require "audio" |
5 | require "color" | 6 | require "buttons" |
6 | require "draw" | 7 | require "color" |
7 | require "draw_floodfill" | 8 | require "draw" |
8 | require "draw_poly" | 9 | require "draw_floodfill" |
9 | require "draw_text" | 10 | require "draw_poly" |
10 | 11 | require "draw_text" | |
11 | require "image" | 12 | |
12 | require "image_save" | 13 | require "image" |
13 | 14 | require "image_save" | |
14 | require "lcd" | 15 | |
15 | require "math_ex" | 16 | require "lcd" |
16 | require "pcm" | 17 | require "math_ex" |
17 | require "playlist" | 18 | require "pcm" |
18 | require "print" | 19 | require "playlist" |
19 | --require "settings" --uses a lot of memory | 20 | require "print" |
20 | require "sound" | 21 | --require "settings" --uses a lot of memory |
22 | require "sound" | ||
23 | end | ||
21 | collectgarbage("collect") | 24 | collectgarbage("collect") |
22 | 25 | ||
23 | local sDumpFile = "/rb-lua_functions.txt" | 26 | local sDumpFile = "/rb-lua_functions.txt" |