2 * FreeRDP: A Remote Desktop Protocol Implementation
5 * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <winpr/crt.h>
30 #include "liblocale.h"
32 #include <freerdp/utils/memory.h>
33 #include <freerdp/locale/locale.h>
34 #include <freerdp/locale/keyboard.h>
36 #include "keyboard_x11.h"
37 #include "keyboard_keymap.h"
38 #include "xkb_layout_ids.h"
41 #include "keyboard_sun.h"
45 extern const RDP_SCANCODE VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[256];
48 UINT32 freerdp_detect_keyboard_layout_from_xkb(char** xkb_layout, char** xkb_variant)
57 UINT32 keyboardLayoutId = 0;
59 /* We start by looking for _XKB_RULES_NAMES_BACKUP which appears to be used by libxklavier */
61 xprop = popen("xprop -root _XKB_RULES_NAMES_BACKUP", "r");
63 /* Sample output for "Canadian Multilingual Standard"
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
72 while(fgets(buffer, sizeof(buffer), xprop) != NULL)
74 if((pch = strstr(buffer, "_XKB_RULES_NAMES_BACKUP(STRING) = ")) != NULL)
77 pch = strchr(&buffer[34], ','); // We assume it is xorg
81 pch = strchr(pch, ',');
84 beg = strchr(pch + 1, '"');
87 end = strchr(beg, '"');
93 beg = strchr(end + 1, '"');
96 end = strchr(beg, '"');
104 DEBUG_KBD("_XKB_RULES_NAMES_BACKUP layout: %s, variant: %s", layout, variant);
105 keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
107 if (keyboardLayoutId > 0)
109 *xkb_layout = _strdup(layout);
110 *xkb_variant = _strdup(variant);
111 return keyboardLayoutId;
114 /* Check _XKB_RULES_NAMES if _XKB_RULES_NAMES_BACKUP fails */
116 xprop = popen("xprop -root _XKB_RULES_NAMES", "r");
118 while(fgets(buffer, sizeof(buffer), xprop) != NULL)
120 if((pch = strstr(buffer, "_XKB_RULES_NAMES(STRING) = ")) != NULL)
123 pch = strchr(&buffer[27], ','); // We assume it is xorg
127 pch = strchr(pch, ',');
130 beg = strchr(pch + 1, '"');
133 end = strchr(beg, '"');
139 beg = strchr(end + 1, '"');
142 end = strchr(beg, '"');
150 DEBUG_KBD("_XKB_RULES_NAMES layout: %s, variant: %s", layout, variant);
151 keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
153 if (keyboardLayoutId > 0)
155 *xkb_layout = _strdup(layout);
156 *xkb_variant = _strdup(variant);
157 return keyboardLayoutId;
163 char* freerdp_detect_keymap_from_xkb()
173 /* this tells us about the current XKB configuration, if XKB is available */
174 setxkbmap = popen("setxkbmap -print", "r");
176 while (fgets(buffer, sizeof(buffer), setxkbmap) != NULL)
178 /* the line with xkb_keycodes is what interests us */
179 pch = strstr(buffer, "xkb_keycodes");
183 pch = strstr(pch, "include");
187 /* check for " " delimiter presence */
188 if ((beg = strchr(pch, '"')) == NULL)
193 if ((pch = strchr(beg + 1, '"')) == NULL)
196 end = strcspn(beg + 1, "\"") + beg + 1;
199 length = (end - beg);
200 keymap = (char*) malloc(length + 1);
201 strncpy(keymap, beg, length);
202 keymap[length] = '\0';
214 UINT32 freerdp_keyboard_init_x11(UINT32 keyboardLayoutId, RDP_SCANCODE x11_keycode_to_rdp_scancode[256])
218 UINT32 keycode_to_vkcode[256];
220 memset(keycode_to_vkcode, 0, sizeof(keycode_to_vkcode));
221 memset(x11_keycode_to_rdp_scancode, 0, sizeof(x11_keycode_to_rdp_scancode));
224 /* Apple X11 breaks XKB detection */
225 freerdp_keyboard_load_map(keycode_to_vkcode, "macosx(macosx)");
226 #elif defined(WITH_SUN)
230 freerdp_detect_keyboard_type_and_layout_solaris(sunkeymap, sizeof(sunkeymap));
231 freerdp_keyboard_load_map(keycode_to_vkcode, sunkeymap);
239 if (keyboardLayoutId == 0)
241 keyboardLayoutId = freerdp_detect_keyboard_layout_from_xkb(&xkb_layout, &xkb_variant);
246 keymap = freerdp_detect_keymap_from_xkb();
250 freerdp_keyboard_load_maps(keycode_to_vkcode, keymap);
256 for (keycode = 0; keycode < 256; keycode++)
258 vkcode = keycode_to_vkcode[keycode];
260 if (!(vkcode > 0 && vkcode < 256))
263 x11_keycode_to_rdp_scancode[keycode] = VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[vkcode];
266 return keyboardLayoutId;