1 ! Copyright (C) 2021 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types alien.data alien.syntax classes.struct kernel math
4 specialized-arrays system-info windows.errors windows.types
10 TYPEDEF: UINT NTSTATUS
11 TYPEDEF: void* PHPOWERNOTIFY
13 CONSTANT: STATUS_SUCCESS 0
14 CONSTANT: STATUS_ACCESS_DENIED 0xC0000022
15 CONSTANT: STATUS_BUFFER_TOO_SMALL 0xC0000023
17 ENUM: SYSTEM_POWER_STATE
18 PowerSystemUnspecified
26 TYPEDEF: SYSTEM_POWER_STATE* PSYSTEM_POWER_STATE
28 ENUM: POWER_PLATFORM_ROLE
29 PlatformRoleUnspecified
32 PlatformRoleWorkstation
33 PlatformRoleEnterpriseServer
34 PlatformRoleSOHOServer
35 PlatformRoleAppliancePC
36 PlatformRolePerformanceServer
39 TYPEDEF: POWER_PLATFORM_ROLE* PPOWER_PLATFORM_ROLE
41 ENUM: POWER_INFORMATION_LEVEL
46 SystemPowerCapabilities
48 SystemPowerStateHandler
50 SystemPowerPolicyCurrent
51 AdministratorPowerPolicy
52 SystemReserveHiberFile
54 SystemPowerInformation
55 ProcessorStateHandler2
59 SystemPowerStateNotifyHandler
60 ProcessorPowerPolicyAc
61 ProcessorPowerPolicyDc
62 VerifyProcessorPowerPolicyAc
63 VerifyProcessorPowerPolicyDc
64 ProcessorPowerPolicyCurrent
65 SystemPowerStateLogging
66 SystemPowerLoggingEntry
68 NotifyUserPowerSetting
69 PowerInformationLevelUnused0
70 SystemMonitorHiberBootPowerOff
72 TraceApplicationPowerMessage
73 TraceApplicationPowerMessageEnd
78 SystemHiberFileInformation
79 TraceServicePowerMessage
81 PowerShutdownNotification
88 ProcessorInformationEx
89 NotifyUserModeLegacyPowerEvent
98 LogicalProcessorIdling
100 PowerSettingNotificationName
104 SessionConnectNotification
111 FirmwareTableInformationRegistered
112 SetShutdownSelectedTime
113 SuspendResumeInvocation
114 PlmPowerRequestCreate
118 LastResumePerformance
120 ExitLatencySamplingPercentage
121 RegisterSpmPowerSettings
125 SystemBatteryStatePrecise
127 PowerRequestActionInternal
129 PowerInformationInternal
132 PhysicalPowerButtonPress
133 QueryPotentialDripsConstraint
136 UpdateBlackBoxRecorder
137 SessionAllowExternalDmaDevices
138 SendSuspendResumeNotification
139 PowerInformationLevelMaximum ;
141 STRUCT: BATTERY_REPORTING_SCALE
142 { Granularity DWORD }
144 TYPEDEF: BATTERY_REPORTING_SCALE* PBATTERY_REPORTING_SCALE
147 STRUCT: PROCESSOR_POWER_INFORMATION
152 { MaxIdleState ULONG }
153 { CurrentIdleState ULONG } ;
154 TYPEDEF: PROCESSOR_POWER_INFORMATION* PPROCESSOR_POWER_INFORMATION
156 STRUCT: SYSTEM_POWER_CAPABILITIES
157 { PowerButtonPresent BOOLEAN }
158 { SleepButtonPresent BOOLEAN }
159 { LidPresent BOOLEAN }
165 { HiberFilePresent BOOLEAN }
167 { VideoDimPresent BOOLEAN }
168 { ApmPresent BOOLEAN }
169 { UpsPresent BOOLEAN }
170 { ThermalControl BOOLEAN }
171 { ProcessorThrottle BOOLEAN }
172 { ProcessorMinThrottle BYTE }
173 { ProcessorThrottleScale BYTE }
175 { ProcessorMaxThrottle BYTE }
176 { FastSystemS4 BOOLEAN }
177 { Hiberboot BOOLEAN }
178 { WakeAlarmPresent BOOLEAN }
180 { DiskSpinDown BOOLEAN }
182 { HiberFileType BYTE }
183 { AoAcConnectivitySupported BOOLEAN }
187 { SystemBatteriesPresent BOOLEAN }
188 { BatteriesAreShortTerm BOOLEAN }
189 { BatteryScale BATTERY_REPORTING_SCALE[3] }
190 { AcOnLineWake SYSTEM_POWER_STATE }
191 { SoftLidWake SYSTEM_POWER_STATE }
192 { RtcWake SYSTEM_POWER_STATE }
193 { MinDeviceWakeState SYSTEM_POWER_STATE }
194 { DefaultLowLatencyWake SYSTEM_POWER_STATE } ;
195 TYPEDEF: SYSTEM_POWER_CAPABILITIES* PSYSTEM_POWER_CAPABILITIES
203 PowerActionShutdownReset
204 PowerActionShutdownOff
206 PowerActionDisplayOff ;
207 TYPEDEF: POWER_ACTION* PPOWER_ACTION
209 STRUCT: POWER_ACTION_POLICY
210 { Action POWER_ACTION }
212 { EventCode DWORD } ;
213 TYPEDEF: POWER_ACTION_POLICY* PPOWER_ACTION_POLICY
215 CONSTANT: DISCHARGE_POLICY_CRITICAL 0
216 CONSTANT: DISCHARGE_POLICY_LOW 1
217 CONSTANT: NUM_DISCHARGE_POLICIES 4
219 STRUCT: SYSTEM_POWER_LEVEL
222 { BatteryLevel DWORD }
223 { PowerPolicy POWER_ACTION_POLICY }
224 { MinSystemState SYSTEM_POWER_STATE } ;
225 TYPEDEF: SYSTEM_POWER_LEVEL* PSYSTEM_POWER_LEVEL
227 STRUCT: SYSTEM_POWER_POLICY
229 { PowerButton POWER_ACTION_POLICY }
230 { SleepButton POWER_ACTION_POLICY }
231 { LidClose POWER_ACTION_POLICY }
232 { LidOpenWake SYSTEM_POWER_STATE }
234 { Idle POWER_ACTION_POLICY }
235 { IdleTimeout DWORD }
236 { IdleSensitivity BYTE }
237 { DynamicThrottle BYTE }
239 { MinSleep SYSTEM_POWER_STATE }
240 { MaxSleep SYSTEM_POWER_STATE }
241 { ReducedLatencySleep SYSTEM_POWER_STATE }
242 { WinLogonFlags DWORD }
244 { DozeS4Timeout DWORD }
245 { BroadcastCapacityResolution DWORD }
246 { DischargePolicy SYSTEM_POWER_LEVEL[NUM_DISCHARGE_POLICIES] }
247 { VideoTimeout DWORD }
248 { VideoDimDisplay BOOLEAN }
249 { VideoReserved DWORD[3] }
250 { SpindownTimeout DWORD }
251 { OptimizeForPower BOOLEAN }
252 { FanThrottleTolerance BYTE }
253 { ForcedThrottle BYTE }
255 { OverThrottled POWER_ACTION_POLICY } ;
256 TYPEDEF: SYSTEM_POWER_POLICY* PSYSTEM_POWER_POLICY
259 STRUCT: SYSTEM_BATTERY_STATE
261 { BatteryPresent BOOLEAN }
263 { Discharging BOOLEAN }
264 { Spare1 BOOLEAN[3] }
266 { MaxCapacity DWORD }
267 { RemainingCapacity DWORD }
269 { EstimatedTime DWORD }
270 { DefaultAlert1 DWORD }
271 { DefaultAlert2 DWORD } ;
272 TYPEDEF: SYSTEM_BATTERY_STATE* PSYSTEM_BATTERY_STATE
274 STRUCT: SYSTEM_POWER_INFORMATION
275 { MaxIdlenessAllowed ULONG }
277 { TimeRemaining ULONG }
278 { CoolingMode UCHAR } ;
279 TYPEDEF: SYSTEM_POWER_INFORMATION* PSYSTEM_POWER_INFORMATION
282 SPECIALIZED-ARRAY: PROCESSOR_POWER_INFORMATION
284 FUNCTION: NTSTATUS CallNtPowerInformation (
285 POWER_INFORMATION_LEVEL InformationLevel
287 ULONG InputBufferLength,
289 ULONG OutputBufferLength
292 FUNCTION: BOOLEAN GetPwrCapabilities (
293 PSYSTEM_POWER_CAPABILITIES lpspc
296 FUNCTION: POWER_PLATFORM_ROLE PowerDeterminePlatformRoleEx (
299 FUNCTION: DWORD PowerRegisterSuspendResumeNotification (
302 PHPOWERNOTIFY RegistrationHandle
304 FUNCTION: DWORD PowerUnregisterSuspendResumeNotification (
305 HPOWERNOTIFY RegistrationHandle
308 ERROR: win32-powrprof-error n ;
309 : win32-power-error ( n -- )
310 dup 0 = [ drop ] [ win32-powrprof-error ] if ;
312 : get-power-capabilities ( -- struct )
313 SYSTEM_POWER_CAPABILITIES new
314 [ GetPwrCapabilities win32-error=0/f ] keep ;
316 : get-processor-power-information ( -- structs )
319 cpus <PROCESSOR_POWER_INFORMATION-array>
320 PROCESSOR_POWER_INFORMATION heap-size cpus *
321 [ CallNtPowerInformation win32-power-error ] keepd ;
323 : simple-call-nt-power-information ( enum class -- struct )
325 [ <struct> ] [ heap-size ] bi
326 [ CallNtPowerInformation win32-power-error ] keepd ;
328 : c-type-call-nt-power-information ( enum c-type -- struct )
331 [ 0 swap <ref> ] [ heap-size ] bi
332 [ CallNtPowerInformation win32-power-error ] keepd
333 ] keep deref ; inline
335 : get-last-sleep-time ( -- nanoseconds )
336 LastSleepTime ULONGLONG c-type-call-nt-power-information 100 * ;
338 : get-last-wake-time ( -- nanoseconds )
339 LastWakeTime ULONGLONG c-type-call-nt-power-information 100 * ;
341 : get-system-execuction-state ( -- enum )
342 SystemExecutionState ULONG c-type-call-nt-power-information ;
344 : get-system-power-capabilities ( -- struct )
345 SystemPowerCapabilities SYSTEM_POWER_CAPABILITIES simple-call-nt-power-information ;
347 : get-system-battery-state ( -- struct )
348 SystemBatteryState SYSTEM_BATTERY_STATE simple-call-nt-power-information ;
350 : get-system-power-policy-ac ( -- struct )
351 SystemPowerPolicyAc SYSTEM_POWER_POLICY simple-call-nt-power-information ;
353 : get-system-power-policy-current ( -- struct )
354 SystemPowerPolicyCurrent SYSTEM_POWER_POLICY simple-call-nt-power-information ;
356 : get-system-power-policy-dc ( -- struct )
357 SystemPowerPolicyDc SYSTEM_POWER_POLICY simple-call-nt-power-information ;