keyboard: Only send SDL_KEYMAPCHANGED when the keymap actually changes

This commit is contained in:
Cameron Gutman 2022-07-31 15:34:03 -05:00 committed by Sam Lantinga
parent a10c57dfe4
commit 8b438f7b51
12 changed files with 33 additions and 28 deletions

View File

@ -590,24 +590,36 @@ SDL_GetDefaultKeymap(SDL_Keycode * keymap)
} }
void void
SDL_SetKeymap(int start, SDL_Keycode * keys, int length) SDL_SetKeymap(int start, SDL_Keycode * keys, int length, SDL_bool send_event)
{ {
SDL_Keyboard *keyboard = &SDL_keyboard; SDL_Keyboard *keyboard = &SDL_keyboard;
SDL_Scancode scancode; SDL_Scancode scancode;
SDL_Keycode normalized_keymap[SDL_NUM_SCANCODES];
if (start < 0 || start + length > SDL_NUM_SCANCODES) { if (start < 0 || start + length > SDL_NUM_SCANCODES) {
return; return;
} }
SDL_memcpy(&keyboard->keymap[start], keys, sizeof(*keys) * length); SDL_memcpy(&normalized_keymap[start], keys, sizeof(*keys) * length);
/* The number key scancodes always map to the number key keycodes. /* The number key scancodes always map to the number key keycodes.
* On AZERTY layouts these technically are symbols, but users (and games) * On AZERTY layouts these technically are symbols, but users (and games)
* always think of them and view them in UI as number keys. * always think of them and view them in UI as number keys.
*/ */
keyboard->keymap[SDL_SCANCODE_0] = SDLK_0; normalized_keymap[SDL_SCANCODE_0] = SDLK_0;
for (scancode = SDL_SCANCODE_1; scancode <= SDL_SCANCODE_9; ++scancode) { for (scancode = SDL_SCANCODE_1; scancode <= SDL_SCANCODE_9; ++scancode) {
keyboard->keymap[scancode] = SDLK_1 + (scancode - SDL_SCANCODE_1); normalized_keymap[scancode] = SDLK_1 + (scancode - SDL_SCANCODE_1);
}
/* If the mapping didn't really change, we're done here */
if (!SDL_memcmp(&keyboard->keymap[start], &normalized_keymap[start], sizeof(*keys) * length)) {
return;
}
SDL_memcpy(&keyboard->keymap[start], &normalized_keymap[start], sizeof(*keys) * length);
if (send_event) {
SDL_SendKeymapChangedEvent();
} }
} }

View File

@ -33,7 +33,7 @@ extern int SDL_KeyboardInit(void);
extern void SDL_GetDefaultKeymap(SDL_Keycode * keymap); extern void SDL_GetDefaultKeymap(SDL_Keycode * keymap);
/* Set the mapping of scancode to key codes */ /* Set the mapping of scancode to key codes */
extern void SDL_SetKeymap(int start, SDL_Keycode * keys, int length); extern void SDL_SetKeymap(int start, SDL_Keycode * keys, int length, SDL_bool send_event);
/* Set a platform-dependent key name, overriding the default platform-agnostic /* Set a platform-dependent key name, overriding the default platform-agnostic
name. Encoded as UTF-8. The string is not copied, thus the pointer given to name. Encoded as UTF-8. The string is not copied, thus the pointer given to

View File

@ -36,7 +36,7 @@ void Android_InitKeyboard(void)
/* Add default scancode to key mapping */ /* Add default scancode to key mapping */
SDL_GetDefaultKeymap(keymap); SDL_GetDefaultKeymap(keymap);
SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES); SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES, SDL_FALSE);
} }
static SDL_Scancode Android_Keycodes[] = { static SDL_Scancode Android_Keycodes[] = {

View File

@ -439,10 +439,7 @@ UpdateKeymap(SDL_VideoData *data, SDL_bool send_event)
keymap[scancode] = s[0]; keymap[scancode] = s[0];
} }
} }
SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES); SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES, send_event);
if (send_event) {
SDL_SendKeymapChangedEvent();
}
return; return;
} }

View File

@ -698,9 +698,9 @@ EnumKeyboards(DFBInputDeviceID device_id,
SDL_GetDefaultKeymap(keymap); SDL_GetDefaultKeymap(keymap);
#if USE_MULTI_API #if USE_MULTI_API
SDL_SetKeymap(devdata->num_keyboard, 0, keymap, SDL_NUM_SCANCODES); SDL_SetKeymap(devdata->num_keyboard, 0, keymap, SDL_NUM_SCANCODES, SDL_FALSE);
#else #else
SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES); SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES, SDL_FALSE);
#endif #endif
devdata->num_keyboard++; devdata->num_keyboard++;

View File

