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