From ae0665214820601942d783a0ec0fa2017505fd48 Mon Sep 17 00:00:00 2001 From: bicarus-dev <202771338+bicarus-dev@users.noreply.github.com> Date: Fri, 4 Apr 2025 21:31:18 -0700 Subject: [PATCH] touch: avoid calling is_touch_available too early (#289) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Link to GitHub Issue, if one exists Fixes #120 ## Description of change Don't call `is_touch_available` in launcher before RawInput stack is initialized. This causes us to incorrectly fall back to the Win7/Win8 touch handler. The desired behavior is to use rawinput touch by default for all games, unless overridden by the user via `-wintouch`. Add a bunch of logging so we can spot who's calling `is_touch_available`. As a side effect, a handful of games that previously used Win7/8 touch will now use RawInput touch instead. If this causes any issues, they can turn `-wintouch` on to force Win7/8 touch logic again. ## Compiling 👍 ## Testing Test: * RB * -wintouch * TDJ, UFC subscreen and native touch * overlay, especially in multi-mon --- src/spice2x/games/qma/qma.cpp | 2 +- src/spice2x/games/rb/touch.cpp | 2 +- src/spice2x/games/sc/sc.cpp | 2 +- src/spice2x/launcher/launcher.cpp | 53 +++++++------------------ src/spice2x/misc/wintouchemu.cpp | 2 +- src/spice2x/overlay/overlay.cpp | 2 +- src/spice2x/overlay/windows/control.cpp | 2 +- src/spice2x/touch/touch.cpp | 30 +++++++++++--- src/spice2x/touch/touch.h | 2 +- 9 files changed, 46 insertions(+), 51 deletions(-) diff --git a/src/spice2x/games/qma/qma.cpp b/src/spice2x/games/qma/qma.cpp index 9db9e38..53dd544 100644 --- a/src/spice2x/games/qma/qma.cpp +++ b/src/spice2x/games/qma/qma.cpp @@ -63,7 +63,7 @@ namespace games::qma { touch_attach_dx_hook(); // cursor - if (!is_touch_available()) { + if (!is_touch_available("QMATouchDevice::open")) { ShowCursor(true); } } diff --git a/src/spice2x/games/rb/touch.cpp b/src/spice2x/games/rb/touch.cpp index dac0683..4831d1c 100644 --- a/src/spice2x/games/rb/touch.cpp +++ b/src/spice2x/games/rb/touch.cpp @@ -86,7 +86,7 @@ bool games::rb::ReflecBeatTouchDeviceHandle::open(LPCWSTR lpFileName) { } // show cursor on window if mouse is used - if (!is_touch_available()) { + if (!is_touch_available("ReflecBeatTouchDeviceHandle::open")) { ShowCursor(TRUE); } diff --git a/src/spice2x/games/sc/sc.cpp b/src/spice2x/games/sc/sc.cpp index 2700c9a..19d4cd2 100644 --- a/src/spice2x/games/sc/sc.cpp +++ b/src/spice2x/games/sc/sc.cpp @@ -72,7 +72,7 @@ public: touch_attach_dx_hook(); // cursor - if (!is_touch_available()) { + if (!is_touch_available("TwTouchDevice::open")) { ShowCursor(true); } } diff --git a/src/spice2x/launcher/launcher.cpp b/src/spice2x/launcher/launcher.cpp index 52dd72e..ec936ae 100644 --- a/src/spice2x/launcher/launcher.cpp +++ b/src/spice2x/launcher/launcher.cpp @@ -217,6 +217,7 @@ int main_implementation(int argc, char *argv[]) { bool attach_qks = false; bool attach_mfg = false; bool attach_museca = false; + bool show_cursor_if_no_touch = false; // misc settings size_t user_heap_size = 0; @@ -1291,11 +1292,7 @@ int main_implementation(int argc, char *argv[]) { avs::game::DLL_NAME = "system.dll"; attach_io = true; attach_shogikai = true; - - // automatically show cursor when no touchscreen is available - if (!is_touch_available()) { - GRAPHICS_SHOW_CURSOR = true; - } + show_cursor_if_no_touch = true; break; } @@ -1338,12 +1335,7 @@ int main_implementation(int argc, char *argv[]) { // game crash fix easrv_maint = false; - - // automatically show cursor when no touchscreen is available - if (!is_touch_available()) { - GRAPHICS_SHOW_CURSOR = true; - } - + show_cursor_if_no_touch = true; break; } @@ -1458,12 +1450,7 @@ int main_implementation(int argc, char *argv[]) { avs::game::DLL_NAME = "nostalgia.dll"; attach_io = true; attach_nostalgia = true; - - // automatically show cursor when no touchscreen is available - if (!is_touch_available()) { - GRAPHICS_SHOW_CURSOR = true; - } - + show_cursor_if_no_touch = true; break; } @@ -1569,12 +1556,7 @@ int main_implementation(int argc, char *argv[]) { avs::game::DLL_NAME = "arknck.dll"; attach_io = true; attach_we = true; - - // automatically show cursor when no touchscreen is available - if (!is_touch_available()) { - GRAPHICS_SHOW_CURSOR = true; - } - + show_cursor_if_no_touch = true; break; } @@ -1607,10 +1589,7 @@ int main_implementation(int argc, char *argv[]) { avs::game::DLL_NAME = "kamunity.dll"; attach_io = true; attach_ccj = true; - // automatically show cursor when no touchscreen is available - if (!is_touch_available()) { - GRAPHICS_SHOW_CURSOR = true; - } + show_cursor_if_no_touch = true; break; } @@ -1619,10 +1598,7 @@ int main_implementation(int argc, char *argv[]) { avs::game::DLL_NAME = "kamunity.dll"; attach_io = true; attach_qks = true; - // automatically show cursor when no touchscreen is available - if (!is_touch_available()) { - GRAPHICS_SHOW_CURSOR = true; - } + show_cursor_if_no_touch = true; break; } @@ -1632,10 +1608,7 @@ int main_implementation(int argc, char *argv[]) { attach_io = true; attach_mfg = true; launcher::signal::USE_VEH_WORKAROUND = true; - // automatically show cursor when no touchscreen is available - if (!is_touch_available()) { - GRAPHICS_SHOW_CURSOR = true; - } + show_cursor_if_no_touch = true; break; } @@ -1644,10 +1617,7 @@ int main_implementation(int argc, char *argv[]) { avs::game::DLL_NAME = "kamunity.dll"; attach_io = true; attach_bc = true; - // automatically show cursor when no touchscreen is available - if (!is_touch_available()) { - GRAPHICS_SHOW_CURSOR = true; - } + show_cursor_if_no_touch = true; break; } @@ -1843,6 +1813,11 @@ int main_implementation(int argc, char *argv[]) { // print devices RI_MGR->devices_print(); + // for certain games, show cursor if no touch is available (must be called after RI_MGR is available) + if (show_cursor_if_no_touch && !is_touch_available("launcher::main_implementation")) { + GRAPHICS_SHOW_CURSOR = true; + } + // cardio if (cardio_enabled) { cardio_runner_start(true); diff --git a/src/spice2x/misc/wintouchemu.cpp b/src/spice2x/misc/wintouchemu.cpp index 9cca98f..ce91830 100644 --- a/src/spice2x/misc/wintouchemu.cpp +++ b/src/spice2x/misc/wintouchemu.cpp @@ -36,7 +36,7 @@ namespace wintouchemu { bool LOG_FPS = false; static inline bool is_emu_enabled() { - return FORCE || !is_touch_available() || GRAPHICS_SHOW_CURSOR; + return FORCE || !is_touch_available("wintouchemu::is_emu_enabled") || GRAPHICS_SHOW_CURSOR; } static decltype(GetSystemMetrics) *GetSystemMetrics_orig = nullptr; diff --git a/src/spice2x/overlay/overlay.cpp b/src/spice2x/overlay/overlay.cpp index 9dd3d06..097597d 100644 --- a/src/spice2x/overlay/overlay.cpp +++ b/src/spice2x/overlay/overlay.cpp @@ -247,7 +247,7 @@ void overlay::SpiceOverlay::init() { io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; } - if (is_touch_available()) { + if (is_touch_available("SpiceOverlay::init")) { io.ConfigFlags |= ImGuiConfigFlags_IsTouchScreen; } diff --git a/src/spice2x/overlay/windows/control.cpp b/src/spice2x/overlay/windows/control.cpp index 3ebd9d5..84d5302 100644 --- a/src/spice2x/overlay/windows/control.cpp +++ b/src/spice2x/overlay/windows/control.cpp @@ -751,7 +751,7 @@ namespace overlay::windows { if (ImGui::CollapsingHeader("Touch")) { // status - ImGui::Text("Status: %s", is_touch_available() ? "available" : "unavailable"); + ImGui::Text("Status: %s", is_touch_available("Control::touch_view") ? "available" : "unavailable"); // touch points ImGui::SetNextItemOpen(true, ImGuiCond_Once); diff --git a/src/spice2x/touch/touch.cpp b/src/spice2x/touch/touch.cpp index 72541c2..fbea901 100644 --- a/src/spice2x/touch/touch.cpp +++ b/src/spice2x/touch/touch.cpp @@ -138,8 +138,15 @@ static void touch_initialize() { } SPICETOUCH_INITIALIZED = true; + if (!RI_MGR) { + log_fatal( + LOG_MODULE_NAME, + "touch_initialize() - RI Manager not available, called too early! " + "This is a BUG in spice, please file a bug report with log.txt."); + } + // initialize handler - if (RI_MGR && rawinput::touch::is_enabled(RI_MGR.get())) { + if (rawinput::touch::is_enabled(RI_MGR.get())) { TOUCH_HANDLER = new RawInputTouchHandler(); } else if (Win8Handler::is_available()) { TOUCH_HANDLER = new Win8Handler(); @@ -166,13 +173,26 @@ static inline void touch_unregister_window(HWND hWnd) { } } -bool is_touch_available() { +bool is_touch_available(LPCSTR caller) { + static bool called_once = false; + + if (!RI_MGR) { + log_fatal( + LOG_MODULE_NAME, + "is_touch_available({}) - RI Manager not available, called too early! " + "This is a BUG in spice, please file a bug report with log.txt.", + caller); + } // initialize touch handler touch_initialize(); - // Check if a touch handler has been set. No need to call `is_available` here - // as `touch_initialize` does that. + if (!called_once) { + log_misc("touch", "is_touch_available called by: {}, returning {}", + caller, (TOUCH_HANDLER != nullptr) ? "TRUE" : "false"); + called_once = true; + } + return TOUCH_HANDLER != nullptr; } @@ -202,7 +222,7 @@ static LRESULT CALLBACK SpiceTouchWndProc(HWND hWnd, UINT msg, WPARAM wParam, LP SPICETOUCH_REGISTERED_TOUCH = true; // check if touch is available - if (is_touch_available()) { + if (is_touch_available("SpiceTouchWndProc")) { // notify the handler of our window TOUCH_HANDLER->window_register(hWnd); diff --git a/src/spice2x/touch/touch.h b/src/spice2x/touch/touch.h index 6cf4bb9..ec041b3 100644 --- a/src/spice2x/touch/touch.h +++ b/src/spice2x/touch/touch.h @@ -27,7 +27,7 @@ extern int SPICETOUCH_TOUCH_Y; extern int SPICETOUCH_TOUCH_WIDTH; extern int SPICETOUCH_TOUCH_HEIGHT; -bool is_touch_available(); +bool is_touch_available(LPCSTR caller); void touch_attach_wnd(HWND hWnd); void touch_attach_dx_hook();