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