A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 107 lines 3.6 kB view raw
1--[[ 2/*************************************************************************** 3 * __________ __ ___. 4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 8 * \/ \/ \/ \/ \/ 9 * $Id$ 10 * 11 * Copyright (C) 2021 William Wilgus 12 * 13 * This program is free software; you can redistribute it and/or 14 * modify it under the terms of the GNU General Public License 15 * as published by the Free Software Foundation; either version 2 16 * of the License, or (at your option) any later version. 17 * 18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 * KIND, either express or implied. 20 * 21 ****************************************************************************/ 22]] 23--create keyboard layout 24--BILGUS 4/2021 25-- create_kbd_layout = require("create_kbd_layout") 26-- local layout = create_kbd_layout("abcd") 27 28local function encode_short(n) 29 return string.char(bit.band(0x00FF, n), bit.rshift(bit.band(0xFF00, n), 8)) 30end 31 32local function utf8decode(str) 33 local INVALID = 0xfffd 34 local t = {} 35 local function check_char(c) 36 local tail = false 37 local code 38 c = string.byte(c) 39 if (c <= 0x7f) or (c >= 0xc2) then 40 -- Start of new character 41 if (c < 0x80) then -- U-00000000 - U-0000007F, 1 string.byte 42 code = c; 43 elseif (c < 0xe0) then -- U-00000080 - U-000007FF, 2 string.bytes 44 tail = 1; 45 code = bit.band(c, 0x1f) 46 elseif (c < 0xf0) then -- U-00000800 - U-0000FFFF, 3 string.bytes 47 tail = 2; 48 code = bit.band(c, 0x0f) 49 elseif (c < 0xf5) then -- U-00010000 - U-001FFFFF, 4 string.bytes 50 tail = 3; 51 code = bit.band(c, 0x07) 52 else 53 -- Invalid size 54 code = INVALID; 55 end 56 57 while tail and c ~= 0 do 58 tail = tail - 1 59 if bit.band(c, 0xc0) == 0x80 then 60 -- Valid continuation character 61 code = bit.bor(bit.lshift(code, 6),bit.band(c, 0x3f)) 62 else 63 -- Invalid continuation char 64 code = INVALID; 65 break; 66 end 67 end 68 else 69 -- Invalid UTF-8 char 70 code = INVALID; 71 end 72 -- currently we don't support chars above U-FFFF 73 t[#t + 1 ] = encode_short((code < 0x10000) and code or INVALID) 74 end 75 str:gsub(".", check_char) -- run check function for every char 76 return table.concat(t) 77end 78 79local function create_keyboard_layout(s_layout) 80 local insert = table.insert 81 lines = {} 82 83 for str in string.gmatch(s_layout, "([^\n]+)") do 84 local len = string.len(str) 85 lines[#lines + 1] = 86 table.concat({encode_short(len), utf8decode(str)}) 87 end 88 lines[#lines + 1] = encode_short(0xFEFF) 89 90 return table.concat(lines) 91end 92 93 94--[[ 95local name = "Test_KBD_LAYOUT_" .. tostring(1) 96local test = create_keyboard_layout("ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n0123456789") 97local file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag 98file:write(test)-- write the layout to the file now 99file:close() 100 101if not file then 102 rb.splash(rb.HZ, "Error opening /" .. name) 103 return 104end 105rb.kbd_input(name, test) 106]] 107return create_keyboard_layout