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