SDL_power.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 Sam Lantinga <[email protected]>
  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. #endif
  41. static SDL_GetPowerInfo_Impl implementations[] = {
  42. #ifndef SDL_POWER_DISABLED
  43. #ifdef SDL_POWER_LINUX /* in order of preference. More than could work. */
  44. SDL_GetPowerInfo_Linux_sys_class_power_supply,
  45. SDL_GetPowerInfo_Linux_proc_acpi,
  46. SDL_GetPowerInfo_Linux_proc_apm,
  47. #endif
  48. #ifdef SDL_POWER_WINDOWS /* handles Win32, Win64, PocketPC. */
  49. SDL_GetPowerInfo_Windows,
  50. #endif
  51. #ifdef SDL_POWER_UIKIT /* handles iPhone/iPad/etc */
  52. SDL_GetPowerInfo_UIKit,
  53. #endif
  54. #ifdef SDL_POWER_MACOSX /* handles Mac OS X, Darwin. */
  55. SDL_GetPowerInfo_MacOSX,
  56. #endif
  57. #ifdef SDL_POWER_HAIKU /* with BeOS euc.jp apm driver. Does this work on Haiku? */
  58. SDL_GetPowerInfo_Haiku,
  59. #endif
  60. #ifdef SDL_POWER_ANDROID /* handles Android. */
  61. SDL_GetPowerInfo_Android,
  62. #endif
  63. #ifdef SDL_POWER_PSP /* handles PSP. */
  64. SDL_GetPowerInfo_PSP,
  65. #endif
  66. #ifdef SDL_POWER_WINRT /* handles WinRT */
  67. SDL_GetPowerInfo_WinRT,
  68. #endif
  69. #ifdef SDL_POWER_EMSCRIPTEN /* handles Emscripten */
  70. SDL_GetPowerInfo_Emscripten,
  71. #endif
  72. #ifdef SDL_POWER_HARDWIRED
  73. SDL_GetPowerInfo_Hardwired,
  74. #endif
  75. #endif
  76. };
  77. SDL_PowerState
  78. SDL_GetPowerInfo(int *seconds, int *percent)
  79. {
  80. const int total = sizeof(implementations) / sizeof(implementations[0]);
  81. int _seconds, _percent;
  82. SDL_PowerState retval = SDL_POWERSTATE_UNKNOWN;
  83. int i;
  84. /* Make these never NULL for platform-specific implementations. */
  85. if (seconds == NULL) {
  86. seconds = &_seconds;
  87. }
  88. if (percent == NULL) {
  89. percent = &_percent;
  90. }
  91. for (i = 0; i < total; i++) {
  92. if (implementations[i](&retval, seconds, percent)) {
  93. return retval;
  94. }
  95. }
  96. /* nothing was definitive. */
  97. *seconds = -1;
  98. *percent = -1;
  99. return SDL_POWERSTATE_UNKNOWN;
  100. }
  101. /* vi: set ts=4 sw=4 expandtab: */