Home | History | Annotate | Line # | Download | only in wsconsctl
util.c revision 1.22
      1 /*	$NetBSD: util.c,v 1.22 2005/01/31 06:24:08 joff 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 	{ WSKBD_TYPE_SUN,		"sun" },
     74 	{ WSKBD_TYPE_SUN5,		"sun-type5" },
     75 	{ WSKBD_TYPE_SGI,		"sgi" },
     76 	{ WSKBD_TYPE_MATRIXKP,		"matrix-keypad" },
     77 };
     78 
     79 static struct nameint mstype_tab[] = {
     80 	{ WSMOUSE_TYPE_VSXXX,		"dec-tc" },
     81 	{ WSMOUSE_TYPE_PS2,		"ps2" },
     82 	{ WSMOUSE_TYPE_USB,		"usb" },
     83 	{ WSMOUSE_TYPE_LMS,		"logitech-bus" },
     84 	{ WSMOUSE_TYPE_MMS,		"ms-inport" },
     85 	{ WSMOUSE_TYPE_TPANEL,		"touch-panel" },
     86 	{ WSMOUSE_TYPE_NEXT,		"next" },
     87 	{ WSMOUSE_TYPE_ARCHIMEDES,	"archimedes" },
     88 	{ WSMOUSE_TYPE_HIL,		"hil" },
     89 	{ WSMOUSE_TYPE_AMIGA,		"amiga" },
     90 	{ WSMOUSE_TYPE_MAXINE,		"dec-maxine" },
     91 	{ WSMOUSE_TYPE_MAPLE,		"maple" },
     92 };
     93 
     94 static struct nameint dpytype_tab[] = {
     95 	{ WSDISPLAY_TYPE_UNKNOWN,	"unknown" },
     96 	{ WSDISPLAY_TYPE_PM_MONO,	"dec-pm-mono" },
     97 	{ WSDISPLAY_TYPE_PM_COLOR,	"dec-pm-color" },
     98 	{ WSDISPLAY_TYPE_CFB,		"dec-cfb" },
     99 	{ WSDISPLAY_TYPE_XCFB,		"dec-xcfb" },
    100 	{ WSDISPLAY_TYPE_MFB,		"dec-mfb" },
    101 	{ WSDISPLAY_TYPE_SFB,		"dec-sfb" },
    102 	{ WSDISPLAY_TYPE_ISAVGA,	"vga-isa" },
    103 	{ WSDISPLAY_TYPE_PCIVGA,	"vga-pci" },
    104 	{ WSDISPLAY_TYPE_TGA,		"dec-tga-pci" },
    105 	{ WSDISPLAY_TYPE_SFBP,		"dec-sfb+" },
    106 	{ WSDISPLAY_TYPE_PCIMISC,	"generic-pci" },
    107 	{ WSDISPLAY_TYPE_NEXTMONO,	"next-mono" },
    108 	{ WSDISPLAY_TYPE_PX,		"dec-px" },
    109 	{ WSDISPLAY_TYPE_PXG,		"dec-pxg" },
    110 	{ WSDISPLAY_TYPE_TX,		"dec-tx" },
    111 	{ WSDISPLAY_TYPE_HPCFB,		"generic-hpc" },
    112 	{ WSDISPLAY_TYPE_VIDC,		"arm-vidc" },
    113 	{ WSDISPLAY_TYPE_SPX,		"dec-spx" },
    114 	{ WSDISPLAY_TYPE_GPX,		"dec-gpx" },
    115 	{ WSDISPLAY_TYPE_LCG,		"dec-lcg" },
    116 	{ WSDISPLAY_TYPE_VAX_MONO,	"dec-vax-mono" },
    117 	{ WSDISPLAY_TYPE_SB_P9100,	"sparcbook-p9100" },
    118 	{ WSDISPLAY_TYPE_EGA,		"ega" },
    119 	{ WSDISPLAY_TYPE_DCPVR,		"dreamcast-pvr" },
    120 	{ WSDISPLAY_TYPE_GATOR,		"hp-gator" },
    121 	{ WSDISPLAY_TYPE_TOPCAT,	"hp-topcat" },
    122 	{ WSDISPLAY_TYPE_RENAISSANCE,	"hp-renaissance" },
    123 	{ WSDISPLAY_TYPE_CATSEYE,	"hp-catseye" },
    124 	{ WSDISPLAY_TYPE_DAVINCI,	"hp-davinci" },
    125 	{ WSDISPLAY_TYPE_TIGER,		"hp-tiger" },
    126 	{ WSDISPLAY_TYPE_HYPERION,	"hp-hyperion" },
    127 	{ WSDISPLAY_TYPE_AMIGACC,	"amiga-cc" },
    128 	{ WSDISPLAY_TYPE_SUN24,		"sun24" },
    129 	{ WSDISPLAY_TYPE_NEWPORT,	"sgi-newport" },
    130 	{ WSDISPLAY_TYPE_GR2,		"sgi-gr2" },
    131 	{ WSDISPLAY_TYPE_SUNCG12,	"suncg12" },
    132 	{ WSDISPLAY_TYPE_SUNCG14,	"suncg14" },
    133 	{ WSDISPLAY_TYPE_SUNTCX,	"suntcx" },
    134 	{ WSDISPLAY_TYPE_SUNFFB,	"sunffb" },
    135 };
    136 
    137 static struct nameint kbdenc_tab[] = {
    138 	KB_ENCTAB
    139 };
    140 
    141 static struct nameint kbdvar_tab[] = {
    142 	KB_VARTAB
    143 };
    144 
    145 static struct nameint color_tab[] = {
    146 	{ WSCOL_UNSUPPORTED,		"unsupported" },
    147 	{ WSCOL_BLACK,			"black" },
    148 	{ WSCOL_RED,			"red" },
    149 	{ WSCOL_GREEN,			"green" },
    150 	{ WSCOL_BROWN,			"brown" },
    151 	{ WSCOL_BLUE,			"blue" },
    152 	{ WSCOL_MAGENTA,		"magenta" },
    153 	{ WSCOL_CYAN,			"cyan" },
    154 	{ WSCOL_WHITE,			"white" },
    155 };
    156 
    157 static struct nameint attr_tab[] = {
    158 	{ WSATTR_NONE,			"none" },
    159 	{ WSATTR_REVERSE,		"reverse" },
    160 	{ WSATTR_HILIT,			"hilit" },
    161 	{ WSATTR_BLINK,			"blink" },
    162 	{ WSATTR_UNDERLINE,		"underline" },
    163 	{ WSATTR_WSCOLORS,		"color" },
    164 };
    165 
    166 static struct field *field_tab;
    167 static int field_tab_len;
    168 
    169 static char *int2name(int, int, struct nameint *, int);
    170 static int name2int(char *, struct nameint *, int);
    171 static void print_kmap(struct wskbd_map_data *);
    172 
    173 void
    174 field_setup(struct field *ftab, int len)
    175 {
    176 	field_tab = ftab;
    177 	field_tab_len = len;
    178 }
    179 
    180 struct field *
    181 field_by_name(char *name)
    182 {
    183 	int i;
    184 
    185 	for (i = 0; i < field_tab_len; i++)
    186 		if (strcmp(field_tab[i].name, name) == 0)
    187 			return(field_tab + i);
    188 
    189 	errx(1, "%s: not found", name);
    190 }
    191 
    192 struct field *
    193 field_by_value(void *addr)
    194 {
    195 	int i;
    196 
    197 	for (i = 0; i < field_tab_len; i++)
    198 		if (field_tab[i].valp == addr)
    199 			return(field_tab + i);
    200 
    201 	errx(1, "internal error: field_by_value: not found");
    202 }
    203 
    204 void
    205 field_disable_by_value(void *addr)
    206 {
    207 	struct field *f;
    208 
    209 	f = field_by_value(addr);
    210 	f->flags |= FLG_DISABLED;
    211 }
    212 
    213 static char *
    214 int2name(int val, int uflag, struct nameint *tab, int len)
    215 {
    216 	static char tmp[20];
    217 	int i;
    218 
    219 	for (i = 0; i < len; i++)
    220 		if (tab[i].value == val)
    221 			return(tab[i].name);
    222 
    223 	if (uflag) {
    224 		snprintf(tmp, sizeof(tmp), "unknown_%d", val);
    225 		return(tmp);
    226 	} else
    227 		return(NULL);
    228 }
    229 
    230 static int
    231 name2int(char *val, struct nameint *tab, int len)
    232 {
    233 	int i;
    234 
    235 	for (i = 0; i < len; i++)
    236 		if (strcmp(tab[i].name, val) == 0)
    237 			return(tab[i].value);
    238 	return(-1);
    239 }
    240 
    241 void
    242 pr_field(struct field *f, char *sep)
    243 {
    244 	char *p;
    245 	u_int flags;
    246 	int first, i, mask;
    247 
    248 	if (sep)
    249 		printf("%s%s", f->name, sep);
    250 
    251 	switch (f->format) {
    252 	case FMT_UINT:
    253 		printf("%u", *((u_int *) f->valp));
    254 		break;
    255 	case FMT_STRING:
    256 		printf("\"%s\"", *((char **) f->valp));
    257 		break;
    258 	case FMT_KBDTYPE:
    259 		p = int2name(*((u_int *) f->valp), 1,
    260 			     kbtype_tab, TABLEN(kbtype_tab));
    261 		printf("%s", p);
    262 		break;
    263 	case FMT_MSTYPE:
    264 		p = int2name(*((u_int *) f->valp), 1,
    265 			     mstype_tab, TABLEN(mstype_tab));
    266 		printf("%s", p);
    267 		break;
    268 	case FMT_DPYTYPE:
    269 		p = int2name(*((u_int *) f->valp), 1,
    270 			     dpytype_tab, TABLEN(dpytype_tab));
    271 		printf("%s", p);
    272 		break;
    273 	case FMT_KBDENC:
    274 		p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1,
    275 			     kbdenc_tab, TABLEN(kbdenc_tab));
    276 		printf("%s", p);
    277 
    278 		flags = KB_VARIANT(*((u_int *) f->valp));
    279 		for (i = 0; i < 32; i++) {
    280 			if (!(flags & (1 << i)))
    281 				continue;
    282 			p = int2name(flags & (1 << i), 1,
    283 				     kbdvar_tab, TABLEN(kbdvar_tab));
    284 			printf(".%s", p);
    285 		}
    286 		break;
    287 	case FMT_KBMAP:
    288 		print_kmap((struct wskbd_map_data *) f->valp);
    289 		break;
    290 	case FMT_COLOR:
    291 		p = int2name(*((u_int *) f->valp), 1,
    292 			     color_tab, TABLEN(color_tab));
    293 		printf("%s", p);
    294 		break;
    295 	case FMT_ATTRS:
    296 		mask = 0x10;
    297 		first = 1;
    298 		while (mask > 0) {
    299 			if (*((u_int *) f->valp) & mask) {
    300 				p = int2name(*((u_int *) f->valp) & mask, 1,
    301 					     attr_tab, TABLEN(attr_tab));
    302 				printf("%s%s", first ? "" : ",", p);
    303 				first = 0;
    304 			}
    305 			mask >>= 1;
    306 		}
    307 		if (first)
    308 			printf("none");
    309 		break;
    310 	default:
    311 		errx(1, "internal error: pr_field: no format %d", f->format);
    312 		break;
    313 	}
    314 
    315 	printf("\n");
    316 }
    317 
    318 void
    319 rd_field(struct field *f, char *val, int merge)
    320 {
    321 	int i;
    322 	u_int u;
    323 	char *p;
    324 	struct wscons_keymap *mp;
    325 
    326 	switch (f->format) {
    327 	case FMT_UINT:
    328 		if (sscanf(val, "%u", &u) != 1)
    329 			errx(1, "%s: not a number", val);
    330 		if (merge)
    331 			*((u_int *) f->valp) += u;
    332 		else
    333 			*((u_int *) f->valp) = u;
    334 		break;
    335 	case FMT_STRING:
    336 		if ((*((char **) f->valp) = strdup(val)) == NULL)
    337 			err(1, "strdup");
    338 		break;
    339 	case FMT_KBDENC:
    340 		p = strchr(val, '.');
    341 		if (p != NULL)
    342 			*p++ = '\0';
    343 
    344 		i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab));
    345 		if (i == -1)
    346 			errx(1, "%s: not a valid encoding", val);
    347 		*((u_int *) f->valp) = i;
    348 
    349 		while (p) {
    350 			val = p;
    351 			p = strchr(p, '.');
    352 			if (p != NULL)
    353 				*p++ = '\0';
    354 			i = name2int(val, kbdvar_tab, TABLEN(kbdvar_tab));
    355 			if (i == -1)
    356 				errx(1, "%s: not a valid variant", val);
    357 			*((u_int *) f->valp) |= i;
    358 		}
    359 		break;
    360 	case FMT_KBMAP:
    361 		if (! merge)
    362 			kbmap.maplen = 0;
    363 		map_scan_setinput(val);
    364 		yyparse();
    365 		if (merge) {
    366 			if (newkbmap.maplen < kbmap.maplen)
    367 				newkbmap.maplen = kbmap.maplen;
    368 			for (i = 0; i < kbmap.maplen; i++) {
    369 				mp = newkbmap.map + i;
    370 				if (mp->command == KS_voidSymbol &&
    371 				    mp->group1[0] == KS_voidSymbol &&
    372 				    mp->group1[1] == KS_voidSymbol &&
    373 				    mp->group2[0] == KS_voidSymbol &&
    374 				    mp->group2[1] == KS_voidSymbol)
    375 					*mp = kbmap.map[i];
    376 			}
    377 		}
    378 		kbmap.maplen = newkbmap.maplen;
    379 		bcopy(newkbmap.map, kbmap.map,
    380 		      kbmap.maplen*sizeof(struct wscons_keymap));
    381 		break;
    382 	case FMT_COLOR:
    383 		i = name2int(val, color_tab, TABLEN(color_tab));
    384 		if (i == -1)
    385 			errx(1, "%s: not a valid color", val);
    386 		*((u_int *) f->valp) = i;
    387 		break;
    388 	case FMT_ATTRS:
    389 		p = val;
    390 		while (p) {
    391 			val = p;
    392 			p = strchr(p, ',');
    393 			if (p != NULL)
    394 				*p++ = '\0';
    395 			i = name2int(val, attr_tab, TABLEN(attr_tab));
    396 			if (i == -1)
    397 				errx(1, "%s: not a valid attribute", val);
    398 			*((u_int *) f->valp) |= i;
    399 		}
    400 		break;
    401 	default:
    402 		errx(1, "internal error: rd_field: no format %d", f->format);
    403 		break;
    404 	}
    405 }
    406 
    407 static void
    408 print_kmap(struct wskbd_map_data *map)
    409 {
    410 	int i;
    411 	struct wscons_keymap *mp;
    412 
    413 	for (i = 0; i < map->maplen; i++) {
    414 		mp = map->map + i;
    415 
    416 		if (mp->command == KS_voidSymbol &&
    417 		    mp->group1[0] == KS_voidSymbol &&
    418 		    mp->group1[1] == KS_voidSymbol &&
    419 		    mp->group2[0] == KS_voidSymbol &&
    420 		    mp->group2[1] == KS_voidSymbol)
    421 			continue;
    422 		printf("\n");
    423 		printf("keycode %u =", i);
    424 		if (mp->command != KS_voidSymbol)
    425 			printf(" %s", ksym2name(mp->command));
    426 		printf(" %s", ksym2name(mp->group1[0]));
    427 		if (mp->group1[0] != mp->group1[1] ||
    428 		    mp->group1[0] != mp->group2[0] ||
    429 		    mp->group1[0] != mp->group2[1]) {
    430 			printf(" %s", ksym2name(mp->group1[1]));
    431 			if (mp->group1[0] != mp->group2[0] ||
    432 			    mp->group1[1] != mp->group2[1]) {
    433 				printf(" %s", ksym2name(mp->group2[0]));
    434 				printf(" %s", ksym2name(mp->group2[1]));
    435 			}
    436 		}
    437 	}
    438 }
    439