SDL: Use the correct device ID (global or default or specific) based on context

From 35756593d6790cfca9914a41f0148c21af16e0ef Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 22 Mar 2024 10:49:07 -0700
Subject: [PATCH] Use the correct device ID (global or default or specific)
 based on context

---
 src/core/haiku/SDL_BApp.h                   | 12 +++---
 src/core/winrt/SDL_winrtapp_direct3d.cpp    |  6 +--
 src/events/SDL_keyboard.c                   | 19 +++++----
 src/events/SDL_keyboard_c.h                 |  6 +++
 src/events/SDL_mouse.c                      | 16 +++++--
 src/events/SDL_mouse_c.h                    |  6 +++
 src/joystick/android/SDL_sysjoystick.c      |  4 +-
 src/video/android/SDL_androidkeyboard.c     |  4 +-
 src/video/android/SDL_androidmouse.c        | 12 +++---
 src/video/android/SDL_androidvideo.c        |  2 -
 src/video/android/SDL_androidvideo.h        |  2 -
 src/video/cocoa/SDL_cocoakeyboard.m         |  9 ++--
 src/video/cocoa/SDL_cocoamouse.m            |  8 ++--
 src/video/cocoa/SDL_cocoavideo.h            |  2 -
 src/video/cocoa/SDL_cocoavideo.m            |  6 +--
 src/video/cocoa/SDL_cocoawindow.m           | 18 +++-----
 src/video/emscripten/SDL_emscriptenevents.c | 10 ++---
 src/video/emscripten/SDL_emscriptenvideo.c  |  4 +-
 src/video/emscripten/SDL_emscriptenvideo.h  |  3 --
 src/video/haiku/SDL_bvideo.cc               |  4 +-
 src/video/kmsdrm/SDL_kmsdrmmouse.c          |  2 +-
 src/video/ngage/SDL_ngageevents.cpp         |  4 +-
 src/video/ngage/SDL_ngagevideo.h            |  3 --
 src/video/psp/SDL_pspevents.c               |  4 +-
 src/video/qnx/SDL_qnx.h                     |  3 --
 src/video/qnx/SDL_qnxkeyboard.c             |  4 +-
 src/video/qnx/SDL_qnxvideo.c                |  4 +-
 src/video/raspberry/SDL_rpimouse.c          |  2 +-
 src/video/riscos/SDL_riscosevents.c         |  8 ++--
 src/video/riscos/SDL_riscosvideo.c          |  6 +--
 src/video/riscos/SDL_riscosvideo.h          |  2 -
 src/video/uikit/SDL_uikitview.m             | 12 +++---
 src/video/wayland/SDL_waylandevents.c       |  2 +-
 src/video/windows/SDL_windowsevents.c       | 47 ++++++++-------------
 src/video/windows/SDL_windowsmouse.c        |  2 +-
 src/video/windows/SDL_windowsvideo.h        |  3 --
 src/video/winrt/SDL_winrtevents_c.h         |  3 --
 src/video/winrt/SDL_winrtkeyboard.cpp       |  2 +-
 src/video/winrt/SDL_winrtpointerinput.cpp   | 12 +++---
 src/video/winrt/SDL_winrtvideo.cpp          |  4 +-
 src/video/x11/SDL_x11events.c               | 22 +++++-----
 src/video/x11/SDL_x11video.c                |  6 +--
 src/video/x11/SDL_x11video.h                |  3 --
 src/video/x11/SDL_x11xinput2.c              |  4 +-
 44 files changed, 143 insertions(+), 174 deletions(-)

diff --git a/src/core/haiku/SDL_BApp.h b/src/core/haiku/SDL_BApp.h
index 54f98b267f561..484b0f3c8b64c 100644
--- a/src/core/haiku/SDL_BApp.h
+++ b/src/core/haiku/SDL_BApp.h
@@ -46,8 +46,6 @@ extern "C" {
 
 #include <vector>
 
-#define BAPP_KEYBOARD_ID    1
-#define BAPP_MOUSE_ID       1
 
 /* Forward declarations */
 class SDL_BLooper;
@@ -251,12 +249,12 @@ class SDL_BLooper : public BLooper
             SDL_GetWindowPosition(win, &winPosX, &winPosY);
             int dx = x - (winWidth / 2);
             int dy = y - (winHeight / 2);
-            SDL_SendMouseMotion(0, win, BAPP_MOUSE_ID, SDL_GetMouse()->relative_mode, (float)dx, (float)dy);
+            SDL_SendMouseMotion(0, win, SDL_DEFAULT_MOUSE_ID, SDL_GetMouse()->relative_mode, (float)dx, (float)dy);
             set_mouse_position((winPosX + winWidth / 2), (winPosY + winHeight / 2));
             if (!be_app->IsCursorHidden())
                 be_app->HideCursor();
         } else {
-            SDL_SendMouseMotion(0, win, BAPP_MOUSE_ID, SDL_FALSE, (float)x, (float)y);
+            SDL_SendMouseMotion(0, win, SDL_DEFAULT_MOUSE_ID, SDL_FALSE, (float)x, (float)y);
             if (SDL_CursorVisible() && be_app->IsCursorHidden())
                 be_app->ShowCursor();
         }
@@ -274,7 +272,7 @@ class SDL_BLooper : public BLooper
             return;
         }
         win = GetSDLWindow(winID);
