SDL_power.c 3.6 KB

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