diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/power/n3ds | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/power/n3ds')
| -rw-r--r-- | contrib/SDL-3.2.8/src/power/n3ds/SDL_syspower.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/power/n3ds/SDL_syspower.c b/contrib/SDL-3.2.8/src/power/n3ds/SDL_syspower.c new file mode 100644 index 0000000..822398a --- /dev/null +++ b/contrib/SDL-3.2.8/src/power/n3ds/SDL_syspower.c | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "SDL_internal.h" | ||
| 23 | |||
| 24 | #if !defined(SDL_POWER_DISABLED) && defined(SDL_POWER_N3DS) | ||
| 25 | |||
| 26 | #include <3ds.h> | ||
| 27 | |||
| 28 | static SDL_PowerState GetPowerState(void); | ||
| 29 | static bool ReadStateFromPTMU(bool *is_plugged, u8 *is_charging); | ||
| 30 | static int GetBatteryPercentage(void); | ||
| 31 | |||
| 32 | #define BATTERY_PERCENT_REG 0xB | ||
| 33 | #define BATTERY_PERCENT_REG_SIZE 2 | ||
| 34 | |||
| 35 | bool SDL_GetPowerInfo_N3DS(SDL_PowerState *state, int *seconds, int *percent) | ||
| 36 | { | ||
| 37 | *state = GetPowerState(); | ||
| 38 | *percent = GetBatteryPercentage(); | ||
| 39 | *seconds = -1; // libctru doesn't provide a way to estimate battery life | ||
| 40 | |||
| 41 | return true; | ||
| 42 | } | ||
| 43 | |||
| 44 | static SDL_PowerState GetPowerState(void) | ||
| 45 | { | ||
| 46 | bool is_plugged; | ||
| 47 | u8 is_charging; | ||
| 48 | |||
| 49 | if (!ReadStateFromPTMU(&is_plugged, &is_charging)) { | ||
| 50 | return SDL_POWERSTATE_UNKNOWN; | ||
| 51 | } | ||
| 52 | |||
| 53 | if (is_charging) { | ||
| 54 | return SDL_POWERSTATE_CHARGING; | ||
| 55 | } | ||
| 56 | |||
| 57 | if (is_plugged) { | ||
| 58 | return SDL_POWERSTATE_CHARGED; | ||
| 59 | } | ||
| 60 | |||
| 61 | return SDL_POWERSTATE_ON_BATTERY; | ||
| 62 | } | ||
| 63 | |||
| 64 | static bool ReadStateFromPTMU(bool *is_plugged, u8 *is_charging) | ||
| 65 | { | ||
| 66 | if (R_FAILED(ptmuInit())) { | ||
| 67 | return SDL_SetError("Failed to initialise PTMU service"); | ||
| 68 | } | ||
| 69 | |||
| 70 | if (R_FAILED(PTMU_GetAdapterState(is_plugged))) { | ||
| 71 | ptmuExit(); | ||
| 72 | return SDL_SetError("Failed to read adapter state"); | ||
| 73 | } | ||
| 74 | |||
| 75 | if (R_FAILED(PTMU_GetBatteryChargeState(is_charging))) { | ||
| 76 | ptmuExit(); | ||
| 77 | return SDL_SetError("Failed to read battery charge state"); | ||
| 78 | } | ||
| 79 | |||
| 80 | ptmuExit(); | ||
| 81 | return true; | ||
| 82 | } | ||
| 83 | |||
| 84 | static int GetBatteryPercentage(void) | ||
| 85 | { | ||
| 86 | u8 data[BATTERY_PERCENT_REG_SIZE]; | ||
| 87 | |||
| 88 | if (R_FAILED(mcuHwcInit())) { | ||
| 89 | SDL_SetError("Failed to initialise mcuHwc service"); | ||
| 90 | return -1; | ||
| 91 | } | ||
| 92 | |||
| 93 | if (R_FAILED(MCUHWC_ReadRegister(BATTERY_PERCENT_REG, data, BATTERY_PERCENT_REG_SIZE))) { | ||
| 94 | mcuHwcExit(); | ||
| 95 | SDL_SetError("Failed to read battery register"); | ||
| 96 | return -1; | ||
| 97 | } | ||
| 98 | |||
| 99 | mcuHwcExit(); | ||
| 100 | |||
| 101 | return (int)SDL_round(data[0] + data[1] / 256.0); | ||
| 102 | } | ||
| 103 | |||
| 104 | #endif // !SDL_POWER_DISABLED && SDL_POWER_N3DS | ||
