1 /**	$MirOS: src/sys/dev/wscons/wskbdutil.c,v 1.2 2009/11/24 17:25:09 tg Exp $ */
2 /*	$OpenBSD: wskbdutil.c,v 1.6 2006/12/17 22:04:04 miod Exp $	*/
3 /*	$NetBSD: wskbdutil.c,v 1.7 1999/12/21 11:59:13 drochner Exp $	*/
4 
5 /*-
6  * Copyright (c) 1997 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Juergen Hannken-Illjes.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the NetBSD
23  *	Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/cdefs.h>
44 #include <sys/errno.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 #include <dev/wscons/wsksymdef.h>
48 #include <dev/wscons/wsksymvar.h>
49 
50 static struct compose_tab_s {
51 	keysym_t elem[2];
52 	keysym_t result;
53 } compose_tab[] = {
54 	{ { KS_plus,			KS_plus },		KS_numbersign },
55 	{ { KS_a,			KS_a },			KS_at },
56 	{ { KS_parenleft,		KS_parenleft },		KS_bracketleft },
57 	{ { KS_slash,			KS_slash },		KS_backslash },
58 	{ { KS_parenright,		KS_parenright },	KS_bracketright },
59 	{ { KS_parenleft,		KS_minus },		KS_braceleft },
60 	{ { KS_slash,			KS_minus },		KS_bar },
61 	{ { KS_parenright,		KS_minus },		KS_braceright },
62 	{ { KS_exclam,			KS_exclam },		KS_exclamdown },
63 	{ { KS_1,			KS_4 },			KS_onequarter },
64 	{ { KS_1,			KS_2 },			KS_onehalf },
65 	{ { KS_3,			KS_4 },			KS_threequarters },
66 	{ { KS_c,			KS_slash },		KS_cent },
67 	{ { KS_l,			KS_minus },		KS_sterling },
68 	{ { KS_y,			KS_minus },		KS_yen },
69 	{ { KS_s,			KS_o },			KS_section },
70 	{ { KS_x,			KS_o },			KS_currency },
71 	{ { KS_c,			KS_o },			KS_copyright },
72 	{ { KS_o,			KS_c },			KS_copyright },
73 	{ { KS_o,			KS_r },			KS_registered },
74 	{ { KS_p,			KS_exclam },		KS_paragraph },
75 	{ { KS_m,			KS_u },			KS_mu },
76 	{ { KS_x,			KS_x },			KS_multiply },
77 	{ { KS_colon,			KS_minus },		KS_division },
78 	{ { KS_period,			KS_period },		KS_periodcentered },
79 	{ { KS_less,			KS_less },		KS_guillemotleft },
80 	{ { KS_greater,			KS_greater },		KS_guillemotright },
81 	{ { KS_question,		KS_question },		KS_questiondown },
82 	{ { KS_dead_acute,		KS_space },		KS_apostrophe },
83 	{ { KS_dead_grave,		KS_space },		KS_grave },
84 	{ { KS_dead_tilde,		KS_space },		KS_asciitilde },
85 	{ { KS_dead_circumflex,		KS_space },		KS_asciicircum },
86 	{ { KS_dead_diaeresis,		KS_space },		KS_quotedbl },
87 	{ { KS_dead_cedilla,		KS_space },		KS_comma },
88 	{ { KS_underscore,		KS_underscore },	KS_macron },
89 	{ { KS_dead_acute,		KS_A },			KS_Aacute },
90 	{ { KS_dead_circumflex,		KS_A },			KS_Acircumflex },
91 	{ { KS_dead_diaeresis,		KS_A },			KS_Adiaeresis },
92 	{ { KS_dead_grave,		KS_A },			KS_Agrave },
93 	{ { KS_dead_abovering,		KS_A },			KS_Aring },
94 	{ { KS_dead_tilde,		KS_A },			KS_Atilde },
95 	{ { KS_dead_cedilla,		KS_C },			KS_Ccedilla },
96 	{ { KS_dead_acute,		KS_E },			KS_Eacute },
97 	{ { KS_dead_circumflex,		KS_E },			KS_Ecircumflex },
98 	{ { KS_dead_diaeresis,		KS_E },			KS_Ediaeresis },
99 	{ { KS_dead_grave,		KS_E },			KS_Egrave },
100 	{ { KS_dead_acute,		KS_I },			KS_Iacute },
101 	{ { KS_dead_circumflex,		KS_I },			KS_Icircumflex },
102 	{ { KS_dead_diaeresis,		KS_I },			KS_Idiaeresis },
103 	{ { KS_dead_grave,		KS_I },			KS_Igrave },
104 	{ { KS_dead_tilde,		KS_N },			KS_Ntilde },
105 	{ { KS_dead_acute,		KS_O },			KS_Oacute },
106 	{ { KS_dead_circumflex,		KS_O },			KS_Ocircumflex },
107 	{ { KS_dead_diaeresis,		KS_O },			KS_Odiaeresis },
108 	{ { KS_dead_grave,		KS_O },			KS_Ograve },
109 	{ { KS_dead_tilde,		KS_O },			KS_Otilde },
110 	{ { KS_dead_acute,		KS_U },			KS_Uacute },
111 	{ { KS_dead_circumflex,		KS_U },			KS_Ucircumflex },
112 	{ { KS_dead_diaeresis,		KS_U },			KS_Udiaeresis },
113 	{ { KS_dead_grave,		KS_U },			KS_Ugrave },
114 	{ { KS_dead_acute,		KS_Y },			KS_Yacute },
115 	{ { KS_dead_acute,		KS_a },			KS_aacute },
116 	{ { KS_dead_circumflex,		KS_a },			KS_acircumflex },
117 	{ { KS_dead_diaeresis,		KS_a },			KS_adiaeresis },
118 	{ { KS_dead_grave,		KS_a },			KS_agrave },
119 	{ { KS_dead_abovering,		KS_a },			KS_aring },
120 	{ { KS_dead_tilde,		KS_a },			KS_atilde },
121 	{ { KS_dead_cedilla,		KS_c },			KS_ccedilla },
122 	{ { KS_dead_acute,		KS_e },			KS_eacute },
123 	{ { KS_dead_circumflex,		KS_e },			KS_ecircumflex },
124 	{ { KS_dead_diaeresis,		KS_e },			KS_ediaeresis },
125 	{ { KS_dead_grave,		KS_e },			KS_egrave },
126 	{ { KS_dead_acute,		KS_i },			KS_iacute },
127 	{ { KS_dead_circumflex,		KS_i },			KS_icircumflex },
128 	{ { KS_dead_diaeresis,		KS_i },			KS_idiaeresis },
129 	{ { KS_dead_grave,		KS_i },			KS_igrave },
130 	{ { KS_dead_tilde,		KS_n },			KS_ntilde },
131 	{ { KS_dead_acute,		KS_o },			KS_oacute },
132 	{ { KS_dead_circumflex,		KS_o },			KS_ocircumflex },
133 	{ { KS_dead_diaeresis,		KS_o },			KS_odiaeresis },
134 	{ { KS_dead_grave,		KS_o },			KS_ograve },
135 	{ { KS_dead_tilde,		KS_o },			KS_otilde },
136 	{ { KS_dead_acute,		KS_u },			KS_uacute },
137 	{ { KS_dead_circumflex,		KS_u },			KS_ucircumflex },
138 	{ { KS_dead_diaeresis,		KS_u },			KS_udiaeresis },
139 	{ { KS_dead_grave,		KS_u },			KS_ugrave },
140 	{ { KS_dead_acute,		KS_y },			KS_yacute },
141 	{ { KS_dead_diaeresis,		KS_y },			KS_ydiaeresis },
142 	{ { KS_quotedbl,		KS_A },			KS_Adiaeresis },
143 	{ { KS_quotedbl,		KS_E },			KS_Ediaeresis },
144 	{ { KS_quotedbl,		KS_I },			KS_Idiaeresis },
145 	{ { KS_quotedbl,		KS_O },			KS_Odiaeresis },
146 	{ { KS_quotedbl,		KS_U },			KS_Udiaeresis },
147 	{ { KS_quotedbl,		KS_a },			KS_adiaeresis },
148 	{ { KS_quotedbl,		KS_e },			KS_ediaeresis },
149 	{ { KS_quotedbl,		KS_i },			KS_idiaeresis },
150 	{ { KS_quotedbl,		KS_o },			KS_odiaeresis },
151 	{ { KS_quotedbl,		KS_u },			KS_udiaeresis },
152 	{ { KS_quotedbl,		KS_y },			KS_ydiaeresis },
153 	{ { KS_acute,			KS_A },			KS_Aacute },
154 	{ { KS_apostrophe,		KS_A },			KS_Aacute },
155 	{ { KS_asciicircum,		KS_A },			KS_Acircumflex },
156 	{ { KS_grave,			KS_A },			KS_Agrave },
157 	{ { KS_asterisk,		KS_A },			KS_Aring },
158 	{ { KS_asciitilde,		KS_A },			KS_Atilde },
159 	{ { KS_cedilla,			KS_C },			KS_Ccedilla },
160 	{ { KS_acute,			KS_E },			KS_Eacute },
161 	{ { KS_apostrophe,		KS_E },			KS_Eacute },
162 	{ { KS_asciicircum,		KS_E },			KS_Ecircumflex },
163 	{ { KS_grave,			KS_E },			KS_Egrave },
164 	{ { KS_acute,			KS_I },			KS_Iacute },
165 	{ { KS_apostrophe,		KS_I },			KS_Iacute },
166 	{ { KS_asciicircum,		KS_I },			KS_Icircumflex },
167 	{ { KS_grave,			KS_I },			KS_Igrave },
168 	{ { KS_asciitilde,		KS_N },			KS_Ntilde },
169 	{ { KS_acute,			KS_O },			KS_Oacute },
170 	{ { KS_apostrophe,		KS_O },			KS_Oacute },
171 	{ { KS_asciicircum,		KS_O },			KS_Ocircumflex },
172 	{ { KS_grave,			KS_O },			KS_Ograve },
173 	{ { KS_asciitilde,		KS_O },			KS_Otilde },
174 	{ { KS_acute,			KS_U },			KS_Uacute },
175 	{ { KS_apostrophe,		KS_U },			KS_Uacute },
176 	{ { KS_asciicircum,		KS_U },			KS_Ucircumflex },
177 	{ { KS_grave,			KS_U },			KS_Ugrave },
178 	{ { KS_acute,			KS_Y },			KS_Yacute },
179 	{ { KS_apostrophe,		KS_Y },			KS_Yacute },
180 	{ { KS_acute,			KS_a },			KS_aacute },
181 	{ { KS_apostrophe,		KS_a },			KS_aacute },
182 	{ { KS_asciicircum,		KS_a },			KS_acircumflex },
183 	{ { KS_grave,			KS_a },			KS_agrave },
184 	{ { KS_asterisk,		KS_a },			KS_aring },
185 	{ { KS_asciitilde,		KS_a },			KS_atilde },
186 	{ { KS_cedilla,			KS_c },			KS_ccedilla },
187 	{ { KS_acute,			KS_e },			KS_eacute },
188 	{ { KS_apostrophe,		KS_e },			KS_eacute },
189 	{ { KS_asciicircum,		KS_e },			KS_ecircumflex },
190 	{ { KS_grave,			KS_e },			KS_egrave },
191 	{ { KS_acute,			KS_i },			KS_iacute },
192 	{ { KS_apostrophe,		KS_i },			KS_iacute },
193 	{ { KS_asciicircum,		KS_i },			KS_icircumflex },
194 	{ { KS_grave,			KS_i },			KS_igrave },
195 	{ { KS_asciitilde,		KS_n },			KS_ntilde },
196 	{ { KS_acute,			KS_o },			KS_oacute },
197 	{ { KS_apostrophe,		KS_o },			KS_oacute },
198 	{ { KS_asciicircum,		KS_o },			KS_ocircumflex },
199 	{ { KS_grave,			KS_o },			KS_ograve },
200 	{ { KS_asciitilde,		KS_o },			KS_otilde },
201 	{ { KS_acute,			KS_u },			KS_uacute },
202 	{ { KS_apostrophe,		KS_u },			KS_uacute },
203 	{ { KS_asciicircum,		KS_u },			KS_ucircumflex },
204 	{ { KS_grave,			KS_u },			KS_ugrave },
205 	{ { KS_acute,			KS_y },			KS_yacute },
206 	{ { KS_apostrophe,		KS_y },			KS_yacute }
207 };
208 
209 #define COMPOSE_SIZE	sizeof(compose_tab)/sizeof(compose_tab[0])
210 
211 static int compose_tab_inorder = 0;
212 
213 inline int compose_tab_cmp(struct compose_tab_s *,
214 				struct compose_tab_s *);
215 keysym_t ksym_upcase(keysym_t);
216 void fillmapentry(const keysym_t *, int, struct wscons_keymap *);
217 
218 inline int
compose_tab_cmp(i,j)219 compose_tab_cmp(i, j)
220 	struct compose_tab_s *i, *j;
221 {
222 	if (i->elem[0] == j->elem[0])
223 		return(i->elem[1] - j->elem[1]);
224 	else
225 		return(i->elem[0] - j->elem[0]);
226 }
227 
228 keysym_t
wskbd_compose_value(compose_buf)229 wskbd_compose_value(compose_buf)
230 	keysym_t *compose_buf;
231 {
232 	int i, j, r;
233 	struct compose_tab_s v;
234 
235 	if (! compose_tab_inorder) {
236 		/* Insertion sort. */
237 		for (i = 1; i < COMPOSE_SIZE; i++) {
238 			v = compose_tab[i];
239 			/* find correct slot, moving others up */
240 			for (j = i; --j >= 0 && compose_tab_cmp(& v, & compose_tab[j]) < 0; )
241 				compose_tab[j + 1] = compose_tab[j];
242 			compose_tab[j + 1] = v;
243 		}
244 		compose_tab_inorder = 1;
245 	}
246 
247 	for (j = 0, i = COMPOSE_SIZE; i != 0; i /= 2) {
248 		if (compose_tab[j + i/2].elem[0] == compose_buf[0]) {
249 			if (compose_tab[j + i/2].elem[1] == compose_buf[1])
250 				return(compose_tab[j + i/2].result);
251 			r = compose_tab[j + i/2].elem[1] < compose_buf[1];
252 		} else
253 			r = compose_tab[j + i/2].elem[0] < compose_buf[0];
254 		if (r) {
255 			j += i/2 + 1;
256 			i--;
257 		}
258 	}
259 
260 	return(KS_voidSymbol);
261 }
262 
263 static const u_char latin1_to_upper[256] = {
264 /*      0  8  1  9  2  a  3  b  4  c  5  d  6  e  7  f               */
265 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
266 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
267 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
268 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
269 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
270 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
271 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
272 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
273 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
274 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
275 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
276 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
277 	0x00,  'A',  'B',  'C',  'D',  'E',  'F',  'G',		/* 6 */
278 	 'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',		/* 6 */
279 	 'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',		/* 7 */
280 	 'X',  'Y',  'Z', 0x00, 0x00, 0x00, 0x00, 0x00,		/* 7 */
281 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
282 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
283 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
284 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
285 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
286 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
287 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
288 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
289 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
290 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
291 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
292 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
293 	0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,		/* e */
294 	0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,		/* e */
295 	0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0x00,		/* f */
296 	0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0x00,		/* f */
297 };
298 
299 keysym_t
ksym_upcase(ksym)300 ksym_upcase(ksym)
301 	keysym_t ksym;
302 {
303 	if (ksym >= KS_f1 && ksym <= KS_f20)
304 		return(KS_F1 - KS_f1 + ksym);
305 
306 	if (KS_GROUP(ksym) == KS_GROUP_Ascii && ksym <= 0xff &&
307 	    latin1_to_upper[ksym] != 0x00)
308 		return(latin1_to_upper[ksym]);
309 
310 	return(ksym);
311 }
312 
313 void
fillmapentry(kp,len,mapentry)314 fillmapentry(kp, len, mapentry)
315 	const keysym_t *kp;
316 	int len;
317 	struct wscons_keymap *mapentry;
318 {
319 	switch (len) {
320 	case 0:
321 		mapentry->group1[0] = KS_voidSymbol;
322 		mapentry->group1[1] = KS_voidSymbol;
323 		mapentry->group2[0] = KS_voidSymbol;
324 		mapentry->group2[1] = KS_voidSymbol;
325 		break;
326 
327 	case 1:
328 		mapentry->group1[0] = kp[0];
329 		mapentry->group1[1] = ksym_upcase(kp[0]);
330 		mapentry->group2[0] = mapentry->group1[0];
331 		mapentry->group2[1] = mapentry->group1[1];
332 		break;
333 
334 	case 2:
335 		mapentry->group1[0] = kp[0];
336 		mapentry->group1[1] = kp[1];
337 		mapentry->group2[0] = mapentry->group1[0];
338 		mapentry->group2[1] = mapentry->group1[1];
339 		break;
340 
341 	case 3:
342 		mapentry->group1[0] = kp[0];
343 		mapentry->group1[1] = kp[1];
344 		mapentry->group2[0] = kp[2];
345 		mapentry->group2[1] = ksym_upcase(kp[2]);
346 		break;
347 
348 	case 4:
349 		mapentry->group1[0] = kp[0];
350 		mapentry->group1[1] = kp[1];
351 		mapentry->group2[0] = kp[2];
352 		mapentry->group2[1] = kp[3];
353 		break;
354 
355 	}
356 }
357 
358 void
wskbd_get_mapentry(mapdata,kc,mapentry)359 wskbd_get_mapentry(mapdata, kc, mapentry)
360 	const struct wskbd_mapdata *mapdata;
361 	int kc;
362 	struct wscons_keymap *mapentry;
363 {
364 	kbd_t cur;
365 	const keysym_t *kp;
366 	const struct wscons_keydesc *mp;
367 	int l;
368 	keysym_t ksg;
369 
370 	mapentry->command = KS_voidSymbol;
371 	mapentry->group1[0] = KS_voidSymbol;
372 	mapentry->group1[1] = KS_voidSymbol;
373 	mapentry->group2[0] = KS_voidSymbol;
374 	mapentry->group2[1] = KS_voidSymbol;
375 
376 	for (cur = mapdata->layout & ~KB_HANDLEDBYWSKBD; cur != 0; ) {
377 		mp = mapdata->keydesc;
378 		while (mp->map_size > 0) {
379 			if (mp->name == cur)
380 				break;
381 			mp++;
382 		}
383 
384 		/* If map not found, return */
385 		if (mp->map_size <= 0)
386 			return;
387 
388 		for (kp = mp->map; kp < mp->map + mp->map_size; kp++) {
389 			ksg = KS_GROUP(*kp);
390 			if (ksg == KS_GROUP_Keycode &&
391 			    KS_VALUE(*kp) == kc) {
392 				/* First skip keycode and possible command */
393 				kp++;
394 				if (KS_GROUP(*kp) == KS_GROUP_Command ||
395 				    *kp == KS_Cmd || *kp == KS_Cmd1 || *kp == KS_Cmd2)
396 					mapentry->command = *kp++;
397 
398 				for (l = 0; kp + l < mp->map + mp->map_size;
399 				    l++) {
400 					ksg = KS_GROUP(kp[l]);
401 					if (ksg == KS_GROUP_Keycode)
402 						break;
403 				}
404 				if (l > 4)
405 					panic("wskbd_get_mapentry: %d(%d): bad entry",
406 					      mp->name, *kp);
407 				fillmapentry(kp, l, mapentry);
408 				return;
409 			}
410 		}
411 
412 		cur = mp->base;
413 	}
414 }
415 
416 void
wskbd_init_keymap(newlen,map,maplen)417 wskbd_init_keymap(newlen, map, maplen)
418 	int newlen;
419 	struct wscons_keymap **map;
420 	int *maplen;
421 {
422 	int i;
423 
424 	if (newlen != *maplen) {
425 		if (*maplen > 0)
426 			free(*map, M_TEMP);
427 		*maplen = newlen;
428 		*map = malloc(newlen*sizeof(struct wscons_keymap),
429 			      M_TEMP, M_WAITOK);
430 	}
431 
432 	for (i = 0; i < *maplen; i++) {
433 		(*map)[i].command = KS_voidSymbol;
434 		(*map)[i].group1[0] = KS_voidSymbol;
435 		(*map)[i].group1[1] = KS_voidSymbol;
436 		(*map)[i].group2[0] = KS_voidSymbol;
437 		(*map)[i].group2[1] = KS_voidSymbol;
438 	}
439 }
440 
441 int
wskbd_load_keymap(mapdata,map,maplen)442 wskbd_load_keymap(mapdata, map, maplen)
443 	const struct wskbd_mapdata *mapdata;
444 	struct wscons_keymap **map;
445 	int *maplen;
446 {
447 	int i, s, kc, stack_ptr;
448 	const keysym_t *kp;
449 	const struct wscons_keydesc *mp, *stack[10];
450 	kbd_t cur;
451 	keysym_t ksg;
452 
453 	for (cur = mapdata->layout & ~KB_HANDLEDBYWSKBD, stack_ptr = 0;
454 	     cur != 0; stack_ptr++) {
455 		mp = mapdata->keydesc;
456 		while (mp->map_size > 0) {
457 			if (cur == 0 || mp->name == cur) {
458 				break;
459 			}
460 			mp++;
461 		}
462 
463 		if (stack_ptr == sizeof(stack)/sizeof(stack[0]))
464 			panic("wskbd_load_keymap: %d: recursion too deep",
465 			      mapdata->layout);
466 		if (mp->map_size <= 0)
467 			return(EINVAL);
468 
469 		stack[stack_ptr] = mp;
470 		cur = mp->base;
471 	}
472 
473 	for (i = 0, s = stack_ptr - 1; s >= 0; s--) {
474 		mp = stack[s];
475 		for (kp = mp->map; kp < mp->map + mp->map_size; kp++) {
476 			ksg = KS_GROUP(*kp);
477 			if (ksg == KS_GROUP_Keycode && KS_VALUE(*kp) > i)
478 				i = KS_VALUE(*kp);
479 		}
480 	}
481 
482 	wskbd_init_keymap(i + 1, map, maplen);
483 
484 	for (s = stack_ptr - 1; s >= 0; s--) {
485 		mp = stack[s];
486 		for (kp = mp->map; kp < mp->map + mp->map_size; ) {
487 			ksg = KS_GROUP(*kp);
488 			if (ksg != KS_GROUP_Keycode)
489 				panic("wskbd_load_keymap: %d(%d): bad entry",
490 				      mp->name, *kp);
491 
492 			kc = KS_VALUE(*kp);
493 			kp++;
494 
495 			if (KS_GROUP(*kp) == KS_GROUP_Command ||
496 			    *kp == KS_Cmd || *kp == KS_Cmd1 || *kp == KS_Cmd2) {
497 				(*map)[kc].command = *kp;
498 				kp++;
499 			}
500 
501 			for (i = 0; kp + i < mp->map + mp->map_size; i++) {
502 				ksg = KS_GROUP(kp[i]);
503 				if (ksg == KS_GROUP_Keycode)
504 					break;
505 			}
506 
507 			if (i > 4)
508 				panic("wskbd_load_keymap: %d(%d): bad entry",
509 				      mp->name, *kp);
510 
511 			fillmapentry(kp, i, &(*map)[kc]);
512 			kp += i;
513 		}
514 	}
515 
516 	return(0);
517 }
518