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>
29 #include <winpr/input.h>
31 #include "liblocale.h"
33 #include <freerdp/locale/locale.h>
34 #include <freerdp/locale/keyboard.h>
36 #include "keyboard_x11.h"
37 #include "xkb_layout_ids.h"
39 UINT32 freerdp_detect_keyboard_layout_from_xkb(char** xkb_layout, char** xkb_variant)
48 UINT32 keyboardLayoutId = 0;
50 /* We start by looking for _XKB_RULES_NAMES_BACKUP which appears to be used by libxklavier */
52 xprop = popen("xprop -root _XKB_RULES_NAMES_BACKUP", "r");
54 /* Sample output for "Canadian Multilingual Standard"
56 * _XKB_RULES_NAMES_BACKUP(STRING) = "xorg", "pc105", "ca", "multix", ""
57 * Where "xorg" is the set of rules
58 * "pc105" the keyboard type
59 * "ca" the keyboard layout
60 * "multi" the keyboard layout variant
63 while (fgets(buffer, sizeof(buffer), xprop) != NULL)
65 if ((pch = strstr(buffer, "_XKB_RULES_NAMES_BACKUP(STRING) = ")) != NULL)
68 pch = strchr(&buffer[34], ','); /* We assume it is xorg */
72 pch = strchr(pch, ',');
75 beg = strchr(pch + 1, '"');
78 end = strchr(beg, '"');
84 beg = strchr(end + 1, '"');
87 end = strchr(beg, '"');
95 DEBUG_KBD("_XKB_RULES_NAMES_BACKUP layout: %s, variant: %s", layout, variant);
96 keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
98 if (keyboardLayoutId > 0)
100 *xkb_layout = _strdup(layout);
101 *xkb_variant = _strdup(variant);
102 return keyboardLayoutId;
105 /* Check _XKB_RULES_NAMES if _XKB_RULES_NAMES_BACKUP fails */
107 xprop = popen("xprop -root _XKB_RULES_NAMES", "r");
109 while (fgets(buffer, sizeof(buffer), xprop) != NULL)
111 if ((pch = strstr(buffer, "_XKB_RULES_NAMES(STRING) = ")) != NULL)
114 pch = strchr(&buffer[27], ','); // We assume it is xorg
118 pch = strchr(pch, ',');
121 beg = strchr(pch + 1, '"');
124 end = strchr(beg, '"');
130 beg = strchr(end + 1, '"');
133 end = strchr(beg, '"');
142 DEBUG_KBD("_XKB_RULES_NAMES layout: %s, variant: %s", layout, variant);
143 keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
145 if (keyboardLayoutId > 0)
147 *xkb_layout = _strdup(layout);
148 *xkb_variant = _strdup(variant);
149 return keyboardLayoutId;
155 char* freerdp_detect_keymap_from_xkb()
165 /* this tells us about the current XKB configuration, if XKB is available */
166 setxkbmap = popen("setxkbmap -print", "r");
168 while (fgets(buffer, sizeof(buffer), setxkbmap) != NULL)
170 /* the line with xkb_keycodes is what interests us */
171 pch = strstr(buffer, "xkb_keycodes");
175 pch = strstr(pch, "include");
179 /* check for " " delimiter presence */
180 if ((beg = strchr(pch, '"')) == NULL)
185 if ((pch = strchr(beg + 1, '"')) == NULL)
188 end = strcspn(beg + 1, "\"") + beg + 1;
191 length = (end - beg);
192 keymap = (char*) malloc(length + 1);
193 strncpy(keymap, beg, length);
194 keymap[length] = '\0';
206 UINT32 freerdp_keyboard_init_x11(UINT32 keyboardLayoutId, DWORD x11_keycode_to_rdp_scancode[256])
210 DWORD keycode_to_vkcode[256];
212 ZeroMemory(keycode_to_vkcode, sizeof(keycode_to_vkcode));
213 ZeroMemory(x11_keycode_to_rdp_scancode, sizeof(DWORD) * 256);
216 for (keycode = 0; keycode < 256; keycode++)
218 keycode_to_vkcode[keycode] = GetVirtualKeyCodeFromKeycode(keycode, KEYCODE_TYPE_APPLE);
222 char* xkb_layout = NULL;
223 char* xkb_variant = NULL;
225 if (keyboardLayoutId == 0)
227 keyboardLayoutId = freerdp_detect_keyboard_layout_from_xkb(&xkb_layout, &xkb_variant);
238 for (keycode = 0; keycode < 256; keycode++)
240 vkcode = keycode_to_vkcode[keycode];
245 x11_keycode_to_rdp_scancode[keycode] = GetVirtualScanCodeFromVirtualKeyCode(vkcode, 4);
248 return keyboardLayoutId;