FreeRDP
SDL3/sdl_kbd.cpp
1 
20 #include "sdl_kbd.hpp"
21 #include "sdl_disp.hpp"
22 #include "sdl_freerdp.hpp"
23 #include "sdl_utils.hpp"
24 #include "sdl_prefs.hpp"
25 
26 #include <SDL3/SDL_oldnames.h>
27 
28 #include <map>
29 
30 #include <freerdp/utils/string.h>
31 #include <freerdp/scancode.h>
32 
33 #include <freerdp/log.h>
34 #define TAG CLIENT_TAG("SDL.kbd")
35 
36 typedef struct
37 {
38  Uint32 sdl;
39  const char* sdl_name;
40  UINT32 rdp;
41  const char* rdp_name;
42 } scancode_entry_t;
43 
44 #define STR(x) #x
45 #define ENTRY(x, y) \
46  { \
47  x, STR(x), y, #y \
48  }
49 static const scancode_entry_t map[] = {
50  ENTRY(SDL_SCANCODE_UNKNOWN, RDP_SCANCODE_UNKNOWN),
51  ENTRY(SDL_SCANCODE_A, RDP_SCANCODE_KEY_A),
52  ENTRY(SDL_SCANCODE_B, RDP_SCANCODE_KEY_B),
53  ENTRY(SDL_SCANCODE_C, RDP_SCANCODE_KEY_C),
54  ENTRY(SDL_SCANCODE_D, RDP_SCANCODE_KEY_D),
55  ENTRY(SDL_SCANCODE_E, RDP_SCANCODE_KEY_E),
56  ENTRY(SDL_SCANCODE_F, RDP_SCANCODE_KEY_F),
57  ENTRY(SDL_SCANCODE_G, RDP_SCANCODE_KEY_G),
58  ENTRY(SDL_SCANCODE_H, RDP_SCANCODE_KEY_H),
59  ENTRY(SDL_SCANCODE_I, RDP_SCANCODE_KEY_I),
60  ENTRY(SDL_SCANCODE_J, RDP_SCANCODE_KEY_J),
61  ENTRY(SDL_SCANCODE_K, RDP_SCANCODE_KEY_K),
62  ENTRY(SDL_SCANCODE_L, RDP_SCANCODE_KEY_L),
63  ENTRY(SDL_SCANCODE_M, RDP_SCANCODE_KEY_M),
64  ENTRY(SDL_SCANCODE_N, RDP_SCANCODE_KEY_N),
65  ENTRY(SDL_SCANCODE_O, RDP_SCANCODE_KEY_O),
66  ENTRY(SDL_SCANCODE_P, RDP_SCANCODE_KEY_P),
67  ENTRY(SDL_SCANCODE_Q, RDP_SCANCODE_KEY_Q),
68  ENTRY(SDL_SCANCODE_R, RDP_SCANCODE_KEY_R),
69  ENTRY(SDL_SCANCODE_S, RDP_SCANCODE_KEY_S),
70  ENTRY(SDL_SCANCODE_T, RDP_SCANCODE_KEY_T),
71  ENTRY(SDL_SCANCODE_U, RDP_SCANCODE_KEY_U),
72  ENTRY(SDL_SCANCODE_V, RDP_SCANCODE_KEY_V),
73  ENTRY(SDL_SCANCODE_W, RDP_SCANCODE_KEY_W),
74  ENTRY(SDL_SCANCODE_X, RDP_SCANCODE_KEY_X),
75  ENTRY(SDL_SCANCODE_Y, RDP_SCANCODE_KEY_Y),
76  ENTRY(SDL_SCANCODE_Z, RDP_SCANCODE_KEY_Z),
77  ENTRY(SDL_SCANCODE_1, RDP_SCANCODE_KEY_1),
78  ENTRY(SDL_SCANCODE_2, RDP_SCANCODE_KEY_2),
79  ENTRY(SDL_SCANCODE_3, RDP_SCANCODE_KEY_3),
80  ENTRY(SDL_SCANCODE_4, RDP_SCANCODE_KEY_4),
81  ENTRY(SDL_SCANCODE_5, RDP_SCANCODE_KEY_5),
82  ENTRY(SDL_SCANCODE_6, RDP_SCANCODE_KEY_6),
83  ENTRY(SDL_SCANCODE_7, RDP_SCANCODE_KEY_7),
84  ENTRY(SDL_SCANCODE_8, RDP_SCANCODE_KEY_8),
85  ENTRY(SDL_SCANCODE_9, RDP_SCANCODE_KEY_9),
86  ENTRY(SDL_SCANCODE_0, RDP_SCANCODE_KEY_0),
87  ENTRY(SDL_SCANCODE_RETURN, RDP_SCANCODE_RETURN),
88  ENTRY(SDL_SCANCODE_ESCAPE, RDP_SCANCODE_ESCAPE),
89  ENTRY(SDL_SCANCODE_BACKSPACE, RDP_SCANCODE_BACKSPACE),
90  ENTRY(SDL_SCANCODE_TAB, RDP_SCANCODE_TAB),
91  ENTRY(SDL_SCANCODE_SPACE, RDP_SCANCODE_SPACE),
92  ENTRY(SDL_SCANCODE_MINUS, RDP_SCANCODE_OEM_MINUS),
93  ENTRY(SDL_SCANCODE_CAPSLOCK, RDP_SCANCODE_CAPSLOCK),
94  ENTRY(SDL_SCANCODE_F1, RDP_SCANCODE_F1),
95  ENTRY(SDL_SCANCODE_F2, RDP_SCANCODE_F2),
96  ENTRY(SDL_SCANCODE_F3, RDP_SCANCODE_F3),
97  ENTRY(SDL_SCANCODE_F4, RDP_SCANCODE_F4),
98  ENTRY(SDL_SCANCODE_F5, RDP_SCANCODE_F5),
99  ENTRY(SDL_SCANCODE_F6, RDP_SCANCODE_F6),
100  ENTRY(SDL_SCANCODE_F7, RDP_SCANCODE_F7),
101  ENTRY(SDL_SCANCODE_F8, RDP_SCANCODE_F8),
102  ENTRY(SDL_SCANCODE_F9, RDP_SCANCODE_F9),
103  ENTRY(SDL_SCANCODE_F10, RDP_SCANCODE_F10),
104  ENTRY(SDL_SCANCODE_F11, RDP_SCANCODE_F11),
105  ENTRY(SDL_SCANCODE_F12, RDP_SCANCODE_F12),
106  ENTRY(SDL_SCANCODE_F13, RDP_SCANCODE_F13),
107  ENTRY(SDL_SCANCODE_F14, RDP_SCANCODE_F14),
108  ENTRY(SDL_SCANCODE_F15, RDP_SCANCODE_F15),
109  ENTRY(SDL_SCANCODE_F16, RDP_SCANCODE_F16),
110  ENTRY(SDL_SCANCODE_F17, RDP_SCANCODE_F17),
111  ENTRY(SDL_SCANCODE_F18, RDP_SCANCODE_F18),
112  ENTRY(SDL_SCANCODE_F19, RDP_SCANCODE_F19),
113  ENTRY(SDL_SCANCODE_F20, RDP_SCANCODE_F20),
114  ENTRY(SDL_SCANCODE_F21, RDP_SCANCODE_F21),
115  ENTRY(SDL_SCANCODE_F22, RDP_SCANCODE_F22),
116  ENTRY(SDL_SCANCODE_F23, RDP_SCANCODE_F23),
117  ENTRY(SDL_SCANCODE_F24, RDP_SCANCODE_F24),
118  ENTRY(SDL_SCANCODE_NUMLOCKCLEAR, RDP_SCANCODE_NUMLOCK),
119  ENTRY(SDL_SCANCODE_KP_DIVIDE, RDP_SCANCODE_DIVIDE),
120  ENTRY(SDL_SCANCODE_KP_MULTIPLY, RDP_SCANCODE_MULTIPLY),
121  ENTRY(SDL_SCANCODE_KP_MINUS, RDP_SCANCODE_SUBTRACT),
122  ENTRY(SDL_SCANCODE_KP_PLUS, RDP_SCANCODE_ADD),
123  ENTRY(SDL_SCANCODE_KP_ENTER, RDP_SCANCODE_RETURN_KP),
124  ENTRY(SDL_SCANCODE_KP_1, RDP_SCANCODE_NUMPAD1),
125  ENTRY(SDL_SCANCODE_KP_2, RDP_SCANCODE_NUMPAD2),
126  ENTRY(SDL_SCANCODE_KP_3, RDP_SCANCODE_NUMPAD3),
127  ENTRY(SDL_SCANCODE_KP_4, RDP_SCANCODE_NUMPAD4),
128  ENTRY(SDL_SCANCODE_KP_5, RDP_SCANCODE_NUMPAD5),
129  ENTRY(SDL_SCANCODE_KP_6, RDP_SCANCODE_NUMPAD6),
130  ENTRY(SDL_SCANCODE_KP_7, RDP_SCANCODE_NUMPAD7),
131  ENTRY(SDL_SCANCODE_KP_8, RDP_SCANCODE_NUMPAD8),
132  ENTRY(SDL_SCANCODE_KP_9, RDP_SCANCODE_NUMPAD9),
133  ENTRY(SDL_SCANCODE_KP_0, RDP_SCANCODE_NUMPAD0),
134  ENTRY(SDL_SCANCODE_KP_PERIOD, RDP_SCANCODE_OEM_PERIOD),
135  ENTRY(SDL_SCANCODE_LCTRL, RDP_SCANCODE_LCONTROL),
136  ENTRY(SDL_SCANCODE_LSHIFT, RDP_SCANCODE_LSHIFT),
137  ENTRY(SDL_SCANCODE_LALT, RDP_SCANCODE_LMENU),
138  ENTRY(SDL_SCANCODE_LGUI, RDP_SCANCODE_LWIN),
139  ENTRY(SDL_SCANCODE_RCTRL, RDP_SCANCODE_RCONTROL),
140  ENTRY(SDL_SCANCODE_RSHIFT, RDP_SCANCODE_RSHIFT),
141  ENTRY(SDL_SCANCODE_RALT, RDP_SCANCODE_RMENU),
142  ENTRY(SDL_SCANCODE_RGUI, RDP_SCANCODE_RWIN),
143  ENTRY(SDL_SCANCODE_MODE, RDP_SCANCODE_APPS),
144  ENTRY(SDL_SCANCODE_MUTE, RDP_SCANCODE_VOLUME_MUTE),
145  ENTRY(SDL_SCANCODE_VOLUMEUP, RDP_SCANCODE_VOLUME_UP),
146  ENTRY(SDL_SCANCODE_VOLUMEDOWN, RDP_SCANCODE_VOLUME_DOWN),
147  ENTRY(SDL_SCANCODE_GRAVE, RDP_SCANCODE_OEM_3),
148  ENTRY(SDL_SCANCODE_COMMA, RDP_SCANCODE_OEM_COMMA),
149  ENTRY(SDL_SCANCODE_PERIOD, RDP_SCANCODE_OEM_PERIOD),
150  ENTRY(SDL_SCANCODE_SLASH, RDP_SCANCODE_OEM_2),
151  ENTRY(SDL_SCANCODE_BACKSLASH, RDP_SCANCODE_OEM_5),
152  ENTRY(SDL_SCANCODE_SCROLLLOCK, RDP_SCANCODE_SCROLLLOCK),
153  ENTRY(SDL_SCANCODE_INSERT, RDP_SCANCODE_INSERT),
154  ENTRY(SDL_SCANCODE_PRINTSCREEN, RDP_SCANCODE_PRINTSCREEN),
155  ENTRY(SDL_SCANCODE_HOME, RDP_SCANCODE_HOME),
156  ENTRY(SDL_SCANCODE_DELETE, RDP_SCANCODE_DELETE),
157  ENTRY(SDL_SCANCODE_RIGHT, RDP_SCANCODE_RIGHT),
158  ENTRY(SDL_SCANCODE_LEFT, RDP_SCANCODE_LEFT),
159  ENTRY(SDL_SCANCODE_DOWN, RDP_SCANCODE_DOWN),
160  ENTRY(SDL_SCANCODE_UP, RDP_SCANCODE_UP),
161  ENTRY(SDL_SCANCODE_SEMICOLON, RDP_SCANCODE_OEM_1),
162  ENTRY(SDL_SCANCODE_PAUSE, RDP_SCANCODE_PAUSE),
163  ENTRY(SDL_SCANCODE_PAGEUP, RDP_SCANCODE_PRIOR),
164  ENTRY(SDL_SCANCODE_END, RDP_SCANCODE_END),
165  ENTRY(SDL_SCANCODE_PAGEDOWN, RDP_SCANCODE_NEXT),
166  ENTRY(SDL_SCANCODE_MEDIA_NEXT_TRACK, RDP_SCANCODE_MEDIA_NEXT_TRACK),
167  ENTRY(SDL_SCANCODE_MEDIA_PREVIOUS_TRACK, RDP_SCANCODE_MEDIA_PREV_TRACK),
168  ENTRY(SDL_SCANCODE_MEDIA_STOP, RDP_SCANCODE_MEDIA_STOP),
169  ENTRY(SDL_SCANCODE_MEDIA_PLAY, RDP_SCANCODE_MEDIA_PLAY_PAUSE),
170  ENTRY(SDL_SCANCODE_MUTE, RDP_SCANCODE_VOLUME_MUTE),
171  ENTRY(SDL_SCANCODE_MEDIA_SELECT, RDP_SCANCODE_LAUNCH_MEDIA_SELECT),
172  ENTRY(SDL_SCANCODE_SYSREQ, RDP_SCANCODE_SYSREQ),
173  ENTRY(SDL_SCANCODE_LEFTBRACKET, RDP_SCANCODE_OEM_4),
174  ENTRY(SDL_SCANCODE_RIGHTBRACKET, RDP_SCANCODE_OEM_6),
175  ENTRY(SDL_SCANCODE_APOSTROPHE, RDP_SCANCODE_OEM_7),
176  ENTRY(SDL_SCANCODE_NONUSBACKSLASH, RDP_SCANCODE_OEM_102),
177  ENTRY(SDL_SCANCODE_SLEEP, RDP_SCANCODE_SLEEP),
178  ENTRY(SDL_SCANCODE_EQUALS, RDP_SCANCODE_OEM_PLUS),
179  ENTRY(SDL_SCANCODE_KP_COMMA, RDP_SCANCODE_DECIMAL),
180  ENTRY(SDL_SCANCODE_FIND, RDP_SCANCODE_BROWSER_SEARCH),
181  ENTRY(SDL_SCANCODE_RETURN2, RDP_SCANCODE_RETURN_KP),
182  ENTRY(SDL_SCANCODE_AC_SEARCH, RDP_SCANCODE_BROWSER_SEARCH),
183  ENTRY(SDL_SCANCODE_AC_HOME, RDP_SCANCODE_BROWSER_HOME),
184  ENTRY(SDL_SCANCODE_AC_BACK, RDP_SCANCODE_BROWSER_BACK),
185  ENTRY(SDL_SCANCODE_AC_FORWARD, RDP_SCANCODE_BROWSER_FORWARD),
186  ENTRY(SDL_SCANCODE_AC_STOP, RDP_SCANCODE_BROWSER_STOP),
187 
188 #if 1 // TODO: unmapped
189  ENTRY(SDL_SCANCODE_NONUSHASH, RDP_SCANCODE_UNKNOWN),
190  ENTRY(SDL_SCANCODE_APPLICATION, RDP_SCANCODE_UNKNOWN),
191  ENTRY(SDL_SCANCODE_POWER, RDP_SCANCODE_UNKNOWN),
192  ENTRY(SDL_SCANCODE_KP_EQUALS, RDP_SCANCODE_UNKNOWN),
193  ENTRY(SDL_SCANCODE_EXECUTE, RDP_SCANCODE_UNKNOWN),
194  ENTRY(SDL_SCANCODE_HELP, RDP_SCANCODE_UNKNOWN),
195  ENTRY(SDL_SCANCODE_MENU, RDP_SCANCODE_UNKNOWN),
196  ENTRY(SDL_SCANCODE_SELECT, RDP_SCANCODE_UNKNOWN),
197  ENTRY(SDL_SCANCODE_STOP, RDP_SCANCODE_UNKNOWN),
198  ENTRY(SDL_SCANCODE_AGAIN, RDP_SCANCODE_UNKNOWN),
199  ENTRY(SDL_SCANCODE_UNDO, RDP_SCANCODE_UNKNOWN),
200  ENTRY(SDL_SCANCODE_CUT, RDP_SCANCODE_UNKNOWN),
201  ENTRY(SDL_SCANCODE_COPY, RDP_SCANCODE_UNKNOWN),
202  ENTRY(SDL_SCANCODE_PASTE, RDP_SCANCODE_UNKNOWN),
203  ENTRY(SDL_SCANCODE_KP_EQUALSAS400, RDP_SCANCODE_UNKNOWN),
204  ENTRY(SDL_SCANCODE_INTERNATIONAL1, RDP_SCANCODE_UNKNOWN),
205  ENTRY(SDL_SCANCODE_INTERNATIONAL2, RDP_SCANCODE_UNKNOWN),
206  ENTRY(SDL_SCANCODE_INTERNATIONAL3, RDP_SCANCODE_UNKNOWN),
207  ENTRY(SDL_SCANCODE_INTERNATIONAL4, RDP_SCANCODE_UNKNOWN),
208  ENTRY(SDL_SCANCODE_INTERNATIONAL5, RDP_SCANCODE_UNKNOWN),
209  ENTRY(SDL_SCANCODE_INTERNATIONAL6, RDP_SCANCODE_UNKNOWN),
210  ENTRY(SDL_SCANCODE_INTERNATIONAL7, RDP_SCANCODE_UNKNOWN),
211  ENTRY(SDL_SCANCODE_INTERNATIONAL8, RDP_SCANCODE_UNKNOWN),
212  ENTRY(SDL_SCANCODE_INTERNATIONAL9, RDP_SCANCODE_UNKNOWN),
213  ENTRY(SDL_SCANCODE_LANG1, RDP_SCANCODE_UNKNOWN),
214  ENTRY(SDL_SCANCODE_LANG2, RDP_SCANCODE_UNKNOWN),
215  ENTRY(SDL_SCANCODE_LANG3, RDP_SCANCODE_UNKNOWN),
216  ENTRY(SDL_SCANCODE_LANG4, RDP_SCANCODE_UNKNOWN),
217  ENTRY(SDL_SCANCODE_LANG5, RDP_SCANCODE_UNKNOWN),
218  ENTRY(SDL_SCANCODE_LANG6, RDP_SCANCODE_UNKNOWN),
219  ENTRY(SDL_SCANCODE_LANG7, RDP_SCANCODE_UNKNOWN),
220  ENTRY(SDL_SCANCODE_LANG8, RDP_SCANCODE_UNKNOWN),
221  ENTRY(SDL_SCANCODE_LANG9, RDP_SCANCODE_UNKNOWN),
222  ENTRY(SDL_SCANCODE_ALTERASE, RDP_SCANCODE_UNKNOWN),
223  ENTRY(SDL_SCANCODE_CANCEL, RDP_SCANCODE_UNKNOWN),
224  ENTRY(SDL_SCANCODE_CLEAR, RDP_SCANCODE_UNKNOWN),
225  ENTRY(SDL_SCANCODE_PRIOR, RDP_SCANCODE_UNKNOWN),
226  ENTRY(SDL_SCANCODE_SEPARATOR, RDP_SCANCODE_UNKNOWN),
227  ENTRY(SDL_SCANCODE_OUT, RDP_SCANCODE_UNKNOWN),
228  ENTRY(SDL_SCANCODE_OPER, RDP_SCANCODE_UNKNOWN),
229  ENTRY(SDL_SCANCODE_CLEARAGAIN, RDP_SCANCODE_UNKNOWN),
230  ENTRY(SDL_SCANCODE_CRSEL, RDP_SCANCODE_UNKNOWN),
231  ENTRY(SDL_SCANCODE_EXSEL, RDP_SCANCODE_UNKNOWN),
232  ENTRY(SDL_SCANCODE_KP_00, RDP_SCANCODE_UNKNOWN),
233  ENTRY(SDL_SCANCODE_KP_000, RDP_SCANCODE_UNKNOWN),
234  ENTRY(SDL_SCANCODE_THOUSANDSSEPARATOR, RDP_SCANCODE_UNKNOWN),
235  ENTRY(SDL_SCANCODE_DECIMALSEPARATOR, RDP_SCANCODE_UNKNOWN),
236  ENTRY(SDL_SCANCODE_CURRENCYUNIT, RDP_SCANCODE_UNKNOWN),
237  ENTRY(SDL_SCANCODE_CURRENCYSUBUNIT, RDP_SCANCODE_UNKNOWN),
238  ENTRY(SDL_SCANCODE_KP_LEFTPAREN, RDP_SCANCODE_UNKNOWN),
239  ENTRY(SDL_SCANCODE_KP_RIGHTPAREN, RDP_SCANCODE_UNKNOWN),
240  ENTRY(SDL_SCANCODE_KP_LEFTBRACE, RDP_SCANCODE_UNKNOWN),
241  ENTRY(SDL_SCANCODE_KP_RIGHTBRACE, RDP_SCANCODE_UNKNOWN),
242  ENTRY(SDL_SCANCODE_KP_TAB, RDP_SCANCODE_UNKNOWN),
243  ENTRY(SDL_SCANCODE_KP_BACKSPACE, RDP_SCANCODE_UNKNOWN),
244  ENTRY(SDL_SCANCODE_KP_A, RDP_SCANCODE_UNKNOWN),
245  ENTRY(SDL_SCANCODE_KP_B, RDP_SCANCODE_UNKNOWN),
246  ENTRY(SDL_SCANCODE_KP_C, RDP_SCANCODE_UNKNOWN),
247  ENTRY(SDL_SCANCODE_KP_D, RDP_SCANCODE_UNKNOWN),
248  ENTRY(SDL_SCANCODE_KP_E, RDP_SCANCODE_UNKNOWN),
249  ENTRY(SDL_SCANCODE_KP_F, RDP_SCANCODE_UNKNOWN),
250  ENTRY(SDL_SCANCODE_KP_XOR, RDP_SCANCODE_UNKNOWN),
251  ENTRY(SDL_SCANCODE_KP_POWER, RDP_SCANCODE_UNKNOWN),
252  ENTRY(SDL_SCANCODE_KP_PERCENT, RDP_SCANCODE_UNKNOWN),
253  ENTRY(SDL_SCANCODE_KP_LESS, RDP_SCANCODE_UNKNOWN),
254  ENTRY(SDL_SCANCODE_KP_GREATER, RDP_SCANCODE_UNKNOWN),
255  ENTRY(SDL_SCANCODE_KP_AMPERSAND, RDP_SCANCODE_UNKNOWN),
256  ENTRY(SDL_SCANCODE_KP_DBLAMPERSAND, RDP_SCANCODE_UNKNOWN),
257  ENTRY(SDL_SCANCODE_KP_VERTICALBAR, RDP_SCANCODE_UNKNOWN),
258  ENTRY(SDL_SCANCODE_KP_DBLVERTICALBAR, RDP_SCANCODE_UNKNOWN),
259  ENTRY(SDL_SCANCODE_KP_COLON, RDP_SCANCODE_UNKNOWN),
260  ENTRY(SDL_SCANCODE_KP_HASH, RDP_SCANCODE_UNKNOWN),
261  ENTRY(SDL_SCANCODE_KP_SPACE, RDP_SCANCODE_UNKNOWN),
262  ENTRY(SDL_SCANCODE_KP_AT, RDP_SCANCODE_UNKNOWN),
263  ENTRY(SDL_SCANCODE_KP_EXCLAM, RDP_SCANCODE_UNKNOWN),
264  ENTRY(SDL_SCANCODE_KP_MEMSTORE, RDP_SCANCODE_UNKNOWN),
265  ENTRY(SDL_SCANCODE_KP_MEMRECALL, RDP_SCANCODE_UNKNOWN),
266  ENTRY(SDL_SCANCODE_KP_MEMCLEAR, RDP_SCANCODE_UNKNOWN),
267  ENTRY(SDL_SCANCODE_KP_MEMADD, RDP_SCANCODE_UNKNOWN),
268  ENTRY(SDL_SCANCODE_KP_MEMSUBTRACT, RDP_SCANCODE_UNKNOWN),
269  ENTRY(SDL_SCANCODE_KP_MEMMULTIPLY, RDP_SCANCODE_UNKNOWN),
270  ENTRY(SDL_SCANCODE_KP_MEMDIVIDE, RDP_SCANCODE_UNKNOWN),
271  ENTRY(SDL_SCANCODE_KP_PLUSMINUS, RDP_SCANCODE_UNKNOWN),
272  ENTRY(SDL_SCANCODE_KP_CLEAR, RDP_SCANCODE_UNKNOWN),
273  ENTRY(SDL_SCANCODE_KP_CLEARENTRY, RDP_SCANCODE_UNKNOWN),
274  ENTRY(SDL_SCANCODE_KP_BINARY, RDP_SCANCODE_UNKNOWN),
275  ENTRY(SDL_SCANCODE_KP_OCTAL, RDP_SCANCODE_UNKNOWN),
276  ENTRY(SDL_SCANCODE_KP_DECIMAL, RDP_SCANCODE_UNKNOWN),
277  ENTRY(SDL_SCANCODE_KP_HEXADECIMAL, RDP_SCANCODE_UNKNOWN),
278  ENTRY(SDL_SCANCODE_AC_REFRESH, RDP_SCANCODE_UNKNOWN),
279  ENTRY(SDL_SCANCODE_AC_BOOKMARKS, RDP_SCANCODE_UNKNOWN),
280  ENTRY(SDL_SCANCODE_MEDIA_EJECT, RDP_SCANCODE_UNKNOWN),
281  ENTRY(SDL_SCANCODE_MEDIA_REWIND, RDP_SCANCODE_UNKNOWN),
282  ENTRY(SDL_SCANCODE_MEDIA_FAST_FORWARD, RDP_SCANCODE_UNKNOWN)
283 #endif
284 };
285 
286 static UINT32 sdl_get_kbd_flags()
287 {
288  UINT32 flags = 0;
289 
290  SDL_Keymod mod = SDL_GetModState();
291  if ((mod & SDL_KMOD_NUM) != 0)
292  flags |= KBD_SYNC_NUM_LOCK;
293  if ((mod & SDL_KMOD_CAPS) != 0)
294  flags |= KBD_SYNC_CAPS_LOCK;
295  if ((mod & SDL_KMOD_SCROLL) != 0)
296  flags |= KBD_SYNC_SCROLL_LOCK;
297 
298  // TODO: KBD_SYNC_KANA_LOCK
299 
300  return flags;
301 }
302 
303 BOOL sdlInput::keyboard_sync_state()
304 {
305  const UINT32 syncFlags = sdl_get_kbd_flags();
306  return freerdp_input_send_synchronize_event(_sdl->context()->input, syncFlags);
307 }
308 
309 BOOL sdlInput::keyboard_focus_in()
310 {
311  auto input = _sdl->context()->input;
312  WINPR_ASSERT(input);
313 
314  auto syncFlags = sdl_get_kbd_flags();
315  freerdp_input_send_focus_in_event(input, syncFlags);
316 
317  /* finish with a mouse pointer position like mstsc.exe if required */
318 #if 0
319  if (xfc->remote_app)
320  return;
321 
322  if (XQueryPointer(xfc->display, xfc->window->handle, &w, &w, &d, &d, &x, &y, &state))
323  {
324  if ((x >= 0) && (x < xfc->window->width) && (y >= 0) && (y < xfc->window->height))
325  {
326  xf_event_adjust_coordinates(xfc, &x, &y);
327  freerdp_client_send_button_event(&xfc->common, FALSE, PTR_FLAGS_MOVE, x, y);
328  }
329  }
330 #endif
331  return TRUE;
332 }
333 
334 /* This function is called to update the keyboard indicator LED */
335 BOOL sdlInput::keyboard_set_indicators(rdpContext* context, UINT16 led_flags)
336 {
337  WINPR_UNUSED(context);
338 
339  int state = SDL_KMOD_NONE;
340 
341  if ((led_flags & KBD_SYNC_NUM_LOCK) != 0)
342  state |= SDL_KMOD_NUM;
343  if ((led_flags & KBD_SYNC_CAPS_LOCK) != 0)
344  state |= SDL_KMOD_CAPS;
345  if ((led_flags & KBD_SYNC_SCROLL_LOCK) != 0)
346  state |= SDL_KMOD_SCROLL;
347 
348  // TODO: KBD_SYNC_KANA_LOCK
349 
350  SDL_SetModState(static_cast<SDL_Keymod>(state));
351 
352  return TRUE;
353 }
354 
355 /* This function is called to set the IME state */
356 BOOL sdlInput::keyboard_set_ime_status(rdpContext* context, UINT16 imeId, UINT32 imeState,
357  UINT32 imeConvMode)
358 {
359  if (!context)
360  return FALSE;
361 
362  WLog_WARN(TAG,
363  "KeyboardSetImeStatus(unitId=%04" PRIx16 ", imeState=%08" PRIx32
364  ", imeConvMode=%08" PRIx32 ") ignored",
365  imeId, imeState, imeConvMode);
366  return TRUE;
367 }
368 
369 uint32_t sdlInput::prefToMask()
370 {
371  const std::map<std::string, uint32_t> mapping = {
372  { "KMOD_LSHIFT", SDL_KMOD_LSHIFT }, { "KMOD_RSHIFT", SDL_KMOD_RSHIFT },
373  { "KMOD_LCTRL", SDL_KMOD_LCTRL }, { "KMOD_RCTRL", SDL_KMOD_RCTRL },
374  { "KMOD_LALT", SDL_KMOD_LALT }, { "KMOD_RALT", SDL_KMOD_RALT },
375  { "KMOD_LGUI", SDL_KMOD_LGUI }, { "KMOD_RGUI", SDL_KMOD_RGUI },
376  { "KMOD_NUM", SDL_KMOD_NUM }, { "KMOD_CAPS", SDL_KMOD_CAPS },
377  { "KMOD_MODE", SDL_KMOD_MODE }, { "KMOD_SCROLL", SDL_KMOD_SCROLL },
378  { "KMOD_CTRL", SDL_KMOD_CTRL }, { "KMOD_SHIFT", SDL_KMOD_SHIFT },
379  { "KMOD_ALT", SDL_KMOD_ALT }, { "KMOD_GUI", SDL_KMOD_GUI }
380  };
381  uint32_t mod = SDL_KMOD_NONE;
382  for (const auto& val : SdlPref::instance()->get_array("SDL_KeyModMask", { "KMOD_RSHIFT" }))
383  {
384  auto it = mapping.find(val);
385  if (it != mapping.end())
386  {
387  mod |= it->second;
388  }
389  }
390  return mod;
391 }
392 
393 static const char* sdl_scancode_name(Uint32 scancode)
394 {
395  for (const auto& cur : map)
396  {
397  if (cur.sdl == scancode)
398  return cur.sdl_name;
399  }
400 
401  return "SDL_SCANCODE_UNKNOWN";
402 }
403 
404 static Uint32 sdl_scancode_val(const char* scancodeName)
405 {
406  for (const auto& cur : map)
407  {
408  if (strcmp(cur.sdl_name, scancodeName) == 0)
409  return cur.sdl;
410  }
411 
412  return SDL_SCANCODE_UNKNOWN;
413 }
414 
415 static const char* sdl_rdp_scancode_name(UINT32 scancode)
416 {
417  for (const auto& cur : map)
418  {
419  if (cur.rdp == scancode)
420  return cur.rdp_name;
421  }
422 
423  return "RDP_SCANCODE_UNKNOWN";
424 }
425 
426 static UINT32 sdl_rdp_scancode_val(const char* scancodeName)
427 {
428  for (const auto& cur : map)
429  {
430  if (strcmp(cur.rdp_name, scancodeName) == 0)
431  return cur.rdp;
432  }
433 
434  return RDP_SCANCODE_UNKNOWN;
435 }
436 
437 static UINT32 sdl_scancode_to_rdp(Uint32 scancode)
438 {
439  UINT32 rdp = RDP_SCANCODE_UNKNOWN;
440 
441  for (const auto& cur : map)
442  {
443  if (cur.sdl == scancode)
444  {
445  rdp = cur.rdp;
446  break;
447  }
448  }
449 
450 #if defined(WITH_DEBUG_SDL_KBD_EVENTS)
451  auto code = static_cast<SDL_Scancode>(scancode);
452  WLog_DBG(TAG, "got %s [%s] -> [%s]", SDL_GetScancodeName(code), sdl_scancode_name(scancode),
453  sdl_rdp_scancode_name(rdp));
454 #endif
455  return rdp;
456 }
457 
458 uint32_t sdlInput::prefKeyValue(const std::string& key, uint32_t fallback)
459 {
460  auto item = SdlPref::instance()->get_string(key);
461  if (item.empty())
462  return fallback;
463  auto val = sdl_scancode_val(item.c_str());
464  if (val == SDL_SCANCODE_UNKNOWN)
465  return fallback;
466  return val;
467 }
468 
469 std::list<std::string> sdlInput::tokenize(const std::string& data, const std::string& delimiter)
470 {
471  size_t lastpos = 0;
472  size_t pos = 0;
473  std::list<std::string> list;
474  while ((pos = data.find(delimiter, lastpos)) != std::string::npos)
475  {
476  auto token = data.substr(lastpos, pos);
477  lastpos = pos + 1;
478  list.push_back(token);
479  }
480  auto token = data.substr(lastpos);
481  list.push_back(token);
482  return list;
483 }
484 
485 bool sdlInput::extract(const std::string& token, uint32_t& key, uint32_t& value)
486 {
487  return freerdp_extract_key_value(token.c_str(), &key, &value);
488 }
489 
490 uint32_t sdlInput::remapScancode(uint32_t scancode)
491 {
492  if (!_remapInitialized.exchange(true))
493  remapInitialize();
494  auto it = _remapList.find(scancode);
495  if (it != _remapList.end())
496  return it->second;
497  return scancode;
498 }
499 
500 void sdlInput::remapInitialize()
501 {
502  WINPR_ASSERT(_sdl);
503 
504  auto context = _sdl->context();
505  WINPR_ASSERT(context);
506  auto KeyboardRemappingList =
507  freerdp_settings_get_string(context->settings, FreeRDP_KeyboardRemappingList);
508  if (!KeyboardRemappingList)
509  return;
510 
511  auto list = tokenize(KeyboardRemappingList);
512  for (auto& token : list)
513  {
514  uint32_t key = 0;
515  uint32_t value = 0;
516  if (!extract(token, key, value))
517  continue;
518  _remapList.emplace(key, value);
519  }
520 }
521 
522 BOOL sdlInput::keyboard_handle_event(const SDL_KeyboardEvent* ev)
523 {
524  WINPR_ASSERT(ev);
525  const UINT32 rdp_scancode = sdl_scancode_to_rdp(ev->scancode);
526  const SDL_Keymod mods = SDL_GetModState();
527 
528  if ((mods & _hotkeyModmask) == _hotkeyModmask)
529  {
530  if (ev->type == SDL_EVENT_KEY_DOWN)
531  {
532  if (ev->scancode == _hotkeyFullscreen)
533  {
534  _sdl->update_fullscreen(!_sdl->fullscreen);
535  return TRUE;
536  }
537  if (ev->scancode == _hotkeyResizable)
538  {
539  _sdl->update_resizeable(!_sdl->resizeable);
540  return TRUE;
541  }
542 
543  if (ev->scancode == _hotkeyGrab)
544  {
545  _sdl->grab_kbd_enabled = !_sdl->grab_kbd_enabled;
546  keyboard_grab(ev->windowID, _sdl->grab_kbd);
547  return TRUE;
548  }
549  if (ev->scancode == _hotkeyDisconnect)
550  {
551  freerdp_abort_connect_context(_sdl->context());
552  return TRUE;
553  }
554  if (ev->scancode == _hotkeyMinimize)
555  {
556  _sdl->update_minimize();
557  return TRUE;
558  }
559  }
560  }
561 
562  auto scancode = remapScancode(rdp_scancode);
563  return freerdp_input_send_keyboard_event_ex(
564  _sdl->context()->input, ev->type == SDL_EVENT_KEY_DOWN, ev->repeat, scancode);
565 }
566 
567 BOOL sdlInput::keyboard_grab(Uint32 windowID, bool enable)
568 {
569  auto it = _sdl->windows.find(windowID);
570  if (it == _sdl->windows.end())
571  return FALSE;
572  auto status = enable && _sdl->grab_kbd_enabled;
573  _sdl->grab_kbd = status;
574  return it->second.grabKeyboard(status);
575 }
576 
577 BOOL sdlInput::mouse_focus(Uint32 windowID)
578 {
579  if (_lastWindowID != windowID)
580  {
581  _lastWindowID = windowID;
582  auto it = _sdl->windows.find(windowID);
583  if (it == _sdl->windows.end())
584  return FALSE;
585 
586  it->second.raise();
587  }
588  return TRUE;
589 }
590 
591 BOOL sdlInput::mouse_grab(Uint32 windowID, bool enable)
592 {
593  auto it = _sdl->windows.find(windowID);
594  if (it == _sdl->windows.end())
595  return FALSE;
596  _sdl->grab_mouse = enable;
597  return it->second.grabMouse(enable);
598 }
599 
600 sdlInput::sdlInput(SdlContext* sdl)
601  : _sdl(sdl), _lastWindowID(UINT32_MAX), _hotkeyModmask(prefToMask())
602 {
603 
604  _hotkeyFullscreen = prefKeyValue("SDL_Fullscreen", SDL_SCANCODE_RETURN);
605  _hotkeyResizable = prefKeyValue("SDL_Resizeable", SDL_SCANCODE_R);
606  _hotkeyGrab = prefKeyValue("SDL_Grab", SDL_SCANCODE_G);
607  _hotkeyDisconnect = prefKeyValue("SDL_Disconnect", SDL_SCANCODE_D);
608  _hotkeyMinimize = prefKeyValue("SDL_Minimize", SDL_SCANCODE_M);
609 }
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.