SDL_power.c 3.8 KB

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