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