power_windows.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**************************************************************************/
  2. /* power_windows.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. /*
  31. Adapted from corresponding SDL 2.0 code.
  32. */
  33. /*
  34. * Simple DirectMedia Layer
  35. * Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
  36. *
  37. * This software is provided 'as-is', without any express or implied
  38. * warranty. In no event will the authors be held liable for any damages
  39. * arising from the use of this software.
  40. *
  41. * Permission is granted to anyone to use this software for any purpose,
  42. * including commercial applications, and to alter it and redistribute it
  43. * freely, subject to the following restrictions:
  44. *
  45. * 1. The origin of this software must not be misrepresented; you must not
  46. * claim that you wrote the original software. If you use this software
  47. * in a product, an acknowledgment in the product documentation would be
  48. * appreciated but is not required.
  49. * 2. Altered source versions must be plainly marked as such, and must not be
  50. * misrepresented as being the original software.
  51. * 3. This notice may not be removed or altered from any source distribution.
  52. */
  53. #include "power_windows.h"
  54. // CODE CHUNK IMPORTED FROM SDL 2.0
  55. bool PowerWindows::GetPowerInfo_Windows() {
  56. SYSTEM_POWER_STATUS status;
  57. bool need_details = FALSE;
  58. /* This API should exist back to Win95. */
  59. if (!GetSystemPowerStatus(&status)) {
  60. /* !!! FIXME: push GetLastError() into GetError() */
  61. power_state = OS::POWERSTATE_UNKNOWN;
  62. } else if (status.BatteryFlag == 0xFF) { /* unknown state */
  63. power_state = OS::POWERSTATE_UNKNOWN;
  64. } else if (status.BatteryFlag & (1 << 7)) { /* no battery */
  65. power_state = OS::POWERSTATE_NO_BATTERY;
  66. } else if (status.BatteryFlag & (1 << 3)) { /* charging */
  67. power_state = OS::POWERSTATE_CHARGING;
  68. need_details = TRUE;
  69. } else if (status.ACLineStatus == 1) {
  70. power_state = OS::POWERSTATE_CHARGED; /* on AC, not charging. */
  71. need_details = TRUE;
  72. } else {
  73. power_state = OS::POWERSTATE_ON_BATTERY; /* not on AC. */
  74. need_details = TRUE;
  75. }
  76. percent_left = -1;
  77. nsecs_left = -1;
  78. if (need_details) {
  79. const int pct = (int)status.BatteryLifePercent;
  80. const int secs = (int)status.BatteryLifeTime;
  81. if (pct != 255) { /* 255 == unknown */
  82. percent_left = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
  83. }
  84. if (secs != (int)0xFFFFFFFF) { /* ((DWORD)-1) == unknown */
  85. nsecs_left = secs;
  86. }
  87. }
  88. return TRUE; /* always the definitive answer on Windows. */
  89. }
  90. OS::PowerState PowerWindows::get_power_state() {
  91. if (GetPowerInfo_Windows()) {
  92. return power_state;
  93. } else {
  94. return OS::POWERSTATE_UNKNOWN;
  95. }
  96. }
  97. int PowerWindows::get_power_seconds_left() {
  98. if (GetPowerInfo_Windows()) {
  99. return nsecs_left;
  100. } else {
  101. return -1;
  102. }
  103. }
  104. int PowerWindows::get_power_percent_left() {
  105. if (GetPowerInfo_Windows()) {
  106. return percent_left;
  107. } else {
  108. return -1;
  109. }
  110. }
  111. PowerWindows::PowerWindows() :
  112. nsecs_left(-1),
  113. percent_left(-1),
  114. power_state(OS::POWERSTATE_UNKNOWN) {
  115. }
  116. PowerWindows::~PowerWindows() {
  117. }