* Sync to trunk HEAD (r53298).
[reactos.git] / dll / win32 / kernel32 / client / power.c
1 /*
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: dll/win32/kernel32/misc/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 #define NDEBUG
16 #include <debug.h>
17
18 /* PUBLIC FUNCTIONS ***********************************************************/
19
20 /*
21 * @implemented
22 */
23 BOOL
24 WINAPI
25 GetSystemPowerStatus(IN LPSYSTEM_POWER_STATUS PowerStatus)
26 {
27 NTSTATUS Status;
28 SYSTEM_BATTERY_STATE BattState;
29 ULONG Max, Current;
30
31 Status = NtPowerInformation(SystemBatteryState,
32 NULL,
33 0,
34 &BattState,
35 sizeof(SYSTEM_BATTERY_STATE));
36
37 if (!NT_SUCCESS(Status))
38 {
39 BaseSetLastNTError(Status);
40 return FALSE;
41 }
42
43 RtlZeroMemory(PowerStatus, sizeof(LPSYSTEM_POWER_STATUS));
44
45 PowerStatus->BatteryLifeTime = BATTERY_LIFE_UNKNOWN;
46 PowerStatus->BatteryFullLifeTime = BATTERY_LIFE_UNKNOWN;
47 PowerStatus->BatteryLifePercent = BATTERY_PERCENTAGE_UNKNOWN;
48 PowerStatus->ACLineStatus = AC_LINE_ONLINE;
49
50 Max = BattState.MaxCapacity;
51 Current = BattState.RemainingCapacity;
52 if (Max)
53 {
54 if (Current <= Max)
55 {
56 PowerStatus->BatteryLifePercent = (100 * Current + Max / 2) / Max;
57 }
58 else
59 {
60 PowerStatus->BatteryLifePercent = 100;
61 }
62
63 if (PowerStatus->BatteryLifePercent <= 32) PowerStatus->BatteryFlag |= BATTERY_FLAG_LOW;
64 if (PowerStatus->BatteryLifePercent >= 67) PowerStatus->BatteryFlag |= BATTERY_FLAG_HIGH;
65 }
66
67 if (!BattState.BatteryPresent) PowerStatus->BatteryFlag |= BATTERY_FLAG_NO_BATTERY;
68
69 if (BattState.Charging) PowerStatus->BatteryFlag |= BATTERY_FLAG_CHARGING;
70
71 if (!(BattState.AcOnLine) && (BattState.BatteryPresent)) PowerStatus->ACLineStatus = AC_LINE_OFFLINE;
72
73 if (BattState.EstimatedTime) PowerStatus->BatteryLifeTime = BattState.EstimatedTime;
74
75 return TRUE;
76 }
77
78 /*
79 * @implemented
80 */
81 BOOL
82 WINAPI
83 SetSystemPowerState(IN BOOL fSuspend,
84 IN BOOL fForce)
85 {
86 NTSTATUS Status;
87
88 Status = NtInitiatePowerAction(PowerActionSleep,
89 (fSuspend != FALSE) ?
90 PowerSystemSleeping1 : PowerSystemHibernate,
91 fForce != TRUE,
92 FALSE);
93 if (!NT_SUCCESS(Status))
94 {
95 BaseSetLastNTError(Status);
96 return FALSE;
97 }
98
99 return TRUE;
100 }
101
102 /*
103 * @implemented
104 */
105 BOOL
106 WINAPI
107 GetDevicePowerState(IN HANDLE hDevice,
108 OUT BOOL *pfOn)
109 {
110 DEVICE_POWER_STATE DevicePowerState;
111 NTSTATUS Status;
112
113 Status = NtGetDevicePowerState(hDevice, &DevicePowerState);
114 if (NT_SUCCESS(Status))
115 {
116 *pfOn = (DevicePowerState == PowerDeviceUnspecified) ||
117 (DevicePowerState == PowerDeviceD0);
118 return TRUE;
119 }
120
121 BaseSetLastNTError(Status);
122 return FALSE;
123 }
124
125 /*
126 * @implemented
127 */
128 BOOL
129 WINAPI
130 RequestDeviceWakeup(IN HANDLE hDevice)
131 {
132 NTSTATUS Status;
133
134 Status = NtRequestDeviceWakeup(hDevice);
135 if (!NT_SUCCESS(Status))
136 {
137 BaseSetLastNTError(Status);
138 return FALSE;
139 }
140
141 return TRUE;
142 }
143
144 /*
145 * @implemented
146 */
147 BOOL
148 WINAPI
149 RequestWakeupLatency(IN LATENCY_TIME latency)
150 {
151 NTSTATUS Status;
152
153 Status = NtRequestWakeupLatency(latency);
154 if (!NT_SUCCESS(Status))
155 {
156 BaseSetLastNTError(Status);
157 return FALSE;
158 }
159
160 return TRUE;
161 }
162
163 /*
164 * @implemented
165 */
166 BOOL
167 WINAPI
168 CancelDeviceWakeupRequest(IN HANDLE hDevice)
169 {
170 NTSTATUS Status;
171
172 Status = NtCancelDeviceWakeupRequest(hDevice);
173 if (!NT_SUCCESS(Status))
174 {
175 BaseSetLastNTError(Status);
176 return FALSE;
177 }
178
179 return TRUE;
180 }
181
182 /*
183 * @implemented
184 */
185 BOOL
186 WINAPI
187 IsSystemResumeAutomatic(VOID)
188 {
189 return (BOOL)NtIsSystemResumeAutomatic();
190 }
191
192 /*
193 * @implemented
194 */
195 BOOL
196 WINAPI
197 SetMessageWaitingIndicator(IN HANDLE hMsgIndicator,
198 IN ULONG ulMsgCount)
199 {
200 /* This is the correct Windows implementation */
201 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
202 return 0;
203 }
204
205 /*
206 * @implemented
207 */
208 EXECUTION_STATE
209 WINAPI
210 SetThreadExecutionState(EXECUTION_STATE esFlags)
211 {
212 NTSTATUS Status;
213
214 Status = NtSetThreadExecutionState(esFlags, &esFlags);
215 if (!NT_SUCCESS(Status))
216 {
217 BaseSetLastNTError(Status);
218 return 0;
219 }
220
221 return esFlags;
222 }