kmap_linux.c (5991B)
1 /* see LICENSE file for copyright and license details */ 2 #include "../kmap.h" 3 #include "../paths.h" 4 #include "../util.h" 5 6 /* the linux console: a key's meaning in one table per held modifier set 7 * (KDSKBENT), and an accent table for the dead keys (KDSKBDIACRUC) */ 8 #if defined(__linux__) && !defined(ARUU_FORCE_POSIX) 9 #include <sys/ioctl.h> 10 11 #include <fcntl.h> 12 #include <string.h> 13 #include <unistd.h> 14 15 // linux/kd.h and linux/keyboard.h, which a libc need not carry 16 #define KDSKBENT 0x4B47 17 #define KDSKBDIACRUC 0x4BFB 18 #define KDGKBTYPE 0x4B33 19 20 #define K(t, v) (((t) << 8) | (v)) 21 #define KT_LATIN 0 22 #define KT_SPEC 2 23 #define KT_PAD 3 24 #define KT_DEAD 4 25 #define KT_SHIFT 7 26 #define KT_META 8 27 #define KT_LETTER 11 28 #define KT_DEAD2 13 29 #define KG_ALTGR 1 30 #define K_HOLE K(KT_SPEC, 0) 31 #define K_ENTER K(KT_SPEC, 1) 32 #define K_COMPOSE K(KT_SPEC, 14) 33 #define K_PCOMMA K(KT_PAD, 15) 34 35 #define T_SHIFT 1 36 #define T_ALTGR 2 37 #define T_CTRL 4 38 #define T_ALT 8 39 40 struct kbentry { 41 unsigned char table; 42 unsigned char index; 43 unsigned short value; 44 }; 45 46 struct kbdiacruc { 47 unsigned int diacr, base, result; 48 }; 49 50 struct kbdiacrsuc { 51 unsigned int cnt; 52 struct kbdiacruc d[256]; 53 }; 54 55 // a dead key's console value: the kernel's own six, then the rest as dead2, 56 // named in the accent table by a latin-1 character, as only those fit 57 static const struct { 58 long comb; // what kmap_dead gives 59 int type, value; 60 unsigned diacr; 61 } deads[] = { 62 {0x300, KT_DEAD, 0, '`'}, {0x301, KT_DEAD, 1, '\''}, {0x302, KT_DEAD, 2, '^'}, 63 {0x303, KT_DEAD, 3, '~'}, {0x308, KT_DEAD, 4, '"'}, {0x327, KT_DEAD, 5, ','}, 64 {0x304, KT_DEAD2, 0xaf, 0xaf}, {0x30a, KT_DEAD2, 0xb0, 0xb0}, {0x30c, KT_DEAD2, 0xa6, 0xa6}, 65 {0x306, KT_DEAD2, 0xa4, 0xa4}, {0x307, KT_DEAD2, 0xb7, 0xb7}, {0x30b, KT_DEAD2, 0xa8, 0xa8}, 66 {0x328, KT_DEAD2, 0xb8, 0xb8}, {0x323, KT_DEAD2, 0xa7, 0xa7}, {0x309, KT_DEAD2, 0xbf, 0xbf}, 67 {0x31b, KT_DEAD2, 0xb9, 0xb9}, {0x335, KT_DEAD2, 0xac, 0xac}, {0x326, KT_DEAD2, 0xb5, 0xb5}, 68 {0x331, KT_DEAD2, 0xb3, 0xb3}, {0x345, KT_DEAD2, 0xbd, 0xbd}, 69 }; 70 71 static char *ttys[] = { 72 ARUU_LINUX_PATH_PROC "/self/fd/0", 73 ARUU_PATH_DEVCONSOLE, 74 ARUU_PATH_DEVTTY, 75 "/dev/tty0", 76 }; 77 78 static int 79 console(void) 80 { 81 char type; 82 size_t i; 83 int fd; 84 85 for (i = 0; i < LEN(ttys); i++) { 86 if ((fd = open(ttys[i], O_RDWR)) < 0 && (fd = open(ttys[i], O_RDONLY)) < 0) 87 continue; 88 if (ioctl(fd, KDGKBTYPE, &type) == 0) 89 return fd; 90 close(fd); 91 } 92 weprintf("no console to set the keyboard of\n"); 93 return -1; 94 } 95 96 static int 97 dead_of(const char *s) 98 { 99 long c = kmap_dead(s); 100 size_t i; 101 102 for (i = 0; c >= 0 && i < LEN(deads); i++) 103 if (deads[i].comb == c) 104 return (int)i; 105 return -1; 106 } 107 108 // latin-1 as itself (a letter when caps shifts it), the rest of unicode as 109 // the kernel takes it, dead keys and the few names the console means 110 // something by, else a hole 111 static unsigned short 112 value_of(const char *s, int caps) 113 { 114 long c; 115 int d; 116 117 if ((d = dead_of(s)) >= 0) 118 return K(deads[d].type, deads[d].value); 119 if (!strcmp(s, "ISO_Level3_Shift")) 120 return K(KT_SHIFT, KG_ALTGR); 121 if (!strcmp(s, "Multi_key")) 122 return K_COMPOSE; 123 if (!strcmp(s, "Return")) 124 return K_ENTER; 125 if ((c = kmap_unicode(s)) < 0 || c >= 0xf000) 126 return K_HOLE; 127 if (c < 0x100) 128 return K(caps ? KT_LETTER : KT_LATIN, c); 129 return (unsigned short)(c ^ 0xf000); 130 } 131 132 static int 133 set(int fd, int table, int code, unsigned short v) 134 { 135 struct kbentry e = {table, code, v}; 136 137 if (ioctl(fd, KDSKBENT, &e) != 0) { 138 weprintf("KDSKBENT table %d key %d:", table, code); 139 return -1; 140 } 141 return 0; 142 } 143 144 int 145 kmap_load_console(const struct kmap *k) 146 { 147 // the console has four of a key's levels 148 static const int tables[4] = {0, T_SHIFT, T_ALTGR, T_SHIFT | T_ALTGR}; 149 struct kbdiacrsuc acc; 150 int fd = console(), rc = 0, l, code; 151 size_t i, j; 152 153 if (fd < 0) 154 return -1; 155 for (i = 0; i < k->nkeys; i++) { 156 const struct kmap_key *key = &k->keys[i]; 157 long c0; 158 159 if ((code = kmap_code(key->name, KMAP_EVDEV)) < 0) 160 continue; 161 // the keypad's decimal key keeps the console's own, which num lock turns 162 // between delete and its character: only which character differs 163 if (!strcmp(key->name, "KPDL")) { 164 for (l = 0; l < key->nsym; l++) 165 if (!strcmp(key->sym[l], ",")) 166 rc |= set(fd, 0, code, K_PCOMMA); 167 continue; 168 } 169 for (l = 0; l < 4; l++) 170 rc |= set(fd, tables[l], code, l < key->nsym ? value_of(key->sym[l], key->caps) : K_HOLE); 171 // control and alt follow the letter the key types, so ctrl+a is ^A on 172 // any latin layout. on another script they stay where the console had them 173 c0 = key->nsym ? kmap_unicode(key->sym[0]) : -1; 174 if (c0 >= 'a' && c0 <= 'z') { 175 rc |= set(fd, T_CTRL, code, K(KT_LATIN, c0 & 0x1f)); 176 rc |= set(fd, T_CTRL | T_SHIFT, code, K(KT_LATIN, c0 & 0x1f)); 177 } 178 if (c0 > 0x20 && c0 < 0x7f) 179 rc |= set(fd, T_ALT, code, K(KT_META, c0)); 180 } 181 182 // the accent table: what this layout's dead keys make. the kernel takes 183 // one fewer than its table holds 184 memset(&acc, 0, sizeof(acc)); 185 for (j = 0; j < k->ncompose && acc.cnt < LEN(acc.d) - 1; j++) { 186 const struct kmap_compose *cp = &k->compose[j]; 187 long base = kmap_unicode(cp->base), res = kmap_unicode(cp->result); 188 int d = dead_of(cp->dead), used = 0; 189 190 if (d < 0 || base < 0 || res < 0) 191 continue; 192 for (i = 0; i < k->nkeys && !used; i++) 193 for (l = 0; l < k->keys[i].nsym && !used; l++) 194 used = !strcmp(k->keys[i].sym[l], cp->dead); 195 if (!used) 196 continue; 197 acc.d[acc.cnt].diacr = deads[d].diacr; 198 acc.d[acc.cnt].base = base; 199 acc.d[acc.cnt].result = res; 200 acc.cnt++; 201 } 202 if (ioctl(fd, KDSKBDIACRUC, &acc) != 0) { 203 weprintf("KDSKBDIACRUC:"); 204 rc = -1; 205 } 206 close(fd); 207 return rc; 208 } 209 #endif