kmap_wscons.c (3292B)
1 /* see LICENSE file for copyright and license details */ 2 #include "../kmap.h" 3 #include "../util.h" 4 5 /* wscons, netbsd's and openbsd's console: the keyboard's whole map read 6 * (WSKBDIO_GETMAP), the layout's keys put in it, and written back. a 7 * keysym is 16 bit unicode and a dead key the combining character it puts 8 * on the next, as kmap_dead gives it; wscons composes them itself. group1 9 * holds levels 1 and 2, group2 levels 3 and 4, reached with Mode_switch */ 10 #if (defined(__NetBSD__) || defined(__OpenBSD__)) && !defined(ARUU_FORCE_POSIX) 11 #include <sys/ioctl.h> 12 #include <sys/time.h> 13 #include <sys/types.h> 14 15 #include <dev/wscons/wsconsio.h> 16 #include <dev/wscons/wsksymdef.h> 17 #include <dev/wscons/wsksymvar.h> 18 19 #include <fcntl.h> 20 #include <stdlib.h> 21 #include <string.h> 22 #include <unistd.h> 23 24 static const char *kbds[] = {"/dev/wskbd", "/dev/wskbd0"}; 25 26 // right alt, where a layout's level 3 is reached from: its pc scancode 27 // (0x38 behind e0, wscons numbers it 0xb8) and its hid usage 28 #define PC_RALT 0xb8 29 #define HID_RALT 0xe6 30 31 static keysym_t 32 keysym_of(const char *s) 33 { 34 long c; 35 36 if ((c = kmap_dead(s)) >= 0) 37 return (keysym_t)c; 38 if (!strcmp(s, "ISO_Level3_Shift")) 39 return KS_Mode_switch; 40 if (!strcmp(s, "Multi_key")) 41 return KS_Multi_key; 42 if (!strcmp(s, "Return")) 43 return KS_Return; 44 if ((c = kmap_unicode(s)) < 0 || c >= 0xf000) 45 return KS_voidSymbol; 46 return (keysym_t)c; 47 } 48 49 int 50 kmap_load_console(const struct kmap *k) 51 { 52 struct wskbd_map_data md; 53 struct wscons_keymap *map; 54 enum kmap_codes scheme; 55 u_int type; 56 size_t i; 57 int fd = -1, code, l, rc = 0; 58 59 for (i = 0; i < LEN(kbds) && fd < 0; i++) 60 fd = open(kbds[i], O_RDWR); 61 if (fd < 0) { 62 weprintf("open %s:", kbds[LEN(kbds) - 1]); 63 return -1; 64 } 65 // a usb keyboard's keys are numbered by hid usage, a pc keyboard's by 66 // its scancodes 67 scheme = ioctl(fd, WSKBDIO_GTYPE, &type) == 0 && type == WSKBD_TYPE_USB ? KMAP_HID : KMAP_PC; 68 map = ecalloc(KS_NUMKEYCODES, sizeof(*map)); 69 md.maplen = KS_NUMKEYCODES; 70 md.map = map; 71 if (ioctl(fd, WSKBDIO_GETMAP, &md) != 0) { 72 weprintf("WSKBDIO_GETMAP:"); 73 goto fail; 74 } 75 for (i = 0; i < k->nkeys; i++) { 76 const struct kmap_key *key = &k->keys[i]; 77 keysym_t s[4]; 78 79 if ((code = kmap_code(key->name, scheme)) < 0 || (u_int)code >= md.maplen) 80 continue; 81 for (l = 0; l < 4; l++) 82 s[l] = l < key->nsym ? keysym_of(key->sym[l]) : KS_voidSymbol; 83 // a key with one level types it shifted too, as wscons keymaps write it 84 if (s[1] == KS_voidSymbol) 85 s[1] = s[0]; 86 map[code].group1[0] = s[0]; 87 map[code].group1[1] = s[1]; 88 map[code].group2[0] = s[2]; 89 map[code].group2[1] = s[3] == KS_voidSymbol ? s[2] : s[3]; 90 } 91 if (k->level3_ralt) { 92 code = scheme == KMAP_HID ? HID_RALT : PC_RALT; 93 if ((u_int)code < md.maplen) { 94 map[code].command = KS_voidSymbol; 95 map[code].group1[0] = map[code].group1[1] = KS_Mode_switch; 96 map[code].group2[0] = map[code].group2[1] = KS_Mode_switch; 97 } 98 } 99 if (ioctl(fd, WSKBDIO_SETMAP, &md) != 0) { 100 weprintf("WSKBDIO_SETMAP:"); 101 goto fail; 102 } 103 free(map); 104 close(fd); 105 return rc; 106 fail: 107 free(map); 108 close(fd); 109 return -1; 110 } 111 #endif