freerdp: get rid of old types
[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/utils/memory.h>
33 #include <freerdp/locale/locale.h>
34 #include <freerdp/locale/keyboard.h>
35
36 #include "keyboard_x11.h"
37 #include "keyboard_keymap.h"
38 #include "xkb_layout_ids.h"
39
40 #ifdef WITH_SUN
41 #include "keyboard_sun.h"
42 #endif
43
44
45 extern const RDP_SCANCODE VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[256];
46
47
48 UINT32 freerdp_detect_keyboard_layout_from_xkb(char** xkb_layout, char** xkb_variant)
49 {
50         char* pch;
51         char* beg;
52         char* end;
53         FILE* xprop;
54         char buffer[1024];
55         char* layout = NULL;
56         char* variant = NULL;
57         UINT32 keyboardLayoutId = 0;
58
59         /* We start by looking for _XKB_RULES_NAMES_BACKUP which appears to be used by libxklavier */
60
61         xprop = popen("xprop -root _XKB_RULES_NAMES_BACKUP", "r");
62
63         /* Sample output for "Canadian Multilingual Standard"
64          *
65          * _XKB_RULES_NAMES_BACKUP(STRING) = "xorg", "pc105", "ca", "multix", ""
66          * Where "xorg" is the set of rules
67          * "pc105" the keyboard type
68          * "ca" the keyboard layout
69          * "multi" the keyboard layout variant
70          */
71
72         while(fgets(buffer, sizeof(buffer), xprop) != NULL)
73         {
74                 if((pch = strstr(buffer, "_XKB_RULES_NAMES_BACKUP(STRING) = ")) != NULL)
75                 {
76                         /* "rules" */
77                         pch = strchr(&buffer[34], ','); // We assume it is xorg
78                         pch += 1;
79
80                         /* "type" */
81                         pch = strchr(pch, ',');
82
83                         /* "layout" */
84                         beg = strchr(pch + 1, '"');
85                         beg += 1;
86
87                         end = strchr(beg, '"');
88                         *end = '\0';
89
90                         layout = beg;
91
92                         /* "variant" */
93                         beg = strchr(end + 1, '"');
94                         beg += 1;
95
96                         end = strchr(beg, '"');
97                         *end = '\0';
98
99                         variant = beg;
100                 }
101         }
102         pclose(xprop);
103
104         DEBUG_KBD("_XKB_RULES_NAMES_BACKUP layout: %s, variant: %s", layout, variant);
105         keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
106
107         if (keyboardLayoutId > 0)
108         {
109                 *xkb_layout = _strdup(layout);
110                 *xkb_variant = _strdup(variant);
111                 return keyboardLayoutId;
112         }
113
114         /* Check _XKB_RULES_NAMES if _XKB_RULES_NAMES_BACKUP fails */
115
116         xprop = popen("xprop -root _XKB_RULES_NAMES", "r");
117
118         while(fgets(buffer, sizeof(buffer), xprop) != NULL)
119         {
120                 if((pch = strstr(buffer, "_XKB_RULES_NAMES(STRING) = ")) != NULL)
121                 {
122                         /* "rules" */
123                         pch = strchr(&buffer[27], ','); // We assume it is xorg
124                         pch += 1;
125
126                         /* "type" */
127                         pch = strchr(pch, ',');
128
129                         /* "layout" */
130                         beg = strchr(pch + 1, '"');
131                         beg += 1;
132
133                         end = strchr(beg, '"');
134                         *end = '\0';
135
136                         layout = beg;
137
138                         /* "variant" */
139                         beg = strchr(end + 1, '"');
140                         beg += 1;
141
142                         end = strchr(beg, '"');
143                         *end = '\0';
144
145                         variant = beg;
146                 }
147         }
148         pclose(xprop);
149
150         DEBUG_KBD("_XKB_RULES_NAMES layout: %s, variant: %s", layout, variant);
151         keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
152
153         if (keyboardLayoutId > 0)
154         {
155                 *xkb_layout = _strdup(layout);
156                 *xkb_variant = _strdup(variant);
157                 return keyboardLayoutId;
158         }
159
160         return 0;
161 }
162
163 char* freerdp_detect_keymap_from_xkb()
164 {
165         char* pch;
166         char* beg;
167         char* end;
168         int length;
169         FILE* setxkbmap;
170         char buffer[1024];
171         char* keymap = NULL;
172
173         /* this tells us about the current XKB configuration, if XKB is available */
174         setxkbmap = popen("setxkbmap -print", "r");
175
176         while (fgets(buffer, sizeof(buffer), setxkbmap) != NULL)
177         {
178                 /* the line with xkb_keycodes is what interests us */
179                 pch = strstr(buffer, "xkb_keycodes");
180
181                 if (pch != NULL)
182                 {
183                         pch = strstr(pch, "include");
184
185                         if (pch != NULL)
186                         {
187                                 /* check for " " delimiter presence */
188                                 if ((beg = strchr(pch, '"')) == NULL)
189                                         break;
190                                 else
191                                         beg++;
192
193                                 if ((pch = strchr(beg + 1, '"')) == NULL)
194                                         break;
195
196                                 end = strcspn(beg + 1, "\"") + beg + 1;
197                                 *end = '\0';
198
199                                 length = (end - beg);
200                                 keymap = (char*) malloc(length + 1);
201                                 strncpy(keymap, beg, length);
202                                 keymap[length] = '\0';
203
204                                 break;
205                         }
206                 }
207         }
208
209         pclose(setxkbmap);
210
211         return keymap;
212 }
213
214 UINT32 freerdp_keyboard_init_x11(UINT32 keyboardLayoutId, RDP_SCANCODE x11_keycode_to_rdp_scancode[256])
215 {
216         UINT32 vkcode;
217         UINT32 keycode;
218         UINT32 keycode_to_vkcode[256];
219
220         memset(keycode_to_vkcode, 0, sizeof(keycode_to_vkcode));
221         memset(x11_keycode_to_rdp_scancode, 0, sizeof(x11_keycode_to_rdp_scancode));
222
223 #ifdef __APPLE__
224         /* Apple X11 breaks XKB detection */
225         freerdp_keyboard_load_map(keycode_to_vkcode, "macosx(macosx)");
226 #elif defined(WITH_SUN)
227         {
228                 char sunkeymap[32];
229
230                 freerdp_detect_keyboard_type_and_layout_solaris(sunkeymap, sizeof(sunkeymap));
231                 freerdp_keyboard_load_map(keycode_to_vkcode, sunkeymap);
232         }
233 #else
234         {
235                 char* keymap;
236                 char* xkb_layout;
237                 char* xkb_variant;
238
239                 if (keyboardLayoutId == 0)
240                 {
241                         keyboardLayoutId = freerdp_detect_keyboard_layout_from_xkb(&xkb_layout, &xkb_variant);
242                         free(xkb_layout);
243                         free(xkb_variant);
244                 }
245
246                 keymap = freerdp_detect_keymap_from_xkb();
247
248                 if (keymap != NULL)
249                 {
250                         freerdp_keyboard_load_maps(keycode_to_vkcode, keymap);
251                         free(keymap);
252                 }
253         }
254 #endif
255
256         for (keycode = 0; keycode < 256; keycode++)
257         {
258                 vkcode = keycode_to_vkcode[keycode];
259
260                 if (!(vkcode > 0 && vkcode < 256))
261                         continue;
262
263                 x11_keycode_to_rdp_scancode[keycode] = VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[vkcode];
264         }
265
266         return keyboardLayoutId;
267 }