kmap.c (12890B)
1 /* see LICENSE file for copyright and license details */ 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 #include "../kmap.h" 7 #include "../util.h" 8 9 void 10 kmap_path(char *buf, size_t sz, const char *dir, const char *layout, const char *variant) 11 { 12 snprintf( 13 buf, sz, "%s/%s/%s.kmap", dir ? dir : KMAP_DIR, layout, variant && *variant ? variant : layout 14 ); 15 } 16 17 int 18 kmap_words(char *line, char **w, int max) 19 { 20 char *r = line, *o = line; 21 int n = 0; 22 23 while (*r) { 24 while (*r == ' ' || *r == '\t' || *r == '\r' || *r == '\n') 25 r++; 26 if (!*r || *r == '#') 27 break; 28 if (n == max) 29 break; 30 w[n++] = o; 31 while (*r && *r != ' ' && *r != '\t' && *r != '\r' && *r != '\n') { 32 if (*r != '\'') { 33 *o++ = *r++; 34 continue; 35 } 36 // a quoted run: '' inside it is one quote 37 for (r++; *r; r++) { 38 if (*r == '\'' && r[1] == '\'') { 39 *o++ = '\''; 40 r++; 41 } else if (*r == '\'') { 42 r++; 43 break; 44 } else 45 *o++ = *r; 46 } 47 } 48 if (*r) 49 r++; 50 *o++ = '\0'; 51 } 52 return n; 53 } 54 55 long 56 kmap_char(const char *s) 57 { 58 const unsigned char *u = (const unsigned char *)s; 59 long c; 60 int n, i; 61 62 if (!u[0]) 63 return -1; 64 if (u[0] < 0x80) 65 return u[1] ? -1 : u[0]; 66 if ((u[0] & 0xe0) == 0xc0) 67 n = 1, c = u[0] & 0x1f; 68 else if ((u[0] & 0xf0) == 0xe0) 69 n = 2, c = u[0] & 0x0f; 70 else if ((u[0] & 0xf8) == 0xf0) 71 n = 3, c = u[0] & 0x07; 72 else 73 return -1; 74 for (i = 1; i <= n; i++) { 75 if ((u[i] & 0xc0) != 0x80) 76 return -1; 77 c = c << 6 | (u[i] & 0x3f); 78 } 79 return u[n + 1] ? -1 : c; 80 } 81 82 static void 83 copy(char *dst, size_t sz, const char *src) 84 { 85 snprintf(dst, sz, "%s", src); 86 } 87 88 // the rest of the words joined by a space, a text such as a name 89 static void 90 join(char *dst, size_t sz, char **w, int n) 91 { 92 int i; 93 94 dst[0] = '\0'; 95 for (i = 0; i < n; i++) { 96 if (i) 97 strlcat(dst, " ", sz); 98 strlcat(dst, w[i], sz); 99 } 100 } 101 102 int 103 kmap_read(const char *path, struct kmap *k) 104 { 105 char line[1024], *w[24]; 106 FILE *fp; 107 int n, lineno = 0; 108 109 if (!(fp = fopen(path, "r"))) { 110 weprintf("open %s:", path); 111 return -1; 112 } 113 while (fgets(line, sizeof(line), fp)) { 114 lineno++; 115 if (!(n = kmap_words(line, w, 24))) 116 continue; 117 // a header field keeps its first value: compose.kmap read after a 118 // layout does not rename it 119 if (!strcmp(w[0], "id")) { 120 if (n > 1 && !k->id[0]) 121 copy(k->id, sizeof(k->id), w[1]); 122 } else if (!strcmp(w[0], "layout")) { 123 if (n > 1 && !k->layout[0]) 124 copy(k->layout, sizeof(k->layout), w[1]); 125 } else if (!strcmp(w[0], "variant")) { 126 if (n > 1 && !k->variant[0]) 127 copy(k->variant, sizeof(k->variant), w[1]); 128 } else if (!strcmp(w[0], "name")) { 129 if (!k->name[0]) 130 join(k->name, sizeof(k->name), w + 1, n - 1); 131 } else if (!strcmp(w[0], "level3") && n > 1) 132 k->level3_ralt = !strcmp(w[1], "ralt"); 133 else if (!strcmp(w[0], "key") && n > 2) { 134 struct kmap_key *key; 135 int i; 136 137 k->keys = ereallocarray(k->keys, k->nkeys + 1, sizeof(*k->keys)); 138 key = &k->keys[k->nkeys++]; 139 memset(key, 0, sizeof(*key)); 140 copy(key->name, sizeof(key->name), w[1]); 141 for (i = 2; i < n; i++) { 142 if (!strcmp(w[i], "caps")) 143 key->caps = 1; 144 else if (!strncmp(w[i], "type=", 5)) 145 copy(key->type, sizeof(key->type), w[i] + 5); 146 else if (key->nsym < KMAP_LEVELS) 147 copy(key->sym[key->nsym++], KMAP_SYM, w[i]); 148 } 149 } else if (!strcmp(w[0], "modmap") && n > 2 && k->nmodmaps < 8) { 150 struct kmap_modmap *m = &k->modmaps[k->nmodmaps++]; 151 int i; 152 153 copy(m->mod, sizeof(m->mod), w[1]); 154 for (i = 2, m->n = 0; i < n && m->n < 16; i++) 155 copy(m->sym[m->n++], KMAP_SYM, w[i]); 156 } else if (!strcmp(w[0], "compose") && n == 4) { 157 struct kmap_compose *c; 158 159 k->compose = ereallocarray(k->compose, k->ncompose + 1, sizeof(*k->compose)); 160 c = &k->compose[k->ncompose++]; 161 copy(c->dead, sizeof(c->dead), w[1]); 162 copy(c->base, sizeof(c->base), w[2]); 163 copy(c->result, sizeof(c->result), w[3]); 164 } else if (strcmp(w[0], "kmap")) 165 weprintf("%s:%d: %s is not a kmap line, skipped\n", path, lineno, w[0]); 166 } 167 fclose(fp); 168 return 0; 169 } 170 171 void 172 kmap_free(struct kmap *k) 173 { 174 free(k->keys); 175 free(k->compose); 176 memset(k, 0, sizeof(*k)); 177 } 178 179 // a symbol as xkb writes it: latin-1 by its code, which is its keysym, other 180 // characters as Uxxxx, names as they are 181 static void 182 xkb_sym(FILE *fp, const char *s) 183 { 184 long c = kmap_char(s); 185 186 if (!strcmp(s, "none")) 187 fputs("NoSymbol", fp); 188 else if (c < 0) 189 fputs(s, fp); 190 else if (c < 0x100) 191 fprintf(fp, "0x%02lx", c); 192 else 193 fprintf(fp, "U%04lX", c); 194 } 195 196 // a file of dir's xkb/ written out as it is 197 static int 198 put_file(FILE *fp, const char *dir, const char *name) 199 { 200 char path[4096], buf[8192]; 201 size_t n; 202 FILE *in; 203 204 snprintf(path, sizeof(path), "%s/xkb/%s", dir, name); 205 if (!(in = fopen(path, "r"))) { 206 weprintf("open %s:", path); 207 return -1; 208 } 209 while ((n = fread(buf, 1, sizeof(buf), in)) > 0) 210 fwrite(buf, 1, n, fp); 211 fclose(in); 212 return 0; 213 } 214 215 static const struct kmap_key * 216 find_key(const struct kmap *k, const char *name) 217 { 218 size_t i; 219 220 for (i = 0; i < k->nkeys; i++) 221 if (!strcmp(k->keys[i].name, name)) 222 return &k->keys[i]; 223 return NULL; 224 } 225 226 static void 227 put_group(FILE *fp, int g, const struct kmap_key *key, int *first) 228 { 229 int l; 230 231 if (key->type[0]) 232 fprintf(fp, "%stype[Group%d] = \"%s\"", *first ? "" : ", ", g + 1, key->type); 233 fprintf(fp, "%ssymbols[Group%d] = [ ", *first && !key->type[0] ? "" : ", ", g + 1); 234 for (l = 0; l < key->nsym; l++) { 235 if (l) 236 fputs(", ", fp); 237 xkb_sym(fp, key->sym[l]); 238 } 239 fputs(" ]", fp); 240 *first = 0; 241 } 242 243 // what a key is in group g: the last option that names it, the layout of g, 244 // right alt as level 3 where the layout reaches it so, the pc keyboard 245 static const struct kmap_key * 246 key_in( 247 const char *name, 248 int g, 249 const struct kmap *pc, 250 const struct kmap *maps, 251 const struct kmap *opts, 252 int nopts 253 ) 254 { 255 static const struct kmap_key lv3 = {"RALT", {"ISO_Level3_Shift"}, 1, 0, "ONE_LEVEL"}; 256 const struct kmap_key *key; 257 int o; 258 259 for (o = nopts - 1; o >= 0; o--) 260 if ((key = find_key(&opts[o], name))) 261 return key; 262 if ((key = find_key(&maps[g], name))) 263 return key; 264 if (maps[g].level3_ralt && !strcmp(name, "RALT")) 265 return &lv3; 266 return find_key(pc, name); 267 } 268 269 int 270 kmap_write_xkb( 271 FILE *fp, 272 const char *dir, 273 const struct kmap *pc, 274 const struct kmap *maps, 275 int n, 276 const struct kmap *opts, 277 int nopts 278 ) 279 { 280 const char **names = NULL; 281 size_t i, j, nnames = 0; 282 int g, m, s; 283 284 fputs("xkb_keymap {\n", fp); 285 if (put_file(fp, dir, "keycodes.xkb") || put_file(fp, dir, "types.xkb") 286 || put_file(fp, dir, "compat.xkb")) 287 return -1; 288 fputs("xkb_symbols \"kmap\" {\n", fp); 289 for (g = 0; g < n; g++) { 290 fprintf(fp, "\tname[Group%d] = \"", g + 1); 291 for (i = 0; maps[g].name[i]; i++) 292 if (maps[g].name[i] != '"' && maps[g].name[i] != '\\') 293 fputc(maps[g].name[i], fp); 294 fputs("\";\n", fp); 295 } 296 // every key the pc keyboard, a layout or an option names, once 297 #define ADD(km) \ 298 for (i = 0; i < (km)->nkeys; i++) { \ 299 for (j = 0; j < nnames && strcmp(names[j], (km)->keys[i].name); j++) \ 300 ; \ 301 if (j == nnames) { \ 302 names = ereallocarray(names, nnames + 1, sizeof(*names)); \ 303 names[nnames++] = (km)->keys[i].name; \ 304 } \ 305 } 306 ADD(pc); 307 for (g = 0; g < n; g++) 308 ADD(&maps[g]); 309 for (g = 0; g < nopts; g++) 310 ADD(&opts[g]); 311 #undef ADD 312 for (j = 0; j < nnames; j++) { 313 int first = 1, last = -1; 314 315 // the groups up to the last that has the key, none skipped 316 for (g = 0; g < n; g++) 317 if (key_in(names[j], g, pc, maps, opts, nopts)) 318 last = g; 319 if (last < 0) 320 continue; 321 fprintf(fp, "\tkey <%s> { ", names[j]); 322 for (g = 0; g <= last; g++) { 323 const struct kmap_key *key = key_in(names[j], g, pc, maps, opts, nopts); 324 static const struct kmap_key none = {"", {"NoSymbol"}, 1, 0, ""}; 325 326 put_group(fp, g, key ? key : &none, &first); 327 } 328 fputs(" };\n", fp); 329 } 330 free(names); 331 // a modifier follows the keysyms that drive it, wherever a layout put them 332 for (m = 0; m < pc->nmodmaps; m++) { 333 fprintf(fp, "\tmodifier_map %s { ", pc->modmaps[m].mod); 334 for (s = 0; s < pc->modmaps[m].n; s++) 335 fprintf(fp, "%s%s", s ? ", " : "", pc->modmaps[m].sym[s]); 336 fputs(" };\n", fp); 337 } 338 fputs("};\n};\n", fp); 339 return ferror(fp) ? -1 : 0; 340 } 341 342 // the keys a layout places and where they are: evdev codes, which are the 343 // pc scancodes of set 1 but for the two japanese keys, and usb hid usages 344 static const struct { 345 const char *name; 346 int evdev, pc, hid; 347 } codes[] = { 348 {"TLDE", 41, 41, 53}, {"AE01", 2, 2, 30}, {"AE02", 3, 3, 31}, {"AE03", 4, 4, 32}, 349 {"AE04", 5, 5, 33}, {"AE05", 6, 6, 34}, {"AE06", 7, 7, 35}, {"AE07", 8, 8, 36}, 350 {"AE08", 9, 9, 37}, {"AE09", 10, 10, 38}, {"AE10", 11, 11, 39}, {"AE11", 12, 12, 45}, 351 {"AE12", 13, 13, 46}, {"AE13", 124, 125, 137}, {"AD01", 16, 16, 20}, {"AD02", 17, 17, 26}, 352 {"AD03", 18, 18, 8}, {"AD04", 19, 19, 21}, {"AD05", 20, 20, 23}, {"AD06", 21, 21, 28}, 353 {"AD07", 22, 22, 24}, {"AD08", 23, 23, 12}, {"AD09", 24, 24, 18}, {"AD10", 25, 25, 19}, 354 {"AD11", 26, 26, 47}, {"AD12", 27, 27, 48}, {"BKSL", 43, 43, 49}, {"AC01", 30, 30, 4}, 355 {"AC02", 31, 31, 22}, {"AC03", 32, 32, 7}, {"AC04", 33, 33, 9}, {"AC05", 34, 34, 10}, 356 {"AC06", 35, 35, 11}, {"AC07", 36, 36, 13}, {"AC08", 37, 37, 14}, {"AC09", 38, 38, 15}, 357 {"AC10", 39, 39, 51}, {"AC11", 40, 40, 52}, {"LSGT", 86, 86, 100}, {"AB01", 44, 44, 29}, 358 {"AB02", 45, 45, 27}, {"AB03", 46, 46, 6}, {"AB04", 47, 47, 25}, {"AB05", 48, 48, 5}, 359 {"AB06", 49, 49, 17}, {"AB07", 50, 50, 16}, {"AB08", 51, 51, 54}, {"AB09", 52, 52, 55}, 360 {"AB10", 53, 53, 56}, {"AB11", 89, 115, 135}, {"SPCE", 57, 57, 44}, {"KPDL", 83, 83, 99}, 361 }; 362 363 int 364 kmap_code(const char *name, enum kmap_codes scheme) 365 { 366 size_t i; 367 368 for (i = 0; i < sizeof(codes) / sizeof(codes[0]); i++) 369 if (!strcmp(codes[i].name, name)) 370 return scheme == KMAP_PC ? codes[i].pc : scheme == KMAP_HID ? codes[i].hid : codes[i].evdev; 371 return -1; 372 } 373 374 // characters a layout writes by name 375 static const struct { 376 const char *name; 377 long c; 378 } named[] = { 379 {"space", ' '}, 380 {"nobreakspace", 0xa0}, 381 {"combining_grave", 0x300}, 382 {"combining_acute", 0x301}, 383 {"combining_tilde", 0x303}, 384 {"combining_hook", 0x309}, 385 {"combining_belowdot", 0x323}, 386 }; 387 388 long 389 kmap_unicode(const char *s) 390 { 391 long c = kmap_char(s); 392 size_t i; 393 char *end; 394 395 if (c >= 0) 396 return c; 397 for (i = 0; i < sizeof(named) / sizeof(named[0]); i++) 398 if (!strcmp(named[i].name, s)) 399 return named[i].c; 400 if (s[0] == 'U' && s[1]) { 401 c = strtol(s + 1, &end, 16); 402 if (!*end && c > 0) 403 return c; 404 } 405 return -1; 406 } 407 408 // the dead keys and the combining characters they stand for 409 static const struct { 410 const char *name; 411 long c; 412 } deads[] = { 413 {"dead_grave", 0x300}, {"dead_acute", 0x301}, 414 {"dead_circumflex", 0x302}, {"dead_tilde", 0x303}, 415 {"dead_macron", 0x304}, {"dead_breve", 0x306}, 416 {"dead_abovedot", 0x307}, {"dead_diaeresis", 0x308}, 417 {"dead_hook", 0x309}, {"dead_abovering", 0x30a}, 418 {"dead_doubleacute", 0x30b}, {"dead_caron", 0x30c}, 419 {"dead_doublegrave", 0x30f}, {"dead_invertedbreve", 0x311}, 420 {"dead_abovecomma", 0x313}, {"dead_abovereversedcomma", 0x314}, 421 {"dead_horn", 0x31b}, {"dead_belowdot", 0x323}, 422 {"dead_belowcomma", 0x326}, {"dead_cedilla", 0x327}, 423 {"dead_ogonek", 0x328}, {"dead_belowmacron", 0x331}, 424 {"dead_stroke", 0x335}, {"dead_iota", 0x345}, 425 }; 426 427 long 428 kmap_dead(const char *s) 429 { 430 size_t i; 431 432 for (i = 0; i < sizeof(deads) / sizeof(deads[0]); i++) 433 if (!strcmp(deads[i].name, s)) 434 return deads[i].c; 435 return -1; 436 }