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