SDL_power.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "SDL_syspower.h"
  20. /*
  21. * Returns true if we have a definitive answer.
  22. * false to try next implementation.
  23. */
  24. typedef bool (*SDL_GetPowerInfo_Impl)(SDL_PowerState *state, int *seconds,
  25. int *percent);
  26. #ifndef SDL_POWER_DISABLED
  27. #ifdef SDL_POWER_HARDWIRED
  28. // This is for things that _never_ have a battery
  29. static bool SDL_GetPowerInfo_Hardwired(SDL_PowerState *state, int *seconds, int *percent)
  30. {
  31. *seconds = -1;
  32. *percent = -1;
  33. *state = SDL_POWERSTATE_NO_BATTERY;
  34. return true;
  35. }
  36. #endif
  37. static SDL_GetPowerInfo_Impl implementations[] = {
  38. #ifdef SDL_POWER_LINUX // in order of preference. More than could work.
  39. SDL_GetPowerInfo_Linux_org_freedesktop_upower,
  40. SDL_GetPowerInfo_Linux_sys_class_power_supply,
  41. SDL_GetPowerInfo_Linux_proc_acpi,
  42. SDL_GetPowerInfo_Linux_proc_apm,
  43. #endif
  44. #ifdef SDL_POWER_WINDOWS // handles Win32, Win64, PocketPC.
  45. SDL_GetPowerInfo_Windows,
  46. #endif
  47. #ifdef SDL_POWER_UIKIT // handles iPhone/iPad/etc
  48. SDL_GetPowerInfo_UIKit,
  49. #endif
  50. #ifdef SDL_POWER_MACOSX // handles macOS, Darwin.
  51. SDL_GetPowerInfo_MacOSX,
  52. #endif
  53. #ifdef SDL_POWER_HAIKU // with BeOS euc.jp apm driver. Does this work on Haiku?
  54. SDL_GetPowerInfo_Haiku,
  55. #endif
  56. #ifdef SDL_POWER_ANDROID // handles Android.
  57. SDL_GetPowerInfo_Android,
  58. #endif
  59. #ifdef SDL_POWER_PSP // handles PSP.
  60. SDL_GetPowerInfo_PSP,
  61. #endif
  62. #ifdef SDL_POWER_VITA // handles PSVita.
  63. SDL_GetPowerInfo_VITA,
  64. #endif
  65. #ifdef SDL_POWER_N3DS // handles N3DS.
  66. SDL_GetPowerInfo_N3DS,
  67. #endif
  68. #ifdef SDL_POWER_EMSCRIPTEN // handles Emscripten
  69. SDL_GetPowerInfo_Emscripten,
  70. #endif
  71. #ifdef SDL_POWER_HARDWIRED
  72. SDL_GetPowerInfo_Hardwired,
  73. #endif
  74. };
  75. #endif
  76. SDL_PowerState SDL_GetPowerInfo(int *seconds, int *percent)
  77. {
  78. #ifndef SDL_POWER_DISABLED
  79. const int total = SDL_arraysize(implementations);
  80. SDL_PowerState result = SDL_POWERSTATE_UNKNOWN;
  81. int i;
  82. #endif
  83. int _seconds, _percent;
  84. // Make these never NULL for platform-specific implementations.
  85. if (!seconds) {
  86. seconds = &_seconds;
  87. }
  88. if (!percent) {
  89. percent = &_percent;
  90. }
  91. #ifndef SDL_POWER_DISABLED
  92. for (i = 0; i < total; i++) {
  93. if (implementations[i](&result, seconds, percent)) {
  94. return result;
  95. }
  96. }
  97. #endif
  98. // nothing was definitive.
  99. *seconds = -1;
  100. *percent = -1;
  101. return SDL_POWERSTATE_UNKNOWN;
  102. }