Skip to content

Commit

Permalink
Libretro: Add support for cursor auto-hide
Browse files Browse the repository at this point in the history
  • Loading branch information
jonian committed Sep 6, 2024
1 parent 449627b commit 43ad6ae
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/libretro_core.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdexcept>
#include <cstdio>
#include <regex>
#include <chrono>

#include <libretro.h>

Expand All @@ -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;
Expand Down Expand Up @@ -204,6 +209,7 @@ static void configInit() {
{"panda3ds_ubershader_lighting_override_threshold", "Light threshold for forcing shadergen; 1|2|3|4|5|6|7|8"},
{"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},
};

Expand All @@ -230,6 +236,7 @@ static void configUpdate() {

touchScreenMode = fetchVariable("panda3ds_touchscreen_mode", "Auto");
renderTouchScreen = fetchVariableBool("panda3ds_render_touchscreen", false);
cursorTimeout = fetchVariableInt("panda3ds_hide_cursor_timeout", 3);

config.save();
}
Expand All @@ -243,6 +250,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<std::chrono::seconds>(current - cursorMovedAt).count();

if (elapsed >= cursorTimeout) {
cursorVisible = false;
}
}
} else {
cursorVisible = true;
}
}

void CursorRenderer::init() {
#ifdef USING_GLES
static const std::string version = R"(
Expand Down Expand Up @@ -411,6 +433,7 @@ void retro_reset() {

void retro_run() {
configCheckVariables();
updateCursorVisibility();

renderer->setFBO(hwRender.get_current_framebuffer());
renderer->resetStateManager();
Expand Down Expand Up @@ -486,6 +509,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));

Expand Down

0 comments on commit 43ad6ae

Please sign in to comment.