-        SDL_SendMouseButton(0, win, BAPP_MOUSE_ID, state, button);
+        SDL_SendMouseButton(0, win, SDL_DEFAULT_MOUSE_ID, state, button);
     }
 
     void _HandleMouseWheel(BMessage *msg)
@@ -289,7 +287,7 @@ class SDL_BLooper : public BLooper
             return;
         }
         win = GetSDLWindow(winID);
-        SDL_SendMouseWheel(0, win, BAPP_MOUSE_ID, xTicks, -yTicks, SDL_MOUSEWHEEL_NORMAL);
+        SDL_SendMouseWheel(0, win, SDL_DEFAULT_MOUSE_ID, xTicks, -yTicks, SDL_MOUSEWHEEL_NORMAL);
     }
 
     void _HandleKey(BMessage *msg)
@@ -306,7 +304,7 @@ class SDL_BLooper : public BLooper
             return;
         }
         HAIKU_SetKeyState(scancode, state);
-        SDL_SendKeyboardKey(0, BAPP_KEYBOARD_ID, state, HAIKU_GetScancodeFromBeKey(scancode));
+        SDL_SendKeyboardKey(0, SDL_DEFAULT_KEYBOARD_ID, state, HAIKU_GetScancodeFromBeKey(scancode));
 
         if (state == SDL_PRESSED && SDL_EventEnabled(SDL_EVENT_TEXT_INPUT)) {
             const int8 *keyUtf8;
diff --git a/src/core/winrt/SDL_winrtapp_direct3d.cpp b/src/core/winrt/SDL_winrtapp_direct3d.cpp
index 0565d4f85542a..013d03e2eb4cc 100644
--- a/src/core/winrt/SDL_winrtapp_direct3d.cpp
+++ b/src/core/winrt/SDL_winrtapp_direct3d.cpp
@@ -538,7 +538,7 @@ void SDL_WinRTApp::OnWindowActivated(CoreWindow ^ sender, WindowActivatedEventAr
              */
 #if !SDL_WINAPI_FAMILY_PHONE || NTDDI_VERSION >= NTDDI_WINBLUE
             Point cursorPos = WINRT_TransformCursorPosition(window, sender->PointerPosition, TransformToSDLWindowSize);
-            SDL_SendMouseMotion(0, window, 0, SDL_FALSE, cursorPos.X, cursorPos.Y);
+            SDL_SendMouseMotion(0, window, SDL_GLOBAL_MOUSE_ID, SDL_FALSE, cursorPos.X, cursorPos.Y);
 #endif
 
             /* TODO, WinRT: see if the Win32 bugfix from https://hg.libsdl.org/SDL/rev/d278747da408 needs to be applied (on window activation) */
@@ -724,8 +724,8 @@ void SDL_WinRTApp::OnCharacterReceived(Windows::UI::Core::CoreWindow ^ sender, W
 template <typename BackButtonEventArgs>
 static void WINRT_OnBackButtonPressed(BackButtonEventArgs ^ args)
 {
-    SDL_SendKeyboardKey(0, 0, SDL_PRESSED, SDL_SCANCODE_AC_BACK);
-    SDL_SendKeyboardKey(0, 0, SDL_RELEASED, SDL_SCANCODE_AC_BACK);
+    SDL_SendKeyboardKey(0, SDL_GLOBAL_KEYBOARD_ID, SDL_PRESSED, SDL_SCANCODE_AC_BACK);
+    SDL_SendKeyboardKey(0, SDL_GLOBAL_KEYBOARD_ID, SDL_RELEASED, SDL_SCANCODE_AC_BACK);
 
     if (SDL_GetHintBoolean(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, SDL_FALSE)) {
         args->Handled = true;
diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c
index 577e6ba5f1b59..3ef19e6506712 100644
--- a/src/events/SDL_keyboard.c
+++ b/src/events/SDL_keyboard.c
@@ -804,7 +804,7 @@ void SDL_ResetKeyboard(void)
 #endif
     for (scancode = (SDL_Scancode)0; scancode < SDL_NUM_SCANCODES; ++scancode) {
         if (keyboard->keystate[scancode] == SDL_PRESSED) {
-            SDL_SendKeyboardKey(0, 0, SDL_RELEASED, scancode);
+            SDL_SendKeyboardKey(0, SDL_GLOBAL_KEYBOARD_ID, SDL_RELEASED, scancode);
         }
     }
 }
@@ -947,6 +947,9 @@ static int SDL_SendKeyboardKeyInternal(Uint64 timestamp, Uint32 flags, SDL_Keybo
     Uint8 repeat = SDL_FALSE;
     const Uint8 source = flags & KEYBOARD_SOURCE_MASK;
 
+    /* We currently don't have raw keyboard mode, so all key events are global */
+    keyboardID = SDL_GLOBAL_KEYBOARD_ID;
+
     if (scancode == SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
         return 0;
     }
@@ -1099,23 +1102,23 @@ int SDL_SendKeyboardUnicodeKey(Uint64 timestamp, Uint32 ch)
 
     if (mod & SDL_KMOD_SHIFT) {
         /* If the character uses shift, press shift down */
-        SDL_SendKeyboardKeyInternal(timestamp, KEYBOARD_VIRTUAL, 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT, SDLK_UNKNOWN);
+        SDL_SendKeyboardKeyInternal(timestamp, KEYBOARD_VIRTUAL, SDL_GLOBAL_KEYBOARD_ID, SDL_PRESSED, SDL_SCANCODE_LSHIFT, SDLK_UNKNOWN);
     }
 
     /* Send a keydown and keyup for the character */
-    SDL_SendKeyboardKeyInternal(timestamp, KEYBOARD_VIRTUAL, 0, SDL_PRESSED, code, SDLK_UNKNOWN);
-    SDL_SendKeyboardKeyInternal(timestamp, KEYBOARD_VIRTUAL, 0, SDL_RELEASED, code, SDLK_UNKNOWN);
+    SDL_SendKeyboardKeyInternal(timestamp, KEYBOARD_VIRTUAL, SDL_GLOBAL_KEYBOARD_ID, SDL_PRESSED, code, SDLK_UNKNOWN);
+    SDL_SendKeyboardKeyInternal(timestamp, KEYBOARD_VIRTUAL, SDL_GLOBAL_KEYBOARD_ID, SDL_RELEASED, code, SDLK_UNKNOWN);
 
     if (mod & SDL_KMOD_SHIFT) {
         /* If the character uses shift, release shift */
-        SDL_SendKeyboardKeyInternal(timestamp, KEYBOARD_VIRTUAL, 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT, SDLK_UNKNOWN);
+        SDL_SendKeyboardKeyInternal(timestamp, KEYBOARD_VIRTUAL, SDL_GLOBAL_KEYBOARD_ID, SDL_RELEASED, SDL_SCANCODE_LSHIFT, SDLK_UNKNOWN);
     }
     return 0;
 }
 
 int SDL_SendVirtualKeyboardKey(Uint64 timestamp, Uint8 state, SDL_Scancode scancode)
 {
-    return SDL_SendKeyboardKeyInternal(timestamp, KEYBOARD_VIRTUAL, 0, state, scancode, SDLK_UNKNOWN);
+    return SDL_SendKeyboardKeyInternal(timestamp, KEYBOARD_VIRTUAL, SDL_GLOBAL_KEYBOARD_ID, state, scancode, SDLK_UNKNOWN);
 }
 
 int SDL_SendKeyboardKey(Uint64 timestamp, SDL_KeyboardID keyboardID, Uint8 state, SDL_Scancode scancode)
@@ -1130,7 +1133,7 @@ int SDL_SendKeyboardKeyAndKeycode(Uint64 timestamp, SDL_KeyboardID keyboardID, U
 
 int SDL_SendKeyboardKeyAutoRelease(Uint64 timestamp, SDL_Scancode scancode)
 {
-    return SDL_SendKeyboardKeyInternal(timestamp, KEYBOARD_AUTORELEASE, 0, SDL_PRESSED, scancode, SDLK_UNKNOWN);
+    return SDL_SendKeyboardKeyInternal(timestamp, KEYBOARD_AUTORELEASE, SDL_GLOBAL_KEYBOARD_ID, SDL_PRESSED, scancode, SDLK_UNKNOWN);
 }
 
 int SDL_SendKeyboardKeyIgnoreModifiers(Uint64 timestamp, SDL_KeyboardID keyboardID, Uint8 state, SDL_Scancode scancode)
@@ -1146,7 +1149,7 @@ void SDL_ReleaseAutoReleaseKeys(void)
     if (keyboard->autorelease_pending) {
         for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES; ++scancode) {
             if (keyboard->keysource[scancode] == KEYBOARD_AUTORELEASE) {
-                SDL_SendKeyboardKeyInternal(0, KEYBOARD_AUTORELEASE, 0, SDL_RELEASED, scancode, SDLK_UNKNOWN);
+                SDL_SendKeyboardKeyInternal(0, KEYBOARD_AUTORELEASE, SDL_GLOBAL_KEYBOARD_ID, SDL_RELEASED, scancode, SDLK_UNKNOWN);
             }
         }
         keyboard->autorelease_pending = SDL_FALSE;
diff --git a/src/events/SDL_keyboard_c.h b/src/events/SDL_keyboard_c.h
index b44ec8276fbd0..323b139d1c285 100644
--- a/src/events/SDL_keyboard_c.h
+++ b/src/events/SDL_keyboard_c.h
@@ -23,6 +23,12 @@
 #ifndef SDL_keyboard_c_h_
 #define SDL_keyboard_c_h_
 
+/* Keyboard events not associated with a specific input device */
+#define SDL_GLOBAL_KEYBOARD_ID     0
+
+/* The default keyboard input device, for platforms that don't have multiple keyboards */
+#define SDL_DEFAULT_KEYBOARD_ID    1
+
 /* Initialize the keyboard subsystem */
 extern int SDL_InitKeyboard(void);
 
diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c
index 1edaab464f1de..3346325a218ea 100644
--- a/src/events/SDL_mouse.c
+++ b/src/events/SDL_mouse.c
@@ -507,7 +507,7 @@ static SDL_bool SDL_UpdateMouseFocus(SDL_Window *window, float x, float y, Uint3
             SDL_Log("Mouse left window, synthesizing move & focus lost event\n");
 #endif
             if (send_mouse_motion) {
-                SDL_PrivateSendMouseMotion(0, window, 0, SDL_FALSE, x, y);
+                SDL_PrivateSendMouseMotion(0, window, SDL_GLOBAL_MOUSE_ID, SDL_FALSE, x, y);
             }
             SDL_SetMouseFocus(NULL);
         }
@@ -520,7 +520,7 @@ static SDL_bool SDL_UpdateMouseFocus(SDL_Window *window, float x, float y, Uint3
 #endif
         SDL_SetMouseFocus(window);
         if (send_mouse_motion) {
-            SDL_PrivateSendMouseMotion(0, window, 0, SDL_FALSE, x, y);
+            SDL_PrivateSendMouseMotion(0, window, SDL_GLOBAL_MOUSE_ID, SDL_FALSE, x, y);
         }
     }
     return SDL_TRUE;
@@ -691,6 +691,11 @@ static int SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_
     float xrel = 0.0f;
     float yrel = 0.0f;
 
+    if (!mouse->relative_mode && mouseID != SDL_TOUCH_MOUSEID && mouseID != SDL_PEN_MOUSEID) {
+        /* We're not in relative mode, so all mouse events are global mouse events */
+        mouseID = SDL_GLOBAL_MOUSE_ID;
+    }
+
     /* SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events */
     if (mouse->mouse_touch_events) {
         if (mouseID != SDL_TOUCH_MOUSEID && !relative && track_mouse_down) {
@@ -880,6 +885,11 @@ static int SDL_PrivateSendMouseButton(Uint64 timestamp, SDL_Window *window, SDL_
     Uint32 buttonstate;
     SDL_MouseInputSource *source;
 
+    if (!mouse->relative_mode && mouseID != SDL_TOUCH_MOUSEID && mouseID != SDL_PEN_MOUSEID) {
+        /* We're not in relative mode, so all mouse events are global mouse events */
+        mouseID = SDL_GLOBAL_MOUSE_ID;
+    }
+
     source = GetMouseInputSource(mouse, mouseID, state, button);
     if (!source) {
         return 0;
@@ -1198,7 +1208,7 @@ void SDL_PerformWarpMouseInWindow(SDL_Window *window, float x, float y, SDL_bool
         (!mouse->relative_mode || mouse->relative_mode_warp)) {
         mouse->WarpMouse(window, x, y);
     } else {
-        SDL_PrivateSendMouseMotion(0, window, 0, SDL_FALSE, x, y);
+        SDL_PrivateSendMouseMotion(0, window, SDL_GLOBAL_MOUSE_ID, SDL_FALSE, x, y);
     }
 }
 
diff --git a/src/events/SDL_mouse_c.h b/src/events/SDL_mouse_c.h
index f02811a5ef7c4..0603577fabd76 100644
--- a/src/events/SDL_mouse_c.h
+++ b/src/events/SDL_mouse_c.h
@@ -23,6 +23,12 @@
 #ifndef SDL_mouse_c_h_
 #define SDL_mouse_c_h_
 
+/* Mouse events not associated with a specific input device */
+#define SDL_GLOBAL_MOUSE_ID     0
+
+/* The default mouse input device, for platforms that don't have multiple mice */
+#define SDL_DEFAULT_MOUSE_ID    1
+
 struct SDL_Cursor
 {
     struct SDL_Cursor *next;
diff --git a/src/joystick/android/SDL_sysjoystick.c b/src/joystick/android/SDL_sysjoystick.c
index c6408c0676ec0..9afd002cf9938 100644
--- a/src/joystick/android/SDL_sysjoystick.c
+++ b/src/joystick/android/SDL_sysjoystick.c
@@ -205,7 +205,7 @@ int Android_OnPadDown(int device_id, int keycode)
         if (item && item->joystick) {
             SDL_SendJoystickButton(timestamp, item->joystick, button, SDL_PRESSED);
         } else {
-            SDL_SendKeyboardKey(timestamp, 0, SDL_PRESSED, button_to_scancode(button));
+            SDL_SendKeyboardKey(timestamp, SDL_GLOBAL_KEYBOARD_ID, SDL_PRESSED, button_to_scancode(button));
         }
         SDL_UnlockJoysticks();
         return 0;
@@ -225,7 +225,7 @@ int Android_OnPadUp(int device_id, int keycode)
         if (item && item->joystick) {
             SDL_SendJoystickButton(timestamp, item->joystick, button, SDL_RELEASED);
         } else {
-            SDL_SendKeyboardKey(timestamp, 0, SDL_RELEASED, button_to_scancode(button));
+            SDL_SendKeyboardKey(timestamp, SDL_GLOBAL_KEYBOARD_ID, SDL_RELEASED, button_to_scancode(button));
         }
         SDL_UnlockJoysticks();
         return 0;
diff --git a/src/video/android/SDL_androidkeyboard.c b/src/video/android/SDL_androidkeyboard.c
index cff44ed6139ae..1422622a8e30d 100644
--- a/src/video/android/SDL_androidkeyboard.c
+++ b/src/video/android/SDL_androidkeyboard.c
@@ -330,12 +330,12 @@ static SDL_Scancode TranslateKeycode(int keycode)
 
 int Android_OnKeyDown(int keycode)
 {
-    return SDL_SendKeyboardKey(0, Android_KeyboardID, SDL_PRESSED, TranslateKeycode(keycode));
+    return SDL_SendKeyboardKey(0, SDL_DEFAULT_KEYBOARD_ID, SDL_PRESSED, TranslateKeycode(keycode));
 }
 
 int Android_OnKeyUp(int keycode)
 {
-    return SDL_SendKeyboardKey(0, Android_KeyboardID, SDL_RELEASED, TranslateKeycode(keycode));
+    return SDL_SendKeyboardKey(0, SDL_DEFAULT_KEYBOARD_ID, SDL_RELEASED, TranslateKeycode(keycode));
 }
 
 SDL_bool Android_HasScreenKeyboardSupport(SDL_VideoDevice *_this)
diff --git a/src/video/android/SDL_androidmouse.c b/src/video/android/SDL_androidmouse.c
index 122fece95fedb..84c15e50a534f 100644
--- a/src/video/android/SDL_androidmouse.c
+++ b/src/video/android/SDL_androidmouse.c
@@ -221,25 +221,25 @@ void Android_OnMouse(SDL_Window *window, int state, int action, float x, float y
         changes = state & ~last_state;
         button = TranslateButton(changes);
         last_state = state;
-        SDL_SendMouseMotion(0, window, Android_MouseID, relative, x, y);
-        SDL_SendMouseButton(0, window, Android_MouseID, SDL_PRESSED, button);
+        SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y);
+        SDL_SendMouseButton(0, window, SDL_DEFAULT_MOUSE_ID, SDL_PRESSED, button);
         break;
 
     case ACTION_UP:
         changes = last_state & ~state;
         button = TranslateButton(changes);
         last_state = state;
-        SDL_SendMouseMotion(0, window, Android_MouseID, relative, x, y);
-        SDL_SendMouseButton(0, window, Android_MouseID, SDL_RELEASED, button);
+        SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y);
+        SDL_SendMouseButton(0, window, SDL_DEFAULT_MOUSE_ID, SDL_RELEASED, button);
         break;
 
     case ACTION_MOVE:
     case ACTION_HOVER_MOVE:
-        SDL_SendMouseMotion(0, window, Android_MouseID, relative, x, y);
+        SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y);
         break;
 
     case ACTION_SCROLL:
-        SDL_SendMouseWheel(0, window, Android_MouseID, x, y, SDL_MOUSEWHEEL_NORMAL);
+        SDL_SendMouseWheel(0, window, SDL_DEFAULT_MOUSE_ID, x, y, SDL_MOUSEWHEEL_NORMAL);
         break;
 
     default:
diff --git a/src/video/android/SDL_androidvideo.c b/src/video/android/SDL_androidvideo.c
index 3a0686ce8a31c..17f7e57048f64 100644
--- a/src/video/android/SDL_androidvideo.c
+++ b/src/video/android/SDL_androidvideo.c
@@ -67,8 +67,6 @@ SDL_Semaphore *Android_PauseSem = NULL;
 SDL_Semaphore *Android_ResumeSem = NULL;
 SDL_Mutex *Android_ActivityMutex = NULL;
 static SDL_SystemTheme Android_SystemTheme;
-SDL_KeyboardID Android_KeyboardID = 0;
-SDL_MouseID Android_MouseID = 0;
 
 static int Android_SuspendScreenSaver(SDL_VideoDevice *_this)
 {
diff --git a/src/video/android/SDL_androidvideo.h b/src/video/android/SDL_androidvideo.h
index ebda0cc200675..f6dd54eb56073 100644
--- a/src/video/android/SDL_androidvideo.h
+++ b/src/video/android/SDL_androidvideo.h
@@ -46,7 +46,5 @@ extern int Android_SurfaceHeight;
 extern float Android_ScreenDensity;
 extern SDL_Semaphore *Android_PauseSem, *Android_ResumeSem;
 extern SDL_Mutex *Android_ActivityMutex;
-extern SDL_KeyboardID Android_KeyboardID;
-extern SDL_MouseID Android_MouseID;
 
 #endif /* SDL_androidvideo_h_ */
diff --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m
index 99dfb99318fc1..fc12196c8dd56 100644
--- a/src/video/cocoa/SDL_cocoakeyboard.m
+++ b/src/video/cocoa/SDL_cocoakeyboard.m
@@ -200,7 +200,6 @@ static bool IsModifierKeyPressed(unsigned int flags,
 
 static void HandleModifiers(SDL_VideoDevice *_this, SDL_Scancode code, unsigned int modifierFlags)
 {
-    SDL_CocoaVideoData *videodata = (__bridge SDL_CocoaVideoData *)_this->driverdata;
     bool pressed = false;
 
     if (code == SDL_SCANCODE_LSHIFT) {
@@ -232,9 +231,9 @@ static void HandleModifiers(SDL_VideoDevice *_this, SDL_Scancode code, unsigned
     }
 
     if (pressed) {
-        SDL_SendKeyboardKey(0, videodata.keyboardID, SDL_PRESSED, code);
+        SDL_SendKeyboardKey(0, SDL_DEFAULT_KEYBOARD_ID, SDL_PRESSED, code);
     } else {
-        SDL_SendKeyboardKey(0, videodata.keyboardID, SDL_RELEASED, code);
+        SDL_SendKeyboardKey(0, SDL_DEFAULT_KEYBOARD_ID, SDL_RELEASED, code);
     }
 }
 
@@ -415,7 +414,7 @@ void Cocoa_HandleKeyEvent(SDL_VideoDevice *_this, NSEvent *event)
             UpdateKeymap(data, SDL_TRUE);
         }
 
-        SDL_SendKeyboardKey(Cocoa_GetEventTimestamp([event timestamp]), data ? data.keyboardID : 0, SDL_PRESSED, code);
+        SDL_SendKeyboardKey(Cocoa_GetEventTimestamp([event timestamp]), SDL_DEFAULT_KEYBOARD_ID, SDL_PRESSED, code);
 #ifdef DEBUG_SCANCODES
         if (code == SDL_SCANCODE_UNKNOWN) {
             SDL_Log("The key you just pressed is not recognized by SDL. To help get this fixed, report this to the SDL forums/mailing list <https://discourse.libsdl.org/> or to Christian Walther <cwalther@gmx.ch>. Mac virtual key code is %d.\n", scancode);
@@ -434,7 +433,7 @@ void Cocoa_HandleKeyEvent(SDL_VideoDevice *_this, NSEvent *event)
         }
         break;
     case NSEventTypeKeyUp:
-        SDL_SendKeyboardKey(Cocoa_GetEventTimestamp([event timestamp]), data ? data.keyboardID : 0, SDL_RELEASED, code);
+        SDL_SendKeyboardKey(Cocoa_GetEventTimestamp([event timestamp]), SDL_DEFAULT_KEYBOARD_ID, SDL_RELEASED, code);
         break;
     case NSEventTypeFlagsChanged: {
         // see if the new modifierFlags mean any existing keys should be pressed/released...
diff --git a/src/video/cocoa/SDL_cocoamouse.m b/src/video/cocoa/SDL_cocoamouse.m
index 727912cae52d4..eef892a67c2bb 100644
--- a/src/video/cocoa/SDL_cocoamouse.m
+++ b/src/video/cocoa/SDL_cocoamouse.m
@@ -302,7 +302,7 @@ static int Cocoa_WarpMouseGlobal(float x, float y)
         SDL_SetMouseFocus(win);
         if (win) {
             SDL_assert(win == mouse->focus);
-            SDL_SendMouseMotion(0, win, 0, SDL_FALSE, x - win->x, y - win->y);
+            SDL_SendMouseMotion(0, win, SDL_GLOBAL_MOUSE_ID, SDL_FALSE, x - win->x, y - win->y);
         }
     }
 
@@ -450,8 +450,7 @@ static void Cocoa_HandleTitleButtonEvent(SDL_VideoDevice *_this, NSEvent *event)
 
 void Cocoa_HandleMouseEvent(SDL_VideoDevice *_this, NSEvent *event)
 {
-    SDL_CocoaVideoData *videodata = (__bridge SDL_CocoaVideoData *)_this->driverdata;
-    SDL_MouseID mouseID = videodata.mouseID;
+    SDL_MouseID mouseID = SDL_DEFAULT_MOUSE_ID;
     SDL_Mouse *mouse;
     SDL_MouseData *driverdata;
     NSPoint location;
@@ -529,8 +528,7 @@ void Cocoa_HandleMouseEvent(SDL_VideoDevice *_this, NSEvent *event)
 
 void Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
 {
-    SDL_CocoaVideoData *videodata = ((__bridge SDL_CocoaWindowData *)window->driverdata).videodata;
-    SDL_MouseID mouseID = videodata.mouseID;
+    SDL_MouseID mouseID = SDL_DEFAULT_MOUSE_ID;
     SDL_MouseWheelDirection direction;
     CGFloat x, y;
 
diff --git a/src/video/cocoa/SDL_cocoavideo.h b/src/video/cocoa/SDL_cocoavideo.h
index a35063a6a22ac..2929fe0ad1755 100644
--- a/src/video/cocoa/SDL_cocoavideo.h
+++ b/src/video/cocoa/SDL_cocoavideo.h
@@ -101,8 +101,6 @@ DECLARE_ALERT_STYLE(Critical);
 @property(nonatomic) int trackpad_is_touch_only;
 @property(nonatomic) unsigned int modifierFlags;
 @property(nonatomic) void *key_layout;
-@property(nonatomic) SDL_KeyboardID keyboardID;
-@property(nonatomic) SDL_MouseID mouseID;
 @property(nonatomic) SDLTranslatorResponder *fieldEdit;
 @property(nonatomic) NSInteger clipboard_count;
 @property(nonatomic) IOPMAssertionID screensaver_assertion;
diff --git a/src/video/cocoa/SDL_cocoavideo.m b/src/video/cocoa/SDL_cocoavideo.m
index eb96d3526677d..ae4bbcfbf016a 100644
--- a/src/video/cocoa/SDL_cocoavideo.m
+++ b/src/video/cocoa/SDL_cocoavideo.m
@@ -203,10 +203,8 @@ int Cocoa_VideoInit(SDL_VideoDevice *_this)
 
         // Assume we have a mouse and keyboard
         // We could use GCMouse and GCKeyboard if we needed to, as is done in SDL_uikitevents.m
-        data.keyboardID = SDL_GetNextObjectID();
-        SDL_AddKeyboard(data.keyboardID, NULL, SDL_FALSE);
-        data.mouseID = SDL_GetNextObjectID();
-        SDL_AddMouse(data.mouseID, NULL, SDL_FALSE);
+        SDL_AddKeyboard(SDL_DEFAULT_KEYBOARD_ID, NULL, SDL_FALSE);
+        SDL_AddMouse(SDL_DEFAULT_MOUSE_ID, NULL, SDL_FALSE);
 
         data.allow_spaces = SDL_GetHintBoolean(SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES, SDL_TRUE);
         data.trackpad_is_touch_only = SDL_GetHintBoolean(SDL_HINT_TRACKPAD_IS_TOUCH_ONLY, SDL_FALSE);
diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m
index 0a43da32c0720..03fdfbe1c85cf 100644
--- a/src/video/cocoa/SDL_cocoawindow.m
+++ b/src/video/cocoa/SDL_cocoawindow.m
@@ -210,7 +210,7 @@ - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
         x = point.x;
         y = (sdlwindow->h - point.y);
         if (x >= 0.0f && x < (float)sdlwindow->w && y >= 0.0f && y < (float)sdlwindow->h) {
-            SDL_SendMouseMotion(0, sdlwindow, 0, SDL_FALSE, x, y);
+            SDL_SendMouseMotion(0, sdlwindow, SDL_GLOBAL_MOUSE_ID, SDL_FALSE, x, y);
         }
         /* Code addon to update the mouse location */
 
@@ -1089,7 +1089,7 @@ - (void)windowDidBecomeKey:(NSNotification *)aNotification
         y = (window->h - point.y);
 
         if (x >= 0.0f && x < (float)window->w && y >= 0.0f && y < (float)window->h) {
-            SDL_SendMouseMotion(0, window, 0, SDL_FALSE, x, y);
+            SDL_SendMouseMotion(0, window, SDL_GLOBAL_MOUSE_ID, SDL_FALSE, x, y);
         }
     }
 
@@ -1376,13 +1376,11 @@ - (void)flagsChanged:(NSEvent *)theEvent
 
     /* Also note that SDL_SendKeyboardKey expects all capslock events to be
        keypresses; it won't toggle the mod state if you send a keyrelease.  */
-    SDL_VideoDevice *_this = SDL_GetVideoDevice();
-    SDL_CocoaVideoData *videodata = (__bridge SDL_CocoaVideoData *)_this->driverdata;
     const SDL_bool osenabled = ([theEvent modifierFlags] & NSEventModifierFlagCapsLock) ? SDL_TRUE : SDL_FALSE;
     const SDL_bool sdlenabled = (SDL_GetModState() & SDL_KMOD_CAPS) ? SDL_TRUE : SDL_FALSE;
     if (osenabled ^ sdlenabled) {
-        SDL_SendKeyboardKey(0, videodata.keyboardID, SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
-        SDL_SendKeyboardKey(0, videodata.keyboardID, SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
+        SDL_SendKeyboardKey(0, SDL_DEFAULT_KEYBOARD_ID, SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
+        SDL_SendKeyboardKey(0, SDL_DEFAULT_KEYBOARD_ID, SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
     }
 }
 - (void)keyDown:(NSEvent *)theEvent
@@ -1457,9 +1455,7 @@ - (BOOL)processHitTest:(NSEvent *)theEvent
 
 static int Cocoa_SendMouseButtonClicks(SDL_Mouse *mouse, NSEvent *theEvent, SDL_Window *window, const Uint8 state, const Uint8 button)
 {
-    SDL_VideoDevice *_this = SDL_GetVideoDevice();
-    SDL_CocoaVideoData *videodata = (__bridge SDL_CocoaVideoData *)_this->driverdata;
-    SDL_MouseID mouseID = videodata.mouseID;
+    SDL_MouseID mouseID = SDL_DEFAULT_MOUSE_ID;
     const int clicks = (int)[theEvent clickCount];
     SDL_Window *focus = SDL_GetKeyboardFocus();
     int rc;
@@ -1591,9 +1587,7 @@ - (void)otherMouseUp:(NSEvent *)theEvent
 
 - (void)mouseMoved:(NSEvent *)theEvent
 {
-    SDL_VideoDevice *_this = SDL_GetVideoDevice();
-    SDL_CocoaVideoData *videodata = (__bridge SDL_CocoaVideoData *)_this->driverdata;
-    SDL_MouseID mouseID = videodata.mouseID;
+    SDL_MouseID mouseID = SDL_DEFAULT_MOUSE_ID;
     SDL_Mouse *mouse = SDL_GetMouse();
     NSPoint point;
     float x, y;
diff --git a/src/video/emscripten/SDL_emscriptenevents.c b/src/video/emscripten/SDL_emscriptenevents.c
index e7f2c1d3f8022..7bf4e76ca3a86 100644
--- a/src/video/emscripten/SDL_emscriptenevents.c
+++ b/src/video/emscripten/SDL_emscriptenevents.c
@@ -631,7 +631,7 @@ static EM_BOOL Emscripten_HandleMouseMove(int eventType, const EmscriptenMouseEv
         my = (float)(mouseEvent->targetY * yscale);
     }
 
-    SDL_SendMouseMotion(0, window_data->window, EMSCRIPTEN_MOUSE_ID, isPointerLocked, mx, my);
+    SDL_SendMouseMotion(0, window_data->window, SDL_DEFAULT_MOUSE_ID, isPointerLocked, mx, my);
     return 0;
 }
 
@@ -669,7 +669,7 @@ static EM_BOOL Emscripten_HandleMouseButton(int eventType, const EmscriptenMouse
         sdl_event_type = SDL_EVENT_MOUSE_BUTTON_UP;
         prevent_default = SDL_EventEnabled(sdl_event_type);
     }
-    SDL_SendMouseButton(0, window_data->window, EMSCRIPTEN_MOUSE_ID, sdl_button_state, sdl_button);
+    SDL_SendMouseButton(0, window_data->window, SDL_DEFAULT_MOUSE_ID, sdl_button_state, sdl_button);
 
     /* Do not consume the event if the mouse is outside of the canvas. */
     emscripten_get_element_css_size(window_data->canvas_id, &css_w, &css_h);
@@ -695,7 +695,7 @@ static EM_BOOL Emscripten_HandleMouseFocus(int eventType, const EmscriptenMouseE
 
         mx = (float)(mouseEvent->targetX * (window_data->window->w / client_w));
         my = (float)(mouseEvent->targetY * (window_data->window->h / client_h));
-        SDL_SendMouseMotion(0, window_data->window, EMSCRIPTEN_MOUSE_ID, isPointerLocked, mx, my);
+        SDL_SendMouseMotion(0, window_data->window, SDL_GLOBAL_MOUSE_ID, isPointerLocked, mx, my);
     }
 
     SDL_SetMouseFocus(eventType == EMSCRIPTEN_EVENT_MOUSEENTER ? window_data->window : NULL);
@@ -720,7 +720,7 @@ static EM_BOOL Emscripten_HandleWheel(int eventType, const EmscriptenWheelEvent
         break;
     }
 
-    SDL_SendMouseWheel(0, window_data->window, EMSCRIPTEN_MOUSE_ID, (float)wheelEvent->deltaX, -deltaY, SDL_MOUSEWHEEL_NORMAL);
+    SDL_SendMouseWheel(0, window_data->window, SDL_DEFAULT_MOUSE_ID, (float)wheelEvent->deltaX, -deltaY, SDL_MOUSEWHEEL_NORMAL);
     return SDL_EventEnabled(SDL_EVENT_MOUSE_WHEEL);
 }
 
@@ -811,7 +811,7 @@ static EM_BOOL Emscripten_HandleKey(int eventType, const EmscriptenKeyboardEvent
     }
 
     if (scancode != SDL_SCANCODE_UNKNOWN) {
-        SDL_SendKeyboardKeyAndKeycode(0, EMSCRIPTEN_KEYBOARD_ID, eventType == EMSCRIPTEN_EVENT_KEYDOWN ? SDL_PRESSED : SDL_RELEASED, scancode, keycode);
+        SDL_SendKeyboardKeyAndKeycode(0, SDL_DEFAULT_KEYBOARD_ID, eventType == EMSCRIPTEN_EVENT_KEYDOWN ? SDL_PRESSED : SDL_RELEASED, scancode, keycode);
     }
 
     /* if TEXTINPUT events are enabled we can't prevent keydown or we won't get keypress
diff --git a/src/video/emscripten/SDL_emscriptenvideo.c b/src/video/emscripten/SDL_emscriptenvideo.c
index 1437b0a66904b..8e220d29e90e9 100644
--- a/src/video/emscripten/SDL_emscriptenvideo.c
+++ b/src/video/emscripten/SDL_emscriptenvideo.c
@@ -137,8 +137,8 @@ int Emscripten_VideoInit(SDL_VideoDevice *_this)
     Emscripten_InitMouse();
 
     /* Assume we have a mouse and keyboard */
-    SDL_AddKeyboard(EMSCRIPTEN_KEYBOARD_ID, NULL, SDL_FALSE);
-    SDL_AddMouse(EMSCRIPTEN_MOUSE_ID, NULL, SDL_FALSE);
+    SDL_AddKeyboard(SDL_DEFAULT_KEYBOARD_ID, NULL, SDL_FALSE);
+    SDL_AddMouse(SDL_DEFAULT_MOUSE_ID, NULL, SDL_FALSE);
 
     /* We're done! */
     return 0;
diff --git a/src/video/emscripten/SDL_emscriptenvideo.h b/src/video/emscripten/SDL_emscriptenvideo.h
index 094f9b1508a80..4417205850c09 100644
--- a/src/video/emscripten/SDL_emscriptenvideo.h
+++ b/src/video/emscripten/SDL_emscriptenvideo.h
@@ -28,9 +28,6 @@
 #include <emscripten/emscripten.h>
 #include <emscripten/html5.h>
 
-#define EMSCRIPTEN_KEYBOARD_ID    1
-#define EMSCRIPTEN_MOUSE_ID       1
-
 struct SDL_WindowData
 {
     SDL_Window *window;
diff --git a/src/video/haiku/SDL_bvideo.cc b/src/video/haiku/SDL_bvideo.cc
index 9720e41d648f3..9b8f1c949ff67 100644
--- a/src/video/haiku/SDL_bvideo.cc
+++ b/src/video/haiku/SDL_bvideo.cc
@@ -282,8 +282,8 @@ int HAIKU_VideoInit(SDL_VideoDevice *_this)
     HAIKU_MouseInit(_this);
 
     /* Assume we have a mouse and keyboard */
-    SDL_AddKeyboard(BAPP_KEYBOARD_ID, NULL, SDL_FALSE);
-    SDL_AddMouse(BAPP_MOUSE_ID, NULL, SDL_FALSE);
+    SDL_AddKeyboard(SDL_DEFAULT_KEYBOARD_ID, NULL, SDL_FALSE);
+    SDL_AddMouse(SDL_DEFAULT_MOUSE_ID, NULL, SDL_FALSE);
 
 #ifdef SDL_VIDEO_OPENGL
         /* testgl application doesn't load library, just tries to load symbols */
diff --git a/src/video/kmsdrm/SDL

(Patch may be truncated, please check the link at the top of this post.)