From 71582a72a46a2f9dc63365559ff10871f45b498e Mon Sep 17 00:00:00 2001
From: Steveice10 <1269164+Steveice10@users.noreply.github.com>
Date: Wed, 19 Jul 2023 01:29:13 -0700
Subject: [PATCH] sdl: Check correct windowID based on event type. (#6703)

---
 src/citra/emu_window/emu_window_sdl2.cpp | 40 +++++++++++++++++++++++-
 src/citra/emu_window/emu_window_sdl2.h   |  4 +++
 2 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/src/citra/emu_window/emu_window_sdl2.cpp b/src/citra/emu_window/emu_window_sdl2.cpp
index 4761e5fe3..698dcc3dd 100644
--- a/src/citra/emu_window/emu_window_sdl2.cpp
+++ b/src/citra/emu_window/emu_window_sdl2.cpp
@@ -128,16 +128,54 @@ void EmuWindow_SDL2::InitializeSDL2() {
     SDL_SetMainReady();
 }
 
+u32 EmuWindow_SDL2::GetEventWindowId(const SDL_Event& event) const {
+    switch (event.type) {
+    case SDL_WINDOWEVENT:
+        return event.window.windowID;
+    case SDL_KEYDOWN:
+    case SDL_KEYUP:
+        return event.key.windowID;
+    case SDL_MOUSEMOTION:
+        return event.motion.windowID;
+    case SDL_MOUSEBUTTONDOWN:
+    case SDL_MOUSEBUTTONUP:
+        return event.button.windowID;
+    case SDL_MOUSEWHEEL:
+        return event.wheel.windowID;
+    case SDL_FINGERDOWN:
+    case SDL_FINGERMOTION:
+    case SDL_FINGERUP:
+        return event.tfinger.windowID;
+    case SDL_TEXTEDITING:
+        return event.edit.windowID;
+    case SDL_TEXTEDITING_EXT:
+        return event.editExt.windowID;
+    case SDL_TEXTINPUT:
+        return event.text.windowID;
+    case SDL_DROPBEGIN:
+    case SDL_DROPFILE:
+    case SDL_DROPTEXT:
+    case SDL_DROPCOMPLETE:
+        return event.drop.windowID;
+    case SDL_USEREVENT:
+        return event.user.windowID;
+    default:
+        // Event is not for any particular window, so we can just pretend it's for this one.
+        return render_window_id;
+    }
+}
+
 void EmuWindow_SDL2::PollEvents() {
     SDL_Event event;
     std::vector<SDL_Event> other_window_events;
 
     // SDL_PollEvent returns 0 when there are no more events in the event queue
     while (SDL_PollEvent(&event)) {
-        if (event.window.windowID != render_window_id) {
+        if (GetEventWindowId(event) != render_window_id) {
             other_window_events.push_back(event);
             continue;
         }
+
         switch (event.type) {
         case SDL_WINDOWEVENT:
             switch (event.window.event) {
diff --git a/src/citra/emu_window/emu_window_sdl2.h b/src/citra/emu_window/emu_window_sdl2.h
index 1a412c049..28f86f81a 100644
--- a/src/citra/emu_window/emu_window_sdl2.h
+++ b/src/citra/emu_window/emu_window_sdl2.h
@@ -8,6 +8,7 @@
 #include "common/common_types.h"
 #include "core/frontend/emu_window.h"
 
+union SDL_Event;
 struct SDL_Window;
 
 namespace Core {
@@ -35,6 +36,9 @@ public:
     void RequestClose();
 
 protected:
+    /// Gets the ID of the window an event originated from.
+    u32 GetEventWindowId(const SDL_Event& event) const;
+
     /// Called by PollEvents when a key is pressed or released.
     void OnKeyEvent(int key, u8 state);