1 /* $OpenBSD: util.c,v 1.68 2020/03/03 22:04:30 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_RKDRM, "rkdrm" } 175 }; 176 177 static const struct nameint kbdenc_tab[] = { 178 KB_ENCTAB 179 }; 180 181 static const struct nameint kbdvar_tab[] = { 182 KB_VARTAB 183 }; 184 185 char *int2name(int, int, const struct nameint *, int); 186 int name2int(char *, const struct nameint *, int); 187 void print_kmap(struct wskbd_map_data *); 188 void print_emul(struct wsdisplay_emultype *); 189 void print_screen(struct wsdisplay_screentype *); 190 191 struct field * 192 field_by_name(struct field *field_tab, char *name) 193 { 194 const char *p = strchr(name, '.'); 195 196 if (!p++) 197 errx(1, "%s: illegal variable name", name); 198 199 for (; field_tab->name; field_tab++) 200 if (strcmp(field_tab->name, p) == 0) 201 return (field_tab); 202 203 errx(1, "%s: not found", name); 204 } 205 206 struct field * 207 field_by_value(struct field *field_tab, void *addr) 208 { 209 for (; field_tab->name; field_tab++) 210 if (field_tab->valp == addr) 211 return (field_tab); 212 213 errx(1, "internal error: field_by_value: not found"); 214 } 215 216 char * 217 int2name(int val, int uflag, const struct nameint *tab, int len) 218 { 219 static char tmp[20]; 220 int i; 221 222 for (i = 0; i < len; i++) 223 if (tab[i].value == val) 224 return(tab[i].name); 225 226 if (uflag) { 227 snprintf(tmp, sizeof(tmp), "unknown_%d", val); 228 return(tmp); 229 } else 230 return(NULL); 231 } 232 233 int 234 name2int(char *val, const struct nameint *tab, int len) 235 { 236 int i; 237 238 for (i = 0; i < len; i++) 239 if (strcmp(tab[i].name, val) == 0) 240 return(tab[i].value); 241 return(-1); 242 } 243 244 void 245 pr_field(const char *pre, struct field *f, const char *sep) 246 { 247 struct field_pc *pc; 248 u_int flags; 249 int i, n; 250 char *p; 251 252 if (sep) 253 printf("%s.%s%s", pre, f->name, sep); 254 255 switch (f->format) { 256 case FMT_UINT: 257 printf("%u", *((u_int *) f->valp)); 258 break; 259 case FMT_INT: 260 printf("%d", *((int *) f->valp)); 261 break; 262 case FMT_BOOL: 263 printf("%s", *((u_int *) f->valp)? "on" : "off"); 264 break; 265 case FMT_PC: 266 pc = f->valp; 267 i = pc->max - pc->min; 268 n = pc->cur - pc->min; 269 printf("%u.%02u%%", n * 100 / i, ((n * 100) % i) * 100 / i); 270 break; 271 case FMT_KBDTYPE: 272 p = int2name(*((u_int *) f->valp), 1, 273 kbtype_tab, TABLEN(kbtype_tab)); 274 printf("%s", p); 275 break; 276 case FMT_MSTYPE: 277 p = int2name(*((u_int *) f->valp), 1, 278 mstype_tab, TABLEN(mstype_tab)); 279 printf("%s", p); 280 break; 281 case FMT_DPYTYPE: 282 p = int2name(*((u_int *) f->valp), 1, 283 dpytype_tab, TABLEN(dpytype_tab)); 284 printf("%s", p); 285 break; 286 case FMT_KBDENC: 287 p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1, 288 kbdenc_tab, TABLEN(kbdenc_tab)); 289 printf("%s", p); 290 291 flags = KB_VARIANT(*((u_int *) f->valp)); 292 for (i = 0; i < 32; i++) { 293 if (!(flags & (1 << i))) 294 continue; 295 p = int2name(flags & (1 << i), 1, 296 kbdvar_tab, TABLEN(kbdvar_tab)); 297 printf(".%s", p); 298 } 299 break; 300 case FMT_KBMAP: 301 print_kmap((struct wskbd_map_data *) f->valp); 302 break; 303 case FMT_SCALE: 304 printf("%d,%d,%d,%d,%d,%d,%d", wmcoords.minx, wmcoords.maxx, 305 wmcoords.miny, wmcoords.maxy, wmcoords.swapxy, 306 wmcoords.resx, wmcoords.resy); 307 break; 308 case FMT_EMUL: 309 print_emul((struct wsdisplay_emultype *) f->valp); 310 break; 311 case FMT_SCREEN: 312 print_screen((struct wsdisplay_screentype *) f->valp); 313 break; 314 case FMT_STRING: 315 printf("%s", (const char *)f->valp); 316 break; 317 case FMT_CFG: 318 mousecfg_pr_field((struct wsmouse_parameters *) f->valp); 319 break; 320 default: 321 errx(1, "internal error: pr_field: no format %d", f->format); 322 break; 323 } 324 325 printf("\n"); 326 } 327 328 void 329 rd_field(struct field *f, char *val, int merge) 330 { 331 struct wscons_keymap *mp; 332 struct field_pc *pc; 333 u_int u, r, fr; 334 char *p; 335 int i; 336 337 switch (f->format) { 338 case FMT_UINT: 339 if (sscanf(val, "%u", &u) != 1) 340 errx(1, "%s: not a number", val); 341 if (merge) 342 *((u_int *) f->valp) += u; 343 else 344 *((u_int *) f->valp) = u; 345 break; 346 case FMT_INT: 347 if (sscanf(val, "%d", &i) != 1) 348 errx(1, "%s: not a number", val); 349 if (merge) 350 *((int *) f->valp) += i; 351 else 352 *((int *) f->valp) = i; 353 break; 354 case FMT_BOOL: 355 if (*val != 'o' || (val[1] != 'n' && 356 (val[1] != 'f' || val[2] != 'f'))) 357 errx(1, "%s: invalid value (on/off)", val); 358 *((u_int *) f->valp) = val[1] == 'n'? 1 : 0; 359 break; 360 case FMT_PC: 361 fr = 0; 362 if ((i = sscanf(val, "%u.%u%%", &u, &fr)) != 2 && i != 1) 363 errx(1, "%s: not a valid number", val); 364 pc = f->valp; 365 r = pc->max - pc->min; 366 i = pc->min + (r * u) / 100 + (r * fr) / 100 / 100; 367 if (merge == '+') 368 pc->cur += i; 369 else if (merge == '-') 370 pc->cur -= i; 371 else 372 pc->cur = i; 373 if (pc->cur > pc->max) 374 pc->cur = pc->max; 375 if (pc->cur < pc->min) 376 pc->cur = pc->min; 377 break; 378 case FMT_KBDENC: 379 p = strchr(val, '.'); 380 if (p != NULL) 381 *p++ = '\0'; 382 383 i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab)); 384 if (i == -1) 385 errx(1, "%s: not a valid encoding", val); 386 *((u_int *) f->valp) = i; 387 388 while (p) { 389 val = p; 390 p = strchr(p, '.'); 391 if (p != NULL) 392 *p++ = '\0'; 393 i = name2int(val, kbdvar_tab, TABLEN(kbdvar_tab)); 394 if (i == -1) 395 errx(1, "%s: not a valid variant", val); 396 *((u_int *) f->valp) |= i; 397 } 398 break; 399 case FMT_KBMAP: 400 if (! merge) 401 kbmap.maplen = 0; 402 map_scan_setinput(val); 403 yyparse(); 404 if (merge) { 405 if (newkbmap.maplen < kbmap.maplen) 406 newkbmap.maplen = kbmap.maplen; 407 for (i = 0; i < kbmap.maplen; i++) { 408 mp = newkbmap.map + i; 409 if (mp->command == KS_voidSymbol && 410 mp->group1[0] == KS_voidSymbol && 411 mp->group1[1] == KS_voidSymbol && 412 mp->group2[0] == KS_voidSymbol && 413 mp->group2[1] == KS_voidSymbol) 414 *mp = kbmap.map[i]; 415 } 416 } 417 kbmap.maplen = newkbmap.maplen; 418 bcopy(newkbmap.map, kbmap.map, 419 kbmap.maplen*sizeof(struct wscons_keymap)); 420 break; 421 case FMT_SCALE: 422 { 423 const char *errstr = 0; 424 425 /* Unspecified values default to 0. */ 426 bzero(&wmcoords, sizeof(wmcoords)); 427 val = (void *)strtok(val, ","); 428 if (val != NULL) { 429 wmcoords.minx = (int)strtonum(val, 430 -32768, 32767, &errstr); 431 val = (void *)strtok(NULL, ","); 432 } 433 if (!errstr && val != NULL) { 434 wmcoords.maxx = (int)strtonum(val, 435 -32768, 32767, &errstr); 436 val = (void *)strtok(NULL, ","); 437 } 438 if (!errstr && val != NULL) { 439 wmcoords.miny = (int)strtonum(val, 440 -32768, 32767, &errstr); 441 val = (void *)strtok(NULL, ","); 442 } 443 if (!errstr && val != NULL) { 444 wmcoords.maxy = (int)strtonum(val, 445 -32768, 32767, &errstr); 446 val = (void *)strtok(NULL, ","); 447 } 448 if (!errstr && val != NULL) { 449 wmcoords.swapxy = (int)strtonum(val, 450 -32768, 32767, &errstr); 451 val = (void *)strtok(NULL, ","); 452 } 453 if (!errstr && val != NULL) { 454 wmcoords.resx = (int)strtonum(val, 455 0, 32767, &errstr); 456 val = (void *)strtok(NULL, ","); 457 } 458 if (!errstr && val != NULL) { 459 wmcoords.resy = (int)strtonum(val, 460 0, 32767, &errstr); 461 val = (void *)strtok(NULL, ","); 462 } 463 if (errstr) 464 errx(1, "calibration value is %s", errstr); 465 if (val != NULL) 466 errx(1, "too many calibration values"); 467 468 break; 469 } 470 case FMT_STRING: 471 strlcpy(f->valp, val, WSFONT_NAME_SIZE); 472 break; 473 case FMT_CFG: 474 mousecfg_rd_field((struct wsmouse_parameters *) f->valp, val); 475 break; 476 default: 477 errx(1, "internal error: rd_field: no format %d", f->format); 478 break; 479 } 480 } 481 482 void 483 print_kmap(struct wskbd_map_data *map) 484 { 485 struct wscons_keymap *mp; 486 int i; 487 488 for (i = 0; i < map->maplen; i++) { 489 mp = map->map + i; 490 491 if (mp->command == KS_voidSymbol && 492 mp->group1[0] == KS_voidSymbol && 493 mp->group1[1] == KS_voidSymbol && 494 mp->group2[0] == KS_voidSymbol && 495 mp->group2[1] == KS_voidSymbol) 496 continue; 497 printf("\n"); 498 printf("keycode %u =", i); 499 if (mp->command != KS_voidSymbol) 500 printf(" %s", ksym2name(mp->command)); 501 printf(" %s", ksym2name(mp->group1[0])); 502 if (mp->group1[0] != mp->group1[1] || 503 mp->group1[0] != mp->group2[0] || 504 mp->group1[0] != mp->group2[1]) { 505 printf(" %s", ksym2name(mp->group1[1])); 506 if (mp->group1[0] != mp->group2[0] || 507 mp->group1[1] != mp->group2[1]) { 508 printf(" %s", ksym2name(mp->group2[0])); 509 printf(" %s", ksym2name(mp->group2[1])); 510 } 511 } 512 } 513 ksymenc(0); 514 } 515 516 void 517 print_emul(struct wsdisplay_emultype *emuls) 518 { 519 struct wsdisplay_emultype e; 520 int fd,i; 521 char c='\0'; 522 523 fd=emuls->idx; 524 e.idx=0; 525 i = ioctl(fd, WSDISPLAYIO_GETEMULTYPE, &e); 526 while(i == 0) { 527 if (c != '\0') 528 printf("%c", c); 529 printf("%s", e.name); 530 c=','; 531 e.idx++; 532 i = ioctl(fd, WSDISPLAYIO_GETEMULTYPE, &e); 533 } 534 } 535 536 void 537 print_screen(struct wsdisplay_screentype *screens) 538 { 539 struct wsdisplay_screentype s; 540 int fd,i; 541 char c='\0'; 542 543 fd=screens->idx; 544 s.idx=0; 545 i = ioctl(fd, WSDISPLAYIO_GETSCREENTYPE, &s); 546 while(i == 0) { 547 if (c != '\0') 548 printf("%c", c); 549 printf("%s", s.name); 550 c=','; 551 s.idx++; 552 i = ioctl(fd, WSDISPLAYIO_GETSCREENTYPE, &s); 553 } 554 } 555