Merge pull request #1058 from awakecoding/master
[platform/upstream/freerdp.git] / libfreerdp / locale / keyboard_x11.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Implementation
3  * X11 Keyboard Mapping
4  *
5  * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <winpr/crt.h>
29 #include <winpr/input.h>
30
31 #include "liblocale.h"
32
33 #include <freerdp/locale/locale.h>
34 #include <freerdp/locale/keyboard.h>
35
36 #include "keyboard_x11.h"
37 #include "xkb_layout_ids.h"
38
39 int freerdp_detect_keyboard_layout_from_xkb(DWORD* keyboardLayoutId)
40 {
41         char* pch;
42         char* beg;
43         char* end;
44         FILE* xprop;
45         char buffer[1024];
46         char* layout = NULL;
47         char* variant = NULL;
48
49         /* We start by looking for _XKB_RULES_NAMES_BACKUP which appears to be used by libxklavier */
50
51         xprop = popen("xprop -root _XKB_RULES_NAMES_BACKUP", "r");
52
53         /* Sample output for "Canadian Multilingual Standard"
54          *
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
60          */
61
62         while (fgets(buffer, sizeof(buffer), xprop) != NULL)
63         {
64                 if ((pch = strstr(buffer, "_XKB_RULES_NAMES_BACKUP(STRING) = ")) != NULL)
65                 {
66                         /* "rules" */
67                         pch = strchr(&buffer[34], ','); /* We assume it is xorg */
68                         pch += 1;
69
70                         /* "type" */
71                         pch = strchr(pch, ',');
72
73                         /* "layout" */
74                         beg = strchr(pch + 1, '"');
75                         beg += 1;
76
77                         end = strchr(beg, '"');
78                         *end = '\0';
79
80                         layout = beg;
81
82                         /* "variant" */
83                         beg = strchr(end + 1, '"');
84                         beg += 1;
85
86                         end = strchr(beg, '"');
87                         *end = '\0';
88
89                         variant = beg;
90                 }
91         }
92
93         pclose(xprop);
94
95         DEBUG_KBD("_XKB_RULES_NAMES_BACKUP layout: %s, variant: %s", layout, variant);
96         *keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
97
98         if (*keyboardLayoutId > 0)
99                 return 0;
100
101         /* Check _XKB_RULES_NAMES if _XKB_RULES_NAMES_BACKUP fails */
102
103         xprop = popen("xprop -root _XKB_RULES_NAMES", "r");
104
105         while (fgets(buffer, sizeof(buffer), xprop) != NULL)
106         {
107                 if ((pch = strstr(buffer, "_XKB_RULES_NAMES(STRING) = ")) != NULL)
108                 {
109                         /* "rules" */
110                         pch = strchr(&buffer[27], ','); // We assume it is xorg
111                         pch += 1;
112
113                         /* "type" */
114                         pch = strchr(pch, ',');
115
116                         /* "layout" */
117                         beg = strchr(pch + 1, '"');
118                         beg += 1;
119
120                         end = strchr(beg, '"');
121                         *end = '\0';
122
123                         layout = beg;
124
125                         /* "variant" */
126                         beg = strchr(end + 1, '"');
127                         beg += 1;
128
129                         end = strchr(beg, '"');
130                         *end = '\0';
131
132                         variant = beg;
133                 }
134         }
135
136         pclose(xprop);
137
138         DEBUG_KBD("_XKB_RULES_NAMES layout: %s, variant: %s", layout, variant);
139         *keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
140
141         if (*keyboardLayoutId > 0)
142                 return *keyboardLayoutId;
143
144         return 0;
145 }
146
147 char* freerdp_detect_keymap_from_xkb()
148 {
149         char* pch;
150         char* beg;
151         char* end;
152         int length;
153         FILE* setxkbmap;
154         char buffer[1024];
155         char* keymap = NULL;
156
157         /* this tells us about the current XKB configuration, if XKB is available */
158         setxkbmap = popen("setxkbmap -print", "r");
159
160         while (fgets(buffer, sizeof(buffer), setxkbmap) != NULL)
161         {
162                 /* the line with xkb_keycodes is what interests us */
163                 pch = strstr(buffer, "xkb_keycodes");
164
165                 if (pch != NULL)
166                 {
167                         pch = strstr(pch, "include");
168
169                         if (pch != NULL)
170                         {
171                                 /* check for " " delimiter presence */
172                                 if ((beg = strchr(pch, '"')) == NULL)
173                                         break;
174                                 else
175                                         beg++;
176
177                                 if ((pch = strchr(beg + 1, '"')) == NULL)
178                                         break;
179
180                                 end = strcspn(beg + 1, "\"") + beg + 1;
181                                 *end = '\0';
182
183                                 length = (end - beg);
184                                 keymap = (char*) malloc(length + 1);
185                                 strncpy(keymap, beg, length);
186                                 keymap[length] = '\0';
187
188                                 break;
189                         }
190                 }
191         }
192
193         pclose(setxkbmap);
194
195         return keymap;
196 }