direct-io.hg

view tools/ioemu/keyboard_rdesktop.c @ 9673:123ff1c70728

Fix swapped calloc() arguments.
Signed-off-by: Hollis Blanchard <hollisb@us.ibm.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Apr 19 18:39:36 2006 +0100 (2006-04-19)
parents 8e5fc5fe636c
children
line source
1 /*
2 * QEMU keylayout reader: read rdesktop style keylaouts
3 *
4 * Copyright (c) 2004,2005 Johannes E. 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 */
25 #include <ctype.h>
27 #ifdef KEYBOARD_IGNORE_CASE
28 #define STRCMP strcasecmp
29 #else
30 #define STRCMP strcmp
31 #endif
33 /* binary search through nameToKeysym */
34 static int get_keysym(const char* name)
35 {
36 int i1=-1,i2=sizeof(name2keysym)/sizeof(name2keysym_t),i3=i2/2,r;
37 while((r=STRCMP(name,name2keysym[i3].name))!=0) {
38 if(r<0)
39 i2=i3;
40 else
41 i1=i3;
42 i3=(i1+i2)/2;
43 if(i2-i1<2)
44 return 0;
45 }
46 return name2keysym[i3].keysym;
47 }
49 typedef unsigned short WORD;
50 #define MAX_NORMAL_KEYCODE 512
51 #define MAX_EXTRA_COUNT 256
52 typedef struct {
53 WORD keysym2keycode[MAX_NORMAL_KEYCODE];
54 struct {
55 int keysym;
56 WORD keycode;
57 } keysym2keycode_extra[MAX_EXTRA_COUNT];
58 int extra_count;
59 } kbd_layout_t;
61 static int parse_int(const char* text)
62 {
63 if(!strncmp(text,"0x",2)) {
64 int result=0;
65 sscanf(text+2,"%x",&result);
66 return result;
67 }
68 return atoi(text);
69 }
71 static kbd_layout_t* parse_keyboard_layout(const char* language,kbd_layout_t* k)
72 {
73 FILE* f;
74 const char* prefix="/keymaps/";
75 char* file_name=malloc(strlen(prefix)+strlen(language)+strlen(bios_dir)+1);
77 if(!k)
78 k=calloc(1, sizeof(kbd_layout_t));
79 strcpy(file_name,bios_dir);
80 strcat(file_name,prefix);
81 strcat(file_name,language);
82 if(file_name[strlen(file_name)-1]=='\n')
83 file_name[strlen(file_name)-1]=0;
84 if(!(f=fopen(file_name,"r"))) {
85 term_printf("Warning: could not read keymap - falling back native keycodes!\n");
86 free(file_name);
87 return 0;
88 }
89 free(file_name);
90 while(!feof(f)) {
91 char line[1024];
92 fgets(line,1024,f);
93 if(line[0]=='#')
94 continue;
95 if(!strncmp(line,"map ",4))
96 continue;
97 if(!strncmp(line,"include ",8))
98 parse_keyboard_layout(line+8,k);
99 else {
100 char* end_of_keysym=line;
101 while(*end_of_keysym!=0 && *end_of_keysym!=' ')
102 end_of_keysym++;
103 if(*end_of_keysym) {
104 int keysym;
105 *end_of_keysym=0;
106 keysym=get_keysym(line);
107 if(keysym==0) {
108 term_printf("Warning: 1unknown keysym %s\n",line);
109 } else {
110 const char* rest=end_of_keysym+1;
111 int keycode=parse_int(rest);
112 /* if(keycode&0x80)
113 keycode=(keycode<<8)^0x80e0; */
114 if(keysym<MAX_NORMAL_KEYCODE) {
115 //term_printf("Setting keysym %s (%d) to %d\n",line,keysym,keycode);
116 k->keysym2keycode[keysym]=keycode;
117 #ifndef KEYBOARD_IGNORE_CASE
118 line[0]=toupper(line[0]);
119 keysym=get_keysym(line);
120 if(keysym)
121 k->keysym2keycode[keysym]=keycode;
122 #endif
123 } else {
124 if(k->extra_count>=MAX_EXTRA_COUNT) {
125 term_printf("Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n",line,keysym);
126 } else {
127 //term_printf("Setting %d: %d,%d\n",k->extra_count,keysym,keycode);
128 k->keysym2keycode_extra[k->extra_count].keysym=keysym;
129 k->keysym2keycode_extra[k->extra_count].keycode=keycode;
130 k->extra_count++;
131 }
132 }
133 }
134 }
135 }
136 }
137 fclose(f);
138 return k;
139 }
141 static void* init_keyboard_layout(const char* language)
142 {
143 return parse_keyboard_layout(language,0);
144 }
146 static WORD keysym2scancode(void* kbd_layout, int keysym)
147 {
148 kbd_layout_t* k=kbd_layout;
149 if(keysym<MAX_NORMAL_KEYCODE) {
150 if(k->keysym2keycode[keysym]==0)
151 term_printf("Warning: no scancode found for keysym %d\n",keysym);
152 return k->keysym2keycode[keysym];
153 } else {
154 int i;
155 #ifdef XK_ISO_Left_Tab
156 if(keysym==XK_ISO_Left_Tab)
157 keysym=XK_Tab;
158 #endif
159 for(i=0;i<k->extra_count;i++)
160 if(k->keysym2keycode_extra[i].keysym==keysym)
161 return k->keysym2keycode_extra[i].keycode;
162 }
163 return 0;
164 }