ReactOS 0.4.15-dev-7887-g64a59a1
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 */
25BOOL
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 */
92BOOL
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 */
115BOOL
116WINAPI
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 */
138BOOL
139WINAPI
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 */
157BOOL
158WINAPI
160{
162
164 if (!NT_SUCCESS(Status))
165 {
167 return FALSE;
168 }
169
170 return TRUE;
171}
172
173/*
174 * @implemented
175 */
176BOOL
177WINAPI
179{
181
183 if (!NT_SUCCESS(Status))
184 {
186 return FALSE;
187 }
188
189 return TRUE;
190}
191
192/*
193 * @implemented
194 */
195BOOL
196WINAPI
198{
200}
201
202/*
203 * @implemented
204 */
205BOOL
206WINAPI
208 IN ULONG ulMsgCount)
209{
210 /* This is the correct Windows implementation */
212 return 0;
213}
214
215/*
216 * @implemented
217 */
219WINAPI
221{
223
224 Status = NtSetThreadExecutionState(esFlags, &esFlags);
225 if (!NT_SUCCESS(Status))
226 {
228 return 0;
229 }
230
231 return esFlags;
232}
LONG NTSTATUS
Definition: precomp.h:26
#define Max(a, b)
Definition: cdprocs.h:78
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:102
#define SetLastError(x)
Definition: compat.h:752
BOOL WINAPI GetSystemPowerStatus(IN LPSYSTEM_POWER_STATUS PowerStatus)
Definition: power.c:27
BOOL WINAPI SetMessageWaitingIndicator(IN HANDLE hMsgIndicator, IN ULONG ulMsgCount)
Definition: power.c:207
BOOL WINAPI RequestWakeupLatency(IN LATENCY_TIME latency)
Definition: power.c:159
BOOL WINAPI RequestDeviceWakeup(IN HANDLE hDevice)
Definition: power.c:140
BOOL WINAPI SetSystemPowerState(IN BOOL fSuspend, IN BOOL fForce)
Definition: power.c:94
BOOL WINAPI CancelDeviceWakeupRequest(IN HANDLE hDevice)
Definition: power.c:178
EXECUTION_STATE WINAPI SetThreadExecutionState(EXECUTION_STATE esFlags)
Definition: power.c:220
unsigned int BOOL
Definition: ntddk_ex.h:94
return pDevice GetDevicePowerState()
Status
Definition: gdiplustypes.h:25
NTSTATUS NTAPI NtRequestDeviceWakeup(IN HANDLE DeviceHandle)
Definition: iofunc.c:4649
NTSTATUS NTAPI NtCancelDeviceWakeupRequest(IN HANDLE DeviceHandle)
Definition: iofunc.c:4638
NTSTATUS NTAPI NtInitiatePowerAction(IN POWER_ACTION SystemAction, IN SYSTEM_POWER_STATE MinSystemState, IN ULONG Flags, IN BOOLEAN Asynchronous)
Definition: power.c:778
BOOLEAN NTAPI NtIsSystemResumeAutomatic(VOID)
Definition: power.c:934
NTSTATUS NTAPI NtRequestWakeupLatency(IN LATENCY_TIME Latency)
Definition: power.c:942
NTSTATUS NTAPI NtSetThreadExecutionState(IN EXECUTION_STATE esFlags, OUT EXECUTION_STATE *PreviousFlags)
Definition: power.c:950
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:792
NTSTATUS NTAPI NtGetDevicePowerState(IN HANDLE Device, IN PDEVICE_POWER_STATE PowerState)
Definition: power.c:925
@ PowerSystemSleeping1
Definition: ntpoapi.h:37
@ PowerSystemHibernate
Definition: ntpoapi.h:40
@ DevicePowerState
Definition: ntpoapi.h:63
@ PowerActionHibernate
Definition: ntpoapi.h:126
@ PowerActionSleep
Definition: ntpoapi.h:125
@ PowerDeviceUnspecified
Definition: ntpoapi.h:48
@ PowerDeviceD0
Definition: ntpoapi.h:49
enum _DEVICE_POWER_STATE DEVICE_POWER_STATE
@ SystemBatteryState
Definition: ntpoapi.h:72
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
ULONG RemainingCapacity
Definition: ntpoapi.h:391
BOOLEAN BatteryPresent
Definition: ntpoapi.h:386
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
BOOL WINAPI IsSystemResumeAutomatic(void)
Definition: power.c:197
#define BATTERY_FLAG_CHARGING
Definition: winbase.h:518
#define BATTERY_LIFE_UNKNOWN
Definition: winbase.h:522
#define BATTERY_FLAG_LOW
Definition: winbase.h:516
#define BATTERY_FLAG_NO_BATTERY
Definition: winbase.h:519
#define AC_LINE_ONLINE
Definition: winbase.h:512
#define BATTERY_FLAG_CRITICAL
Definition: winbase.h:517
#define AC_LINE_OFFLINE
Definition: winbase.h:511
#define BATTERY_FLAG_HIGH
Definition: winbase.h:515
#define BATTERY_PERCENTAGE_UNKNOWN
Definition: winbase.h:521
#define WINAPI
Definition: msvc.h:6
LATENCY_TIME
Definition: potypes.h:215
ULONG EXECUTION_STATE
Definition: potypes.h:213
unsigned char UCHAR
Definition: xmlstorage.h:181