libfreerdp: purged source tree from deprecated memory utils
[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 UINT32 freerdp_keyboard_init_x11(UINT32 keyboardLayoutId, RDP_SCANCODE x11_keycode_to_rdp_scancode[256])
214 {
215         UINT32 vkcode;
216         UINT32 keycode;
217         UINT32 keycode_to_vkcode[256];
218
219         memset(keycode_to_vkcode, 0, sizeof(keycode_to_vkcode));
220         memset(x11_keycode_to_rdp_scancode, 0, sizeof(x11_keycode_to_rdp_scancode));
221
222 #ifdef __APPLE__
223         /* Apple X11 breaks XKB detection */
224         freerdp_keyboard_load_map(keycode_to_vkcode, "macosx(macosx)");
225 #elif defined(WITH_SUN)
226         {
227                 char sunkeymap[32];
228
229                 freerdp_detect_keyboard_type_and_layout_solaris(sunkeymap, sizeof(sunkeymap));
230                 freerdp_keyboard_load_map(keycode_to_vkcode, sunkeymap);
231         }
232 #else
233         {
234                 char* keymap;
235                 char* xkb_layout;
236                 char* xkb_variant;
237
238                 if (keyboardLayoutId == 0)
239                 {
240                         keyboardLayoutId = freerdp_detect_keyboard_layout_from_xkb(&xkb_layout, &xkb_variant);
241                         free(xkb_layout);
242                         free(xkb_variant);
243                 }
244
245                 keymap = freerdp_detect_keymap_from_xkb();
246
247                 if (keymap != NULL)
248                 {
249                         freerdp_keyboard_load_maps(keycode_to_vkcode, keymap);
250                         free(keymap);
251                 }
252         }
253 #endif
254
255         for (keycode = 0; keycode < 256; keycode++)
256         {
257                 vkcode = keycode_to_vkcode[keycode];
258
259                 if (!(vkcode > 0 && vkcode < 256))
260                         continue;
261
262                 x11_keycode_to_rdp_scancode[keycode] = VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[vkcode];
263         }
264
265         return keyboardLayoutId;
266 }