From 9975f1ed23f1859d5904560638a789557e569fe3 Mon Sep 17 00:00:00 2001 From: Arignir Date: Thu, 1 Aug 2024 02:43:03 +0200 Subject: [PATCH] Add a new option to hide the mouse's cursor when the mouse isn't moving for 2 seconds. --- include/app/app.h | 6 ++++++ source/app/config.c | 10 ++++++++-- source/app/main.c | 20 +++++++++++++++++++- source/app/sdl/event.c | 7 +++++-- source/app/windows/settings.c | 11 +++++++++++ 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/include/app/app.h b/include/app/app.h index 0a0529a..a7fb6b6 100644 --- a/include/app/app.h +++ b/include/app/app.h @@ -233,6 +233,9 @@ struct settings { // Pause when the game resets bool pause_when_game_resets; + + // Hide the cursor after a few seconds of inactivity + bool hide_cursor_when_mouse_inactive; } misc; }; @@ -354,6 +357,9 @@ struct app { /* Temporary value used to measure the FPS. */ uint32_t ticks_last_frame; + /* Temporary value used to measure the time since the last mouse movement (in ms) */ + float time_elapsed_since_last_mouse_motion_ms; + /* ** The size of the `game` window. ** Usually the size of the window minus the menubar's height (if it is visible). diff --git a/source/app/config.c b/source/app/config.c index 22aa12d..e5cbc18 100644 --- a/source/app/config.c +++ b/source/app/config.c @@ -156,6 +156,10 @@ app_config_load( if (mjson_get_bool(data, data_len, "$.misc.pause_when_game_resets", &b)) { app->settings.misc.pause_when_game_resets = b; } + + if (mjson_get_bool(data, data_len, "$.misc.hide_cursor_when_mouse_inactive", &b)) { + app->settings.misc.hide_cursor_when_mouse_inactive = b; + } } // Binds @@ -285,7 +289,8 @@ app_config_save( // Misc "misc": { "pause_when_window_inactive": %B, - "pause_when_game_resets": %B + "pause_when_game_resets": %B, + "hide_cursor_when_mouse_inactive": %B } }), app->settings.emulation.bios_path, @@ -310,7 +315,8 @@ app_config_save( (int)app->settings.audio.mute, app->settings.audio.level, (int)app->settings.misc.pause_when_window_inactive, - (int)app->settings.misc.pause_when_game_resets + (int)app->settings.misc.pause_when_game_resets, + (int)app->settings.misc.hide_cursor_when_mouse_inactive ); if (!data) { diff --git a/source/app/main.c b/source/app/main.c index 22f5102..92dd5f7 100644 --- a/source/app/main.c +++ b/source/app/main.c @@ -68,9 +68,10 @@ app_settings_default( settings->video.vsync = false; settings->video.display_size = 3; settings->video.aspect_ratio = ASPECT_RATIO_RESIZE; + settings->video.texture_filter = TEXTURE_FILTER_NEAREST; settings->audio.mute = false; settings->audio.level = 1.0f; - settings->video.texture_filter = TEXTURE_FILTER_NEAREST; + settings->misc.hide_cursor_when_mouse_inactive = true; } int @@ -256,6 +257,23 @@ main( } } + // Hide the cursor if the game is running and the mouse is inactive for a while + if (app.settings.misc.hide_cursor_when_mouse_inactive && app.emulation.is_started && app.emulation.is_running) { + int show_cursor; + + show_cursor = SDL_DISABLE; + if (app.ui.time_elapsed_since_last_mouse_motion_ms <= 2000.0) { // 2s + app.ui.time_elapsed_since_last_mouse_motion_ms += elapsed_ms; + show_cursor = SDL_ENABLE; + } + + if (SDL_ShowCursor(SDL_QUERY) != show_cursor) { + SDL_ShowCursor(show_cursor); + } + } else { + app.ui.time_elapsed_since_last_mouse_motion_ms = 0; + } + // Flush the quick save cache if (app.file.flush_qsaves_cache) { size_t i; diff --git a/source/app/sdl/event.c b/source/app/sdl/event.c index cbca0c6..3802e6f 100644 --- a/source/app/sdl/event.c +++ b/source/app/sdl/event.c @@ -206,7 +206,7 @@ app_sdl_handle_events( bool state_a; bool state_b; - /* Disable the joysticks if the settings are visible */ + /* Disable the joysticks if the settings are open */ if (app->ui.settings.open) { break; } @@ -229,7 +229,10 @@ app_sdl_handle_events( break; } case SDL_MOUSEMOTION: { - + // Handle the "hide cursor on mouse inactivity" option by + // resetting the "time_elapsed_since_last_mouse_motion_ms" counter. + // The main loop will then update the cursor's visibility if needed. + app->ui.time_elapsed_since_last_mouse_motion_ms = 0.f; } } } diff --git a/source/app/windows/settings.c b/source/app/windows/settings.c index 3cd01e6..9c0fcde 100644 --- a/source/app/windows/settings.c +++ b/source/app/windows/settings.c @@ -622,6 +622,17 @@ app_win_settings_misc( igCheckbox("##PauseWhenGameResets", &app->settings.misc.pause_when_game_resets); #endif + // Hide the cursor when the mouse is inactive + igTableNextRow(ImGuiTableRowFlags_None, 0.f); + igTableNextColumn(); + igTextWrapped("Hide cursor when the mouse is inactive"); + + igTableNextColumn(); + if (igCheckbox("##HideCursorWhenMouseInactive", &app->settings.misc.hide_cursor_when_mouse_inactive)) { + app->ui.time_elapsed_since_last_mouse_motion_ms = 0; + SDL_ShowCursor(SDL_ENABLE); + } + igEndTable(); } }