2 * Copyright (C) 2005 Benjamin Cutler
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define WIN32_NO_STATUS
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(powrprof);
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
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. */
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;
64 NTSTATUS WINAPI CallNtPowerInformation(
65 POWER_INFORMATION_LEVEL InformationLevel,
66 PVOID lpInputBuffer, ULONG nInputBufferSize,
67 PVOID lpOutputBuffer, ULONG nOutputBufferSize)
69 return NtPowerInformation(InformationLevel, lpInputBuffer,
70 nInputBufferSize, lpOutputBuffer, nOutputBufferSize);
73 BOOLEAN WINAPI CanUserWritePwrScheme(VOID)
77 BOOLEAN bSuccess = TRUE;
81 r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, szPowerCfgSubKey, 0, KEY_READ | KEY_WRITE, &hKey);
83 if (r != ERROR_SUCCESS) {
84 TRACE("RegOpenKeyEx failed: %ld\n", r);
93 BOOLEAN WINAPI DeletePwrScheme(UINT uiIndex)
95 /* FIXME: See note #1 */
96 FIXME("(%d) stub!\n", uiIndex);
97 SetLastError(ERROR_FILE_NOT_FOUND);
101 BOOLEAN WINAPI EnumPwrSchemes(PWRSCHEMESENUMPROC lpfnPwrSchemesEnumProc,
104 /* FIXME: See note #1 */
105 FIXME("(%p, %ld) stub!\n", lpfnPwrSchemesEnumProc, lParam);
106 SetLastError(ERROR_FILE_NOT_FOUND);
110 BOOLEAN WINAPI GetActivePwrScheme(PUINT puiID)
112 /* FIXME: See note #1 */
113 FIXME("(%p) stub!\n", puiID);
114 SetLastError(ERROR_FILE_NOT_FOUND);
118 BOOLEAN WINAPI GetCurrentPowerPolicies(
119 PGLOBAL_POWER_POLICY pGlobalPowerPolicy,
120 PPOWER_POLICY pPowerPolicy)
122 /* FIXME: See note #2 */
123 SYSTEM_POWER_POLICY ACPower, DCPower;
125 FIXME("(%p, %p) stub!\n", pGlobalPowerPolicy, pPowerPolicy);
127 NtPowerInformation(SystemPowerPolicyAc, 0, 0, &ACPower, sizeof(SYSTEM_POWER_POLICY));
128 NtPowerInformation(SystemPowerPolicyDc, 0, 0, &DCPower, sizeof(SYSTEM_POWER_POLICY));
133 BOOLEAN WINAPI GetPwrCapabilities(
134 PSYSTEM_POWER_CAPABILITIES lpSystemPowerCapabilities)
138 TRACE("(%p)\n", lpSystemPowerCapabilities);
140 r = NtPowerInformation(SystemPowerCapabilities, 0, 0, lpSystemPowerCapabilities, sizeof(SYSTEM_POWER_CAPABILITIES));
142 SetLastError(RtlNtStatusToDosError(r));
144 return r == STATUS_SUCCESS;
147 BOOLEAN WINAPI GetPwrDiskSpindownRange(PUINT RangeMax, PUINT RangeMin)
152 DWORD cbValue = sizeof(lpValue);
154 TRACE("(%p, %p)\n", RangeMax, RangeMin);
156 if (RangeMax == NULL || RangeMin == NULL) {
157 SetLastError(ERROR_INVALID_PARAMETER);
161 SetLastError(ERROR_SUCCESS);
163 WaitForSingleObject(PPRegSemaphore, INFINITE);
165 r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, szPowerCfgSubKey, 0, KEY_READ, &hKey);
166 if (r != ERROR_SUCCESS) {
167 TRACE("RegOpenKeyEx failed: %ld\n", r);
168 TRACE("Using defaults: 3600, 3\n");
171 ReleaseSemaphore(PPRegSemaphore, 1, NULL);
175 r = RegQueryValueExW(hKey, szDiskMax, 0, 0, lpValue, &cbValue);
176 if (r != ERROR_SUCCESS) {
177 TRACE("Couldn't open DiskSpinDownMax: %ld\n", r);
178 TRACE("Using default: 3600\n");
181 *RangeMax = atoiW((LPCWSTR)lpValue);
184 cbValue = sizeof(lpValue);
186 r = RegQueryValueExW(hKey, szDiskMin, 0, 0, lpValue, &cbValue);
187 if (r != ERROR_SUCCESS) {
188 TRACE("Couldn't open DiskSpinDownMin: %ld\n", r);
189 TRACE("Using default: 3\n");
192 *RangeMin = atoiW((LPCWSTR)lpValue);
197 ReleaseSemaphore(PPRegSemaphore, 1, NULL);
202 BOOLEAN WINAPI IsAdminOverrideActive(PADMINISTRATOR_POWER_POLICY p)
204 FIXME("( %p) stub!\n", p);
208 BOOLEAN WINAPI IsPwrHibernateAllowed(VOID)
210 /* FIXME: See note #2 */
211 SYSTEM_POWER_CAPABILITIES PowerCaps;
213 NtPowerInformation(SystemPowerCapabilities, NULL, 0, &PowerCaps, sizeof(PowerCaps));
217 BOOLEAN WINAPI IsPwrShutdownAllowed(VOID)
219 /* FIXME: See note #2 */
220 SYSTEM_POWER_CAPABILITIES PowerCaps;
222 NtPowerInformation(SystemPowerCapabilities, NULL, 0, &PowerCaps, sizeof(PowerCaps));
226 BOOLEAN WINAPI IsPwrSuspendAllowed(VOID)
228 /* FIXME: See note #2 */
229 SYSTEM_POWER_CAPABILITIES PowerCaps;
231 NtPowerInformation(SystemPowerCapabilities, NULL, 0, &PowerCaps, sizeof(PowerCaps));
235 BOOLEAN WINAPI ReadGlobalPwrPolicy(PGLOBAL_POWER_POLICY pGlobalPowerPolicy)
237 /* FIXME: See note #1 */
238 FIXME("(%p) stub!\n", pGlobalPowerPolicy);
239 SetLastError(ERROR_FILE_NOT_FOUND);
243 BOOLEAN WINAPI ReadProcessorPwrScheme(UINT uiID,
244 PMACHINE_PROCESSOR_POWER_POLICY pMachineProcessorPowerPolicy)
246 /* FIXME: See note #1 */
247 FIXME("(%d, %p) stub!\n", uiID, pMachineProcessorPowerPolicy);
248 SetLastError(ERROR_FILE_NOT_FOUND);
252 BOOLEAN WINAPI ReadPwrScheme(UINT uiID,
253 PPOWER_POLICY pPowerPolicy)
255 /* FIXME: See note #1 */
256 FIXME("(%d, %p) stub!\n", uiID, pPowerPolicy);
257 SetLastError(ERROR_FILE_NOT_FOUND);
261 BOOLEAN WINAPI SetActivePwrScheme(UINT uiID,
262 PGLOBAL_POWER_POLICY lpGlobalPowerPolicy,
263 PPOWER_POLICY lpPowerPolicy)
265 /* FIXME: See note #1 */
266 FIXME("(%d, %p, %p) stub!\n", uiID, lpGlobalPowerPolicy, lpPowerPolicy);
267 SetLastError(ERROR_FILE_NOT_FOUND);
271 BOOLEAN WINAPI SetSuspendState(BOOLEAN Hibernate, BOOLEAN ForceCritical,
272 BOOLEAN DisableWakeEvent)
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);
280 BOOLEAN WINAPI WriteGlobalPwrPolicy(PGLOBAL_POWER_POLICY pGlobalPowerPolicy)
282 /* FIXME: See note #3 */
283 FIXME("(%p) stub!\n", pGlobalPowerPolicy);
284 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
288 BOOLEAN WINAPI WriteProcessorPwrScheme(UINT ID,
289 PMACHINE_PROCESSOR_POWER_POLICY pMachineProcessorPowerPolicy)
291 /* FIXME: See note #3 */
292 FIXME("(%d, %p) stub!\n", ID, pMachineProcessorPowerPolicy);
293 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
297 BOOLEAN WINAPI WritePwrScheme(PUINT puiID, LPWSTR lpszName, LPWSTR lpszDescription,
298 PPOWER_POLICY pPowerPolicy)
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);
306 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
308 FIXME("(%p, %ld, %p) not fully implemented\n", hinstDLL, fdwReason, lpvReserved);
311 case DLL_PROCESS_ATTACH: {
316 DisableThreadLibraryCalls(hinstDLL);
318 r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, szPowerCfgSubKey, 0, KEY_READ | KEY_WRITE, &hKey);
320 if (r != ERROR_SUCCESS) {
321 TRACE("Couldn't open registry key HKLM\\%s, using some sane(?) defaults\n", debugstr_w(szPowerCfgSubKey));
324 DWORD cbValue = sizeof(lpValue);
325 r = RegQueryValueExW(hKey, szLastID, 0, 0, lpValue, &cbValue);
326 if (r != ERROR_SUCCESS) {
327 TRACE("Couldn't open registry entry HKLM\\%s\\LastID, using some sane(?) defaults\n", debugstr_w(szPowerCfgSubKey));
332 PPRegSemaphore = CreateSemaphoreW(NULL, 1, 1, szSemaphoreName);
333 if (PPRegSemaphore == NULL) {
334 ERR("Couldn't create Semaphore: %ld\n", GetLastError());
339 case DLL_PROCESS_DETACH:
340 CloseHandle(PPRegSemaphore);