source: mainline/uspace/srv/hid/input/layout/us_qwerty.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: 5.0 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 US QWERTY layout
35 */
36
37#include <errno.h>
38#include <io/console.h>
39#include <io/keycode.h>
40#include "../layout.h"
41#include "../kbd.h"
42
43static errno_t us_qwerty_create(layout_t *);
44static void us_qwerty_destroy(layout_t *);
45static char32_t us_qwerty_parse_ev(layout_t *, kbd_event_t *ev);
46
47layout_ops_t us_qwerty_ops = {
48 .create = us_qwerty_create,
49 .destroy = us_qwerty_destroy,
50 .parse_ev = us_qwerty_parse_ev
51};
52
53static char32_t map_lcase[] = {
54 [KC_Q] = 'q',
55 [KC_W] = 'w',
56 [KC_E] = 'e',
57 [KC_R] = 'r',
58 [KC_T] = 't',
59 [KC_Y] = 'y',
60 [KC_U] = 'u',
61 [KC_I] = 'i',
62 [KC_O] = 'o',
63 [KC_P] = 'p',
64
65 [KC_A] = 'a',
66 [KC_S] = 's',
67 [KC_D] = 'd',
68 [KC_F] = 'f',
69 [KC_G] = 'g',
70 [KC_H] = 'h',
71 [KC_J] = 'j',
72 [KC_K] = 'k',
73 [KC_L] = 'l',
74
75 [KC_Z] = 'z',
76 [KC_X] = 'x',
77 [KC_C] = 'c',
78 [KC_V] = 'v',
79 [KC_B] = 'b',
80 [KC_N] = 'n',
81 [KC_M] = 'm',
82};
83
84static char32_t map_ucase[] = {
85 [KC_Q] = 'Q',
86 [KC_W] = 'W',
87 [KC_E] = 'E',
88 [KC_R] = 'R',
89 [KC_T] = 'T',
90 [KC_Y] = 'Y',
91 [KC_U] = 'U',
92 [KC_I] = 'I',
93 [KC_O] = 'O',
94 [KC_P] = 'P',
95
96 [KC_A] = 'A',
97 [KC_S] = 'S',
98 [KC_D] = 'D',
99 [KC_F] = 'F',
100 [KC_G] = 'G',
101 [KC_H] = 'H',
102 [KC_J] = 'J',
103 [KC_K] = 'K',
104 [KC_L] = 'L',
105
106 [KC_Z] = 'Z',
107 [KC_X] = 'X',
108 [KC_C] = 'C',
109 [KC_V] = 'V',
110 [KC_B] = 'B',
111 [KC_N] = 'N',
112 [KC_M] = 'M',
113};
114
115static char32_t map_not_shifted[] = {
116 [KC_BACKTICK] = '`',
117
118 [KC_1] = '1',
119 [KC_2] = '2',
120 [KC_3] = '3',
121 [KC_4] = '4',
122 [KC_5] = '5',
123 [KC_6] = '6',
124 [KC_7] = '7',
125 [KC_8] = '8',
126 [KC_9] = '9',
127 [KC_0] = '0',
128
129 [KC_MINUS] = '-',
130 [KC_EQUALS] = '=',
131
132 [KC_LBRACKET] = '[',
133 [KC_RBRACKET] = ']',
134
135 [KC_SEMICOLON] = ';',
136 [KC_QUOTE] = '\'',
137 [KC_BACKSLASH] = '\\',
138
139 [KC_COMMA] = ',',
140 [KC_PERIOD] = '.',
141 [KC_SLASH] = '/',
142};
143
144static char32_t map_shifted[] = {
145 [KC_BACKTICK] = '~',
146
147 [KC_1] = '!',
148 [KC_2] = '@',
149 [KC_3] = '#',
150 [KC_4] = '$',
151 [KC_5] = '%',
152 [KC_6] = '^',
153 [KC_7] = '&',
154 [KC_8] = '*',
155 [KC_9] = '(',
156 [KC_0] = ')',
157
158 [KC_MINUS] = '_',
159 [KC_EQUALS] = '+',
160
161 [KC_LBRACKET] = '{',
162 [KC_RBRACKET] = '}',
163
164 [KC_SEMICOLON] = ':',
165 [KC_QUOTE] = '"',
166 [KC_BACKSLASH] = '|',
167
168 [KC_COMMA] = '<',
169 [KC_PERIOD] = '>',
170 [KC_SLASH] = '?',
171};
172
173static char32_t map_neutral[] = {
174 [KC_BACKSPACE] = '\b',
175 [KC_TAB] = '\t',
176 [KC_ENTER] = '\n',
177 [KC_SPACE] = ' ',
178
179 [KC_NSLASH] = '/',
180 [KC_NTIMES] = '*',
181 [KC_NMINUS] = '-',
182 [KC_NPLUS] = '+',
183 [KC_NENTER] = '\n'
184};
185
186static char32_t map_numeric[] = {
187 [KC_N7] = '7',
188 [KC_N8] = '8',
189 [KC_N9] = '9',
190 [KC_N4] = '4',
191 [KC_N5] = '5',
192 [KC_N6] = '6',
193 [KC_N1] = '1',
194 [KC_N2] = '2',
195 [KC_N3] = '3',
196
197 [KC_N0] = '0',
198 [KC_NPERIOD] = '.'
199};
200
201static char32_t translate(unsigned int key, char32_t *map, size_t map_length)
202{
203 if (key >= map_length)
204 return 0;
205 return map[key];
206}
207
208static errno_t us_qwerty_create(layout_t *state)
209{
210 return EOK;
211}
212
213static void us_qwerty_destroy(layout_t *state)
214{
215}
216
217static char32_t us_qwerty_parse_ev(layout_t *state, kbd_event_t *ev)
218{
219 char32_t c;
220
221 c = translate(ev->key, map_neutral, sizeof(map_neutral) / sizeof(char32_t));
222 if (c != 0)
223 return c;
224
225 if (((ev->mods & KM_SHIFT) != 0) ^ ((ev->mods & KM_CAPS_LOCK) != 0))
226 c = translate(ev->key, map_ucase, sizeof(map_ucase) / sizeof(char32_t));
227 else
228 c = translate(ev->key, map_lcase, sizeof(map_lcase) / sizeof(char32_t));
229
230 if (c != 0)
231 return c;
232
233 if ((ev->mods & KM_SHIFT) != 0)
234 c = translate(ev->key, map_shifted, sizeof(map_shifted) / sizeof(char32_t));
235 else
236 c = translate(ev->key, map_not_shifted, sizeof(map_not_shifted) / sizeof(char32_t));
237
238 if (c != 0)
239 return c;
240
241 if ((ev->mods & KM_NUM_LOCK) != 0)
242 c = translate(ev->key, map_numeric, sizeof(map_numeric) / sizeof(char32_t));
243 else
244 c = 0;
245
246 return c;
247}
248
249/**
250 * @}
251 */
Note: See TracBrowser for help on using the repository browser.