xref: /openbsd-src/sbin/wsconsctl/util.c (revision 9aaad64f1a41a015999e02ec8c94d0b0fb8ddbcf)
1 /*	$OpenBSD: util.c,v 1.52 2010/07/01 03:00:14 maja 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/time.h>
34 #include <dev/wscons/wsconsio.h>
35 #include <dev/wscons/wsksymdef.h>
36 #include <err.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <unistd.h>
41 #include "wsconsctl.h"
42 
43 #define TABLEN(t)		(sizeof(t)/sizeof(t[0]))
44 
45 extern struct wskbd_map_data kbmap;	/* from keyboard.c */
46 extern struct wskbd_map_data newkbmap;	/* from map_parse.y */
47 extern struct wsmouse_calibcoords wmcoords; 	/* from mouse.c */
48 
49 struct nameint {
50 	int value;
51 	char *name;
52 };
53 
54 static const struct nameint kbtype_tab[] = {
55 	{ WSKBD_TYPE_LK201,	"lk201" },
56 	{ WSKBD_TYPE_LK401,	"lk401" },
57 	{ WSKBD_TYPE_PC_XT,	"pc-xt" },
58 	{ WSKBD_TYPE_PC_AT,	"pc-at" },
59 	{ WSKBD_TYPE_USB,	"usb" },
60 	{ WSKBD_TYPE_NEXT,	"NeXT" },
61 	{ WSKBD_TYPE_HPC_KBD,	"hpc-kbd" },
62 	{ WSKBD_TYPE_HPC_BTN,	"hpc-btn" },
63 	{ WSKBD_TYPE_ADB,	"adb" },
64 	{ WSKBD_TYPE_SUN,	"sun" },
65 	{ WSKBD_TYPE_SUN5,	"sun5" },
66 	{ WSKBD_TYPE_HIL,	"hil" },
67 	{ WSKBD_TYPE_GSC,	"hp-ps2" },
68 	{ WSKBD_TYPE_LUNA,	"luna" },
69 	{ WSKBD_TYPE_ZAURUS,	"zaurus" },
70 	{ WSKBD_TYPE_DOMAIN,	"domain" },
71 	{ WSKBD_TYPE_BLUETOOTH,	"bluetooth" },
72 	{ WSKBD_TYPE_KPC,	"kpc" },
73 };
74 
75 static const struct nameint mstype_tab[] = {
76 	{ WSMOUSE_TYPE_VSXXX,	"dec-tc" },
77 	{ WSMOUSE_TYPE_PS2,	"ps2" },
78 	{ WSMOUSE_TYPE_USB,	"usb" },
79 	{ WSMOUSE_TYPE_LMS,	"lms" },
80 	{ WSMOUSE_TYPE_MMS,	"mms" },
81 	{ WSMOUSE_TYPE_TPANEL,	"touch-panel" },
82 	{ WSMOUSE_TYPE_NEXT,	"NeXT" },
83 	{ WSMOUSE_TYPE_ARCHIMEDES, "archimedes" },
84 	{ WSMOUSE_TYPE_ADB,	"adb" },
85 	{ WSMOUSE_TYPE_HIL,	"hil" },
86 	{ WSMOUSE_TYPE_LUNA,	"luna" },
87 	{ WSMOUSE_TYPE_DOMAIN,	"domain" },
88 	{ WSMOUSE_TYPE_BLUETOOTH, "bluetooth" },
89 	{ WSMOUSE_TYPE_SUN,	"sun" }
90 };
91 
92 static const 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-mono" },
115 	{ WSDISPLAY_TYPE_SB_P9100,	"p9100" },
116 	{ WSDISPLAY_TYPE_EGA,		"ega" },
117 	{ WSDISPLAY_TYPE_DCPVR,		"powervr" },
118 	{ WSDISPLAY_TYPE_SUN24,		"sun24" },
119 	{ WSDISPLAY_TYPE_SUNBW,		"sunbw" },
120 	{ WSDISPLAY_TYPE_STI,		"hp-sti" },
121 	{ WSDISPLAY_TYPE_SUNCG3,	"suncg3" },
122 	{ WSDISPLAY_TYPE_SUNCG6,	"suncg6" },
123 	{ WSDISPLAY_TYPE_SUNFFB,	"sunffb" },
124 	{ WSDISPLAY_TYPE_SUNCG14,	"suncg14" },
125 	{ WSDISPLAY_TYPE_SUNCG2,	"suncg2" },
126 	{ WSDISPLAY_TYPE_SUNCG4,	"suncg4" },
127 	{ WSDISPLAY_TYPE_SUNCG8,	"suncg8" },
128 	{ WSDISPLAY_TYPE_SUNTCX,	"suntcx" },
129 	{ WSDISPLAY_TYPE_AGTEN,		"agten" },
130 	{ WSDISPLAY_TYPE_XVIDEO,	"xvideo" },
131 	{ WSDISPLAY_TYPE_SUNCG12,	"suncg12" },
132 	{ WSDISPLAY_TYPE_MGX,		"mgx" },
133 	{ WSDISPLAY_TYPE_SB_P9000,	"p9000" },
134 	{ WSDISPLAY_TYPE_RFLEX,		"rasterflex" },
135 	{ WSDISPLAY_TYPE_LUNA,		"luna" },
136 	{ WSDISPLAY_TYPE_DVBOX,		"davinci" },
137 	{ WSDISPLAY_TYPE_GBOX,		"gatorbox" },
138 	{ WSDISPLAY_TYPE_RBOX,		"renaissance" },
139 	{ WSDISPLAY_TYPE_HYPERION,	"hyperion" },
140 	{ WSDISPLAY_TYPE_TOPCAT,	"topcat" },
141 	{ WSDISPLAY_TYPE_PXALCD,	"pxalcd" },
142 	{ WSDISPLAY_TYPE_MAC68K,	"mac68k" },
143 	{ WSDISPLAY_TYPE_SUNLEO,	"sunleo" },
144 	{ WSDISPLAY_TYPE_TVRX,		"tvrx" },
145 	{ WSDISPLAY_TYPE_CFXGA,		"cfxga" },
146 	{ WSDISPLAY_TYPE_LCSPX,		"dec-lcspx" },
147 	{ WSDISPLAY_TYPE_GBE,		"gbe" },
148 	{ WSDISPLAY_TYPE_LEGSS,		"dec-legss" },
149 	{ WSDISPLAY_TYPE_IFB,		"ifb" },
150 	{ WSDISPLAY_TYPE_RAPTOR,	"raptor" },
151 	{ WSDISPLAY_TYPE_DL,		"displaylink" },
152 	{ WSDISPLAY_TYPE_MACHFB,	"mach64" },
153 	{ WSDISPLAY_TYPE_GFXP,		"gfxp" },
154 	{ WSDISPLAY_TYPE_RADEONFB,	"radeon" },
155 	{ WSDISPLAY_TYPE_SMFB,		"smfb" },
156 	{ WSDISPLAY_TYPE_SISFB,		"sisfb" },
157 	{ WSDISPLAY_TYPE_ODYSSEY,	"odyssey" },
158 	{ WSDISPLAY_TYPE_IMPACT,	"impact" }
159 };
160 
161 static const struct nameint kbdenc_tab[] = {
162 	KB_ENCTAB
163 };
164 
165 static const struct nameint kbdvar_tab[] = {
166 	KB_VARTAB
167 };
168 
169 char *int2name(int, int, const struct nameint *, int);
170 int name2int(char *, const struct nameint *, int);
171 void print_kmap(struct wskbd_map_data *);
172 void print_emul(struct wsdisplay_emultype *);
173 void print_screen(struct wsdisplay_screentype *);
174 
175 struct field *
176 field_by_name(struct field *field_tab, char *name)
177 {
178 	const char *p = strchr(name, '.');
179 
180 	if (!p++)
181 		errx(1, "%s: illegal variable name", name);
182 
183 	for (; field_tab->name; field_tab++)
184 		if (strcmp(field_tab->name, p) == 0)
185 			return (field_tab);
186 
187 	errx(1, "%s: not found", name);
188 }
189 
190 struct field *
191 field_by_value(struct field *field_tab, void *addr)
192 {
193 	for (; field_tab->name; field_tab++)
194 		if (field_tab->valp == addr)
195 			return (field_tab);
196 
197 	errx(1, "internal error: field_by_value: not found");
198 }
199 
200 char *
201 int2name(int val, int uflag, const struct nameint *tab, int len)
202 {
203 	static char tmp[20];
204 	int i;
205 
206 	for (i = 0; i < len; i++)
207 		if (tab[i].value == val)
208 			return(tab[i].name);
209 
210 	if (uflag) {
211 		snprintf(tmp, sizeof(tmp), "unknown_%d", val);
212 		return(tmp);
213 	} else
214 		return(NULL);
215 }
216 
217 int
218 name2int(char *val, const struct nameint *tab, int len)
219 {
220 	int i;
221 
222 	for (i = 0; i < len; i++)
223 		if (strcmp(tab[i].name, val) == 0)
224 			return(tab[i].value);
225 	return(-1);
226 }
227 
228 void
229 pr_field(const char *pre, struct field *f, const char *sep)
230 {
231 	struct field_pc *pc;
232 	u_int flags;
233 	int i, n;
234 	char *p;
235 
236 	if (sep)
237 		printf("%s.%s%s", pre, f->name, sep);
238 
239 	switch (f->format) {
240 	case FMT_UINT:
241 		printf("%u", *((u_int *) f->valp));
242 		break;
243 	case FMT_INT:
244 		printf("%d", *((int *) f->valp));
245 		break;
246 	case FMT_BOOL:
247 		printf("%s", *((u_int *) f->valp)? "on" : "off");
248 		break;
249 	case FMT_PC:
250 		pc = f->valp;
251 		i = pc->max - pc->min;
252 		n = pc->cur - pc->min;
253 		printf("%u.%02u%%", n * 100 / i, ((n * 100) % i) * 100 / i);
254 		break;
255 	case FMT_KBDTYPE:
256 		p = int2name(*((u_int *) f->valp), 1,
257 			     kbtype_tab, TABLEN(kbtype_tab));
258 		printf("%s", p);
259 		break;
260 	case FMT_MSTYPE:
261 		p = int2name(*((u_int *) f->valp), 1,
262 			     mstype_tab, TABLEN(mstype_tab));
263 		printf("%s", p);
264 		break;
265 	case FMT_DPYTYPE:
266 		p = int2name(*((u_int *) f->valp), 1,
267 			     dpytype_tab, TABLEN(dpytype_tab));
268 		printf("%s", p);
269 		break;
270 	case FMT_KBDENC:
271 		p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1,
272 			     kbdenc_tab, TABLEN(kbdenc_tab));
273 		printf("%s", p);
274 
275 		flags = KB_VARIANT(*((u_int *) f->valp));
276 		for (i = 0; i < 32; i++) {
277 			if (!(flags & (1 << i)))
278 				continue;
279 			p = int2name(flags & (1 << i), 1,
280 				     kbdvar_tab, TABLEN(kbdvar_tab));
281 			printf(".%s", p);
282 		}
283 		break;
284 	case FMT_KBMAP:
285 		print_kmap((struct wskbd_map_data *) f->valp);
286 		break;
287 	case FMT_SCALE:
288 		printf("%d,%d,%d,%d,%d,%d,%d", wmcoords.minx, wmcoords.maxx,
289 		    wmcoords.miny, wmcoords.maxy, wmcoords.swapxy,
290 		    wmcoords.resx, wmcoords.resy);
291 		break;
292 	case FMT_EMUL:
293 		print_emul((struct wsdisplay_emultype *) f->valp);
294 		break;
295 	case FMT_SCREEN:
296 		print_screen((struct wsdisplay_screentype *) f->valp);
297 		break;
298 	default:
299 		errx(1, "internal error: pr_field: no format %d", f->format);
300 		break;
301 	}
302 
303 	printf("\n");
304 }
305 
306 void
307 rd_field(struct field *f, char *val, int merge)
308 {
309 	struct wscons_keymap *mp;
310 	struct field_pc *pc;
311 	u_int u, r, fr;
312 	char *p;
313 	int i;
314 
315 	switch (f->format) {
316 	case FMT_UINT:
317 		if (sscanf(val, "%u", &u) != 1)
318 			errx(1, "%s: not a number", val);
319 		if (merge)
320 			*((u_int *) f->valp) += u;
321 		else
322 			*((u_int *) f->valp) = u;
323 		break;
324 	case FMT_INT:
325 		if (sscanf(val, "%d", &i) != 1)
326 			errx(1, "%s: not a number", val);
327 		if (merge)
328 			*((int *) f->valp) += i;
329 		else
330 			*((int *) f->valp) = i;
331 		break;
332 	case FMT_BOOL:
333 		if (*val != 'o' || (val[1] != 'n' &&
334 		    (val[1] != 'f' || val[2] != 'f')))
335 			errx(1, "%s: invalid value (on/off)", val);
336 		*((u_int *) f->valp) = val[1] == 'n'? 1 : 0;
337 		break;
338 	case FMT_PC:
339 		fr = 0;
340 		if ((i = sscanf(val, "%u.%u%%", &u, &fr)) != 2 && i != 1)
341 			errx(1, "%s: not a valid number", val);
342 		pc = f->valp;
343 		r = pc->max - pc->min;
344 		i = pc->min + (r * u) / 100 + (r * fr) / 100 / 100;
345 		if (merge == '+')
346 			pc->cur += i;
347 		else if (merge == '-')
348 			pc->cur -= i;
349 		else
350 			pc->cur = i;
351 		if (pc->cur > pc->max)
352 			pc->cur = pc->max;
353 		if (pc->cur < pc->min)
354 			pc->cur = pc->min;
355 		break;
356 	case FMT_KBDENC:
357 		p = strchr(val, '.');
358 		if (p != NULL)
359 			*p++ = '\0';
360 
361 		i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab));
362 		if (i == -1)
363 			errx(1, "%s: not a valid encoding", val);
364 		*((u_int *) f->valp) = i;
365 
366 		while (p) {
367 			val = p;
368 			p = strchr(p, '.');
369 			if (p != NULL)
370 				*p++ = '\0';
371 			i = name2int(val, kbdvar_tab, TABLEN(kbdvar_tab));
372 			if (i == -1)
373 				errx(1, "%s: not a valid variant", val);
374 			*((u_int *) f->valp) |= i;
375 		}
376 		break;
377 	case FMT_KBMAP:
378 		if (! merge)
379 			kbmap.maplen = 0;
380 		map_scan_setinput(val);
381 		yyparse();
382 		if (merge) {
383 			if (newkbmap.maplen < kbmap.maplen)
384 				newkbmap.maplen = kbmap.maplen;
385 			for (i = 0; i < kbmap.maplen; i++) {
386 				mp = newkbmap.map + i;
387 				if (mp->command == KS_voidSymbol &&
388 				    mp->group1[0] == KS_voidSymbol &&
389 				    mp->group1[1] == KS_voidSymbol &&
390 				    mp->group2[0] == KS_voidSymbol &&
391 				    mp->group2[1] == KS_voidSymbol)
392 					*mp = kbmap.map[i];
393 			}
394 		}
395 		kbmap.maplen = newkbmap.maplen;
396 		bcopy(newkbmap.map, kbmap.map,
397 		      kbmap.maplen*sizeof(struct wscons_keymap));
398 		break;
399 	case FMT_SCALE:
400 	{
401 		const char *errstr = 0;
402 
403 		/* Unspecified values default to 0. */
404 		bzero(&wmcoords, sizeof(wmcoords));
405 		val = (void *)strtok(val, ",");
406 		if (val != NULL) {
407 			wmcoords.minx = (int)strtonum(val,
408 			    0, 32768, &errstr);
409 			val = (void *)strtok(NULL, ",");
410 		}
411 		if (!errstr && val != NULL) {
412 			wmcoords.maxx = (int)strtonum(val,
413 			    0, 32768, &errstr);
414 			val = (void *)strtok(NULL, ",");
415 		}
416 		if (!errstr && val != NULL) {
417 			wmcoords.miny = (int)strtonum(val,
418 			    0, 32768, &errstr);
419 			val = (void *)strtok(NULL, ",");
420 		}
421 		if (!errstr && val != NULL) {
422 			wmcoords.maxy = (int)strtonum(val,
423 			    0, 32768, &errstr);
424 			val = (void *)strtok(NULL, ",");
425 		}
426 		if (!errstr && val != NULL) {
427 			wmcoords.swapxy = (int)strtonum(val,
428 			    0, 32768, &errstr);
429 			val = (void *)strtok(NULL, ",");
430 		}
431 		if (!errstr && val != NULL) {
432 			wmcoords.resx = (int)strtonum(val,
433 			    0, 32768, &errstr);
434 			val = (void *)strtok(NULL, ",");
435 		}
436 		if (!errstr && val != NULL) {
437 			wmcoords.resy = (int)strtonum(val,
438 			    0, 32768, &errstr);
439 			val = (void *)strtok(NULL, ",");
440 		}
441 		if (errstr)
442 			errx(1, "calibration value is %s", errstr);
443 		if (val != NULL)
444 			errx(1, "too many calibration values");
445 
446 		break;
447 	}
448 	default:
449 		errx(1, "internal error: rd_field: no format %d", f->format);
450 		break;
451 	}
452 }
453 
454 void
455 print_kmap(struct wskbd_map_data *map)
456 {
457 	struct wscons_keymap *mp;
458 	int i;
459 
460 	for (i = 0; i < map->maplen; i++) {
461 		mp = map->map + i;
462 
463 		if (mp->command == KS_voidSymbol &&
464 		    mp->group1[0] == KS_voidSymbol &&
465 		    mp->group1[1] == KS_voidSymbol &&
466 		    mp->group2[0] == KS_voidSymbol &&
467 		    mp->group2[1] == KS_voidSymbol)
468 			continue;
469 		printf("\n");
470 		printf("keycode %u =", i);
471 		if (mp->command != KS_voidSymbol)
472 			printf(" %s", ksym2name(mp->command));
473 		printf(" %s", ksym2name(mp->group1[0]));
474 		if (mp->group1[0] != mp->group1[1] ||
475 		    mp->group1[0] != mp->group2[0] ||
476 		    mp->group1[0] != mp->group2[1]) {
477 			printf(" %s", ksym2name(mp->group1[1]));
478 			if (mp->group1[0] != mp->group2[0] ||
479 			    mp->group1[1] != mp->group2[1]) {
480 				printf(" %s", ksym2name(mp->group2[0]));
481 				printf(" %s", ksym2name(mp->group2[1]));
482 			}
483 		}
484 	}
485 	ksymenc(0);
486 }
487 
488 void
489 print_emul(struct wsdisplay_emultype *emuls)
490 {
491 	struct wsdisplay_emultype e;
492 	int fd,i;
493 	char c='\0';
494 
495 	fd=emuls->idx;
496 	e.idx=0;
497 	i = ioctl(fd, WSDISPLAYIO_GETEMULTYPE, &e);
498 	while(i == 0) {
499 		if (c != '\0')
500 			printf("%c", c);
501 		printf("%s", e.name);
502 		c=',';
503 		e.idx++;
504 		i = ioctl(fd, WSDISPLAYIO_GETEMULTYPE, &e);
505 	}
506 }
507 
508 void
509 print_screen(struct wsdisplay_screentype *screens)
510 {
511 	struct wsdisplay_screentype s;
512 	int fd,i;
513 	char c='\0';
514 
515 	fd=screens->idx;
516 	s.idx=0;
517 	i = ioctl(fd, WSDISPLAYIO_GETSCREENTYPE, &s);
518 	while(i == 0) {
519 		if (c != '\0')
520 			printf("%c", c);
521 		printf("%s", s.name);
522 		c=',';
523 		s.idx++;
524 		i = ioctl(fd, WSDISPLAYIO_GETSCREENTYPE, &s);
525 	}
526 }
527