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