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 | #include "SDL_internal.h" |
22 | #include "SDL_syspower.h" |
23 | |
24 | /* |
25 | * Returns true if we have a definitive answer. |
26 | * false to try next implementation. |
27 | */ |
28 | typedef bool (*SDL_GetPowerInfo_Impl)(SDL_PowerState *state, int *seconds, |
29 | int *percent); |
30 | |
31 | #ifndef SDL_POWER_DISABLED |
32 | #ifdef SDL_POWER_HARDWIRED |
33 | // This is for things that _never_ have a battery |
34 | static bool SDL_GetPowerInfo_Hardwired(SDL_PowerState *state, int *seconds, int *percent) |
35 | { |
36 | *seconds = -1; |
37 | *percent = -1; |
38 | *state = SDL_POWERSTATE_NO_BATTERY; |
39 | return true; |
40 | } |
41 | #endif |
42 | |
43 | static SDL_GetPowerInfo_Impl implementations[] = { |
44 | #ifdef SDL_POWER_LINUX // in order of preference. More than could work. |
45 | SDL_GetPowerInfo_Linux_org_freedesktop_upower, |
46 | SDL_GetPowerInfo_Linux_sys_class_power_supply, |
47 | SDL_GetPowerInfo_Linux_proc_acpi, |
48 | SDL_GetPowerInfo_Linux_proc_apm, |
49 | #endif |
50 | #ifdef SDL_POWER_WINDOWS // handles Win32, Win64, PocketPC. |
51 | SDL_GetPowerInfo_Windows, |
52 | #endif |
53 | #ifdef SDL_POWER_UIKIT // handles iPhone/iPad/etc |
54 | SDL_GetPowerInfo_UIKit, |
55 | #endif |
56 | #ifdef SDL_POWER_MACOSX // handles macOS, Darwin. |
57 | SDL_GetPowerInfo_MacOSX, |
58 | #endif |
59 | #ifdef SDL_POWER_HAIKU // with BeOS euc.jp apm driver. Does this work on Haiku? |
60 | SDL_GetPowerInfo_Haiku, |
61 | #endif |
62 | #ifdef SDL_POWER_ANDROID // handles Android. |
63 | SDL_GetPowerInfo_Android, |
64 | #endif |
65 | #ifdef SDL_POWER_PSP // handles PSP. |
66 | SDL_GetPowerInfo_PSP, |
67 | #endif |
68 | #ifdef SDL_POWER_VITA // handles PSVita. |
69 | SDL_GetPowerInfo_VITA, |
70 | #endif |
71 | #ifdef SDL_POWER_N3DS // handles N3DS. |
72 | SDL_GetPowerInfo_N3DS, |
73 | #endif |
74 | #ifdef SDL_POWER_EMSCRIPTEN // handles Emscripten |
75 | SDL_GetPowerInfo_Emscripten, |
76 | #endif |
77 | |
78 | #ifdef SDL_POWER_HARDWIRED |
79 | SDL_GetPowerInfo_Hardwired, |
80 | #endif |
81 | }; |
82 | #endif |
83 | |
84 | SDL_PowerState SDL_GetPowerInfo(int *seconds, int *percent) |
85 | { |
86 | #ifndef SDL_POWER_DISABLED |
87 | const int total = sizeof(implementations) / sizeof(implementations[0]); |
88 | SDL_PowerState result = SDL_POWERSTATE_UNKNOWN; |
89 | int i; |
90 | #endif |
91 | |
92 | int _seconds, _percent; |
93 | // Make these never NULL for platform-specific implementations. |
94 | if (!seconds) { |
95 | seconds = &_seconds; |
96 | } |
97 | if (!percent) { |
98 | percent = &_percent; |
99 | } |
100 | |
101 | #ifndef SDL_POWER_DISABLED |
102 | for (i = 0; i < total; i++) { |
103 | if (implementations[i](&result, seconds, percent)) { |
104 | return result; |
105 | } |
106 | } |
107 | #endif |
108 | |
109 | // nothing was definitive. |
110 | *seconds = -1; |
111 | *percent = -1; |
112 | return SDL_POWERSTATE_UNKNOWN; |
113 | } |
114 | |