source: mainline/uspace/srv/hid/input/layout/us_qwerty.c@ 2f7a564

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2f7a564 was 2f7a564, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Make key lock/modifier state as well as layout state per-device.

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