FreeRDP
keyboard_xkbfile.c
1 
20 #include <freerdp/config.h>
21 
22 #include "keyboard_xkbfile.h"
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 <freerdp/locale/keyboard.h>
32 
33 #include "keyboard_x11.h"
34 #include "xkb_layout_ids.h"
35 #include "liblocale.h"
36 
37 #include <X11/X.h>
38 #include <X11/Xlib.h>
39 #include <X11/XKBlib.h>
40 #include <X11/extensions/XKBfile.h>
41 #include <X11/extensions/XKBrules.h>
42 
43 typedef struct
44 {
45  const char* xkb_keyname; /* XKB keyname */
46  DWORD rdp_scancode;
47 } XKB_KEY_NAME_SCANCODE;
48 
49 static const XKB_KEY_NAME_SCANCODE XKB_KEY_NAME_SCANCODE_TABLE[] = {
50  { "", RDP_SCANCODE_UNKNOWN }, /* 008: [(null)] */
51  { "ESC", RDP_SCANCODE_ESCAPE }, /* 009: ESC [Escape] */
52  { "AE01", RDP_SCANCODE_KEY_1 }, /* 010: AE01 [1] */
53  { "AE02", RDP_SCANCODE_KEY_2 }, /* 011: AE02 [2] */
54  { "AE03", RDP_SCANCODE_KEY_3 }, /* 012: AE03 [3] */
55  { "AE04", RDP_SCANCODE_KEY_4 }, /* 013: AE04 [4] */
56  { "AE05", RDP_SCANCODE_KEY_5 }, /* 014: AE05 [5] */
57  { "AE06", RDP_SCANCODE_KEY_6 }, /* 015: AE06 [6] */
58  { "AE07", RDP_SCANCODE_KEY_7 }, /* 016: AE07 [7] */
59  { "AE08", RDP_SCANCODE_KEY_8 }, /* 017: AE08 [8] */
60  { "AE09", RDP_SCANCODE_KEY_9 }, /* 018: AE09 [9] */
61  { "AE10", RDP_SCANCODE_KEY_0 }, /* 019: AE10 [0] */
62  { "AE11", RDP_SCANCODE_OEM_MINUS }, /* 020: AE11 [minus] */
63  { "AE12", RDP_SCANCODE_OEM_PLUS }, /* 021: AE12 [equal] */
64  { "BKSP", RDP_SCANCODE_BACKSPACE }, /* 022: BKSP [BackSpace] */
65  { "TAB", RDP_SCANCODE_TAB }, /* 023: TAB [Tab] */
66  { "AD01", RDP_SCANCODE_KEY_Q }, /* 024: AD01 [q] */
67  { "AD02", RDP_SCANCODE_KEY_W }, /* 025: AD02 [w] */
68  { "AD03", RDP_SCANCODE_KEY_E }, /* 026: AD03 [e] */
69  { "AD04", RDP_SCANCODE_KEY_R }, /* 027: AD04 [r] */
70  { "AD05", RDP_SCANCODE_KEY_T }, /* 028: AD05 [t] */
71  { "AD06", RDP_SCANCODE_KEY_Y }, /* 029: AD06 [y] */
72  { "AD07", RDP_SCANCODE_KEY_U }, /* 030: AD07 [u] */
73  { "AD08", RDP_SCANCODE_KEY_I }, /* 031: AD08 [i] */
74  { "AD09", RDP_SCANCODE_KEY_O }, /* 032: AD09 [o] */
75  { "AD10", RDP_SCANCODE_KEY_P }, /* 033: AD10 [p] */
76  { "AD11", RDP_SCANCODE_OEM_4 }, /* 034: AD11 [bracketleft] */
77  { "AD12", RDP_SCANCODE_OEM_6 }, /* 035: AD12 [bracketright] */
78  { "RTRN", RDP_SCANCODE_RETURN }, /* 036: RTRN [Return] */
79  { "LCTL", RDP_SCANCODE_LCONTROL }, /* 037: LCTL [Control_L] */
80  { "AC01", RDP_SCANCODE_KEY_A }, /* 038: AC01 [a] */
81  { "AC02", RDP_SCANCODE_KEY_S }, /* 039: AC02 [s] */
82  { "AC03", RDP_SCANCODE_KEY_D }, /* 040: AC03 [d] */
83  { "AC04", RDP_SCANCODE_KEY_F }, /* 041: AC04 [f] */
84  { "AC05", RDP_SCANCODE_KEY_G }, /* 042: AC05 [g] */
85  { "AC06", RDP_SCANCODE_KEY_H }, /* 043: AC06 [h] */
86  { "AC07", RDP_SCANCODE_KEY_J }, /* 044: AC07 [j] */
87  { "AC08", RDP_SCANCODE_KEY_K }, /* 045: AC08 [k] */
88  { "AC09", RDP_SCANCODE_KEY_L }, /* 046: AC09 [l] */
89  { "AC10", RDP_SCANCODE_OEM_1 }, /* 047: AC10 [semicolon] */
90  { "AC11", RDP_SCANCODE_OEM_7 }, /* 048: AC11 [dead_acute] */
91  { "TLDE", RDP_SCANCODE_OEM_3 }, /* 049: TLDE [dead_grave] */
92  { "LFSH", RDP_SCANCODE_LSHIFT }, /* 050: LFSH [Shift_L] */
93  { "BKSL", RDP_SCANCODE_OEM_5 }, /* 051: BKSL [backslash] */
94  { "AB01", RDP_SCANCODE_KEY_Z }, /* 052: AB01 [z] */
95  { "AB02", RDP_SCANCODE_KEY_X }, /* 053: AB02 [x] */
96  { "AB03", RDP_SCANCODE_KEY_C }, /* 054: AB03 [c] */
97  { "AB04", RDP_SCANCODE_KEY_V }, /* 055: AB04 [v] */
98  { "AB05", RDP_SCANCODE_KEY_B }, /* 056: AB05 [b] */
99  { "AB06", RDP_SCANCODE_KEY_N }, /* 057: AB06 [n] */
100  { "AB07", RDP_SCANCODE_KEY_M }, /* 058: AB07 [m] */
101  { "AB08", RDP_SCANCODE_OEM_COMMA }, /* 059: AB08 [comma] */
102  { "AB09", RDP_SCANCODE_OEM_PERIOD }, /* 060: AB09 [period] */
103  { "AB10", RDP_SCANCODE_OEM_2 }, /* 061: AB10 [slash] */
104  { "RTSH", RDP_SCANCODE_RSHIFT }, /* 062: RTSH [Shift_R] */
105  { "KPMU", RDP_SCANCODE_MULTIPLY }, /* 063: KPMU [KP_Multiply] */
106  { "LALT", RDP_SCANCODE_LMENU }, /* 064: LALT [Alt_L] */
107  { "SPCE", RDP_SCANCODE_SPACE }, /* 065: SPCE [space] */
108  { "CAPS", RDP_SCANCODE_CAPSLOCK }, /* 066: CAPS [Caps_Lock] */
109  { "FK01", RDP_SCANCODE_F1 }, /* 067: FK01 [F1] */
110  { "FK02", RDP_SCANCODE_F2 }, /* 068: FK02 [F2] */
111  { "FK03", RDP_SCANCODE_F3 }, /* 069: FK03 [F3] */
112  { "FK04", RDP_SCANCODE_F4 }, /* 070: FK04 [F4] */
113  { "FK05", RDP_SCANCODE_F5 }, /* 071: FK05 [F5] */
114  { "FK06", RDP_SCANCODE_F6 }, /* 072: FK06 [F6] */
115  { "FK07", RDP_SCANCODE_F7 }, /* 073: FK07 [F7] */
116  { "FK08", RDP_SCANCODE_F8 }, /* 074: FK08 [F8] */
117  { "FK09", RDP_SCANCODE_F9 }, /* 075: FK09 [F9] */
118  { "FK10", RDP_SCANCODE_F10 }, /* 076: FK10 [F10] */
119  { "NMLK", RDP_SCANCODE_NUMLOCK }, /* 077: NMLK [Num_Lock] */
120  { "SCLK", RDP_SCANCODE_SCROLLLOCK }, /* 078: SCLK [Multi_key] */
121  { "KP7", RDP_SCANCODE_NUMPAD7 }, /* 079: KP7 [KP_Home] */
122  { "KP8", RDP_SCANCODE_NUMPAD8 }, /* 080: KP8 [KP_Up] */
123  { "KP9", RDP_SCANCODE_NUMPAD9 }, /* 081: KP9 [KP_Prior] */
124  { "KPSU", RDP_SCANCODE_SUBTRACT }, /* 082: KPSU [KP_Subtract] */
125  { "KP4", RDP_SCANCODE_NUMPAD4 }, /* 083: KP4 [KP_Left] */
126  { "KP5", RDP_SCANCODE_NUMPAD5 }, /* 084: KP5 [KP_Begin] */
127  { "KP6", RDP_SCANCODE_NUMPAD6 }, /* 085: KP6 [KP_Right] */
128  { "KPAD", RDP_SCANCODE_ADD }, /* 086: KPAD [KP_Add] */
129  { "KP1", RDP_SCANCODE_NUMPAD1 }, /* 087: KP1 [KP_End] */
130  { "KP2", RDP_SCANCODE_NUMPAD2 }, /* 088: KP2 [KP_Down] */
131  { "KP3", RDP_SCANCODE_NUMPAD3 }, /* 089: KP3 [KP_Next] */
132  { "KP0", RDP_SCANCODE_NUMPAD0 }, /* 090: KP0 [KP_Insert] */
133  { "KPDL", RDP_SCANCODE_DECIMAL }, /* 091: KPDL [KP_Delete] */
134  { "LVL3", RDP_SCANCODE_RMENU }, /* 092: LVL3 [ISO_Level3_Shift] */
135  { "", RDP_SCANCODE_UNKNOWN }, /* 093: [(null)] */
136  { "LSGT", RDP_SCANCODE_OEM_102 }, /* 094: LSGT [backslash] */
137  { "FK11", RDP_SCANCODE_F11 }, /* 095: FK11 [F11] */
138  { "FK12", RDP_SCANCODE_F12 }, /* 096: FK12 [F12] */
139  { "AB11", RDP_SCANCODE_ABNT_C1 }, /* 097: AB11 [(null)] */
140  { "KATA", RDP_SCANCODE_KANA_HANGUL }, /* 098: KATA [Katakana] */
141  { "HIRA", RDP_SCANCODE_HIRAGANA }, /* 099: HIRA [Hiragana] */
142  { "HENK", RDP_SCANCODE_CONVERT_JP }, /* 100: HENK [Henkan_Mode] */
143  { "HKTG", RDP_SCANCODE_HIRAGANA }, /* 101: HKTG [Hiragana_Katakana] */
144  { "MUHE", RDP_SCANCODE_NONCONVERT_JP }, /* 102: MUHE [Muhenkan] */
145  { "JPCM", RDP_SCANCODE_UNKNOWN }, /* 103: JPCM [(null)] */
146  { "KPEN", RDP_SCANCODE_RETURN_KP }, /* 104: KPEN [KP_Enter] */
147  { "RCTL", RDP_SCANCODE_RCONTROL }, /* 105: RCTL [Control_R] */
148  { "KPDV", RDP_SCANCODE_DIVIDE }, /* 106: KPDV [KP_Divide] */
149  { "PRSC", RDP_SCANCODE_PRINTSCREEN }, /* 107: PRSC [Print] */
150  { "RALT", RDP_SCANCODE_RMENU }, /* 108: RALT [ISO_Level3_Shift] */
151  { "LNFD", RDP_SCANCODE_UNKNOWN }, /* 109: LNFD [Linefeed] */
152  { "HOME", RDP_SCANCODE_HOME }, /* 110: HOME [Home] */
153  { "UP", RDP_SCANCODE_UP }, /* 111: UP [Up] */
154  { "PGUP", RDP_SCANCODE_PRIOR }, /* 112: PGUP [Prior] */
155  { "LEFT", RDP_SCANCODE_LEFT }, /* 113: LEFT [Left] */
156  { "RGHT", RDP_SCANCODE_RIGHT }, /* 114: RGHT [Right] */
157  { "END", RDP_SCANCODE_END }, /* 115: END [End] */
158  { "DOWN", RDP_SCANCODE_DOWN }, /* 116: DOWN [Down] */
159  { "PGDN", RDP_SCANCODE_NEXT }, /* 117: PGDN [Next] */
160  { "INS", RDP_SCANCODE_INSERT }, /* 118: INS [Insert] */
161  { "DELE", RDP_SCANCODE_DELETE }, /* 119: DELE [Delete] */
162  { "I120", RDP_SCANCODE_UNKNOWN }, /* 120: I120 [(null)] */
163  { "MUTE", RDP_SCANCODE_VOLUME_MUTE }, /* 121: MUTE [XF86AudioMute] */
164  { "VOL-", RDP_SCANCODE_VOLUME_DOWN }, /* 122: VOL- [XF86AudioLowerVolume] */
165  { "VOL+", RDP_SCANCODE_VOLUME_UP }, /* 123: VOL+ [XF86AudioRaiseVolume] */
166  { "POWR", RDP_SCANCODE_UNKNOWN }, /* 124: POWR [XF86PowerOff] */
167  { "KPEQ", RDP_SCANCODE_UNKNOWN }, /* 125: KPEQ [KP_Equal] */
168  { "I126", RDP_SCANCODE_UNKNOWN }, /* 126: I126 [plusminus] */
169  { "PAUS", RDP_SCANCODE_PAUSE }, /* 127: PAUS [Pause] */
170  { "I128", RDP_SCANCODE_LAUNCH_MEDIA_SELECT }, /* 128: I128 [XF86LaunchA] */
171  { "I129", RDP_SCANCODE_ABNT_C2 }, /* 129: I129 [KP_Decimal] */
172  { "HNGL", RDP_SCANCODE_HANGUL }, /* 130: HNGL [Hangul] */
173  { "HJCV", RDP_SCANCODE_HANJA }, /* 131: HJCV [Hangul_Hanja] */
174  { "AE13", RDP_SCANCODE_BACKSLASH_JP }, /* 132: AE13 [(null)] */
175  { "LWIN", RDP_SCANCODE_LWIN }, /* 133: LWIN [Super_L] */
176  { "RWIN", RDP_SCANCODE_RWIN }, /* 134: RWIN [Super_R] */
177  { "COMP", RDP_SCANCODE_APPS }, /* 135: COMP [Menu] */
178  { "STOP", RDP_SCANCODE_BROWSER_STOP }, /* 136: STOP [Cancel] */
179  { "AGAI", RDP_SCANCODE_UNKNOWN }, /* 137: AGAI [Redo] */
180  { "PROP", RDP_SCANCODE_UNKNOWN }, /* 138: PROP [SunProps] */
181  { "UNDO", RDP_SCANCODE_UNKNOWN }, /* 139: UNDO [Undo] */
182  { "FRNT", RDP_SCANCODE_UNKNOWN }, /* 140: FRNT [SunFront] */
183  { "COPY", RDP_SCANCODE_UNKNOWN }, /* 141: COPY [XF86Copy] */
184  { "OPEN", RDP_SCANCODE_UNKNOWN }, /* 142: OPEN [XF86Open] */
185  { "PAST", RDP_SCANCODE_UNKNOWN }, /* 143: PAST [XF86Paste] */
186  { "FIND", RDP_SCANCODE_UNKNOWN }, /* 144: FIND [Find] */
187  { "CUT", RDP_SCANCODE_UNKNOWN }, /* 145: CUT [XF86Cut] */
188  { "HELP", RDP_SCANCODE_HELP }, /* 146: HELP [Help] */
189  { "I147", RDP_SCANCODE_UNKNOWN }, /* 147: I147 [XF86MenuKB] */
190  { "I148", RDP_SCANCODE_UNKNOWN }, /* 148: I148 [XF86Calculator] */
191  { "I149", RDP_SCANCODE_UNKNOWN }, /* 149: I149 [(null)] */
192  { "I150", RDP_SCANCODE_SLEEP }, /* 150: I150 [XF86Sleep] */
193  { "I151", RDP_SCANCODE_UNKNOWN }, /* 151: I151 [XF86WakeUp] */
194  { "I152", RDP_SCANCODE_UNKNOWN }, /* 152: I152 [XF86Explorer] */
195  { "I153", RDP_SCANCODE_UNKNOWN }, /* 153: I153 [XF86Send] */
196  { "I154", RDP_SCANCODE_UNKNOWN }, /* 154: I154 [(null)] */
197  { "I155", RDP_SCANCODE_UNKNOWN }, /* 155: I155 [XF86Xfer] */
198  { "I156", RDP_SCANCODE_LAUNCH_APP1 }, /* 156: I156 [XF86Launch1] */
199  { "I157", RDP_SCANCODE_LAUNCH_APP2 }, /* 157: I157 [XF86Launch2] */
200  { "I158", RDP_SCANCODE_BROWSER_HOME }, /* 158: I158 [XF86WWW] */
201  { "I159", RDP_SCANCODE_UNKNOWN }, /* 159: I159 [XF86DOS] */
202  { "I160", RDP_SCANCODE_UNKNOWN }, /* 160: I160 [XF86ScreenSaver] */
203  { "I161", RDP_SCANCODE_UNKNOWN }, /* 161: I161 [XF86RotateWindows] */
204  { "I162", RDP_SCANCODE_UNKNOWN }, /* 162: I162 [XF86TaskPane] */
205  { "I163", RDP_SCANCODE_LAUNCH_MAIL }, /* 163: I163 [XF86Mail] */
206  { "I164", RDP_SCANCODE_BROWSER_FAVORITES }, /* 164: I164 [XF86Favorites] */
207  { "I165", RDP_SCANCODE_UNKNOWN }, /* 165: I165 [XF86MyComputer] */
208  { "I166", RDP_SCANCODE_BROWSER_BACK }, /* 166: I166 [XF86Back] */
209  { "I167", RDP_SCANCODE_BROWSER_FORWARD }, /* 167: I167 [XF86Forward] */
210  { "I168", RDP_SCANCODE_UNKNOWN }, /* 168: I168 [(null)] */
211  { "I169", RDP_SCANCODE_UNKNOWN }, /* 169: I169 [XF86Eject] */
212  { "I170", RDP_SCANCODE_UNKNOWN }, /* 170: I170 [XF86Eject] */
213  { "I171", RDP_SCANCODE_MEDIA_NEXT_TRACK }, /* 171: I171 [XF86AudioNext] */
214  { "I172", RDP_SCANCODE_MEDIA_PLAY_PAUSE }, /* 172: I172 [XF86AudioPlay] */
215  { "I173", RDP_SCANCODE_MEDIA_PREV_TRACK }, /* 173: I173 [XF86AudioPrev] */
216  { "I174", RDP_SCANCODE_MEDIA_STOP }, /* 174: I174 [XF86AudioStop] */
217  { "I175", RDP_SCANCODE_UNKNOWN }, /* 175: I175 [XF86AudioRecord] */
218  { "I176", RDP_SCANCODE_UNKNOWN }, /* 176: I176 [XF86AudioRewind] */
219  { "I177", RDP_SCANCODE_UNKNOWN }, /* 177: I177 [XF86Phone] */
220  { "I178", RDP_SCANCODE_UNKNOWN }, /* 178: I178 [(null)] */
221  { "I179", RDP_SCANCODE_UNKNOWN }, /* 179: I179 [XF86Tools] */
222  { "I180", RDP_SCANCODE_BROWSER_HOME }, /* 180: I180 [XF86HomePage] */
223  { "I181", RDP_SCANCODE_BROWSER_REFRESH }, /* 181: I181 [XF86Reload] */
224  { "I182", RDP_SCANCODE_UNKNOWN }, /* 182: I182 [XF86Close] */
225  { "I183", RDP_SCANCODE_UNKNOWN }, /* 183: I183 [(null)] */
226  { "I184", RDP_SCANCODE_UNKNOWN }, /* 184: I184 [(null)] */
227  { "I185", RDP_SCANCODE_UNKNOWN }, /* 185: I185 [XF86ScrollUp] */
228  { "I186", RDP_SCANCODE_UNKNOWN }, /* 186: I186 [XF86ScrollDown] */
229  { "I187", RDP_SCANCODE_UNKNOWN }, /* 187: I187 [parenleft] */
230  { "I188", RDP_SCANCODE_UNKNOWN }, /* 188: I188 [parenright] */
231  { "I189", RDP_SCANCODE_UNKNOWN }, /* 189: I189 [XF86New] */
232  { "I190", RDP_SCANCODE_UNKNOWN }, /* 190: I190 [Redo] */
233  { "FK13", RDP_SCANCODE_F13 }, /* 191: FK13 [XF86Tools] */
234  { "FK14", RDP_SCANCODE_F14 }, /* 192: FK14 [XF86Launch5] */
235  { "FK15", RDP_SCANCODE_F15 }, /* 193: FK15 [XF86Launch6] */
236  { "FK16", RDP_SCANCODE_F16 }, /* 194: FK16 [XF86Launch7] */
237  { "FK17", RDP_SCANCODE_F17 }, /* 195: FK17 [XF86Launch8] */
238  { "FK18", RDP_SCANCODE_F18 }, /* 196: FK18 [XF86Launch9] */
239  { "FK19", RDP_SCANCODE_F19 }, /* 197: FK19 [(null)] */
240  { "FK20", RDP_SCANCODE_F20 }, /* 198: FK20 [XF86AudioMicMute] */
241  { "FK21", RDP_SCANCODE_F21 }, /* 199: FK21 [XF86TouchpadToggle] */
242  { "FK22", RDP_SCANCODE_F22 }, /* 200: FK22 [XF86TouchpadOn] */
243  { "FK23", RDP_SCANCODE_F23 }, /* 201: FK23 [XF86TouchpadOff] */
244  { "FK24", RDP_SCANCODE_F24 }, /* 202: FK24 [(null)] */
245  { "LVL5", RDP_SCANCODE_UNKNOWN }, /* 203: LVL5 [ISO_Level5_Shift] */
246  { "ALT", RDP_SCANCODE_LMENU }, /* 204: ALT [(null)] */
247  { "META", RDP_SCANCODE_LMENU }, /* 205: META [(null)] */
248  { "SUPR", RDP_SCANCODE_LWIN }, /* 206: SUPR [(null)] */
249  { "HYPR", RDP_SCANCODE_LWIN }, /* 207: HYPR [(null)] */
250  { "I208", RDP_SCANCODE_MEDIA_PLAY_PAUSE }, /* 208: I208 [XF86AudioPlay] */
251  { "I209", RDP_SCANCODE_MEDIA_PLAY_PAUSE }, /* 209: I209 [XF86AudioPause] */
252  { "I210", RDP_SCANCODE_UNKNOWN }, /* 210: I210 [XF86Launch3] */
253  { "I211", RDP_SCANCODE_UNKNOWN }, /* 211: I211 [XF86Launch4] */
254  { "I212", RDP_SCANCODE_UNKNOWN }, /* 212: I212 [XF86LaunchB] */
255  { "I213", RDP_SCANCODE_UNKNOWN }, /* 213: I213 [XF86Suspend] */
256  { "I214", RDP_SCANCODE_UNKNOWN }, /* 214: I214 [XF86Close] */
257  { "I215", RDP_SCANCODE_MEDIA_PLAY_PAUSE }, /* 215: I215 [XF86AudioPlay] */
258  { "I216", RDP_SCANCODE_MEDIA_NEXT_TRACK }, /* 216: I216 [XF86AudioForward] */
259  { "I217", RDP_SCANCODE_UNKNOWN }, /* 217: I217 [(null)] */
260  { "I218", RDP_SCANCODE_UNKNOWN }, /* 218: I218 [Print] */
261  { "I219", RDP_SCANCODE_UNKNOWN }, /* 219: I219 [(null)] */
262  { "I220", RDP_SCANCODE_UNKNOWN }, /* 220: I220 [XF86WebCam] */
263  { "I221", RDP_SCANCODE_UNKNOWN }, /* 221: I221 [XF86AudioPreset] */
264  { "I222", RDP_SCANCODE_UNKNOWN }, /* 222: I222 [(null)] */
265  { "I223", RDP_SCANCODE_LAUNCH_MAIL }, /* 223: I223 [XF86Mail] */
266  { "I224", RDP_SCANCODE_UNKNOWN }, /* 224: I224 [XF86Messenger] */
267  { "I225", RDP_SCANCODE_BROWSER_SEARCH }, /* 225: I225 [XF86Search] */
268  { "I226", RDP_SCANCODE_UNKNOWN }, /* 226: I226 [XF86Go] */
269  { "I227", RDP_SCANCODE_UNKNOWN }, /* 227: I227 [XF86Finance] */
270  { "I228", RDP_SCANCODE_UNKNOWN }, /* 228: I228 [XF86Game] */
271  { "I229", RDP_SCANCODE_UNKNOWN }, /* 229: I229 [XF86Shop] */
272  { "I230", RDP_SCANCODE_UNKNOWN }, /* 230: I230 [(null)] */
273  { "I231", RDP_SCANCODE_UNKNOWN }, /* 231: I231 [Cancel] */
274  { "I232", RDP_SCANCODE_UNKNOWN }, /* 232: I232 [XF86MonBrightnessDown] */
275  { "I233", RDP_SCANCODE_UNKNOWN }, /* 233: I233 [XF86MonBrightnessUp] */
276  { "I234", RDP_SCANCODE_LAUNCH_MEDIA_SELECT }, /* 234: I234 [XF86AudioMedia] */
277  { "I235", RDP_SCANCODE_UNKNOWN }, /* 235: I235 [XF86Display] */
278  { "I236", RDP_SCANCODE_UNKNOWN }, /* 236: I236 [XF86KbdLightOnOff] */
279  { "I237", RDP_SCANCODE_UNKNOWN }, /* 237: I237 [XF86KbdBrightnessDown] */
280  { "I238", RDP_SCANCODE_UNKNOWN }, /* 238: I238 [XF86KbdBrightnessUp] */
281  { "I239", RDP_SCANCODE_UNKNOWN }, /* 239: I239 [XF86Send] */
282  { "I240", RDP_SCANCODE_UNKNOWN }, /* 240: I240 [XF86Reply] */
283  { "I241", RDP_SCANCODE_UNKNOWN }, /* 241: I241 [XF86MailForward] */
284  { "I242", RDP_SCANCODE_UNKNOWN }, /* 242: I242 [XF86Save] */
285  { "I243", RDP_SCANCODE_UNKNOWN }, /* 243: I243 [XF86Documents] */
286  { "I244", RDP_SCANCODE_UNKNOWN }, /* 244: I244 [XF86Battery] */
287  { "I245", RDP_SCANCODE_UNKNOWN }, /* 245: I245 [XF86Bluetooth] */
288  { "I246", RDP_SCANCODE_UNKNOWN }, /* 246: I246 [XF86WLAN] */
289  { "I247", RDP_SCANCODE_UNKNOWN }, /* 247: I247 [XF86UWB] */
290  { "I248", RDP_SCANCODE_UNKNOWN }, /* 248: I248 [(null)] */
291  { "I249", RDP_SCANCODE_UNKNOWN }, /* 249: I249 [XF86Next_VMode] */
292  { "I250", RDP_SCANCODE_UNKNOWN }, /* 250: I250 [XF86Prev_VMode] */
293  { "I251", RDP_SCANCODE_UNKNOWN }, /* 251: I251 [XF86MonBrightnessCycle] */
294  { "I252", RDP_SCANCODE_UNKNOWN }, /* 252: I252 [XF86BrightnessAuto] */
295  { "I253", RDP_SCANCODE_UNKNOWN }, /* 253: I253 [XF86DisplayOff] */
296  { "I254", RDP_SCANCODE_UNKNOWN }, /* 254: I254 [XF86WWAN] */
297  { "I255", RDP_SCANCODE_UNKNOWN } /* 255: I255 [XF86RFKill] */
298 };
299 
300 static int detect_keyboard_layout_from_xkbfile(void* display, DWORD* keyboardLayoutId);
301 static int freerdp_keyboard_load_map_from_xkbfile(void* display, DWORD* x11_keycode_to_rdp_scancode,
302  size_t count);
303 
304 static void* freerdp_keyboard_xkb_init(void)
305 {
306  int status = 0;
307 
308  Display* display = XOpenDisplay(NULL);
309 
310  if (!display)
311  return NULL;
312 
313  status = XkbQueryExtension(display, NULL, NULL, NULL, NULL, NULL);
314 
315  if (!status)
316  return NULL;
317 
318  return (void*)display;
319 }
320 
321 int freerdp_keyboard_init_xkbfile(DWORD* keyboardLayoutId, DWORD* x11_keycode_to_rdp_scancode,
322  size_t count)
323 {
324  WINPR_ASSERT(keyboardLayoutId);
325  WINPR_ASSERT(x11_keycode_to_rdp_scancode);
326  ZeroMemory(x11_keycode_to_rdp_scancode, sizeof(DWORD) * count);
327 
328  void* display = freerdp_keyboard_xkb_init();
329 
330  if (!display)
331  {
332  DEBUG_KBD("Error initializing xkb");
333  return -1;
334  }
335 
336  if (*keyboardLayoutId == 0)
337  {
338  detect_keyboard_layout_from_xkbfile(display, keyboardLayoutId);
339  DEBUG_KBD("detect_keyboard_layout_from_xkb: %" PRIu32 " (0x%08" PRIX32 ")",
340  *keyboardLayoutId, *keyboardLayoutId);
341  }
342 
343  const int rc =
344  freerdp_keyboard_load_map_from_xkbfile(display, x11_keycode_to_rdp_scancode, count);
345 
346  XCloseDisplay(display);
347 
348  return rc;
349 }
350 
351 /* return substring starting after nth comma, ending at following comma */
352 static char* comma_substring(char* s, size_t n)
353 {
354  char* p = NULL;
355 
356  if (!s)
357  return "";
358 
359  while (n-- > 0)
360  {
361  if (!(p = strchr(s, ',')))
362  break;
363 
364  s = p + 1;
365  }
366 
367  if ((p = strchr(s, ',')))
368  *p = 0;
369 
370  return s;
371 }
372 
373 int detect_keyboard_layout_from_xkbfile(void* display, DWORD* keyboardLayoutId)
374 {
375  DEBUG_KBD("display: %p", display);
376  if (!display)
377  return -2;
378 
379  char* rules = NULL;
380  XkbRF_VarDefsRec rules_names = { 0 };
381  const Bool rc = XkbRF_GetNamesProp(display, &rules, &rules_names);
382  if (!rc)
383  {
384  DEBUG_KBD("XkbRF_GetNamesProp == False");
385  }
386  else
387  {
388  DEBUG_KBD("rules: %s", rules ? rules : "");
389  DEBUG_KBD("model: %s", rules_names.model ? rules_names.model : "");
390  DEBUG_KBD("layouts: %s", rules_names.layout ? rules_names.layout : "");
391  DEBUG_KBD("variants: %s", rules_names.variant ? rules_names.variant : "");
392 
393  DWORD group = 0;
394  XkbStateRec state = { 0 };
395  XKeyboardState coreKbdState = { 0 };
396  XGetKeyboardControl(display, &coreKbdState);
397 
398  if (XkbGetState(display, XkbUseCoreKbd, &state) == Success)
399  group = state.group;
400 
401  DEBUG_KBD("group: %u", state.group);
402 
403  const char* layout = comma_substring(rules_names.layout, group);
404  const char* variant = comma_substring(rules_names.variant, group);
405 
406  DEBUG_KBD("layout: %s", layout ? layout : "");
407  DEBUG_KBD("variant: %s", variant ? variant : "");
408 
409  *keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
410  }
411  free(rules_names.model);
412  free(rules_names.layout);
413  free(rules_names.variant);
414  free(rules_names.options);
415  free(rules);
416 
417  return 0;
418 }
419 
420 static int xkb_cmp(const void* pva, const void* pvb)
421 {
422  const XKB_KEY_NAME_SCANCODE* a = pva;
423  const XKB_KEY_NAME_SCANCODE* b = pvb;
424 
425  if (!a && !b)
426  return 0;
427  if (!a)
428  return 1;
429  if (!b)
430  return -1;
431  return strcmp(a->xkb_keyname, b->xkb_keyname);
432 }
433 
434 static BOOL try_add(size_t offset, const char* xkb_keyname, DWORD* x11_keycode_to_rdp_scancode,
435 
436  size_t count)
437 {
438  static BOOL initialized = FALSE;
439  static XKB_KEY_NAME_SCANCODE copy[ARRAYSIZE(XKB_KEY_NAME_SCANCODE_TABLE)] = { 0 };
440  if (!initialized)
441  {
442  memcpy(copy, XKB_KEY_NAME_SCANCODE_TABLE, sizeof(copy));
443  qsort(copy, ARRAYSIZE(copy), sizeof(XKB_KEY_NAME_SCANCODE), xkb_cmp);
444  initialized = TRUE;
445  }
446 
447  XKB_KEY_NAME_SCANCODE key = { 0 };
448  key.xkb_keyname = xkb_keyname;
449  XKB_KEY_NAME_SCANCODE* found =
450  bsearch(&key, copy, ARRAYSIZE(copy), sizeof(XKB_KEY_NAME_SCANCODE), xkb_cmp);
451  if (found)
452  {
453  DEBUG_KBD("%4s: keycode: 0x%02" PRIuz " -> rdp scancode: 0x%08" PRIx32 "", xkb_keyname,
454  offset, found->rdp_scancode);
455  x11_keycode_to_rdp_scancode[offset] = found->rdp_scancode;
456  return TRUE;
457  }
458  return FALSE;
459 }
460 
461 int freerdp_keyboard_load_map_from_xkbfile(void* display, DWORD* x11_keycode_to_rdp_scancode,
462  size_t count)
463 {
464  int status = -1;
465 
466  if (!display)
467  return -2;
468 
469  XkbDescPtr xkb = XkbGetMap(display, 0, XkbUseCoreKbd);
470  if (!xkb)
471  {
472  DEBUG_KBD("XkbGetMap() == NULL");
473  return -3;
474  }
475 
476  if (XkbGetNames(display, XkbKeyNamesMask, xkb) != Success)
477  {
478  DEBUG_KBD("XkbGetNames() != Success");
479  }
480  else
481  {
482  char xkb_keyname[XkbKeyNameLength + 1] = { 42, 42, 42, 42,
483  0 }; /* end-of-string at index 5 */
484 
485  DEBUG_KBD("XkbGetNames() == Success, min=%" PRIu8 ", max=%" PRIu8, xkb->min_key_code,
486  xkb->max_key_code);
487  for (size_t i = xkb->min_key_code; i <= MIN(xkb->max_key_code, count); i++)
488  {
489  BOOL found = FALSE;
490  strncpy(xkb_keyname, xkb->names->keys[i].name, XkbKeyNameLength);
491 
492  DEBUG_KBD("KeyCode %" PRIuz " -> %s", i, xkb_keyname);
493  if (strnlen(xkb_keyname, ARRAYSIZE(xkb_keyname)) < 1)
494  continue;
495 
496  found = try_add(i, xkb_keyname, x11_keycode_to_rdp_scancode, count);
497 
498  if (!found)
499  {
500  DEBUG_KBD("%4s: keycode: 0x%02X -> no RDP scancode found", xkb_keyname, i);
501  }
502  else
503  status = 0;
504  }
505  }
506 
507  XkbFreeKeyboard(xkb, 0, 1);
508 
509  return status;
510 }