xref: /netbsd-src/sbin/wsconsctl/util.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: util.c,v 1.8 2000/03/14 08:11:53 sato 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 };
67 
68 static struct nameint mstype_tab[] = {
69 	{ WSMOUSE_TYPE_VSXXX,	"dec-tc" },
70 	{ WSMOUSE_TYPE_PS2,	"ps2" },
71 	{ WSMOUSE_TYPE_USB,	"usb" },
72 	{ WSMOUSE_TYPE_TPANEL,	"touch-pannel" },
73 };
74 
75 static struct nameint dpytype_tab[] = {
76 	{ WSDISPLAY_TYPE_UNKNOWN,	"unknown" },
77 	{ WSDISPLAY_TYPE_PM_MONO,	"dec-pm-mono" },
78 	{ WSDISPLAY_TYPE_PM_COLOR,	"dec-pm-color" },
79 	{ WSDISPLAY_TYPE_CFB,		"dec-cfb" },
80 	{ WSDISPLAY_TYPE_XCFB,		"dec-xcfb" },
81 	{ WSDISPLAY_TYPE_MFB,		"dec-mfb" },
82 	{ WSDISPLAY_TYPE_SFB,		"dec-sfb" },
83 	{ WSDISPLAY_TYPE_ISAVGA,	"vga-isa" },
84 	{ WSDISPLAY_TYPE_PCIVGA,	"vga-pci" },
85 	{ WSDISPLAY_TYPE_TGA,		"dec-tga-pci" },
86 	{ WSDISPLAY_TYPE_SFBP,		"dec-sfb+" },
87 	{ WSDISPLAY_TYPE_PCIMISC,	"generic-pci" },
88 	{ WSDISPLAY_TYPE_NEXTMONO,	"next-mono" },
89 	{ WSDISPLAY_TYPE_PX,		"dex-px" },
90 	{ WSDISPLAY_TYPE_PXG,		"dex-pxg" },
91 	{ WSDISPLAY_TYPE_TX,		"dex-tx" },
92 	{ WSDISPLAY_TYPE_HPCFB,		"generic-hpc" },
93 };
94 
95 static struct nameint kbdenc_tab[] = {
96 	KB_ENCTAB
97 };
98 
99 static struct nameint kbdvar_tab[] = {
100 	KB_VARTAB
101 };
102 
103 static struct field *field_tab;
104 static int field_tab_len;
105 
106 static char *int2name __P((int, int, struct nameint *, int));
107 static int name2int __P((char *, struct nameint *, int));
108 static void print_kmap __P((struct wskbd_map_data *));
109 
110 void
111 field_setup(ftab, len)
112 	struct field *ftab;
113 	int len;
114 {
115 	field_tab = ftab;
116 	field_tab_len = len;
117 }
118 
119 struct field *
120 field_by_name(name)
121 	char *name;
122 {
123 	int i;
124 
125 	for (i = 0; i < field_tab_len; i++)
126 		if (strcmp(field_tab[i].name, name) == 0)
127 			return(field_tab + i);
128 
129 	errx(1, "%s: not found", name);
130 }
131 
132 struct field *
133 field_by_value(addr)
134 	void *addr;
135 {
136 	int i;
137 
138 	for (i = 0; i < field_tab_len; i++)
139 		if (field_tab[i].valp == addr)
140 			return(field_tab + i);
141 
142 	errx(1, "internal error: field_by_value: not found");
143 }
144 
145 static char *
146 int2name(val, uflag, tab, len)
147 	int val;
148 	int uflag;
149 	struct nameint *tab;
150 	int len;
151 {
152 	static char tmp[20];
153 	int i;
154 
155 	for (i = 0; i < len; i++)
156 		if (tab[i].value == val)
157 			return(tab[i].name);
158 
159 	if (uflag) {
160 		snprintf(tmp, sizeof(tmp), "unknown_%d", val);
161 		return(tmp);
162 	} else
163 		return(NULL);
164 }
165 
166 static int
167 name2int(val, tab, len)
168 	char *val;
169 	struct nameint *tab;
170 	int len;
171 {
172 	int i;
173 
174 	for (i = 0; i < len; i++)
175 		if (strcmp(tab[i].name, val) == 0)
176 			return(tab[i].value);
177 	return(-1);
178 }
179 
180 void
181 pr_field(f, sep)
182 	struct field *f;
183 	char *sep;
184 {
185 	char *p;
186 	u_int flags;
187 	int i;
188 
189 	if (sep)
190 		printf("%s%s", f->name, sep);
191 
192 	switch (f->format) {
193 	case FMT_UINT:
194 		printf("%u", *((u_int *) f->valp));
195 		break;
196 	case FMT_KBDTYPE:
197 		p = int2name(*((u_int *) f->valp), 1,
198 			     kbtype_tab, TABLEN(kbtype_tab));
199 		printf("%s", p);
200 		break;
201 	case FMT_MSTYPE:
202 		p = int2name(*((u_int *) f->valp), 1,
203 			     mstype_tab, TABLEN(mstype_tab));
204 		printf("%s", p);
205 		break;
206 	case FMT_DPYTYPE:
207 		p = int2name(*((u_int *) f->valp), 1,
208 			     dpytype_tab, TABLEN(dpytype_tab));
209 		printf("%s", p);
210 		break;
211 	case FMT_KBDENC:
212 		p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1,
213 			     kbdenc_tab, TABLEN(kbdenc_tab));
214 		printf("%s", p);
215 
216 		flags = KB_VARIANT(*((u_int *) f->valp));
217 		for (i = 0; i < 32; i++) {
218 			if (!(flags & (1 << i)))
219 				continue;
220 			p = int2name(flags & (1 << i), 1,
221 				     kbdvar_tab, TABLEN(kbdvar_tab));
222 			printf(".%s", p);
223 		}
224 		break;
225 	case FMT_KBMAP:
226 		print_kmap((struct wskbd_map_data *) f->valp);
227 		break;
228 	default:
229 		errx(1, "internal error: pr_field: no format %d", f->format);
230 		break;
231 	}
232 
233 	printf("\n");
234 }
235 
236 void
237 rd_field(f, val, merge)
238 	struct field *f;
239 	char *val;
240 	int merge;
241 {
242 	int i;
243 	u_int u;
244 	char *p;
245 	struct wscons_keymap *mp;
246 
247 	switch (f->format) {
248 	case FMT_UINT:
249 		if (sscanf(val, "%u", &u) != 1)
250 			errx(1, "%s: not a number", val);
251 		if (merge)
252 			*((u_int *) f->valp) += u;
253 		else
254 			*((u_int *) f->valp) = u;
255 		break;
256 	case FMT_KBDENC:
257 		p = strchr(val, '.');
258 		if (p != NULL)
259 			*p++ = '\0';
260 
261 		i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab));
262 		if (i == -1)
263 			errx(1, "%s: not a valid encoding", val);
264 		*((u_int *) f->valp) = i;
265 
266 		while (p) {
267 			val = p;
268 			p = strchr(p, '.');
269 			if (p != NULL)
270 				*p++ = '\0';
271 			i = name2int(val, kbdvar_tab, TABLEN(kbdvar_tab));
272 			if (i == -1)
273 				errx(1, "%s: not a valid variant", val);
274 			*((u_int *) f->valp) |= i;
275 		}
276 		break;
277 	case FMT_KBMAP:
278 		if (! merge)
279 			kbmap.maplen = 0;
280 		map_scan_setinput(val);
281 		yyparse();
282 		if (merge) {
283 			if (newkbmap.maplen < kbmap.maplen)
284 				newkbmap.maplen = kbmap.maplen;
285 			for (i = 0; i < kbmap.maplen; i++) {
286 				mp = newkbmap.map + i;
287 				if (mp->command == KS_voidSymbol &&
288 				    mp->group1[0] == KS_voidSymbol &&
289 				    mp->group1[1] == KS_voidSymbol &&
290 				    mp->group2[0] == KS_voidSymbol &&
291 				    mp->group2[1] == KS_voidSymbol)
292 					*mp = kbmap.map[i];
293 			}
294 		}
295 		kbmap.maplen = newkbmap.maplen;
296 		bcopy(newkbmap.map, kbmap.map,
297 		      kbmap.maplen*sizeof(struct wscons_keymap));
298 		break;
299 	default:
300 		errx(1, "internal error: rd_field: no format %d", f->format);
301 		break;
302 	}
303 }
304 
305 static void
306 print_kmap(map)
307 	struct wskbd_map_data *map;
308 {
309 	int i;
310 	struct wscons_keymap *mp;
311 
312 	for (i = 0; i < map->maplen; i++) {
313 		mp = map->map + i;
314 
315 		if (mp->command == KS_voidSymbol &&
316 		    mp->group1[0] == KS_voidSymbol &&
317 		    mp->group1[1] == KS_voidSymbol &&
318 		    mp->group2[0] == KS_voidSymbol &&
319 		    mp->group2[1] == KS_voidSymbol)
320 			continue;
321 		printf("\n");
322 		printf("keycode %u =", i);
323 		if (mp->command != KS_voidSymbol)
324 			printf(" %s", ksym2name(mp->command));
325 		printf(" %s", ksym2name(mp->group1[0]));
326 		if (mp->group1[0] != mp->group1[1] ||
327 		    mp->group1[0] != mp->group2[0] ||
328 		    mp->group1[0] != mp->group2[1]) {
329 			printf(" %s", ksym2name(mp->group1[1]));
330 			if (mp->group1[0] != mp->group2[0] ||
331 			    mp->group1[1] != mp->group2[1]) {
332 				printf(" %s", ksym2name(mp->group2[0]));
333 				printf(" %s", ksym2name(mp->group2[1]));
334 			}
335 		}
336 	}
337 }
338