From 22f533daef21906f7b3156d70180f49900cc445a Mon Sep 17 00:00:00 2001 From: Jonian Guveli Date: Fri, 6 Sep 2024 19:40:11 +0300 Subject: [PATCH] Libretro: Add support for cursor auto-hide --- src/libretro_core.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/libretro_core.cpp b/src/libretro_core.cpp index 0aaa951a..da132856 100644 --- a/src/libretro_core.cpp +++ b/src/libretro_core.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include @@ -20,6 +21,10 @@ static std::filesystem::path savePath; static std::string touchScreenMode; static bool renderTouchScreen; +static auto cursorTimeout = 0; +static auto cursorMovedAt = std::chrono::steady_clock::now(); +static bool cursorVisible = false; + static bool screenTouched; static int lastMouseX; static int lastMouseY; @@ -209,6 +214,7 @@ static void configInit() { {"panda3ds_use_charger", "Charger plugged; enabled|disabled"}, {"panda3ds_touchscreen_mode", "Touchscreen touch mode; Auto|Pointer|Joystick|None"}, {"panda3ds_render_touchscreen", "Render touchscreen pointer; disabled|enabled"}, + {"panda3ds_hide_cursor_timeout", "Hide touchScreen pointer timeout; 3 Seconds|5 Seconds|10 Seconds|15 Seconds|20 Seconds|Never Hide"}, {nullptr, nullptr}, }; @@ -241,6 +247,7 @@ static void configUpdate() { touchScreenMode = fetchVariable("panda3ds_touchscreen_mode", "Auto"); renderTouchScreen = fetchVariableBool("panda3ds_render_touchscreen", false); + cursorTimeout = fetchVariableInt("panda3ds_hide_cursor_timeout", 3); // Handle any settings that might need the emulator core to be notified when they're changed, and save the config. emulator->setAudioEnabled(config.audioEnabled); @@ -257,6 +264,21 @@ static void configCheckVariables() { } } +static void updateCursorVisibility() { + if (renderTouchScreen && cursorTimeout) { + if (cursorVisible) { + auto current = std::chrono::steady_clock::now(); + auto elapsed = std::chrono::duration_cast(current - cursorMovedAt).count(); + + if (elapsed >= cursorTimeout) { + cursorVisible = false; + } + } + } else { + cursorVisible = true; + } +} + void CursorRenderer::init() { #ifdef USING_GLES static const std::string version = R"( @@ -425,6 +447,7 @@ void retro_reset() { void retro_run() { configCheckVariables(); + updateCursorVisibility(); renderer->setFBO(hwRender.get_current_framebuffer()); renderer->resetStateManager(); @@ -500,6 +523,11 @@ void retro_run() { } } + if (cursorTimeout && (pointerX != touchX || pointerY != touchY)) { + cursorVisible = true; + cursorMovedAt = std::chrono::steady_clock::now(); + } + touchX = std::clamp(pointerX, 0, (int)(emulator->width - (offsetX * 2))); touchY = std::clamp(pointerY, 0, (int)(emulator->height - offsetY));