Home | History | Annotate | Line # | Download | only in wsconsctl
util.c revision 1.16
      1 /*	$NetBSD: util.c,v 1.16 2003/01/31 23:32:21 thomas 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 	{ WSKBD_TYPE_HPC_KBD,		"hpc-kbd" },
     65 	{ WSKBD_TYPE_HPC_BTN,		"hpc-btn" },
     66 	{ WSKBD_TYPE_ARCHIMEDES,	"archimedes" },
     67 	{ WSKBD_TYPE_RISCPC,		"riscpc" },
     68 	{ WSKBD_TYPE_ADB,		"adb" },
     69 	{ WSKBD_TYPE_HIL,		"hil" },
     70 	{ WSKBD_TYPE_AMIGA,		"amiga" },
     71 	{ WSKBD_TYPE_MAPLE,		"maple" },
     72 	{ WSKBD_TYPE_ATARI,		"atari" },
     73 };
     74 
     75 static struct nameint mstype_tab[] = {
     76 	{ WSMOUSE_TYPE_VSXXX,		"dec-tc" },
     77 	{ WSMOUSE_TYPE_PS2,		"ps2" },
     78 	{ WSMOUSE_TYPE_USB,		"usb" },
     79 	{ WSMOUSE_TYPE_LMS,		"logitech-bus" },
     80 	{ WSMOUSE_TYPE_MMS,		"ms-inport" },
     81 	{ WSMOUSE_TYPE_TPANEL,		"touch-panel" },
     82 	{ WSMOUSE_TYPE_NEXT,		"next" },
     83 	{ WSMOUSE_TYPE_ARCHIMEDES,	"archimedes" },
     84 	{ WSMOUSE_TYPE_HIL,		"hil" },
     85 	{ WSMOUSE_TYPE_AMIGA,		"amiga" },
     86 	{ WSMOUSE_TYPE_MAXINE,		"dec-maxine" },
     87 	{ WSMOUSE_TYPE_MAPLE,		"maple" },
     88 };
     89 
     90 static struct nameint dpytype_tab[] = {
     91 	{ WSDISPLAY_TYPE_UNKNOWN,	"unknown" },
     92 	{ WSDISPLAY_TYPE_PM_MONO,	"dec-pm-mono" },
     93 	{ WSDISPLAY_TYPE_PM_COLOR,	"dec-pm-color" },
     94 	{ WSDISPLAY_TYPE_CFB,		"dec-cfb" },
     95 	{ WSDISPLAY_TYPE_XCFB,		"dec-xcfb" },
     96 	{ WSDISPLAY_TYPE_MFB,		"dec-mfb" },
     97 	{ WSDISPLAY_TYPE_SFB,		"dec-sfb" },
     98 	{ WSDISPLAY_TYPE_ISAVGA,	"vga-isa" },
     99 	{ WSDISPLAY_TYPE_PCIVGA,	"vga-pci" },
    100 	{ WSDISPLAY_TYPE_TGA,		"dec-tga-pci" },
    101 	{ WSDISPLAY_TYPE_SFBP,		"dec-sfb+" },
    102 	{ WSDISPLAY_TYPE_PCIMISC,	"generic-pci" },
    103 	{ WSDISPLAY_TYPE_NEXTMONO,	"next-mono" },
    104 	{ WSDISPLAY_TYPE_PX,		"dec-px" },
    105 	{ WSDISPLAY_TYPE_PXG,		"dec-pxg" },
    106 	{ WSDISPLAY_TYPE_TX,		"dec-tx" },
    107 	{ WSDISPLAY_TYPE_HPCFB,		"generic-hpc" },
    108 	{ WSDISPLAY_TYPE_VIDC,		"arm-vidc" },
    109 	{ WSDISPLAY_TYPE_SPX,		"dec-spx" },
    110 	{ WSDISPLAY_TYPE_GPX,		"dec-gpx" },
    111 	{ WSDISPLAY_TYPE_LCG,		"dec-lcg" },
    112 	{ WSDISPLAY_TYPE_VAX_MONO,	"dec-vax-mono" },
    113 	{ WSDISPLAY_TYPE_SB_P9100,	"sparcbook-p9100" },
    114 	{ WSDISPLAY_TYPE_EGA,		"ega" },
    115 	{ WSDISPLAY_TYPE_DCPVR,		"dreamcast-pvr" },
    116 	{ WSDISPLAY_TYPE_GATOR,		"hp-gator" },
    117 	{ WSDISPLAY_TYPE_TOPCAT,	"hp-topcat" },
    118 	{ WSDISPLAY_TYPE_RENAISSANCE,	"hp-renaissance" },
    119 	{ WSDISPLAY_TYPE_CATSEYE,	"hp-catseye" },
    120 	{ WSDISPLAY_TYPE_DAVINCI,	"hp-davinci" },
    121 	{ WSDISPLAY_TYPE_TIGER,		"hp-tiger" },
    122 	{ WSDISPLAY_TYPE_HYPERION,	"hp-hyperion" },
    123 	{ WSDISPLAY_TYPE_AMIGACC,	"amiga-cc" },
    124 };
    125 
    126 static struct nameint kbdenc_tab[] = {
    127 	KB_ENCTAB
    128 };
    129 
    130 static struct nameint kbdvar_tab[] = {
    131 	KB_VARTAB
    132 };
    133 
    134 static struct field *field_tab;
    135 static int field_tab_len;
    136 
    137 static char *int2name __P((int, int, struct nameint *, int));
    138 static int name2int __P((char *, struct nameint *, int));
    139 static void print_kmap __P((struct wskbd_map_data *));
    140 
    141 void
    142 field_setup(ftab, len)
    143 	struct field *ftab;
    144 	int len;
    145 {
    146 	field_tab = ftab;
    147 	field_tab_len = len;
    148 }
    149 
    150 struct field *
    151 field_by_name(name)
    152 	char *name;
    153 {
    154 	int i;
    155 
    156 	for (i = 0; i < field_tab_len; i++)
    157 		if (strcmp(field_tab[i].name, name) == 0)
    158 			return(field_tab + i);
    159 
    160 	errx(1, "%s: not found", name);
    161 }
    162 
    163 struct field *
    164 field_by_value(addr)
    165 	void *addr;
    166 {
    167 	int i;
    168 
    169 	for (i = 0; i < field_tab_len; i++)
    170 		if (field_tab[i].valp == addr)
    171 			return(field_tab + i);
    172 
    173 	errx(1, "internal error: field_by_value: not found");
    174 }
    175 
    176 static char *
    177 int2name(val, uflag, tab, len)
    178 	int val;
    179 	int uflag;
    180 	struct nameint *tab;
    181 	int len;
    182 {
    183 	static char tmp[20];
    184 	int i;
    185 
    186 	for (i = 0; i < len; i++)
    187 		if (tab[i].value == val)
    188 			return(tab[i].name);
    189 
    190 	if (uflag) {
    191 		snprintf(tmp, sizeof(tmp), "unknown_%d", val);
    192 		return(tmp);
    193 	} else
    194 		return(NULL);
    195 }
    196 
    197 static int
    198 name2int(val, tab, len)
    199 	char *val;
    200 	struct nameint *tab;
    201 	int len;
    202 {
    203 	int i;
    204 
    205 	for (i = 0; i < len; i++)
    206 		if (strcmp(tab[i].name, val) == 0)
    207 			return(tab[i].value);
    208 	return(-1);
    209 }
    210 
    211 void
    212 pr_field(f, sep)
    213 	struct field *f;
    214 	char *sep;
    215 {
    216 	char *p;
    217 	u_int flags;
    218 	int i;
    219 
    220 	if (sep)
    221 		printf("%s%s", f->name, sep);
    222 
    223 	switch (f->format) {
    224 	case FMT_UINT:
    225 		printf("%u", *((u_int *) f->valp));
    226 		break;
    227 	case FMT_STRING:
    228 		printf("\"%s\"", *((char **) f->valp));
    229 		break;
    230 	case FMT_KBDTYPE:
    231 		p = int2name(*((u_int *) f->valp), 1,
    232 			     kbtype_tab, TABLEN(kbtype_tab));
    233 		printf("%s", p);
    234 		break;
    235 	case FMT_MSTYPE:
    236 		p = int2name(*((u_int *) f->valp), 1,
    237 			     mstype_tab, TABLEN(mstype_tab));
    238 		printf("%s", p);
    239 		break;
    240 	case FMT_DPYTYPE:
    241 		p = int2name(*((u_int *) f->valp), 1,
    242 			     dpytype_tab, TABLEN(dpytype_tab));
    243 		printf("%s", p);
    244 		break;
    245 	case FMT_KBDENC:
    246 		p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1,
    247 			     kbdenc_tab, TABLEN(kbdenc_tab));
    248 		printf("%s", p);
    249 
    250 		flags = KB_VARIANT(*((u_int *) f->valp));
    251 		for (i = 0; i < 32; i++) {
    252 			if (!(flags & (1 << i)))
    253 				continue;
    254 			p = int2name(flags & (1 << i), 1,
    255 				     kbdvar_tab, TABLEN(kbdvar_tab));
    256 			printf(".%s", p);
    257 		}
    258 		break;
    259 	case FMT_KBMAP:
    260 		print_kmap((struct wskbd_map_data *) f->valp);
    261 		break;
    262 	default:
    263 		errx(1, "internal error: pr_field: no format %d", f->format);
    264 		break;
    265 	}
    266 
    267 	printf("\n");
    268 }
    269 
    270 void
    271 rd_field(f, val, merge)
    272 	struct field *f;
    273 	char *val;
    274 	int merge;
    275 {
    276 	int i;
    277 	u_int u;
    278 	char *p;
    279 	struct wscons_keymap *mp;
    280 
    281 	switch (f->format) {
    282 	case FMT_UINT:
    283 		if (sscanf(val, "%u", &u) != 1)
    284 			errx(1, "%s: not a number", val);
    285 		if (merge)
    286 			*((u_int *) f->valp) += u;
    287 		else
    288 			*((u_int *) f->valp) = u;
    289 		break;
    290 	case FMT_STRING:
    291 		if ((*((char **) f->valp) = strdup(val)) == NULL)
    292 			err(1, "strdup");
    293 		break;
    294 	case FMT_KBDENC:
    295 		p = strchr(val, '.');
    296 		if (p != NULL)
    297 			*p++ = '\0';
    298 
    299 		i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab));
    300 		if (i == -1)
    301 			errx(1, "%s: not a valid encoding", val);
    302 		*((u_int *) f->valp) = i;
    303 
    304 		while (p) {
    305 			val = p;
    306 			p = strchr(p, '.');
    307 			if (p != NULL)
    308 				*p++ = '\0';
    309 			i = name2int(val, kbdvar_tab, TABLEN(kbdvar_tab));
    310 			if (i == -1)
    311 				errx(1, "%s: not a valid variant", val);
    312 			*((u_int *) f->valp) |= i;
    313 		}
    314 		break;
    315 	case FMT_KBMAP:
    316 		if (! merge)
    317 			kbmap.maplen = 0;
    318 		map_scan_setinput(val);
    319 		yyparse();
    320 		if (merge) {
    321 			if (newkbmap.maplen < kbmap.maplen)
    322 				newkbmap.maplen = kbmap.maplen;
    323 			for (i = 0; i < kbmap.maplen; i++) {
    324 				mp = newkbmap.map + i;
    325 				if (mp->command == KS_voidSymbol &&
    326 				    mp->group1[0] == KS_voidSymbol &&
    327 				    mp->group1[1] == KS_voidSymbol &&
    328 				    mp->group2[0] == KS_voidSymbol &&
    329 				    mp->group2[1] == KS_voidSymbol)
    330 					*mp = kbmap.map[i];
    331 			}
    332 		}
    333 		kbmap.maplen = newkbmap.maplen;
    334 		bcopy(newkbmap.map, kbmap.map,
    335 		      kbmap.maplen*sizeof(struct wscons_keymap));
    336 		break;
    337 	default:
    338 		errx(1, "internal error: rd_field: no format %d", f->format);
    339 		break;
    340 	}
    341 }
    342 
    343 static void
    344 print_kmap(map)
    345 	struct wskbd_map_data *map;
    346 {
    347 	int i;
    348 	struct wscons_keymap *mp;
    349 
    350 	for (i = 0; i < map->maplen; i++) {
    351 		mp = map->map + i;
    352 
    353 		if (mp->command == KS_voidSymbol &&
    354 		    mp->group1[0] == KS_voidSymbol &&
    355 		    mp->group1[1] == KS_voidSymbol &&
    356 		    mp->group2[0] == KS_voidSymbol &&
    357 		    mp->group2[1] == KS_voidSymbol)
    358 			continue;
    359 		printf("\n");
    360 		printf("keycode %u =", i);
    361 		if (mp->command != KS_voidSymbol)
    362 			printf(" %s", ksym2name(mp->command));
    363 		printf(" %s", ksym2name(mp->group1[0]));
    364 		if (mp->group1[0] != mp->group1[1] ||
    365 		    mp->group1[0] != mp->group2[0] ||
    366 		    mp->group1[0] != mp->group2[1]) {
    367 			printf(" %s", ksym2name(mp->group1[1]));
    368 			if (mp->group1[0] != mp->group2[0] ||
    369 			    mp->group1[1] != mp->group2[1]) {
    370 				printf(" %s", ksym2name(mp->group2[0]));
    371 				printf(" %s", ksym2name(mp->group2[1]));
    372 			}
    373 		}
    374 	}
    375 }
    376