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