From 0019c34c7324c79c5504c2669e4f01fb0f953faf Mon Sep 17 00:00:00 2001
From: Michael Maltese <mchtly@gmail.com>
Date: Wed, 12 Jul 2017 00:11:29 -0700
Subject: [PATCH] DolphinWX: fix input bitmaps not working when background
 input is off

---
 Source/Android/jni/MainAndroid.cpp              |  2 +-
 Source/Core/Core/Host.h                         |  2 +-
 Source/Core/DolphinNoGUI/MainNoGUI.cpp          |  2 +-
 Source/Core/DolphinQt2/Host.cpp                 |  2 +-
 Source/Core/DolphinWX/CMakeLists.txt            |  1 +
 Source/Core/DolphinWX/DolphinWX.vcxproj         |  2 ++
 Source/Core/DolphinWX/DolphinWX.vcxproj.filters |  6 ++++++
 Source/Core/DolphinWX/Input/InputConfigDiag.cpp |  8 ++++++++
 Source/Core/DolphinWX/Input/InputConfigDiag.h   |  1 +
 Source/Core/DolphinWX/Main.cpp                  |  5 +++--
 .../Core/DolphinWX/UINeedsControllerState.cpp   | 17 +++++++++++++++++
 Source/Core/DolphinWX/UINeedsControllerState.h  |  8 ++++++++
 .../ControlReference/ControlReference.cpp       |  3 ++-
 Source/UnitTests/StubHost.cpp                   |  2 +-
 14 files changed, 53 insertions(+), 8 deletions(-)
 create mode 100644 Source/Core/DolphinWX/UINeedsControllerState.cpp
 create mode 100644 Source/Core/DolphinWX/UINeedsControllerState.h

diff --git a/Source/Android/jni/MainAndroid.cpp b/Source/Android/jni/MainAndroid.cpp
index 350b78b361..b2f6ca467f 100644
--- a/Source/Android/jni/MainAndroid.cpp
+++ b/Source/Android/jni/MainAndroid.cpp
@@ -125,7 +125,7 @@ void Host_SetStartupDebuggingParameters()
 {
 }
 
-bool Host_UIHasFocus()
+bool Host_UINeedsControllerState()
 {
   return true;
 }
diff --git a/Source/Core/Core/Host.h b/Source/Core/Core/Host.h
index dc63e762ec..3fa68c9df9 100644
--- a/Source/Core/Core/Host.h
+++ b/Source/Core/Core/Host.h
@@ -23,7 +23,7 @@
 // The host can be just a command line app that opens a window, or a full blown debugger
 // interface.
 
-bool Host_UIHasFocus();
+bool Host_UINeedsControllerState();
 bool Host_RendererHasFocus();
 bool Host_RendererIsFullscreen();
 void Host_ConnectWiimote(int wm_idx, bool connect);
diff --git a/Source/Core/DolphinNoGUI/MainNoGUI.cpp b/Source/Core/DolphinNoGUI/MainNoGUI.cpp
index 56671c72a1..b62cf0e22c 100644
--- a/Source/Core/DolphinNoGUI/MainNoGUI.cpp
+++ b/Source/Core/DolphinNoGUI/MainNoGUI.cpp
@@ -116,7 +116,7 @@ void Host_RequestRenderWindowSize(int width, int height)
 {
 }
 
-bool Host_UIHasFocus()
+bool Host_UINeedsControllerState()
 {
   return false;
 }
diff --git a/Source/Core/DolphinQt2/Host.cpp b/Source/Core/DolphinQt2/Host.cpp
index fdd3b5aecd..79a48d3e5d 100644
--- a/Source/Core/DolphinQt2/Host.cpp
+++ b/Source/Core/DolphinQt2/Host.cpp
@@ -103,7 +103,7 @@ void Host_UpdateMainFrame()
 void Host_RequestRenderWindowSize(int w, int h)
 {
 }
-bool Host_UIHasFocus()
+bool Host_UINeedsControllerState()
 {
   return false;
 }
