kmap_fbsd.c (4849B)
1 /* see LICENSE file for copyright and license details */ 2 #include "../kmap.h" 3 #include "../util.h" 4 5 /* syscons and vt, freebsd's consoles: the keyboard's map 6 * read (GIO_KEYMAP), the layout's keys put in it and written back, and an 7 * accent map made of the layout's dead keys (PIO_DEADKEYMAP). a key has 8 * eight states, plain, shift, ctrl, ctrl shift, then the same with alt, 9 * which is where these consoles keep level 3 and 4, right alt reaching them */ 10 #if defined(__FreeBSD__) && !defined(ARUU_FORCE_POSIX) 11 #include <sys/ioctl.h> 12 #include <sys/kbio.h> 13 #include <sys/types.h> 14 15 #include <fcntl.h> 16 #include <stdlib.h> 17 #include <string.h> 18 #include <unistd.h> 19 20 // the states the four levels go in, and those control and alt get 21 #define S_PLAIN 0 22 #define S_SHIFT 1 23 #define S_CTRL 2 24 #define S_CTRL_SHIFT 3 25 #define S_ALT 4 26 #define S_ALT_SHIFT 5 27 28 // right alt in these consoles' numbering 29 #define KC_RALT 93 30 31 static const char *ttys[] = {"/dev/ttyv0", "/dev/console"}; 32 33 // the console's accent keys and the combining characters kmap names them by, 34 // with the spacing character the accent map gives each 35 static const struct { 36 long comb; 37 u_int key; 38 u_int spacing; 39 } accs[] = { 40 {0x300, DGRA, '`'}, 41 {0x301, DACU, 0xb4}, 42 {0x302, DCIR, '^'}, 43 {0x303, DTIL, '~'}, 44 {0x304, DMAC, 0xaf}, 45 {0x306, DBRE, 0x2d8}, 46 {0x307, DDOT, 0x2d9}, 47 {0x308, DDIA, 0xa8}, 48 {0x335, DSLA, '/'}, 49 {0x30a, DRIN, 0xb0}, 50 {0x327, DCED, 0xb8}, 51 {0x30b, DDAC, 0x2dd}, 52 {0x328, DOGO, 0x2db}, 53 {0x30c, DCAR, 0x2c7}, 54 }; 55 56 static int 57 acc_of(const char *s) 58 { 59 long c = kmap_dead(s); 60 size_t i; 61 62 for (i = 0; c >= 0 && i < LEN(accs); i++) 63 if (accs[i].comb == c) 64 return (int)i; 65 return -1; 66 } 67 68 // a state of a key: a character, or a special key (a dead key, alt gr) 69 static void 70 put(struct keyent_t *e, int state, const char *s) 71 { 72 long c; 73 int a; 74 75 e->spcl &= ~(0x80 >> state); 76 if ((a = acc_of(s)) >= 0) { 77 e->map[state] = accs[a].key; 78 e->spcl |= 0x80 >> state; 79 } else if (!strcmp(s, "ISO_Level3_Shift")) { 80 e->map[state] = RALT; 81 e->spcl |= 0x80 >> state; 82 } else if ((c = kmap_unicode(s)) >= 0) 83 e->map[state] = (u_int)c; 84 else { 85 e->map[state] = NOP; 86 e->spcl |= 0x80 >> state; 87 } 88 } 89 90 int 91 kmap_load_console(const struct kmap *k) 92 { 93 static const int states[4] = {S_PLAIN, S_SHIFT, S_ALT, S_ALT_SHIFT}; 94 keymap_t *km = ecalloc(1, sizeof(*km)); 95 accentmap_t *am = ecalloc(1, sizeof(*am)); 96 size_t i, j; 97 int fd = -1, code, l, rc = -1; 98 99 if (ioctl(0, GIO_KEYMAP, km) == 0) 100 fd = 0; 101 for (i = 0; i < LEN(ttys) && fd < 0; i++) 102 if ((fd = open(ttys[i], O_RDWR)) >= 0 && ioctl(fd, GIO_KEYMAP, km) != 0) { 103 close(fd); 104 fd = -1; 105 } 106 if (fd < 0) { 107 weprintf("no console to set the keyboard of\n"); 108 goto done; 109 } 110 for (i = 0; i < k->nkeys; i++) { 111 const struct kmap_key *key = &k->keys[i]; 112 struct keyent_t *e; 113 long c0; 114 115 if ((code = kmap_code(key->name, KMAP_PC)) < 0 || code >= km->n_keys) 116 continue; 117 e = &km->key[code]; 118 for (l = 0; l < 4; l++) 119 put(e, states[l], l < key->nsym ? key->sym[l] : (l & 1 ? key->sym[l - 1] : "none")); 120 e->flgs = key->caps ? FLAG_LOCK_C : 0; 121 // control follows the letter the key types, so ctrl+a is ^A on any latin 122 // layout. on another script it stays where the console had it 123 c0 = key->nsym ? kmap_unicode(key->sym[0]) : -1; 124 if (c0 >= 'a' && c0 <= 'z') { 125 e->map[S_CTRL] = e->map[S_CTRL_SHIFT] = (u_int)(c0 & 0x1f); 126 e->spcl &= ~((0x80 >> S_CTRL) | (0x80 >> S_CTRL_SHIFT)); 127 } 128 } 129 if (k->level3_ralt && KC_RALT < km->n_keys) { 130 for (l = 0; l < NUM_STATES; l++) 131 km->key[KC_RALT].map[l] = RALT; 132 km->key[KC_RALT].spcl = 0xff; 133 km->key[KC_RALT].flgs = 0; 134 } 135 136 // the accent map: for each dead key of the layout, what it makes 137 memset(am, 0, sizeof(*am)); 138 am->n_accs = NUM_DEADKEYS; 139 for (j = 0; j < k->ncompose; j++) { 140 const struct kmap_compose *cp = &k->compose[j]; 141 long base = kmap_unicode(cp->base), res = kmap_unicode(cp->result); 142 int a = acc_of(cp->dead), n; 143 struct acc_t *acc; 144 145 if (a < 0 || base < 0 || res < 0) 146 continue; 147 acc = &am->acc[accs[a].key - F_ACC]; 148 acc->accchar = accs[a].spacing; 149 for (n = 0; n < NUM_ACCENTCHARS && acc->map[n][0]; n++) 150 ; 151 if (n < NUM_ACCENTCHARS) { 152 acc->map[n][0] = (u_int)base; 153 acc->map[n][1] = (u_int)res; 154 } 155 } 156 157 rc = 0; 158 if (ioctl(fd, PIO_KEYMAP, km) != 0) { 159 weprintf("PIO_KEYMAP:"); 160 rc = -1; 161 } 162 if (ioctl(fd, PIO_DEADKEYMAP, am) != 0) { 163 weprintf("PIO_DEADKEYMAP:"); 164 rc = -1; 165 } 166 if (fd > 0) 167 close(fd); 168 done: 169 free(km); 170 free(am); 171 return rc; 172 } 173 #endif