From: Doug Coleman Date: Sun, 31 Jan 2021 17:13:27 +0000 (-0600) Subject: windows.powrprof: Add more functions to get power settings. X-Git-Tag: 0.99~2630 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=7856fb6e1ec3777c66bd00fbf97cdaeb68ba5e43 windows.powrprof: Add more functions to get power settings. --- diff --git a/basis/windows/powrprof/powrprof.factor b/basis/windows/powrprof/powrprof.factor index 9460fd3502..a94b4ffdf1 100644 --- a/basis/windows/powrprof/powrprof.factor +++ b/basis/windows/powrprof/powrprof.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2021 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.c-types alien.syntax classes.struct kernel math +USING: alien.c-types alien.data alien.syntax classes.struct kernel math specialized-arrays system-info windows.errors windows.types windows.user32 ; IN: windows.powrprof @@ -11,6 +11,8 @@ TYPEDEF: UINT NTSTATUS TYPEDEF: void* PHPOWERNOTIFY CONSTANT: STATUS_SUCCESS 0 +CONSTANT: STATUS_ACCESS_DENIED 0xC0000022 +CONSTANT: STATUS_BUFFER_TOO_SMALL 0xC0000023 ENUM: SYSTEM_POWER_STATE PowerSystemUnspecified @@ -192,6 +194,68 @@ STRUCT: SYSTEM_POWER_CAPABILITIES { DefaultLowLatencyWake SYSTEM_POWER_STATE } ; TYPEDEF: SYSTEM_POWER_CAPABILITIES* PSYSTEM_POWER_CAPABILITIES +ENUM: POWER_ACTION + PowerActionNone + PowerActionReserved + PowerActionSleep + PowerActionHibernate + PowerActionShutdown + PowerActionShutdownReset + PowerActionShutdownOff + PowerActionWarmEject + PowerActionDisplayOff ; +TYPEDEF: POWER_ACTION* PPOWER_ACTION + +STRUCT: POWER_ACTION_POLICY + { Action POWER_ACTION } + { Flags DWORD } + { EventCode DWORD } ; +TYPEDEF: POWER_ACTION_POLICY* PPOWER_ACTION_POLICY + +CONSTANT: DISCHARGE_POLICY_CRITICAL 0 +CONSTANT: DISCHARGE_POLICY_LOW 1 +CONSTANT: NUM_DISCHARGE_POLICIES 4 + +STRUCT: SYSTEM_POWER_LEVEL + { Enable BOOLEAN } + { Spare BYTE[3] } + { BatteryLevel DWORD } + { PowerPolicy POWER_ACTION_POLICY } + { MinSystemState SYSTEM_POWER_STATE } ; +TYPEDEF: SYSTEM_POWER_LEVEL* PSYSTEM_POWER_LEVEL + +STRUCT: SYSTEM_POWER_POLICY + { Revision DWORD } + { PowerButton POWER_ACTION_POLICY } + { SleepButton POWER_ACTION_POLICY } + { LidClose POWER_ACTION_POLICY } + { LidOpenWake SYSTEM_POWER_STATE } + { Reserved DWORD } + { Idle POWER_ACTION_POLICY } + { IdleTimeout DWORD } + { IdleSensitivity BYTE } + { DynamicThrottle BYTE } + { Spare2 BYTE[2] } + { MinSleep SYSTEM_POWER_STATE } + { MaxSleep SYSTEM_POWER_STATE } + { ReducedLatencySleep SYSTEM_POWER_STATE } + { WinLogonFlags DWORD } + { Spare3 DWORD } + { DozeS4Timeout DWORD } + { BroadcastCapacityResolution DWORD } + { DischargePolicy SYSTEM_POWER_LEVEL[NUM_DISCHARGE_POLICIES] } + { VideoTimeout DWORD } + { VideoDimDisplay BOOLEAN } + { VideoReserved DWORD[3] } + { SpindownTimeout DWORD } + { OptimizeForPower BOOLEAN } + { FanThrottleTolerance BYTE } + { ForcedThrottle BYTE } + { MinThrottle BYTE } + { OverThrottled POWER_ACTION_POLICY } ; +TYPEDEF: SYSTEM_POWER_POLICY* PSYSTEM_POWER_POLICY + + STRUCT: SYSTEM_BATTERY_STATE { AcOnLine BOOLEAN } { BatteryPresent BOOLEAN } @@ -241,15 +305,54 @@ FUNCTION: DWORD PowerUnregisterSuspendResumeNotification ( HPOWERNOTIFY RegistrationHandle ) +ERROR: win32-powrprof-error n ; +: win32-power-error ( n -- ) + dup 0 = [ drop ] [ win32-powrprof-error ] if ; + +: get-power-capabilities ( -- struct ) + SYSTEM_POWER_CAPABILITIES + [ GetPwrCapabilities win32-error=0/f ] keep ; + : get-processor-power-information ( -- structs ) ProcessorInformation f 0 cpus PROCESSOR_POWER_INFORMATION heap-size cpus * + [ CallNtPowerInformation win32-power-error ] keepd ; + +: simple-call-nt-power-information ( enum class -- struct ) + [ f 0 ] dip + [ ] [ heap-size ] bi + [ CallNtPowerInformation win32-power-error ] keepd ; + +: c-type-call-nt-power-information ( enum c-type -- struct ) [ - CallNtPowerInformation win32-error=0/f - ] keepd ; + [ f 0 ] dip + [ 0 swap ] [ heap-size ] bi + [ CallNtPowerInformation win32-power-error ] keepd + ] keep deref ; inline + +: get-last-sleep-time ( -- nanoseconds ) + LastSleepTime ULONGLONG c-type-call-nt-power-information 100 * ; + +: get-last-wake-time ( -- nanoseconds ) + LastWakeTime ULONGLONG c-type-call-nt-power-information 100 * ; + +: get-system-execuction-state ( -- enum ) + SystemExecutionState ULONG c-type-call-nt-power-information ; + +: get-system-power-capabilities ( -- struct ) + SystemPowerCapabilities SYSTEM_POWER_CAPABILITIES simple-call-nt-power-information ; + +: get-system-battery-state ( -- struct ) + SystemBatteryState SYSTEM_BATTERY_STATE simple-call-nt-power-information ; + +: get-system-power-policy-ac ( -- struct ) + SystemPowerPolicyAc SYSTEM_POWER_POLICY simple-call-nt-power-information ; + +: get-system-power-policy-current ( -- struct ) + SystemPowerPolicyCurrent SYSTEM_POWER_POLICY simple-call-nt-power-information ; + +: get-system-power-policy-dc ( -- struct ) + SystemPowerPolicyDc SYSTEM_POWER_POLICY simple-call-nt-power-information ; -: get-power-capabilities ( -- struct ) - SYSTEM_POWER_CAPABILITIES - [ GetPwrCapabilities win32-error=0/f ] keep ;