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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 453c5ce was 4122410, checked in by Jakub Jermar <jakub@…>, 7 years ago

Improve Doxygen documentaion

This is stil WiP. A number of libraries, drivers and services were
converted to using a more hierarchical and decentralized scheme when it
comes to specifying to which doxygen group they belong.

  • Property mode set to 100644
File size: 8.3 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 wchar_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 wchar_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 wchar_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 wchar_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 wchar_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 wchar_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 wchar_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 wchar_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 wchar_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 wchar_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 wchar_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 wchar_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 wchar_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 wchar_t translate(unsigned int key, wchar_t *map, size_t map_length)
278{
279 if (key >= map_length)
280 return 0;
281 return map[key];
282}
283
284static wchar_t parse_ms_hacek(layout_cz_t *cz_state, kbd_event_t *ev)
285{
286 wchar_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(wchar_t));
296 else
297 c = translate(ev->key, map_hacek_lcase, sizeof(map_hacek_lcase) / sizeof(wchar_t));
298
299 return c;
300}
301
302static wchar_t parse_ms_carka(layout_cz_t *cz_state, kbd_event_t *ev)
303{
304 wchar_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(wchar_t));
314 else
315 c = translate(ev->key, map_carka_lcase, sizeof(map_carka_lcase) / sizeof(wchar_t));
316
317 return c;
318}
319
320static wchar_t parse_ms_start(layout_cz_t *cz_state, kbd_event_t *ev)
321{
322 wchar_t c;
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->key == KC_EQUALS) {
329 if ((ev->mods & KM_SHIFT) != 0)
330 cz_state->mstate = ms_hacek;
331 else
332 cz_state->mstate = ms_carka;
333
334 return 0;
335 }
336
337 c = translate(ev->key, map_neutral, sizeof(map_neutral) / sizeof(wchar_t));
338 if (c != 0)
339 return c;
340
341 if ((ev->mods & KM_SHIFT) == 0) {
342 if ((ev->mods & KM_CAPS_LOCK) != 0)
343 c = translate(ev->key, map_ns_caps, sizeof(map_ns_caps) / sizeof(wchar_t));
344 else
345 c = translate(ev->key, map_ns_nocaps, sizeof(map_ns_nocaps) / sizeof(wchar_t));
346
347 if (c != 0)
348 return c;
349 }
350
351 if (((ev->mods & KM_SHIFT) != 0) ^ ((ev->mods & KM_CAPS_LOCK) != 0))
352 c = translate(ev->key, map_ucase, sizeof(map_ucase) / sizeof(wchar_t));
353 else
354 c = translate(ev->key, map_lcase, sizeof(map_lcase) / sizeof(wchar_t));
355
356 if (c != 0)
357 return c;
358
359 if ((ev->mods & KM_SHIFT) != 0)
360 c = translate(ev->key, map_shifted, sizeof(map_shifted) / sizeof(wchar_t));
361 else
362 c = translate(ev->key, map_not_shifted, sizeof(map_not_shifted) / sizeof(wchar_t));
363
364 if (c != 0)
365 return c;
366
367 if ((ev->mods & KM_NUM_LOCK) != 0)
368 c = translate(ev->key, map_numeric, sizeof(map_numeric) / sizeof(wchar_t));
369 else
370 c = 0;
371
372 return c;
373}
374
375static bool key_is_mod(unsigned key)
376{
377 switch (key) {
378 case KC_LSHIFT:
379 case KC_RSHIFT:
380 case KC_LALT:
381 case KC_RALT:
382 case KC_LCTRL:
383 case KC_RCTRL:
384 return true;
385 default:
386 return false;
387 }
388}
389
390static errno_t cz_create(layout_t *state)
391{
392 layout_cz_t *cz_state;
393
394 cz_state = malloc(sizeof(layout_cz_t));
395 if (cz_state == NULL) {
396 printf("%s: Out of memory.\n", NAME);
397 return ENOMEM;
398 }
399
400 cz_state->mstate = ms_start;
401 state->layout_priv = (void *) cz_state;
402
403 return EOK;
404}
405
406static void cz_destroy(layout_t *state)
407{
408 free(state->layout_priv);
409}
410
411static wchar_t cz_parse_ev(layout_t *state, kbd_event_t *ev)
412{
413 layout_cz_t *cz_state = (layout_cz_t *) state->layout_priv;
414
415 if (ev->type != KEY_PRESS)
416 return 0;
417
418 if (key_is_mod(ev->key))
419 return 0;
420
421 switch (cz_state->mstate) {
422 case ms_start:
423 return parse_ms_start(cz_state, ev);
424 case ms_hacek:
425 return parse_ms_hacek(cz_state, ev);
426 case ms_carka:
427 return parse_ms_carka(cz_state, ev);
428 }
429
430 return 0;
431}
432
433/**
434 * @}
435 */
Note: See TracBrowser for help on using the repository browser.