From feb0b0a68cb4d0a51ee845174e8b6a4b56a4c155 Mon Sep 17 00:00:00 2001 From: Benau Date: Thu, 28 Jan 2021 12:23:21 +0800 Subject: [PATCH] Add warning when low battery level for joysticks --- src/input/input_manager.cpp | 4 ++++ src/input/sdl_controller.cpp | 27 +++++++++++++++++++++++++++ src/input/sdl_controller.hpp | 4 ++++ 3 files changed, 35 insertions(+) diff --git a/src/input/input_manager.cpp b/src/input/input_manager.cpp index c15954753..1037c3f78 100644 --- a/src/input/input_manager.cpp +++ b/src/input/input_manager.cpp @@ -207,6 +207,10 @@ void InputManager::update(float dt) m_timer -= dt; if(m_timer < 0) m_timer_in_use = false; } +#ifndef SERVER_ONLY + for (auto& controller : m_sdl_controller) + controller.second->checkPowerLevel(); +#endif } //----------------------------------------------------------------------------- diff --git a/src/input/sdl_controller.cpp b/src/input/sdl_controller.cpp index 28edb3d30..5ecbcf39a 100644 --- a/src/input/sdl_controller.cpp +++ b/src/input/sdl_controller.cpp @@ -18,12 +18,17 @@ #ifndef SERVER_ONLY #include "input/sdl_controller.hpp" +#include "guiengine/message_queue.hpp" #include "input/device_config.hpp" #include "input/device_manager.hpp" #include "input/gamepad_device.hpp" #include "input/input_manager.hpp" #include "utils/log.hpp" +#include "utils/time.hpp" +#include "utils/string_utils.hpp" +#include "utils/translation.hpp" +#include #include #include @@ -31,6 +36,7 @@ SDLController::SDLController(int device_id) : m_gamepad(NULL) { + m_last_power_level_time = StkTime::getMonoTimeMs(); m_irr_event = {}; m_irr_event.EventType = irr::EET_JOYSTICK_INPUT_EVENT; memset(m_prev_axes, 0, @@ -220,6 +226,27 @@ void SDLController::handleAxisInputSense(const SDL_Event& event) m_prev_axes[axis_idx]); } // handleAxisInputSense +// ---------------------------------------------------------------------------- +void SDLController::checkPowerLevel() +{ +#if SDL_VERSION_ATLEAST(2, 0, 4) + const uint64_t time_now = StkTime::getMonoTimeMs(); + if (time_now > m_last_power_level_time + 60000) + { + m_last_power_level_time = time_now; + SDL_JoystickPowerLevel pl = SDL_JoystickCurrentPowerLevel(m_joystick); + if (pl == SDL_JOYSTICK_POWER_EMPTY || pl == SDL_JOYSTICK_POWER_LOW) + { + core::stringw msg = + _("%s has low battery level.", SDL_JoystickName(m_joystick)); + MessageQueue::add(MessageQueue::MT_ERROR, msg); + // Check 5 min later + m_last_power_level_time += 240000; + } + } +#endif +} // checkPowerLevel + // ---------------------------------------------------------------------------- #ifdef ANDROID void SDLController::handleDirectScanCode(const SDL_Event& event) diff --git a/src/input/sdl_controller.hpp b/src/input/sdl_controller.hpp index 0fc5feccf..e883d92d4 100644 --- a/src/input/sdl_controller.hpp +++ b/src/input/sdl_controller.hpp @@ -49,6 +49,8 @@ private: irr::SEvent m_irr_event; int16_t m_prev_axes[irr::SEvent::SJoystickEvent::NUMBER_OF_AXES]; + + uint64_t m_last_power_level_time; #ifdef ANDROID void handleDirectScanCode(const SDL_Event& event); #endif @@ -148,6 +150,8 @@ public: } // handleButton // ------------------------------------------------------------------------ SDL_GameController* getGameController() const { return m_game_controller; } + // ------------------------------------------------------------------------ + void checkPowerLevel(); }; #endif