source: mainline/uspace/srv/hid/input/layout/cz.c

Last change on this file was b987eb4, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Translate keys to characters even if modifiers are pressed.

So that we can match Alt-key to menu accelerators. Of course
this means clients must check Ctrl and Alt state to determine
if they should insert characters, not just just kbd_event_t.c.

  • Property mode set to 100644
File size: 8.2 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup input
30 * @{
31 */
32
33/** @file
34 * @brief Czech QWERTZ layout
35 */
36
37#include <errno.h>
38#include <io/console.h>
39#include <io/keycode.h>
40#include <stdbool.h>
41#include <stdlib.h>
42#include "../input.h"
43#include "../layout.h"
44
45static errno_t cz_create(layout_t *);
46static void cz_destroy(layout_t *);
47static char32_t cz_parse_ev(layout_t *, kbd_event_t *ev);
48
49enum m_state {
50 ms_start,
51 ms_hacek,
52 ms_carka
53};
54
55typedef struct {
56 enum m_state mstate;
57} layout_cz_t;
58
59layout_ops_t cz_ops = {
60 .create = cz_create,
61 .destroy = cz_destroy,
62 .parse_ev = cz_parse_ev
63};
64
65static char32_t map_lcase[] = {
66 [KC_Q] = 'q',
67 [KC_W] = 'w',
68 [KC_E] = 'e',
69 [KC_R] = 'r',
70 [KC_T] = 't',
71 [KC_Y] = 'z',
72 [KC_U] = 'u',
73 [KC_I] = 'i',
74 [KC_O] = 'o',
75 [KC_P] = 'p',
76
77 [KC_A] = 'a',
78 [KC_S] = 's',
79 [KC_D] = 'd',
80 [KC_F] = 'f',
81 [KC_G] = 'g',
82 [KC_H] = 'h',
83 [KC_J] = 'j',
84 [KC_K] = 'k',
85 [KC_L] = 'l',
86
87 [KC_Z] = 'y',
88 [KC_X] = 'x',
89 [KC_C] = 'c',
90 [KC_V] = 'v',
91 [KC_B] = 'b',
92 [KC_N] = 'n',
93 [KC_M] = 'm',
94};
95
96static char32_t map_ucase[] = {
97 [KC_Q] = 'Q',
98 [KC_W] = 'W',
99 [KC_E] = 'E',
100 [KC_R] = 'R',
101 [KC_T] = 'T',
102 [KC_Y] = 'Z',
103 [KC_U] = 'U',
104 [KC_I] = 'I',
105 [KC_O] = 'O',
106 [KC_P] = 'P',
107
108 [KC_A] = 'A',
109 [KC_S] = 'S',
110 [KC_D] = 'D',
111 [KC_F] = 'F',
112 [KC_G] = 'G',
113 [KC_H] = 'H',
114 [KC_J] = 'J',
115 [KC_K] = 'K',
116 [KC_L] = 'L',
117
118 [KC_Z] = 'Y',
119 [KC_X] = 'X',
120 [KC_C] = 'C',
121 [KC_V] = 'V',
122 [KC_B] = 'B',
123 [KC_N] = 'N',
124 [KC_M] = 'M',
125};
126
127static char32_t map_not_shifted[] = {
128 [KC_BACKTICK] = ';',
129
130 [KC_1] = '+',
131
132 [KC_MINUS] = '=',
133
134 [KC_RBRACKET] = ')',
135
136 [KC_QUOTE] = L'§',
137
138 [KC_COMMA] = ',',
139 [KC_PERIOD] = '.',
140 [KC_SLASH] = '-',
141};
142
143static char32_t map_shifted[] = {
144 [KC_1] = '1',
145 [KC_2] = '2',
146 [KC_3] = '3',
147 [KC_4] = '4',
148 [KC_5] = '5',
149 [KC_6] = '6',
150 [KC_7] = '7',
151 [KC_8] = '8',
152 [KC_9] = '9',
153 [KC_0] = '0',
154
155 [KC_MINUS] = '%',
156
157 [KC_LBRACKET] = '/',
158 [KC_RBRACKET] = '(',
159
160 [KC_SEMICOLON] = '"',
161 [KC_QUOTE] = '!',
162 [KC_BACKSLASH] = '\'',
163
164 [KC_COMMA] = '?',
165 [KC_PERIOD] = ':',
166 [KC_SLASH] = '_',
167};
168
169static char32_t map_ns_nocaps[] = {
170 [KC_2] = L'ě',
171 [KC_3] = L'š',
172 [KC_4] = L'č',
173 [KC_5] = L'ř',
174 [KC_6] = L'ž',
175 [KC_7] = L'ý',
176 [KC_8] = L'á',
177 [KC_9] = L'í',
178 [KC_0] = L'é',
179
180 [KC_LBRACKET] = L'ú',
181 [KC_SEMICOLON] = L'ů'
182};
183
184static char32_t map_ns_caps[] = {
185 [KC_2] = L'Ě',
186 [KC_3] = L'Š',
187 [KC_4] = L'Č',
188 [KC_5] = L'Ř',
189 [KC_6] = L'Ž',
190 [KC_7] = L'Ý',
191 [KC_8] = L'Á',
192 [KC_9] = L'Í',
193 [KC_0] = L'É',
194
195 [KC_LBRACKET] = L'Ú',
196 [KC_SEMICOLON] = L'Ů'
197};
198
199static char32_t map_neutral[] = {
200 [KC_BACKSPACE] = '\b',
201 [KC_TAB] = '\t',
202 [KC_ENTER] = '\n',
203 [KC_SPACE] = ' ',
204
205 [KC_NSLASH] = '/',
206 [KC_NTIMES] = '*',
207 [KC_NMINUS] = '-',
208 [KC_NPLUS] = '+',
209 [KC_NENTER] = '\n'
210};
211
212static char32_t map_numeric[] = {
213 [KC_N7] = '7',
214 [KC_N8] = '8',
215 [KC_N9] = '9',
216 [KC_N4] = '4',
217 [KC_N5] = '5',
218 [KC_N6] = '6',
219 [KC_N1] = '1',
220 [KC_N2] = '2',
221 [KC_N3] = '3',
222
223 [KC_N0] = '0',
224 [KC_NPERIOD] = '.'
225};
226
227static char32_t map_hacek_lcase[] = {
228 [KC_E] = L'ě',
229 [KC_R] = L'ř',
230 [KC_T] = L'ť',
231 [KC_Y] = L'ž',
232 [KC_U] = L'ů',
233
234 [KC_S] = L'š',
235 [KC_D] = L'ď',
236
237 [KC_C] = L'č',
238 [KC_N] = L'ň'
239};
240
241static char32_t map_hacek_ucase[] = {
242 [KC_E] = L'Ě',
243 [KC_R] = L'Ř',
244 [KC_T] = L'Ť',
245 [KC_Y] = L'Ž',
246 [KC_U] = L'Ů',
247
248 [KC_S] = L'Š',
249 [KC_D] = L'Ď',
250
251 [KC_C] = L'Č',
252 [KC_N] = L'Ň'
253};
254
255static char32_t map_carka_lcase[] = {
256 [KC_E] = L'é',
257 [KC_U] = L'ú',
258 [KC_I] = L'í',
259 [KC_O] = L'ó',
260
261 [KC_A] = L'á',
262
263 [KC_Z] = L'ý',
264};
265
266static char32_t map_carka_ucase[] = {
267 [KC_E] = L'É',
268 [KC_U] = L'Ú',
269 [KC_I] = L'Í',
270 [KC_O] = L'Ó',
271
272 [KC_A] = L'Á',
273
274 [KC_Z] = L'Ý',
275};
276
277static char32_t translate(unsigned int key, char32_t *map, size_t map_length)
278{
279 if (key >= map_length)
280 return 0;
281 return map[key];
282}
283
284static char32_t parse_ms_hacek(layout_cz_t *cz_state, kbd_event_t *ev)
285{
286 char32_t c;
287
288 cz_state->mstate = ms_start;
289
290 /* Produce no characters when Ctrl or Alt is pressed. */
291 if ((ev->mods & (KM_CTRL | KM_ALT)) != 0)
292 return 0;
293
294 if (((ev->mods & KM_SHIFT) != 0) ^ ((ev->mods & KM_CAPS_LOCK) != 0))
295 c = translate(ev->key, map_hacek_ucase, sizeof(map_hacek_ucase) / sizeof(char32_t));
296 else
297 c = translate(ev->key, map_hacek_lcase, sizeof(map_hacek_lcase) / sizeof(char32_t));
298
299 return c;
300}
301
302static char32_t parse_ms_carka(layout_cz_t *cz_state, kbd_event_t *ev)
303{
304 char32_t c;
305
306 cz_state->mstate = ms_start;
307
308 /* Produce no characters when Ctrl or Alt is pressed. */
309 if ((ev->mods & (KM_CTRL | KM_ALT)) != 0)
310 return 0;
311
312 if (((ev->mods & KM_SHIFT) != 0) ^ ((ev->mods & KM_CAPS_LOCK) != 0))
313 c = translate(ev->key, map_carka_ucase, sizeof(map_carka_ucase) / sizeof(char32_t));
314 else
315 c = translate(ev->key, map_carka_lcase, sizeof(map_carka_lcase) / sizeof(char32_t));
316
317 return c;
318}
319
320static char32_t parse_ms_start(layout_cz_t *cz_state, kbd_event_t *ev)
321{
322 char32_t c;
323
324 if (ev->key == KC_EQUALS) {
325 if ((ev->mods & KM_SHIFT) != 0)
326 cz_state->mstate = ms_hacek;
327 else
328 cz_state->mstate = ms_carka;
329
330 return 0;
331 }
332
333 c = translate(ev->key, map_neutral, sizeof(map_neutral) / sizeof(char32_t));
334 if (c != 0)
335 return c;
336
337 if ((ev->mods & KM_SHIFT) == 0) {
338 if ((ev->mods & KM_CAPS_LOCK) != 0)
339 c = translate(ev->key, map_ns_caps, sizeof(map_ns_caps) / sizeof(char32_t));
340 else
341 c = translate(ev->key, map_ns_nocaps, sizeof(map_ns_nocaps) / sizeof(char32_t));
342
343 if (c != 0)
344 return c;
345 }
346
347 if (((ev->mods & KM_SHIFT) != 0) ^ ((ev->mods & KM_CAPS_LOCK) != 0))
348 c = translate(ev->key, map_ucase, sizeof(map_ucase) / sizeof(char32_t));
349 else
350 c = translate(ev->key, map_lcase, sizeof(map_lcase) / sizeof(char32_t));
351
352 if (c != 0)
353 return c;
354
355 if ((ev->mods & KM_SHIFT) != 0)
356 c = translate(ev->key, map_shifted, sizeof(map_shifted) / sizeof(char32_t));
357 else
358 c = translate(ev->key, map_not_shifted, sizeof(map_not_shifted) / sizeof(char32_t));
359
360 if (c != 0)
361 return c;
362
363 if ((ev->mods & KM_NUM_LOCK) != 0)
364 c = translate(ev->key, map_numeric, sizeof(map_numeric) / sizeof(char32_t));
365 else
366 c = 0;
367
368 return c;
369}
370
371static bool key_is_mod(unsigned key)
372{
373 switch (key) {
374 case KC_LSHIFT:
375 case KC_RSHIFT:
376 case KC_LALT:
377 case KC_RALT:
378 case KC_LCTRL:
379 case KC_RCTRL:
380 return true;
381 default:
382 return false;
383 }
384}
385
386static errno_t cz_create(layout_t *state)
387{
388 layout_cz_t *cz_state;
389
390 cz_state = malloc(sizeof(layout_cz_t));
391 if (cz_state == NULL) {
392 printf("%s: Out of memory.\n", NAME);
393 return ENOMEM;
394 }
395
396 cz_state->mstate = ms_start;
397 state->layout_priv = (void *) cz_state;
398
399 return EOK;
400}
401
402static void cz_destroy(layout_t *state)
403{
404 free(state->layout_priv);
405}
406
407static char32_t cz_parse_ev(layout_t *state, kbd_event_t *ev)
408{
409 layout_cz_t *cz_state = (layout_cz_t *) state->layout_priv;
410
411 if (ev->type != KEY_PRESS)
412 return 0;
413
414 if (key_is_mod(ev->key))
415 return 0;
416
417 switch (cz_state->mstate) {
418 case ms_start:
419 return parse_ms_start(cz_state, ev);
420 case ms_hacek:
421 return parse_ms_hacek(cz_state, ev);
422 case ms_carka:
423 return parse_ms_carka(cz_state, ev);
424 }
425
426 return 0;
427}
428
429/**
430 * @}
431 */
Note: See TracBrowser for help on using the repository browser.