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
RevLine 
[cee8d3e]1/*
[2f7a564]2 * Copyright (c) 2011 Jiri Svoboda
[cee8d3e]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
[5f88293]29/** @addtogroup input
[cee8d3e]30 * @{
[215abc1]31 */
[cee8d3e]32
[4122410]33/** @file
34 * @brief Czech QWERTZ layout
35 */
36
[2f7a564]37#include <errno.h>
[215abc1]38#include <io/console.h>
39#include <io/keycode.h>
[3e6a98c5]40#include <stdbool.h>
[2f7a564]41#include <stdlib.h>
[b6a088f]42#include "../input.h"
43#include "../layout.h"
[cee8d3e]44
[b7fd2a0]45static errno_t cz_create(layout_t *);
[2f7a564]46static void cz_destroy(layout_t *);
[28a5ebd]47static char32_t cz_parse_ev(layout_t *, kbd_event_t *ev);
[0175246]48
[d15815e2]49enum m_state {
50 ms_start,
51 ms_hacek,
[215abc1]52 ms_carka
[d15815e2]53};
54
[2f7a564]55typedef struct {
56 enum m_state mstate;
57} layout_cz_t;
[d15815e2]58
[2f7a564]59layout_ops_t cz_ops = {
60 .create = cz_create,
61 .destroy = cz_destroy,
62 .parse_ev = cz_parse_ev
[0175246]63};
64
[28a5ebd]65static char32_t map_lcase[] = {
[cee8d3e]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
[28a5ebd]96static char32_t map_ucase[] = {
[cee8d3e]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
[28a5ebd]127static char32_t map_not_shifted[] = {
[cee8d3e]128 [KC_BACKTICK] = ';',
129
130 [KC_1] = '+',
131
132 [KC_MINUS] = '=',
133
134 [KC_RBRACKET] = ')',
135
[56fa418]136 [KC_QUOTE] = L'§',
[cee8d3e]137
138 [KC_COMMA] = ',',
139 [KC_PERIOD] = '.',
140 [KC_SLASH] = '-',
141};
142
[28a5ebd]143static char32_t map_shifted[] = {
[cee8d3e]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
[28a5ebd]169static char32_t map_ns_nocaps[] = {
[fe24a52]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
[28a5ebd]184static char32_t map_ns_caps[] = {
[fe24a52]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
[28a5ebd]199static char32_t map_neutral[] = {
[cee8d3e]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
[28a5ebd]212static char32_t map_numeric[] = {
[cee8d3e]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
[28a5ebd]227static char32_t map_hacek_lcase[] = {
[d15815e2]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
[28a5ebd]241static char32_t map_hacek_ucase[] = {
[d15815e2]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
[28a5ebd]255static char32_t map_carka_lcase[] = {
[d15815e2]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
[28a5ebd]266static char32_t map_carka_ucase[] = {
[d15815e2]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
[28a5ebd]277static char32_t translate(unsigned int key, char32_t *map, size_t map_length)
[cee8d3e]278{
279 if (key >= map_length)
280 return 0;
281 return map[key];
282}
283
[28a5ebd]284static char32_t parse_ms_hacek(layout_cz_t *cz_state, kbd_event_t *ev)
[cee8d3e]285{
[28a5ebd]286 char32_t c;
[cee8d3e]287
[2f7a564]288 cz_state->mstate = ms_start;
[d15815e2]289
[cee8d3e]290 /* Produce no characters when Ctrl or Alt is pressed. */
291 if ((ev->mods & (KM_CTRL | KM_ALT)) != 0)
292 return 0;
293
[d15815e2]294 if (((ev->mods & KM_SHIFT) != 0) ^ ((ev->mods & KM_CAPS_LOCK) != 0))
[28a5ebd]295 c = translate(ev->key, map_hacek_ucase, sizeof(map_hacek_ucase) / sizeof(char32_t));
[d15815e2]296 else
[28a5ebd]297 c = translate(ev->key, map_hacek_lcase, sizeof(map_hacek_lcase) / sizeof(char32_t));
[d15815e2]298
299 return c;
300}
301
[28a5ebd]302static char32_t parse_ms_carka(layout_cz_t *cz_state, kbd_event_t *ev)
[d15815e2]303{
[28a5ebd]304 char32_t c;
[d15815e2]305
[2f7a564]306 cz_state->mstate = ms_start;
[d15815e2]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))
[28a5ebd]313 c = translate(ev->key, map_carka_ucase, sizeof(map_carka_ucase) / sizeof(char32_t));
[d15815e2]314 else
[28a5ebd]315 c = translate(ev->key, map_carka_lcase, sizeof(map_carka_lcase) / sizeof(char32_t));
[d15815e2]316
317 return c;
318}
319
[28a5ebd]320static char32_t parse_ms_start(layout_cz_t *cz_state, kbd_event_t *ev)
[d15815e2]321{
[28a5ebd]322 char32_t c;
[d15815e2]323
324 if (ev->key == KC_EQUALS) {
325 if ((ev->mods & KM_SHIFT) != 0)
[2f7a564]326 cz_state->mstate = ms_hacek;
[d15815e2]327 else
[2f7a564]328 cz_state->mstate = ms_carka;
[d15815e2]329
330 return 0;
331 }
332
[28a5ebd]333 c = translate(ev->key, map_neutral, sizeof(map_neutral) / sizeof(char32_t));
[cee8d3e]334 if (c != 0)
335 return c;
336
[fe24a52]337 if ((ev->mods & KM_SHIFT) == 0) {
338 if ((ev->mods & KM_CAPS_LOCK) != 0)
[28a5ebd]339 c = translate(ev->key, map_ns_caps, sizeof(map_ns_caps) / sizeof(char32_t));
[fe24a52]340 else
[28a5ebd]341 c = translate(ev->key, map_ns_nocaps, sizeof(map_ns_nocaps) / sizeof(char32_t));
[fe24a52]342
343 if (c != 0)
344 return c;
[1b20da0]345 }
[fe24a52]346
[cee8d3e]347 if (((ev->mods & KM_SHIFT) != 0) ^ ((ev->mods & KM_CAPS_LOCK) != 0))
[28a5ebd]348 c = translate(ev->key, map_ucase, sizeof(map_ucase) / sizeof(char32_t));
[cee8d3e]349 else
[28a5ebd]350 c = translate(ev->key, map_lcase, sizeof(map_lcase) / sizeof(char32_t));
[cee8d3e]351
352 if (c != 0)
353 return c;
354
355 if ((ev->mods & KM_SHIFT) != 0)
[28a5ebd]356 c = translate(ev->key, map_shifted, sizeof(map_shifted) / sizeof(char32_t));
[cee8d3e]357 else
[28a5ebd]358 c = translate(ev->key, map_not_shifted, sizeof(map_not_shifted) / sizeof(char32_t));
[cee8d3e]359
360 if (c != 0)
361 return c;
362
363 if ((ev->mods & KM_NUM_LOCK) != 0)
[28a5ebd]364 c = translate(ev->key, map_numeric, sizeof(map_numeric) / sizeof(char32_t));
[cee8d3e]365 else
366 c = 0;
367
368 return c;
369}
370
[d15815e2]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
[b7fd2a0]386static errno_t cz_create(layout_t *state)
[d15815e2]387{
[2f7a564]388 layout_cz_t *cz_state;
389
390 cz_state = malloc(sizeof(layout_cz_t));
391 if (cz_state == NULL) {
[1875a0c]392 printf("%s: Out of memory.\n", NAME);
[2f7a564]393 return ENOMEM;
394 }
395
396 cz_state->mstate = ms_start;
397 state->layout_priv = (void *) cz_state;
398
399 return EOK;
[d15815e2]400}
401
[2f7a564]402static void cz_destroy(layout_t *state)
[d15815e2]403{
[2f7a564]404 free(state->layout_priv);
405}
406
[28a5ebd]407static char32_t cz_parse_ev(layout_t *state, kbd_event_t *ev)
[2f7a564]408{
409 layout_cz_t *cz_state = (layout_cz_t *) state->layout_priv;
410
[215abc1]411 if (ev->type != KEY_PRESS)
412 return 0;
[a35b458]413
[d15815e2]414 if (key_is_mod(ev->key))
[215abc1]415 return 0;
[a35b458]416
[2f7a564]417 switch (cz_state->mstate) {
[215abc1]418 case ms_start:
[2f7a564]419 return parse_ms_start(cz_state, ev);
[215abc1]420 case ms_hacek:
[2f7a564]421 return parse_ms_hacek(cz_state, ev);
[215abc1]422 case ms_carka:
[2f7a564]423 return parse_ms_carka(cz_state, ev);
[d15815e2]424 }
[a35b458]425
[f954906]426 return 0;
[d15815e2]427}
428
[cee8d3e]429/**
430 * @}
[215abc1]431 */
Note: See TracBrowser for help on using the repository browser.