SDL_power.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. #ifndef SDL_power_h_
  19. #define SDL_power_h_
  20. /**
  21. * # CategoryPower
  22. *
  23. * SDL power management routines.
  24. *
  25. * There is a single function in this category: SDL_GetPowerInfo().
  26. *
  27. * This function is useful for games on the go. This allows an app to know if
  28. * it's running on a draining battery, which can be useful if the app wants to
  29. * reduce processing, or perhaps framerate, to extend the duration of the
  30. * battery's charge. Perhaps the app just wants to show a battery meter when
  31. * fullscreen, or alert the user when the power is getting extremely low, so
  32. * they can save their game.
  33. */
  34. #include <SDL3/SDL_stdinc.h>
  35. #include <SDL3/SDL_error.h>
  36. #include <SDL3/SDL_begin_code.h>
  37. /* Set up for C function definitions, even when using C++ */
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /**
  42. * The basic state for the system's power supply.
  43. *
  44. * These are results returned by SDL_GetPowerInfo().
  45. *
  46. * \since This enum is available since SDL 3.2.0.
  47. */
  48. typedef enum SDL_PowerState
  49. {
  50. SDL_POWERSTATE_ERROR = -1, /**< error determining power status */
  51. SDL_POWERSTATE_UNKNOWN, /**< cannot determine power status */
  52. SDL_POWERSTATE_ON_BATTERY, /**< Not plugged in, running on the battery */
  53. SDL_POWERSTATE_NO_BATTERY, /**< Plugged in, no battery available */
  54. SDL_POWERSTATE_CHARGING, /**< Plugged in, charging battery */
  55. SDL_POWERSTATE_CHARGED /**< Plugged in, battery charged */
  56. } SDL_PowerState;
  57. /**
  58. * Get the current power supply details.
  59. *
  60. * You should never take a battery status as absolute truth. Batteries
  61. * (especially failing batteries) are delicate hardware, and the values
  62. * reported here are best estimates based on what that hardware reports. It's
  63. * not uncommon for older batteries to lose stored power much faster than it
  64. * reports, or completely drain when reporting it has 20 percent left, etc.
  65. *
  66. * Battery status can change at any time; if you are concerned with power
  67. * state, you should call this function frequently, and perhaps ignore changes
  68. * until they seem to be stable for a few seconds.
  69. *
  70. * It's possible a platform can only report battery percentage or time left
  71. * but not both.
  72. *
  73. * On some platforms, retrieving power supply details might be expensive. If
  74. * you want to display continuous status you could call this function every
  75. * minute or so.
  76. *
  77. * \param seconds a pointer filled in with the seconds of battery life left,
  78. * or NULL to ignore. This will be filled in with -1 if we
  79. * can't determine a value or there is no battery.
  80. * \param percent a pointer filled in with the percentage of battery life
  81. * left, between 0 and 100, or NULL to ignore. This will be
  82. * filled in with -1 we can't determine a value or there is no
  83. * battery.
  84. * \returns the current battery state or `SDL_POWERSTATE_ERROR` on failure;
  85. * call SDL_GetError() for more information.
  86. *
  87. * \since This function is available since SDL 3.2.0.
  88. */
  89. extern SDL_DECLSPEC SDL_PowerState SDLCALL SDL_GetPowerInfo(int *seconds, int *percent);
  90. /* Ends C function definitions when using C++ */
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94. #include <SDL3/SDL_close_code.h>
  95. #endif /* SDL_power_h_ */