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