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/locale/locale.h>
33 #include <freerdp/locale/keyboard.h>
35 #include "keyboard_x11.h"
36 #include "xkb_layout_ids.h"
39 #include "keyboard_sun.h"
43 extern const RDP_SCANCODE VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[256];
46 UINT32 freerdp_detect_keyboard_layout_from_xkb(char** xkb_layout, char** xkb_variant)
55 UINT32 keyboardLayoutId = 0;
57 /* We start by looking for _XKB_RULES_NAMES_BACKUP which appears to be used by libxklavier */
59 xprop = popen("xprop -root _XKB_RULES_NAMES_BACKUP", "r");
61 /* Sample output for "Canadian Multilingual Standard"
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
70 while(fgets(buffer, sizeof(buffer), xprop) != NULL)
72 if((pch = strstr(buffer, "_XKB_RULES_NAMES_BACKUP(STRING) = ")) != NULL)
75 pch = strchr(&buffer[34], ','); // We assume it is xorg
79 pch = strchr(pch, ',');
82 beg = strchr(pch + 1, '"');
85 end = strchr(beg, '"');
91 beg = strchr(end + 1, '"');
94 end = strchr(beg, '"');
102 DEBUG_KBD("_XKB_RULES_NAMES_BACKUP layout: %s, variant: %s", layout, variant);
103 keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
105 if (keyboardLayoutId > 0)
107 *xkb_layout = _strdup(layout);
108 *xkb_variant = _strdup(variant);
109 return keyboardLayoutId;
112 /* Check _XKB_RULES_NAMES if _XKB_RULES_NAMES_BACKUP fails */
114 xprop = popen("xprop -root _XKB_RULES_NAMES", "r");
116 while(fgets(buffer, sizeof(buffer), xprop) != NULL)
118 if((pch = strstr(buffer, "_XKB_RULES_NAMES(STRING) = ")) != NULL)
121 pch = strchr(&buffer[27], ','); // We assume it is xorg
125 pch = strchr(pch, ',');
128 beg = strchr(pch + 1, '"');
131 end = strchr(beg, '"');
137 beg = strchr(end + 1, '"');
140 end = strchr(beg, '"');
148 DEBUG_KBD("_XKB_RULES_NAMES layout: %s, variant: %s", layout, variant);
149 keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
151 if (keyboardLayoutId > 0)
153 *xkb_layout = _strdup(layout);
154 *xkb_variant = _strdup(variant);
155 return keyboardLayoutId;
161 char* freerdp_detect_keymap_from_xkb()
171 /* this tells us about the current XKB configuration, if XKB is available */
172 setxkbmap = popen("setxkbmap -print", "r");
174 while (fgets(buffer, sizeof(buffer), setxkbmap) != NULL)
176 /* the line with xkb_keycodes is what interests us */
177 pch = strstr(buffer, "xkb_keycodes");
181 pch = strstr(pch, "include");
185 /* check for " " delimiter presence */
186 if ((beg = strchr(pch, '"')) == NULL)
191 if ((pch = strchr(beg + 1, '"')) == NULL)
194 end = strcspn(beg + 1, "\"") + beg + 1;
197 length = (end - beg);
198 keymap = (char*) malloc(length + 1);
199 strncpy(keymap, beg, length);
200 keymap[length] = '\0';
214 const UINT32 KEYCODE_TO_VKCODE_MACOSX[256] =
248 VK_OEM_PLUS, /* 32 */
251 VK_OEM_MINUS, /* 35 */
267 VK_OEM_COMMA, /* 51 */
271 VK_OEM_PERIOD, /* 55 */
283 VK_LCONTROL, /* 67 */
291 VK_MULTIPLY, /* 75 */
302 VK_SUBTRACT, /* 86 */
316 VK_NUMPAD9, /* 100 */
329 VK_SNAPSHOT, /* 113 */
476 UINT32 freerdp_keyboard_init_x11(UINT32 keyboardLayoutId, RDP_SCANCODE x11_keycode_to_rdp_scancode[256])
480 UINT32 keycode_to_vkcode[256];
482 ZeroMemory(keycode_to_vkcode, sizeof(keycode_to_vkcode));
483 ZeroMemory(x11_keycode_to_rdp_scancode, sizeof(RDP_SCANCODE) * 256);
486 /* Apple X11 breaks XKB detection */
488 CopyMemory(keycode_to_vkcode, KEYCODE_TO_VKCODE_MACOSX, sizeof(keycode_to_vkcode));
490 #elif defined(WITH_SUN)
494 freerdp_detect_keyboard_type_and_layout_solaris(sunkeymap, sizeof(sunkeymap));
495 freerdp_keyboard_load_map(keycode_to_vkcode, sunkeymap);
500 char* xkb_layout = 0;
501 char* xkb_variant = 0;
503 if (keyboardLayoutId == 0)
505 keyboardLayoutId = freerdp_detect_keyboard_layout_from_xkb(&xkb_layout, &xkb_variant);
513 keymap = freerdp_detect_keymap_from_xkb();
517 freerdp_keyboard_load_maps(keycode_to_vkcode, keymap);
523 for (keycode = 0; keycode < 256; keycode++)
525 vkcode = keycode_to_vkcode[keycode];
527 if (!(vkcode > 0 && vkcode < 256))
530 x11_keycode_to_rdp_scancode[keycode] = VIRTUAL_KEY_CODE_TO_DEFAULT_RDP_SCANCODE_TABLE[vkcode];
533 return keyboardLayoutId;