Home | History | Annotate | Line # | Download | only in wsconsctl
util.c revision 1.5
      1 /*	$NetBSD: util.c,v 1.5 1999/11/10 16:49:38 drochner Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 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/time.h>
     40 #include <dev/wscons/wsconsio.h>
     41 #include <dev/wscons/wsksymdef.h>
     42 #include <err.h>
     43 #include <string.h>
     44 #include <stdio.h>
     45 #include <unistd.h>
     46 #include "wsconsctl.h"
     47 
     48 #define TABLEN(t)		(sizeof(t)/sizeof(t[0]))
     49 
     50 extern struct wskbd_map_data kbmap;	/* from keyboard.c */
     51 extern struct wskbd_map_data newkbmap;	/* from map_parse.y */
     52 
     53 struct nameint {
     54 	int value;
     55 	char *name;
     56 };
     57 
     58 static struct nameint kbtype_tab[] = {
     59 	{ WSKBD_TYPE_LK201,	"lk201" },
     60 	{ WSKBD_TYPE_LK401,	"lk401" },
     61 	{ WSKBD_TYPE_PC_XT,	"pc-xt" },
     62 	{ WSKBD_TYPE_PC_AT,	"pc-at" },
     63 	{ WSKBD_TYPE_USB,	"usb" },
     64 };
     65 
     66 static struct nameint mstype_tab[] = {
     67 	{ WSMOUSE_TYPE_VSXXX,	"dec-tc" },
     68 	{ WSMOUSE_TYPE_PS2,	"ps2" },
     69 	{ WSMOUSE_TYPE_USB,	"usb" },
     70 };
     71 
     72 static struct nameint dpytype_tab[] = {
     73 	{ WSDISPLAY_TYPE_UNKNOWN,	"unknown" },
     74 	{ WSDISPLAY_TYPE_PM_MONO,	"dec-?mono" },
     75 	{ WSDISPLAY_TYPE_PM_COLOR,	"dec-?color" },
     76 	{ WSDISPLAY_TYPE_CFB,		"dec-cfb" },
     77 	{ WSDISPLAY_TYPE_XCFB,		"dec-?xcfb" },
     78 	{ WSDISPLAY_TYPE_MFB,		"dec-mfb" },
     79 	{ WSDISPLAY_TYPE_SFB,		"dec-sfb" },
     80 	{ WSDISPLAY_TYPE_ISAVGA,	"vga-isa" },
     81 	{ WSDISPLAY_TYPE_PCIVGA,	"vga-pci" },
     82 	{ WSDISPLAY_TYPE_TGA,		"dec-tga-pci" },
     83 	{ WSDISPLAY_TYPE_SFBP,		"dec-sfb+" },
     84 	{ WSDISPLAY_TYPE_PCIMISC,	"generic-pci" },
     85 };
     86 
     87 static struct nameint kbdenc_tab[] = {
     88 	KB_ENCTAB
     89 };
     90 
     91 static struct nameint kbdvar_tab[] = {
     92 	KB_VARTAB
     93 };
     94 
     95 static struct field *field_tab;
     96 static int field_tab_len;
     97 
     98 static char *int2name __P((int, int, struct nameint *, int));
     99 static int name2int __P((char *, struct nameint *, int));
    100 static void print_kmap __P((struct wskbd_map_data *));
    101 
    102 void
    103 field_setup(ftab, len)
    104 	struct field *ftab;
    105 	int len;
    106 {
    107 	field_tab = ftab;
    108 	field_tab_len = len;
    109 }
    110 
    111 struct field *
    112 field_by_name(name)
    113 	char *name;
    114 {
    115 	int i;
    116 
    117 	for (i = 0; i < field_tab_len; i++)
    118 		if (strcmp(field_tab[i].name, name) == 0)
    119 			return(field_tab + i);
    120 
    121 	errx(1, "%s: not found", name);
    122 }
    123 
    124 struct field *
    125 field_by_value(addr)
    126 	void *addr;
    127 {
    128 	int i;
    129 
    130 	for (i = 0; i < field_tab_len; i++)
    131 		if (field_tab[i].valp == addr)
    132 			return(field_tab + i);
    133 
    134 	errx(1, "internal error: field_by_value: not found");
    135 }
    136 
    137 static char *
    138 int2name(val, uflag, tab, len)
    139 	int val;
    140 	int uflag;
    141 	struct nameint *tab;
    142 	int len;
    143 {
    144 	static char tmp[20];
    145 	int i;
    146 
    147 	for (i = 0; i < len; i++)
    148 		if (tab[i].value == val)
    149 			return(tab[i].name);
    150 
    151 	if (uflag) {
    152 		snprintf(tmp, sizeof(tmp), "unknown_%d", val);
    153 		return(tmp);
    154 	} else
    155 		return(NULL);
    156 }
    157 
    158 static int
    159 name2int(val, tab, len)
    160 	char *val;
    161 	struct nameint *tab;
    162 	int len;
    163 {
    164 	int i;
    165 
    166 	for (i = 0; i < len; i++)
    167 		if (strcmp(tab[i].name, val) == 0)
    168 			return(tab[i].value);
    169 	return(-1);
    170 }
    171 
    172 void
    173 pr_field(f, sep)
    174 	struct field *f;
    175 	char *sep;
    176 {
    177 	char *p;
    178 	u_int flags;
    179 	int i;
    180 
    181 	if (sep)
    182 		printf("%s%s", f->name, sep);
    183 
    184 	switch (f->format) {
    185 	case FMT_UINT:
    186 		printf("%u", *((u_int *) f->valp));
    187 		break;
    188 	case FMT_KBDTYPE:
    189 		p = int2name(*((u_int *) f->valp), 1,
    190 			     kbtype_tab, TABLEN(kbtype_tab));
    191 		printf("%s", p);
    192 		break;
    193 	case FMT_MSTYPE:
    194 		p = int2name(*((u_int *) f->valp), 1,
    195 			     mstype_tab, TABLEN(mstype_tab));
    196 		printf("%s", p);
    197 		break;
    198 	case FMT_DPYTYPE:
    199 		p = int2name(*((u_int *) f->valp), 1,
    200 			     dpytype_tab, TABLEN(dpytype_tab));
    201 		printf("%s", p);
    202 		break;
    203 	case FMT_KBDENC:
    204 		p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1,
    205 			     kbdenc_tab, TABLEN(kbdenc_tab));
    206 		printf("%s", p);
    207 
    208 		flags = KB_VARIANT(*((u_int *) f->valp));
    209 		for (i = 0; i < 32; i++) {
    210 			if (!(flags & (1 << i)))
    211 				continue;
    212 			p = int2name(flags & (1 << i), 1,
    213 				     kbdvar_tab, TABLEN(kbdvar_tab));
    214 			printf(".%s", p);
    215 		}
    216 		break;
    217 	case FMT_KBMAP:
    218 		print_kmap((struct wskbd_map_data *) f->valp);
    219 		break;
    220 	default:
    221 		errx(1, "internal error: pr_field: no format %d", f->format);
    222 		break;
    223 	}
    224 
    225 	printf("\n");
    226 }
    227 
    228 void
    229 rd_field(f, val, merge)
    230 	struct field *f;
    231 	char *val;
    232 	int merge;
    233 {
    234 	int i;
    235 	u_int u;
    236 	char *p;
    237 	struct wscons_keymap *mp;
    238 
    239 	switch (f->format) {
    240 	case FMT_UINT:
    241 		if (sscanf(val, "%u", &u) != 1)
    242 			errx(1, "%s: not a number", val);
    243 		if (merge)
    244 			*((u_int *) f->valp) += u;
    245 		else
    246 			*((u_int *) f->valp) = u;
    247 		break;
    248 	case FMT_KBDENC:
    249 		p = strchr(val, '.');
    250 		if (p != NULL)
    251 			*p++ = '\0';
    252 
    253 		i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab));
    254 		if (i == -1)
    255 			errx(1, "%s: not a valid encoding", val);
    256 		*((u_int *) f->valp) = i;
    257 
    258 		while (p) {
    259 			val = p;
    260 			p = strchr(p, '.');
    261 			if (p != NULL)
    262 				*p++ = '\0';
    263 			i = name2int(val, kbdvar_tab, TABLEN(kbdvar_tab));
    264 			if (i == -1)
    265 				errx(1, "%s: not a valid variant", val);
    266 			*((u_int *) f->valp) |= i;
    267 		}
    268 		printf("enc: %x\n", *((u_int *) f->valp));
    269 		break;
    270 	case FMT_KBMAP:
    271 		if (! merge)
    272 			kbmap.maplen = 0;
    273 		map_scan_setinput(val);
    274 		yyparse();
    275 		if (merge) {
    276 			if (newkbmap.maplen < kbmap.maplen)
    277 				newkbmap.maplen = kbmap.maplen;
    278 			for (i = 0; i < kbmap.maplen; i++) {
    279 				mp = newkbmap.map + i;
    280 				if (mp->command == KS_voidSymbol &&
    281 				    mp->group1[0] == KS_voidSymbol &&
    282 				    mp->group1[1] == KS_voidSymbol &&
    283 				    mp->group2[0] == KS_voidSymbol &&
    284 				    mp->group2[1] == KS_voidSymbol)
    285 					*mp = kbmap.map[i];
    286 			}
    287 		}
    288 		kbmap.maplen = newkbmap.maplen;
    289 		bcopy(newkbmap.map, kbmap.map,
    290 		      kbmap.maplen*sizeof(struct wscons_keymap));
    291 		break;
    292 	default:
    293 		errx(1, "internal error: rd_field: no format %d", f->format);
    294 		break;
    295 	}
    296 }
    297 
    298 static void
    299 print_kmap(map)
    300 	struct wskbd_map_data *map;
    301 {
    302 	int i;
    303 	struct wscons_keymap *mp;
    304 
    305 	for (i = 0; i < map->maplen; i++) {
    306 		mp = map->map + i;
    307 
    308 		if (mp->command == KS_voidSymbol &&
    309 		    mp->group1[0] == KS_voidSymbol &&
    310 		    mp->group1[1] == KS_voidSymbol &&
    311 		    mp->group2[0] == KS_voidSymbol &&
    312 		    mp->group2[1] == KS_voidSymbol)
    313 			continue;
    314 		printf("\n");
    315 		printf("keycode %u =", i);
    316 		if (mp->command != KS_voidSymbol)
    317 			printf(" %s", ksym2name(mp->command));
    318 		printf(" %s", ksym2name(mp->group1[0]));
    319 		if (mp->group1[0] != mp->group1[1] ||
    320 		    mp->group1[0] != mp->group2[0] ||
    321 		    mp->group1[0] != mp->group2[1]) {
    322 			printf(" %s", ksym2name(mp->group1[1]));
    323 			if (mp->group1[0] != mp->group2[0] ||
    324 			    mp->group1[1] != mp->group2[1]) {
    325 				printf(" %s", ksym2name(mp->group2[0]));
    326 				printf(" %s", ksym2name(mp->group2[1]));
    327 			}
    328 		}
    329 	}
    330 }
    331