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 Aug 24, 2024
1 parent 470aec7 commit 3c8d068
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion 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 @@ -168,6 +173,16 @@ static std::string FetchVariable(std::string key, std::string def) {
return std::string(var.value);
}

static int FetchVariableInt(std::string key, int def) {
std::string value = FetchVariable(key, std::to_string(def));

if (!value.empty() && std::isdigit(value[0])) {
return std::stoi(value);
}

return 0;
}

static bool FetchVariableBool(std::string key, bool def) {
return FetchVariable(key, def ? "enabled" : "disabled") == "enabled";
}
Expand All @@ -189,6 +204,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 @@ -215,6 +231,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 @@ -228,6 +245,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 @@ -396,6 +428,7 @@ void retro_reset() {

void retro_run() {
ConfigCheckVariables();
UpdateCursorVisibility();

renderer->setFBO(hw_render.get_current_framebuffer());
renderer->resetStateManager();
Expand Down Expand Up @@ -471,6 +504,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 All @@ -488,7 +526,7 @@ void retro_run() {
hid.updateInputs(emulator->getTicks());
emulator->runFrame();

if (renderTouchScreen) {
if (renderTouchScreen && cursorVisible) {
cursorRenderer->draw(touchX, touchY);
}

Expand Down

0 comments on commit 3c8d068

Please sign in to comment.