diff --git a/Source/Core/DolphinWX/CMakeLists.txt b/Source/Core/DolphinWX/CMakeLists.txt
index 843ef69258..505d11268f 100644
--- a/Source/Core/DolphinWX/CMakeLists.txt
+++ b/Source/Core/DolphinWX/CMakeLists.txt
@@ -78,6 +78,7 @@ set(SRCS
   PostProcessingConfigDiag.cpp
   SoftwareVideoConfigDialog.cpp
   TASInputDlg.cpp
+  UINeedsControllerState.cpp
   VideoConfigDiag.cpp
   WxEventUtils.cpp
   WxUtils.cpp
diff --git a/Source/Core/DolphinWX/DolphinWX.vcxproj b/Source/Core/DolphinWX/DolphinWX.vcxproj
index c8ff9f30dd..1904e297ff 100644
--- a/Source/Core/DolphinWX/DolphinWX.vcxproj
+++ b/Source/Core/DolphinWX/DolphinWX.vcxproj
@@ -125,6 +125,7 @@
     <ClCompile Include="PatchAddEdit.cpp" />
     <ClCompile Include="SoftwareVideoConfigDialog.cpp" />
     <ClCompile Include="TASInputDlg.cpp" />
+    <ClCompile Include="UINeedsControllerState.cpp" />
     <ClCompile Include="VideoConfigDiag.cpp" />
     <ClCompile Include="PostProcessingConfigDiag.cpp" />
     <ClCompile Include="ControllerConfigDiag.cpp" />
@@ -203,6 +204,7 @@
     <ClInclude Include="PatchAddEdit.h" />
     <ClInclude Include="SoftwareVideoConfigDialog.h" />
     <ClInclude Include="TASInputDlg.h" />
+    <ClInclude Include="UINeedsControllerState.h" />
     <ClInclude Include="VideoConfigDiag.h" />
     <ClInclude Include="PostProcessingConfigDiag.h" />
     <ClInclude Include="ControllerConfigDiag.h" />
diff --git a/Source/Core/DolphinWX/DolphinWX.vcxproj.filters b/Source/Core/DolphinWX/DolphinWX.vcxproj.filters
index aaedfc7269..5555786156 100644
--- a/Source/Core/DolphinWX/DolphinWX.vcxproj.filters
+++ b/Source/Core/DolphinWX/DolphinWX.vcxproj.filters
@@ -196,6 +196,9 @@
     <ClCompile Include="TASInputDlg.cpp">
       <Filter>GUI</Filter>
     </ClCompile>
+    <ClCompile Include="UINeedsControllerState.cpp">
+      <Filter>GUI</Filter>
+    </ClCompile>
     <ClCompile Include="SoftwareVideoConfigDialog.cpp">
       <Filter>GUI\Video</Filter>
     </ClCompile>
@@ -420,6 +423,9 @@
     <ClInclude Include="TASInputDlg.h">
       <Filter>GUI</Filter>
     </ClInclude>
+    <ClInclude Include="UINeedsControllerState.h">
+      <Filter>GUI</Filter>
+    </ClInclude>
     <ClInclude Include="SoftwareVideoConfigDialog.h">
       <Filter>GUI\Video</Filter>
     </ClInclude>
diff --git a/Source/Core/DolphinWX/Input/InputConfigDiag.cpp b/Source/Core/DolphinWX/Input/InputConfigDiag.cpp
index b0e5c96371..15c213ac94 100644
--- a/Source/Core/DolphinWX/Input/InputConfigDiag.cpp
+++ b/Source/Core/DolphinWX/Input/InputConfigDiag.cpp
@@ -55,6 +55,7 @@
 #include "DolphinWX/Input/GuitarInputConfigDiag.h"
 #include "DolphinWX/Input/NunchukInputConfigDiag.h"
 #include "DolphinWX/Input/TurntableInputConfigDiag.h"
+#include "DolphinWX/UINeedsControllerState.h"
 #include "DolphinWX/WxUtils.h"
 
 #include "InputCommon/ControlReference/ControlReference.h"
@@ -1259,6 +1260,7 @@ InputConfigDialog::InputConfigDialog(wxWindow* const parent, InputConfig& config
 {
   Bind(wxEVT_CLOSE_WINDOW, &InputConfigDialog::OnClose, this);
   Bind(wxEVT_BUTTON, &InputConfigDialog::OnCloseButton, this, wxID_CLOSE);
+  Bind(wxEVT_ACTIVATE, &InputConfigDialog::OnActivate, this);
 
   SetLayoutAdaptationMode(wxDIALOG_ADAPTATION_MODE_ENABLED);
   SetLayoutAdaptationLevel(wxDIALOG_ADAPTATION_STANDARD_SIZER);
@@ -1269,6 +1271,12 @@ InputConfigDialog::InputConfigDialog(wxWindow* const parent, InputConfig& config
   m_update_timer.Start(PREVIEW_UPDATE_TIME, wxTIMER_CONTINUOUS);
 }
 
+void InputConfigDialog::OnActivate(wxActivateEvent& event)
+{
+  // Needed for input bitmaps
+  SetUINeedsControllerState(event.GetActive());
+}
+
 InputEventFilter::InputEventFilter()
 {
   wxEvtHandler::AddFilter(this);
diff --git a/Source/Core/DolphinWX/Input/InputConfigDiag.h b/Source/Core/DolphinWX/Input/InputConfigDiag.h
index 81dbb1760f..f1aa037bab 100644
--- a/Source/Core/DolphinWX/Input/InputConfigDiag.h
+++ b/Source/Core/DolphinWX/Input/InputConfigDiag.h
@@ -191,6 +191,7 @@ public:
                     const int port_num = 0);
   virtual ~InputConfigDialog() = default;
 
+  void OnActivate(wxActivateEvent& event);
   void OnClose(wxCloseEvent& event);
   void OnCloseButton(wxCommandEvent& event);
 
diff --git a/Source/Core/DolphinWX/Main.cpp b/Source/Core/DolphinWX/Main.cpp
index 70f34ab482..19da6231e4 100644
--- a/Source/Core/DolphinWX/Main.cpp
+++ b/Source/Core/DolphinWX/Main.cpp
@@ -47,6 +47,7 @@
 #include "DolphinWX/Main.h"
 #include "DolphinWX/NetPlay/NetWindow.h"
 #include "DolphinWX/SoftwareVideoConfigDialog.h"
+#include "DolphinWX/UINeedsControllerState.h"
 #include "DolphinWX/VideoConfigDiag.h"
 #include "DolphinWX/WxUtils.h"
 
@@ -486,9 +487,9 @@ void Host_SetWiiMoteConnectionState(int _State)
   main_frame->GetEventHandler()->AddPendingEvent(event);
 }
 
-bool Host_UIHasFocus()
+bool Host_UINeedsControllerState()
 {
-  return wxGetApp().IsActiveThreadsafe();
+  return wxGetApp().IsActiveThreadsafe() && GetUINeedsControllerState();
 }
 
 bool Host_RendererHasFocus()
diff --git a/Source/Core/DolphinWX/UINeedsControllerState.cpp b/Source/Core/DolphinWX/UINeedsControllerState.cpp
new file mode 100644
index 0000000000..bffeef1b59
--- /dev/null
+++ b/Source/Core/DolphinWX/UINeedsControllerState.cpp
@@ -0,0 +1,17 @@
+// Copyright 2017 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#include <atomic>
+
+static std::atomic<bool> s_needs_controller_state{false};
+
+void SetUINeedsControllerState(bool needs_controller_state)
+{
+  s_needs_controller_state = needs_controller_state;
+}
+
+bool GetUINeedsControllerState()
+{
+  return s_needs_controller_state;
+}
diff --git a/Source/Core/DolphinWX/UINeedsControllerState.h b/Source/Core/DolphinWX/UINeedsControllerState.h
new file mode 100644
index 0000000000..3548abc9f6
--- /dev/null
+++ b/Source/Core/DolphinWX/UINeedsControllerState.h
@@ -0,0 +1,8 @@
+// Copyright 2017 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#pragma once
+
+void SetUINeedsControllerState(bool needs_controller_state);
+bool GetUINeedsControllerState();
diff --git a/Source/Core/InputCommon/ControlReference/ControlReference.cpp b/Source/Core/InputCommon/ControlReference/ControlReference.cpp
index c92d3601ed..e4162e22a7 100644
--- a/Source/Core/InputCommon/ControlReference/ControlReference.cpp
+++ b/Source/Core/InputCommon/ControlReference/ControlReference.cpp
@@ -17,7 +17,8 @@ constexpr ControlState INPUT_DETECT_THRESHOLD = 0.55;
 
 bool ControlReference::InputGateOn()
 {
-  return SConfig::GetInstance().m_BackgroundInput || Host_RendererHasFocus();
+  return SConfig::GetInstance().m_BackgroundInput || Host_RendererHasFocus() ||
+         Host_UINeedsControllerState();
 }
 
 //
diff --git a/Source/UnitTests/StubHost.cpp b/Source/UnitTests/StubHost.cpp
index 1519af27a6..019833650c 100644
--- a/Source/UnitTests/StubHost.cpp
+++ b/Source/UnitTests/StubHost.cpp
@@ -39,7 +39,7 @@ void Host_RequestRenderWindowSize(int, int)
 void Host_SetStartupDebuggingParameters()
 {
 }
-bool Host_UIHasFocus()
+bool Host_UINeedsControllerState()
 {
   return false;
 }