Autosyncing with Wine HEAD
[reactos.git] / reactos / dll / win32 / powrprof / powrprof.c
1 /*
2 * Copyright (C) 2005 Benjamin Cutler
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19
20 #include <stdarg.h>
21
22 #include "ntstatus.h"
23 #define WIN32_NO_STATUS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnt.h"
27 #include "winreg.h"
28 #include "winternl.h"
29 #include "powrprof.h"
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(powrprof);
34
35 /* Notes to implementors:
36 * #1: The native implementation of these functions attempted to read in
37 * registry entries that I was unable to locate on any of the Windows
38 * machines I checked, but I only had desktops available, so maybe
39 * laptop users will have better luck. They return FNF errors because
40 * that's what the native DLL was returning during my tests.
41 * #2: These functions call NtPowerInformation but I don't know what they
42 * do with the results, and NtPowerInformation doesn't do much in WINE yet
43 * anyway.
44 * #3: Since I can't get several other functions working (see note #1),
45 * implementing these functions is going to have to wait until somebody can
46 * cobble together some sane test input. */
47
48 static const WCHAR szPowerCfgSubKey[] = { 'S', 'o', 'f', 't', 'w', 'a', 'r', 'e',
49 '\\', 'M', 'i', 'c', 'r', 'o', 's', 'o', 'f', 't', '\\', 'W', 'i',
50 'n', 'd', 'o', 'w', 's', '\\', 'C', 'u', 'r', 'r', 'e', 'n', 't',
51 'V', 'e', 'r', 's', 'i', 'o', 'n', '\\', 'C', 'o', 'n', 't', 'r',
52 'o', 'l', 's', ' ', 'F', 'o', 'l', 'd', 'e', 'r', '\\', 'P', 'o',
53 'w', 'e', 'r', 'C', 'f', 'g', 0 };
54 static const WCHAR szSemaphoreName[] = { 'P', 'o', 'w', 'e', 'r', 'P', 'r', 'o',
55 'f', 'i', 'l', 'e', 'R', 'e', 'g', 'i', 's', 't', 'r', 'y', 'S',
56 'e', 'm', 'a', 'p', 'h', 'o', 'r', 'e', 0 };
57 static const WCHAR szDiskMax[] = { 'D', 'i', 's', 'k', 'S', 'p', 'i', 'n', 'd',
58 'o', 'w', 'n', 'M', 'a', 'x', 0 };
59 static const WCHAR szDiskMin[] = { 'D', 'i', 's', 'k', 'S', 'p', 'i', 'n', 'd',
60 'o', 'w', 'n', 'M', 'i', 'n', 0 };
61 static const WCHAR szLastID[] = { 'L', 'a', 's', 't', 'I', 'D', 0 };
62 static HANDLE PPRegSemaphore = NULL;
63
64 NTSTATUS WINAPI CallNtPowerInformation(
65 POWER_INFORMATION_LEVEL InformationLevel,
66 PVOID lpInputBuffer, ULONG nInputBufferSize,
67 PVOID lpOutputBuffer, ULONG nOutputBufferSize)
68 {
69 return NtPowerInformation(InformationLevel, lpInputBuffer,
70 nInputBufferSize, lpOutputBuffer, nOutputBufferSize);
71 }
72
73 BOOLEAN WINAPI CanUserWritePwrScheme(VOID)
74 {
75 HKEY hKey = NULL;
76 LONG r;
77 BOOLEAN bSuccess = TRUE;
78
79 TRACE("()\n");
80
81 r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, szPowerCfgSubKey, 0, KEY_READ | KEY_WRITE, &hKey);
82
83 if (r != ERROR_SUCCESS) {
84 TRACE("RegOpenKeyEx failed: %d\n", r);
85 bSuccess = FALSE;
86 }
87
88 SetLastError(r);
89 RegCloseKey(hKey);
90 return bSuccess;
91 }
92
93 BOOLEAN WINAPI DeletePwrScheme(UINT uiIndex)
94 {
95 /* FIXME: See note #1 */
96 FIXME("(%d) stub!\n", uiIndex);
97 SetLastError(ERROR_FILE_NOT_FOUND);
98 return FALSE;
99 }
100
101 BOOLEAN WINAPI EnumPwrSchemes(PWRSCHEMESENUMPROC lpfnPwrSchemesEnumProc,
102 LPARAM lParam)
103 {
104 /* FIXME: See note #1 */
105 FIXME("(%p, %ld) stub!\n", lpfnPwrSchemesEnumProc, lParam);
106 SetLastError(ERROR_FILE_NOT_FOUND);
107 return FALSE;
108 }
109
110 BOOLEAN WINAPI GetActivePwrScheme(PUINT puiID)
111 {
112 /* FIXME: See note #1 */
113 FIXME("(%p) stub!\n", puiID);
114 SetLastError(ERROR_FILE_NOT_FOUND);
115 return FALSE;
116 }
117
118 BOOLEAN WINAPI GetCurrentPowerPolicies(
119 PGLOBAL_POWER_POLICY pGlobalPowerPolicy,
120 PPOWER_POLICY pPowerPolicy)
121 {
122 /* FIXME: See note #2 */
123 SYSTEM_POWER_POLICY ACPower, DCPower;
124
125 FIXME("(%p, %p) stub!\n", pGlobalPowerPolicy, pPowerPolicy);
126
127 NtPowerInformation(SystemPowerPolicyAc, 0, 0, &ACPower, sizeof(SYSTEM_POWER_POLICY));
128 NtPowerInformation(SystemPowerPolicyDc, 0, 0, &DCPower, sizeof(SYSTEM_POWER_POLICY));
129
130 return FALSE;
131 }
132
133 BOOLEAN WINAPI GetPwrCapabilities(
134 PSYSTEM_POWER_CAPABILITIES lpSystemPowerCapabilities)
135 {
136 NTSTATUS r;
137
138 TRACE("(%p)\n", lpSystemPowerCapabilities);
139
140 r = NtPowerInformation(SystemPowerCapabilities, 0, 0, lpSystemPowerCapabilities, sizeof(SYSTEM_POWER_CAPABILITIES));
141
142 SetLastError(RtlNtStatusToDosError(r));
143
144 return r == STATUS_SUCCESS;
145 }
146
147 BOOLEAN WINAPI GetPwrDiskSpindownRange(PUINT RangeMax, PUINT RangeMin)
148 {
149 HKEY hKey;
150 BYTE lpValue[40];
151 LONG r;
152 DWORD cbValue = sizeof(lpValue);
153
154 TRACE("(%p, %p)\n", RangeMax, RangeMin);
155
156 if (RangeMax == NULL || RangeMin == NULL) {
157 SetLastError(ERROR_INVALID_PARAMETER);
158 return FALSE;
159 }
160
161 SetLastError(ERROR_SUCCESS);
162
163 WaitForSingleObject(PPRegSemaphore, INFINITE);
164
165 r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, szPowerCfgSubKey, 0, KEY_READ, &hKey);
166 if (r != ERROR_SUCCESS) {
167 TRACE("RegOpenKeyEx failed: %d\n", r);
168 TRACE("Using defaults: 3600, 3\n");
169 *RangeMax = 3600;
170 *RangeMin = 3;
171 ReleaseSemaphore(PPRegSemaphore, 1, NULL);
172 return TRUE;
173 }
174
175 r = RegQueryValueExW(hKey, szDiskMax, 0, 0, lpValue, &cbValue);
176 if (r != ERROR_SUCCESS) {
177 TRACE("Couldn't open DiskSpinDownMax: %d\n", r);
178 TRACE("Using default: 3600\n");
179 *RangeMax = 3600;
180 } else {
181 *RangeMax = atoiW((LPCWSTR)lpValue);
182 }
183
184 cbValue = sizeof(lpValue);
185
186 r = RegQueryValueExW(hKey, szDiskMin, 0, 0, lpValue, &cbValue);
187 if (r != ERROR_SUCCESS) {
188 TRACE("Couldn't open DiskSpinDownMin: %d\n", r);
189 TRACE("Using default: 3\n");
190 *RangeMin = 3;
191 } else {
192 *RangeMin = atoiW((LPCWSTR)lpValue);
193 }
194
195 RegCloseKey(hKey);
196
197 ReleaseSemaphore(PPRegSemaphore, 1, NULL);
198
199 return TRUE;
200 }
201
202 BOOLEAN WINAPI IsAdminOverrideActive(PADMINISTRATOR_POWER_POLICY p)
203 {
204 FIXME("( %p) stub!\n", p);
205 return FALSE;
206 }
207
208 BOOLEAN WINAPI IsPwrHibernateAllowed(VOID)
209 {
210 /* FIXME: See note #2 */
211 SYSTEM_POWER_CAPABILITIES PowerCaps;
212 FIXME("() stub!\n");
213 NtPowerInformation(SystemPowerCapabilities, NULL, 0, &PowerCaps, sizeof(PowerCaps));
214 return FALSE;
215 }
216
217 BOOLEAN WINAPI IsPwrShutdownAllowed(VOID)
218 {
219 /* FIXME: See note #2 */
220 SYSTEM_POWER_CAPABILITIES PowerCaps;
221 FIXME("() stub!\n");
222 NtPowerInformation(SystemPowerCapabilities, NULL, 0, &PowerCaps, sizeof(PowerCaps));
223 return FALSE;
224 }
225
226 BOOLEAN WINAPI IsPwrSuspendAllowed(VOID)
227 {
228 /* FIXME: See note #2 */
229 SYSTEM_POWER_CAPABILITIES PowerCaps;
230 FIXME("() stub!\n");
231 NtPowerInformation(SystemPowerCapabilities, NULL, 0, &PowerCaps, sizeof(PowerCaps));
232 return FALSE;
233 }
234
235 BOOLEAN WINAPI ReadGlobalPwrPolicy(PGLOBAL_POWER_POLICY pGlobalPowerPolicy)
236 {
237 /* FIXME: See note #1 */
238 FIXME("(%p) stub!\n", pGlobalPowerPolicy);
239 SetLastError(ERROR_FILE_NOT_FOUND);
240 return FALSE;
241 }
242
243 BOOLEAN WINAPI ReadProcessorPwrScheme(UINT uiID,
244 PMACHINE_PROCESSOR_POWER_POLICY pMachineProcessorPowerPolicy)
245 {
246 /* FIXME: See note #1 */
247 FIXME("(%d, %p) stub!\n", uiID, pMachineProcessorPowerPolicy);
248 SetLastError(ERROR_FILE_NOT_FOUND);
249 return FALSE;
250 }
251
252 BOOLEAN WINAPI ReadPwrScheme(UINT uiID,
253 PPOWER_POLICY pPowerPolicy)
254 {
255 /* FIXME: See note #1 */
256 FIXME("(%d, %p) stub!\n", uiID, pPowerPolicy);
257 SetLastError(ERROR_FILE_NOT_FOUND);
258 return FALSE;
259 }
260
261 BOOLEAN WINAPI SetActivePwrScheme(UINT uiID,
262 PGLOBAL_POWER_POLICY lpGlobalPowerPolicy,
263 PPOWER_POLICY lpPowerPolicy)
264 {
265 /* FIXME: See note #1 */
266 FIXME("(%d, %p, %p) stub!\n", uiID, lpGlobalPowerPolicy, lpPowerPolicy);
267 SetLastError(ERROR_FILE_NOT_FOUND);
268 return FALSE;
269 }
270
271 BOOLEAN WINAPI SetSuspendState(BOOLEAN Hibernate, BOOLEAN ForceCritical,
272 BOOLEAN DisableWakeEvent)
273 {
274 /* FIXME: I have NO idea how you're supposed to call NtInitiatePowerAction
275 * here, because it's not a documented function that I can find */
276 FIXME("(%d, %d, %d) stub!\n", Hibernate, ForceCritical, DisableWakeEvent);
277 return TRUE;
278 }
279
280 BOOLEAN WINAPI WriteGlobalPwrPolicy(PGLOBAL_POWER_POLICY pGlobalPowerPolicy)
281 {
282 /* FIXME: See note #3 */
283 FIXME("(%p) stub!\n", pGlobalPowerPolicy);
284 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
285 return FALSE;
286 }
287
288 BOOLEAN WINAPI WriteProcessorPwrScheme(UINT ID,
289 PMACHINE_PROCESSOR_POWER_POLICY pMachineProcessorPowerPolicy)
290 {
291 /* FIXME: See note #3 */
292 FIXME("(%d, %p) stub!\n", ID, pMachineProcessorPowerPolicy);
293 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
294 return FALSE;
295 }
296
297 BOOLEAN WINAPI WritePwrScheme(PUINT puiID, LPWSTR lpszName, LPWSTR lpszDescription,
298 PPOWER_POLICY pPowerPolicy)
299 {
300 /* FIXME: See note #3 */
301 FIXME("(%p, %s, %s, %p) stub!\n", puiID, debugstr_w(lpszName), debugstr_w(lpszDescription), pPowerPolicy);
302 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
303 return FALSE;
304 }
305
306 BOOLEAN WINAPI ValidatePowerPolicies(PGLOBAL_POWER_POLICY pGPP, PPOWER_POLICY pPP)
307 {
308 /* FIXME: See note #3 */
309 FIXME("(%p, %p) stub!\n", pGPP, pPP);
310 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
311 return TRUE;
312
313 }
314
315 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
316 {
317 FIXME("(%p, %d, %p) not fully implemented\n", hinstDLL, fdwReason, lpvReserved);
318
319 switch(fdwReason) {
320 case DLL_PROCESS_ATTACH: {
321
322 HKEY hKey;
323 LONG r;
324
325 DisableThreadLibraryCalls(hinstDLL);
326
327 r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, szPowerCfgSubKey, 0, KEY_READ | KEY_WRITE, &hKey);
328
329 if (r != ERROR_SUCCESS) {
330 TRACE("Couldn't open registry key HKLM\\%s, using some sane(?) defaults\n", debugstr_w(szPowerCfgSubKey));
331 } else {
332 BYTE lpValue[40];
333 DWORD cbValue = sizeof(lpValue);
334 r = RegQueryValueExW(hKey, szLastID, 0, 0, lpValue, &cbValue);
335 if (r != ERROR_SUCCESS) {
336 TRACE("Couldn't open registry entry HKLM\\%s\\LastID, using some sane(?) defaults\n", debugstr_w(szPowerCfgSubKey));
337 }
338 RegCloseKey(hKey);
339 }
340
341 PPRegSemaphore = CreateSemaphoreW(NULL, 1, 1, szSemaphoreName);
342 if (PPRegSemaphore == NULL) {
343 ERR("Couldn't create Semaphore: %d\n", GetLastError());
344 return FALSE;
345 }
346 break;
347 }
348 case DLL_PROCESS_DETACH:
349 CloseHandle(PPRegSemaphore);
350 break;
351 }
352 return TRUE;
353 }