window: auto-hide the cursor over the game window (#958)

* window: auto-hide the cursor over the game window

Hide the cursor when the game window is created and whenever the mouse
has been idle for two seconds; any mouse motion, button or wheel event
shows it again. Games that draw their own cursor (e.g. Blue Prince) no
longer show the host pointer on top of it.

Only real mouse input counts as activity: synthetic touch/pen mouse
events and zero-delta motion (sent by some platforms on focus or cursor
visibility changes) are ignored. The event loop only waits with a
timeout while the cursor is visible, so it still blocks indefinitely
once the cursor is hidden. The cursor is restored on shutdown.

Uses SDL_HideCursor/SDL_ShowCursor, which SDL implements for X11,
Wayland, Cocoa and Win32 and which only affect the cursor over the
emulator window.

Closes #282

* window: make cursor auto-hide optional in CLI and launcher

Co-authored-by: Anatolii Kucheruk <anatolii.kucheruk01@gmail.com>

---------

Co-authored-by: nmzik <Nmzik@mail.ru>
Co-authored-by: Anatolii Kucheruk <anatolii.kucheruk01@gmail.com>
This commit is contained in:
Aryan Sharma
2026-10-03 19:45:15 +05:30
committed by GitHub
parent 820856b301
commit feeced81d2
8 changed files with 78 additions and 8 deletions
+4
View File
@@ -72,6 +72,10 @@ bool FullscreenEnabled() {
return g_config->fullscreen_enabled; return g_config->fullscreen_enabled;
} }
bool HideCursorEnabled() {
return g_config->hide_cursor_enabled;
}
bool VrEnabled() { bool VrEnabled() {
return g_config->vr_enabled; return g_config->vr_enabled;
} }
+2
View File
@@ -53,6 +53,7 @@ struct ConfigOptions {
PresentMode present_mode = PresentMode::Mailbox; PresentMode present_mode = PresentMode::Mailbox;
int32_t gpu_index = -1; int32_t gpu_index = -1;
bool fullscreen_enabled = false; bool fullscreen_enabled = false;
bool hide_cursor_enabled = false;
bool vr_enabled = false; bool vr_enabled = false;
bool amd_cpu_enabled = false; bool amd_cpu_enabled = false;
uint32_t vblank_frequency = 60; uint32_t vblank_frequency = 60;
@@ -93,6 +94,7 @@ uint32_t GetControllerVibrationIntensity();
PresentMode GetPresentMode(); PresentMode GetPresentMode();
int32_t GetGpuIndex(); int32_t GetGpuIndex();
bool FullscreenEnabled(); bool FullscreenEnabled();
bool HideCursorEnabled();
bool VrEnabled(); bool VrEnabled();
bool AmdCpuEnabled(); bool AmdCpuEnabled();
uint32_t GetVblankFrequency(); uint32_t GetVblankFrequency();
+50 -8
View File
@@ -72,6 +72,7 @@ struct Binding {
}; };
constexpr int MOUSE_POLL_INTERVAL_MS = 33; constexpr int MOUSE_POLL_INTERVAL_MS = 33;
constexpr uint64_t CURSOR_IDLE_HIDE_MS = 2000;
constexpr std::string_view MOUSE_SENSITIVITY = "MouseSensitivity="; constexpr std::string_view MOUSE_SENSITIVITY = "MouseSensitivity=";
struct MouseJoystickState { struct MouseJoystickState {
@@ -81,7 +82,8 @@ struct MouseJoystickState {
}; };
MouseJoystickState g_mouse; MouseJoystickState g_mouse;
SDL_Window* g_mouse_window = nullptr; SDL_Window* g_mouse_window = nullptr;
uint64_t g_cursor_hide_at = 0;
std::size_t ControlFromName(std::string_view name) { std::size_t ControlFromName(std::string_view name) {
const auto info = std::find_if(CONTROL_INFO.begin(), CONTROL_INFO.end(), const auto info = std::find_if(CONTROL_INFO.begin(), CONTROL_INFO.end(),
@@ -380,11 +382,31 @@ int PollMouse(uint64_t now_ms) {
return MOUSE_POLL_INTERVAL_MS; return MOUSE_POLL_INTERVAL_MS;
} }
bool IsCursorActivity(const SDL_Event& event) {
SDL_MouseID which;
switch (event.type) {
case SDL_EVENT_MOUSE_MOTION:
if (event.motion.xrel == 0.0f && event.motion.yrel == 0.0f) {
return false;
}
which = event.motion.which;
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP: which = event.button.which; break;
case SDL_EVENT_MOUSE_WHEEL: which = event.wheel.which; break;
default: return false;
}
return which != SDL_TOUCH_MOUSEID && which != SDL_PEN_MOUSEID;
}
} // namespace } // namespace
void HostInputInit(SDL_Window* window) { void HostInputInit(SDL_Window* window) {
GetInputMap(); GetInputMap();
g_mouse_window = window; g_mouse_window = window;
if (Config::HideCursorEnabled()) {
g_cursor_hide_at = SDL_GetTicks() + CURSOR_IDLE_HIDE_MS;
}
} }
void HostInputShutdown() { void HostInputShutdown() {
@@ -393,7 +415,11 @@ void HostInputShutdown() {
CenterMouseStick(); CenterMouseStick();
g_mouse = {}; g_mouse = {};
} }
g_mouse_window = nullptr; g_mouse_window = nullptr;
g_cursor_hide_at = 0;
if (Config::HideCursorEnabled()) {
SDL_ShowCursor();
}
} }
void HostInputKey(int key_code, bool down) { void HostInputKey(int key_code, bool down) {
@@ -429,20 +455,36 @@ void HostInputToggleMouseToJoystick() {
} }
bool HostInputWaitEvent(SDL_Event* event) { bool HostInputWaitEvent(SDL_Event* event) {
bool has_event; int timeout = -1;
if (!g_mouse.enabled || SDL_GetKeyboardFocus() != g_mouse_window) { if (!g_mouse.enabled || SDL_GetKeyboardFocus() != g_mouse_window) {
g_mouse.next_poll = 0; g_mouse.next_poll = 0;
CenterMouseStick(); CenterMouseStick();
has_event = SDL_WaitEvent(event);
if (!has_event) {
EXIT("%s\n", SDL_GetError());
}
} else { } else {
if (g_mouse.next_poll == 0) { if (g_mouse.next_poll == 0) {
SDL_GetRelativeMouseState(nullptr, nullptr); SDL_GetRelativeMouseState(nullptr, nullptr);
g_mouse.next_poll = SDL_GetTicks() + MOUSE_POLL_INTERVAL_MS; g_mouse.next_poll = SDL_GetTicks() + MOUSE_POLL_INTERVAL_MS;
} }
has_event = SDL_WaitEventTimeout(event, PollMouse(SDL_GetTicks())); timeout = PollMouse(SDL_GetTicks());
}
if (g_cursor_hide_at != 0) {
const auto now_ms = SDL_GetTicks();
const int cursor_timeout =
now_ms < g_cursor_hide_at ? static_cast<int>(g_cursor_hide_at - now_ms) : 0;
timeout = timeout < 0 ? cursor_timeout : std::min(timeout, cursor_timeout);
}
const bool has_event = SDL_WaitEventTimeout(event, timeout);
if (Config::HideCursorEnabled()) {
const auto now_ms = SDL_GetTicks();
if (has_event && !g_mouse.enabled && IsCursorActivity(*event) &&
SDL_GetWindowFromEvent(event) == g_mouse_window) {
SDL_ShowCursor();
g_cursor_hide_at = now_ms + CURSOR_IDLE_HIDE_MS;
} else if (g_cursor_hide_at != 0 && now_ms >= g_cursor_hide_at) {
SDL_HideCursor();
g_cursor_hide_at = 0;
}
} }
if (has_event && event->type == SDL_EVENT_WINDOW_FOCUS_LOST && if (has_event && event->type == SDL_EVENT_WINDOW_FOCUS_LOST &&
@@ -173,6 +173,12 @@
<property name="text"><string>Enable tessellation support (experimental)</string></property> <property name="text"><string>Enable tessellation support (experimental)</string></property>
</widget> </widget>
</item> </item>
<item row="8" column="1">
<widget class="QCheckBox" name="checkBox_hide_cursor">
<property name="toolTip"><string>Hide the host cursor after 2 seconds without mouse activity</string></property>
<property name="text"><string>Auto-hide cursor</string></property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>
+4
View File
@@ -119,6 +119,7 @@ public:
PresentMode present_mode = PresentMode::Mailbox; PresentMode present_mode = PresentMode::Mailbox;
int gpu_index = -1; int gpu_index = -1;
bool fullscreen_enabled = false; bool fullscreen_enabled = false;
bool hide_cursor_enabled = false;
bool readback_linear_images = false; bool readback_linear_images = false;
bool tessellation_enabled = false; bool tessellation_enabled = false;
int vblank_frequency = 60; int vblank_frequency = 60;
@@ -150,6 +151,7 @@ public:
present_mode = other.present_mode; present_mode = other.present_mode;
gpu_index = other.gpu_index; gpu_index = other.gpu_index;
fullscreen_enabled = other.fullscreen_enabled; fullscreen_enabled = other.fullscreen_enabled;
hide_cursor_enabled = other.hide_cursor_enabled;
readback_linear_images = other.readback_linear_images; readback_linear_images = other.readback_linear_images;
tessellation_enabled = other.tessellation_enabled; tessellation_enabled = other.tessellation_enabled;
vblank_frequency = other.vblank_frequency; vblank_frequency = other.vblank_frequency;
@@ -196,6 +198,7 @@ public:
KYTY_CFG_SET(present_mode); KYTY_CFG_SET(present_mode);
KYTY_CFG_SET(gpu_index); KYTY_CFG_SET(gpu_index);
KYTY_CFG_SET(fullscreen_enabled); KYTY_CFG_SET(fullscreen_enabled);
KYTY_CFG_SET(hide_cursor_enabled);
KYTY_CFG_SET(readback_linear_images); KYTY_CFG_SET(readback_linear_images);
KYTY_CFG_SET(tessellation_enabled); KYTY_CFG_SET(tessellation_enabled);
KYTY_CFG_SET(vblank_frequency); KYTY_CFG_SET(vblank_frequency);
@@ -238,6 +241,7 @@ public:
present_mode = PresentMode::Mailbox; present_mode = PresentMode::Mailbox;
} }
KYTY_CFG_GET(fullscreen_enabled); KYTY_CFG_GET(fullscreen_enabled);
KYTY_CFG_GET(hide_cursor_enabled);
KYTY_CFG_GET(readback_linear_images); KYTY_CFG_GET(readback_linear_images);
KYTY_CFG_GET(tessellation_enabled); KYTY_CFG_GET(tessellation_enabled);
vblank_frequency = s->value("vblank_frequency", vblank_frequency).toInt(); vblank_frequency = s->value("vblank_frequency", vblank_frequency).toInt();
@@ -273,6 +273,7 @@ void ConfigurationEditDialog::Init(const Configuration& info) {
info.gpu_index >= 0 && info.gpu_index < m_ui->comboBox_gpu->count() - 1 ? info.gpu_index + 1 info.gpu_index >= 0 && info.gpu_index < m_ui->comboBox_gpu->count() - 1 ? info.gpu_index + 1
: 0); : 0);
m_ui->checkBox_fullscreen->setChecked(info.fullscreen_enabled); m_ui->checkBox_fullscreen->setChecked(info.fullscreen_enabled);
m_ui->checkBox_hide_cursor->setChecked(info.hide_cursor_enabled);
m_ui->checkBox_readback->setChecked(info.readback_linear_images); m_ui->checkBox_readback->setChecked(info.readback_linear_images);
m_ui->checkBox_tessellation->setChecked(info.tessellation_enabled); m_ui->checkBox_tessellation->setChecked(info.tessellation_enabled);
m_ui->spinBox_vblank_frequency->setValue(info.vblank_frequency); m_ui->spinBox_vblank_frequency->setValue(info.vblank_frequency);
@@ -425,6 +426,7 @@ static void UpdateInfo(Configuration& info, Ui::ConfigurationEditDialog& ui, boo
TextToEnum<Configuration::PresentMode>(ui.comboBox_present_mode->currentText()); TextToEnum<Configuration::PresentMode>(ui.comboBox_present_mode->currentText());
info.gpu_index = ui.comboBox_gpu->currentIndex() - 1; info.gpu_index = ui.comboBox_gpu->currentIndex() - 1;
info.fullscreen_enabled = ui.checkBox_fullscreen->isChecked(); info.fullscreen_enabled = ui.checkBox_fullscreen->isChecked();
info.hide_cursor_enabled = ui.checkBox_hide_cursor->isChecked();
info.readback_linear_images = ui.checkBox_readback->isChecked(); info.readback_linear_images = ui.checkBox_readback->isChecked();
info.tessellation_enabled = ui.checkBox_tessellation->isChecked(); info.tessellation_enabled = ui.checkBox_tessellation->isChecked();
info.vblank_frequency = ui.spinBox_vblank_frequency->value(); info.vblank_frequency = ui.spinBox_vblank_frequency->value();
+3
View File
@@ -240,6 +240,9 @@ static QStringList CreateEmulatorArgs(const Configuration& info) {
if (info.fullscreen_enabled) { if (info.fullscreen_enabled) {
args << "--fullscreen"; args << "--fullscreen";
} }
if (info.hide_cursor_enabled) {
args << "--hide-cursor";
}
args << "--readback-linear-images" << BoolArg(info.readback_linear_images); args << "--readback-linear-images" << BoolArg(info.readback_linear_images);
if (info.tessellation_enabled) { if (info.tessellation_enabled) {
args << "--tessellation"; args << "--tessellation";
+7
View File
@@ -61,6 +61,8 @@ static void PrintUsage() {
::printf( ::printf(
" --gpu <index> Vulkan physical device index. Default: auto.\n"); " --gpu <index> Vulkan physical device index. Default: auto.\n");
::printf(" --fullscreen Run in borderless desktop fullscreen.\n"); ::printf(" --fullscreen Run in borderless desktop fullscreen.\n");
::printf(
" --hide-cursor Hide the cursor after 2 s idle. Default: off.\n");
::printf(" --vr Enable the virtual VR headset.\n"); ::printf(" --vr Enable the virtual VR headset.\n");
::printf(" --amd-cpu Apply AMD CPU instruction patches.\n"); ::printf(" --amd-cpu Apply AMD CPU instruction patches.\n");
::printf(" --vblank-frequency <num> Virtual vblank frequency. Default: 60.\n"); ::printf(" --vblank-frequency <num> Virtual vblank frequency. Default: 60.\n");
@@ -205,6 +207,11 @@ static bool ParseArgs(int argc, char* argv[], RunOptions& options, bool& show_he
continue; continue;
} }
if (arg == "--hide-cursor") {
options.config.hide_cursor_enabled = true;
continue;
}
if (arg == "--vr") { if (arg == "--vr") {
options.config.vr_enabled = true; options.config.vr_enabled = true;
continue; continue;