From 4eb5d77750aa316e90552de390defa4db3290317 Mon Sep 17 00:00:00 2001 From: cpasjuste Date: Tue, 22 Jun 2021 14:28:56 +0200 Subject: [PATCH] switch: add power support --- CMakeLists.txt | 2 - src/power/SDL_power.c | 3 ++ src/power/SDL_syspower.h | 1 + src/power/switch/SDL_syspower.c | 66 +++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/power/switch/SDL_syspower.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 209cd13aa..6f2f02df7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2008,14 +2008,12 @@ elseif(NINTENDO_SWITCH) set(SOURCE_FILES ${SOURCE_FILES} ${SWITCH_JOYSTICK_SOURCES}) set(HAVE_SDL_JOYSTICK TRUE) endif() - #[[ if(SDL_POWER) set(SDL_POWER_SWITCH 1) file(GLOB SWITCH_POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/switch/*.c) set(SOURCE_FILES ${SOURCE_FILES} ${SWITCH_POWER_SOURCES}) set(HAVE_SDL_POWER TRUE) endif() - ]] if(SDL_TIMERS) set(SDL_TIMER_SWITCH 1) file(GLOB SWITCH_TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/switch/*.c) diff --git a/src/power/SDL_power.c b/src/power/SDL_power.c index ca19d4492..216da4fd5 100644 --- a/src/power/SDL_power.c +++ b/src/power/SDL_power.c @@ -74,6 +74,9 @@ static SDL_GetPowerInfo_Impl implementations[] = { #ifdef SDL_POWER_EMSCRIPTEN /* handles Emscripten */ SDL_GetPowerInfo_Emscripten, #endif +#ifdef SDL_POWER_SWITCH /* handles Nintendo Switch. */ + SDL_GetPowerInfo_SWITCH, +#endif #ifdef SDL_POWER_HARDWIRED SDL_GetPowerInfo_Hardwired, diff --git a/src/power/SDL_syspower.h b/src/power/SDL_syspower.h index f28cc982b..905b4fb4c 100644 --- a/src/power/SDL_syspower.h +++ b/src/power/SDL_syspower.h @@ -40,6 +40,7 @@ SDL_bool SDL_GetPowerInfo_Android(SDL_PowerState *, int *, int *); SDL_bool SDL_GetPowerInfo_PSP(SDL_PowerState *, int *, int *); SDL_bool SDL_GetPowerInfo_WinRT(SDL_PowerState *, int *, int *); SDL_bool SDL_GetPowerInfo_Emscripten(SDL_PowerState *, int *, int *); +SDL_bool SDL_GetPowerInfo_SWITCH(SDL_PowerState *, int *, int *); /* this one is static in SDL_power.c */ /* SDL_bool SDL_GetPowerInfo_Hardwired(SDL_PowerState *, int *, int *);*/ diff --git a/src/power/switch/SDL_syspower.c b/src/power/switch/SDL_syspower.c new file mode 100644 index 000000000..3f27fa7cb --- /dev/null +++ b/src/power/switch/SDL_syspower.c @@ -0,0 +1,66 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2015 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#ifndef SDL_POWER_DISABLED +#if SDL_POWER_SWITCH + +#include +#include "SDL_power.h" + +SDL_bool +SDL_GetPowerInfo_SWITCH(SDL_PowerState * state, int *seconds, + int *percent) +{ + PsmChargerType chargerType; + u32 charge; + double age; + Result rc; + + rc = psmGetChargerType(&chargerType); + if (R_FAILED(rc)) { + *state = SDL_POWERSTATE_UNKNOWN; + *seconds = -1; + *percent = -1; + return SDL_FALSE; + } + + psmGetBatteryChargePercentage(&charge); + *percent = (int) charge; + + psmGetBatteryAgePercentage(&age); + *seconds = (int) age; + + if(chargerType == PsmChargerType_EnoughPower) { + *state = SDL_POWERSTATE_CHARGED; + } else { + // TODO: check if device charging... + *state = SDL_POWERSTATE_CHARGING; + } + + return SDL_TRUE; +} + +#endif /* SDL_POWER_SWITCH */ +#endif /* SDL_POWER_DISABLED */ + +/* vi: set ts=4 sw=4 expandtab: */