freerdp: fix build and warnings on Mac OS X
[platform/upstream/freerdp.git] / libfreerdp / locale / keyboard_x11.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Implementation
3  * X11 Keyboard Mapping
4  *
5  * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <winpr/crt.h>
29
30 #include "liblocale.h"
31
32 #include <freerdp/locale/locale.h>
33 #include <freerdp/locale/keyboard.h>
34
35 #include "keyboard_x11.h"
36 #include "keyboard_keymap.h"
37 #include "xkb_layout_ids.h"
38
39 #ifdef WITH_SUN
40 #include "keyboard_sun.h"
41 #endif
42
43
44 extern const RDP_SCANCODE VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[256];
45
46
47 UINT32 freerdp_detect_keyboard_layout_from_xkb(char** xkb_layout, char** xkb_variant)
48 {
49         char* pch;
50         char* beg;
51         char* end;
52         FILE* xprop;
53         char buffer[1024];
54         char* layout = NULL;
55         char* variant = NULL;
56         UINT32 keyboardLayoutId = 0;
57
58         /* We start by looking for _XKB_RULES_NAMES_BACKUP which appears to be used by libxklavier */
59
60         xprop = popen("xprop -root _XKB_RULES_NAMES_BACKUP", "r");
61
62         /* Sample output for "Canadian Multilingual Standard"
63          *
64          * _XKB_RULES_NAMES_BACKUP(STRING) = "xorg", "pc105", "ca", "multix", ""
65          * Where "xorg" is the set of rules
66          * "pc105" the keyboard type
67          * "ca" the keyboard layout
68          * "multi" the keyboard layout variant
69          */
70
71         while(fgets(buffer, sizeof(buffer), xprop) != NULL)
72         {
73                 if((pch = strstr(buffer, "_XKB_RULES_NAMES_BACKUP(STRING) = ")) != NULL)
74                 {
75                         /* "rules" */
76                         pch = strchr(&buffer[34], ','); // We assume it is xorg
77                         pch += 1;
78
79                         /* "type" */
80                         pch = strchr(pch, ',');
81
82                         /* "layout" */
83                         beg = strchr(pch + 1, '"');
84                         beg += 1;
85
86                         end = strchr(beg, '"');
87                         *end = '\0';
88
89                         layout = beg;
90
91                         /* "variant" */
92                         beg = strchr(end + 1, '"');
93                         beg += 1;
94
95                         end = strchr(beg, '"');
96                         *end = '\0';
97
98                         variant = beg;
99                 }
100         }
101         pclose(xprop);
102
103         DEBUG_KBD("_XKB_RULES_NAMES_BACKUP layout: %s, variant: %s", layout, variant);
104         keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
105
106         if (keyboardLayoutId > 0)
107         {
108                 *xkb_layout = _strdup(layout);
109                 *xkb_variant = _strdup(variant);
110                 return keyboardLayoutId;
111         }
112
113         /* Check _XKB_RULES_NAMES if _XKB_RULES_NAMES_BACKUP fails */
114
115         xprop = popen("xprop -root _XKB_RULES_NAMES", "r");
116
117         while(fgets(buffer, sizeof(buffer), xprop) != NULL)
118         {
119                 if((pch = strstr(buffer, "_XKB_RULES_NAMES(STRING) = ")) != NULL)
120                 {
121                         /* "rules" */
122                         pch = strchr(&buffer[27], ','); // We assume it is xorg
123                         pch += 1;
124
125                         /* "type" */
126                         pch = strchr(pch, ',');
127
128                         /* "layout" */
129                         beg = strchr(pch + 1, '"');
130                         beg += 1;
131
132                         end = strchr(beg, '"');
133                         *end = '\0';
134
135                         layout = beg;
136
137                         /* "variant" */
138                         beg = strchr(end + 1, '"');
139                         beg += 1;
140
141                         end = strchr(beg, '"');
142                         *end = '\0';
143
144                         variant = beg;
145                 }
146         }
147         pclose(xprop);
148
149         DEBUG_KBD("_XKB_RULES_NAMES layout: %s, variant: %s", layout, variant);
150         keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
151
152         if (keyboardLayoutId > 0)
153         {
154                 *xkb_layout = _strdup(layout);
155                 *xkb_variant = _strdup(variant);
156                 return keyboardLayoutId;
157         }
158
159         return 0;
160 }
161
162 char* freerdp_detect_keymap_from_xkb()
163 {
164         char* pch;
165         char* beg;
166         char* end;
167         int length;
168         FILE* setxkbmap;
169         char buffer[1024];
170         char* keymap = NULL;
171
172         /* this tells us about the current XKB configuration, if XKB is available */
173         setxkbmap = popen("setxkbmap -print", "r");
174
175         while (fgets(buffer, sizeof(buffer), setxkbmap) != NULL)
176         {
177                 /* the line with xkb_keycodes is what interests us */
178                 pch = strstr(buffer, "xkb_keycodes");
179
180                 if (pch != NULL)
181                 {
182                         pch = strstr(pch, "include");
183
184                         if (pch != NULL)
185                         {
186                                 /* check for " " delimiter presence */
187                                 if ((beg = strchr(pch, '"')) == NULL)
188                                         break;
189                                 else
190                                         beg++;
191
192                                 if ((pch = strchr(beg + 1, '"')) == NULL)
193                                         break;
194
195                                 end = strcspn(beg + 1, "\"") + beg + 1;
196                                 *end = '\0';
197
198                                 length = (end - beg);
199                                 keymap = (char*) malloc(length + 1);
200                                 strncpy(keymap, beg, length);
201                                 keymap[length] = '\0';
202
203                                 break;
204                         }
205                 }
206         }
207
208         pclose(setxkbmap);
209
210         return keymap;
211 }
212
213 #ifdef __APPLE__
214
215 const UINT32 KEYCODE_TO_VKCODE_MACOSX[256] =
216 {
217         0, /* 0 */
218         0, /* 1 */
219         0, /* 2 */
220         0, /* 3 */
221         0, /* 4 */
222         0, /* 5 */
223         0, /* 6 */
224         0, /* 7 */
225         VK_KEY_A, /* 8 */
226         VK_KEY_S, /* 9 */
227         VK_KEY_D, /* 10 */
228         VK_KEY_F, /* 11 */
229         VK_KEY_H, /* 12 */
230         VK_KEY_G, /* 13 */
231         VK_KEY_Z, /* 14 */
232         VK_KEY_X, /* 15 */
233         VK_KEY_C, /* 16 */
234         VK_KEY_V, /* 17 */
235         VK_OEM_102, /* 18 */
236         VK_KEY_B, /* 19 */
237         VK_KEY_Q, /* 20 */
238         VK_KEY_W, /* 21 */
239         VK_KEY_E, /* 22 */
240         VK_KEY_R, /* 23 */
241         VK_KEY_Y, /* 24 */
242         VK_KEY_T, /* 25 */
243         VK_KEY_1, /* 26 */
244         VK_KEY_2, /* 27 */
245         VK_KEY_3, /* 28 */
246         VK_KEY_4, /* 29 */
247         VK_KEY_6, /* 30 */
248         VK_KEY_5, /* 31 */
249         VK_OEM_PLUS, /* 32 */
250         VK_KEY_9, /* 33 */
251         VK_KEY_7, /* 34 */
252         VK_OEM_MINUS, /* 35 */
253         VK_KEY_8, /* 36 */
254         VK_KEY_0, /* 37 */
255         VK_OEM_6, /* 38 */
256         VK_KEY_O, /* 39 */
257         VK_KEY_U, /* 40 */
258         VK_OEM_4, /* 41 */
259         VK_KEY_I, /* 42 */
260         VK_KEY_P, /* 43 */
261         VK_RETURN, /* 44 */
262         VK_KEY_L, /* 45 */
263         VK_KEY_J, /* 46 */
264         VK_OEM_7, /* 47 */
265         VK_KEY_K, /* 48 */
266         VK_OEM_1, /* 49 */
267         VK_OEM_5, /* 50 */
268         VK_OEM_COMMA, /* 51 */
269         VK_OEM_2, /* 52 */
270         VK_KEY_N, /* 53 */
271         VK_KEY_M, /* 54 */
272         VK_OEM_PERIOD, /* 55 */
273         VK_TAB, /* 56 */
274         VK_SPACE, /* 57 */
275         VK_OEM_3, /* 58 */
276         VK_BACK, /* 59 */
277         0, /* 60 */
278         VK_ESCAPE, /* 61 */
279         0, /* 62 */
280         0, /* 63 */
281         VK_LSHIFT, /* 64 */
282         VK_CAPITAL, /* 65 */
283         VK_LMENU, /* 66 */
284         VK_LCONTROL, /* 67 */
285         VK_RSHIFT, /* 68 */
286         VK_RMENU, /* 69 */
287         0, /* 70 */
288         0, /* 71 */
289         0, /* 72 */
290         VK_DECIMAL, /* 73 */
291         0, /* 74 */
292         VK_MULTIPLY, /* 75 */
293         0, /* 76 */
294         VK_ADD, /* 77 */
295         0, /* 78 */
296         VK_NUMLOCK, /* 79 */
297         0, /* 80 */
298         0, /* 81 */
299         0, /* 82 */
300         VK_DIVIDE, /* 83 */
301         VK_RETURN, /* 84 */
302         0, /* 85 */
303         VK_SUBTRACT, /* 86 */
304         0, /* 87 */
305         0, /* 88 */
306         0, /* 89 */
307         VK_NUMPAD0, /* 90 */
308         VK_NUMPAD1, /* 91 */
309         VK_NUMPAD2, /* 92 */
310         VK_NUMPAD3, /* 93 */
311         VK_NUMPAD4, /* 94 */
312         VK_NUMPAD5, /* 95 */
313         VK_NUMPAD6, /* 96 */
314         VK_NUMPAD7, /* 97 */
315         0, /* 98 */
316         VK_NUMPAD8, /* 99 */
317         VK_NUMPAD9, /* 100 */
318         0, /* 101 */
319         0, /* 102 */
320         0, /* 103 */
321         VK_F5, /* 104 */
322         VK_F6, /* 105 */
323         VK_F7, /* 106 */
324         VK_F3, /* 107 */
325         VK_F8, /* 108 */
326         VK_F9, /* 109 */
327         0, /* 110 */
328         VK_F11, /* 111 */
329         0, /* 112 */
330         VK_SNAPSHOT, /* 113 */
331         0, /* 114 */
332         VK_SCROLL, /* 115 */
333         0, /* 116 */
334         VK_F10, /* 117 */
335         0, /* 118 */
336         VK_F12, /* 119 */
337         0, /* 120 */
338         VK_PAUSE, /* 121 */
339         VK_INSERT, /* 122 */
340         VK_HOME, /* 123 */
341         VK_PRIOR, /* 124 */
342         VK_DELETE, /* 125 */
343         VK_F4, /* 126 */
344         VK_END, /* 127 */
345         VK_F2, /* 128 */
346         VK_NEXT, /* 129 */
347         VK_F1, /* 130 */
348         VK_LEFT, /* 131 */
349         VK_RIGHT, /* 132 */
350         VK_DOWN, /* 133 */
351         VK_UP, /* 134 */
352         0, /* 135 */
353         0, /* 136 */
354         0, /* 137 */
355         0, /* 138 */
356         0, /* 139 */
357         0, /* 140 */
358         0, /* 141 */
359         0, /* 142 */
360         0, /* 143 */
361         0, /* 144 */
362         0, /* 145 */
363         0, /* 146 */
364         0, /* 147 */
365         0, /* 148 */
366         0, /* 149 */
367         0, /* 150 */
368         0, /* 151 */
369         0, /* 152 */
370         0, /* 153 */
371         0, /* 154 */
372         0, /* 155 */
373         0, /* 156 */
374         0, /* 157 */
375         0, /* 158 */
376         0, /* 159 */
377         0, /* 160 */
378         0, /* 161 */
379         0, /* 162 */
380         0, /* 163 */
381         0, /* 164 */
382         0, /* 165 */
383         0, /* 166 */
384         0, /* 167 */
385         0, /* 168 */
386         0, /* 169 */
387         0, /* 170 */
388         0, /* 171 */
389         0, /* 172 */
390         0, /* 173 */
391         0, /* 174 */
392         0, /* 175 */
393         0, /* 176 */
394         0, /* 177 */
395         0, /* 178 */
396         0, /* 179 */
397         0, /* 180 */
398         0, /* 181 */
399         0, /* 182 */
400         0, /* 183 */
401         0, /* 184 */
402         0, /* 185 */
403         0, /* 186 */
404         0, /* 187 */
405         0, /* 188 */
406         0, /* 189 */
407         0, /* 190 */
408         0, /* 191 */
409         0, /* 192 */
410         0, /* 193 */
411         0, /* 194 */
412         0, /* 195 */
413         0, /* 196 */
414         0, /* 197 */
415         0, /* 198 */
416         0, /* 199 */
417         0, /* 200 */
418         0, /* 201 */
419         0, /* 202 */
420         0, /* 203 */
421         0, /* 204 */
422         0, /* 205 */
423         0, /* 206 */
424         0, /* 207 */
425         0, /* 208 */
426         0, /* 209 */
427         0, /* 210 */
428         0, /* 211 */
429         0, /* 212 */
430         0, /* 213 */
431         0, /* 214 */
432         0, /* 215 */
433         0, /* 216 */
434         0, /* 217 */
435         0, /* 218 */
436         0, /* 219 */
437         0, /* 220 */
438         0, /* 221 */
439         0, /* 222 */
440         0, /* 223 */
441         0, /* 224 */
442         0, /* 225 */
443         0, /* 226 */
444         0, /* 227 */
445         0, /* 228 */
446         0, /* 229 */
447         0, /* 230 */
448         0, /* 231 */
449         0, /* 232 */
450         0, /* 233 */
451         0, /* 234 */
452         0, /* 235 */
453         0, /* 236 */
454         0, /* 237 */
455         0, /* 238 */
456         0, /* 239 */
457         0, /* 240 */
458         0, /* 241 */
459         0, /* 242 */
460         0, /* 243 */
461         0, /* 244 */
462         0, /* 245 */
463         0, /* 246 */
464         0, /* 247 */
465         0, /* 248 */
466         0, /* 249 */
467         0, /* 250 */
468         0, /* 251 */
469         0, /* 252 */
470         0, /* 253 */
471         0, /* 254 */
472         0 /* 255 */
473 };
474
475 #endif
476
477 UINT32 freerdp_keyboard_init_x11(UINT32 keyboardLayoutId, RDP_SCANCODE x11_keycode_to_rdp_scancode[256])
478 {
479         UINT32 vkcode;
480         UINT32 keycode;
481         UINT32 keycode_to_vkcode[256];
482
483         ZeroMemory(keycode_to_vkcode, sizeof(keycode_to_vkcode));
484         ZeroMemory(x11_keycode_to_rdp_scancode, sizeof(RDP_SCANCODE) * 256);
485
486 #ifdef __APPLE__
487         /* Apple X11 breaks XKB detection */
488
489         CopyMemory(keycode_to_vkcode, KEYCODE_TO_VKCODE_MACOSX, sizeof(keycode_to_vkcode));
490
491         freerdp_keyboard_load_map(keycode_to_vkcode, "macosx(macosx)");
492
493 #elif defined(WITH_SUN)
494         {
495                 char sunkeymap[32];
496
497                 freerdp_detect_keyboard_type_and_layout_solaris(sunkeymap, sizeof(sunkeymap));
498                 freerdp_keyboard_load_map(keycode_to_vkcode, sunkeymap);
499         }
500 #else
501         {
502                 char* keymap;
503                 char* xkb_layout = 0;
504                 char* xkb_variant = 0;
505
506                 if (keyboardLayoutId == 0)
507                 {
508                         keyboardLayoutId = freerdp_detect_keyboard_layout_from_xkb(&xkb_layout, &xkb_variant);
509                         if (xkb_layout)
510                                 free(xkb_layout);
511                         if (xkb_variant)
512                                 free(xkb_variant);
513
514                 }
515
516                 keymap = freerdp_detect_keymap_from_xkb();
517
518                 if (keymap != NULL)
519                 {
520                         freerdp_keyboard_load_maps(keycode_to_vkcode, keymap);
521                         free(keymap);
522                 }
523         }
524 #endif
525
526         for (keycode = 0; keycode < 256; keycode++)
527         {
528                 vkcode = keycode_to_vkcode[keycode];
529
530                 if (!(vkcode > 0 && vkcode < 256))
531                         continue;
532
533                 x11_keycode_to_rdp_scancode[keycode] = VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[vkcode];
534         }
535
536         return keyboardLayoutId;
537 }