Add warning when low battery level for joysticks

This commit is contained in:
Benau 2021-01-28 12:23:21 +08:00
parent e05c0b095d
commit feb0b0a68c
3 changed files with 35 additions and 0 deletions

View File

@ -207,6 +207,10 @@ void InputManager::update(float dt)
m_timer -= dt; m_timer -= dt;
if(m_timer < 0) m_timer_in_use = false; if(m_timer < 0) m_timer_in_use = false;
} }
#ifndef SERVER_ONLY
for (auto& controller : m_sdl_controller)
controller.second->checkPowerLevel();
#endif
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -18,12 +18,17 @@
#ifndef SERVER_ONLY #ifndef SERVER_ONLY
#include "input/sdl_controller.hpp" #include "input/sdl_controller.hpp"
#include "guiengine/message_queue.hpp"
#include "input/device_config.hpp" #include "input/device_config.hpp"
#include "input/device_manager.hpp" #include "input/device_manager.hpp"
#include "input/gamepad_device.hpp" #include "input/gamepad_device.hpp"
#include "input/input_manager.hpp" #include "input/input_manager.hpp"
#include "utils/log.hpp" #include "utils/log.hpp"
#include "utils/time.hpp"
#include "utils/string_utils.hpp"
#include "utils/translation.hpp"
#include <SDL_version.h>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
@ -31,6 +36,7 @@
SDLController::SDLController(int device_id) SDLController::SDLController(int device_id)
: m_gamepad(NULL) : m_gamepad(NULL)
{ {
m_last_power_level_time = StkTime::getMonoTimeMs();
m_irr_event = {}; m_irr_event = {};
m_irr_event.EventType = irr::EET_JOYSTICK_INPUT_EVENT; m_irr_event.EventType = irr::EET_JOYSTICK_INPUT_EVENT;
memset(m_prev_axes, 0, memset(m_prev_axes, 0,
@ -220,6 +226,27 @@ void SDLController::handleAxisInputSense(const SDL_Event& event)
m_prev_axes[axis_idx]); m_prev_axes[axis_idx]);
} // handleAxisInputSense } // 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 #ifdef ANDROID
void SDLController::handleDirectScanCode(const SDL_Event& event) void SDLController::handleDirectScanCode(const SDL_Event& event)

View File

@ -49,6 +49,8 @@ private:
irr::SEvent m_irr_event; irr::SEvent m_irr_event;
int16_t m_prev_axes[irr::SEvent::SJoystickEvent::NUMBER_OF_AXES]; int16_t m_prev_axes[irr::SEvent::SJoystickEvent::NUMBER_OF_AXES];
uint64_t m_last_power_level_time;
#ifdef ANDROID #ifdef ANDROID
void handleDirectScanCode(const SDL_Event& event); void handleDirectScanCode(const SDL_Event& event);
#endif #endif
@ -148,6 +150,8 @@ public:
} // handleButton } // handleButton
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
SDL_GameController* getGameController() const { return m_game_controller; } SDL_GameController* getGameController() const { return m_game_controller; }
// ------------------------------------------------------------------------
void checkPowerLevel();
}; };
#endif #endif