xref: /openbsd/sbin/wsconsctl/util.c (revision 404b540a)
1 /*	$OpenBSD: util.c,v 1.45 2009/07/03 17:37:02 kettenis 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 };
72 
73 static const struct nameint mstype_tab[] = {
74 	{ WSMOUSE_TYPE_VSXXX,	"dec-tc" },
75 	{ WSMOUSE_TYPE_PS2,	"ps2" },
76 	{ WSMOUSE_TYPE_USB,	"usb" },
77 	{ WSMOUSE_TYPE_LMS,	"lms" },
78 	{ WSMOUSE_TYPE_MMS,	"mms" },
79 	{ WSMOUSE_TYPE_TPANEL,	"touch-panel" },
80 	{ WSMOUSE_TYPE_NEXT,	"NeXT" },
81 	{ WSMOUSE_TYPE_ARCHIMEDES, "archimedes" },
82 	{ WSMOUSE_TYPE_ADB,	"adb" },
83 	{ WSMOUSE_TYPE_HIL,	"hil" },
84 	{ WSMOUSE_TYPE_LUNA,	"luna" },
85 	{ WSMOUSE_TYPE_DOMAIN,	"domain" },
86 	{ WSMOUSE_TYPE_SUN,	"sun" }
87 };
88 
89 static const struct nameint dpytype_tab[] = {
90 	{ WSDISPLAY_TYPE_UNKNOWN,	"unknown" },
91 	{ WSDISPLAY_TYPE_PM_MONO,	"dec-pm-mono" },
92 	{ WSDISPLAY_TYPE_PM_COLOR,	"dec-pm-color" },
93 	{ WSDISPLAY_TYPE_CFB,		"dec-cfb" },
94 	{ WSDISPLAY_TYPE_XCFB,		"dec-xcfb" },
95 	{ WSDISPLAY_TYPE_MFB,		"dec-mfb" },
96 	{ WSDISPLAY_TYPE_SFB,		"dec-sfb" },
97 	{ WSDISPLAY_TYPE_ISAVGA,	"vga-isa" },
98 	{ WSDISPLAY_TYPE_PCIVGA,	"vga-pci" },
99 	{ WSDISPLAY_TYPE_TGA,		"dec-tga-pci" },
100 	{ WSDISPLAY_TYPE_SFBP,		"dec-sfb+" },
101 	{ WSDISPLAY_TYPE_PCIMISC,	"generic-pci" },
102 	{ WSDISPLAY_TYPE_NEXTMONO,	"next-mono" },
103 	{ WSDISPLAY_TYPE_PX,		"dec-px" },
104 	{ WSDISPLAY_TYPE_PXG,		"dec-pxg" },
105 	{ WSDISPLAY_TYPE_TX,		"dec-tx" },
106 	{ WSDISPLAY_TYPE_HPCFB,		"generic-hpc" },
107 	{ WSDISPLAY_TYPE_VIDC,		"arm-vidc" },
108 	{ WSDISPLAY_TYPE_SPX,		"dec-spx" },
109 	{ WSDISPLAY_TYPE_GPX,		"dec-gpx" },
110 	{ WSDISPLAY_TYPE_LCG,		"dec-lcg" },
111 	{ WSDISPLAY_TYPE_VAX_MONO,	"dec-mono" },
112 	{ WSDISPLAY_TYPE_SB_P9100,	"p9100" },
113 	{ WSDISPLAY_TYPE_EGA,		"ega" },
114 	{ WSDISPLAY_TYPE_DCPVR,		"powervr" },
115 	{ WSDISPLAY_TYPE_SUN24,		"sun24" },
116 	{ WSDISPLAY_TYPE_SUNBW,		"sunbw" },
117 	{ WSDISPLAY_TYPE_STI,		"hp-sti" },
118 	{ WSDISPLAY_TYPE_SUNCG3,	"suncg3" },
119 	{ WSDISPLAY_TYPE_SUNCG6,	"suncg6" },
120 	{ WSDISPLAY_TYPE_SUNFFB,	"sunffb" },
121 	{ WSDISPLAY_TYPE_SUNCG14,	"suncg14" },
122 	{ WSDISPLAY_TYPE_SUNCG2,	"suncg2" },
123 	{ WSDISPLAY_TYPE_SUNCG4,	"suncg4" },
124 	{ WSDISPLAY_TYPE_SUNCG8,	"suncg8" },
125 	{ WSDISPLAY_TYPE_SUNTCX,	"suntcx" },
126 	{ WSDISPLAY_TYPE_AGTEN,		"agten" },
127 	{ WSDISPLAY_TYPE_XVIDEO,	"xvideo" },
128 	{ WSDISPLAY_TYPE_SUNCG12,	"suncg12" },
129 	{ WSDISPLAY_TYPE_MGX,		"mgx" },
130 	{ WSDISPLAY_TYPE_SB_P9000,	"p9000" },
131 	{ WSDISPLAY_TYPE_RFLEX,		"rasterflex" },
132 	{ WSDISPLAY_TYPE_LUNA,		"luna" },
133 	{ WSDISPLAY_TYPE_DVBOX,		"davinci" },
134 	{ WSDISPLAY_TYPE_GBOX,		"gatorbox" },
135 	{ WSDISPLAY_TYPE_RBOX,		"renaissance" },
136 	{ WSDISPLAY_TYPE_HYPERION,	"hyperion" },
137 	{ WSDISPLAY_TYPE_TOPCAT,	"topcat" },
138 	{ WSDISPLAY_TYPE_PXALCD,	"pxalcd" },
139 	{ WSDISPLAY_TYPE_MAC68K,	"mac68k" },
140 	{ WSDISPLAY_TYPE_SUNLEO,	"sunleo" },
141 	{ WSDISPLAY_TYPE_TVRX,		"tvrx" },
142 	{ WSDISPLAY_TYPE_CFXGA,		"cfxga" },
143 	{ WSDISPLAY_TYPE_LCSPX,		"dec-lcspx" },
144 	{ WSDISPLAY_TYPE_GBE,		"gbe" },
145 	{ WSDISPLAY_TYPE_LEGSS,		"dec-legss" },
146 	{ WSDISPLAY_TYPE_IFB,		"ifb" },
147 	{ WSDISPLAY_TYPE_RAPTOR,	"raptor" },
148 	{ WSDISPLAY_TYPE_DL,		"displaylink" },
149 	{ WSDISPLAY_TYPE_MACHFB,	"mach64" },
150 	{ WSDISPLAY_TYPE_GFXP,		"gfxp" },
151 	{ WSDISPLAY_TYPE_RADEONFB,	"radeon" }
152 };
153 
154 static const struct nameint kbdenc_tab[] = {
155 	KB_ENCTAB
156 };
157 
158 static const struct nameint kbdvar_tab[] = {
159 	KB_VARTAB
160 };
161 
162 char *int2name(int, int, const struct nameint *, int);
163 int name2int(char *, const struct nameint *, int);
164 void print_kmap(struct wskbd_map_data *);
165 
166 struct field *
167 field_by_name(struct field *field_tab, char *name)
168 {
169 	const char *p = strchr(name, '.');
170 
171 	if (!p++)
172 		errx(1, "%s: illegal variable name", name);
173 
174 	for (; field_tab->name; field_tab++)
175 		if (strcmp(field_tab->name, p) == 0)
176 			return (field_tab);
177 
178 	errx(1, "%s: not found", name);
179 }
180 
181 struct field *
182 field_by_value(struct field *field_tab, void *addr)
183 {
184 	for (; field_tab->name; field_tab++)
185 		if (field_tab->valp == addr)
186 			return (field_tab);
187 
188 	errx(1, "internal error: field_by_value: not found");
189 }
190 
191 char *
192 int2name(int val, int uflag, const struct nameint *tab, int len)
193 {
194 	static char tmp[20];
195 	int i;
196 
197 	for (i = 0; i < len; i++)
198 		if (tab[i].value == val)
199 			return(tab[i].name);
200 
201 	if (uflag) {
202 		snprintf(tmp, sizeof(tmp), "unknown_%d", val);
203 		return(tmp);
204 	} else
205 		return(NULL);
206 }
207 
208 int
209 name2int(char *val, const struct nameint *tab, int len)
210 {
211 	int i;
212 
213 	for (i = 0; i < len; i++)
214 		if (strcmp(tab[i].name, val) == 0)
215 			return(tab[i].value);
216 	return(-1);
217 }
218 
219 void
220 pr_field(const char *pre, struct field *f, const char *sep)
221 {
222 	struct field_pc *pc;
223 	u_int flags;
224 	int i, n;
225 	char *p;
226 
227 	if (sep)
228 		printf("%s.%s%s", pre, f->name, sep);
229 
230 	switch (f->format) {
231 	case FMT_UINT:
232 		printf("%u", *((u_int *) f->valp));
233 		break;
234 	case FMT_INT:
235 		printf("%d", *((int *) f->valp));
236 		break;
237 	case FMT_BOOL:
238 		printf("%s", *((u_int *) f->valp)? "on" : "off");
239 		break;
240 	case FMT_PC:
241 		pc = f->valp;
242 		i = pc->max - pc->min;
243 		n = pc->cur - pc->min;
244 		printf("%u.%02u%%", n * 100 / i, ((n * 100) % i) * 100 / i);
245 		break;
246 	case FMT_KBDTYPE:
247 		p = int2name(*((u_int *) f->valp), 1,
248 			     kbtype_tab, TABLEN(kbtype_tab));
249 		printf("%s", p);
250 		break;
251 	case FMT_MSTYPE:
252 		p = int2name(*((u_int *) f->valp), 1,
253 			     mstype_tab, TABLEN(mstype_tab));
254 		printf("%s", p);
255 		break;
256 	case FMT_DPYTYPE:
257 		p = int2name(*((u_int *) f->valp), 1,
258 			     dpytype_tab, TABLEN(dpytype_tab));
259 		printf("%s", p);
260 		break;
261 	case FMT_KBDENC:
262 		p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1,
263 			     kbdenc_tab, TABLEN(kbdenc_tab));
264 		printf("%s", p);
265 
266 		flags = KB_VARIANT(*((u_int *) f->valp));
267 		for (i = 0; i < 32; i++) {
268 			if (!(flags & (1 << i)))
269 				continue;
270 			p = int2name(flags & (1 << i), 1,
271 				     kbdvar_tab, TABLEN(kbdvar_tab));
272 			printf(".%s", p);
273 		}
274 		break;
275 	case FMT_KBMAP:
276 		print_kmap((struct wskbd_map_data *) f->valp);
277 		break;
278 	case FMT_SCALE:
279 		printf("%d,%d,%d,%d,%d,%d,%d", wmcoords.minx, wmcoords.maxx,
280 		    wmcoords.miny, wmcoords.maxy, wmcoords.swapxy,
281 		    wmcoords.resx, wmcoords.resy);
282 		break;
283 	default:
284 		errx(1, "internal error: pr_field: no format %d", f->format);
285 		break;
286 	}
287 
288 	printf("\n");
289 }
290 
291 void
292 rd_field(struct field *f, char *val, int merge)
293 {
294 	struct wscons_keymap *mp;
295 	struct field_pc *pc;
296 	u_int u, r, fr;
297 	char *p;
298 	int i;
299 
300 	switch (f->format) {
301 	case FMT_UINT:
302 		if (sscanf(val, "%u", &u) != 1)
303 			errx(1, "%s: not a number", val);
304 		if (merge)
305 			*((u_int *) f->valp) += u;
306 		else
307 			*((u_int *) f->valp) = u;
308 		break;
309 	case FMT_INT:
310 		if (sscanf(val, "%d", &i) != 1)
311 			errx(1, "%s: not a number", val);
312 		if (merge)
313 			*((int *) f->valp) += i;
314 		else
315 			*((int *) f->valp) = i;
316 		break;
317 	case FMT_BOOL:
318 		if (*val != 'o' || (val[1] != 'n' &&
319 		    (val[1] != 'f' || val[2] != 'f')))
320 			errx(1, "%s: invalid value (on/off)", val);
321 		*((u_int *) f->valp) = val[1] == 'n'? 1 : 0;
322 		break;
323 	case FMT_PC:
324 		fr = 0;
325 		if ((i = sscanf(val, "%u.%u%%", &u, &fr)) != 2 && i != 1)
326 			errx(1, "%s: not a valid number", val);
327 		pc = f->valp;
328 		r = pc->max - pc->min;
329 		i = pc->min + (r * u) / 100 + (r * fr) / 100 / 100;
330 		if (merge == '+')
331 			pc->cur += i;
332 		else if (merge == '-')
333 			pc->cur -= i;
334 		else
335 			pc->cur = i;
336 		if (pc->cur > pc->max)
337 			pc->cur = pc->max;
338 		if (pc->cur < pc->min)
339 			pc->cur = pc->min;
340 		break;
341 	case FMT_KBDENC:
342 		p = strchr(val, '.');
343 		if (p != NULL)
344 			*p++ = '\0';
345 
346 		i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab));
347 		if (i == -1)
348 			errx(1, "%s: not a valid encoding", val);
349 		*((u_int *) f->valp) = i;
350 
351 		while (p) {
352 			val = p;
353 			p = strchr(p, '.');
354 			if (p != NULL)
355 				*p++ = '\0';
356 			i = name2int(val, kbdvar_tab, TABLEN(kbdvar_tab));
357 			if (i == -1)
358 				errx(1, "%s: not a valid variant", val);
359 			*((u_int *) f->valp) |= i;
360 		}
361 		break;
362 	case FMT_KBMAP:
363 		if (! merge)
364 			kbmap.maplen = 0;
365 		map_scan_setinput(val);
366 		yyparse();
367 		if (merge) {
368 			if (newkbmap.maplen < kbmap.maplen)
369 				newkbmap.maplen = kbmap.maplen;
370 			for (i = 0; i < kbmap.maplen; i++) {
371 				mp = newkbmap.map + i;
372 				if (mp->command == KS_voidSymbol &&
373 				    mp->group1[0] == KS_voidSymbol &&
374 				    mp->group1[1] == KS_voidSymbol &&
375 				    mp->group2[0] == KS_voidSymbol &&
376 				    mp->group2[1] == KS_voidSymbol)
377 					*mp = kbmap.map[i];
378 			}
379 		}
380 		kbmap.maplen = newkbmap.maplen;
381 		bcopy(newkbmap.map, kbmap.map,
382 		      kbmap.maplen*sizeof(struct wscons_keymap));
383 		break;
384 	case FMT_SCALE:
385 	{
386 		const char *errstr = 0;
387 
388 		/* Unspecified values default to 0. */
389 		bzero(&wmcoords, sizeof(wmcoords));
390 		val = (void *)strtok(val, ",");
391 		if (val != NULL) {
392 			wmcoords.minx = (int)strtonum(val,
393 			    0, 32768, &errstr);
394 			val = (void *)strtok(NULL, ",");
395 		}
396 		if (!errstr && val != NULL) {
397 			wmcoords.maxx = (int)strtonum(val,
398 			    0, 32768, &errstr);
399 			val = (void *)strtok(NULL, ",");
400 		}
401 		if (!errstr && val != NULL) {
402 			wmcoords.miny = (int)strtonum(val,
403 			    0, 32768, &errstr);
404 			val = (void *)strtok(NULL, ",");
405 		}
406 		if (!errstr && val != NULL) {
407 			wmcoords.maxy = (int)strtonum(val,
408 			    0, 32768, &errstr);
409 			val = (void *)strtok(NULL, ",");
410 		}
411 		if (!errstr && val != NULL) {
412 			wmcoords.swapxy = (int)strtonum(val,
413 			    0, 32768, &errstr);
414 			val = (void *)strtok(NULL, ",");
415 		}
416 		if (!errstr && val != NULL) {
417 			wmcoords.resx = (int)strtonum(val,
418 			    0, 32768, &errstr);
419 			val = (void *)strtok(NULL, ",");
420 		}
421 		if (!errstr && val != NULL) {
422 			wmcoords.resy = (int)strtonum(val,
423 			    0, 32768, &errstr);
424 			val = (void *)strtok(NULL, ",");
425 		}
426 		if (errstr)
427 			errx(1, "calibration value is %s", errstr);
428 		if (val != NULL)
429 			errx(1, "too many calibration values");
430 
431 		break;
432 	}
433 	default:
434 		errx(1, "internal error: rd_field: no format %d", f->format);
435 		break;
436 	}
437 }
438 
439 void
440 print_kmap(struct wskbd_map_data *map)
441 {
442 	struct wscons_keymap *mp;
443 	int i;
444 
445 	for (i = 0; i < map->maplen; i++) {
446 		mp = map->map + i;
447 
448 		if (mp->command == KS_voidSymbol &&
449 		    mp->group1[0] == KS_voidSymbol &&
450 		    mp->group1[1] == KS_voidSymbol &&
451 		    mp->group2[0] == KS_voidSymbol &&
452 		    mp->group2[1] == KS_voidSymbol)
453 			continue;
454 		printf("\n");
455 		printf("keycode %u =", i);
456 		if (mp->command != KS_voidSymbol)
457 			printf(" %s", ksym2name(mp->command));
458 		printf(" %s", ksym2name(mp->group1[0]));
459 		if (mp->group1[0] != mp->group1[1] ||
460 		    mp->group1[0] != mp->group2[0] ||
461 		    mp->group1[0] != mp->group2[1]) {
462 			printf(" %s", ksym2name(mp->group1[1]));
463 			if (mp->group1[0] != mp->group2[0] ||
464 			    mp->group1[1] != mp->group2[1]) {
465 				printf(" %s", ksym2name(mp->group2[0]));
466 				printf(" %s", ksym2name(mp->group2[1]));
467 			}
468 		}
469 	}
470 }
471