From 91848f0be635ef495b18600abd3a95afe3b7beeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Thu, 9 May 2024 14:39:31 +0200 Subject: [PATCH] Quit if Android back-button is pressed 3 times within 1 second Interpret fast repeated presses of the back-button (3 times within 1 second) as a quit-event, so the app can be quit cleanly and quickly without using the UI. The client settings are otherwise not saved if the app is closed by minimizing it using the home button and waiting for the OS to kill it or by discarding it in the recent apps view. --- src/engine/client/input.cpp | 26 ++++++++++++++++++++++++++ src/engine/client/input.h | 7 +++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/engine/client/input.cpp b/src/engine/client/input.cpp index 5632e1ef1..96f5f42cc 100644 --- a/src/engine/client/input.cpp +++ b/src/engine/client/input.cpp @@ -641,9 +641,35 @@ int CInput::Update() // handle keys case SDL_KEYDOWN: +#if defined(CONF_PLATFORM_ANDROID) + if(Event.key.keysym.scancode == KEY_AC_BACK && m_BackButtonReleased) + { + if(m_LastBackPress == -1 || (Now - m_LastBackPress) / (float)time_freq() > 1.0f) + { + m_NumBackPresses = 1; + m_LastBackPress = Now; + } + else + { + m_NumBackPresses++; + if(m_NumBackPresses >= 3) + { + // Quit if the Android back-button was pressed 3 times within 1 second + return 1; + } + } + m_BackButtonReleased = false; + } +#endif Scancode = TranslateScancode(Event.key); break; case SDL_KEYUP: +#if defined(CONF_PLATFORM_ANDROID) + if(Event.key.keysym.scancode == KEY_AC_BACK && !m_BackButtonReleased) + { + m_BackButtonReleased = true; + } +#endif Action = IInput::FLAG_RELEASE; Scancode = TranslateScancode(Event.key); break; diff --git a/src/engine/client/input.h b/src/engine/client/input.h index 73ec93ef7..00fd15c8e 100644 --- a/src/engine/client/input.h +++ b/src/engine/client/input.h @@ -78,8 +78,11 @@ private: bool m_MouseFocus; bool m_MouseDoubleClick; -#if defined(CONF_PLATFORM_ANDROID) // No relative mouse on Android - ivec2 m_LastMousePos = ivec2(0, 0); +#if defined(CONF_PLATFORM_ANDROID) + ivec2 m_LastMousePos = ivec2(0, 0); // No relative mouse on Android + int m_NumBackPresses = 0; + bool m_BackButtonReleased = true; + int64_t m_LastBackPress = -1; #endif // IME support