3 #include <msp/strings/format.h>
11 extern unsigned sys_keymap[];
13 unsigned key_from_sys(unsigned code)
15 static bool init_done = false;
16 static map<unsigned, unsigned> reverse_map;
20 for(unsigned i=0; i<N_KEYS_; ++i)
22 reverse_map[sys_keymap[i]] = i;
27 auto i = reverse_map.find(code);
28 if(i!=reverse_map.end())
34 unsigned key_to_sys(unsigned key)
37 throw invalid_argument("key_to_sys");
38 return sys_keymap[key];
41 void operator>>(const LexicalConverter &conv, Key &key)
43 const string &str = conv.get();
48 case '0': { key = KEY_0; return; }
49 case '1': { key = KEY_1; return; }
50 case '2': { key = KEY_2; return; }
51 case '3': { key = KEY_3; return; }
52 case '4': { key = KEY_4; return; }
53 case '5': { key = KEY_5; return; }
54 case '6': { key = KEY_6; return; }
55 case '7': { key = KEY_7; return; }
56 case '8': { key = KEY_8; return; }
57 case '9': { key = KEY_9; return; }
58 case 'A': { key = KEY_A; return; }
59 case 'B': { key = KEY_B; return; }
60 case 'C': { key = KEY_C; return; }
61 case 'D': { key = KEY_D; return; }
62 case 'E': { key = KEY_E; return; }
63 case 'F': { key = KEY_F; return; }
64 case 'G': { key = KEY_G; return; }
65 case 'H': { key = KEY_H; return; }
66 case 'I': { key = KEY_I; return; }
67 case 'J': { key = KEY_J; return; }
68 case 'K': { key = KEY_K; return; }
69 case 'L': { key = KEY_L; return; }
70 case 'M': { key = KEY_M; return; }
71 case 'N': { key = KEY_N; return; }
72 case 'O': { key = KEY_O; return; }
73 case 'P': { key = KEY_P; return; }
74 case 'Q': { key = KEY_Q; return; }
75 case 'R': { key = KEY_R; return; }
76 case 'S': { key = KEY_S; return; }
77 case 'T': { key = KEY_T; return; }
78 case 'U': { key = KEY_U; return; }
79 case 'V': { key = KEY_V; return; }
80 case 'W': { key = KEY_W; return; }
81 case 'X': { key = KEY_X; return; }
82 case 'Y': { key = KEY_Y; return; }
83 case 'Z': { key = KEY_Z; return; }
91 if(!str.compare(1, string::npos, "DIAERESIS")) { key = KEY_ADIAERESIS; return; }
92 if(!str.compare(1, string::npos, "LT_L")) { key = KEY_ALT_L; return; }
93 if(!str.compare(1, string::npos, "LT_R")) { key = KEY_ALT_R; return; }
94 if(!str.compare(1, string::npos, "POSTROPHE")) { key = KEY_APOSTROPHE; return; }
95 if(!str.compare(1, string::npos, "RING")) { key = KEY_ARING; return; }
98 if(!str.compare(1, string::npos, "ACK")) { key = KEY_BACK; return; }
99 if(!str.compare(1, string::npos, "ACKSLASH")) { key = KEY_BACKSLASH; return; }
100 if(!str.compare(1, string::npos, "ACKSPACE")) { key = KEY_BACKSPACE; return; }
101 if(!str.compare(1, string::npos, "RACKET_L")) { key = KEY_BRACKET_L; return; }
102 if(!str.compare(1, string::npos, "RACKET_R")) { key = KEY_BRACKET_R; return; }
105 if(!str.compare(1, string::npos, "APS_LOCK")) { key = KEY_CAPS_LOCK; return; }
106 if(!str.compare(1, string::npos, "OMMA")) { key = KEY_COMMA; return; }
107 if(!str.compare(1, string::npos, "TRL_L")) { key = KEY_CTRL_L; return; }
108 if(!str.compare(1, string::npos, "TRL_R")) { key = KEY_CTRL_R; return; }
111 if(!str.compare(1, string::npos, "ELETE")) { key = KEY_DELETE; return; }
112 if(!str.compare(1, string::npos, "OWN")) { key = KEY_DOWN; return; }
115 if(!str.compare(1, string::npos, "ND")) { key = KEY_END; return; }
116 if(!str.compare(1, string::npos, "NTER")) { key = KEY_ENTER; return; }
117 if(!str.compare(1, string::npos, "QUAL")) { key = KEY_EQUAL; return; }
118 if(!str.compare(1, string::npos, "SC")) { key = KEY_ESC; return; }
121 if(!str.compare(1, string::npos, "1")) { key = KEY_F1; return; }
122 if(!str.compare(1, string::npos, "2")) { key = KEY_F2; return; }
123 if(!str.compare(1, string::npos, "3")) { key = KEY_F3; return; }
124 if(!str.compare(1, string::npos, "4")) { key = KEY_F4; return; }
125 if(!str.compare(1, string::npos, "5")) { key = KEY_F5; return; }
126 if(!str.compare(1, string::npos, "6")) { key = KEY_F6; return; }
127 if(!str.compare(1, string::npos, "7")) { key = KEY_F7; return; }
128 if(!str.compare(1, string::npos, "8")) { key = KEY_F8; return; }
129 if(!str.compare(1, string::npos, "9")) { key = KEY_F9; return; }
130 if(!str.compare(1, string::npos, "10")) { key = KEY_F10; return; }
131 if(!str.compare(1, string::npos, "11")) { key = KEY_F11; return; }
132 if(!str.compare(1, string::npos, "12")) { key = KEY_F12; return; }
135 if(!str.compare(1, string::npos, "RAVE")) { key = KEY_GRAVE; return; }
138 if(!str.compare(1, string::npos, "OME")) { key = KEY_HOME; return; }
141 if(!str.compare(1, string::npos, "NSERT")) { key = KEY_INSERT; return; }
146 if(!str.compare(2, string::npos, "0")) { key = KEY_KP0; return; }
147 if(!str.compare(2, string::npos, "1")) { key = KEY_KP1; return; }
148 if(!str.compare(2, string::npos, "2")) { key = KEY_KP2; return; }
149 if(!str.compare(2, string::npos, "3")) { key = KEY_KP3; return; }
150 if(!str.compare(2, string::npos, "4")) { key = KEY_KP4; return; }
151 if(!str.compare(2, string::npos, "5")) { key = KEY_KP5; return; }
152 if(!str.compare(2, string::npos, "6")) { key = KEY_KP6; return; }
153 if(!str.compare(2, string::npos, "7")) { key = KEY_KP7; return; }
154 if(!str.compare(2, string::npos, "8")) { key = KEY_KP8; return; }
155 if(!str.compare(2, string::npos, "9")) { key = KEY_KP9; return; }
156 if(!str.compare(2, string::npos, "_ADD")) { key = KEY_KP_ADD; return; }
157 if(!str.compare(2, string::npos, "_DIVIDE")) { key = KEY_KP_DIVIDE; return; }
158 if(!str.compare(2, string::npos, "_MULTIPLY")) { key = KEY_KP_MULTIPLY; return; }
159 if(!str.compare(2, string::npos, "_SEPARATOR")) { key = KEY_KP_SEPARATOR; return; }
160 if(!str.compare(2, string::npos, "_SUBTRACT")) { key = KEY_KP_SUBTRACT; return; }
164 if(!str.compare(1, string::npos, "EFT")) { key = KEY_LEFT; return; }
165 if(!str.compare(1, string::npos, "ESS")) { key = KEY_LESS; return; }
168 if(!str.compare(1, string::npos, "ENU")) { key = KEY_MENU; return; }
169 if(!str.compare(1, string::npos, "INUS")) { key = KEY_MINUS; return; }
172 if(!str.compare(1, string::npos, "ONE")) { key = KEY_NONE; return; }
173 if(!str.compare(1, string::npos, "UM_LOCK")) { key = KEY_NUM_LOCK; return; }
176 if(!str.compare(1, string::npos, "DIAERESIS")) { key = KEY_ODIAERESIS; return; }
179 if(!str.compare(1, string::npos, "AUSE")) { key = KEY_PAUSE; return; }
180 if(!str.compare(1, string::npos, "ERIOD")) { key = KEY_PERIOD; return; }
181 if(!str.compare(1, string::npos, "GUP")) { key = KEY_PGUP; return; }
182 if(!str.compare(1, string::npos, "GDN")) { key = KEY_PGDN; return; }
183 if(!str.compare(1, string::npos, "LUS")) { key = KEY_PLUS; return; }
184 if(!str.compare(1, string::npos, "RINT_SCREEN")) { key = KEY_PRINT_SCREEN; return; }
187 if(!str.compare(1, string::npos, "IGHT")) { key = KEY_RIGHT; return; }
190 if(!str.compare(1, string::npos, "CROLL_LOCK")) { key = KEY_SCROLL_LOCK; return; }
191 if(!str.compare(1, string::npos, "EMICOLON")) { key = KEY_SEMICOLON; return; }
192 if(!str.compare(1, string::npos, "HIFT_L")) { key = KEY_SHIFT_L; return; }
193 if(!str.compare(1, string::npos, "HIFT_R")) { key = KEY_SHIFT_R; return; }
194 if(!str.compare(1, string::npos, "LASH")) { key = KEY_SLASH; return; }
195 if(!str.compare(1, string::npos, "PACE")) { key = KEY_SPACE; return; }
196 if(!str.compare(1, string::npos, "UPER_R")) { key = KEY_SUPER_R; return; }
197 if(!str.compare(1, string::npos, "UPER_L")) { key = KEY_SUPER_L; return; }
200 if(!str.compare(1, string::npos, "AB")) { key = KEY_TAB; return; }
203 if(!str.compare(1, string::npos, "P")) { key = KEY_UP; return; }
206 if(!str.compare(1, string::npos, "OLUME_DOWN")) { key = KEY_VOLUME_DOWN; return; }
207 if(!str.compare(1, string::npos, "OLUME_UP")) { key = KEY_VOLUME_UP; return; }
212 throw lexical_error(format("conversion of '%s' to Key", str));
215 void operator<<(LexicalConverter &conv, Key key)
219 case KEY_NONE: conv.result("NONE"); break;
220 case KEY_BACKSPACE: conv.result("BACKSPACE"); break;
221 case KEY_TAB: conv.result("TAB"); break;
222 case KEY_ENTER: conv.result("ENTER"); break;
223 case KEY_ESC: conv.result("ESC"); break;
224 case KEY_SPACE: conv.result("SPACE"); break;
225 case KEY_APOSTROPHE: conv.result("APOSTROPHE"); break;
226 case KEY_PLUS: conv.result("PLUS"); break;
227 case KEY_COMMA: conv.result("COMMA"); break;
228 case KEY_MINUS: conv.result("MINUS"); break;
229 case KEY_PERIOD: conv.result("PERIOD"); break;
230 case KEY_SLASH: conv.result("SLASH"); break;
231 case KEY_0: conv.result("0"); break;
232 case KEY_1: conv.result("1"); break;
233 case KEY_2: conv.result("2"); break;
234 case KEY_3: conv.result("3"); break;
235 case KEY_4: conv.result("4"); break;
236 case KEY_5: conv.result("5"); break;
237 case KEY_6: conv.result("6"); break;
238 case KEY_7: conv.result("7"); break;
239 case KEY_8: conv.result("8"); break;
240 case KEY_9: conv.result("9"); break;
241 case KEY_SEMICOLON: conv.result("SEMICOLON"); break;
242 case KEY_LESS: conv.result("LESS"); break;
243 case KEY_EQUAL: conv.result("EQUAL"); break;
244 case KEY_A: conv.result("A"); break;
245 case KEY_B: conv.result("B"); break;
246 case KEY_C: conv.result("C"); break;
247 case KEY_D: conv.result("D"); break;
248 case KEY_E: conv.result("E"); break;
249 case KEY_F: conv.result("F"); break;
250 case KEY_G: conv.result("G"); break;
251 case KEY_H: conv.result("H"); break;
252 case KEY_I: conv.result("I"); break;
253 case KEY_J: conv.result("J"); break;
254 case KEY_K: conv.result("K"); break;
255 case KEY_L: conv.result("L"); break;
256 case KEY_M: conv.result("M"); break;
257 case KEY_N: conv.result("N"); break;
258 case KEY_O: conv.result("O"); break;
259 case KEY_P: conv.result("P"); break;
260 case KEY_Q: conv.result("Q"); break;
261 case KEY_R: conv.result("R"); break;
262 case KEY_S: conv.result("S"); break;
263 case KEY_T: conv.result("T"); break;
264 case KEY_U: conv.result("U"); break;
265 case KEY_V: conv.result("V"); break;
266 case KEY_W: conv.result("W"); break;
267 case KEY_X: conv.result("X"); break;
268 case KEY_Y: conv.result("Y"); break;
269 case KEY_Z: conv.result("Z"); break;
270 case KEY_BRACKET_L: conv.result("BRACKET_L"); break;
271 case KEY_BACKSLASH: conv.result("BACKSLASH"); break;
272 case KEY_BRACKET_R: conv.result("BRACKET_R"); break;
273 case KEY_GRAVE: conv.result("GRAVE"); break;
274 case KEY_ADIAERESIS: conv.result("ADIAERESIS"); break;
275 case KEY_ODIAERESIS: conv.result("ODIAERESIS"); break;
276 case KEY_UDIAERESIS: conv.result("UDIAERESIS"); break;
277 case KEY_ARING: conv.result("ARING"); break;
278 case KEY_LEFT: conv.result("LEFT"); break;
279 case KEY_RIGHT: conv.result("RIGHT"); break;
280 case KEY_UP: conv.result("UP"); break;
281 case KEY_DOWN: conv.result("DOWN"); break;
282 case KEY_HOME: conv.result("HOME"); break;
283 case KEY_END: conv.result("END"); break;
284 case KEY_PGUP: conv.result("PGUP"); break;
285 case KEY_PGDN: conv.result("PGDN"); break;
286 case KEY_INSERT: conv.result("INSERT"); break;
287 case KEY_DELETE: conv.result("DELETE"); break;
288 case KEY_F1: conv.result("F1"); break;
289 case KEY_F2: conv.result("F2"); break;
290 case KEY_F3: conv.result("F3"); break;
291 case KEY_F4: conv.result("F4"); break;
292 case KEY_F5: conv.result("F5"); break;
293 case KEY_F6: conv.result("F6"); break;
294 case KEY_F7: conv.result("F7"); break;
295 case KEY_F8: conv.result("F8"); break;
296 case KEY_F9: conv.result("F9"); break;
297 case KEY_F10: conv.result("F10"); break;
298 case KEY_F11: conv.result("F11"); break;
299 case KEY_F12: conv.result("F12"); break;
300 case KEY_SHIFT_L: conv.result("SHIFT_L"); break;
301 case KEY_SHIFT_R: conv.result("SHIFT_R"); break;
302 case KEY_CTRL_L: conv.result("CTRL_L"); break;
303 case KEY_CTRL_R: conv.result("CTRL_R"); break;
304 case KEY_ALT_L: conv.result("ALT_L"); break;
305 case KEY_ALT_R: conv.result("ALT_R"); break;
306 case KEY_SUPER_L: conv.result("SUPER_L"); break;
307 case KEY_SUPER_R: conv.result("SUPER_R"); break;
308 case KEY_CAPS_LOCK: conv.result("CAPS_LOCK"); break;
309 case KEY_SCROLL_LOCK: conv.result("SCROLL_LOCK"); break;
310 case KEY_NUM_LOCK: conv.result("NUM_LOCK"); break;
311 case KEY_KP0: conv.result("KP0"); break;
312 case KEY_KP1: conv.result("KP1"); break;
313 case KEY_KP2: conv.result("KP2"); break;
314 case KEY_KP3: conv.result("KP3"); break;
315 case KEY_KP4: conv.result("KP4"); break;
316 case KEY_KP5: conv.result("KP5"); break;
317 case KEY_KP6: conv.result("KP6"); break;
318 case KEY_KP7: conv.result("KP7"); break;
319 case KEY_KP8: conv.result("KP8"); break;
320 case KEY_KP9: conv.result("KP9"); break;
321 case KEY_KP_ADD: conv.result("KP_ADD"); break;
322 case KEY_KP_SUBTRACT: conv.result("KP_SUBTRACT"); break;
323 case KEY_KP_MULTIPLY: conv.result("KP_MULTIPLY"); break;
324 case KEY_KP_DIVIDE: conv.result("KP_DIVIDE"); break;
325 case KEY_KP_SEPARATOR: conv.result("KP_SEPARATOR"); break;
326 case KEY_PAUSE: conv.result("PAUSE"); break;
327 case KEY_PRINT_SCREEN: conv.result("PRINT_SCREEN"); break;
328 case KEY_MENU: conv.result("MENU"); break;
329 case KEY_BACK: conv.result("BACK"); break;
330 case KEY_VOLUME_UP: conv.result("VOLUME_UP"); break;
331 case KEY_VOLUME_DOWN: conv.result("VOLUME_DOWN"); break;
332 default: conv.result(format("Key(%#x)", static_cast<int>(key)));
336 void operator>>(const LexicalConverter &conv, MouseAxis &axis)
340 else if(conv.get()=="Y")
343 throw lexical_error(format("conversion of '%s' to MouseAxis", conv.get()));
346 void operator<<(LexicalConverter &conv, MouseAxis axis)
350 case MOUSE_X_AXIS: conv.result("X"); break;
351 case MOUSE_Y_AXIS: conv.result("Y"); break;
352 default: conv.result(format("MouseAxis(%#x)", static_cast<int>(axis)));
356 void operator>>(const LexicalConverter &conv, MouseButton &btn)
358 if(conv.get()=="LEFT")
360 else if(conv.get()=="MIDDLE")
362 else if(conv.get()=="RIGHT")
364 else if(conv.get()=="WHEEL_UP")
365 btn = MOUSE_WHEEL_UP;
366 else if(conv.get()=="WHEEL_DOWN")
367 btn = MOUSE_WHEEL_DOWN;
369 throw lexical_error(format("conversion of '%s' to MouseButton", conv.get()));
372 void operator<<(LexicalConverter &conv, MouseButton btn)
376 case MOUSE_LEFT: conv.result("LEFT"); break;
377 case MOUSE_MIDDLE: conv.result("MIDDLE"); break;
378 case MOUSE_RIGHT: conv.result("RIGHT"); break;
379 case MOUSE_WHEEL_UP: conv.result("WHEEL_UP"); break;
380 case MOUSE_WHEEL_DOWN: conv.result("WHEEL_DOWN"); break;
381 default: conv.result(format("MouseButton(%#x)", static_cast<int>(btn)));