diff options
Diffstat (limited to 'apps/plugins/lua/include_lua/create_kbd_layout.lua')
-rw-r--r-- | apps/plugins/lua/include_lua/create_kbd_layout.lua | 81 |
1 files changed, 81 insertions, 0 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 | ]] | ||