xref: /netbsd-src/sbin/wsconsctl/util.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: util.c,v 1.20 2004/07/30 11:08:03 jmmv Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 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.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/time.h>
40 #include <dev/wscons/wsconsio.h>
41 #include <dev/wscons/wsksymdef.h>
42 #include <err.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 #include "wsconsctl.h"
47 
48 #define TABLEN(t)		(sizeof(t)/sizeof(t[0]))
49 
50 extern struct wskbd_map_data kbmap;	/* from keyboard.c */
51 extern struct wskbd_map_data newkbmap;	/* from map_parse.y */
52 
53 struct nameint {
54 	int value;
55 	char *name;
56 };
57 
58 static struct nameint kbtype_tab[] = {
59 	{ WSKBD_TYPE_LK201,		"lk201" },
60 	{ WSKBD_TYPE_LK401,		"lk401" },
61 	{ WSKBD_TYPE_PC_XT,		"pc-xt" },
62 	{ WSKBD_TYPE_PC_AT,		"pc-at" },
63 	{ WSKBD_TYPE_USB,		"usb" },
64 	{ WSKBD_TYPE_HPC_KBD,		"hpc-kbd" },
65 	{ WSKBD_TYPE_HPC_BTN,		"hpc-btn" },
66 	{ WSKBD_TYPE_ARCHIMEDES,	"archimedes" },
67 	{ WSKBD_TYPE_RISCPC,		"riscpc" },
68 	{ WSKBD_TYPE_ADB,		"adb" },
69 	{ WSKBD_TYPE_HIL,		"hil" },
70 	{ WSKBD_TYPE_AMIGA,		"amiga" },
71 	{ WSKBD_TYPE_MAPLE,		"maple" },
72 	{ WSKBD_TYPE_ATARI,		"atari" },
73 	{ WSKBD_TYPE_SUN,		"sun" },
74 	{ WSKBD_TYPE_SUN5,		"sun-type5" },
75 };
76 
77 static struct nameint mstype_tab[] = {
78 	{ WSMOUSE_TYPE_VSXXX,		"dec-tc" },
79 	{ WSMOUSE_TYPE_PS2,		"ps2" },
80 	{ WSMOUSE_TYPE_USB,		"usb" },
81 	{ WSMOUSE_TYPE_LMS,		"logitech-bus" },
82 	{ WSMOUSE_TYPE_MMS,		"ms-inport" },
83 	{ WSMOUSE_TYPE_TPANEL,		"touch-panel" },
84 	{ WSMOUSE_TYPE_NEXT,		"next" },
85 	{ WSMOUSE_TYPE_ARCHIMEDES,	"archimedes" },
86 	{ WSMOUSE_TYPE_HIL,		"hil" },
87 	{ WSMOUSE_TYPE_AMIGA,		"amiga" },
88 	{ WSMOUSE_TYPE_MAXINE,		"dec-maxine" },
89 	{ WSMOUSE_TYPE_MAPLE,		"maple" },
90 };
91 
92 static 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-vax-mono" },
115 	{ WSDISPLAY_TYPE_SB_P9100,	"sparcbook-p9100" },
116 	{ WSDISPLAY_TYPE_EGA,		"ega" },
117 	{ WSDISPLAY_TYPE_DCPVR,		"dreamcast-pvr" },
118 	{ WSDISPLAY_TYPE_GATOR,		"hp-gator" },
119 	{ WSDISPLAY_TYPE_TOPCAT,	"hp-topcat" },
120 	{ WSDISPLAY_TYPE_RENAISSANCE,	"hp-renaissance" },
121 	{ WSDISPLAY_TYPE_CATSEYE,	"hp-catseye" },
122 	{ WSDISPLAY_TYPE_DAVINCI,	"hp-davinci" },
123 	{ WSDISPLAY_TYPE_TIGER,		"hp-tiger" },
124 	{ WSDISPLAY_TYPE_HYPERION,	"hp-hyperion" },
125 	{ WSDISPLAY_TYPE_AMIGACC,	"amiga-cc" },
126 	{ WSDISPLAY_TYPE_SUN24,		"sun24" },
127 	{ WSDISPLAY_TYPE_NEWPORT,	"sgi-newport" },
128 	{ WSDISPLAY_TYPE_GR2,		"sgi-gr2" },
129 	{ WSDISPLAY_TYPE_SUNCG12,	"suncg12" },
130 	{ WSDISPLAY_TYPE_SUNCG14,	"suncg14" },
131 	{ WSDISPLAY_TYPE_SUNTCX,	"suntcx" },
132 	{ WSDISPLAY_TYPE_SUNFFB,	"sunffb" },
133 };
134 
135 static struct nameint kbdenc_tab[] = {
136 	KB_ENCTAB
137 };
138 
139 static struct nameint kbdvar_tab[] = {
140 	KB_VARTAB
141 };
142 
143 static struct nameint color_tab[] = {
144 	{ WSCOL_UNSUPPORTED,		"unsupported" },
145 	{ WSCOL_BLACK,			"black" },
146 	{ WSCOL_RED,			"red" },
147 	{ WSCOL_GREEN,			"green" },
148 	{ WSCOL_BROWN,			"brown" },
149 	{ WSCOL_BLUE,			"blue" },
150 	{ WSCOL_MAGENTA,		"magenta" },
151 	{ WSCOL_CYAN,			"cyan" },
152 	{ WSCOL_WHITE,			"white" },
153 };
154 
155 static struct nameint attr_tab[] = {
156 	{ WSATTR_NONE,			"none" },
157 	{ WSATTR_REVERSE,		"reverse" },
158 	{ WSATTR_HILIT,			"hilit" },
159 	{ WSATTR_BLINK,			"blink" },
160 	{ WSATTR_UNDERLINE,		"underline" },
161 	{ WSATTR_WSCOLORS,		"color" },
162 };
163 
164 static struct field *field_tab;
165 static int field_tab_len;
166 
167 static char *int2name __P((int, int, struct nameint *, int));
168 static int name2int __P((char *, struct nameint *, int));
169 static void print_kmap __P((struct wskbd_map_data *));
170 
171 void
172 field_setup(ftab, len)
173 	struct field *ftab;
174 	int len;
175 {
176 	field_tab = ftab;
177 	field_tab_len = len;
178 }
179 
180 struct field *
181 field_by_name(name)
182 	char *name;
183 {
184 	int i;
185 
186 	for (i = 0; i < field_tab_len; i++)
187 		if (strcmp(field_tab[i].name, name) == 0)
188 			return(field_tab + i);
189 
190 	errx(1, "%s: not found", name);
191 }
192 
193 struct field *
194 field_by_value(addr)
195 	void *addr;
196 {
197 	int i;
198 
199 	for (i = 0; i < field_tab_len; i++)
200 		if (field_tab[i].valp == addr)
201 			return(field_tab + i);
202 
203 	errx(1, "internal error: field_by_value: not found");
204 }
205 
206 void
207 field_disable_by_value(addr)
208 	void *addr;
209 {
210 	struct field *f;
211 
212 	f = field_by_value(addr);
213 	f->flags |= FLG_DISABLED;
214 }
215 
216 static char *
217 int2name(val, uflag, tab, len)
218 	int val;
219 	int uflag;
220 	struct nameint *tab;
221 	int len;
222 {
223 	static char tmp[20];
224 	int i;
225 
226 	for (i = 0; i < len; i++)
227 		if (tab[i].value == val)
228 			return(tab[i].name);
229 
230 	if (uflag) {
231 		snprintf(tmp, sizeof(tmp), "unknown_%d", val);
232 		return(tmp);
233 	} else
234 		return(NULL);
235 }
236 
237 static int
238 name2int(val, tab, len)
239 	char *val;
240 	struct nameint *tab;
241 	int len;
242 {
243 	int i;
244 
245 	for (i = 0; i < len; i++)
246 		if (strcmp(tab[i].name, val) == 0)
247 			return(tab[i].value);
248 	return(-1);
249 }
250 
251 void
252 pr_field(f, sep)
253 	struct field *f;
254 	char *sep;
255 {
256 	char *p;
257 	u_int flags;
258 	int first, i, mask;
259 
260 	if (sep)
261 		printf("%s%s", f->name, sep);
262 
263 	switch (f->format) {
264 	case FMT_UINT:
265 		printf("%u", *((u_int *) f->valp));
266 		break;
267 	case FMT_STRING:
268 		printf("\"%s\"", *((char **) f->valp));
269 		break;
270 	case FMT_KBDTYPE:
271 		p = int2name(*((u_int *) f->valp), 1,
272 			     kbtype_tab, TABLEN(kbtype_tab));
273 		printf("%s", p);
274 		break;
275 	case FMT_MSTYPE:
276 		p = int2name(*((u_int *) f->valp), 1,
277 			     mstype_tab, TABLEN(mstype_tab));
278 		printf("%s", p);
279 		break;
280 	case FMT_DPYTYPE:
281 		p = int2name(*((u_int *) f->valp), 1,
282 			     dpytype_tab, TABLEN(dpytype_tab));
283 		printf("%s", p);
284 		break;
285 	case FMT_KBDENC:
286 		p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1,
287 			     kbdenc_tab, TABLEN(kbdenc_tab));
288 		printf("%s", p);
289 
290 		flags = KB_VARIANT(*((u_int *) f->valp));
291 		for (i = 0; i < 32; i++) {
292 			if (!(flags & (1 << i)))
293 				continue;
294 			p = int2name(flags & (1 << i), 1,
295 				     kbdvar_tab, TABLEN(kbdvar_tab));
296 			printf(".%s", p);
297 		}
298 		break;
299 	case FMT_KBMAP:
300 		print_kmap((struct wskbd_map_data *) f->valp);
301 		break;
302 	case FMT_COLOR:
303 		p = int2name(*((u_int *) f->valp), 1,
304 			     color_tab, TABLEN(color_tab));
305 		printf("%s", p);
306 		break;
307 	case FMT_ATTRS:
308 		mask = 0x10;
309 		first = 1;
310 		while (mask > 0) {
311 			if (*((u_int *) f->valp) & mask) {
312 				p = int2name(*((u_int *) f->valp) & mask, 1,
313 					     attr_tab, TABLEN(attr_tab));
314 				printf("%s%s", first ? "" : ",", p);
315 				first = 0;
316 			}
317 			mask >>= 1;
318 		}
319 		if (first)
320 			printf("none");
321 		break;
322 	default:
323 		errx(1, "internal error: pr_field: no format %d", f->format);
324 		break;
325 	}
326 
327 	printf("\n");
328 }
329 
330 void
331 rd_field(f, val, merge)
332 	struct field *f;
333 	char *val;
334 	int merge;
335 {
336 	int i;
337 	u_int u;
338 	char *p;
339 	struct wscons_keymap *mp;
340 
341 	switch (f->format) {
342 	case FMT_UINT:
343 		if (sscanf(val, "%u", &u) != 1)
344 			errx(1, "%s: not a number", val);
345 		if (merge)
346 			*((u_int *) f->valp) += u;
347 		else
348 			*((u_int *) f->valp) = u;
349 		break;
350 	case FMT_STRING:
351 		if ((*((char **) f->valp) = strdup(val)) == NULL)
352 			err(1, "strdup");
353 		break;
354 	case FMT_KBDENC:
355 		p = strchr(val, '.');
356 		if (p != NULL)
357 			*p++ = '\0';
358 
359 		i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab));
360 		if (i == -1)
361 			errx(1, "%s: not a valid encoding", val);
362 		*((u_int *) f->valp) = i;
363 
364 		while (p) {
365 			val = p;
366 			p = strchr(p, '.');
367 			if (p != NULL)
368 				*p++ = '\0';
369 			i = name2int(val, kbdvar_tab, TABLEN(kbdvar_tab));
370 			if (i == -1)
371 				errx(1, "%s: not a valid variant", val);
372 			*((u_int *) f->valp) |= i;
373 		}
374 		break;
375 	case FMT_KBMAP:
376 		if (! merge)
377 			kbmap.maplen = 0;
378 		map_scan_setinput(val);
379 		yyparse();
380 		if (merge) {
381 			if (newkbmap.maplen < kbmap.maplen)
382 				newkbmap.maplen = kbmap.maplen;
383 			for (i = 0; i < kbmap.maplen; i++) {
384 				mp = newkbmap.map + i;
385 				if (mp->command == KS_voidSymbol &&
386 				    mp->group1[0] == KS_voidSymbol &&
387 				    mp->group1[1] == KS_voidSymbol &&
388 				    mp->group2[0] == KS_voidSymbol &&
389 				    mp->group2[1] == KS_voidSymbol)
390 					*mp = kbmap.map[i];
391 			}
392 		}
393 		kbmap.maplen = newkbmap.maplen;
394 		bcopy(newkbmap.map, kbmap.map,
395 		      kbmap.maplen*sizeof(struct wscons_keymap));
396 		break;
397 	case FMT_COLOR:
398 		i = name2int(val, color_tab, TABLEN(color_tab));
399 		if (i == -1)
400 			errx(1, "%s: not a valid color", val);
401 		*((u_int *) f->valp) = i;
402 		break;
403 	case FMT_ATTRS:
404 		p = val;
405 		while (p) {
406 			val = p;
407 			p = strchr(p, ',');
408 			if (p != NULL)
409 				*p++ = '\0';
410 			i = name2int(val, attr_tab, TABLEN(attr_tab));
411 			if (i == -1)
412 				errx(1, "%s: not a valid attribute", val);
413 			*((u_int *) f->valp) |= i;
414 		}
415 		break;
416 	default:
417 		errx(1, "internal error: rd_field: no format %d", f->format);
418 		break;
419 	}
420 }
421 
422 static void
423 print_kmap(map)
424 	struct wskbd_map_data *map;
425 {
426 	int i;
427 	struct wscons_keymap *mp;
428 
429 	for (i = 0; i < map->maplen; i++) {
430 		mp = map->map + i;
431 
432 		if (mp->command == KS_voidSymbol &&
433 		    mp->group1[0] == KS_voidSymbol &&
434 		    mp->group1[1] == KS_voidSymbol &&
435 		    mp->group2[0] == KS_voidSymbol &&
436 		    mp->group2[1] == KS_voidSymbol)
437 			continue;
438 		printf("\n");
439 		printf("keycode %u =", i);
440 		if (mp->command != KS_voidSymbol)
441 			printf(" %s", ksym2name(mp->command));
442 		printf(" %s", ksym2name(mp->group1[0]));
443 		if (mp->group1[0] != mp->group1[1] ||
444 		    mp->group1[0] != mp->group2[0] ||
445 		    mp->group1[0] != mp->group2[1]) {
446 			printf(" %s", ksym2name(mp->group1[1]));
447 			if (mp->group1[0] != mp->group2[0] ||
448 			    mp->group1[1] != mp->group2[1]) {
449 				printf(" %s", ksym2name(mp->group2[0]));
450 				printf(" %s", ksym2name(mp->group2[1]));
451 			}
452 		}
453 	}
454 }
455