Home | History | Annotate | Line # | Download | only in wsconsctl
util.c revision 1.33
      1 /*	$NetBSD: util.c,v 1.33 2021/12/25 13:54:13 mlelstv Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998, 2006, 2012 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 and Julio M. Merino Vidal.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/time.h>
     33 
     34 #include <dev/wscons/wsconsio.h>
     35 #include <dev/wscons/wsksymdef.h>
     36 #include <dev/videomode/videomode.h>
     37 #include <dev/videomode/edidreg.h>
     38 #include <dev/videomode/edidvar.h>
     39 
     40 #include <err.h>
     41 #include <errno.h>
     42 #include <limits.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 
     48 #include "wsconsctl.h"
     49 
     50 #define TABLEN(t)		(sizeof(t)/sizeof(t[0]))
     51 
     52 extern struct wskbd_map_data kbmap;	/* from keyboard.c */
     53 extern struct wskbd_map_data newkbmap;	/* from map_parse.y */
     54 
     55 struct nameint {
     56 	int value;
     57 	const char *name;
     58 };
     59 
     60 static struct nameint kbtype_tab[] = {
     61 	{ WSKBD_TYPE_LK201,		"lk201" },
     62 	{ WSKBD_TYPE_LK401,		"lk401" },
     63 	{ WSKBD_TYPE_PC_XT,		"pc-xt" },
     64 	{ WSKBD_TYPE_PC_AT,		"pc-at" },
     65 	{ WSKBD_TYPE_USB,		"usb" },
     66 	{ WSKBD_TYPE_HPC_KBD,		"hpc-kbd" },
     67 	{ WSKBD_TYPE_HPC_BTN,		"hpc-btn" },
     68 	{ WSKBD_TYPE_ARCHIMEDES,	"archimedes" },
     69 	{ WSKBD_TYPE_RISCPC,		"riscpc" },
     70 	{ WSKBD_TYPE_ADB,		"adb" },
     71 	{ WSKBD_TYPE_HIL,		"hil" },
     72 	{ WSKBD_TYPE_AMIGA,		"amiga" },
     73 	{ WSKBD_TYPE_MAPLE,		"maple" },
     74 	{ WSKBD_TYPE_ATARI,		"atari" },
     75 	{ WSKBD_TYPE_SUN,		"sun" },
     76 	{ WSKBD_TYPE_SUN5,		"sun-type5" },
     77 	{ WSKBD_TYPE_SGI,		"sgi" },
     78 	{ WSKBD_TYPE_MATRIXKP,		"matrix-keypad" },
     79 	{ WSKBD_TYPE_BLUETOOTH,		"bluetooth" },
     80 };
     81 
     82 static struct nameint mstype_tab[] = {
     83 	{ WSMOUSE_TYPE_VSXXX,		"dec-tc" },
     84 	{ WSMOUSE_TYPE_PS2,		"ps2" },
     85 	{ WSMOUSE_TYPE_USB,		"usb" },
     86 	{ WSMOUSE_TYPE_LMS,		"logitech-bus" },
     87 	{ WSMOUSE_TYPE_MMS,		"ms-inport" },
     88 	{ WSMOUSE_TYPE_TPANEL,		"touch-panel" },
     89 	{ WSMOUSE_TYPE_NEXT,		"next" },
     90 	{ WSMOUSE_TYPE_ARCHIMEDES,	"archimedes" },
     91 	{ WSMOUSE_TYPE_HIL,		"hil" },
     92 	{ WSMOUSE_TYPE_AMIGA,		"amiga" },
     93 	{ WSMOUSE_TYPE_MAXINE,		"dec-maxine" },
     94 	{ WSMOUSE_TYPE_MAPLE,		"maple" },
     95 	{ WSMOUSE_TYPE_BLUETOOTH,	"bluetooth" },
     96 };
     97 
     98 static struct nameint dpytype_tab[] = {
     99 	{ WSDISPLAY_TYPE_UNKNOWN,	"unknown" },
    100 	{ WSDISPLAY_TYPE_PM_MONO,	"dec-pm-mono" },
    101 	{ WSDISPLAY_TYPE_PM_COLOR,	"dec-pm-color" },
    102 	{ WSDISPLAY_TYPE_CFB,		"dec-cfb" },
    103 	{ WSDISPLAY_TYPE_XCFB,		"dec-xcfb" },
    104 	{ WSDISPLAY_TYPE_MFB,		"dec-mfb" },
    105 	{ WSDISPLAY_TYPE_SFB,		"dec-sfb" },
    106 	{ WSDISPLAY_TYPE_ISAVGA,	"vga-isa" },
    107 	{ WSDISPLAY_TYPE_PCIVGA,	"vga-pci" },
    108 	{ WSDISPLAY_TYPE_TGA,		"dec-tga-pci" },
    109 	{ WSDISPLAY_TYPE_SFBP,		"dec-sfb+" },
    110 	{ WSDISPLAY_TYPE_PCIMISC,	"generic-pci" },
    111 	{ WSDISPLAY_TYPE_NEXTMONO,	"next-mono" },
    112 	{ WSDISPLAY_TYPE_PX,		"dec-px" },
    113 	{ WSDISPLAY_TYPE_PXG,		"dec-pxg" },
    114 	{ WSDISPLAY_TYPE_TX,		"dec-tx" },
    115 	{ WSDISPLAY_TYPE_HPCFB,		"generic-hpc" },
    116 	{ WSDISPLAY_TYPE_VIDC,		"arm-vidc" },
    117 	{ WSDISPLAY_TYPE_SPX,		"dec-spx" },
    118 	{ WSDISPLAY_TYPE_GPX,		"dec-gpx" },
    119 	{ WSDISPLAY_TYPE_LCG,		"dec-lcg" },
    120 	{ WSDISPLAY_TYPE_VAX_MONO,	"dec-vax-mono" },
    121 	{ WSDISPLAY_TYPE_SB_P9100,	"sparcbook-p9100" },
    122 	{ WSDISPLAY_TYPE_EGA,		"ega" },
    123 	{ WSDISPLAY_TYPE_DCPVR,		"dreamcast-pvr" },
    124 	{ WSDISPLAY_TYPE_GBOX,		"hp-gator" },
    125 	{ WSDISPLAY_TYPE_TOPCAT,	"hp-topcat" },
    126 	{ WSDISPLAY_TYPE_RBOX,		"hp-renaissance" },
    127 	{ WSDISPLAY_TYPE_CATSEYE,	"hp-catseye" },
    128 	{ WSDISPLAY_TYPE_DVBOX,		"hp-davinci" },
    129 	{ WSDISPLAY_TYPE_TVRX,		"hp-tiger" },
    130 	{ WSDISPLAY_TYPE_HYPERION,	"hp-hyperion" },
    131 	{ WSDISPLAY_TYPE_AMIGACC,	"amiga-cc" },
    132 	{ WSDISPLAY_TYPE_GRF,		"grf" },
    133 	{ WSDISPLAY_TYPE_SUN24,		"sun24" },
    134 	{ WSDISPLAY_TYPE_NEWPORT,	"sgi-newport" },
    135 	{ WSDISPLAY_TYPE_GR2,		"sgi-gr2" },
    136 	{ WSDISPLAY_TYPE_SUNCG12,	"suncg12" },
    137 	{ WSDISPLAY_TYPE_SUNCG14,	"suncg14" },
    138 	{ WSDISPLAY_TYPE_SUNTCX,	"suntcx" },
    139 	{ WSDISPLAY_TYPE_SUNFFB,	"sunffb" },
    140 };
    141 
    142 static struct nameint kbdenc_tab[] = {
    143 	KB_ENCTAB
    144 };
    145 
    146 static struct nameint kbdvar_tab[] = {
    147 	KB_VARTAB
    148 };
    149 
    150 static struct nameint color_tab[] = {
    151 	{ WSCOL_UNSUPPORTED,		"unsupported" },
    152 	{ WSCOL_BLACK,			"black" },
    153 	{ WSCOL_RED,			"red" },
    154 	{ WSCOL_GREEN,			"green" },
    155 	{ WSCOL_BROWN,			"brown" },
    156 	{ WSCOL_BLUE,			"blue" },
    157 	{ WSCOL_MAGENTA,		"magenta" },
    158 	{ WSCOL_CYAN,			"cyan" },
    159 	{ WSCOL_WHITE,			"white" },
    160 };
    161 
    162 static struct nameint attr_tab[] = {
    163 	{ WSATTR_NONE,			"none" },
    164 	{ WSATTR_REVERSE,		"reverse" },
    165 	{ WSATTR_HILIT,			"hilit" },
    166 	{ WSATTR_BLINK,			"blink" },
    167 	{ WSATTR_UNDERLINE,		"underline" },
    168 	{ WSATTR_WSCOLORS,		"color" },
    169 };
    170 
    171 static struct field *field_tab;
    172 static int field_tab_len;
    173 
    174 static const char *int2name(int, int, struct nameint *, int);
    175 static int name2int(char *, struct nameint *, int);
    176 static void print_kmap(struct wskbd_map_data *);
    177 static unsigned int rd_bitfield(const char *);
    178 static void pr_bitfield(unsigned int);
    179 
    180 void
    181 field_setup(struct field *ftab, int len)
    182 {
    183 
    184 	field_tab = ftab;
    185 	field_tab_len = len;
    186 }
    187 
    188 struct field *
    189 field_by_name(char *name)
    190 {
    191 	int i;
    192 
    193 	for (i = 0; i < field_tab_len; i++)
    194 		if (strcmp(field_tab[i].name, name) == 0)
    195 			return field_tab + i;
    196 
    197 	errx(EXIT_FAILURE, "%s: not found", name);
    198 }
    199 
    200 struct field *
    201 field_by_value(void *addr)
    202 {
    203 	int i;
    204 
    205 	for (i = 0; i < field_tab_len; i++)
    206 		if (field_tab[i].valp == addr)
    207 			return field_tab + i;
    208 
    209 	errx(EXIT_FAILURE, "internal error: field_by_value: not found");
    210 }
    211 
    212 void
    213 field_disable_by_value(void *addr)
    214 {
    215 	struct field *f;
    216 
    217 	f = field_by_value(addr);
    218 	f->flags |= FLG_DISABLED;
    219 }
    220 
    221 static const char *
    222 int2name(int val, int uflag, struct nameint *tab, int len)
    223 {
    224 	static char tmp[20];
    225 	int i;
    226 
    227 	for (i = 0; i < len; i++)
    228 		if (tab[i].value == val)
    229 			return tab[i].name;
    230 
    231 	if (uflag) {
    232 		(void)snprintf(tmp, sizeof(tmp), "unknown_%d", val);
    233 		return tmp;
    234 	} else
    235 		return NULL;
    236 }
    237 
    238 static int
    239 name2int(char *val, struct nameint *tab, int len)
    240 {
    241 	int i;
    242 
    243 	for (i = 0; i < len; i++)
    244 		if (strcmp(tab[i].name, val) == 0)
    245 			return tab[i].value;
    246 	return -1;
    247 }
    248 
    249 void
    250 pr_field(struct field *f, const char *sep)
    251 {
    252 	const char *p;
    253 	unsigned int flags;
    254 	int first, i, mask;
    255 	struct wsdisplayio_edid_info *info;
    256 	struct edid_info edid;
    257 
    258 	if (sep)
    259 		(void)printf("%s%s", f->name, sep);
    260 
    261 	switch (f->format) {
    262 	case FMT_UINT:
    263 		(void)printf("%u", *((unsigned int *) f->valp));
    264 		break;
    265 	case FMT_INT:
    266 		(void)printf("%d", *((int *) f->valp));
    267 		break;
    268 	case FMT_STRING:
    269 		(void)printf("\"%s\"", *((char **) f->valp));
    270 		break;
    271 	case FMT_BITFIELD:
    272 		pr_bitfield(*((unsigned int *) f->valp));
    273 		break;
    274 	case FMT_KBDTYPE:
    275 		p = int2name(*((unsigned int *) f->valp), 1,
    276 		    kbtype_tab, TABLEN(kbtype_tab));
    277 		(void)printf("%s", p);
    278 		break;
    279 	case FMT_MSTYPE:
    280 		p = int2name(*((unsigned int *) f->valp), 1,
    281 		    mstype_tab, TABLEN(mstype_tab));
    282 		(void)printf("%s", p);
    283 		break;
    284 	case FMT_DPYTYPE:
    285 		p = int2name(*((unsigned int *) f->valp), 1,
    286 		    dpytype_tab, TABLEN(dpytype_tab));
    287 		(void)printf("%s", p);
    288 		break;
    289 	case FMT_KBDENC:
    290 		p = int2name(KB_ENCODING(*((unsigned int *) f->valp)), 1,
    291 		    kbdenc_tab, TABLEN(kbdenc_tab));
    292 		(void)printf("%s", p);
    293 
    294 		flags = KB_VARIANT(*((unsigned int *) f->valp));
    295 		for (i = 0; i < 32; i++) {
    296 			if (!(flags & (1 << i)))
    297 				continue;
    298 			p = int2name(flags & (1 << i), 1,
    299 			    kbdvar_tab, TABLEN(kbdvar_tab));
    300 			(void)printf(".%s", p);
    301 		}
    302 		break;
    303 	case FMT_KBMAP:
    304 		print_kmap((struct wskbd_map_data *) f->valp);
    305 		break;
    306 	case FMT_COLOR:
    307 		p = int2name(*((unsigned int *) f->valp), 1,
    308 		    color_tab, TABLEN(color_tab));
    309 		(void)printf("%s", p);
    310 		break;
    311 	case FMT_ATTRS:
    312 		mask = 0x10;
    313 		first = 1;
    314 		while (mask > 0) {
    315 			if (*((unsigned int *) f->valp) & mask) {
    316 				p = int2name(*((unsigned int *) f->valp) & mask,
    317 				    1, attr_tab, TABLEN(attr_tab));
    318 				(void)printf("%s%s", first ? "" : ",", p);
    319 				first = 0;
    320 			}
    321 			mask >>= 1;
    322 		}
    323 		if (first)
    324 			(void)printf("none");
    325 		break;
    326 	case FMT_EDID:
    327 		info = (struct wsdisplayio_edid_info *)f->valp;
    328 		if (edid_parse(info->edid_data, &edid))
    329 			(void)printf("invalid");
    330 		else {
    331 			(void)printf("\n");
    332 			edid_print(&edid);
    333 		}
    334 		break;
    335 	default:
    336 		errx(EXIT_FAILURE, "internal error: pr_field: no format %d",
    337 		    f->format);
    338 		break;
    339 	}
    340 
    341 	(void)printf("\n");
    342 }
    343 
    344 static void
    345 pr_bitfield(unsigned int f)
    346 {
    347 
    348 	if (f == 0)
    349 		(void)printf("none");
    350 	else {
    351 		unsigned int i;
    352 		int first, mask;
    353 
    354 		for (i = 0, first = 1, mask = 1; i < sizeof(f) * 8; i++) {
    355 			if (f & mask) {
    356 				(void)printf("%s%u", first ? "" : " ", i);
    357 				first = 0;
    358 			}
    359 			mask = mask << 1;
    360 		}
    361 	}
    362 }
    363 
    364 void
    365 rd_field(struct field *f, char *val, int merge)
    366 {
    367 	int i;
    368 	unsigned int u;
    369 	char *p;
    370 	struct wscons_keymap *mp;
    371 
    372 	switch (f->format) {
    373 	case FMT_UINT:
    374 		if (sscanf(val, "%u", &u) != 1)
    375 			errx(EXIT_FAILURE, "%s: not a number", val);
    376 		if (merge)
    377 			*((unsigned int *) f->valp) += u;
    378 		else
    379 			*((unsigned int *) f->valp) = u;
    380 		break;
    381 	case FMT_INT:
    382 		if (sscanf(val, "%d", &i) != 1)
    383 			errx(EXIT_FAILURE, "%s: not a number", val);
    384 		if (merge)
    385 			*((int *) f->valp) += i;
    386 		else
    387 			*((int *) f->valp) = i;
    388 		break;
    389 	case FMT_STRING:
    390 		if ((*((char **) f->valp) = strdup(val)) == NULL)
    391 			err(EXIT_FAILURE, "strdup");
    392 		break;
    393 	case FMT_BITFIELD:
    394 		*((unsigned int *) f->valp) = rd_bitfield(val);
    395 		break;
    396 	case FMT_KBDENC:
    397 		p = strchr(val, '.');
    398 		if (p != NULL)
    399 			*p++ = '\0';
    400 
    401 		i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab));
    402 		if (i == -1)
    403 			errx(EXIT_FAILURE, "%s: not a valid encoding", val);
    404 		*((unsigned int *) f->valp) = i;
    405 
    406 		while (p) {
    407 			val = p;
    408 			p = strchr(p, '.');
    409 			if (p != NULL)
    410 				*p++ = '\0';
    411 			i = name2int(val, kbdvar_tab, TABLEN(kbdvar_tab));
    412 			if (i == -1)
    413 				errx(EXIT_FAILURE, "%s: not a valid variant",
    414 				    val);
    415 			*((unsigned int *) f->valp) |= i;
    416 		}
    417 		break;
    418 	case FMT_KBMAP:
    419 		if (! merge)
    420 			kbmap.maplen = 0;
    421 		map_scan_setinput(val);
    422 		yyparse();
    423 		if (merge) {
    424 			if (newkbmap.maplen < kbmap.maplen)
    425 				newkbmap.maplen = kbmap.maplen;
    426 			for (u = 0; u < kbmap.maplen; u++) {
    427 				mp = newkbmap.map + u;
    428 				if (mp->command == KS_voidSymbol &&
    429 				    mp->group1[0] == KS_voidSymbol &&
    430 				    mp->group1[1] == KS_voidSymbol &&
    431 				    mp->group2[0] == KS_voidSymbol &&
    432 				    mp->group2[1] == KS_voidSymbol)
    433 					*mp = kbmap.map[u];
    434 			}
    435 		}
    436 		kbmap.maplen = newkbmap.maplen;
    437 		memcpy(kbmap.map, newkbmap.map,
    438 		    kbmap.maplen * sizeof(struct wscons_keymap));
    439 		break;
    440 	case FMT_COLOR:
    441 		i = name2int(val, color_tab, TABLEN(color_tab));
    442 		if (i == -1)
    443 			errx(EXIT_FAILURE, "%s: not a valid color", val);
    444 		*((unsigned int *) f->valp) = i;
    445 		break;
    446 	case FMT_ATTRS:
    447 		p = val;
    448 		while (p) {
    449 			val = p;
    450 			p = strchr(p, ',');
    451 			if (p != NULL)
    452 				*p++ = '\0';
    453 			i = name2int(val, attr_tab, TABLEN(attr_tab));
    454 			if (i == -1)
    455 				errx(EXIT_FAILURE, "%s: not a valid attribute",
    456 				    val);
    457 			*((unsigned int *) f->valp) |= i;
    458 		}
    459 		break;
    460 	default:
    461 		errx(EXIT_FAILURE, "internal error: rd_field: no format %d",
    462 		    f->format);
    463 		break;
    464 	}
    465 }
    466 
    467 static unsigned int
    468 rd_bitfield(const char *str)
    469 {
    470 	const char *ptr;
    471 	char *ep;
    472 	long lval;
    473 	unsigned int result;
    474 
    475 	ep = NULL;
    476 	ptr = str;
    477 	result = 0;
    478 	while (*ptr != '\0') {
    479 		errno = 0;
    480 		lval = strtol(ptr, &ep, 10);
    481 		if (*ep != '\0' && *ep != ' ')
    482 			errx(EXIT_FAILURE, "%s: not a valid number list", str);
    483 		if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
    484 			errx(EXIT_FAILURE, "%s: not a valid number list", str);
    485 		if (lval >= (long)sizeof(result) * 8)
    486 			errx(EXIT_FAILURE, "%ld: number out of range", lval);
    487 		result |= (1 << lval);
    488 
    489 		ptr = ep;
    490 		while (*ptr == ' ')
    491 			ptr++;
    492 	}
    493 
    494 	return result;
    495 }
    496 
    497 static void
    498 print_kmap(struct wskbd_map_data *map)
    499 {
    500 	unsigned int i;
    501 	struct wscons_keymap *mp;
    502 
    503 	for (i = 0; i < map->maplen; i++) {
    504 		mp = map->map + i;
    505 
    506 		if (mp->command == KS_voidSymbol &&
    507 		    mp->group1[0] == KS_voidSymbol &&
    508 		    mp->group1[1] == KS_voidSymbol &&
    509 		    mp->group2[0] == KS_voidSymbol &&
    510 		    mp->group2[1] == KS_voidSymbol)
    511 			continue;
    512 		(void)printf("\n");
    513 		(void)printf("keycode %u =", i);
    514 		if (mp->command != KS_voidSymbol)
    515 			(void)printf(" %s", ksym2name(mp->command));
    516 		(void)printf(" %s", ksym2name(mp->group1[0]));
    517 		if (mp->group1[0] != mp->group1[1] ||
    518 		    mp->group1[0] != mp->group2[0] ||
    519 		    mp->group1[0] != mp->group2[1]) {
    520 			(void)printf(" %s", ksym2name(mp->group1[1]));
    521 			if (mp->group1[0] != mp->group2[0] ||
    522 			    mp->group1[1] != mp->group2[1]) {
    523 				(void)printf(" %s", ksym2name(mp->group2[0]));
    524 				(void)printf(" %s", ksym2name(mp->group2[1]));
    525 			}
    526 		}
    527 	}
    528 }
    529 
    530