1 #include <linux/keyboard.h>
10 #include "syms.cp1250.h"
11 #include "syms.ethiopic.h"
12 #include "syms.iso8859_15.h"
13 #include "syms.iso8859_5.h"
14 #include "syms.iso8859_7.h"
15 #include "syms.iso8859_8.h"
16 #include "syms.iso8859_9.h"
17 #include "syms.koi8.h"
18 #include "syms.latin1.h"
19 #include "syms.latin2.h"
20 #include "syms.latin3.h"
21 #include "syms.latin4.h"
22 #include "syms.mazovia.h"
23 #include "syms.sami.h"
24 #include "syms.thai.h"
26 #include "syms.synonyms.h"
28 #include "syms.ktyp.h"
30 #define E(x) { x, sizeof(x) / sizeof(x[0]) }
34 E(iso646_syms), /* KT_LATIN */
35 E(fn_syms), /* KT_FN */
36 E(spec_syms), /* KT_SPEC */
37 E(pad_syms), /* KT_PAD */
38 E(dead_syms), /* KT_DEAD */
39 E(cons_syms), /* KT_CONS */
40 E(cur_syms), /* KT_CUR */
41 E(shift_syms), /* KT_SHIFT */
42 { 0, 0 }, /* KT_META */
43 E(ascii_syms), /* KT_ASCII */
44 E(lock_syms), /* KT_LOCK */
45 { 0, 0 }, /* KT_LETTER */
46 E(sticky_syms), /* KT_SLOCK */
48 E(brl_syms) /* KT_BRL */
53 const unsigned int syms_size = sizeof(syms) / sizeof(syms_entry);
54 const unsigned int syn_size = sizeof(synonyms) / sizeof(synonyms[0]);
62 { "iso-8859-1", latin1_syms, 160 },
63 { "iso-8859-2", latin2_syms, 160 },
64 { "iso-8859-3", latin3_syms, 160 },
65 { "iso-8859-4", latin4_syms, 160 },
66 { "iso-8859-5", iso_8859_5_syms, 160 },
67 { "iso-8859-7", iso_8859_7_syms, 160 },
68 { "iso-8859-8", iso_8859_8_syms, 160 },
69 { "iso-8859-9", iso_8859_9_syms, 208 },
70 { "iso-8859-10", latin6_syms, 160 },
71 { "iso-8859-15", iso_8859_15_syms, 160 },
72 { "mazovia", mazovia_syms, 128 },
73 { "cp-1250", cp1250_syms, 128 },
74 { "koi8-r", koi8_syms, 128 },
75 { "koi8-u", koi8_syms, 128 },
76 { "tis-620", tis_620_syms, 160 }, /* thai */
77 { "iso-10646-18", iso_10646_18_syms, 159 }, /* ethiopic */
78 { "iso-ir-197", iso_ir_197_syms, 160 }, /* sami */
79 { "iso-ir-209", iso_ir_209_syms, 160 }, /* sami */
82 static unsigned int charsets_size = sizeof(charsets) / sizeof(charsets[0]);
83 static unsigned int kmap_charset = 0;
85 /* Functions for both dumpkeys and loadkeys. */
88 list_charsets(FILE *f) {
91 char *mm[] = { "iso-8859-", "koi8-" };
93 for (j=0; j<sizeof(mm)/sizeof(mm[0]); j++) {
96 fprintf(f, "%s{", mm[j]);
99 for(i=1; i < charsets_size; i++) {
100 if(!strncmp(charsets[i].charset, mm[j], lth)) {
103 fprintf(f, "%s", charsets[i].charset+lth);
108 for(i=1; i < charsets_size; i++) {
109 for (j=0; j<sizeof(mm)/sizeof(mm[0]); j++) {
111 if(!strncmp(charsets[i].charset, mm[j], lth))
114 fprintf(f, ",%s", charsets[i].charset);
121 set_charset(const char *charset) {
124 for (i = 1; i < charsets_size; i++) {
125 if (!strcasecmp(charsets[i].charset, charset)) {
134 codetoksym(int code) {
142 if (code < 0x1000) { /* "traditional" keysym */
144 return iso646_syms[code];
146 if (KTYP(code) == KT_META)
149 if (KTYP(code) == KT_LETTER)
150 code = K(KT_LATIN, KVAL(code));
152 if (KTYP(code) > KT_LATIN)
153 return syms[KTYP(code)].table[KVAL(code)];
157 p = charsets[i].charnames;
159 p += KVAL(code) - charsets[i].start;
167 if (i == charsets_size)
169 if (i == kmap_charset)
174 else { /* Unicode keysym */
178 return iso646_syms[code];
182 p = charsets[i].charnames;
184 for (j = charsets[i].start; j < 256; j++, p++) {
185 if (p->uni == code && p->name[0])
192 if (i == charsets_size)
194 if (i == kmap_charset)
203 /* Functions for loadkeys. */
206 kt_latin(struct keymap *kmap, const char *s, int direction) {
210 sym *p = charsets[kmap_charset].charnames;
212 for (i = charsets[kmap_charset].start; i < 256; i++, p++) {
213 if(p->name[0] && !strcmp(s, p->name))
214 return K(KT_LATIN, i);
218 max = (direction == TO_UNICODE ? 128 : syms[KT_LATIN].size);
220 for (i = 0; i < max; i++) {
221 if (!strcmp(s, syms[KT_LATIN].table[i]))
222 return K(KT_LATIN, i);
229 ksymtocode(struct keymap *kmap, const char *s, int direction) {
235 if (direction == TO_AUTO)
236 direction = kmap->prefer_unicode ? TO_UNICODE : TO_8BIT;
238 if (!strncmp(s, "Meta_", 5)) {
239 keycode = ksymtocode(kmap, s+5, TO_8BIT);
240 if (KTYP(keycode) == KT_LATIN)
241 return K(KT_META, KVAL(keycode));
243 /* Avoid error messages for Meta_acute with UTF-8 */
244 else if(direction == TO_UNICODE)
247 /* fall through to error printf */
250 if ((n = kt_latin(kmap, s, direction)) >= 0) {
254 for (i = 1; i < syms_size; i++) {
255 for (j = 0; j < syms[i].size; j++) {
256 if (!strcmp(s,syms[i].table[j]))
261 for (i = 0; i < syn_size; i++)
262 if (!strcmp(s, synonyms[i].synonym))
263 return ksymtocode(kmap, synonyms[i].official_name, direction);
265 if (direction == TO_UNICODE) {
269 p = charsets[i].charnames;
271 for (j = charsets[i].start; j < 256; j++, p++) {
272 if (!strcmp(s,p->name))
273 return (p->uni ^ 0xf000);
279 if (i == charsets_size)
281 if (i == kmap_charset)
284 } else /* if (!chosen_charset[0]) */ {
285 /* note: some keymaps use latin1 but with euro,
286 so set_charset() would fail */
287 /* note: some keymaps with charset line still use
288 symbols from more than one character set,
289 so we cannot have the `if (!chosen_charset[0])' here */
291 for (i = 0; i < 256 - 160; i++)
292 if (!strcmp(s, latin1_syms[i].name)) {
293 log_verbose(kmap, LOG_VERBOSE1,
294 _("assuming iso-8859-1 %s\n"), s);
295 return K(KT_LATIN, 160 + i);
298 for (i = 0; i < 256 - 160; i++)
299 if (!strcmp(s, iso_8859_15_syms[i].name)) {
300 log_verbose(kmap, LOG_VERBOSE1,
301 _("assuming iso-8859-15 %s\n"), s);
302 return K(KT_LATIN, 160 + i);
305 for (i = 0; i < 256 - 160; i++)
306 if (!strcmp(s, latin2_syms[i].name)) {
307 log_verbose(kmap, LOG_VERBOSE1,
308 _("assuming iso-8859-2 %s\n"), s);
309 return K(KT_LATIN, 160 + i);
312 for (i = 0; i < 256 - 160; i++)
313 if (!strcmp(s, latin3_syms[i].name)) {
314 log_verbose(kmap, LOG_VERBOSE1,
315 _("assuming iso-8859-3 %s\n"), s);
316 return K(KT_LATIN, 160 + i);
319 for (i = 0; i < 256 - 160; i++)
320 if (!strcmp(s, latin4_syms[i].name)) {
321 log_verbose(kmap, LOG_VERBOSE1,
322 _("assuming iso-8859-4 %s\n"), s);
323 return K(KT_LATIN, 160 + i);
327 log_error(kmap, _("unknown keysym '%s'\n"), s);
329 return CODE_FOR_UNKNOWN_KSYM;
333 convert_code(struct keymap *kmap, int code, int direction)
336 int unicode_forced = (direction == TO_UNICODE);
337 int input_is_unicode = (code >= 0x1000);
340 if (direction == TO_AUTO)
341 direction = kmap->prefer_unicode ? TO_UNICODE : TO_8BIT;
343 if (KTYP(code) == KT_META)
345 else if (!input_is_unicode && code < 0x80)
346 /* basic ASCII is fine in every situation */
348 else if (input_is_unicode && (code ^ 0xf000) < 0x80)
349 /* so is Unicode "Basic Latin" */
350 return code ^ 0xf000;
351 else if ((input_is_unicode && direction == TO_UNICODE) ||
352 (!input_is_unicode && direction == TO_8BIT))
353 /* no conversion necessary */
356 /* depending on direction, this will give us either an 8-bit
357 * K(KTYP, KVAL) or a Unicode keysym xor 0xf000 */
358 ksym = codetoksym(code);
360 result = ksymtocode(kmap, ksym, direction);
365 /* if direction was TO_UNICODE from the beginning, we return the true
366 * Unicode value (without the 0xf000 mask) */
367 if (unicode_forced && result >= 0x1000)
368 return result ^ 0xf000;
374 add_capslock(struct keymap *kmap, int code)
376 if (KTYP(code) == KT_LATIN && (!(kmap->prefer_unicode) || code < 0x80))
377 return K(KT_LETTER, KVAL(code));
378 else if ((code ^ 0xf000) < 0x100)
379 /* Unicode Latin-1 Supplement */
380 /* a bit dirty to use KT_LETTER here, but it should work */
381 return K(KT_LETTER, code ^ 0xf000);
383 return convert_code(kmap, code, TO_AUTO);