🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB

  1. /*
  2. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Simple test of power subsystem. */
  11. #include <stdio.h>
  12. #include "SDL.h"
  13. static void
  14. report_power(void)
  15. {
  16. int seconds, percent;
  17. const SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
  18. const char *statestr = NULL;
  19. SDL_Log("SDL-reported power info...\n");
  20. switch (state) {
  21. case SDL_POWERSTATE_UNKNOWN:
  22. statestr = "Unknown";
  23. break;
  24. case SDL_POWERSTATE_ON_BATTERY:
  25. statestr = "On battery";
  26. break;
  27. case SDL_POWERSTATE_NO_BATTERY:
  28. statestr = "No battery";
  29. break;
  30. case SDL_POWERSTATE_CHARGING:
  31. statestr = "Charging";
  32. break;
  33. case SDL_POWERSTATE_CHARGED:
  34. statestr = "Charged";
  35. break;
  36. default:
  37. statestr = "!!API ERROR!!";
  38. break;
  39. }
  40. SDL_Log("State: %s\n", statestr);
  41. if (percent == -1) {
  42. SDL_Log("Percent left: unknown\n");
  43. } else {
  44. SDL_Log("Percent left: %d%%\n", percent);
  45. }
  46. if (seconds == -1) {
  47. SDL_Log("Time left: unknown\n");
  48. } else {
  49. SDL_Log("Time left: %d minutes, %d seconds\n", seconds / 60, seconds % 60);
  50. }
  51. }
  52. int main(int argc, char *argv[])
  53. {
  54. /* Enable standard application logging */
  55. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  56. if (SDL_Init(0) == -1) {
  57. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
  58. return 1;
  59. }
  60. report_power();
  61. SDL_Quit();
  62. return 0;
  63. }
  64. /* end of testpower.c ... */