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