xref: /openbsd-src/sbin/wsconsctl/util.c (revision 94e02902daa744c619bd1212ad19d1d894d8e0f5)
1 /*	$OpenBSD: util.c,v 1.46 2009/12/25 22:02:46 miod 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 };
157 
158 static const struct nameint kbdenc_tab[] = {
159 	KB_ENCTAB
160 };
161 
162 static const struct nameint kbdvar_tab[] = {
163 	KB_VARTAB
164 };
165 
166 char *int2name(int, int, const struct nameint *, int);
167 int name2int(char *, const struct nameint *, int);
168 void print_kmap(struct wskbd_map_data *);
169 
170 struct field *
171 field_by_name(struct field *field_tab, char *name)
172 {
173 	const char *p = strchr(name, '.');
174 
175 	if (!p++)
176 		errx(1, "%s: illegal variable name", name);
177 
178 	for (; field_tab->name; field_tab++)
179 		if (strcmp(field_tab->name, p) == 0)
180 			return (field_tab);
181 
182 	errx(1, "%s: not found", name);
183 }
184 
185 struct field *
186 field_by_value(struct field *field_tab, void *addr)
187 {
188 	for (; field_tab->name; field_tab++)
189 		if (field_tab->valp == addr)
190 			return (field_tab);
191 
192 	errx(1, "internal error: field_by_value: not found");
193 }
194 
195 char *
196 int2name(int val, int uflag, const struct nameint *tab, int len)
197 {
198 	static char tmp[20];
199 	int i;
200 
201 	for (i = 0; i < len; i++)
202 		if (tab[i].value == val)
203 			return(tab[i].name);
204 
205 	if (uflag) {
206 		snprintf(tmp, sizeof(tmp), "unknown_%d", val);
207 		return(tmp);
208 	} else
209 		return(NULL);
210 }
211 
212 int
213 name2int(char *val, const struct nameint *tab, int len)
214 {
215 	int i;
216 
217 	for (i = 0; i < len; i++)
218 		if (strcmp(tab[i].name, val) == 0)
219 			return(tab[i].value);
220 	return(-1);
221 }
222 
223 void
224 pr_field(const char *pre, struct field *f, const char *sep)
225 {
226 	struct field_pc *pc;
227 	u_int flags;
228 	int i, n;
229 	char *p;
230 
231 	if (sep)
232 		printf("%s.%s%s", pre, f->name, sep);
233 
234 	switch (f->format) {
235 	case FMT_UINT:
236 		printf("%u", *((u_int *) f->valp));
237 		break;
238 	case FMT_INT:
239 		printf("%d", *((int *) f->valp));
240 		break;
241 	case FMT_BOOL:
242 		printf("%s", *((u_int *) f->valp)? "on" : "off");
243 		break;
244 	case FMT_PC:
245 		pc = f->valp;
246 		i = pc->max - pc->min;
247 		n = pc->cur - pc->min;
248 		printf("%u.%02u%%", n * 100 / i, ((n * 100) % i) * 100 / i);
249 		break;
250 	case FMT_KBDTYPE:
251 		p = int2name(*((u_int *) f->valp), 1,
252 			     kbtype_tab, TABLEN(kbtype_tab));
253 		printf("%s", p);
254 		break;
255 	case FMT_MSTYPE:
256 		p = int2name(*((u_int *) f->valp), 1,
257 			     mstype_tab, TABLEN(mstype_tab));
258 		printf("%s", p);
259 		break;
260 	case FMT_DPYTYPE:
261 		p = int2name(*((u_int *) f->valp), 1,
262 			     dpytype_tab, TABLEN(dpytype_tab));
263 		printf("%s", p);
264 		break;
265 	case FMT_KBDENC:
266 		p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1,
267 			     kbdenc_tab, TABLEN(kbdenc_tab));
268 		printf("%s", p);
269 
270 		flags = KB_VARIANT(*((u_int *) f->valp));
271 		for (i = 0; i < 32; i++) {
272 			if (!(flags & (1 << i)))
273 				continue;
274 			p = int2name(flags & (1 << i), 1,
275 				     kbdvar_tab, TABLEN(kbdvar_tab));
276 			printf(".%s", p);
277 		}
278 		break;
279 	case FMT_KBMAP:
280 		print_kmap((struct wskbd_map_data *) f->valp);
281 		break;
282 	case FMT_SCALE:
283 		printf("%d,%d,%d,%d,%d,%d,%d", wmcoords.minx, wmcoords.maxx,
284 		    wmcoords.miny, wmcoords.maxy, wmcoords.swapxy,
285 		    wmcoords.resx, wmcoords.resy);
286 		break;
287 	default:
288 		errx(1, "internal error: pr_field: no format %d", f->format);
289 		break;
290 	}
291 
292 	printf("\n");
293 }
294 
295 void
296 rd_field(struct field *f, char *val, int merge)
297 {
298 	struct wscons_keymap *mp;
299 	struct field_pc *pc;
300 	u_int u, r, fr;
301 	char *p;
302 	int i;
303 
304 	switch (f->format) {
305 	case FMT_UINT:
306 		if (sscanf(val, "%u", &u) != 1)
307 			errx(1, "%s: not a number", val);
308 		if (merge)
309 			*((u_int *) f->valp) += u;
310 		else
311 			*((u_int *) f->valp) = u;
312 		break;
313 	case FMT_INT:
314 		if (sscanf(val, "%d", &i) != 1)
315 			errx(1, "%s: not a number", val);
316 		if (merge)
317 			*((int *) f->valp) += i;
318 		else
319 			*((int *) f->valp) = i;
320 		break;
321 	case FMT_BOOL:
322 		if (*val != 'o' || (val[1] != 'n' &&
323 		    (val[1] != 'f' || val[2] != 'f')))
324 			errx(1, "%s: invalid value (on/off)", val);
325 		*((u_int *) f->valp) = val[1] == 'n'? 1 : 0;
326 		break;
327 	case FMT_PC:
328 		fr = 0;
329 		if ((i = sscanf(val, "%u.%u%%", &u, &fr)) != 2 && i != 1)
330 			errx(1, "%s: not a valid number", val);
331 		pc = f->valp;
332 		r = pc->max - pc->min;
333 		i = pc->min + (r * u) / 100 + (r * fr) / 100 / 100;
334 		if (merge == '+')
335 			pc->cur += i;
336 		else if (merge == '-')
337 			pc->cur -= i;
338 		else
339 			pc->cur = i;
340 		if (pc->cur > pc->max)
341 			pc->cur = pc->max;
342 		if (pc->cur < pc->min)
343 			pc->cur = pc->min;
344 		break;
345 	case FMT_KBDENC:
346 		p = strchr(val, '.');
347 		if (p != NULL)
348 			*p++ = '\0';
349 
350 		i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab));
351 		if (i == -1)
352 			errx(1, "%s: not a valid encoding", val);
353 		*((u_int *) f->valp) = i;
354 
355 		while (p) {
356 			val = p;
357 			p = strchr(p, '.');
358 			if (p != NULL)
359 				*p++ = '\0';
360 			i = name2int(val, kbdvar_tab, TABLEN(kbdvar_tab));
361 			if (i == -1)
362 				errx(1, "%s: not a valid variant", val);
363 			*((u_int *) f->valp) |= i;
364 		}
365 		break;
366 	case FMT_KBMAP:
367 		if (! merge)
368 			kbmap.maplen = 0;
369 		map_scan_setinput(val);
370 		yyparse();
371 		if (merge) {
372 			if (newkbmap.maplen < kbmap.maplen)
373 				newkbmap.maplen = kbmap.maplen;
374 			for (i = 0; i < kbmap.maplen; i++) {
375 				mp = newkbmap.map + i;
376 				if (mp->command == KS_voidSymbol &&
377 				    mp->group1[0] == KS_voidSymbol &&
378 				    mp->group1[1] == KS_voidSymbol &&
379 				    mp->group2[0] == KS_voidSymbol &&
380 				    mp->group2[1] == KS_voidSymbol)
381 					*mp = kbmap.map[i];
382 			}
383 		}
384 		kbmap.maplen = newkbmap.maplen;
385 		bcopy(newkbmap.map, kbmap.map,
386 		      kbmap.maplen*sizeof(struct wscons_keymap));
387 		break;
388 	case FMT_SCALE:
389 	{
390 		const char *errstr = 0;
391 
392 		/* Unspecified values default to 0. */
393 		bzero(&wmcoords, sizeof(wmcoords));
394 		val = (void *)strtok(val, ",");
395 		if (val != NULL) {
396 			wmcoords.minx = (int)strtonum(val,
397 			    0, 32768, &errstr);
398 			val = (void *)strtok(NULL, ",");
399 		}
400 		if (!errstr && val != NULL) {
401 			wmcoords.maxx = (int)strtonum(val,
402 			    0, 32768, &errstr);
403 			val = (void *)strtok(NULL, ",");
404 		}
405 		if (!errstr && val != NULL) {
406 			wmcoords.miny = (int)strtonum(val,
407 			    0, 32768, &errstr);
408 			val = (void *)strtok(NULL, ",");
409 		}
410 		if (!errstr && val != NULL) {
411 			wmcoords.maxy = (int)strtonum(val,
412 			    0, 32768, &errstr);
413 			val = (void *)strtok(NULL, ",");
414 		}
415 		if (!errstr && val != NULL) {
416 			wmcoords.swapxy = (int)strtonum(val,
417 			    0, 32768, &errstr);
418 			val = (void *)strtok(NULL, ",");
419 		}
420 		if (!errstr && val != NULL) {
421 			wmcoords.resx = (int)strtonum(val,
422 			    0, 32768, &errstr);
423 			val = (void *)strtok(NULL, ",");
424 		}
425 		if (!errstr && val != NULL) {
426 			wmcoords.resy = (int)strtonum(val,
427 			    0, 32768, &errstr);
428 			val = (void *)strtok(NULL, ",");
429 		}
430 		if (errstr)
431 			errx(1, "calibration value is %s", errstr);
432 		if (val != NULL)
433 			errx(1, "too many calibration values");
434 
435 		break;
436 	}
437 	default:
438 		errx(1, "internal error: rd_field: no format %d", f->format);
439 		break;
440 	}
441 }
442 
443 void
444 print_kmap(struct wskbd_map_data *map)
445 {
446 	struct wscons_keymap *mp;
447 	int i;
448 
449 	for (i = 0; i < map->maplen; i++) {
450 		mp = map->map + i;
451 
452 		if (mp->command == KS_voidSymbol &&
453 		    mp->group1[0] == KS_voidSymbol &&
454 		    mp->group1[1] == KS_voidSymbol &&
455 		    mp->group2[0] == KS_voidSymbol &&
456 		    mp->group2[1] == KS_voidSymbol)
457 			continue;
458 		printf("\n");
459 		printf("keycode %u =", i);
460 		if (mp->command != KS_voidSymbol)
461 			printf(" %s", ksym2name(mp->command));
462 		printf(" %s", ksym2name(mp->group1[0]));
463 		if (mp->group1[0] != mp->group1[1] ||
464 		    mp->group1[0] != mp->group2[0] ||
465 		    mp->group1[0] != mp->group2[1]) {
466 			printf(" %s", ksym2name(mp->group1[1]));
467 			if (mp->group1[0] != mp->group2[0] ||
468 			    mp->group1[1] != mp->group2[1]) {
469 				printf(" %s", ksym2name(mp->group2[0]));
470 				printf(" %s", ksym2name(mp->group2[1]));
471 			}
472 		}
473 	}
474 }
475