Skip to content

Commit 3c8d068

Browse files
committed
Libretro: Add support for cursor auto-hide
1 parent 470aec7 commit 3c8d068

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/libretro_core.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdexcept>
22
#include <cstdio>
33
#include <regex>
4+
#include <chrono>
45

56
#include <libretro.h>
67

@@ -20,6 +21,10 @@ static std::filesystem::path savePath;
2021
static std::string touchScreenMode;
2122
static bool renderTouchScreen;
2223

24+
static auto cursorTimeout = 0;
25+
static auto cursorMovedAt = std::chrono::steady_clock::now();
26+
static bool cursorVisible = false;
27+
2328
static bool screenTouched;
2429
static int lastMouseX;
2530
static int lastMouseY;
@@ -168,6 +173,16 @@ static std::string FetchVariable(std::string key, std::string def) {
168173
return std::string(var.value);
169174
}
170175

176+
static int FetchVariableInt(std::string key, int def) {
177+
std::string value = FetchVariable(key, std::to_string(def));
178+
179+
if (!value.empty() && std::isdigit(value[0])) {
180+
return std::stoi(value);
181+
}
182+
183+
return 0;
184+
}
185+
171186
static bool FetchVariableBool(std::string key, bool def) {
172187
return FetchVariable(key, def ? "enabled" : "disabled") == "enabled";
173188
}
@@ -189,6 +204,7 @@ static void configInit() {
189204
{"panda3ds_ubershader_lighting_override_threshold", "Light threshold for forcing shadergen; 1|2|3|4|5|6|7|8"},
190205
{"panda3ds_touchscreen_mode", "Touchscreen touch mode; Auto|Pointer|Joystick|None"},
191206
{"panda3ds_render_touchscreen", "Render touchscreen pointer; disabled|enabled"},
207+
{"panda3ds_hide_cursor_timeout", "Hide touchScreen pointer timeout; 3 Seconds|5 Seconds|10 Seconds|15 Seconds|20 Seconds|Never Hide"},
192208
{nullptr, nullptr},
193209
};
194210

@@ -215,6 +231,7 @@ static void configUpdate() {
215231

216232
touchScreenMode = FetchVariable("panda3ds_touchscreen_mode", "Auto");
217233
renderTouchScreen = FetchVariableBool("panda3ds_render_touchscreen", false);
234+
cursorTimeout = FetchVariableInt("panda3ds_hide_cursor_timeout", 3);
218235

219236
config.save();
220237
}
@@ -228,6 +245,21 @@ static void ConfigCheckVariables() {
228245
}
229246
}
230247

248+
static void UpdateCursorVisibility() {
249+
if (renderTouchScreen && cursorTimeout) {
250+
if (cursorVisible) {
251+
auto current = std::chrono::steady_clock::now();
252+
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(current - cursorMovedAt).count();
253+
254+
if (elapsed >= cursorTimeout) {
255+
cursorVisible = false;
256+
}
257+
}
258+
} else {
259+
cursorVisible = true;
260+
}
261+
}
262+
231263
void CursorRenderer::init() {
232264
#ifdef USING_GLES
233265
static const std::string version = R"(
@@ -396,6 +428,7 @@ void retro_reset() {
396428

397429
void retro_run() {
398430
ConfigCheckVariables();
431+
UpdateCursorVisibility();
399432

400433
renderer->setFBO(hw_render.get_current_framebuffer());
401434
renderer->resetStateManager();
@@ -471,6 +504,11 @@ void retro_run() {
471504
}
472505
}
473506

507+
if (cursorTimeout && (pointerX != touchX || pointerY != touchY)) {
508+
cursorVisible = true;
509+
cursorMovedAt = std::chrono::steady_clock::now();
510+
}
511+
474512
touchX = std::clamp(pointerX, 0, (int)(emulator->width - (offsetX * 2)));
475513
touchY = std::clamp(pointerY, 0, (int)(emulator->height - offsetY));
476514

@@ -488,7 +526,7 @@ void retro_run() {
488526
hid.updateInputs(emulator->getTicks());
489527
emulator->runFrame();
490528

491-
if (renderTouchScreen) {
529+
if (renderTouchScreen && cursorVisible) {
492530
cursorRenderer->draw(touchX, touchY);
493531
}
494532

0 commit comments

Comments
 (0)