power.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /* We will use this renderer to draw into this window every frame. */
  10. static SDL_Window *window = NULL;
  11. static SDL_Renderer *renderer = NULL;
  12. /* This function runs once at startup. */
  13. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  14. {
  15. SDL_SetAppMetadata("Example Misc Power", "1.0", "com.example.misc-power");
  16. if (!SDL_Init(SDL_INIT_VIDEO)) {
  17. SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
  18. return SDL_APP_FAILURE;
  19. }
  20. if (!SDL_CreateWindowAndRenderer("examples/misc/power", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
  21. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  22. return SDL_APP_FAILURE;
  23. }
  24. SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
  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. /* Query for battery info */
  40. int seconds = 0, percent = 0;
  41. const SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
  42. /* We set up different drawing details for each power state, then
  43. run it all through the same drawing code. */
  44. int clearr = 0, clearg = 0, clearb = 0; /* clear window to this color. */
  45. int textr = 255, textg = 255, textb = 255; /* draw messages in this color. */
  46. int framer = 255, frameg = 255, frameb = 255; /* draw a percentage bar frame in this color. */
  47. int barr = 0, barg = 0, barb = 0; /* draw a percentage bar in this color. */
  48. const char *msg = NULL;
  49. const char *msg2 = NULL;
  50. switch (state) {
  51. case SDL_POWERSTATE_ERROR:
  52. msg2 = "ERROR GETTING POWER STATE";
  53. msg = SDL_GetError();
  54. clearr = 255; /* red background */
  55. break;
  56. default: /* in case this does something unexpected later, treat it as unknown. */
  57. case SDL_POWERSTATE_UNKNOWN:
  58. msg = "Power state is unknown.";
  59. clearr = clearb = clearg = 50; /* grey background */
  60. break;
  61. case SDL_POWERSTATE_ON_BATTERY:
  62. msg = "Running on battery.";
  63. barr = 255; /* draw in red */
  64. break;
  65. case SDL_POWERSTATE_NO_BATTERY:
  66. msg = "Plugged in, no battery available.";
  67. clearg = 50; /* green background */
  68. break;
  69. case SDL_POWERSTATE_CHARGING:
  70. msg = "Charging.";
  71. barb = barg = 255; /* draw in cyan */
  72. break;
  73. case SDL_POWERSTATE_CHARGED:
  74. msg = "Charged.";
  75. barg = 255; /* draw in green */
  76. break;
  77. }
  78. SDL_SetRenderDrawColor(renderer, clearr, clearg, clearb, 255);
  79. SDL_RenderClear(renderer);
  80. if (percent >= 0) {
  81. float x, y;
  82. SDL_FRect pctrect;
  83. char remainstr[64];
  84. char msgbuf[128];
  85. SDL_copyp(&pctrect, &frame);
  86. pctrect.w *= percent / 100.0f;
  87. if (seconds < 0) {
  88. SDL_strlcpy(remainstr, "unknown time", sizeof (remainstr));
  89. } else {
  90. int hours, minutes;
  91. hours = seconds / (60 * 60);
  92. seconds -= hours * (60 * 60);
  93. minutes = seconds / 60;
  94. seconds -= minutes * 60;
  95. SDL_snprintf(remainstr, sizeof (remainstr), "%02d:%02d:%02d", hours, minutes, seconds);
  96. }
  97. SDL_snprintf(msgbuf, sizeof (msgbuf), "Battery: %3d percent, %s remaining", percent, remainstr);
  98. x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msgbuf))) / 2.0f);
  99. y = frame.y + frame.h + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
  100. SDL_SetRenderDrawColor(renderer, barr, barg, barb, 255); /* draw percent bar. */
  101. SDL_RenderFillRect(renderer, &pctrect);
  102. SDL_SetRenderDrawColor(renderer, framer, frameg, frameb, 255); /* draw frame on top of bar. */
  103. SDL_RenderRect(renderer, &frame);
  104. SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
  105. SDL_RenderDebugText(renderer, x, y, msgbuf); /* draw text about battery level */
  106. }
  107. if (msg) {
  108. const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
  109. const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2);
  110. SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
  111. SDL_RenderDebugText(renderer, x, y, msg);
  112. }
  113. if (msg2) {
  114. const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg2))) / 2.0f);
  115. const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 4);
  116. SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
  117. SDL_RenderDebugText(renderer, x, y, msg2);
  118. }
  119. /* put the new rendering on the screen. */
  120. SDL_RenderPresent(renderer);
  121. return SDL_APP_CONTINUE; /* carry on with the program! */
  122. }
  123. /* This function runs once at shutdown. */
  124. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  125. {
  126. /* SDL will clean up the window/renderer for us. */
  127. }