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 int freerdp_detect_keyboard_layout_from_xkb(DWORD* keyboardLayoutId)
49 /* We start by looking for _XKB_RULES_NAMES_BACKUP which appears to be used by libxklavier */
51 xprop = popen("xprop -root _XKB_RULES_NAMES_BACKUP", "r");
53 /* Sample output for "Canadian Multilingual Standard"
55 * _XKB_RULES_NAMES_BACKUP(STRING) = "xorg", "pc105", "ca", "multix", ""
56 * Where "xorg" is the set of rules
57 * "pc105" the keyboard type
58 * "ca" the keyboard layout
59 * "multi" the keyboard layout variant
62 while (fgets(buffer, sizeof(buffer), xprop) != NULL)
64 if ((pch = strstr(buffer, "_XKB_RULES_NAMES_BACKUP(STRING) = ")) != NULL)
67 pch = strchr(&buffer[34], ','); /* We assume it is xorg */
71 pch = strchr(pch, ',');
74 beg = strchr(pch + 1, '"');
77 end = strchr(beg, '"');
83 beg = strchr(end + 1, '"');
86 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)
101 /* Check _XKB_RULES_NAMES if _XKB_RULES_NAMES_BACKUP fails */
103 xprop = popen("xprop -root _XKB_RULES_NAMES", "r");
105 while (fgets(buffer, sizeof(buffer), xprop) != NULL)
107 if ((pch = strstr(buffer, "_XKB_RULES_NAMES(STRING) = ")) != NULL)
110 pch = strchr(&buffer[27], ','); // We assume it is xorg
114 pch = strchr(pch, ',');
117 beg = strchr(pch + 1, '"');
120 end = strchr(beg, '"');
126 beg = strchr(end + 1, '"');
129 end = strchr(beg, '"');
138 DEBUG_KBD("_XKB_RULES_NAMES layout: %s, variant: %s", layout, variant);
139 *keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
141 if (*keyboardLayoutId > 0)
142 return *keyboardLayoutId;
147 char* freerdp_detect_keymap_from_xkb()
157 /* this tells us about the current XKB configuration, if XKB is available */
158 setxkbmap = popen("setxkbmap -print", "r");
160 while (fgets(buffer, sizeof(buffer), setxkbmap) != NULL)
162 /* the line with xkb_keycodes is what interests us */
163 pch = strstr(buffer, "xkb_keycodes");
167 pch = strstr(pch, "include");
171 /* check for " " delimiter presence */
172 if ((beg = strchr(pch, '"')) == NULL)
177 if ((pch = strchr(beg + 1, '"')) == NULL)
180 end = strcspn(beg + 1, "\"") + beg + 1;
183 length = (end - beg);
184 keymap = (char*) malloc(length + 1);
185 strncpy(keymap, beg, length);
186 keymap[length] = '\0';