Streamtools: Add FLAC output driver
[jpcrr.git] / scripts / translate-raw-input-to-keycodes
blobb0ce22fbe34dba47c16f9d96e771c3327db02aa8
1 #!/usr/bin/env lua
3 demappings = {};
5 for k, v in ipairs(arg) do
6         demap = string.match(v, "ignore=([0-9]+)");
7         if demap then
8                 demap = tonumber(demap);
9                 if not demap then
10                         error("Bad ignore");
11                 end
12                 table.insert(demappings, demap);
13         end
14 end
16 add_keyboard_map_entry = function(keynum, raw, shifted)
17         if type(raw) == "string" then
18                 raw = string.byte(raw);
19         end
20         if type(shifted) == "string" then
21                 shifted = string.byte(shifted);
22         end
23         if raw then
24                 keyboard_map[raw] = {keynum, keynum};
25                 keyboard_map[raw + 128] = {56, keynum, keynum, 56};
26                 if raw >= 64 and raw <= 95 then
27                         keyboard_map[raw - 64] = {29, keynum, keynum, 29};
28                         keyboard_map[raw + 64] = {56, 29, keynum, keynum, 29, 56};
29                 end
30         end
31         if shifted then
32                 keyboard_map[shifted] = {54, keynum, keynum, 54};
33                 keyboard_map[shifted + 128] = {56, 54, keynum, keynum, 54, 56};
34                 if shifted >= 64 and shifted <= 95 then
35                         keyboard_map[shifted - 64] = {29, 54, keynum, keynum, 54, 29};
36                         keyboard_map[shifted + 64] = {56, 54, 29, keynum, keynum, 29, 54, 56};
37                 end
38         end
39 end
41 -- Specials.
42 special_keys = {};
43 special_keys["1"] = {2, 2};
44 special_keys["2"] = {3, 3};
45 special_keys["3"] = {4, 4};
46 special_keys["4"] = {5, 5};
47 special_keys["5"] = {6, 6};
48 special_keys["6"] = {7, 7};
49 special_keys["7"] = {8, 8};
50 special_keys["8"] = {9, 9};
51 special_keys["9"] = {10, 10};
52 special_keys["0"] = {11, 11};
53 special_keys["A"] = {63, 63};
54 special_keys["B"] = {66, 66};
55 special_keys["C"] = {15, 15};
56 special_keys["r"] = {28, 28};
57 special_keys["n"] = {28, 28};
58 special_keys["D"] = {95, 95};
61 -- Keyboard mapping table
62 keyboard_map = {};
63 add_keyboard_map_entry(1);                              -- ESCAPE
64 add_keyboard_map_entry(2, "1", "!");
65 add_keyboard_map_entry(3, "2", "@");
66 add_keyboard_map_entry(4, "3", "#");
67 add_keyboard_map_entry(5, "4", "$");
68 add_keyboard_map_entry(6, "5", "%");
69 add_keyboard_map_entry(7, "6", "^");
70 add_keyboard_map_entry(8, "7", "&");
71 add_keyboard_map_entry(9, "8", "*");
72 add_keyboard_map_entry(10, "9", "(");
73 add_keyboard_map_entry(11, "0", ")");
74 add_keyboard_map_entry(12, "-", "_");
75 add_keyboard_map_entry(13, "=", "+");
76 add_keyboard_map_entry(14);                             -- BACKSPACE
77 add_keyboard_map_entry(15);                             -- TABULATOR
78 add_keyboard_map_entry(16, "q", "Q");
79 add_keyboard_map_entry(17, "w", "W");
80 add_keyboard_map_entry(18, "e", "E");
81 add_keyboard_map_entry(19, "r", "R");
82 add_keyboard_map_entry(20, "t", "T");
83 add_keyboard_map_entry(21, "y", "Y");
84 add_keyboard_map_entry(22, "u", "U");
85 add_keyboard_map_entry(23, "i", "I");
86 add_keyboard_map_entry(24, "o", "O");
87 add_keyboard_map_entry(25, "p", "P");
88 add_keyboard_map_entry(26, "[", "{");
89 add_keyboard_map_entry(27, "]", "}");
90 add_keyboard_map_entry(28);                             -- ENTER
91 add_keyboard_map_entry(29);                             -- Left CONTROL
92 add_keyboard_map_entry(30, "a", "A");
93 add_keyboard_map_entry(31, "s", "S");
94 add_keyboard_map_entry(32, "d", "D");
95 add_keyboard_map_entry(33, "f", "F");
96 add_keyboard_map_entry(34, "g", "G");
97 add_keyboard_map_entry(35, "h", "H");
98 add_keyboard_map_entry(36, "j", "J");
99 add_keyboard_map_entry(37, "k", "K");
100 add_keyboard_map_entry(38, "l", "L");
101 add_keyboard_map_entry(39, ";", ":");
102 add_keyboard_map_entry(40, "'", "\"");
103 add_keyboard_map_entry(41, "`", "~");
104 add_keyboard_map_entry(42);                             -- LEFT SHIFT
105 add_keyboard_map_entry(43, "\\", "|");
106 add_keyboard_map_entry(44, "z", "Z");
107 add_keyboard_map_entry(45, "x", "X");
108 add_keyboard_map_entry(46, "c", "C");
109 add_keyboard_map_entry(47, "v", "V");
110 add_keyboard_map_entry(48, "b", "B");
111 add_keyboard_map_entry(49, "n", "N");
112 add_keyboard_map_entry(50, "m", "M");
113 add_keyboard_map_entry(51, ",", "<");
114 add_keyboard_map_entry(52, ".", ">");
115 add_keyboard_map_entry(53, "/", "?");
116 add_keyboard_map_entry(54);                             -- RIGHT SHIFT
117 add_keyboard_map_entry(55);                             -- KEYPAD MULTIPLY
118 add_keyboard_map_entry(56);                             -- LEFT ALT
119 add_keyboard_map_entry(57, " ");
120 add_keyboard_map_entry(58);                             -- CAPS LOCK
121 add_keyboard_map_entry(59);                             -- F1
122 add_keyboard_map_entry(60);                             -- F2
123 add_keyboard_map_entry(61);                             -- F3
124 add_keyboard_map_entry(62);                             -- F4
125 add_keyboard_map_entry(63);                             -- F5
126 add_keyboard_map_entry(64);                             -- F6
127 add_keyboard_map_entry(65);                             -- F7
128 add_keyboard_map_entry(66);                             -- F8
129 add_keyboard_map_entry(67);                             -- F9
130 add_keyboard_map_entry(68);                             -- F10
131 add_keyboard_map_entry(69);                             -- NUM LOCK
132 add_keyboard_map_entry(70);                             -- SCROLL LOCK
133 add_keyboard_map_entry(71);                             -- KP7
134 add_keyboard_map_entry(72);                             -- KP8
135 add_keyboard_map_entry(73);                             -- KP9
136 add_keyboard_map_entry(74);                             -- KEYPAD MINUS
137 add_keyboard_map_entry(75);                             -- KP4
138 add_keyboard_map_entry(76);                             -- KP5
139 add_keyboard_map_entry(77);                             -- KP6
140 add_keyboard_map_entry(78);                             -- KEYPAD PLUS
141 add_keyboard_map_entry(79);                             -- KP1
142 add_keyboard_map_entry(80);                             -- KP2
143 add_keyboard_map_entry(81);                             -- KP3
144 add_keyboard_map_entry(82);                             -- KP0
145 add_keyboard_map_entry(83);                             -- KEYPAD DECIMAL SIGN
146 add_keyboard_map_entry(84);                             -- RESERVED BY EMULATOR
147 add_keyboard_map_entry(85);                             -- UNKNOWN
148 add_keyboard_map_entry(86);                             -- UNKNOWN
149 add_keyboard_map_entry(87);                             -- F11
150 add_keyboard_map_entry(88);                             -- F12
151 add_keyboard_map_entry(89);                             -- UNKNOWN
152 add_keyboard_map_entry(90);                             -- UNKNOWN
153 add_keyboard_map_entry(91);                             -- UNKNOWN
154 add_keyboard_map_entry(92);                             -- UNKNOWN
155 add_keyboard_map_entry(93);                             -- UNKNOWN
156 add_keyboard_map_entry(94);                             -- UNKNOWN
157 add_keyboard_map_entry(95);                             -- UNKNOWN
159 -- Ignore ENTER.
160 keyboard_map[0x0A] = {};
162 -- Demap unwanted keys.
163 for k, v in ipairs(demappings) do
164         keyboard_map[v] = {};
167 write = function(tab)
168         local k, v;
169         for k, v in ipairs(tab) do
170                 print(v);
171         end
174 special_escape = false;
175 while true do
176         charo = io.stdin:read(1);
177         if special_escape then
178                 if charo == "\\" then
179                         -- Handle as ordinary.
180                         char = string.byte(charo);
181                         if not keyboard_map[char] then
182                                 error(string.format("Unknown character 0x%02X (not found in keyboard map)", char));
183                         end
184                         write(keyboard_map[char]);
185                 elseif special_keys[charo] then
186                         write(special_keys[charo]);
187                 else
188                         error("Bad special '" .. charo .. "'");
189                 end
190                 special_escape = false;
191         elseif charo == "\\" then
192                 special_escape = true;
193         else
194                 if not charo then
195                         break;
196                 end
197                 char = string.byte(charo);
198                 if not keyboard_map[char] then
199                         error(string.format("Unknown character 0x%02X (not found in keyboard map)", char));
200                 end
201                 write(keyboard_map[char]);
202         end