1 /* $NetBSD: kbd.c,v 1.5 2021/09/03 16:21:15 tsutsui Exp $ */
2
3 /*
4 * Copyright (c) 1992 OMRON Corporation.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * OMRON Corporation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)kbd.c 8.1 (Berkeley) 6/10/93
38 */
39 /*
40 * Copyright (c) 1992, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * OMRON Corporation.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)kbd.c 8.1 (Berkeley) 6/10/93
71 */
72
73 /*
74 * kbd.c -- key-code decoding routine
75 * by A.Fujita, Dec-12-1992
76 */
77
78 #include <sys/param.h>
79 #include <luna68k/stand/boot/samachdep.h>
80 #include <luna68k/stand/boot/kbdreg.h>
81
82 static const struct kbd_keymap kbd_keymap[] = {
83 { KC_IGNORE, { 0, 0 } }, /* 0 [0x00] */
84 { KC_IGNORE, { 0, 0 } }, /* 1 [0x01] */
85 { KC_IGNORE, { 0, 0 } }, /* 2 [0x02] */
86 { KC_IGNORE, { 0, 0 } }, /* 3 [0x03] */
87 { KC_IGNORE, { 0, 0 } }, /* 4 [0x04] */
88 { KC_IGNORE, { 0, 0 } }, /* 5 [0x05] */
89 { KC_IGNORE, { 0, 0 } }, /* 6 [0x06] */
90 { KC_IGNORE, { 0, 0 } }, /* 7 [0x07] */
91 { KC_IGNORE, { 0, 0 } }, /* 8 [0x08] */
92 { KC_CODE, { 0x09, 0x09 } }, /* 9 [0x09] TAB */
93 { KC_SHIFT, { KS_CTRL, KS_CTRL } }, /* 10 [0x0A] CTRL */
94 { KC_IGNORE, { 0, 0 } }, /* 11 [0x0B] */
95 { KC_SHIFT, { KS_SHIFT, KS_SHIFT } }, /* 12 [0x0C] SHIFT */
96 { KC_SHIFT, { KS_SHIFT, KS_SHIFT } }, /* 13 [0x0D] SHIFT */
97 { KC_IGNORE, { 0, 0 } }, /* 14 [0x0E] */
98 { KC_SHIFT, { KS_META, KS_META } }, /* 15 [0x0F] META */
99 { KC_CODE, { 0x1B, 0x1B } }, /* 16 [0x10] ESC */
100 { KC_CODE, { 0x08, 0x08 } }, /* 17 [0x11] BS */
101 { KC_CODE, { 0x0D, 0x0D } }, /* 18 [0x12] CR */
102 { KC_IGNORE, { 0, 0 } }, /* 19 [0x13] */
103 { KC_CODE, { 0x20, 0x20 } }, /* 20 [0x14] SP */
104 { KC_CODE, { 0x7F, 0x7F } }, /* 21 [0x15] DEL */
105 { KC_IGNORE, { 0, 0 } }, /* 22 [0x16] */
106 { KC_IGNORE, { 0, 0 } }, /* 23 [0x17] */
107 { KC_IGNORE, { 0, 0 } }, /* 24 [0x18] */
108 { KC_IGNORE, { 0, 0 } }, /* 25 [0x19] */
109 { KC_IGNORE, { 0, 0 } }, /* 26 [0x1A] */
110 { KC_IGNORE, { 0, 0 } }, /* 27 [0x1B] */
111 { KC_IGNORE, { 0, 0 } }, /* 28 [0x1C] */
112 { KC_IGNORE, { 0, 0 } }, /* 29 [0x1D] */
113 { KC_IGNORE, { 0, 0 } }, /* 30 [0x1E] */
114 { KC_IGNORE, { 0, 0 } }, /* 31 [0x1F] */
115 { KC_IGNORE, { 0, 0 } }, /* 32 [0x20] */
116 { KC_IGNORE, { 0, 0 } }, /* 33 [0x21] */
117 { KC_CODE, { 0x31, 0x21 } }, /* 34 [0x22] 1 */
118 { KC_CODE, { 0x32, 0x22 } }, /* 35 [0x23] 2 */
119 { KC_CODE, { 0x33, 0x23 } }, /* 36 [0x24] 3 */
120 { KC_CODE, { 0x34, 0x24 } }, /* 37 [0x25] 4 */
121 { KC_CODE, { 0x35, 0x25 } }, /* 38 [0x26] 5 */
122 { KC_CODE, { 0x36, 0x26 } }, /* 39 [0x27] 6 */
123 { KC_CODE, { 0x37, 0x27 } }, /* 40 [0x28] 7 */
124 { KC_CODE, { 0x38, 0x28 } }, /* 41 [0x29] 8 */
125 { KC_CODE, { 0x39, 0x29 } }, /* 42 [0x2A] 9 */
126 { KC_CODE, { 0x30, 0x30 } }, /* 43 [0x2B] 0 */
127 { KC_CODE, { 0x2D, 0x3D } }, /* 44 [0x2C] - */
128 { KC_CODE, { 0x5E, 0x7E } }, /* 45 [0x2D] ^ */
129 { KC_CODE, { 0x5C, 0x7C } }, /* 46 [0x2E] \ */
130 { KC_IGNORE, { 0, 0 } }, /* 47 [0x2F] */
131 { KC_IGNORE, { 0, 0 } }, /* 48 [0x30] */
132 { KC_IGNORE, { 0, 0 } }, /* 49 [0x31] */
133 { KC_CODE, { 0x71, 0x51 } }, /* 50 [0x32] q */
134 { KC_CODE, { 0x77, 0x57 } }, /* 51 [0x33] w */
135 { KC_CODE, { 0x65, 0x45 } }, /* 52 [0x34] e */
136 { KC_CODE, { 0x72, 0x52 } }, /* 53 [0x35] r */
137 { KC_CODE, { 0x74, 0x54 } }, /* 54 [0x36] t */
138 { KC_CODE, { 0x79, 0x59 } }, /* 55 [0x37] y */
139 { KC_CODE, { 0x75, 0x55 } }, /* 56 [0x38] u */
140 { KC_CODE, { 0x69, 0x49 } }, /* 57 [0x39] i */
141 { KC_CODE, { 0x6F, 0x4F } }, /* 58 [0x3A] o */
142 { KC_CODE, { 0x70, 0x50 } }, /* 59 [0x3B] p */
143 { KC_CODE, { 0x40, 0x60 } }, /* 60 [0x3C] @ */
144 { KC_CODE, { 0x5B, 0x7B } }, /* 61 [0x3D] [ */
145 { KC_IGNORE, { 0, 0 } }, /* 62 [0x3E] */
146 { KC_IGNORE, { 0, 0 } }, /* 63 [0x3F] */
147 { KC_IGNORE, { 0, 0 } }, /* 64 [0x40] */
148 { KC_IGNORE, { 0, 0 } }, /* 65 [0x41] */
149 { KC_CODE, { 0x61, 0x41 } }, /* 66 [0x42] a */
150 { KC_CODE, { 0x73, 0x53 } }, /* 67 [0x43] s */
151 { KC_CODE, { 0x64, 0x44 } }, /* 68 [0x44] d */
152 { KC_CODE, { 0x66, 0x46 } }, /* 69 [0x45] f */
153 { KC_CODE, { 0x67, 0x47 } }, /* 70 [0x46] g */
154 { KC_CODE, { 0x68, 0x48 } }, /* 71 [0x47] h */
155 { KC_CODE, { 0x6A, 0x4A } }, /* 72 [0x48] j */
156 { KC_CODE, { 0x6B, 0x4B } }, /* 73 [0x49] k */
157 { KC_CODE, { 0x6C, 0x4C } }, /* 74 [0x4A] l */
158 { KC_CODE, { 0x3B, 0x2B } }, /* 75 [0x4B] ; */
159 { KC_CODE, { 0x3A, 0x2A } }, /* 76 [0x4C] : */
160 { KC_CODE, { 0x5D, 0x7D } }, /* 77 [0x4D] ] */
161 { KC_IGNORE, { 0, 0 } }, /* 78 [0x4E] */
162 { KC_IGNORE, { 0, 0 } }, /* 79 [0x4F] */
163 { KC_IGNORE, { 0, 0 } }, /* 80 [0x50] */
164 { KC_IGNORE, { 0, 0 } }, /* 81 [0x51] */
165 { KC_CODE, { 0x7A, 0x5A } }, /* 82 [0x52] z */
166 { KC_CODE, { 0x78, 0x58 } }, /* 83 [0x53] x */
167 { KC_CODE, { 0x63, 0x43 } }, /* 84 [0x54] c */
168 { KC_CODE, { 0x76, 0x56 } }, /* 85 [0x55] v */
169 { KC_CODE, { 0x62, 0x42 } }, /* 86 [0x56] b */
170 { KC_CODE, { 0x6E, 0x4E } }, /* 87 [0x57] n */
171 { KC_CODE, { 0x6D, 0x4D } }, /* 88 [0x58] m */
172 { KC_CODE, { 0x2C, 0x3C } }, /* 89 [0x59] , */
173 { KC_CODE, { 0x2E, 0x3E } }, /* 90 [0x5A] . */
174 { KC_CODE, { 0x2F, 0x3F } }, /* 91 [0x5B] / */
175 { KC_CODE, { 0x5F, 0x5F } }, /* 92 [0x5C] _ */
176 { KC_IGNORE, { 0, 0 } }, /* 93 [0x5D] */
177 { KC_IGNORE, { 0, 0 } }, /* 94 [0x5E] */
178 { KC_IGNORE, { 0, 0 } }, /* 95 [0x5F] */
179 { KC_IGNORE, { 0, 0 } }, /* 96 [0x60] */
180 { KC_IGNORE, { 0, 0 } }, /* 97 [0x61] */
181 { KC_IGNORE, { 0, 0 } }, /* 98 [0x62] */
182 { KC_IGNORE, { 0, 0 } }, /* 99 [0x63] */
183 { KC_IGNORE, { 0, 0 } }, /* 100 [0x64] */
184 { KC_IGNORE, { 0, 0 } }, /* 101 [0x65] */
185 { KC_IGNORE, { 0, 0 } }, /* 102 [0x66] */
186 { KC_IGNORE, { 0, 0 } }, /* 103 [0x67] */
187 { KC_IGNORE, { 0, 0 } }, /* 104 [0x68] */
188 { KC_IGNORE, { 0, 0 } }, /* 105 [0x69] */
189 { KC_IGNORE, { 0, 0 } }, /* 106 [0x6A] */
190 { KC_IGNORE, { 0, 0 } }, /* 107 [0x6B] */
191 { KC_IGNORE, { 0, 0 } }, /* 108 [0x6C] */
192 { KC_IGNORE, { 0, 0 } }, /* 109 [0x6D] */
193 { KC_IGNORE, { 0, 0 } }, /* 110 [0x6E] */
194 { KC_IGNORE, { 0, 0 } }, /* 111 [0x6F] */
195 { KC_IGNORE, { 0, 0 } }, /* 112 [0x70] */
196 { KC_IGNORE, { 0, 0 } }, /* 113 [0x71] */
197 { KC_IGNORE, { 0, 0 } }, /* 114 [0x72] */
198 { KC_IGNORE, { 0, 0 } }, /* 115 [0x73] */
199 { KC_IGNORE, { 0, 0 } }, /* 116 [0x74] */
200 { KC_IGNORE, { 0, 0 } }, /* 117 [0x75] */
201 { KC_IGNORE, { 0, 0 } }, /* 118 [0x76] */
202 { KC_IGNORE, { 0, 0 } }, /* 119 [0x77] */
203 { KC_IGNORE, { 0, 0 } }, /* 120 [0x78] */
204 { KC_IGNORE, { 0, 0 } }, /* 121 [0x79] */
205 { KC_IGNORE, { 0, 0 } }, /* 122 [0x7A] */
206 { KC_IGNORE, { 0, 0 } }, /* 123 [0x7B] */
207 { KC_IGNORE, { 0, 0 } }, /* 124 [0x7C] */
208 { KC_IGNORE, { 0, 0 } }, /* 125 [0x7D] */
209 { KC_IGNORE, { 0, 0 } }, /* 126 [0x7E] */
210 { KC_IGNORE, { 0, 0 } }, /* 127 [0x7F] */
211 };
212
213 int shift_flag = 0;
214 int ctrl_flag = 0;
215 int meta_flag = 0;
216 int mouse_flag = 0;
217
218 int
kbd_decode(uint8_t code)219 kbd_decode(uint8_t code)
220 {
221 unsigned int c, updown = 0;
222
223 /* ignore mouse data */
224 if (mouse_flag != 0) {
225 mouse_flag--;
226 return KC_IGNORE;
227 }
228 if ((code & 0xf8) == 0x80) {
229 mouse_flag = 2;
230 return KC_IGNORE;
231 }
232
233 if (code & 0x80)
234 updown = 1;
235
236 code &= 0x7F;
237
238 c = kbd_keymap[code].km_type;
239
240 if (c == KC_IGNORE)
241 return KC_IGNORE;
242
243 if ((c == KC_CODE) && updown)
244 return KC_IGNORE;
245
246 if (c == KC_SHIFT) {
247 switch (kbd_keymap[code].km_code[0]) {
248
249 case KS_SHIFT:
250 shift_flag = 1 - updown;
251 break;
252
253 case KS_CTRL:
254 ctrl_flag = 1 - updown;
255 break;
256
257 case KS_META:
258 meta_flag = 1 - updown;
259 break;
260 }
261
262 return KC_IGNORE;
263 }
264
265 if (shift_flag)
266 c = kbd_keymap[code].km_code[1];
267 else
268 c = kbd_keymap[code].km_code[0];
269
270 if (meta_flag)
271 c |= 0x80;
272
273 if (ctrl_flag)
274 c &= 0x1F;
275
276 return c;
277 }
278