@ -1111,8 +1111,7 @@ keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
WAYLAND_xkb_keymap_key_for_each(input->xkb.keymap, WAYLAND_xkb_keymap_key_for_each(input->xkb.keymap,
Wayland_keymap_iter, Wayland_keymap_iter,
&keymap); &keymap);
SDL_SetKeymap(0, keymap.keymap, SDL_NUM_SCANCODES); SDL_SetKeymap(0, keymap.keymap, SDL_NUM_SCANCODES, SDL_TRUE);
SDL_SendKeymapChangedEvent();
} }
static void static void

View File

@ -1044,8 +1044,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
#ifdef WM_INPUTLANGCHANGE #ifdef WM_INPUTLANGCHANGE
case WM_INPUTLANGCHANGE: case WM_INPUTLANGCHANGE:
{ {
WIN_UpdateKeymap(); WIN_UpdateKeymap(SDL_TRUE);
SDL_SendKeymapChangedEvent();
} }
returnCode = 1; returnCode = 1;
break; break;

View File

@ -102,7 +102,7 @@ WIN_InitKeyboard(_THIS)
data->ime_uielemsink = 0; data->ime_uielemsink = 0;
data->ime_ippasink = 0; data->ime_ippasink = 0;
WIN_UpdateKeymap(); WIN_UpdateKeymap(SDL_FALSE);
SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu"); SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Windows"); SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Windows");
@ -115,7 +115,7 @@ WIN_InitKeyboard(_THIS)
} }
void void
WIN_UpdateKeymap() WIN_UpdateKeymap(SDL_bool send_event)
{ {
int i; int i;
SDL_Scancode scancode; SDL_Scancode scancode;
@ -152,7 +152,7 @@ WIN_UpdateKeymap()
} }
} }
SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES); SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES, send_event);
} }
void void

View File

@ -24,7 +24,7 @@
#define SDL_windowskeyboard_h_ #define SDL_windowskeyboard_h_
extern void WIN_InitKeyboard(_THIS); extern void WIN_InitKeyboard(_THIS);
extern void WIN_UpdateKeymap(void); extern void WIN_UpdateKeymap(SDL_bool send_event);
extern void WIN_QuitKeyboard(_THIS); extern void WIN_QuitKeyboard(_THIS);
extern void WIN_ResetDeadKeys(void); extern void WIN_ResetDeadKeys(void);

View File

@ -842,8 +842,7 @@ X11_DispatchEvent(_THIS, XEvent *xevent)
X11_XRefreshKeyboardMapping(&xevent->xmapping); X11_XRefreshKeyboardMapping(&xevent->xmapping);
} }
X11_UpdateKeymap(_this); X11_UpdateKeymap(_this, SDL_TRUE);
SDL_SendKeymapChangedEvent();
} else if (xevent->type == PropertyNotify && videodata && videodata->windowlist) { } else if (xevent->type == PropertyNotify && videodata && videodata->windowlist) {
char* name_of_atom = X11_XGetAtomName(display, xevent->xproperty.atom); char* name_of_atom = X11_XGetAtomName(display, xevent->xproperty.atom);
@ -1470,8 +1469,7 @@ X11_DispatchEvent(_THIS, XEvent *xevent)
icon). Since it changes the XKLAVIER_STATE property, we icon). Since it changes the XKLAVIER_STATE property, we
notice and reinit our keymap here. This might not be the notice and reinit our keymap here. This might not be the
right approach, but it seems to work. */ right approach, but it seems to work. */
X11_UpdateKeymap(_this); X11_UpdateKeymap(_this, SDL_TRUE);
SDL_SendKeymapChangedEvent();
} else if (xevent->xproperty.atom == videodata->_NET_FRAME_EXTENTS) { } else if (xevent->xproperty.atom == videodata->_NET_FRAME_EXTENTS) {
Atom type; Atom type;
int format; int format;

View File

@ -394,7 +394,7 @@ X11_InitKeyboard(_THIS)
} }
} }
X11_UpdateKeymap(_this); X11_UpdateKeymap(_this, SDL_FALSE);
SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu"); SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
@ -408,7 +408,7 @@ X11_InitKeyboard(_THIS)
} }
void void
X11_UpdateKeymap(_THIS) X11_UpdateKeymap(_THIS, SDL_bool send_event)
{ {
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
int i; int i;
@ -468,7 +468,7 @@ X11_UpdateKeymap(_THIS)
} }
} }
} }
SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES); SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES, send_event);
} }
void void

View File

@ -24,7 +24,7 @@
#define SDL_x11keyboard_h_ #define SDL_x11keyboard_h_
extern int X11_InitKeyboard(_THIS); extern int X11_InitKeyboard(_THIS);
extern void X11_UpdateKeymap(_THIS); extern void X11_UpdateKeymap(_THIS, SDL_bool send_event);
extern void X11_QuitKeyboard(_THIS); extern void X11_QuitKeyboard(_THIS);
extern void X11_StartTextInput(_THIS); extern void X11_StartTextInput(_THIS);
extern void X11_StopTextInput(_THIS); extern void X11_StopTextInput(_THIS);