SDL_power.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2013 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. #ifndef SDL_POWER_DISABLED
  36. #ifdef SDL_POWER_HARDWIRED
  37. /* This is for things that _never_ have a battery */
  38. static SDL_bool
  39. SDL_GetPowerInfo_Hardwired(SDL_PowerState * state, int *seconds, int *percent)
  40. {
  41. *seconds = -1;
  42. *percent = -1;
  43. *state = SDL_POWERSTATE_NO_BATTERY;
  44. return SDL_TRUE;
  45. }
  46. #endif
  47. #endif
  48. static SDL_GetPowerInfo_Impl implementations[] = {
  49. #ifndef SDL_POWER_DISABLED
  50. #ifdef SDL_POWER_LINUX /* in order of preference. More than could work. */
  51. SDL_GetPowerInfo_Linux_proc_acpi,
  52. SDL_GetPowerInfo_Linux_proc_apm,
  53. #endif
  54. #ifdef SDL_POWER_WINDOWS /* handles Win32, Win64, PocketPC. */
  55. SDL_GetPowerInfo_Windows,
  56. #endif
  57. #ifdef SDL_POWER_UIKIT /* handles iPhone/iPad/etc */
  58. SDL_GetPowerInfo_UIKit,
  59. #endif
  60. #ifdef SDL_POWER_MACOSX /* handles Mac OS X, Darwin. */
  61. SDL_GetPowerInfo_MacOSX,
  62. #endif
  63. #ifdef SDL_POWER_HAIKU /* with BeOS euc.jp apm driver. Does this work on Haiku? */
  64. SDL_GetPowerInfo_Haiku,
  65. #endif
  66. #ifdef SDL_POWER_ANDROID /* handles Android. */
  67. SDL_GetPowerInfo_Android,
  68. #endif
  69. #ifdef SDL_POWER_PSP /* handles PSP. */
  70. SDL_GetPowerInfo_PSP,
  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;
  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: */