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