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