SDL_syspower.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #ifndef SDL_POWER_DISABLED
  20. #if SDL_POWER_MACOSX
  21. #include <CoreFoundation/CoreFoundation.h>
  22. #include <IOKit/ps/IOPowerSources.h>
  23. #include <IOKit/ps/IOPSKeys.h>
  24. #include "SDL_power.h"
  25. /* CoreFoundation is so verbose... */
  26. #define STRMATCH(a,b) (CFStringCompare(a, b, 0) == kCFCompareEqualTo)
  27. #define GETVAL(k,v) \
  28. CFDictionaryGetValueIfPresent(dict, CFSTR(k), (const void **) v)
  29. /* Note that AC power sources also include a laptop battery it is charging. */
  30. static void
  31. checkps(CFDictionaryRef dict, SDL_bool * have_ac, SDL_bool * have_battery,
  32. SDL_bool * charging, int *seconds, int *percent)
  33. {
  34. CFStringRef strval; /* don't CFRelease() this. */
  35. CFBooleanRef bval;
  36. CFNumberRef numval;
  37. SDL_bool charge = SDL_FALSE;
  38. SDL_bool choose = SDL_FALSE;
  39. SDL_bool is_ac = SDL_FALSE;
  40. int secs = -1;
  41. int maxpct = -1;
  42. int pct = -1;
  43. if ((GETVAL(kIOPSIsPresentKey, &bval)) && (bval == kCFBooleanFalse)) {
  44. return; /* nothing to see here. */
  45. }
  46. if (!GETVAL(kIOPSPowerSourceStateKey, &strval)) {
  47. return;
  48. }
  49. if (STRMATCH(strval, CFSTR(kIOPSACPowerValue))) {
  50. is_ac = *have_ac = SDL_TRUE;
  51. } else if (!STRMATCH(strval, CFSTR(kIOPSBatteryPowerValue))) {
  52. return; /* not a battery? */
  53. }
  54. if ((GETVAL(kIOPSIsChargingKey, &bval)) && (bval == kCFBooleanTrue)) {
  55. charge = SDL_TRUE;
  56. }
  57. if (GETVAL(kIOPSMaxCapacityKey, &numval)) {
  58. SInt32 val = -1;
  59. CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
  60. if (val > 0) {
  61. *have_battery = SDL_TRUE;
  62. maxpct = (int) val;
  63. }
  64. }
  65. if (GETVAL(kIOPSMaxCapacityKey, &numval)) {
  66. SInt32 val = -1;
  67. CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
  68. if (val > 0) {
  69. *have_battery = SDL_TRUE;
  70. maxpct = (int) val;
  71. }
  72. }
  73. if (GETVAL(kIOPSTimeToEmptyKey, &numval)) {
  74. SInt32 val = -1;
  75. CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
  76. /* Mac OS X reports 0 minutes until empty if you're plugged in. :( */
  77. if ((val == 0) && (is_ac)) {
  78. val = -1; /* !!! FIXME: calc from timeToFull and capacity? */
  79. }
  80. secs = (int) val;
  81. if (secs > 0) {
  82. secs *= 60; /* value is in minutes, so convert to seconds. */
  83. }
  84. }
  85. if (GETVAL(kIOPSCurrentCapacityKey, &numval)) {
  86. SInt32 val = -1;
  87. CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
  88. pct = (int) val;
  89. }
  90. if ((pct > 0) && (maxpct > 0)) {
  91. pct = (int) ((((double) pct) / ((double) maxpct)) * 100.0);
  92. }
  93. if (pct > 100) {
  94. pct = 100;
  95. }
  96. /*
  97. * We pick the battery that claims to have the most minutes left.
  98. * (failing a report of minutes, we'll take the highest percent.)
  99. */
  100. if ((secs < 0) && (*seconds < 0)) {
  101. if ((pct < 0) && (*percent < 0)) {
  102. choose = SDL_TRUE; /* at least we know there's a battery. */
  103. }
  104. if (pct > *percent) {
  105. choose = SDL_TRUE;
  106. }
  107. } else if (secs > *seconds) {
  108. choose = SDL_TRUE;
  109. }
  110. if (choose) {
  111. *seconds = secs;
  112. *percent = pct;
  113. *charging = charge;
  114. }
  115. }
  116. #undef GETVAL
  117. #undef STRMATCH
  118. SDL_bool
  119. SDL_GetPowerInfo_MacOSX(SDL_PowerState * state, int *seconds, int *percent)
  120. {
  121. CFTypeRef blob = IOPSCopyPowerSourcesInfo();
  122. *seconds = -1;
  123. *percent = -1;
  124. *state = SDL_POWERSTATE_UNKNOWN;
  125. if (blob != NULL) {
  126. CFArrayRef list = IOPSCopyPowerSourcesList(blob);
  127. if (list != NULL) {
  128. /* don't CFRelease() the list items, or dictionaries! */
  129. SDL_bool have_ac = SDL_FALSE;
  130. SDL_bool have_battery = SDL_FALSE;
  131. SDL_bool charging = SDL_FALSE;
  132. const CFIndex total = CFArrayGetCount(list);
  133. CFIndex i;
  134. for (i = 0; i < total; i++) {
  135. CFTypeRef ps = (CFTypeRef) CFArrayGetValueAtIndex(list, i);
  136. CFDictionaryRef dict =
  137. IOPSGetPowerSourceDescription(blob, ps);
  138. if (dict != NULL) {
  139. checkps(dict, &have_ac, &have_battery, &charging,
  140. seconds, percent);
  141. }
  142. }
  143. if (!have_battery) {
  144. *state = SDL_POWERSTATE_NO_BATTERY;
  145. } else if (charging) {
  146. *state = SDL_POWERSTATE_CHARGING;
  147. } else if (have_ac) {
  148. *state = SDL_POWERSTATE_CHARGED;
  149. } else {
  150. *state = SDL_POWERSTATE_ON_BATTERY;
  151. }
  152. CFRelease(list);
  153. }
  154. CFRelease(blob);
  155. }
  156. return SDL_TRUE; /* always the definitive answer on Mac OS X. */
  157. }
  158. #endif /* SDL_POWER_MACOSX */
  159. #endif /* SDL_POWER_DISABLED */
  160. /* vi: set ts=4 sw=4 expandtab: */