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