Home | History | Annotate | Line # | Download | only in wscons
wskbdutil.c revision 1.14
      1 /*	$NetBSD: wskbdutil.c,v 1.14 2007/01/29 01:52:45 hubertf Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Juergen Hannken-Illjes.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: wskbdutil.c,v 1.14 2007/01/29 01:52:45 hubertf Exp $");
     41 
     42 #include <sys/param.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 	{ { KS_dead_semi,		KS_gr_A },		KS_gr_At  },
    181 	{ { KS_dead_semi,		KS_gr_E },		KS_gr_Et  },
    182 	{ { KS_dead_semi,		KS_gr_H },		KS_gr_Ht  },
    183 	{ { KS_dead_semi,		KS_gr_I },		KS_gr_It  },
    184 	{ { KS_dead_semi,		KS_gr_O },		KS_gr_Ot  },
    185 	{ { KS_dead_semi,		KS_gr_Y },		KS_gr_Yt  },
    186 	{ { KS_dead_semi,		KS_gr_V },		KS_gr_Vt  },
    187 	{ { KS_dead_colon,		KS_gr_I },		KS_gr_Id  },
    188 	{ { KS_dead_colon,		KS_gr_Y },		KS_gr_Yd  },
    189 	{ { KS_dead_semi,		KS_gr_a },		KS_gr_at  },
    190 	{ { KS_dead_semi,		KS_gr_e },		KS_gr_et  },
    191 	{ { KS_dead_semi,		KS_gr_h },		KS_gr_ht  },
    192 	{ { KS_dead_semi,		KS_gr_i },		KS_gr_it  },
    193 	{ { KS_dead_semi,		KS_gr_o },		KS_gr_ot  },
    194 	{ { KS_dead_semi,		KS_gr_y },		KS_gr_yt  },
    195 	{ { KS_dead_semi,		KS_gr_v },		KS_gr_vt  },
    196 	{ { KS_dead_colon,		KS_gr_i },		KS_gr_id  },
    197 	{ { KS_dead_colon,		KS_gr_y },		KS_gr_yd  }
    198 };
    199 
    200 #define COMPOSE_SIZE	sizeof(compose_tab)/sizeof(compose_tab[0])
    201 
    202 static int compose_tab_inorder = 0;
    203 
    204 static inline int compose_tab_cmp(struct compose_tab_s *, struct compose_tab_s *);
    205 static keysym_t ksym_upcase(keysym_t);
    206 static void fillmapentry(const keysym_t *, int, struct wscons_keymap *);
    207 
    208 static inline int
    209 compose_tab_cmp(struct compose_tab_s *i, struct compose_tab_s *j)
    210 {
    211 	if (i->elem[0] == j->elem[0])
    212 		return(i->elem[1] - j->elem[1]);
    213 	else
    214 		return(i->elem[0] - j->elem[0]);
    215 }
    216 
    217 keysym_t
    218 wskbd_compose_value(keysym_t *compose_buf)
    219 {
    220 	int i, j, r;
    221 	struct compose_tab_s v;
    222 
    223 	if (! compose_tab_inorder) {
    224 		/* Insertion sort. */
    225 		for (i = 1; i < COMPOSE_SIZE; i++) {
    226 			v = compose_tab[i];
    227 			/* find correct slot, moving others up */
    228 			for (j = i; --j >= 0 && compose_tab_cmp(& v, & compose_tab[j]) < 0; )
    229 				compose_tab[j + 1] = compose_tab[j];
    230 			compose_tab[j + 1] = v;
    231 		}
    232 		compose_tab_inorder = 1;
    233 	}
    234 
    235 	for (j = 0, i = COMPOSE_SIZE; i != 0; i /= 2) {
    236 		if (compose_tab[j + i/2].elem[0] == compose_buf[0]) {
    237 			if (compose_tab[j + i/2].elem[1] == compose_buf[1])
    238 				return(compose_tab[j + i/2].result);
    239 			r = compose_tab[j + i/2].elem[1] < compose_buf[1];
    240 		} else
    241 			r = compose_tab[j + i/2].elem[0] < compose_buf[0];
    242 		if (r) {
    243 			j += i/2 + 1;
    244 			i--;
    245 		}
    246 	}
    247 
    248 	return(KS_voidSymbol);
    249 }
    250 
    251 static const u_char latin1_to_upper[256] = {
    252 /*      0  8  1  9  2  a  3  b  4  c  5  d  6  e  7  f               */
    253 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
    254 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
    255 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
    256 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
    257 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
    258 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
    259 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
    260 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
    261 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
    262 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
    263 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
    264 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
    265 	0x00,  'A',  'B',  'C',  'D',  'E',  'F',  'G',		/* 6 */
    266 	 'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',		/* 6 */
    267 	 'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',		/* 7 */
    268 	 'X',  'Y',  'Z', 0x00, 0x00, 0x00, 0x00, 0x00,		/* 7 */
    269 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
    270 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
    271 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
    272 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
    273 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
    274 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
    275 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
    276 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
    277 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
    278 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
    279 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
    280 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
    281 	0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,		/* e */
    282 	0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,		/* e */
    283 	0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0x00,		/* f */
    284 	0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0x00,		/* f */
    285 };
    286 
    287 static keysym_t
    288 ksym_upcase(keysym_t ksym)
    289 {
    290 	if (ksym >= KS_f1 && ksym <= KS_f20)
    291 		return(KS_F1 - KS_f1 + ksym);
    292 
    293 	if (KS_GROUP(ksym) == KS_GROUP_Ascii && ksym <= 0xff &&
    294 	    latin1_to_upper[ksym] != 0x00)
    295 		return(latin1_to_upper[ksym]);
    296 
    297 	return(ksym);
    298 }
    299 
    300 static void
    301 fillmapentry(const keysym_t *kp, int len, struct wscons_keymap *mapentry)
    302 {
    303 	switch (len) {
    304 	case 0:
    305 		mapentry->group1[0] = KS_voidSymbol;
    306 		mapentry->group1[1] = KS_voidSymbol;
    307 		mapentry->group2[0] = KS_voidSymbol;
    308 		mapentry->group2[1] = KS_voidSymbol;
    309 		break;
    310 
    311 	case 1:
    312 		mapentry->group1[0] = kp[0];
    313 		mapentry->group1[1] = ksym_upcase(kp[0]);
    314 		mapentry->group2[0] = mapentry->group1[0];
    315 		mapentry->group2[1] = mapentry->group1[1];
    316 		break;
    317 
    318 	case 2:
    319 		mapentry->group1[0] = kp[0];
    320 		mapentry->group1[1] = kp[1];
    321 		mapentry->group2[0] = mapentry->group1[0];
    322 		mapentry->group2[1] = mapentry->group1[1];
    323 		break;
    324 
    325 	case 3:
    326 		mapentry->group1[0] = kp[0];
    327 		mapentry->group1[1] = kp[1];
    328 		mapentry->group2[0] = kp[2];
    329 		mapentry->group2[1] = ksym_upcase(kp[2]);
    330 		break;
    331 
    332 	case 4:
    333 		mapentry->group1[0] = kp[0];
    334 		mapentry->group1[1] = kp[1];
    335 		mapentry->group2[0] = kp[2];
    336 		mapentry->group2[1] = kp[3];
    337 		break;
    338 
    339 	}
    340 }
    341 
    342 void
    343 wskbd_get_mapentry(const struct wskbd_mapdata *mapdata, int kc,
    344 	struct wscons_keymap *mapentry)
    345 {
    346 	kbd_t cur;
    347 	const keysym_t *kp;
    348 	const struct wscons_keydesc *mp;
    349 	int l;
    350 
    351 	mapentry->command = KS_voidSymbol;
    352 	mapentry->group1[0] = KS_voidSymbol;
    353 	mapentry->group1[1] = KS_voidSymbol;
    354 	mapentry->group2[0] = KS_voidSymbol;
    355 	mapentry->group2[1] = KS_voidSymbol;
    356 
    357 	for (cur = mapdata->layout & ~KB_HANDLEDBYWSKBD; cur != 0; ) {
    358 		mp = mapdata->keydesc;
    359 		while (mp->map_size > 0) {
    360 			if (mp->name == cur)
    361 				break;
    362 			mp++;
    363 		}
    364 
    365 		/* If map not found, return */
    366 		if (mp->map_size <= 0)
    367 			return;
    368 
    369 		for (kp = mp->map; kp < mp->map + mp->map_size; kp++)
    370 			if (KS_GROUP(*kp) == KS_GROUP_Keycode &&
    371 			    KS_VALUE(*kp) == kc) {
    372 				/* First skip keycode and possible command */
    373 				kp++;
    374 				if (KS_GROUP(*kp) == KS_GROUP_Command ||
    375 				    *kp == KS_Cmd || *kp == KS_Cmd1 || *kp == KS_Cmd2)
    376 					mapentry->command = *kp++;
    377 
    378 				for (l = 0; kp + l < mp->map + mp->map_size; l++)
    379 					if (KS_GROUP(kp[l]) == KS_GROUP_Keycode)
    380 						break;
    381 				if (l > 4)
    382 					panic("wskbd_get_mapentry: %d(%d): bad entry",
    383 					      mp->name, *kp);
    384 				fillmapentry(kp, l, mapentry);
    385 				return;
    386 			}
    387 
    388 		cur = mp->base;
    389 	}
    390 }
    391 
    392 void
    393 wskbd_init_keymap(int newlen, struct wscons_keymap **map, int *maplen)
    394 {
    395 	int i;
    396 
    397 	if (newlen != *maplen) {
    398 		if (*maplen > 0)
    399 			free(*map, M_TEMP);
    400 		*maplen = newlen;
    401 		*map = malloc(newlen*sizeof(struct wscons_keymap),
    402 			      M_TEMP, M_WAITOK);
    403 	}
    404 
    405 	for (i = 0; i < *maplen; i++) {
    406 		(*map)[i].command = KS_voidSymbol;
    407 		(*map)[i].group1[0] = KS_voidSymbol;
    408 		(*map)[i].group1[1] = KS_voidSymbol;
    409 		(*map)[i].group2[0] = KS_voidSymbol;
    410 		(*map)[i].group2[1] = KS_voidSymbol;
    411 	}
    412 }
    413 
    414 int
    415 wskbd_load_keymap(const struct wskbd_mapdata *mapdata,
    416 	struct wscons_keymap **map, int *maplen)
    417 {
    418 	int i, s, kc, stack_ptr;
    419 	const keysym_t *kp;
    420 	const struct wscons_keydesc *mp, *stack[10];
    421 	kbd_t cur;
    422 
    423 	for (cur = mapdata->layout & ~KB_HANDLEDBYWSKBD, stack_ptr = 0;
    424 	     cur != 0; stack_ptr++) {
    425 		mp = mapdata->keydesc;
    426 		while (mp->map_size > 0) {
    427 			if (cur == 0 || mp->name == cur) {
    428 				break;
    429 			}
    430 			mp++;
    431 		}
    432 
    433 		if (stack_ptr == sizeof(stack)/sizeof(stack[0]))
    434 			panic("wskbd_load_keymap: %d: recursion too deep",
    435 			      mapdata->layout);
    436 		if (mp->map_size <= 0)
    437 			return(EINVAL);
    438 
    439 		stack[stack_ptr] = mp;
    440 		cur = mp->base;
    441 	}
    442 
    443 	for (i = 0, s = stack_ptr - 1; s >= 0; s--) {
    444 		mp = stack[s];
    445 		for (kp = mp->map; kp < mp->map + mp->map_size; kp++)
    446 			if (KS_GROUP(*kp) == KS_GROUP_Keycode && KS_VALUE(*kp) > i)
    447 				i = KS_VALUE(*kp);
    448 	}
    449 
    450 	wskbd_init_keymap(i + 1, map, maplen);
    451 
    452 	for (s = stack_ptr - 1; s >= 0; s--) {
    453 		mp = stack[s];
    454 		for (kp = mp->map; kp < mp->map + mp->map_size; ) {
    455 			if (KS_GROUP(*kp) != KS_GROUP_Keycode)
    456 				panic("wskbd_load_keymap: %d(%d): bad entry",
    457 				      mp->name, *kp);
    458 
    459 			kc = KS_VALUE(*kp);
    460 			kp++;
    461 
    462 			if (KS_GROUP(*kp) == KS_GROUP_Command ||
    463 			    *kp == KS_Cmd || *kp == KS_Cmd1 || *kp == KS_Cmd2) {
    464 				(*map)[kc].command = *kp;
    465 				kp++;
    466 			}
    467 
    468 			for (i = 0; kp + i < mp->map + mp->map_size; i++)
    469 				if (KS_GROUP(kp[i]) == KS_GROUP_Keycode)
    470 					break;
    471 
    472 			if (i > 4)
    473 				panic("wskbd_load_keymap: %d(%d): bad entry",
    474 				      mp->name, *kp);
    475 
    476 			fillmapentry(kp, i, &(*map)[kc]);
    477 			kp += i;
    478 		}
    479 	}
    480 
    481 	return(0);
    482 }
    483