Home | History | Annotate | Download | only in ui
      1 /*
      2  * QEMU keysym to keycode conversion using rdesktop keymaps
      3  *
      4  * Copyright (c) 2004 Johannes Schindelin
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a copy
      7  * of this software and associated documentation files (the "Software"), to deal
      8  * in the Software without restriction, including without limitation the rights
      9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     10  * copies of the Software, and to permit persons to whom the Software is
     11  * furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     22  * THE SOFTWARE.
     23  */
     24 
     25 #include "keymaps.h"
     26 #include "sysemu/sysemu.h"
     27 
     28 static int get_keysym(const name2keysym_t *table,
     29 		      const char *name)
     30 {
     31     const name2keysym_t *p;
     32     for(p = table; p->name != NULL; p++) {
     33         if (!strcmp(p->name, name))
     34             return p->keysym;
     35     }
     36     return 0;
     37 }
     38 
     39 
     40 static void add_to_key_range(struct key_range **krp, int code) {
     41     struct key_range *kr;
     42     for (kr = *krp; kr; kr = kr->next) {
     43 	if (code >= kr->start && code <= kr->end)
     44 	    break;
     45 	if (code == kr->start - 1) {
     46 	    kr->start--;
     47 	    break;
     48 	}
     49 	if (code == kr->end + 1) {
     50 	    kr->end++;
     51 	    break;
     52 	}
     53     }
     54     if (kr == NULL) {
     55 	kr = g_malloc0(sizeof(*kr));
     56         kr->start = kr->end = code;
     57         kr->next = *krp;
     58         *krp = kr;
     59     }
     60 }
     61 
     62 static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
     63 					   const char *language,
     64 					   kbd_layout_t * k)
     65 {
     66     FILE *f;
     67     /* This file is used by both, UI and core components. There are differences
     68      * in the way how keymap file path is obtained for these two different
     69      * configurations. */
     70     char * filename;
     71     char line[1024];
     72     int len;
     73 
     74     filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
     75     if (!filename) {
     76         fprintf(stderr,
     77             "Could not read keymap file: '%s'\n", language);
     78         return NULL;
     79     }
     80 
     81     if (!k)
     82 	k = g_malloc0(sizeof(kbd_layout_t));
     83     if (!(f = fopen(filename, "r"))) {
     84 	fprintf(stderr,
     85 		"Could not read keymap file: '%s'\n", language);
     86 	return NULL;
     87     }
     88     for(;;) {
     89 	if (fgets(line, 1024, f) == NULL)
     90             break;
     91         len = strlen(line);
     92         if (len > 0 && line[len - 1] == '\n')
     93             line[len - 1] = '\0';
     94         if (line[0] == '#')
     95 	    continue;
     96 	if (!strncmp(line, "map ", 4))
     97 	    continue;
     98 	if (!strncmp(line, "include ", 8)) {
     99 	    parse_keyboard_layout(table, line + 8, k);
    100         } else {
    101 	    char *end_of_keysym = line;
    102 	    while (*end_of_keysym != 0 && *end_of_keysym != ' ')
    103 		end_of_keysym++;
    104 	    if (*end_of_keysym) {
    105 		int keysym;
    106 		*end_of_keysym = 0;
    107 		keysym = get_keysym(table, line);
    108 		if (keysym == 0) {
    109                     //		    fprintf(stderr, "Warning: unknown keysym %s\n", line);
    110 		} else {
    111 		    const char *rest = end_of_keysym + 1;
    112 		    char *rest2;
    113 		    int keycode = strtol(rest, &rest2, 0);
    114 
    115 		    if (rest && strstr(rest, "numlock")) {
    116 			add_to_key_range(&k->keypad_range, keycode);
    117 			add_to_key_range(&k->numlock_range, keysym);
    118 			//fprintf(stderr, "keypad keysym %04x keycode %d\n", keysym, keycode);
    119 		    }
    120 
    121 		    /* if(keycode&0x80)
    122 		       keycode=(keycode<<8)^0x80e0; */
    123 		    if (keysym < MAX_NORMAL_KEYCODE) {
    124 			//fprintf(stderr,"Setting keysym %s (%d) to %d\n",line,keysym,keycode);
    125 			k->keysym2keycode[keysym] = keycode;
    126 		    } else {
    127 			if (k->extra_count >= MAX_EXTRA_COUNT) {
    128 			    fprintf(stderr,
    129 				    "Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n",
    130 				    line, keysym);
    131 			} else {
    132 #if 0
    133 			    fprintf(stderr, "Setting %d: %d,%d\n",
    134 				    k->extra_count, keysym, keycode);
    135 #endif
    136 			    k->keysym2keycode_extra[k->extra_count].
    137 				keysym = keysym;
    138 			    k->keysym2keycode_extra[k->extra_count].
    139 				keycode = keycode;
    140 			    k->extra_count++;
    141 			}
    142 		    }
    143 		}
    144 	    }
    145 	}
    146     }
    147     fclose(f);
    148     return k;
    149 }
    150 
    151 
    152 void *init_keyboard_layout(const name2keysym_t *table, const char *language)
    153 {
    154     return parse_keyboard_layout(table, language, NULL);
    155 }
    156 
    157 
    158 int keysym2scancode(void *kbd_layout, int keysym)
    159 {
    160     kbd_layout_t *k = kbd_layout;
    161     if (keysym < MAX_NORMAL_KEYCODE) {
    162 	if (k->keysym2keycode[keysym] == 0)
    163 	    fprintf(stderr, "Warning: no scancode found for keysym %d\n",
    164 		    keysym);
    165 	return k->keysym2keycode[keysym];
    166     } else {
    167 	int i;
    168 #ifdef XK_ISO_Left_Tab
    169 	if (keysym == XK_ISO_Left_Tab)
    170 	    keysym = XK_Tab;
    171 #endif
    172 	for (i = 0; i < k->extra_count; i++)
    173 	    if (k->keysym2keycode_extra[i].keysym == keysym)
    174 		return k->keysym2keycode_extra[i].keycode;
    175     }
    176     return 0;
    177 }
    178 
    179 int keycode_is_keypad(void *kbd_layout, int keycode)
    180 {
    181     kbd_layout_t *k = kbd_layout;
    182     struct key_range *kr;
    183 
    184     for (kr = k->keypad_range; kr; kr = kr->next)
    185         if (keycode >= kr->start && keycode <= kr->end)
    186             return 1;
    187     return 0;
    188 }
    189 
    190 int keysym_is_numlock(void *kbd_layout, int keysym)
    191 {
    192     kbd_layout_t *k = kbd_layout;
    193     struct key_range *kr;
    194 
    195     for (kr = k->numlock_range; kr; kr = kr->next)
    196         if (keysym >= kr->start && keysym <= kr->end)
    197             return 1;
    198     return 0;
    199 }
    200