ReactOS  0.4.14-dev-815-ge410a12
power.c
Go to the documentation of this file.
1 /*
2  *
3  * COPYRIGHT: See COPYING in the top level directory
4  * PROJECT: ReactOS system libraries
5  * FILE: dll/win32/kernel32/client/power.c
6  * PURPOSE: Power Management Functions
7  * PROGRAMMER: Dmitry Chapyshev <dmitry@reactos.org>
8  *
9  * UPDATE HISTORY:
10  * 01/15/2009 Created
11  */
12 
13 #include <k32.h>
14 
15 #include <ndk/pofuncs.h>
16 
17 #define NDEBUG
18 #include <debug.h>
19 
20 /* PUBLIC FUNCTIONS ***********************************************************/
21 
22 /*
23  * @implemented
24  */
25 BOOL
26 WINAPI
28 {
30  SYSTEM_BATTERY_STATE BattState;
31  ULONG Max, Current;
32 
34  NULL,
35  0,
36  &BattState,
37  sizeof(SYSTEM_BATTERY_STATE));
38  if (!NT_SUCCESS(Status))
39  {
41  return FALSE;
42  }
43 
44  RtlZeroMemory(PowerStatus, sizeof(SYSTEM_POWER_STATUS));
45 
46  PowerStatus->BatteryLifeTime = BATTERY_LIFE_UNKNOWN;
47  PowerStatus->BatteryFullLifeTime = BATTERY_LIFE_UNKNOWN;
48  PowerStatus->BatteryLifePercent = BATTERY_PERCENTAGE_UNKNOWN;
49  PowerStatus->ACLineStatus = AC_LINE_ONLINE;
50 
51  Max = BattState.MaxCapacity;
52  Current = BattState.RemainingCapacity;
53  if (Max)
54  {
55  if (Current <= Max)
56  {
57  PowerStatus->BatteryLifePercent = (UCHAR)((100 * Current + Max / 2) / Max);
58  }
59  else
60  {
61  PowerStatus->BatteryLifePercent = 100;
62  }
63 
64  if (PowerStatus->BatteryLifePercent <= 4)
65  PowerStatus->BatteryFlag |= BATTERY_FLAG_CRITICAL;
66 
67  if (PowerStatus->BatteryLifePercent <= 32)
68  PowerStatus->BatteryFlag |= BATTERY_FLAG_LOW;
69 
70  if (PowerStatus->BatteryLifePercent >= 67)
71  PowerStatus->BatteryFlag |= BATTERY_FLAG_HIGH;
72  }
73 
74  if (!BattState.BatteryPresent)
75  PowerStatus->BatteryFlag |= BATTERY_FLAG_NO_BATTERY;
76 
77  if (BattState.Charging)
78  PowerStatus->BatteryFlag |= BATTERY_FLAG_CHARGING;
79 
80  if (!(BattState.AcOnLine) && (BattState.BatteryPresent))
81  PowerStatus->ACLineStatus = AC_LINE_OFFLINE;
82 
83  if (BattState.EstimatedTime)
84  PowerStatus->BatteryLifeTime = BattState.EstimatedTime;
85 
86  return TRUE;
87 }
88 
89 /*
90  * @implemented
91  */
92 BOOL
93 WINAPI
95  IN BOOL fForce)
96 {
98 
101  (fForce == FALSE),
102  FALSE);
103  if (!NT_SUCCESS(Status))
104  {
106  return FALSE;
107  }
108 
109  return TRUE;
110 }
111 
112 /*
113  * @implemented
114  */
115 BOOL
116 WINAPI
118  OUT BOOL *pfOn)
119 {
122 
124  if (NT_SUCCESS(Status))
125  {
128  return TRUE;
129  }
130 
132  return FALSE;
133 }
134 
135 /*
136  * @implemented
137  */
138 BOOL
139 WINAPI
141 {
143 
144  Status = NtRequestDeviceWakeup(hDevice);
145  if (!NT_SUCCESS(Status))
146  {
148  return FALSE;
149  }
150 
151  return TRUE;
152 }
153 
154 /*
155  * @implemented
156  */
157 BOOL
158 WINAPI
160 {
162 
163  Status = NtRequestWakeupLatency(latency);
164  if (!NT_SUCCESS(Status))
165  {
167  return FALSE;
168  }
169 
170  return TRUE;
171 }
172 
173 /*
174  * @implemented
175  */
176 BOOL
177 WINAPI
179 {
181 
183  if (!NT_SUCCESS(Status))
184  {
186  return FALSE;
187  }
188 
189  return TRUE;
190 }
191 
192 /*
193  * @implemented
194  */
195 BOOL
196 WINAPI
198 {
200 }
201 
202 /*
203  * @implemented
204  */
205 BOOL
206 WINAPI
208  IN ULONG ulMsgCount)
209 {
210  /* This is the correct Windows implementation */
212  return 0;
213 }
214 
215 /*
216  * @implemented
217  */
219 WINAPI
221 {
223 
224  Status = NtSetThreadExecutionState(esFlags, &esFlags);
225  if (!NT_SUCCESS(Status))
226  {
228  return 0;
229  }
230 
231  return esFlags;
232 }
BOOL WINAPI SetSystemPowerState(IN BOOL fSuspend, IN BOOL fForce)
Definition: power.c:94
#define IN
Definition: typedefs.h:38
#define BATTERY_PERCENTAGE_UNKNOWN
Definition: winbase.h:502
#define TRUE
Definition: types.h:120
BOOL WINAPI CancelDeviceWakeupRequest(IN HANDLE hDevice)
Definition: power.c:178
LONG NTSTATUS
Definition: precomp.h:26
BOOL WINAPI GetSystemPowerStatus(IN LPSYSTEM_POWER_STATUS PowerStatus)
Definition: power.c:27
#define BATTERY_FLAG_NO_BATTERY
Definition: winbase.h:500
BOOL WINAPI RequestDeviceWakeup(IN HANDLE hDevice)
Definition: power.c:140
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI SetMessageWaitingIndicator(IN HANDLE hMsgIndicator, IN ULONG ulMsgCount)
Definition: power.c:207
#define Max(a, b)
Definition: cdprocs.h:70
#define BATTERY_FLAG_HIGH
Definition: winbase.h:496
NTSTATUS NTAPI NtInitiatePowerAction(IN POWER_ACTION SystemAction, IN SYSTEM_POWER_STATE MinSystemState, IN ULONG Flags, IN BOOLEAN Asynchronous)
Definition: power.c:646
smooth NULL
Definition: ftsmooth.c:416
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
LATENCY_TIME
Definition: potypes.h:171
#define AC_LINE_ONLINE
Definition: winbase.h:493
#define BATTERY_LIFE_UNKNOWN
Definition: winbase.h:503
enum _DEVICE_POWER_STATE DEVICE_POWER_STATE
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS NTAPI NtGetDevicePowerState(IN HANDLE Device, IN PDEVICE_POWER_STATE PowerState)
Definition: power.c:787
BOOL WINAPI GetDevicePowerState(IN HANDLE hDevice, OUT BOOL *pfOn)
Definition: power.c:117
BOOLEAN NTAPI NtIsSystemResumeAutomatic(VOID)
Definition: power.c:796
#define WINAPI
Definition: msvc.h:6
ULONG RemainingCapacity
Definition: ntpoapi.h:391
BOOL WINAPI IsSystemResumeAutomatic(VOID)
Definition: power.c:197
#define SetLastError(x)
Definition: compat.h:417
unsigned char UCHAR
Definition: xmlstorage.h:181
NTSTATUS NTAPI NtRequestWakeupLatency(IN LATENCY_TIME Latency)
Definition: power.c:804
ULONG EXECUTION_STATE
Definition: potypes.h:169
Status
Definition: gdiplustypes.h:24
BOOL WINAPI RequestWakeupLatency(IN LATENCY_TIME latency)
Definition: power.c:159
#define AC_LINE_OFFLINE
Definition: winbase.h:492
NTSTATUS NTAPI NtRequestDeviceWakeup(IN HANDLE DeviceHandle)
Definition: iofunc.c:4576
#define BATTERY_FLAG_CHARGING
Definition: winbase.h:499
NTSTATUS NTAPI NtPowerInformation(IN POWER_INFORMATION_LEVEL PowerInformationLevel, IN PVOID InputBuffer OPTIONAL, IN ULONG InputBufferLength, OUT PVOID OutputBuffer OPTIONAL, IN ULONG OutputBufferLength)
Definition: power.c:660
#define BATTERY_FLAG_CRITICAL
Definition: winbase.h:498
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:92
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
EXECUTION_STATE WINAPI SetThreadExecutionState(EXECUTION_STATE esFlags)
Definition: power.c:220
NTSTATUS NTAPI NtCancelDeviceWakeupRequest(IN HANDLE DeviceHandle)
Definition: iofunc.c:4565
NTSTATUS NTAPI NtSetThreadExecutionState(IN EXECUTION_STATE esFlags, OUT EXECUTION_STATE *PreviousFlags)
Definition: power.c:812
BOOLEAN BatteryPresent
Definition: ntpoapi.h:386
#define BATTERY_FLAG_LOW
Definition: winbase.h:497