diff --git a/src/app/ControllerManager.cpp b/src/app/ControllerManager.cpp index 86d6604..beb54ee 100644 --- a/src/app/ControllerManager.cpp +++ b/src/app/ControllerManager.cpp @@ -20,9 +20,24 @@ void ControllerManager::OnDeviceChange() { TryOpen(); else if (g_ctrl && !g_ctrl->IsOpen()) Close(/*restoreLizard=*/false); + + if (m_connected && m_wantsGameMode && !m_gameModeActive) + EnableGameMode(); +} + +void ControllerManager::Tick() { + if (!m_connected) { + TryOpen(); + } else if (g_ctrl && !g_ctrl->IsOpen()) { + Close(/*restoreLizard=*/false); + } + + if (m_connected && m_wantsGameMode && !m_gameModeActive) + EnableGameMode(); } void ControllerManager::EnableGameMode() { + m_wantsGameMode = true; if (!m_connected || m_gameModeActive) return; if (!g_ctrl->DisableLizardMode()) return; @@ -52,6 +67,7 @@ void ControllerManager::EnableGameMode() { } void ControllerManager::DisableGameMode() { + m_wantsGameMode = false; if (!m_gameModeActive) return; StopReadLoop(); if (g_ctrl && g_ctrl->IsOpen()) @@ -83,6 +99,8 @@ void ControllerManager::TryOpen() { if (g_ctrl->Open()) { m_connected = true; m_onStateChanged(m_connected, m_gameModeActive, false); + if (m_wantsGameMode && !m_gameModeActive) + EnableGameMode(); } } diff --git a/src/app/ControllerManager.h b/src/app/ControllerManager.h index 1d6a3eb..39c399e 100644 --- a/src/app/ControllerManager.h +++ b/src/app/ControllerManager.h @@ -21,6 +21,7 @@ public: // Called when Windows reports a device arrival or removal (WM_DEVICECHANGE). void OnDeviceChange(); + void Tick(); // Toggle game mode on/off. No-op if controller is not connected. void EnableGameMode(); @@ -46,6 +47,7 @@ private: StateChangedFn m_onStateChanged; bool m_connected = false; bool m_gameModeActive = false; + bool m_wantsGameMode = true; bool m_trackpadMouseEnabled = false; bool m_backButtonsEnabled = false; bool m_useLeftTrackpad = false; diff --git a/src/app/TrayApp.cpp b/src/app/TrayApp.cpp index ec99600..b0fed27 100644 --- a/src/app/TrayApp.cpp +++ b/src/app/TrayApp.cpp @@ -14,6 +14,8 @@ TrayApp::TrayApp() { } TrayApp::~TrayApp() { + if (m_hwnd) + KillTimer(m_hwnd, TIMER_RECONNECT); RemoveTrayIcon(); g_app = nullptr; } @@ -52,6 +54,7 @@ bool TrayApp::Init(HINSTANCE hInstance) { LoadSettings(); AddTrayIcon(); + SetTimer(m_hwnd, TIMER_RECONNECT, 2000, nullptr); return true; } @@ -121,6 +124,11 @@ LRESULT TrayApp::HandleMessage(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { m_controller->OnDeviceChange(); return TRUE; + case WM_TIMER: + if (wp == TIMER_RECONNECT) + m_controller->Tick(); + return 0; + case WM_DESTROY: PostQuitMessage(0); return 0; diff --git a/src/app/TrayApp.h b/src/app/TrayApp.h index 8914583..5825ce6 100644 --- a/src/app/TrayApp.h +++ b/src/app/TrayApp.h @@ -39,6 +39,7 @@ private: static constexpr UINT IDM_BACKBUTTONS = 1004; static constexpr UINT IDM_LEFT_TRACKPAD = 1005; static constexpr UINT IDM_STARTUP = 1006; + static constexpr UINT_PTR TIMER_RECONNECT = 1; static constexpr UINT WM_TRAY = WM_APP + 1; static constexpr UINT TRAY_UID = 1; };