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