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 "liblocale.h"
29 #include <freerdp/utils/memory.h>
30 #include <freerdp/locale/locale.h>
31 #include <freerdp/locale/keyboard.h>
33 #include "keyboard_x11.h"
34 #include "keyboard_keymap.h"
35 #include "xkb_layout_ids.h"
38 #include "keyboard_sun.h"
42 extern const RDP_SCANCODE VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[256];
45 uint32 freerdp_detect_keyboard_layout_from_xkb(char** xkb_layout, char** xkb_variant)
54 uint32 keyboardLayoutId = 0;
56 /* We start by looking for _XKB_RULES_NAMES_BACKUP which appears to be used by libxklavier */
58 xprop = popen("xprop -root _XKB_RULES_NAMES_BACKUP", "r");
60 /* Sample output for "Canadian Multilingual Standard"
62 * _XKB_RULES_NAMES_BACKUP(STRING) = "xorg", "pc105", "ca", "multix", ""
63 * Where "xorg" is the set of rules
64 * "pc105" the keyboard type
65 * "ca" the keyboard layout
66 * "multi" the keyboard layout variant
69 while(fgets(buffer, sizeof(buffer), xprop) != NULL)
71 if((pch = strstr(buffer, "_XKB_RULES_NAMES_BACKUP(STRING) = ")) != NULL)
74 pch = strchr(&buffer[34], ','); // We assume it is xorg
78 pch = strchr(pch, ',');
81 beg = strchr(pch + 1, '"');
84 end = strchr(beg, '"');
90 beg = strchr(end + 1, '"');
93 end = strchr(beg, '"');
101 DEBUG_KBD("_XKB_RULES_NAMES_BACKUP layout: %s, variant: %s", layout, variant);
102 keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
104 if (keyboardLayoutId > 0)
106 *xkb_layout = xstrdup(layout);
107 *xkb_variant = xstrdup(variant);
108 return keyboardLayoutId;
111 /* Check _XKB_RULES_NAMES if _XKB_RULES_NAMES_BACKUP fails */
113 xprop = popen("xprop -root _XKB_RULES_NAMES", "r");
115 while(fgets(buffer, sizeof(buffer), xprop) != NULL)
117 if((pch = strstr(buffer, "_XKB_RULES_NAMES(STRING) = ")) != NULL)
120 pch = strchr(&buffer[27], ','); // We assume it is xorg
124 pch = strchr(pch, ',');
127 beg = strchr(pch + 1, '"');
130 end = strchr(beg, '"');
136 beg = strchr(end + 1, '"');
139 end = strchr(beg, '"');
147 DEBUG_KBD("_XKB_RULES_NAMES layout: %s, variant: %s", layout, variant);
148 keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
150 if (keyboardLayoutId > 0)
152 *xkb_layout = xstrdup(layout);
153 *xkb_variant = xstrdup(variant);
154 return keyboardLayoutId;
160 char* freerdp_detect_keymap_from_xkb()
170 /* this tells us about the current XKB configuration, if XKB is available */
171 setxkbmap = popen("setxkbmap -print", "r");
173 while (fgets(buffer, sizeof(buffer), setxkbmap) != NULL)
175 /* the line with xkb_keycodes is what interests us */
176 pch = strstr(buffer, "xkb_keycodes");
180 pch = strstr(pch, "include");
184 /* check for " " delimiter presence */
185 if ((beg = strchr(pch, '"')) == NULL)
190 if ((pch = strchr(beg + 1, '"')) == NULL)
193 end = strcspn(beg + 1, "\"") + beg + 1;
196 length = (end - beg);
197 keymap = (char*) xmalloc(length + 1);
198 strncpy(keymap, beg, length);
199 keymap[length] = '\0';
211 uint32 freerdp_keyboard_init_x11(uint32 keyboardLayoutId, RDP_SCANCODE x11_keycode_to_rdp_scancode[256])
215 uint32 keycode_to_vkcode[256];
217 memset(keycode_to_vkcode, 0, sizeof(keycode_to_vkcode));
218 memset(x11_keycode_to_rdp_scancode, 0, sizeof(x11_keycode_to_rdp_scancode));
221 /* Apple X11 breaks XKB detection */
222 freerdp_keyboard_load_map(keycode_to_vkcode, "macosx(macosx)");
223 #elif defined(WITH_SUN)
227 freerdp_detect_keyboard_type_and_layout_solaris(sunkeymap, sizeof(sunkeymap));
228 freerdp_keyboard_load_map(keycode_to_vkcode, sunkeymap);
236 if (keyboardLayoutId == 0)
238 keyboardLayoutId = freerdp_detect_keyboard_layout_from_xkb(&xkb_layout, &xkb_variant);
243 keymap = freerdp_detect_keymap_from_xkb();
247 freerdp_keyboard_load_maps(keycode_to_vkcode, keymap);
253 for (keycode = 0; keycode < 256; keycode++)
255 vkcode = keycode_to_vkcode[keycode];
257 if (!(vkcode > 0 && vkcode < 256))
260 x11_keycode_to_rdp_scancode[keycode] = VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[vkcode];
263 return keyboardLayoutId;