power.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, 0, &window, &renderer)) {
  21. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  22. return SDL_APP_FAILURE;
  23. }
  24. return SDL_APP_CONTINUE; /* carry on with the program! */
  25. }
  26. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  27. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  28. {
  29. if (event->type == SDL_EVENT_QUIT) {
  30. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  31. }
  32. return SDL_APP_CONTINUE; /* carry on with the program! */
  33. }
  34. /* This function runs once per frame, and is the heart of the program. */
  35. SDL_AppResult SDL_AppIterate(void *appstate)
  36. {
  37. const SDL_FRect frame = { 100, 200, 440, 80 }; /* the percentage bar dimensions. */
  38. /* Query for battery info */
  39. int seconds = 0, percent = 0;
  40. const SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
  41. /* We set up different drawing details for each power state, then
  42. run it all through the same drawing code. */
  43. int clearr = 0, clearg = 0, clearb = 0; /* clear window to this color. */
  44. int textr = 255, textg = 255, textb = 255; /* draw messages in this color. */
  45. int framer = 255, frameg = 255, frameb = 255; /* draw a percentage bar frame in this color. */
  46. int barr = 0, barg = 0, barb = 0; /* draw a percentage bar in this color. */
  47. const char *msg = NULL;
  48. const char *msg2 = NULL;
  49. switch (state) {
  50. case SDL_POWERSTATE_ERROR:
  51. msg2 = "ERROR GETTING POWER STATE";
  52. msg = SDL_GetError();
  53. clearr = 255; /* red background */
  54. break;
  55. default: /* in case this does something unexpected later, treat it as unknown. */
  56. case SDL_POWERSTATE_UNKNOWN:
  57. msg = "Power state is unknown.";
  58. clearr = clearb = clearg = 50; /* grey background */
  59. break;
  60. case SDL_POWERSTATE_ON_BATTERY:
  61. msg = "Running on battery.";
  62. barr = 255; /* draw in red */
  63. break;
  64. case SDL_POWERSTATE_NO_BATTERY:
  65. msg = "Plugged in, no battery available.";
  66. clearg = 50; /* green background */
  67. break;
  68. case SDL_POWERSTATE_CHARGING:
  69. msg = "Charging.";
  70. barb = barg = 255; /* draw in cyan */
  71. break;
  72. case SDL_POWERSTATE_CHARGED:
  73. msg = "Charged.";
  74. barg = 255; /* draw in green */
  75. break;
  76. }
  77. SDL_SetRenderDrawColor(renderer, clearr, clearg, clearb, 255);
  78. SDL_RenderClear(renderer);
  79. if (percent >= 0) {
  80. float x, y;
  81. SDL_FRect pctrect;
  82. char remainstr[64];
  83. char msgbuf[128];
  84. SDL_copyp(&pctrect, &frame);
  85. pctrect.w *= percent / 100.0f;
  86. if (seconds < 0) {
  87. SDL_strlcpy(remainstr, "unknown time", sizeof (remainstr));
  88. } else {
  89. int hours, minutes;
  90. hours = seconds / (60 * 60);
  91. seconds -= hours * (60 * 60);
  92. minutes = seconds / 60;
  93. seconds -= minutes * 60;
  94. SDL_snprintf(remainstr, sizeof (remainstr), "%02d:%02d:%02d", hours, minutes, seconds);
  95. }
  96. SDL_snprintf(msgbuf, sizeof (msgbuf), "Battery: %3d percent, %s remaining", percent, remainstr);
  97. x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msgbuf))) / 2.0f);
  98. y = frame.y + frame.h + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
  99. SDL_SetRenderDrawColor(renderer, barr, barg, barb, 255); /* draw percent bar. */
  100. SDL_RenderFillRect(renderer, &pctrect);
  101. SDL_SetRenderDrawColor(renderer, framer, frameg, frameb, 255); /* draw frame on top of bar. */
  102. SDL_RenderRect(renderer, &frame);
  103. SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
  104. SDL_RenderDebugText(renderer, x, y, msgbuf); /* draw text about battery level */
  105. }
  106. if (msg) {
  107. const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
  108. const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2);
  109. SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
  110. SDL_RenderDebugText(renderer, x, y, msg);
  111. }
  112. if (msg2) {
  113. const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg2))) / 2.0f);
  114. const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 4);
  115. SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
  116. SDL_RenderDebugText(renderer, x, y, msg2);
  117. }
  118. /* put the new rendering on the screen. */
  119. SDL_RenderPresent(renderer);
  120. return SDL_APP_CONTINUE; /* carry on with the program! */
  121. }
  122. /* This function runs once at shutdown. */
  123. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  124. {
  125. /* SDL will clean up the window/renderer for us. */
  126. }