xref: /openbsd-src/sbin/wsconsctl/util.c (revision 5e9c6f1cc10c48b551b235eb717abeaf38cd2ce8)
1 /*	$OpenBSD: util.c,v 1.57 2012/04/17 14:53:47 deraadt Exp $ */
2 /*	$NetBSD: util.c,v 1.8 2000/03/14 08:11:53 sato Exp $ */
3 
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Juergen Hannken-Illjes.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/ioctl.h>
34 #include <sys/time.h>
35 #include <dev/wscons/wsconsio.h>
36 #include <dev/wscons/wsksymdef.h>
37 #include <err.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include "wsconsctl.h"
43 
44 #define TABLEN(t)		(sizeof(t)/sizeof(t[0]))
45 
46 extern struct wskbd_map_data kbmap;	/* from keyboard.c */
47 extern struct wskbd_map_data newkbmap;	/* from map_parse.y */
48 extern struct wsmouse_calibcoords wmcoords; 	/* from mouse.c */
49 
50 struct nameint {
51 	int value;
52 	char *name;
53 };
54 
55 static const struct nameint kbtype_tab[] = {
56 	{ WSKBD_TYPE_LK201,	"lk201" },
57 	{ WSKBD_TYPE_LK401,	"lk401" },
58 	{ WSKBD_TYPE_PC_XT,	"pc-xt" },
59 	{ WSKBD_TYPE_PC_AT,	"pc-at" },
60 	{ WSKBD_TYPE_USB,	"usb" },
61 	{ WSKBD_TYPE_NEXT,	"NeXT" },
62 	{ WSKBD_TYPE_HPC_KBD,	"hpc-kbd" },
63 	{ WSKBD_TYPE_HPC_BTN,	"hpc-btn" },
64 	{ WSKBD_TYPE_ADB,	"adb" },
65 	{ WSKBD_TYPE_SUN,	"sun" },
66 	{ WSKBD_TYPE_SUN5,	"sun5" },
67 	{ WSKBD_TYPE_HIL,	"hil" },
68 	{ WSKBD_TYPE_GSC,	"hp-ps2" },
69 	{ WSKBD_TYPE_LUNA,	"luna" },
70 	{ WSKBD_TYPE_ZAURUS,	"zaurus" },
71 	{ WSKBD_TYPE_DOMAIN,	"domain" },
72 	{ WSKBD_TYPE_BLUETOOTH,	"bluetooth" },
73 	{ WSKBD_TYPE_KPC,	"kpc" },
74 	{ WSKBD_TYPE_SGI,	"sgi" },
75 };
76 
77 static const struct nameint mstype_tab[] = {
78 	{ WSMOUSE_TYPE_VSXXX,	"dec-tc" },
79 	{ WSMOUSE_TYPE_PS2,	"ps2" },
80 	{ WSMOUSE_TYPE_USB,	"usb" },
81 	{ WSMOUSE_TYPE_LMS,	"lms" },
82 	{ WSMOUSE_TYPE_MMS,	"mms" },
83 	{ WSMOUSE_TYPE_TPANEL,	"touch-panel" },
84 	{ WSMOUSE_TYPE_NEXT,	"NeXT" },
85 	{ WSMOUSE_TYPE_ARCHIMEDES, "archimedes" },
86 	{ WSMOUSE_TYPE_ADB,	"adb" },
87 	{ WSMOUSE_TYPE_HIL,	"hil" },
88 	{ WSMOUSE_TYPE_LUNA,	"luna" },
89 	{ WSMOUSE_TYPE_DOMAIN,	"domain" },
90 	{ WSMOUSE_TYPE_BLUETOOTH, "bluetooth" },
91 	{ WSMOUSE_TYPE_SUN,	"sun" },
92 	{ WSMOUSE_TYPE_SYNAPTICS, "synaptics" },
93 	{ WSMOUSE_TYPE_ALPS,	"alps" },
94 	{ WSMOUSE_TYPE_SGI,	"sgi" }
95 };
96 
97 static const struct nameint dpytype_tab[] = {
98 	{ WSDISPLAY_TYPE_UNKNOWN,	"unknown" },
99 	{ WSDISPLAY_TYPE_PM_MONO,	"dec-pm-mono" },
100 	{ WSDISPLAY_TYPE_PM_COLOR,	"dec-pm-color" },
101 	{ WSDISPLAY_TYPE_CFB,		"dec-cfb" },
102 	{ WSDISPLAY_TYPE_XCFB,		"dec-xcfb" },
103 	{ WSDISPLAY_TYPE_MFB,		"dec-mfb" },
104 	{ WSDISPLAY_TYPE_SFB,		"dec-sfb" },
105 	{ WSDISPLAY_TYPE_ISAVGA,	"vga-isa" },
106 	{ WSDISPLAY_TYPE_PCIVGA,	"vga-pci" },
107 	{ WSDISPLAY_TYPE_TGA,		"dec-tga-pci" },
108 	{ WSDISPLAY_TYPE_SFBP,		"dec-sfb+" },
109 	{ WSDISPLAY_TYPE_PCIMISC,	"generic-pci" },
110 	{ WSDISPLAY_TYPE_NEXTMONO,	"next-mono" },
111 	{ WSDISPLAY_TYPE_PX,		"dec-px" },
112 	{ WSDISPLAY_TYPE_PXG,		"dec-pxg" },
113 	{ WSDISPLAY_TYPE_TX,		"dec-tx" },
114 	{ WSDISPLAY_TYPE_HPCFB,		"generic-hpc" },
115 	{ WSDISPLAY_TYPE_VIDC,		"arm-vidc" },
116 	{ WSDISPLAY_TYPE_SPX,		"dec-spx" },
117 	{ WSDISPLAY_TYPE_GPX,		"dec-gpx" },
118 	{ WSDISPLAY_TYPE_LCG,		"dec-lcg" },
119 	{ WSDISPLAY_TYPE_VAX_MONO,	"dec-mono" },
120 	{ WSDISPLAY_TYPE_SB_P9100,	"p9100" },
121 	{ WSDISPLAY_TYPE_EGA,		"ega" },
122 	{ WSDISPLAY_TYPE_DCPVR,		"powervr" },
123 	{ WSDISPLAY_TYPE_SUN24,		"sun24" },
124 	{ WSDISPLAY_TYPE_SUNBW,		"sunbw" },
125 	{ WSDISPLAY_TYPE_STI,		"hp-sti" },
126 	{ WSDISPLAY_TYPE_SUNCG3,	"suncg3" },
127 	{ WSDISPLAY_TYPE_SUNCG6,	"suncg6" },
128 	{ WSDISPLAY_TYPE_SUNFFB,	"sunffb" },
129 	{ WSDISPLAY_TYPE_SUNCG14,	"suncg14" },
130 	{ WSDISPLAY_TYPE_SUNCG2,	"suncg2" },
131 	{ WSDISPLAY_TYPE_SUNCG4,	"suncg4" },
132 	{ WSDISPLAY_TYPE_SUNCG8,	"suncg8" },
133 	{ WSDISPLAY_TYPE_SUNTCX,	"suntcx" },
134 	{ WSDISPLAY_TYPE_AGTEN,		"agten" },
135 	{ WSDISPLAY_TYPE_XVIDEO,	"xvideo" },
136 	{ WSDISPLAY_TYPE_SUNCG12,	"suncg12" },
137 	{ WSDISPLAY_TYPE_MGX,		"mgx" },
138 	{ WSDISPLAY_TYPE_SB_P9000,	"p9000" },
139 	{ WSDISPLAY_TYPE_RFLEX,		"rasterflex" },
140 	{ WSDISPLAY_TYPE_LUNA,		"luna" },
141 	{ WSDISPLAY_TYPE_DVBOX,		"davinci" },
142 	{ WSDISPLAY_TYPE_GBOX,		"gatorbox" },
143 	{ WSDISPLAY_TYPE_RBOX,		"renaissance" },
144 	{ WSDISPLAY_TYPE_HYPERION,	"hyperion" },
145 	{ WSDISPLAY_TYPE_TOPCAT,	"topcat" },
146 	{ WSDISPLAY_TYPE_PXALCD,	"pxalcd" },
147 	{ WSDISPLAY_TYPE_MAC68K,	"mac68k" },
148 	{ WSDISPLAY_TYPE_SUNLEO,	"sunleo" },
149 	{ WSDISPLAY_TYPE_TVRX,		"tvrx" },
150 	{ WSDISPLAY_TYPE_CFXGA,		"cfxga" },
151 	{ WSDISPLAY_TYPE_LCSPX,		"dec-lcspx" },
152 	{ WSDISPLAY_TYPE_GBE,		"gbe" },
153 	{ WSDISPLAY_TYPE_LEGSS,		"dec-legss" },
154 	{ WSDISPLAY_TYPE_IFB,		"ifb" },
155 	{ WSDISPLAY_TYPE_RAPTOR,	"raptor" },
156 	{ WSDISPLAY_TYPE_DL,		"displaylink" },
157 	{ WSDISPLAY_TYPE_MACHFB,	"mach64" },
158 	{ WSDISPLAY_TYPE_GFXP,		"gfxp" },
159 	{ WSDISPLAY_TYPE_RADEONFB,	"radeon" },
160 	{ WSDISPLAY_TYPE_SMFB,		"smfb" },
161 	{ WSDISPLAY_TYPE_SISFB,		"sisfb" },
162 	{ WSDISPLAY_TYPE_ODYSSEY,	"odyssey" },
163 	{ WSDISPLAY_TYPE_IMPACT,	"impact" },
164 	{ WSDISPLAY_TYPE_GRTWO,		"grtwo" },
165 	{ WSDISPLAY_TYPE_NEWPORT,	"newport" },
166 	{ WSDISPLAY_TYPE_LIGHT,		"light" }
167 };
168 
169 static const struct nameint kbdenc_tab[] = {
170 	KB_ENCTAB
171 };
172 
173 static const struct nameint kbdvar_tab[] = {
174 	KB_VARTAB
175 };
176 
177 char *int2name(int, int, const struct nameint *, int);
178 int name2int(char *, const struct nameint *, int);
179 void print_kmap(struct wskbd_map_data *);
180 void print_emul(struct wsdisplay_emultype *);
181 void print_screen(struct wsdisplay_screentype *);
182 
183 struct field *
184 field_by_name(struct field *field_tab, char *name)
185 {
186 	const char *p = strchr(name, '.');
187 
188 	if (!p++)
189 		errx(1, "%s: illegal variable name", name);
190 
191 	for (; field_tab->name; field_tab++)
192 		if (strcmp(field_tab->name, p) == 0)
193 			return (field_tab);
194 
195 	errx(1, "%s: not found", name);
196 }
197 
198 struct field *
199 field_by_value(struct field *field_tab, void *addr)
200 {
201 	for (; field_tab->name; field_tab++)
202 		if (field_tab->valp == addr)
203 			return (field_tab);
204 
205 	errx(1, "internal error: field_by_value: not found");
206 }
207 
208 char *
209 int2name(int val, int uflag, const struct nameint *tab, int len)
210 {
211 	static char tmp[20];
212 	int i;
213 
214 	for (i = 0; i < len; i++)
215 		if (tab[i].value == val)
216 			return(tab[i].name);
217 
218 	if (uflag) {
219 		snprintf(tmp, sizeof(tmp), "unknown_%d", val);
220 		return(tmp);
221 	} else
222 		return(NULL);
223 }
224 
225 int
226 name2int(char *val, const struct nameint *tab, int len)
227 {
228 	int i;
229 
230 	for (i = 0; i < len; i++)
231 		if (strcmp(tab[i].name, val) == 0)
232 			return(tab[i].value);
233 	return(-1);
234 }
235 
236 void
237 pr_field(const char *pre, struct field *f, const char *sep)
238 {
239 	struct field_pc *pc;
240 	u_int flags;
241 	int i, n;
242 	char *p;
243 
244 	if (sep)
245 		printf("%s.%s%s", pre, f->name, sep);
246 
247 	switch (f->format) {
248 	case FMT_UINT:
249 		printf("%u", *((u_int *) f->valp));
250 		break;
251 	case FMT_INT:
252 		printf("%d", *((int *) f->valp));
253 		break;
254 	case FMT_BOOL:
255 		printf("%s", *((u_int *) f->valp)? "on" : "off");
256 		break;
257 	case FMT_PC:
258 		pc = f->valp;
259 		i = pc->max - pc->min;
260 		n = pc->cur - pc->min;
261 		printf("%u.%02u%%", n * 100 / i, ((n * 100) % i) * 100 / i);
262 		break;
263 	case FMT_KBDTYPE:
264 		p = int2name(*((u_int *) f->valp), 1,
265 			     kbtype_tab, TABLEN(kbtype_tab));
266 		printf("%s", p);
267 		break;
268 	case FMT_MSTYPE:
269 		p = int2name(*((u_int *) f->valp), 1,
270 			     mstype_tab, TABLEN(mstype_tab));
271 		printf("%s", p);
272 		break;
273 	case FMT_DPYTYPE:
274 		p = int2name(*((u_int *) f->valp), 1,
275 			     dpytype_tab, TABLEN(dpytype_tab));
276 		printf("%s", p);
277 		break;
278 	case FMT_KBDENC:
279 		p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1,
280 			     kbdenc_tab, TABLEN(kbdenc_tab));
281 		printf("%s", p);
282 
283 		flags = KB_VARIANT(*((u_int *) f->valp));
284 		for (i = 0; i < 32; i++) {
285 			if (!(flags & (1 << i)))
286 				continue;
287 			p = int2name(flags & (1 << i), 1,
288 				     kbdvar_tab, TABLEN(kbdvar_tab));
289 			printf(".%s", p);
290 		}
291 		break;
292 	case FMT_KBMAP:
293 		print_kmap((struct wskbd_map_data *) f->valp);
294 		break;
295 	case FMT_SCALE:
296 		printf("%d,%d,%d,%d,%d,%d,%d", wmcoords.minx, wmcoords.maxx,
297 		    wmcoords.miny, wmcoords.maxy, wmcoords.swapxy,
298 		    wmcoords.resx, wmcoords.resy);
299 		break;
300 	case FMT_EMUL:
301 		print_emul((struct wsdisplay_emultype *) f->valp);
302 		break;
303 	case FMT_SCREEN:
304 		print_screen((struct wsdisplay_screentype *) f->valp);
305 		break;
306 	default:
307 		errx(1, "internal error: pr_field: no format %d", f->format);
308 		break;
309 	}
310 
311 	printf("\n");
312 }
313 
314 void
315 rd_field(struct field *f, char *val, int merge)
316 {
317 	struct wscons_keymap *mp;
318 	struct field_pc *pc;
319 	u_int u, r, fr;
320 	char *p;
321 	int i;
322 
323 	switch (f->format) {
324 	case FMT_UINT:
325 		if (sscanf(val, "%u", &u) != 1)
326 			errx(1, "%s: not a number", val);
327 		if (merge)
328 			*((u_int *) f->valp) += u;
329 		else
330 			*((u_int *) f->valp) = u;
331 		break;
332 	case FMT_INT:
333 		if (sscanf(val, "%d", &i) != 1)
334 			errx(1, "%s: not a number", val);
335 		if (merge)
336 			*((int *) f->valp) += i;
337 		else
338 			*((int *) f->valp) = i;
339 		break;
340 	case FMT_BOOL:
341 		if (*val != 'o' || (val[1] != 'n' &&
342 		    (val[1] != 'f' || val[2] != 'f')))
343 			errx(1, "%s: invalid value (on/off)", val);
344 		*((u_int *) f->valp) = val[1] == 'n'? 1 : 0;
345 		break;
346 	case FMT_PC:
347 		fr = 0;
348 		if ((i = sscanf(val, "%u.%u%%", &u, &fr)) != 2 && i != 1)
349 			errx(1, "%s: not a valid number", val);
350 		pc = f->valp;
351 		r = pc->max - pc->min;
352 		i = pc->min + (r * u) / 100 + (r * fr) / 100 / 100;
353 		if (merge == '+')
354 			pc->cur += i;
355 		else if (merge == '-')
356 			pc->cur -= i;
357 		else
358 			pc->cur = i;
359 		if (pc->cur > pc->max)
360 			pc->cur = pc->max;
361 		if (pc->cur < pc->min)
362 			pc->cur = pc->min;
363 		break;
364 	case FMT_KBDENC:
365 		p = strchr(val, '.');
366 		if (p != NULL)
367 			*p++ = '\0';
368 
369 		i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab));
370 		if (i == -1)
371 			errx(1, "%s: not a valid encoding", val);
372 		*((u_int *) f->valp) = i;
373 
374 		while (p) {
375 			val = p;
376 			p = strchr(p, '.');
377 			if (p != NULL)
378 				*p++ = '\0';
379 			i = name2int(val, kbdvar_tab, TABLEN(kbdvar_tab));
380 			if (i == -1)
381 				errx(1, "%s: not a valid variant", val);
382 			*((u_int *) f->valp) |= i;
383 		}
384 		break;
385 	case FMT_KBMAP:
386 		if (! merge)
387 			kbmap.maplen = 0;
388 		map_scan_setinput(val);
389 		yyparse();
390 		if (merge) {
391 			if (newkbmap.maplen < kbmap.maplen)
392 				newkbmap.maplen = kbmap.maplen;
393 			for (i = 0; i < kbmap.maplen; i++) {
394 				mp = newkbmap.map + i;
395 				if (mp->command == KS_voidSymbol &&
396 				    mp->group1[0] == KS_voidSymbol &&
397 				    mp->group1[1] == KS_voidSymbol &&
398 				    mp->group2[0] == KS_voidSymbol &&
399 				    mp->group2[1] == KS_voidSymbol)
400 					*mp = kbmap.map[i];
401 			}
402 		}
403 		kbmap.maplen = newkbmap.maplen;
404 		bcopy(newkbmap.map, kbmap.map,
405 		      kbmap.maplen*sizeof(struct wscons_keymap));
406 		break;
407 	case FMT_SCALE:
408 	{
409 		const char *errstr = 0;
410 
411 		/* Unspecified values default to 0. */
412 		bzero(&wmcoords, sizeof(wmcoords));
413 		val = (void *)strtok(val, ",");
414 		if (val != NULL) {
415 			wmcoords.minx = (int)strtonum(val,
416 			    0, 32768, &errstr);
417 			val = (void *)strtok(NULL, ",");
418 		}
419 		if (!errstr && val != NULL) {
420 			wmcoords.maxx = (int)strtonum(val,
421 			    0, 32768, &errstr);
422 			val = (void *)strtok(NULL, ",");
423 		}
424 		if (!errstr && val != NULL) {
425 			wmcoords.miny = (int)strtonum(val,
426 			    0, 32768, &errstr);
427 			val = (void *)strtok(NULL, ",");
428 		}
429 		if (!errstr && val != NULL) {
430 			wmcoords.maxy = (int)strtonum(val,
431 			    0, 32768, &errstr);
432 			val = (void *)strtok(NULL, ",");
433 		}
434 		if (!errstr && val != NULL) {
435 			wmcoords.swapxy = (int)strtonum(val,
436 			    0, 32768, &errstr);
437 			val = (void *)strtok(NULL, ",");
438 		}
439 		if (!errstr && val != NULL) {
440 			wmcoords.resx = (int)strtonum(val,
441 			    0, 32768, &errstr);
442 			val = (void *)strtok(NULL, ",");
443 		}
444 		if (!errstr && val != NULL) {
445 			wmcoords.resy = (int)strtonum(val,
446 			    0, 32768, &errstr);
447 			val = (void *)strtok(NULL, ",");
448 		}
449 		if (errstr)
450 			errx(1, "calibration value is %s", errstr);
451 		if (val != NULL)
452 			errx(1, "too many calibration values");
453 
454 		break;
455 	}
456 	default:
457 		errx(1, "internal error: rd_field: no format %d", f->format);
458 		break;
459 	}
460 }
461 
462 void
463 print_kmap(struct wskbd_map_data *map)
464 {
465 	struct wscons_keymap *mp;
466 	int i;
467 
468 	for (i = 0; i < map->maplen; i++) {
469 		mp = map->map + i;
470 
471 		if (mp->command == KS_voidSymbol &&
472 		    mp->group1[0] == KS_voidSymbol &&
473 		    mp->group1[1] == KS_voidSymbol &&
474 		    mp->group2[0] == KS_voidSymbol &&
475 		    mp->group2[1] == KS_voidSymbol)
476 			continue;
477 		printf("\n");
478 		printf("keycode %u =", i);
479 		if (mp->command != KS_voidSymbol)
480 			printf(" %s", ksym2name(mp->command));
481 		printf(" %s", ksym2name(mp->group1[0]));
482 		if (mp->group1[0] != mp->group1[1] ||
483 		    mp->group1[0] != mp->group2[0] ||
484 		    mp->group1[0] != mp->group2[1]) {
485 			printf(" %s", ksym2name(mp->group1[1]));
486 			if (mp->group1[0] != mp->group2[0] ||
487 			    mp->group1[1] != mp->group2[1]) {
488 				printf(" %s", ksym2name(mp->group2[0]));
489 				printf(" %s", ksym2name(mp->group2[1]));
490 			}
491 		}
492 	}
493 	ksymenc(0);
494 }
495 
496 void
497 print_emul(struct wsdisplay_emultype *emuls)
498 {
499 	struct wsdisplay_emultype e;
500 	int fd,i;
501 	char c='\0';
502 
503 	fd=emuls->idx;
504 	e.idx=0;
505 	i = ioctl(fd, WSDISPLAYIO_GETEMULTYPE, &e);
506 	while(i == 0) {
507 		if (c != '\0')
508 			printf("%c", c);
509 		printf("%s", e.name);
510 		c=',';
511 		e.idx++;
512 		i = ioctl(fd, WSDISPLAYIO_GETEMULTYPE, &e);
513 	}
514 }
515 
516 void
517 print_screen(struct wsdisplay_screentype *screens)
518 {
519 	struct wsdisplay_screentype s;
520 	int fd,i;
521 	char c='\0';
522 
523 	fd=screens->idx;
524 	s.idx=0;
525 	i = ioctl(fd, WSDISPLAYIO_GETSCREENTYPE, &s);
526 	while(i == 0) {
527 		if (c != '\0')
528 			printf("%c", c);
529 		printf("%s", s.name);
530 		c=',';
531 		s.idx++;
532 		i = ioctl(fd, WSDISPLAYIO_GETSCREENTYPE, &s);
533 	}
534 }
535