Home | History | Annotate | Line # | Download | only in wscons
wskbdutil.c revision 1.2
      1 /*	$NetBSD: wskbdutil.c,v 1.2 1998/04/09 13:09:46 hannken 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/param.h>
     40 #include <sys/types.h>
     41 #include <sys/cdefs.h>
     42 #include <sys/errno.h>
     43 #include <sys/systm.h>
     44 #include <sys/malloc.h>
     45 #include <dev/wscons/wsksymdef.h>
     46 #include <dev/wscons/wsksymvar.h>
     47 
     48 static struct compose_tab_s {
     49 	keysym_t elem[2];
     50 	keysym_t result;
     51 } compose_tab[] = {
     52 	{ { KS_plus,			KS_plus },		KS_numbersign },
     53 	{ { KS_a,			KS_a },			KS_at },
     54 	{ { KS_parenleft,		KS_parenleft },		KS_bracketleft },
     55 	{ { KS_slash,			KS_slash },		KS_backslash },
     56 	{ { KS_parenright,		KS_parenright },	KS_bracketright },
     57 	{ { KS_parenleft,		KS_minus },		KS_braceleft },
     58 	{ { KS_slash,			KS_minus },		KS_bar },
     59 	{ { KS_parenright,		KS_minus },		KS_braceright },
     60 	{ { KS_exclam,			KS_exclam },		KS_exclamdown },
     61 	{ { KS_c,			KS_slash },		KS_cent },
     62 	{ { KS_l,			KS_minus },		KS_sterling },
     63 	{ { KS_y,			KS_minus },		KS_yen },
     64 	{ { KS_s,			KS_o },			KS_section },
     65 	{ { KS_x,			KS_o },			KS_currency },
     66 	{ { KS_c,			KS_o },			KS_copyright },
     67 	{ { KS_less,			KS_less },		KS_guillemotleft },
     68 	{ { KS_greater,			KS_greater },		KS_guillemotright },
     69 	{ { KS_question,		KS_question },		KS_questiondown },
     70 	{ { KS_dead_acute,		KS_space },		KS_acute },
     71 	{ { KS_dead_grave,		KS_space },		KS_grave },
     72 	{ { KS_dead_tilde,		KS_space },		KS_asciitilde },
     73 	{ { KS_dead_circumflex,		KS_space },		KS_asciicircum },
     74 	{ { KS_dead_circumflex,		KS_A },			KS_Acircumflex },
     75 	{ { KS_dead_diaeresis,		KS_A },			KS_Adiaeresis },
     76 	{ { KS_dead_grave,		KS_A },			KS_Agrave },
     77 	{ { KS_dead_abovering,		KS_A },			KS_Aring },
     78 	{ { KS_dead_tilde,		KS_A },			KS_Atilde },
     79 	{ { KS_dead_cedilla,		KS_C },			KS_Ccedilla },
     80 	{ { KS_dead_acute,		KS_E },			KS_Eacute },
     81 	{ { KS_dead_circumflex,		KS_E },			KS_Ecircumflex },
     82 	{ { KS_dead_diaeresis,		KS_E },			KS_Ediaeresis },
     83 	{ { KS_dead_grave,		KS_E },			KS_Egrave },
     84 	{ { KS_dead_acute,		KS_I },			KS_Iacute },
     85 	{ { KS_dead_circumflex,		KS_I },			KS_Icircumflex },
     86 	{ { KS_dead_diaeresis,		KS_I },			KS_Idiaeresis },
     87 	{ { KS_dead_grave,		KS_I },			KS_Igrave },
     88 	{ { KS_dead_tilde,		KS_N },			KS_Ntilde },
     89 	{ { KS_dead_acute,		KS_O },			KS_Oacute },
     90 	{ { KS_dead_circumflex,		KS_O },			KS_Ocircumflex },
     91 	{ { KS_dead_diaeresis,		KS_O },			KS_Odiaeresis },
     92 	{ { KS_dead_grave,		KS_O },			KS_Ograve },
     93 	{ { KS_dead_tilde,		KS_O },			KS_Otilde },
     94 	{ { KS_dead_acute,		KS_U },			KS_Uacute },
     95 	{ { KS_dead_circumflex,		KS_U },			KS_Ucircumflex },
     96 	{ { KS_dead_diaeresis,		KS_U },			KS_Udiaeresis },
     97 	{ { KS_dead_grave,		KS_U },			KS_Ugrave },
     98 	{ { KS_dead_acute,		KS_Y },			KS_Yacute },
     99 	{ { KS_dead_acute,		KS_a },			KS_aacute },
    100 	{ { KS_dead_circumflex,		KS_a },			KS_acircumflex },
    101 	{ { KS_dead_diaeresis,		KS_a },			KS_adiaeresis },
    102 	{ { KS_dead_grave,		KS_a },			KS_agrave },
    103 	{ { KS_dead_abovering,		KS_a },			KS_aring },
    104 	{ { KS_dead_tilde,		KS_a },			KS_atilde },
    105 	{ { KS_dead_cedilla,		KS_c },			KS_ccedilla },
    106 	{ { KS_dead_acute,		KS_e },			KS_eacute },
    107 	{ { KS_dead_circumflex,		KS_e },			KS_ecircumflex },
    108 	{ { KS_dead_diaeresis,		KS_e },			KS_ediaeresis },
    109 	{ { KS_dead_grave,		KS_e },			KS_egrave },
    110 	{ { KS_dead_acute,		KS_i },			KS_iacute },
    111 	{ { KS_dead_circumflex,		KS_i },			KS_icircumflex },
    112 	{ { KS_dead_diaeresis,		KS_i },			KS_idiaeresis },
    113 	{ { KS_dead_grave,		KS_i },			KS_igrave },
    114 	{ { KS_dead_tilde,		KS_n },			KS_ntilde },
    115 	{ { KS_dead_acute,		KS_o },			KS_oacute },
    116 	{ { KS_dead_circumflex,		KS_o },			KS_ocircumflex },
    117 	{ { KS_dead_diaeresis,		KS_o },			KS_odiaeresis },
    118 	{ { KS_dead_grave,		KS_o },			KS_ograve },
    119 	{ { KS_dead_tilde,		KS_o },			KS_otilde },
    120 	{ { KS_dead_acute,		KS_u },			KS_uacute },
    121 	{ { KS_dead_circumflex,		KS_u },			KS_ucircumflex },
    122 	{ { KS_dead_diaeresis,		KS_u },			KS_udiaeresis },
    123 	{ { KS_dead_grave,		KS_u },			KS_ugrave },
    124 	{ { KS_dead_acute,		KS_y },			KS_yacute },
    125 	{ { KS_dead_diaeresis,		KS_y },			KS_ydiaeresis },
    126 	{ { KS_quotedbl,		KS_A },			KS_Adiaeresis },
    127 	{ { KS_quotedbl,		KS_E },			KS_Ediaeresis },
    128 	{ { KS_quotedbl,		KS_I },			KS_Idiaeresis },
    129 	{ { KS_quotedbl,		KS_O },			KS_Odiaeresis },
    130 	{ { KS_quotedbl,		KS_U },			KS_Udiaeresis },
    131 	{ { KS_quotedbl,		KS_a },			KS_adiaeresis },
    132 	{ { KS_quotedbl,		KS_e },			KS_ediaeresis },
    133 	{ { KS_quotedbl,		KS_i },			KS_idiaeresis },
    134 	{ { KS_quotedbl,		KS_o },			KS_odiaeresis },
    135 	{ { KS_quotedbl,		KS_u },			KS_udiaeresis },
    136 	{ { KS_quotedbl,		KS_y },			KS_ydiaeresis },
    137 	{ { KS_acute,			KS_A },			KS_Aacute },
    138 	{ { KS_asciicircum,		KS_A },			KS_Acircumflex },
    139 	{ { KS_grave,			KS_A },			KS_Agrave },
    140 	{ { KS_asterisk,		KS_A },			KS_Aring },
    141 	{ { KS_asciitilde,		KS_A },			KS_Atilde },
    142 	{ { KS_cedilla,			KS_C },			KS_Ccedilla },
    143 	{ { KS_acute,			KS_E },			KS_Eacute },
    144 	{ { KS_asciicircum,		KS_E },			KS_Ecircumflex },
    145 	{ { KS_grave,			KS_E },			KS_Egrave },
    146 	{ { KS_acute,			KS_I },			KS_Iacute },
    147 	{ { KS_asciicircum,		KS_I },			KS_Icircumflex },
    148 	{ { KS_grave,			KS_I },			KS_Igrave },
    149 	{ { KS_asciitilde,		KS_N },			KS_Ntilde },
    150 	{ { KS_acute,			KS_O },			KS_Oacute },
    151 	{ { KS_asciicircum,		KS_O },			KS_Ocircumflex },
    152 	{ { KS_grave,			KS_O },			KS_Ograve },
    153 	{ { KS_asciitilde,		KS_O },			KS_Otilde },
    154 	{ { KS_acute,			KS_U },			KS_Uacute },
    155 	{ { KS_asciicircum,		KS_U },			KS_Ucircumflex },
    156 	{ { KS_grave,			KS_U },			KS_Ugrave },
    157 	{ { KS_acute,			KS_Y },			KS_Yacute },
    158 	{ { KS_acute,			KS_a },			KS_aacute },
    159 	{ { KS_asciicircum,		KS_a },			KS_acircumflex },
    160 	{ { KS_grave,			KS_a },			KS_agrave },
    161 	{ { KS_asterisk,		KS_a },			KS_aring },
    162 	{ { KS_asciitilde,		KS_a },			KS_atilde },
    163 	{ { KS_cedilla,			KS_c },			KS_ccedilla },
    164 	{ { KS_acute,			KS_e },			KS_eacute },
    165 	{ { KS_asciicircum,		KS_e },			KS_ecircumflex },
    166 	{ { KS_grave,			KS_e },			KS_egrave },
    167 	{ { KS_acute,			KS_i },			KS_iacute },
    168 	{ { KS_asciicircum,		KS_i },			KS_icircumflex },
    169 	{ { KS_grave,			KS_i },			KS_igrave },
    170 	{ { KS_asciitilde,		KS_n },			KS_ntilde },
    171 	{ { KS_acute,			KS_o },			KS_oacute },
    172 	{ { KS_asciicircum,		KS_o },			KS_ocircumflex },
    173 	{ { KS_grave,			KS_o },			KS_ograve },
    174 	{ { KS_asciitilde,		KS_o },			KS_otilde },
    175 	{ { KS_acute,			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 };
    180 
    181 #define COMPOSE_SIZE	sizeof(compose_tab)/sizeof(compose_tab[0])
    182 
    183 static struct string_tab_s {
    184 	keysym_t ksym;
    185 	char value[WSKBD_STRING_LEN];
    186 } string_tab[] = {
    187 	{ KS_f1,		"\033[OP" },
    188 	{ KS_f2,		"\033[OQ" },
    189 	{ KS_f3,		"\033[OR" },
    190 	{ KS_f4,		"\033[OS" },
    191 	{ KS_f5,		"\033[15~" },
    192 	{ KS_f6,		"\033[17~" },
    193 	{ KS_f7,		"\033[18~" },
    194 	{ KS_f8,		"\033[19~" },
    195 	{ KS_f9,		"\033[20~" },
    196 	{ KS_f10,		"\033[21~" },
    197 	{ KS_f11,		"\033[23~" },
    198 	{ KS_f12,		"\033[24~" },
    199 	{ KS_f13,		"\033[25~" },
    200 	{ KS_f14,		"\033[26~" },
    201 	{ KS_f15,		"\033[28~" },
    202 	{ KS_f16,		"\033[29~" },
    203 	{ KS_f17,		"\033[31~" },
    204 	{ KS_f18,		"\033[32~" },
    205 	{ KS_f19,		"\033[33~" },
    206 	{ KS_f20,		"\033[34~" },
    207 	{ KS_F1,		"\033[OP" },
    208 	{ KS_F2,		"\033[OQ" },
    209 	{ KS_F3,		"\033[OR" },
    210 	{ KS_F4,		"\033[OS" },
    211 	{ KS_F5,		"\033[15~" },
    212 	{ KS_F6,		"\033[17~" },
    213 	{ KS_F7,		"\033[18~" },
    214 	{ KS_F8,		"\033[19~" },
    215 	{ KS_F9,		"\033[20~" },
    216 	{ KS_F10,		"\033[21~" },
    217 	{ KS_F11,		"\033[23~" },
    218 	{ KS_F12,		"\033[24~" },
    219 	{ KS_F13,		"\033[25~" },
    220 	{ KS_F14,		"\033[26~" },
    221 	{ KS_F15,		"\033[28~" },
    222 	{ KS_F16,		"\033[29~" },
    223 	{ KS_F17,		"\033[31~" },
    224 	{ KS_F18,		"\033[32~" },
    225 	{ KS_F19,		"\033[33~" },
    226 	{ KS_F20,		"\033[34~" },
    227 	{ KS_KP_F1,		"\033[OP" },
    228 	{ KS_KP_F2,		"\033[OQ" },
    229 	{ KS_KP_F3,		"\033[OR" },
    230 	{ KS_KP_F4,		"\033[OS" },
    231 	{ KS_KP_Home,		"\033[H" },
    232 	{ KS_Home,		"\033[H" },
    233 	{ KS_KP_Left,		"\033[D" },
    234 	{ KS_Left,		"\033[D" },
    235 	{ KS_KP_Up,		"\033[A" },
    236 	{ KS_Up,		"\033[A" },
    237 	{ KS_KP_Right,		"\033[C" },
    238 	{ KS_Right,		"\033[C" },
    239 	{ KS_KP_Down,		"\033[B" },
    240 	{ KS_Down,		"\033[B" },
    241 	{ KS_KP_Prior,		"\033[I" },
    242 	{ KS_Prior,		"\033[I" },
    243 	{ KS_KP_Next,		"\033[G" },
    244 	{ KS_Next,		"\033[G" },
    245 	{ KS_KP_End,		"\033[F" },
    246 	{ KS_End,		"\033[F" },
    247 	{ KS_KP_Begin,		"\033[H" },
    248 	{ KS_KP_Insert,		"\033[L" },
    249 	{ KS_Insert,		"\033[L" },
    250 	{ KS_KP_Delete,		"\b" }
    251 };
    252 
    253 #define STRING_SIZE	sizeof(string_tab)/sizeof(string_tab[0])
    254 
    255 static int compose_tab_inorder = 0;
    256 static int string_tab_inorder = 0;
    257 
    258 static inline int compose_tab_cmp __P((struct compose_tab_s *, struct compose_tab_s *));
    259 static keysym_t ksym_upcase __P((keysym_t));
    260 static void fillmapentry __P((const keysym_t *, int, struct wscons_keymap *));
    261 
    262 static inline int
    263 compose_tab_cmp(i, j)
    264 	struct compose_tab_s *i, *j;
    265 {
    266 	if (i->elem[0] == j->elem[0])
    267 		return(i->elem[1] - j->elem[1]);
    268 	else
    269 		return(i->elem[0] - j->elem[0]);
    270 }
    271 
    272 keysym_t
    273 wskbd_compose_value(compose_buf)
    274 	keysym_t *compose_buf;
    275 {
    276 	int i, j, r;
    277 	struct compose_tab_s v;
    278 
    279 	if (! compose_tab_inorder) {
    280 		/* Insertion sort. */
    281 		for (i = 1; i < COMPOSE_SIZE; i++) {
    282 			v = compose_tab[i];
    283 			/* find correct slot, moving others up */
    284 			for (j = i; --j >= 0 && compose_tab_cmp(& v, & compose_tab[j]) < 0; )
    285 				compose_tab[j + 1] = compose_tab[j];
    286 			compose_tab[j + 1] = v;
    287 		}
    288 		compose_tab_inorder = 1;
    289 	}
    290 
    291 	for (j = 0, i = COMPOSE_SIZE; i != 0; i /= 2) {
    292 		if (compose_tab[j + i/2].elem[0] == compose_buf[0]) {
    293 			if (compose_tab[j + i/2].elem[1] == compose_buf[1])
    294 				return(compose_tab[j + i/2].result);
    295 			r = compose_tab[j + i/2].elem[1] < compose_buf[1];
    296 		} else
    297 			r = compose_tab[j + i/2].elem[0] < compose_buf[0];
    298 		if (r) {
    299 			j += i/2 + 1;
    300 			i--;
    301 		}
    302 	}
    303 
    304 	return(KS_voidSymbol);
    305 }
    306 
    307 char *
    308 wskbd_get_string(sym)
    309 	keysym_t sym;
    310 {
    311 	int i, j;
    312 	struct string_tab_s v;
    313 
    314 	if (! string_tab_inorder) {
    315 		/* Insertion sort. */
    316 		for (i = 1; i < STRING_SIZE; i++) {
    317 			v = string_tab[i];
    318 			/* find correct slot, moving others up */
    319 			for (j = i; --j >= 0 && v.ksym < string_tab[j].ksym; )
    320 				string_tab[j + 1] = string_tab[j];
    321 			string_tab[j + 1] = v;
    322 		}
    323 		string_tab_inorder = 1;
    324 	}
    325 
    326 	for (j = 0, i = STRING_SIZE; i != 0; i /= 2) {
    327 		if (string_tab[j + i/2].ksym == sym)
    328 			return(string_tab[j + i/2].value);
    329 		else if (string_tab[j + i/2].ksym < sym) {
    330 			j += i/2 + 1;
    331 			i--;
    332 		}
    333 	}
    334 
    335 	return(NULL);
    336 }
    337 
    338 int
    339 wskbd_set_string(sym, data)
    340 	keysym_t sym;
    341 	char *data;
    342 {
    343 	int i, j;
    344 	struct string_tab_s v;
    345 
    346 	if (! string_tab_inorder) {
    347 		/* Insertion sort. */
    348 		for (i = 1; i < STRING_SIZE; i++) {
    349 			v = string_tab[i];
    350 			/* find correct slot, moving others up */
    351 			for (j = i; --j >= 0 && v.ksym < string_tab[j].ksym; )
    352 				string_tab[j + 1] = string_tab[j];
    353 			string_tab[j + 1] = v;
    354 		}
    355 		string_tab_inorder = 1;
    356 	}
    357 
    358 	for (j = 0, i = STRING_SIZE; i != 0; i /= 2) {
    359 		if (string_tab[j + i/2].ksym == sym) {
    360 			bcopy(data, string_tab[j + i/2].value, WSKBD_STRING_LEN);
    361 			return(0);
    362 		} else if (string_tab[j + i/2].ksym < sym) {
    363 			j += i/2 + 1;
    364 			i--;
    365 		}
    366 	}
    367 
    368 	return(EINVAL);
    369 }
    370 
    371 static const u_char latin1_to_upper[256] = {
    372 /*      0  8  1  9  2  a  3  b  4  c  5  d  6  e  7  f               */
    373 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
    374 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
    375 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
    376 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
    377 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
    378 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
    379 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
    380 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
    381 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
    382 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
    383 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
    384 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
    385 	0x00,  'A',  'B',  'C',  'D',  'E',  'F',  'G',		/* 6 */
    386 	 'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',		/* 6 */
    387 	 'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',		/* 7 */
    388 	 'X',  'Y',  'Z', 0x00, 0x00, 0x00, 0x00, 0x00,		/* 7 */
    389 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
    390 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
    391 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
    392 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
    393 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
    394 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
    395 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
    396 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
    397 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
    398 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
    399 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
    400 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
    401 	0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,		/* e */
    402 	0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,		/* e */
    403 	0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0x00,		/* f */
    404 	0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0x00,		/* f */
    405 };
    406 
    407 static keysym_t
    408 ksym_upcase(ksym)
    409 	keysym_t ksym;
    410 {
    411 	if (ksym >= KS_f1 && ksym <= KS_f20)
    412 		return(KS_F1 - KS_f1 + ksym);
    413 
    414 	if (KS_GROUP(ksym) == KS_GROUP_Ascii &&
    415 	    latin1_to_upper[ksym & 0xff] != 0x00)
    416 		return(latin1_to_upper[ksym & 0xff] | KS_GROUP_Ascii);
    417 
    418 	return(ksym);
    419 }
    420 
    421 static void
    422 fillmapentry(kp, len, mapentry)
    423 	const keysym_t *kp;
    424 	int len;
    425 	struct wscons_keymap *mapentry;
    426 {
    427 	switch (len) {
    428 	case 0:
    429 		mapentry->group1[0] = KS_voidSymbol;
    430 		mapentry->group1[1] = KS_voidSymbol;
    431 		mapentry->group2[0] = KS_voidSymbol;
    432 		mapentry->group2[1] = KS_voidSymbol;
    433 		break;
    434 
    435 	case 1:
    436 		mapentry->group1[0] = kp[0];
    437 		mapentry->group1[1] = ksym_upcase(kp[0]);
    438 		mapentry->group2[0] = mapentry->group1[0];
    439 		mapentry->group2[1] = mapentry->group1[1];
    440 		break;
    441 
    442 	case 2:
    443 		mapentry->group1[0] = kp[0];
    444 		mapentry->group1[1] = kp[1];
    445 		mapentry->group2[0] = mapentry->group1[0];
    446 		mapentry->group2[1] = mapentry->group1[1];
    447 		break;
    448 
    449 	case 3:
    450 		mapentry->group1[0] = kp[0];
    451 		mapentry->group1[1] = kp[1];
    452 		mapentry->group2[0] = kp[2];
    453 		mapentry->group2[1] = ksym_upcase(kp[2]);
    454 		break;
    455 
    456 	case 4:
    457 		mapentry->group1[0] = kp[0];
    458 		mapentry->group1[1] = kp[1];
    459 		mapentry->group2[0] = kp[2];
    460 		mapentry->group2[1] = kp[3];
    461 		break;
    462 
    463 	}
    464 }
    465 
    466 void
    467 wskbd_get_mapentry(name, kdesc, kdesclen, kc, mapentry)
    468 	kbd_t name;
    469 	const struct wscons_keydesc *kdesc;
    470 	int kdesclen;
    471 	int kc;
    472 	struct wscons_keymap *mapentry;
    473 {
    474 	kbd_t cur;
    475 	const keysym_t *kp;
    476 	const struct wscons_keydesc *mp;
    477 	int i, l;
    478 
    479 	mapentry->command = KS_voidSymbol;
    480 	mapentry->group1[0] = KS_voidSymbol;
    481 	mapentry->group1[1] = KS_voidSymbol;
    482 	mapentry->group2[0] = KS_voidSymbol;
    483 	mapentry->group2[1] = KS_voidSymbol;
    484 
    485 	for (cur = name; cur != 0; ) {
    486 		for (i = 0; i < kdesclen; i++)
    487 			if (kdesc[i].name == cur)
    488 				break;
    489 
    490 		/* If map not found, return */
    491 		if (i == kdesclen)
    492 			return;
    493 
    494 		mp = kdesc + i;
    495 		for (kp = mp->map; kp < mp->map + mp->map_size; kp++)
    496 			if (KS_GROUP(*kp) == KS_GROUP_Keycode &&
    497 			    KS_VALUE(*kp) == kc) {
    498 				/* First skip keycode and possible command */
    499 				kp++;
    500 				if (KS_GROUP(*kp) == KS_GROUP_Command ||
    501 				    *kp == KS_Cmd || *kp == KS_Cmd1 || *kp == KS_Cmd2)
    502 					mapentry->command = *kp++;
    503 
    504 				for (l = 0; kp + l < mp->map + mp->map_size; l++)
    505 					if (KS_GROUP(kp[l]) == KS_GROUP_Keycode)
    506 						break;
    507 				if (l > 4)
    508 					panic("wskbd_get_mapentry: %d(%d): bad entry",
    509 					      mp->name, *kp);
    510 				fillmapentry(kp, l, mapentry);
    511 				return;
    512 			}
    513 
    514 		cur = kdesc[i].base;
    515 	}
    516 }
    517 
    518 void
    519 wskbd_init_keymap(newlen, map, maplen)
    520 	int newlen;
    521 	struct wscons_keymap **map;
    522 	int *maplen;
    523 {
    524 	int i;
    525 
    526 	if (newlen != *maplen) {
    527 		if (*maplen > 0)
    528 			free(*map, M_TEMP);
    529 		*maplen = newlen;
    530 		*map = malloc(newlen*sizeof(struct wscons_keymap),
    531 			      M_TEMP, M_WAITOK);
    532 	}
    533 
    534 	for (i = 0; i < *maplen; i++) {
    535 		(*map)[i].command = KS_voidSymbol;
    536 		(*map)[i].group1[0] = KS_voidSymbol;
    537 		(*map)[i].group1[1] = KS_voidSymbol;
    538 		(*map)[i].group2[0] = KS_voidSymbol;
    539 		(*map)[i].group2[1] = KS_voidSymbol;
    540 	}
    541 }
    542 
    543 int
    544 wskbd_load_keymap(name, kdesc, kdesclen, map, maplen)
    545 	kbd_t name;
    546 	const struct wscons_keydesc *kdesc;
    547 	int kdesclen;
    548 	struct wscons_keymap **map;
    549 	int *maplen;
    550 {
    551 	int i, s, kc, found, stack[10], stack_ptr;
    552 	const keysym_t *kp;
    553 	const struct wscons_keydesc *mp;
    554 	kbd_t cur;
    555 
    556 	for (cur = name, stack_ptr = 0; cur != 0; stack_ptr++) {
    557 		for (i = found = 0; i < kdesclen; i++)
    558 			if (cur == 0 || kdesc[i].name == cur) {
    559 				found = 1;
    560 				break;
    561 			}
    562 
    563 		if (stack_ptr == sizeof(stack)/sizeof(stack[0]))
    564 			panic("wskbd_load_keymap: %d: recursion too deep", name);
    565 		if (! found)
    566 			return(EINVAL);
    567 
    568 		stack[stack_ptr] = i;
    569 		cur = kdesc[i].base;
    570 	}
    571 
    572 	for (i = 0, s = stack_ptr - 1; s >= 0; s--) {
    573 		mp = kdesc + stack[s];
    574 		for (kp = mp->map; kp < mp->map + mp->map_size; kp++)
    575 			if (KS_GROUP(*kp) == KS_GROUP_Keycode && KS_VALUE(*kp) > i)
    576 				i = KS_VALUE(*kp);
    577 	}
    578 
    579 	wskbd_init_keymap(i + 1, map, maplen);
    580 
    581 	for (s = stack_ptr - 1; s >= 0; s--) {
    582 		mp = kdesc + stack[s];
    583 		for (kp = mp->map; kp < mp->map + mp->map_size; ) {
    584 			if (KS_GROUP(*kp) != KS_GROUP_Keycode)
    585 				panic("wskbd_load_keymap: %d(%d): bad entry",
    586 				      mp->name, *kp);
    587 
    588 			kc = KS_VALUE(*kp);
    589 			kp++;
    590 
    591 			if (KS_GROUP(*kp) == KS_GROUP_Command ||
    592 			    *kp == KS_Cmd || *kp == KS_Cmd1 || *kp == KS_Cmd2) {
    593 				(*map)[kc].command = *kp;
    594 				kp++;
    595 			}
    596 
    597 			for (i = 0; kp + i < mp->map + mp->map_size; i++)
    598 				if (KS_GROUP(kp[i]) == KS_GROUP_Keycode)
    599 					break;
    600 
    601 			if (i > 4)
    602 				panic("wskbd_load_keymap: %d(%d): bad entry",
    603 				      mp->name, *kp);
    604 
    605 			fillmapentry(kp, i, &(*map)[kc]);
    606 			kp += i;
    607 		}
    608 	}
    609 
    610 	return(0);
    611 }
    612