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