wayland: Handle virtual keyboards that don't fit the X mapping SDL is built around the concept of keyboards having a fixed layout with scancodes that correspond to physical keys no matter what linguistic layout is used. Virtual keyboards don't have this concept and can present an arbitrary layout of keys with arbitrary scancodes and names, which don't fit the SDL model. When one of these keyboards is encountered, it requires special handling: use the keysym of the pressed keys to derive their ANSI keyboard scancode equivalents for control keys and ASCII characters. All other characters are passed through as text events only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
diff --git a/src/events/SDL_keysym_to_scancode_c.h b/src/events/SDL_keysym_to_scancode_c.h
index da6c7e1..d2feb8c 100644
--- a/src/events/SDL_keysym_to_scancode_c.h
+++ b/src/events/SDL_keysym_to_scancode_c.h
@@ -24,6 +24,7 @@
#include "SDL_scancode.h"
+/* This function only correctly maps letters and numbers for keyboards in US QWERTY layout */
extern SDL_Scancode SDL_GetScancodeFromKeySym(Uint32 keysym, Uint32 keycode);
#endif /* SDL_keysym_to_scancode_c_h_ */
diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c
index 2d37a0e..5ccffd9 100644
--- a/src/video/wayland/SDL_waylandevents.c
+++ b/src/video/wayland/SDL_waylandevents.c
@@ -63,6 +63,7 @@
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-compose.h>
#include "../../events/imKStoUCS.h"
+#include "../../events/SDL_keysym_to_scancode_c.h"
/* Clamp the wl_seat version on older versions of libwayland. */
#if SDL_WAYLAND_CHECK_VERSION(1, 21, 0)
@@ -74,28 +75,6 @@
/* Weston uses a ratio of 10 units per scroll tick */
#define WAYLAND_WHEEL_AXIS_UNIT 10
-static const struct {
- xkb_keysym_t keysym;
- SDL_KeyCode keycode;
-} KeySymToSDLKeyCode[] = {
- { XKB_KEY_Escape, SDLK_ESCAPE },
- { XKB_KEY_Num_Lock, SDLK_NUMLOCKCLEAR },
- { XKB_KEY_Shift_L, SDLK_LSHIFT },
- { XKB_KEY_Shift_R, SDLK_RSHIFT },
- { XKB_KEY_Control_L, SDLK_LCTRL },
- { XKB_KEY_Control_R, SDLK_RCTRL },
- { XKB_KEY_Caps_Lock, SDLK_CAPSLOCK },
- { XKB_KEY_Alt_L, SDLK_LALT },
- { XKB_KEY_Alt_R, SDLK_RALT },
- { XKB_KEY_Meta_L, SDLK_LGUI },
- { XKB_KEY_Meta_R, SDLK_RGUI },
- { XKB_KEY_Super_L, SDLK_LGUI },
- { XKB_KEY_Super_R, SDLK_RGUI },
- { XKB_KEY_Hyper_L, SDLK_LGUI },
- { XKB_KEY_Hyper_R, SDLK_RGUI },
- { XKB_KEY_BackSpace, SDLK_BACKSPACE },
-};
-
struct SDL_WaylandTouchPoint {
SDL_TouchID id;
float x;
@@ -113,19 +92,6 @@ struct SDL_WaylandTouchPointList {
static struct SDL_WaylandTouchPointList touch_points = {NULL, NULL};
-static SDL_KeyCode
-Wayland_KeySymToSDLKeyCode(xkb_keysym_t keysym)
-{
- int i;
-
- for (i = 0; i < SDL_arraysize(KeySymToSDLKeyCode); ++i) {
- if (keysym == KeySymToSDLKeyCode[i].keysym) {
- return KeySymToSDLKeyCode[i].keycode;
- }
- }
- return SDLK_UNKNOWN;
-}
-
static void
touch_add(SDL_TouchID id, float x, float y, struct wl_surface *surface)
{
@@ -937,6 +903,59 @@ static const struct wl_touch_listener touch_listener = {
NULL, /* orientation */
};
+typedef struct Wayland_Keymap
+{
+ xkb_layout_index_t layout;
+ SDL_Keycode keymap[SDL_NUM_SCANCODES];
+} Wayland_Keymap;
+
+static void
+Wayland_keymap_iter(struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
+{
+ const xkb_keysym_t *syms;
+ Wayland_Keymap *sdlKeymap = (Wayland_Keymap *)data;
+ SDL_Scancode scancode;
+
+ scancode = SDL_GetScancodeFromTable(SDL_SCANCODE_TABLE_XFREE86_2, (key - 8));
+ if (scancode == SDL_SCANCODE_UNKNOWN) {
+ return;
+ }
+
+ if (WAYLAND_xkb_keymap_key_get_syms_by_level(keymap, key, sdlKeymap->layout, 0, &syms) > 0) {
+ uint32_t keycode = SDL_KeySymToUcs4(syms[0]);
+
+ if (!keycode) {
+ const SDL_Scancode sc = SDL_GetScancodeFromKeySym(syms[0], key);
+ keycode = SDL_GetDefaultKeyFromScancode(sc);
+ }
+
+ if (keycode) {
+ sdlKeymap->keymap[scancode] = keycode;
+ } else {
+ switch (scancode) {
+ case SDL_SCANCODE_RETURN:
+ sdlKeymap->keymap[scancode] = SDLK_RETURN;
+ break;
+ case SDL_SCANCODE_ESCAPE:
+ sdlKeymap->keymap[scancode] = SDLK_ESCAPE;
+ break;
+ case SDL_SCANCODE_BACKSPACE:
+ sdlKeymap->keymap[scancode] = SDLK_BACKSPACE;
+ break;
+ case SDL_SCANCODE_TAB:
+ sdlKeymap->keymap[scancode] = SDLK_TAB;
+ break;
+ case SDL_SCANCODE_DELETE:
+ sdlKeymap->keymap[scancode] = SDLK_DELETE;
+ break;
+ default:
+ sdlKeymap->keymap[scancode] = SDL_SCANCODE_TO_KEYCODE(scancode);
+ break;
+ }
+ }
+ }
+}
+
static void
keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
uint32_t format, int fd, uint32_t size)
@@ -962,9 +981,9 @@ keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
}
input->xkb.keymap = WAYLAND_xkb_keymap_new_from_string(input->display->xkb_context,
- map_str,
- XKB_KEYMAP_FORMAT_TEXT_V1,
- 0);
+ map_str,
+ XKB_KEYMAP_FORMAT_TEXT_V1,
+ 0);
munmap(map_str, size);
close(fd);
@@ -973,6 +992,16 @@ keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
return;
}
+#define GET_MOD_INDEX(mod) \
+ WAYLAND_xkb_keymap_mod_get_index(input->xkb.keymap, XKB_MOD_NAME_##mod)
+ input->xkb.idx_shift = 1 << GET_MOD_INDEX(SHIFT);
+ input->xkb.idx_ctrl = 1 << GET_MOD_INDEX(CTRL);
+ input->xkb.idx_alt = 1 << GET_MOD_INDEX(ALT);
+ input->xkb.idx_gui = 1 << GET_MOD_INDEX(LOGO);
+ input->xkb.idx_num = 1 << GET_MOD_INDEX(NUM);
+ input->xkb.idx_caps = 1 << GET_MOD_INDEX(CAPS);
+#undef GET_MOD_INDEX
+
input->xkb.state = WAYLAND_xkb_state_new(input->xkb.keymap);
if (!input->xkb.state) {
SDL_SetError("failed to create XKB state\n");
@@ -982,6 +1011,25 @@ keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
}
/*
+ * Assume that a nameless layout implies a virtual keyboard with an arbitrary layout.
+ * TODO: Use a better method of detection?
+ */
+ input->keyboard_is_virtual = WAYLAND_xkb_keymap_layout_get_name(input->xkb.keymap, 0) == NULL;
+
+ /* Update the keymap if changed. Virtual keyboards use the default keymap. */
+ if (input->xkb.current_group != XKB_GROUP_INVALID) {
+ Wayland_Keymap keymap;
+ keymap.layout = input->xkb.current_group;
+ SDL_GetDefaultKeymap(keymap.keymap);
+ if (!input->keyboard_is_virtual) {
+ WAYLAND_xkb_keymap_key_for_each(input->xkb.keymap,
+ Wayland_keymap_iter,
+ &keymap);
+ }
+ SDL_SetKeymap(0, keymap.keymap, SDL_NUM_SCANCODES, SDL_TRUE);
+ }
+
+ /*
* See https://blogs.s-osg.org/compose-key-support-weston/
* for further explanation on dead keys in Wayland.
*/
@@ -997,11 +1045,11 @@ keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
/* Set up XKB compose table */
input->xkb.compose_table = WAYLAND_xkb_compose_table_new_from_locale(input->display->xkb_context,
- locale, XKB_COMPOSE_COMPILE_NO_FLAGS);
+ locale, XKB_COMPOSE_COMPILE_NO_FLAGS);
if (input->xkb.compose_table) {
/* Set up XKB compose state */
input->xkb.compose_state = WAYLAND_xkb_compose_state_new(input->xkb.compose_table,
- XKB_COMPOSE_STATE_NO_FLAGS);
+ XKB_COMPOSE_STATE_NO_FLAGS);
if (!input->xkb.compose_state) {
SDL_SetError("could not create XKB compose state\n");
WAYLAND_xkb_compose_table_unref(input->xkb.compose_table);
@@ -1010,16 +1058,37 @@ keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
}
}
+/*
+ * Virtual keyboards can have arbitrary layouts, arbitrary scancodes/keycodes, etc...
+ * Key presses from these devices must be looked up by their keysym value.
+ */
+static SDL_Scancode
+Wayland_get_scancode_from_key(struct SDL_WaylandInput *input, uint32_t key)
+{
+ SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
+
+ if (!input->keyboard_is_virtual) {
+ scancode = SDL_GetScancodeFromTable(SDL_SCANCODE_TABLE_XFREE86_2, key - 8);
+ } else {
+ const xkb_keysym_t *syms;
+ if (WAYLAND_xkb_keymap_key_get_syms_by_level(input->xkb.keymap, key, input->xkb.current_group, 0, &syms) > 0) {
+ scancode = SDL_GetScancodeFromKeySym(syms[0], key);
+ }
+ }
+
+ return scancode;
+}
static void
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface,
struct wl_array *keys)
{
- // Caps Lock not included because it only makes sense to consider modifiers
- // that get held down, for the case where a user clicks on an unfocused
- // window with a modifier key like Shift pressed, in a situation where the
- // application handles Shift+click differently from a click
+ /* Caps Lock not included because it only makes sense to consider modifiers
+ * that get held down, for the case where a user clicks on an unfocused
+ * window with a modifier key like Shift pressed, in a situation where the
+ * application handles Shift+click differently from a click
+ */
const SDL_Scancode mod_scancodes[] = {
SDL_SCANCODE_LSHIFT,
SDL_SCANCODE_RSHIFT,
@@ -1033,7 +1102,6 @@ keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
struct SDL_WaylandInput *input = data;
SDL_WindowData *window;
uint32_t *key;
- SDL_Scancode scancode;
if (!surface) {
/* enter event for a window we've just destroyed */
@@ -1057,12 +1125,15 @@ keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
}
#endif
- wl_array_for_each(key, keys) {
- scancode = SDL_GetScancodeFromTable(SDL_SCANCODE_TABLE_XFREE86_2, *key);
- for (uint32_t i = 0; i < sizeof mod_scancodes / sizeof *mod_scancodes; ++i) {
- if (mod_scancodes[i] == scancode) {
- SDL_SendKeyboardKey(SDL_PRESSED, scancode);
- break;
+ wl_array_for_each (key, keys) {
+ const SDL_Scancode scancode = Wayland_get_scancode_from_key(input, *key + 8);
+
+ if (scancode != SDL_SCANCODE_UNKNOWN) {
+ for (uint32_t i = 0; i < sizeof mod_scancodes / sizeof *mod_scancodes; ++i) {
+ if (mod_scancodes[i] == scancode) {
+ SDL_SendKeyboardKey(SDL_PRESSED, scancode);
+ break;
+ }
}
}
}
@@ -1156,7 +1227,7 @@ keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
{
struct SDL_WaylandInput *input = data;
enum wl_keyboard_key_state state = state_w;
- uint32_t scancode = SDL_SCANCODE_UNKNOWN;
+ SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
char text[8];
SDL_bool has_text = SDL_FALSE;
SDL_bool handled_by_ime = SDL_FALSE;
@@ -1177,11 +1248,8 @@ keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
}
if (!handled_by_ime) {
- scancode = SDL_GetScancodeFromTable(SDL_SCANCODE_TABLE_XFREE86_2, key);
- if (scancode != SDL_SCANCODE_UNKNOWN) {
- SDL_SendKeyboardKey(state == WL_KEYBOARD_KEY_STATE_PRESSED ?
- SDL_PRESSED : SDL_RELEASED, scancode);
- }
+ scancode = Wayland_get_scancode_from_key(input, key + 8);
+ SDL_SendKeyboardKey(state == WL_KEYBOARD_KEY_STATE_PRESSED ? SDL_PRESSED : SDL_RELEASED, scancode);
}
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
@@ -1198,58 +1266,6 @@ keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
}
}
-typedef struct Wayland_Keymap
-{
- xkb_layout_index_t layout;
- SDL_Keycode keymap[SDL_NUM_SCANCODES];
-} Wayland_Keymap;
-
-static void
-Wayland_keymap_iter(struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
-{
- const xkb_keysym_t *syms;
- Wayland_Keymap *sdlKeymap = (Wayland_Keymap *)data;
- SDL_Scancode scancode;
-
- scancode = SDL_GetScancodeFromTable(SDL_SCANCODE_TABLE_XFREE86_2, (key - 8));
- if (scancode == SDL_SCANCODE_UNKNOWN) {
- return;
- }
-
- if (WAYLAND_xkb_keymap_key_get_syms_by_level(keymap, key, sdlKeymap->layout, 0, &syms) > 0) {
- uint32_t keycode = SDL_KeySymToUcs4(syms[0]);
-
- if (!keycode) {
- keycode = Wayland_KeySymToSDLKeyCode(syms[0]);
- }
-
- if (keycode) {
- sdlKeymap->keymap[scancode] = keycode;
- } else {
- switch (scancode) {
- case SDL_SCANCODE_RETURN:
- sdlKeymap->keymap[scancode] = SDLK_RETURN;
- break;
- case SDL_SCANCODE_ESCAPE:
- sdlKeymap->keymap[scancode] = SDLK_ESCAPE;
- break;
- case SDL_SCANCODE_BACKSPACE:
- sdlKeymap->keymap[scancode] = SDLK_BACKSPACE;
- break;
- case SDL_SCANCODE_TAB:
- sdlKeymap->keymap[scancode] = SDLK_TAB;
- break;
- case SDL_SCANCODE_DELETE:
- sdlKeymap->keymap[scancode] = SDLK_DELETE;
- break;
- default:
- sdlKeymap->keymap[scancode] = SDL_SCANCODE_TO_KEYCODE(scancode);
- break;
- }
- }
- }
-}
-
static void
keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t mods_depressed,
@@ -1258,10 +1274,21 @@ keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
{
struct SDL_WaylandInput *input = data;
Wayland_Keymap keymap;
+ const uint32_t modstate = (mods_depressed | mods_latched | mods_locked);
WAYLAND_xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
mods_locked, 0, 0, group);
+ /* Toggle the modifier states for virtual keyboards, as they may not send key presses. */
+ if (input->keyboard_is_virtual) {
+ SDL_ToggleModState(KMOD_SHIFT, modstate & input->xkb.idx_shift);
+ SDL_ToggleModState(KMOD_CTRL, modstate & input->xkb.idx_ctrl);
+ SDL_ToggleModState(KMOD_ALT, modstate & input->xkb.idx_alt);
+ SDL_ToggleModState(KMOD_GUI, modstate & input->xkb.idx_gui);
+ SDL_ToggleModState(KMOD_NUM, modstate & input->xkb.idx_num);
+ SDL_ToggleModState(KMOD_CAPS, modstate & input->xkb.idx_caps);
+ }
+
/* If a key is repeating, update the text to apply the modifier. */
if(keyboard_repeat_is_set(&input->keyboard_repeat)) {
char text[8];
@@ -1276,13 +1303,15 @@ keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
return;
}
- /* The layout changed, remap and fire an event */
+ /* The layout changed, remap and fire an event. Virtual keyboards use the default keymap. */
input->xkb.current_group = group;
keymap.layout = group;
SDL_GetDefaultKeymap(keymap.keymap);
- WAYLAND_xkb_keymap_key_for_each(input->xkb.keymap,
- Wayland_keymap_iter,
- &keymap);
+ if (!input->keyboard_is_virtual) {
+ WAYLAND_xkb_keymap_key_for_each(input->xkb.keymap,
+ Wayland_keymap_iter,
+ &keymap);
+ }
SDL_SetKeymap(0, keymap.keymap, SDL_NUM_SCANCODES, SDL_TRUE);
}
@@ -2394,7 +2423,7 @@ Wayland_display_add_input(SDL_VideoData *d, uint32_t id, uint32_t version)
input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, SDL_min(SDL_WL_SEAT_VERSION, version));
input->sx_w = wl_fixed_from_int(0);
input->sy_w = wl_fixed_from_int(0);
- input->xkb.current_group = ~0;
+ input->xkb.current_group = XKB_GROUP_INVALID;
d->input = input;
if (d->data_device_manager != NULL) {
diff --git a/src/video/wayland/SDL_waylandevents_c.h b/src/video/wayland/SDL_waylandevents_c.h
index f1f4726..aab2abf 100644
--- a/src/video/wayland/SDL_waylandevents_c.h
+++ b/src/video/wayland/SDL_waylandevents_c.h
@@ -110,6 +110,14 @@ struct SDL_WaylandInput {
/* Keyboard layout "group" */
uint32_t current_group;
+
+ /* Modifier bitshift values */
+ uint32_t idx_shift;
+ uint32_t idx_ctrl;
+ uint32_t idx_alt;
+ uint32_t idx_gui;
+ uint32_t idx_num;
+ uint32_t idx_caps;
} xkb;
/* information about axis events on current frame */
@@ -129,6 +137,7 @@ struct SDL_WaylandInput {
SDL_bool cursor_visible;
SDL_bool relative_mode_override;
SDL_bool warp_emulation_prohibited;
+ SDL_bool keyboard_is_virtual;
};
extern void Wayland_PumpEvents(_THIS);
diff --git a/src/video/wayland/SDL_waylandsym.h b/src/video/wayland/SDL_waylandsym.h
index 987bc49..8b31d8c 100644
--- a/src/video/wayland/SDL_waylandsym.h
+++ b/src/video/wayland/SDL_waylandsym.h
@@ -155,6 +155,7 @@ SDL_WAYLAND_SYM(int, xkb_keymap_key_get_syms_by_level, (struct xkb_keymap *,
SDL_WAYLAND_SYM(uint32_t, xkb_keysym_to_utf32, (xkb_keysym_t) )
SDL_WAYLAND_SYM(uint32_t, xkb_keymap_mod_get_index, (struct xkb_keymap *,
const char *) )
+SDL_WAYLAND_SYM(const char *, xkb_keymap_layout_get_name, (struct xkb_keymap*, xkb_layout_index_t))
#ifdef HAVE_LIBDECOR_H
SDL_WAYLAND_MODULE(WAYLAND_LIBDECOR)
diff --git a/src/video/x11/SDL_x11keyboard.c b/src/video/x11/SDL_x11keyboard.c
index 6f0eb6a..bad2029 100644
--- a/src/video/x11/SDL_x11keyboard.c
+++ b/src/video/x11/SDL_x11keyboard.c
@@ -45,7 +45,6 @@ static SDL_ScancodeTable scancode_set[] = {
SDL_SCANCODE_TABLE_XFREE86_2,
SDL_SCANCODE_TABLE_XVNC,
};
-/* *INDENT-ON* */ /* clang-format on */
/* This function only correctly maps letters and numbers for keyboards in US QWERTY layout */
static SDL_Scancode