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