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