power.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * This example code reports power status (plugged in, battery level, etc).
  3. *
  4. * This code is public domain. Feel free to use it for any purpose!
  5. */
  6. #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
  7. #include <SDL3/SDL.h>
  8. #include <SDL3/SDL_main.h>
  9. #include "../../save-rendering-to-bitmaps.h"
  10. /* We will use this renderer to draw into this window every frame. */
  11. static SDL_Window *window = NULL;
  12. static SDL_Renderer *renderer = NULL;
  13. /* This function runs once at startup. */
  14. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  15. {
  16. SDL_SetAppMetadata("Example Misc Power", "1.0", "com.example.misc-power");
  17. if (!SDL_Init(SDL_INIT_VIDEO)) {
  18. SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
  19. return SDL_APP_FAILURE;
  20. }
  21. if (!SDL_CreateWindowAndRenderer("examples/misc/power", 640, 480, 0, &window, &renderer)) {
  22. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  23. return SDL_APP_FAILURE;
  24. }
  25. return SDL_APP_CONTINUE; /* carry on with the program! */
  26. }
  27. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  28. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  29. {
  30. if (event->type == SDL_EVENT_QUIT) {
  31. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  32. }
  33. return SDL_APP_CONTINUE; /* carry on with the program! */
  34. }
  35. /* This function runs once per frame, and is the heart of the program. */
  36. SDL_AppResult SDL_AppIterate(void *appstate)
  37. {
  38. const SDL_FRect frame = { 100, 200, 440, 80 }; /* the percentage bar dimensions. */
  39. static Uint64 start = 0;
  40. Uint64 now = SDL_GetTicks();
  41. if (!start) {
  42. start = now;
  43. }
  44. now -= start;
  45. /* Query for battery info */
  46. int seconds = 0, percent = 0;
  47. //const SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
  48. SDL_PowerState state;
  49. if (now < 500) {
  50. state = SDL_POWERSTATE_ON_BATTERY;
  51. seconds = 8088 + ((500 / 1000) * 300);
  52. percent = ((int) ((500 / 1500.0f) * 100.0f));
  53. } else {
  54. state = (now < 1500) ? SDL_POWERSTATE_CHARGING : SDL_POWERSTATE_CHARGED;
  55. seconds = 8088 + ((int) ((SDL_min(now, 1500) / 1500.0) * 10000));
  56. percent = (now < 1500) ? ((int) ((now / 1500.0f) * 100.0f)) : 100;
  57. }
  58. /* We set up different drawing details for each power state, then
  59. run it all through the same drawing code. */
  60. int clearr = 0, clearg = 0, clearb = 0; /* clear window to this color. */
  61. int textr = 255, textg = 255, textb = 255; /* draw messages in this color. */
  62. int framer = 255, frameg = 255, frameb = 255; /* draw a percentage bar frame in this color. */
  63. int barr = 0, barg = 0, barb = 0; /* draw a percentage bar in this color. */
  64. const char *msg = NULL;
  65. const char *msg2 = NULL;
  66. switch (state) {
  67. case SDL_POWERSTATE_ERROR:
  68. msg2 = "ERROR GETTING POWER STATE";
  69. msg = SDL_GetError();
  70. clearr = 255; /* red background */
  71. break;
  72. default: /* in case this does something unexpected later, treat it as unknown. */
  73. case SDL_POWERSTATE_UNKNOWN:
  74. msg = "Power state is unknown.";
  75. clearr = clearb = clearg = 50; /* grey background */
  76. break;
  77. case SDL_POWERSTATE_ON_BATTERY:
  78. msg = "Running on battery.";
  79. barr = 255; /* draw in red */
  80. break;
  81. case SDL_POWERSTATE_NO_BATTERY:
  82. msg = "Plugged in, no battery available.";
  83. clearg = 50; /* green background */
  84. break;
  85. case SDL_POWERSTATE_CHARGING:
  86. msg = "Charging.";
  87. barb = barg = 255; /* draw in cyan */
  88. break;
  89. case SDL_POWERSTATE_CHARGED:
  90. msg = "Charged.";
  91. barg = 255; /* draw in green */
  92. break;
  93. }
  94. SDL_SetRenderDrawColor(renderer, clearr, clearg, clearb, 255);
  95. SDL_RenderClear(renderer);
  96. if (percent >= 0) {
  97. float x, y;
  98. SDL_FRect pctrect;
  99. char remainstr[64];
  100. char msgbuf[128];
  101. SDL_copyp(&pctrect, &frame);
  102. pctrect.w *= percent / 100.0f;
  103. if (seconds < 0) {
  104. SDL_strlcpy(remainstr, "unknown time", sizeof (remainstr));
  105. } else {
  106. int hours, minutes;
  107. hours = seconds / (60 * 60);
  108. seconds -= hours * (60 * 60);
  109. minutes = seconds / 60;
  110. seconds -= minutes * 60;
  111. SDL_snprintf(remainstr, sizeof (remainstr), "%02d:%02d:%02d", hours, minutes, seconds);
  112. }
  113. SDL_snprintf(msgbuf, sizeof (msgbuf), "Battery: %3d percent, %s remaining", percent, remainstr);
  114. x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msgbuf))) / 2.0f);
  115. y = frame.y + frame.h + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
  116. SDL_SetRenderDrawColor(renderer, barr, barg, barb, 255); /* draw percent bar. */
  117. SDL_RenderFillRect(renderer, &pctrect);
  118. SDL_SetRenderDrawColor(renderer, framer, frameg, frameb, 255); /* draw frame on top of bar. */
  119. SDL_RenderRect(renderer, &frame);
  120. SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
  121. SDL_RenderDebugText(renderer, x, y, msgbuf); /* draw text about battery level */
  122. }
  123. if (msg) {
  124. const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
  125. const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2);
  126. SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
  127. SDL_RenderDebugText(renderer, x, y, msg);
  128. }
  129. if (msg2) {
  130. const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg2))) / 2.0f);
  131. const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 4);
  132. SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
  133. SDL_RenderDebugText(renderer, x, y, msg2);
  134. }
  135. /* put the new rendering on the screen. */
  136. SDL_RenderPresent(renderer);
  137. return SDL_APP_CONTINUE; /* carry on with the program! */
  138. }
  139. /* This function runs once at shutdown. */
  140. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  141. {
  142. /* SDL will clean up the window/renderer for us. */
  143. }