mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / kernel32 / powermgnt.c
blob8c55686c552c200a705ce99328e48abd9fd6022c
1 /*
2 * Copyright 1995 Thomas Sandford (tdgsandf@prds-grn.demon.co.uk)
3 * Copyright 2003 Dimitrie O. Paun
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
29 #include "kernel_private.h"
30 #include "wine/debug.h"
31 #include "wine/heap.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(powermgnt);
35 /******************************************************************************
36 * GetDevicePowerState (KERNEL32.@)
38 BOOL WINAPI GetDevicePowerState(HANDLE hDevice, BOOL* pfOn)
40 WARN("(hDevice %p pfOn %p): stub\n", hDevice, pfOn);
41 return TRUE; /* no information */
44 /***********************************************************************
45 * GetSystemPowerStatus (KERNEL32.@)
47 BOOL WINAPI GetSystemPowerStatus(LPSYSTEM_POWER_STATUS ps)
49 SYSTEM_BATTERY_STATE bs;
50 NTSTATUS status;
52 TRACE("(%p)\n", ps);
54 ps->ACLineStatus = AC_LINE_UNKNOWN;
55 ps->BatteryFlag = BATTERY_FLAG_UNKNOWN;
56 ps->BatteryLifePercent = BATTERY_PERCENTAGE_UNKNOWN;
57 ps->SystemStatusFlag = 0;
58 ps->BatteryLifeTime = BATTERY_LIFE_UNKNOWN;
59 ps->BatteryFullLifeTime = BATTERY_LIFE_UNKNOWN;
61 status = NtPowerInformation(SystemBatteryState, NULL, 0, &bs, sizeof(bs));
62 if (status == STATUS_NOT_IMPLEMENTED) return TRUE;
63 if (FAILED(status)) return FALSE;
65 ps->ACLineStatus = bs.AcOnLine;
67 if (bs.BatteryPresent)
69 ps->BatteryLifePercent = bs.MaxCapacity ? bs.RemainingCapacity / bs.MaxCapacity : 100;
70 ps->BatteryLifeTime = bs.EstimatedTime;
71 if (!bs.Charging && (LONG)bs.Rate < 0)
72 ps->BatteryFullLifeTime = 3600 * bs.MaxCapacity / -(LONG)bs.Rate;
74 ps->BatteryFlag = 0;
75 if (bs.Charging)
76 ps->BatteryFlag |= BATTERY_FLAG_CHARGING;
77 if (ps->BatteryLifePercent > 66)
78 ps->BatteryFlag |= BATTERY_FLAG_HIGH;
79 if (ps->BatteryLifePercent < 33)
80 ps->BatteryFlag |= BATTERY_FLAG_LOW;
81 if (ps->BatteryLifePercent < 5)
82 ps->BatteryFlag |= BATTERY_FLAG_CRITICAL;
84 else
86 ps->BatteryFlag = BATTERY_FLAG_NO_BATTERY;
89 return TRUE;
92 /***********************************************************************
93 * IsSystemResumeAutomatic (KERNEL32.@)
95 BOOL WINAPI IsSystemResumeAutomatic(void)
97 WARN("(): stub, harmless.\n");
98 return FALSE;
101 /***********************************************************************
102 * RequestWakeupLatency (KERNEL32.@)
104 BOOL WINAPI RequestWakeupLatency(LATENCY_TIME latency)
106 WARN("(): stub, harmless.\n");
107 return TRUE;
110 /***********************************************************************
111 * RequestDeviceWakeup (KERNEL32.@)
113 BOOL WINAPI RequestDeviceWakeup(HANDLE device)
115 FIXME("(%p): stub\n", device);
116 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
117 return FALSE;
120 /***********************************************************************
121 * SetSystemPowerState (KERNEL32.@)
123 BOOL WINAPI SetSystemPowerState(BOOL suspend_or_hibernate,
124 BOOL force_flag)
126 WARN("(): stub, harmless.\n");
127 return TRUE;
130 /***********************************************************************
131 * SetThreadExecutionState (KERNEL32.@)
133 * Informs the system that activity is taking place for
134 * power management purposes.
136 EXECUTION_STATE WINAPI SetThreadExecutionState(EXECUTION_STATE flags)
138 EXECUTION_STATE old;
140 NtSetThreadExecutionState(flags, &old);
142 return old;
145 /***********************************************************************
146 * PowerCreateRequest (KERNEL32.@)
148 HANDLE WINAPI PowerCreateRequest(REASON_CONTEXT *context)
150 COUNTED_REASON_CONTEXT nt_context;
151 HANDLE handle;
152 NTSTATUS status;
153 WCHAR module_name[MAX_PATH];
155 TRACE( "(%p)\n", context );
157 nt_context.Version = context->Version;
158 nt_context.Flags = context->Flags;
159 if (context->Flags & POWER_REQUEST_CONTEXT_SIMPLE_STRING)
160 RtlInitUnicodeString( &nt_context.u.SimpleString, context->Reason.SimpleReasonString );
161 else if (context->Flags & POWER_REQUEST_CONTEXT_DETAILED_STRING)
163 int i;
165 GetModuleFileNameW( context->Reason.Detailed.LocalizedReasonModule, module_name, ARRAY_SIZE(module_name) );
166 RtlInitUnicodeString( &nt_context.u.s.ResourceFileName, module_name );
167 nt_context.u.s.ResourceReasonId = context->Reason.Detailed.LocalizedReasonId;
168 nt_context.u.s.StringCount = context->Reason.Detailed.ReasonStringCount;
169 nt_context.u.s.ReasonStrings = heap_alloc( nt_context.u.s.StringCount * sizeof(UNICODE_STRING) );
170 for (i = 0; i < nt_context.u.s.StringCount; i++)
171 RtlInitUnicodeString( &nt_context.u.s.ReasonStrings[i], context->Reason.Detailed.ReasonStrings[i] );
174 status = NtCreatePowerRequest( &handle, &nt_context );
175 if (nt_context.Flags & POWER_REQUEST_CONTEXT_DETAILED_STRING)
176 heap_free( nt_context.u.s.ReasonStrings );
177 if (!set_ntstatus( status )) return INVALID_HANDLE_VALUE;
178 return handle;
181 /***********************************************************************
182 * PowerSetRequest (KERNEL32.@)
184 BOOL WINAPI PowerSetRequest(HANDLE request, POWER_REQUEST_TYPE type)
186 return set_ntstatus( NtSetPowerRequest( request, type ));
189 /***********************************************************************
190 * PowerClearRequest (KERNEL32.@)
192 BOOL WINAPI PowerClearRequest(HANDLE request, POWER_REQUEST_TYPE type)
194 return set_ntstatus( NtClearPowerRequest( request, type ));