]> gitweb.factorcode.org Git - factor.git/blob - basis/windows/powrprof/powrprof.factor
scryfall: add more filter/reject words, better mtga parser
[factor.git] / basis / windows / powrprof / powrprof.factor
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
5 windows.user32 ;
6 IN: windows.powrprof
7
8 LIBRARY: powrprof
9
10 TYPEDEF: UINT NTSTATUS
11 TYPEDEF: void* PHPOWERNOTIFY
12
13 CONSTANT: STATUS_SUCCESS 0
14 CONSTANT: STATUS_ACCESS_DENIED 0xC0000022
15 CONSTANT: STATUS_BUFFER_TOO_SMALL 0xC0000023
16
17 ENUM: SYSTEM_POWER_STATE
18     PowerSystemUnspecified
19     PowerSystemWorking
20     PowerSystemSleeping1
21     PowerSystemSleeping2
22     PowerSystemSleeping3
23     PowerSystemHibernate
24     PowerSystemShutdown
25     PowerSystemMaximum ;
26 TYPEDEF: SYSTEM_POWER_STATE* PSYSTEM_POWER_STATE
27
28 ENUM: POWER_PLATFORM_ROLE
29     PlatformRoleUnspecified
30     PlatformRoleDesktop
31     PlatformRoleMobile
32     PlatformRoleWorkstation
33     PlatformRoleEnterpriseServer
34     PlatformRoleSOHOServer
35     PlatformRoleAppliancePC
36     PlatformRolePerformanceServer
37     PlatformRoleSlate
38     PlatformRoleMaximum ;
39 TYPEDEF: POWER_PLATFORM_ROLE* PPOWER_PLATFORM_ROLE
40
41 ENUM: POWER_INFORMATION_LEVEL
42     SystemPowerPolicyAc
43     SystemPowerPolicyDc
44     VerifySystemPolicyAc
45     VerifySystemPolicyDc
46     SystemPowerCapabilities
47     SystemBatteryState
48     SystemPowerStateHandler
49     ProcessorStateHandler
50     SystemPowerPolicyCurrent
51     AdministratorPowerPolicy
52     SystemReserveHiberFile
53     ProcessorInformation
54     SystemPowerInformation
55     ProcessorStateHandler2
56     LastWakeTime
57     LastSleepTime
58     SystemExecutionState
59     SystemPowerStateNotifyHandler
60     ProcessorPowerPolicyAc
61     ProcessorPowerPolicyDc
62     VerifyProcessorPowerPolicyAc
63     VerifyProcessorPowerPolicyDc
64     ProcessorPowerPolicyCurrent
65     SystemPowerStateLogging
66     SystemPowerLoggingEntry
67     SetPowerSettingValue
68     NotifyUserPowerSetting
69     PowerInformationLevelUnused0
70     SystemMonitorHiberBootPowerOff
71     SystemVideoState
72     TraceApplicationPowerMessage
73     TraceApplicationPowerMessageEnd
74     ProcessorPerfStates
75     ProcessorIdleStates
76     ProcessorCap
77     SystemWakeSource
78     SystemHiberFileInformation
79     TraceServicePowerMessage
80     ProcessorLoad
81     PowerShutdownNotification
82     MonitorCapabilities
83     SessionPowerInit
84     SessionDisplayState
85     PowerRequestCreate
86     PowerRequestAction
87     GetPowerRequestList
88     ProcessorInformationEx
89     NotifyUserModeLegacyPowerEvent
90     GroupPark
91     ProcessorIdleDomains
92     WakeTimerList
93     SystemHiberFileSize
94     ProcessorIdleStatesHv
95     ProcessorPerfStatesHv
96     ProcessorPerfCapHv
97     ProcessorSetIdle
98     LogicalProcessorIdling
99     UserPresence
100     PowerSettingNotificationName
101     GetPowerSettingValue
102     IdleResiliency
103     SessionRITState
104     SessionConnectNotification
105     SessionPowerCleanup
106     SessionLockState
107     SystemHiberbootState
108     PlatformInformation
109     PdcInvocation
110     MonitorInvocation
111     FirmwareTableInformationRegistered
112     SetShutdownSelectedTime
113     SuspendResumeInvocation
114     PlmPowerRequestCreate
115     ScreenOff
116     CsDeviceNotification
117     PlatformRole
118     LastResumePerformance
119     DisplayBurst
120     ExitLatencySamplingPercentage
121     RegisterSpmPowerSettings
122     PlatformIdleStates
123     ProcessorIdleVeto
124     PlatformIdleVeto
125     SystemBatteryStatePrecise
126     ThermalEvent
127     PowerRequestActionInternal
128     BatteryDeviceState
129     PowerInformationInternal
130     ThermalStandby
131     SystemHiberFileType
132     PhysicalPowerButtonPress
133     QueryPotentialDripsConstraint
134     EnergyTrackerCreate
135     EnergyTrackerQuery
136     UpdateBlackBoxRecorder
137     SessionAllowExternalDmaDevices
138     SendSuspendResumeNotification
139     PowerInformationLevelMaximum ;
140
141 STRUCT: BATTERY_REPORTING_SCALE
142     { Granularity DWORD }
143     { Capacity DWORD } ;
144 TYPEDEF: BATTERY_REPORTING_SCALE* PBATTERY_REPORTING_SCALE
145
146
147 STRUCT: PROCESSOR_POWER_INFORMATION
148     { Number ULONG }
149     { MaxMhz ULONG }
150     { CurrentMhz ULONG }
151     { MhzLimit ULONG }
152     { MaxIdleState ULONG }
153     { CurrentIdleState ULONG } ;
154 TYPEDEF: PROCESSOR_POWER_INFORMATION* PPROCESSOR_POWER_INFORMATION
155
156 STRUCT: SYSTEM_POWER_CAPABILITIES
157     { PowerButtonPresent BOOLEAN }
158     { SleepButtonPresent BOOLEAN }
159     { LidPresent BOOLEAN }
160     { SystemS1 BOOLEAN }
161     { SystemS2 BOOLEAN }
162     { SystemS3 BOOLEAN }
163     { SystemS4 BOOLEAN }
164     { SystemS5 BOOLEAN }
165     { HiberFilePresent BOOLEAN }
166     { FullWake BOOLEAN }
167     { VideoDimPresent BOOLEAN }
168     { ApmPresent BOOLEAN }
169     { UpsPresent BOOLEAN }
170     { ThermalControl BOOLEAN }
171     { ProcessorThrottle BOOLEAN }
172     { ProcessorMinThrottle BYTE }
173     { ProcessorThrottleScale BYTE }
174     { spare2 BYTE[4] }
175     { ProcessorMaxThrottle BYTE }
176     { FastSystemS4 BOOLEAN }
177     { Hiberboot BOOLEAN }
178     { WakeAlarmPresent BOOLEAN }
179     { AoAc BOOLEAN }
180     { DiskSpinDown BOOLEAN }
181     { spare3 BYTE[8] }
182     { HiberFileType BYTE }
183     { AoAcConnectivitySupported BOOLEAN }
184 ! #else
185 !  BYTE                    spare3[6];
186 ! #endif
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
196
197 ENUM: POWER_ACTION
198   PowerActionNone
199   PowerActionReserved
200   PowerActionSleep
201   PowerActionHibernate
202   PowerActionShutdown
203   PowerActionShutdownReset
204   PowerActionShutdownOff
205   PowerActionWarmEject
206   PowerActionDisplayOff ;
207 TYPEDEF: POWER_ACTION* PPOWER_ACTION
208
209 STRUCT: POWER_ACTION_POLICY
210     { Action POWER_ACTION }
211     { Flags DWORD }
212     { EventCode DWORD } ;
213 TYPEDEF: POWER_ACTION_POLICY* PPOWER_ACTION_POLICY
214
215 CONSTANT: DISCHARGE_POLICY_CRITICAL 0
216 CONSTANT: DISCHARGE_POLICY_LOW 1
217 CONSTANT: NUM_DISCHARGE_POLICIES 4
218
219 STRUCT: SYSTEM_POWER_LEVEL
220     { Enable BOOLEAN }
221     { Spare BYTE[3] }
222     { BatteryLevel DWORD }
223     { PowerPolicy POWER_ACTION_POLICY }
224     { MinSystemState SYSTEM_POWER_STATE } ;
225 TYPEDEF: SYSTEM_POWER_LEVEL* PSYSTEM_POWER_LEVEL
226
227 STRUCT: SYSTEM_POWER_POLICY
228     { Revision DWORD }
229     { PowerButton POWER_ACTION_POLICY }
230     { SleepButton POWER_ACTION_POLICY }
231     { LidClose POWER_ACTION_POLICY }
232     { LidOpenWake SYSTEM_POWER_STATE }
233     { Reserved DWORD }
234     { Idle POWER_ACTION_POLICY }
235     { IdleTimeout DWORD }
236     { IdleSensitivity BYTE }
237     { DynamicThrottle BYTE }
238     { Spare2 BYTE[2] }
239     { MinSleep SYSTEM_POWER_STATE }
240     { MaxSleep SYSTEM_POWER_STATE }
241     { ReducedLatencySleep SYSTEM_POWER_STATE }
242     { WinLogonFlags DWORD }
243     { Spare3 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 }
254     { MinThrottle BYTE }
255     { OverThrottled POWER_ACTION_POLICY } ;
256 TYPEDEF: SYSTEM_POWER_POLICY* PSYSTEM_POWER_POLICY
257
258
259 STRUCT: SYSTEM_BATTERY_STATE
260     { AcOnLine BOOLEAN }
261     { BatteryPresent BOOLEAN }
262     { Charging BOOLEAN }
263     { Discharging BOOLEAN }
264     { Spare1 BOOLEAN[3] }
265     { Tag BYTE }
266     { MaxCapacity DWORD }
267     { RemainingCapacity DWORD }
268     { Rate DWORD }
269     { EstimatedTime DWORD }
270     { DefaultAlert1 DWORD }
271     { DefaultAlert2 DWORD } ;
272 TYPEDEF: SYSTEM_BATTERY_STATE* PSYSTEM_BATTERY_STATE
273
274 STRUCT: SYSTEM_POWER_INFORMATION
275     { MaxIdlenessAllowed ULONG }
276     { Idleness ULONG }
277     { TimeRemaining ULONG }
278     { CoolingMode UCHAR } ;
279 TYPEDEF: SYSTEM_POWER_INFORMATION* PSYSTEM_POWER_INFORMATION 
280
281
282 SPECIALIZED-ARRAY: PROCESSOR_POWER_INFORMATION
283
284 FUNCTION: NTSTATUS CallNtPowerInformation (
285     POWER_INFORMATION_LEVEL InformationLevel
286     PVOID                   InputBuffer,
287     ULONG                   InputBufferLength,
288     PVOID                   OutputBuffer,
289     ULONG                   OutputBufferLength
290 )
291
292 FUNCTION: BOOLEAN GetPwrCapabilities (
293     PSYSTEM_POWER_CAPABILITIES lpspc
294 )
295
296 FUNCTION: POWER_PLATFORM_ROLE PowerDeterminePlatformRoleEx (
297     ULONG Version
298 )
299 FUNCTION: DWORD PowerRegisterSuspendResumeNotification (
300     DWORD         Flags,
301     HANDLE        Recipient,
302     PHPOWERNOTIFY RegistrationHandle
303 )
304 FUNCTION: DWORD PowerUnregisterSuspendResumeNotification (
305     HPOWERNOTIFY RegistrationHandle
306 )
307
308 ERROR: win32-powrprof-error n ;
309 : win32-power-error ( n -- )
310     dup 0 = [ drop ] [ win32-powrprof-error ] if ;
311
312 : get-power-capabilities ( -- struct )
313     SYSTEM_POWER_CAPABILITIES new
314     [ GetPwrCapabilities win32-error=0/f ] keep ;
315
316 : get-processor-power-information ( -- structs )
317     ProcessorInformation
318     f 0
319     cpus <PROCESSOR_POWER_INFORMATION-array>
320     PROCESSOR_POWER_INFORMATION heap-size cpus *
321     [ CallNtPowerInformation win32-power-error ] keepd ;
322
323 : simple-call-nt-power-information ( enum class -- struct )
324     [ f 0 ] dip
325     [ <struct> ] [ heap-size ] bi
326     [ CallNtPowerInformation win32-power-error ] keepd ;
327
328 : c-type-call-nt-power-information ( enum c-type -- struct )
329     [
330         [ f 0 ] dip
331         [ 0 swap <ref> ] [ heap-size ] bi
332         [ CallNtPowerInformation win32-power-error ] keepd
333     ] keep deref ; inline
334
335 : get-last-sleep-time ( -- nanoseconds )
336     LastSleepTime ULONGLONG c-type-call-nt-power-information 100 * ;
337
338 : get-last-wake-time ( -- nanoseconds )
339     LastWakeTime ULONGLONG c-type-call-nt-power-information 100 * ;
340
341 : get-system-execuction-state ( -- enum )
342     SystemExecutionState ULONG c-type-call-nt-power-information ;
343
344 : get-system-power-capabilities ( -- struct )
345     SystemPowerCapabilities SYSTEM_POWER_CAPABILITIES simple-call-nt-power-information ;
346
347 : get-system-battery-state ( -- struct )
348     SystemBatteryState SYSTEM_BATTERY_STATE simple-call-nt-power-information ;
349
350 : get-system-power-policy-ac ( -- struct )
351     SystemPowerPolicyAc SYSTEM_POWER_POLICY simple-call-nt-power-information ;
352
353 : get-system-power-policy-current ( -- struct )
354     SystemPowerPolicyCurrent SYSTEM_POWER_POLICY simple-call-nt-power-information ;
355
356 : get-system-power-policy-dc ( -- struct )
357     SystemPowerPolicyDc SYSTEM_POWER_POLICY simple-call-nt-power-information ;
358