From 3669d2320c9e87ebd20eb8d559db537b79668022 Mon Sep 17 00:00:00 2001
From: Leon Styhre <leon@leonstyhre.com>
Date: Thu, 7 Oct 2021 18:33:13 +0200
Subject: [PATCH] Added info popups when adding and removing input devices.

---
 es-core/src/InputManager.cpp | 26 +++++++++++++++++++++-----
 es-core/src/InputManager.h   |  4 ++--
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/es-core/src/InputManager.cpp b/es-core/src/InputManager.cpp
index d69433ca4..501d9ab96 100644
--- a/es-core/src/InputManager.cpp
+++ b/es-core/src/InputManager.cpp
@@ -112,7 +112,7 @@ void InputManager::init()
             numJoysticks--;
 
     for (int i = 0; i < numJoysticks; i++)
-        addControllerByDeviceIndex(i);
+        addControllerByDeviceIndex(nullptr, i);
 
     SDL_USER_CECBUTTONDOWN = SDL_RegisterEvents(2);
     SDL_USER_CECBUTTONUP = SDL_USER_CECBUTTONDOWN + 1;
@@ -482,11 +482,11 @@ bool InputManager::parseEvent(const SDL_Event& event, Window* window)
             break;
         }
         case SDL_CONTROLLERDEVICEADDED: {
-            addControllerByDeviceIndex(event.cdevice.which);
+            addControllerByDeviceIndex(window, event.cdevice.which);
             return true;
         }
         case SDL_CONTROLLERDEVICEREMOVED: {
-            removeControllerByJoystickID(event.cdevice.which);
+            removeControllerByJoystickID(window, event.cdevice.which);
             return false;
         }
     }
@@ -621,7 +621,7 @@ void InputManager::loadDefaultControllerConfig(SDL_JoystickID deviceIndex)
     // clang-format on
 }
 
-void InputManager::addControllerByDeviceIndex(int deviceIndex)
+void InputManager::addControllerByDeviceIndex(Window* window, int deviceIndex)
 {
     // Open joystick and add it to our list.
     SDL_GameController* controller = SDL_GameControllerOpen(deviceIndex);
@@ -652,6 +652,14 @@ void InputManager::addControllerByDeviceIndex(int deviceIndex)
                      << ", instance ID: " << joyID << ", device index: " << deviceIndex << ")";
     }
 
+    if (window != nullptr) {
+        window->queueInfoPopup(
+            "ADDED INPUT DEVICE '" +
+                Utils::String::toUpper(std::string(SDL_GameControllerName(mControllers[joyID]))) +
+                "'",
+            4000);
+    }
+
     int numAxes = SDL_JoystickNumAxes(joy);
     int numButtons = SDL_JoystickNumButtons(joy);
 
@@ -662,7 +670,7 @@ void InputManager::addControllerByDeviceIndex(int deviceIndex)
         mPrevButtonValues[std::make_pair(joyID, button)] = -1;
 }
 
-void InputManager::removeControllerByJoystickID(SDL_JoystickID joyID)
+void InputManager::removeControllerByJoystickID(Window* window, SDL_JoystickID joyID)
 {
     assert(joyID != -1);
 
@@ -673,6 +681,14 @@ void InputManager::removeControllerByJoystickID(SDL_JoystickID joyID)
     LOG(LogInfo) << "Removed controller \"" << SDL_GameControllerName(mControllers[joyID])
                  << "\" (GUID: " << guid << ", instance ID: " << joyID << ")";
 
+    if (window != nullptr) {
+        window->queueInfoPopup(
+            "REMOVED INPUT DEVICE '" +
+                Utils::String::toUpper(std::string(SDL_GameControllerName(mControllers[joyID]))) +
+                "'",
+            4000);
+    }
+
     // Delete mPrevAxisValues for the device.
     int axisEntries = static_cast<int>(mPrevAxisValues.size());
     for (int i = 0; i < axisEntries; i++) {
diff --git a/es-core/src/InputManager.h b/es-core/src/InputManager.h
index 5bdc89ea2..9cf9835f9 100644
--- a/es-core/src/InputManager.h
+++ b/es-core/src/InputManager.h
@@ -61,8 +61,8 @@ private:
     void loadDefaultKBConfig();
     void loadDefaultControllerConfig(SDL_JoystickID deviceIndex);
 
-    void addControllerByDeviceIndex(int deviceIndex);
-    void removeControllerByJoystickID(SDL_JoystickID joyID);
+    void addControllerByDeviceIndex(Window* window, int deviceIndex);
+    void removeControllerByJoystickID(Window* window, SDL_JoystickID joyID);
 
     static InputManager* sInstance;
     static const int DEADZONE_TRIGGERS = 18000;