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 |
|
---|
45 | static errno_t cz_create(layout_t *);
|
---|
46 | static void cz_destroy(layout_t *);
|
---|
47 | static wchar_t cz_parse_ev(layout_t *, kbd_event_t *ev);
|
---|
48 |
|
---|
49 | enum m_state {
|
---|
50 | ms_start,
|
---|
51 | ms_hacek,
|
---|
52 | ms_carka
|
---|
53 | };
|
---|
54 |
|
---|
55 | typedef struct {
|
---|
56 | enum m_state mstate;
|
---|
57 | } layout_cz_t;
|
---|
58 |
|
---|
59 | #ifdef CONFIG_KB_LAYOUT_cz
|
---|
60 |
|
---|
61 | layout_ops_t layout_default = {
|
---|
62 | .create = cz_create,
|
---|
63 | .destroy = cz_destroy,
|
---|
64 | .parse_ev = cz_parse_ev
|
---|
65 | };
|
---|
66 |
|
---|
67 | #else
|
---|
68 |
|
---|
69 | layout_ops_t get_layout(void);
|
---|
70 | layout_ops_t get_layout(void) {
|
---|
71 | layout_ops_t layout_default = {
|
---|
72 | .create = cz_create,
|
---|
73 | .destroy = cz_destroy,
|
---|
74 | .parse_ev = cz_parse_ev
|
---|
75 | };
|
---|
76 | return layout_default;
|
---|
77 | }
|
---|
78 |
|
---|
79 | #endif
|
---|
80 |
|
---|
81 | static wchar_t map_lcase[] = {
|
---|
82 | [KC_Q] = 'q',
|
---|
83 | [KC_W] = 'w',
|
---|
84 | [KC_E] = 'e',
|
---|
85 | [KC_R] = 'r',
|
---|
86 | [KC_T] = 't',
|
---|
87 | [KC_Y] = 'z',
|
---|
88 | [KC_U] = 'u',
|
---|
89 | [KC_I] = 'i',
|
---|
90 | [KC_O] = 'o',
|
---|
91 | [KC_P] = 'p',
|
---|
92 |
|
---|
93 | [KC_A] = 'a',
|
---|
94 | [KC_S] = 's',
|
---|
95 | [KC_D] = 'd',
|
---|
96 | [KC_F] = 'f',
|
---|
97 | [KC_G] = 'g',
|
---|
98 | [KC_H] = 'h',
|
---|
99 | [KC_J] = 'j',
|
---|
100 | [KC_K] = 'k',
|
---|
101 | [KC_L] = 'l',
|
---|
102 |
|
---|
103 | [KC_Z] = 'y',
|
---|
104 | [KC_X] = 'x',
|
---|
105 | [KC_C] = 'c',
|
---|
106 | [KC_V] = 'v',
|
---|
107 | [KC_B] = 'b',
|
---|
108 | [KC_N] = 'n',
|
---|
109 | [KC_M] = 'm',
|
---|
110 | };
|
---|
111 |
|
---|
112 | static wchar_t map_ucase[] = {
|
---|
113 | [KC_Q] = 'Q',
|
---|
114 | [KC_W] = 'W',
|
---|
115 | [KC_E] = 'E',
|
---|
116 | [KC_R] = 'R',
|
---|
117 | [KC_T] = 'T',
|
---|
118 | [KC_Y] = 'Z',
|
---|
119 | [KC_U] = 'U',
|
---|
120 | [KC_I] = 'I',
|
---|
121 | [KC_O] = 'O',
|
---|
122 | [KC_P] = 'P',
|
---|
123 |
|
---|
124 | [KC_A] = 'A',
|
---|
125 | [KC_S] = 'S',
|
---|
126 | [KC_D] = 'D',
|
---|
127 | [KC_F] = 'F',
|
---|
128 | [KC_G] = 'G',
|
---|
129 | [KC_H] = 'H',
|
---|
130 | [KC_J] = 'J',
|
---|
131 | [KC_K] = 'K',
|
---|
132 | [KC_L] = 'L',
|
---|
133 |
|
---|
134 | [KC_Z] = 'Y',
|
---|
135 | [KC_X] = 'X',
|
---|
136 | [KC_C] = 'C',
|
---|
137 | [KC_V] = 'V',
|
---|
138 | [KC_B] = 'B',
|
---|
139 | [KC_N] = 'N',
|
---|
140 | [KC_M] = 'M',
|
---|
141 | };
|
---|
142 |
|
---|
143 | static wchar_t map_not_shifted[] = {
|
---|
144 | [KC_BACKTICK] = ';',
|
---|
145 |
|
---|
146 | [KC_1] = '+',
|
---|
147 |
|
---|
148 | [KC_MINUS] = '=',
|
---|
149 |
|
---|
150 | [KC_RBRACKET] = ')',
|
---|
151 |
|
---|
152 | [KC_QUOTE] = L'§',
|
---|
153 |
|
---|
154 | [KC_COMMA] = ',',
|
---|
155 | [KC_PERIOD] = '.',
|
---|
156 | [KC_SLASH] = '-',
|
---|
157 | };
|
---|
158 |
|
---|
159 | static wchar_t map_shifted[] = {
|
---|
160 | [KC_1] = '1',
|
---|
161 | [KC_2] = '2',
|
---|
162 | [KC_3] = '3',
|
---|
163 | [KC_4] = '4',
|
---|
164 | [KC_5] = '5',
|
---|
165 | [KC_6] = '6',
|
---|
166 | [KC_7] = '7',
|
---|
167 | [KC_8] = '8',
|
---|
168 | [KC_9] = '9',
|
---|
169 | [KC_0] = '0',
|
---|
170 |
|
---|
171 | [KC_MINUS] = '%',
|
---|
172 |
|
---|
173 | [KC_LBRACKET] = '/',
|
---|
174 | [KC_RBRACKET] = '(',
|
---|
175 |
|
---|
176 | [KC_SEMICOLON] = '"',
|
---|
177 | [KC_QUOTE] = '!',
|
---|
178 | [KC_BACKSLASH] = '\'',
|
---|
179 |
|
---|
180 | [KC_COMMA] = '?',
|
---|
181 | [KC_PERIOD] = ':',
|
---|
182 | [KC_SLASH] = '_',
|
---|
183 | };
|
---|
184 |
|
---|
185 | static wchar_t map_ns_nocaps[] = {
|
---|
186 | [KC_2] = L'ě',
|
---|
187 | [KC_3] = L'š',
|
---|
188 | [KC_4] = L'č',
|
---|
189 | [KC_5] = L'ř',
|
---|
190 | [KC_6] = L'ž',
|
---|
191 | [KC_7] = L'ý',
|
---|
192 | [KC_8] = L'á',
|
---|
193 | [KC_9] = L'í',
|
---|
194 | [KC_0] = L'é',
|
---|
195 |
|
---|
196 | [KC_LBRACKET] = L'ú',
|
---|
197 | [KC_SEMICOLON] = L'ů'
|
---|
198 | };
|
---|
199 |
|
---|
200 | static wchar_t map_ns_caps[] = {
|
---|
201 | [KC_2] = L'Ě',
|
---|
202 | [KC_3] = L'Š',
|
---|
203 | [KC_4] = L'Č',
|
---|
204 | [KC_5] = L'Ř',
|
---|
205 | [KC_6] = L'Ž',
|
---|
206 | [KC_7] = L'Ý',
|
---|
207 | [KC_8] = L'Á',
|
---|
208 | [KC_9] = L'Í',
|
---|
209 | [KC_0] = L'É',
|
---|
210 |
|
---|
211 | [KC_LBRACKET] = L'Ú',
|
---|
212 | [KC_SEMICOLON] = L'Ů'
|
---|
213 | };
|
---|
214 |
|
---|
215 | static wchar_t map_neutral[] = {
|
---|
216 | [KC_BACKSPACE] = '\b',
|
---|
217 | [KC_TAB] = '\t',
|
---|
218 | [KC_ENTER] = '\n',
|
---|
219 | [KC_SPACE] = ' ',
|
---|
220 |
|
---|
221 | [KC_NSLASH] = '/',
|
---|
222 | [KC_NTIMES] = '*',
|
---|
223 | [KC_NMINUS] = '-',
|
---|
224 | [KC_NPLUS] = '+',
|
---|
225 | [KC_NENTER] = '\n'
|
---|
226 | };
|
---|
227 |
|
---|
228 | static wchar_t map_numeric[] = {
|
---|
229 | [KC_N7] = '7',
|
---|
230 | [KC_N8] = '8',
|
---|
231 | [KC_N9] = '9',
|
---|
232 | [KC_N4] = '4',
|
---|
233 | [KC_N5] = '5',
|
---|
234 | [KC_N6] = '6',
|
---|
235 | [KC_N1] = '1',
|
---|
236 | [KC_N2] = '2',
|
---|
237 | [KC_N3] = '3',
|
---|
238 |
|
---|
239 | [KC_N0] = '0',
|
---|
240 | [KC_NPERIOD] = '.'
|
---|
241 | };
|
---|
242 |
|
---|
243 | static wchar_t map_hacek_lcase[] = {
|
---|
244 | [KC_E] = L'ě',
|
---|
245 | [KC_R] = L'ř',
|
---|
246 | [KC_T] = L'ť',
|
---|
247 | [KC_Y] = L'ž',
|
---|
248 | [KC_U] = L'ů',
|
---|
249 |
|
---|
250 | [KC_S] = L'š',
|
---|
251 | [KC_D] = L'ď',
|
---|
252 |
|
---|
253 | [KC_C] = L'č',
|
---|
254 | [KC_N] = L'ň'
|
---|
255 | };
|
---|
256 |
|
---|
257 | static wchar_t map_hacek_ucase[] = {
|
---|
258 | [KC_E] = L'Ě',
|
---|
259 | [KC_R] = L'Ř',
|
---|
260 | [KC_T] = L'Ť',
|
---|
261 | [KC_Y] = L'Ž',
|
---|
262 | [KC_U] = L'Ů',
|
---|
263 |
|
---|
264 | [KC_S] = L'Š',
|
---|
265 | [KC_D] = L'Ď',
|
---|
266 |
|
---|
267 | [KC_C] = L'Č',
|
---|
268 | [KC_N] = L'Ň'
|
---|
269 | };
|
---|
270 |
|
---|
271 | static wchar_t map_carka_lcase[] = {
|
---|
272 | [KC_E] = L'é',
|
---|
273 | [KC_U] = L'ú',
|
---|
274 | [KC_I] = L'í',
|
---|
275 | [KC_O] = L'ó',
|
---|
276 |
|
---|
277 | [KC_A] = L'á',
|
---|
278 |
|
---|
279 | [KC_Z] = L'ý',
|
---|
280 | };
|
---|
281 |
|
---|
282 | static wchar_t map_carka_ucase[] = {
|
---|
283 | [KC_E] = L'É',
|
---|
284 | [KC_U] = L'Ú',
|
---|
285 | [KC_I] = L'Í',
|
---|
286 | [KC_O] = L'Ó',
|
---|
287 |
|
---|
288 | [KC_A] = L'Á',
|
---|
289 |
|
---|
290 | [KC_Z] = L'Ý',
|
---|
291 | };
|
---|
292 |
|
---|
293 | static wchar_t translate(unsigned int key, wchar_t *map, size_t map_length)
|
---|
294 | {
|
---|
295 | if (key >= map_length)
|
---|
296 | return 0;
|
---|
297 | return map[key];
|
---|
298 | }
|
---|
299 |
|
---|
300 | static wchar_t parse_ms_hacek(layout_cz_t *cz_state, kbd_event_t *ev)
|
---|
301 | {
|
---|
302 | wchar_t c;
|
---|
303 |
|
---|
304 | cz_state->mstate = ms_start;
|
---|
305 |
|
---|
306 | /* Produce no characters when Ctrl or Alt is pressed. */
|
---|
307 | if ((ev->mods & (KM_CTRL | KM_ALT)) != 0)
|
---|
308 | return 0;
|
---|
309 |
|
---|
310 | if (((ev->mods & KM_SHIFT) != 0) ^ ((ev->mods & KM_CAPS_LOCK) != 0))
|
---|
311 | c = translate(ev->key, map_hacek_ucase, sizeof(map_hacek_ucase) / sizeof(wchar_t));
|
---|
312 | else
|
---|
313 | c = translate(ev->key, map_hacek_lcase, sizeof(map_hacek_lcase) / sizeof(wchar_t));
|
---|
314 |
|
---|
315 | return c;
|
---|
316 | }
|
---|
317 |
|
---|
318 | static wchar_t parse_ms_carka(layout_cz_t *cz_state, kbd_event_t *ev)
|
---|
319 | {
|
---|
320 | wchar_t c;
|
---|
321 |
|
---|
322 | cz_state->mstate = ms_start;
|
---|
323 |
|
---|
324 | /* Produce no characters when Ctrl or Alt is pressed. */
|
---|
325 | if ((ev->mods & (KM_CTRL | KM_ALT)) != 0)
|
---|
326 | return 0;
|
---|
327 |
|
---|
328 | if (((ev->mods & KM_SHIFT) != 0) ^ ((ev->mods & KM_CAPS_LOCK) != 0))
|
---|
329 | c = translate(ev->key, map_carka_ucase, sizeof(map_carka_ucase) / sizeof(wchar_t));
|
---|
330 | else
|
---|
331 | c = translate(ev->key, map_carka_lcase, sizeof(map_carka_lcase) / sizeof(wchar_t));
|
---|
332 |
|
---|
333 | return c;
|
---|
334 | }
|
---|
335 |
|
---|
336 | static wchar_t parse_ms_start(layout_cz_t *cz_state, kbd_event_t *ev)
|
---|
337 | {
|
---|
338 | wchar_t c;
|
---|
339 |
|
---|
340 | /* Produce no characters when Ctrl or Alt is pressed. */
|
---|
341 | if ((ev->mods & (KM_CTRL | KM_ALT)) != 0)
|
---|
342 | return 0;
|
---|
343 |
|
---|
344 | if (ev->key == KC_EQUALS) {
|
---|
345 | if ((ev->mods & KM_SHIFT) != 0)
|
---|
346 | cz_state->mstate = ms_hacek;
|
---|
347 | else
|
---|
348 | cz_state->mstate = ms_carka;
|
---|
349 |
|
---|
350 | return 0;
|
---|
351 | }
|
---|
352 |
|
---|
353 | c = translate(ev->key, map_neutral, sizeof(map_neutral) / sizeof(wchar_t));
|
---|
354 | if (c != 0)
|
---|
355 | return c;
|
---|
356 |
|
---|
357 | if ((ev->mods & KM_SHIFT) == 0) {
|
---|
358 | if ((ev->mods & KM_CAPS_LOCK) != 0)
|
---|
359 | c = translate(ev->key, map_ns_caps, sizeof(map_ns_caps) / sizeof(wchar_t));
|
---|
360 | else
|
---|
361 | c = translate(ev->key, map_ns_nocaps, sizeof(map_ns_nocaps) / sizeof(wchar_t));
|
---|
362 |
|
---|
363 | if (c != 0)
|
---|
364 | return c;
|
---|
365 | }
|
---|
366 |
|
---|
367 | if (((ev->mods & KM_SHIFT) != 0) ^ ((ev->mods & KM_CAPS_LOCK) != 0))
|
---|
368 | c = translate(ev->key, map_ucase, sizeof(map_ucase) / sizeof(wchar_t));
|
---|
369 | else
|
---|
370 | c = translate(ev->key, map_lcase, sizeof(map_lcase) / sizeof(wchar_t));
|
---|
371 |
|
---|
372 | if (c != 0)
|
---|
373 | return c;
|
---|
374 |
|
---|
375 | if ((ev->mods & KM_SHIFT) != 0)
|
---|
376 | c = translate(ev->key, map_shifted, sizeof(map_shifted) / sizeof(wchar_t));
|
---|
377 | else
|
---|
378 | c = translate(ev->key, map_not_shifted, sizeof(map_not_shifted) / sizeof(wchar_t));
|
---|
379 |
|
---|
380 | if (c != 0)
|
---|
381 | return c;
|
---|
382 |
|
---|
383 | if ((ev->mods & KM_NUM_LOCK) != 0)
|
---|
384 | c = translate(ev->key, map_numeric, sizeof(map_numeric) / sizeof(wchar_t));
|
---|
385 | else
|
---|
386 | c = 0;
|
---|
387 |
|
---|
388 | return c;
|
---|
389 | }
|
---|
390 |
|
---|
391 | static bool key_is_mod(unsigned key)
|
---|
392 | {
|
---|
393 | switch (key) {
|
---|
394 | case KC_LSHIFT:
|
---|
395 | case KC_RSHIFT:
|
---|
396 | case KC_LALT:
|
---|
397 | case KC_RALT:
|
---|
398 | case KC_LCTRL:
|
---|
399 | case KC_RCTRL:
|
---|
400 | return true;
|
---|
401 | default:
|
---|
402 | return false;
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | static errno_t cz_create(layout_t *state)
|
---|
407 | {
|
---|
408 | layout_cz_t *cz_state;
|
---|
409 |
|
---|
410 | cz_state = malloc(sizeof(layout_cz_t));
|
---|
411 | if (cz_state == NULL) {
|
---|
412 | printf("%s: Out of memory.\n", NAME);
|
---|
413 | return ENOMEM;
|
---|
414 | }
|
---|
415 |
|
---|
416 | cz_state->mstate = ms_start;
|
---|
417 | state->layout_priv = (void *) cz_state;
|
---|
418 |
|
---|
419 | return EOK;
|
---|
420 | }
|
---|
421 |
|
---|
422 | static void cz_destroy(layout_t *state)
|
---|
423 | {
|
---|
424 | free(state->layout_priv);
|
---|
425 | }
|
---|
426 |
|
---|
427 | static wchar_t cz_parse_ev(layout_t *state, kbd_event_t *ev)
|
---|
428 | {
|
---|
429 | layout_cz_t *cz_state = (layout_cz_t *) state->layout_priv;
|
---|
430 |
|
---|
431 | if (ev->type != KEY_PRESS)
|
---|
432 | return 0;
|
---|
433 |
|
---|
434 | if (key_is_mod(ev->key))
|
---|
435 | return 0;
|
---|
436 |
|
---|
437 | switch (cz_state->mstate) {
|
---|
438 | case ms_start:
|
---|
439 | return parse_ms_start(cz_state, ev);
|
---|
440 | case ms_hacek:
|
---|
441 | return parse_ms_hacek(cz_state, ev);
|
---|
442 | case ms_carka:
|
---|
443 | return parse_ms_carka(cz_state, ev);
|
---|
444 | }
|
---|
445 |
|
---|
446 | return 0;
|
---|
447 | }
|
---|
448 |
|
---|
449 | /**
|
---|
450 | * @}
|
---|
451 | */
|
---|