UWSCで現在の電源管理情報を取得する
以前、「画面見ながら考えてるとスクリーンセーバーがうざい」と書きました。
UWSCでユーザーの最終入力からの経過時間を取得する - じゅんじゅんのきまぐれ
今度は、電源管理のディスプレイOFFがうざくなってきました。
ということで、あとどれくらいでスクリーンセーバーになるかお知らせしてくれるスクリプトに、ディスプレイOFFを考慮させてみました。
スクリプト
スクリーンセーバータイムアウト時間取得対応LastUserInput.uwsに依存しています。
UWSCでユーザーの最終入力からの経過時間を取得する - じゅんじゅんのきまぐれ
のを修正してあります。
PowerPolicy.uws
OPTION EXPLICIT CALL LastUserInput IFB GET_UWSC_NAME = "PowerPolicy.uws" THEN DIM msg, premsg, s, tt = PowerPolicy.GetVideoTimeout(), timeout = LUI.GetScreenSaverTimeout() IF tt > 0 AND timeout > tt THEN timeout = tt CONST DELTA = 200 CONST THRESHOLD = 60000 WHILE TRUE IFB LUI.IsScreenSaver() THEN msg = "スクリーンセーバー中" s = 1 ELSEIF LUI.IsLocked() THEN msg = "ロック中" s = 10 ELSE tt = LUI.GetTick() + timeout - LUI.GetTickCount() IFB tt < DELTA THEN s = DELTA / 1000 msg = "スクリーンセーバー!" ELSEIF tt < THRESHOLD + DELTA THEN s = ((tt - DELTA) MOD 1000) / 1000 msg = "残り " + INT(tt / 1000) + " 秒!" ELSEIF tt < THRESHOLD * 2 + DELTA THEN s = ((tt - DELTA) MOD 10000) / 1000 msg = "残り " + INT(tt / THRESHOLD) + " 分 " + INT((tt MOD THRESHOLD) / 1000) + " 秒" ELSE s = (tt - THRESHOLD * 2 - DELTA) / 1000 msg = "" ENDIF ENDIF IFB msg <> premsg THEN IFB s < 1 THEN BALLOON(msg, 0, 0, 0, , , , , 60) ELSE BALLOON(msg, 0, 0, 0, 10, , , , 120) ENDIF premsg = msg ENDIF SLEEP(s) WEND ENDIF MODULE PowerPolicy DEF_DLL GetSystemPowerStatus(var DWORD[]): BOOL: kernel32 DEF_DLL GetCurrentPowerPolicies(var DWORD[], var DWORD[]): BOOL: PowrProf FUNCTION GetVideoTimeout() DIM sps[2], gpp[47], pp[35], i = 15, bAc = TRUE IF GetSystemPowerStatus(sps) THEN bAc = (sps[0] MOD $100) IF bAc THEN i = 14 RESULT = -1 IF GetCurrentPowerPolicies(gpp, pp) THEN RESULT = pp[i] FEND FUNCTION ToPowerAction(val) SELECT val CASE 0 RESULT = "None" //No system power action. CASE 1 RESULT = "Reserved" //Reserved; do not use. CASE 2 RESULT = "Sleep" CASE 3 RESULT = "Hibernate" CASE 4 RESULT = "Shutdown" CASE 5 RESULT = "ShutdownReset" //Shutdown and reset. CASE 6 RESULT = "ShutdownOff" //Shutdown and power off. CASE 7 RESULT = "WarmEject" //Warm eject. DEFAULT RESULT = "Not defined" SELEND FEND FUNCTION ToPowerActionPolicyFlag(val) RESULT = "" IF val AND $80000000 THEN RESULT = RESULT + "CRITICAL|" //Forces a critical suspension. IF val AND $40000000 THEN RESULT = RESULT + "DISABLE_WAKES|" //Disables all wake events. IF val AND $10000000 THEN RESULT = RESULT + "LIGHTEST_FIRST|" //Uses the first lightest available sleep state. IF val AND $20000000 THEN RESULT = RESULT + "LOCK_CONSOLE|" //Requires entry of the system password upon resume from one of the system standby states. IF val AND $00000004 THEN RESULT = RESULT + "OVERRIDE_APPS|" //Has no effect. Windows Server 2003, Windows XP, and Windows 2000: Ignores applications that do not respond to the PBT_APMQUERYSUSPEND event broadcast in the WM_POWERBROADCAST message. IF val AND $00000001 THEN RESULT = RESULT + "QUERY_ALLOWED|" //Has no effect. Windows Server 2003, Windows XP, and Windows 2000: Broadcasts a PBT_APMQUERYSUSPEND event to each application to request permission to suspend operation. IF val AND $00000002 THEN RESULT = RESULT + "UI_ALLOWED|" //Applications can prompt the user for directions on how to prepare for suspension. Sets bit 0 in the Flags parameter passed in the lParam parameter of WM_POWERBROADCAST. FEND FUNCTION ToPowerActionPolicyEventCode(val) RESULT = "" IF val AND $80000000 THEN RESULT = RESULT + "FORCE_TRIGGER_RESET|" //Clears a user power button press. IF val AND $00000004 THEN RESULT = RESULT + "USER_NOTIFY_EXEC|" //Specifies a program to be executed. IF val AND $00000002 THEN RESULT = RESULT + "USER_NOTIFY_SOUND|" //User notified using sound. IF val AND $00000001 THEN RESULT = RESULT + "USER_NOTIFY_TEXT|" //User notified using the UI. IF val AND $00000008 THEN RESULT = RESULT + "USER_NOTIFY_BUTTON|" //Indicates that the power action is in response to a user power button press. IF val AND $00000010 THEN RESULT = RESULT + "USER_NOTIFY_SHUTDOWN|" //Indicates a power action of shutdown/off. FEND FUNCTION ToSystemPowerState(val) SELECT val CASE 0 RESULT = "Unspecified" //A lid-open event does not wake the system. CASE 1 RESULT = "Working" //Specifies system power state S0. CASE 2 RESULT = "Sleeping1" //Specifies system power state S1. CASE 3 RESULT = "Sleeping2" //Specifies system power state S2. CASE 4 RESULT = "Sleeping3" //Specifies system power state S3. CASE 5 RESULT = "Hibernate" //Specifies system power state S4 (HIBERNATE). CASE 6 RESULT = "Shutdown" //Specifies system power state S5 (OFF). CASE 7 RESULT = "Maximum" //Specifies the maximum enumeration value. DEFAULT RESULT = "Not defined" SELEND FEND FUNCTION ToGlobalUserPowerPolicyGlobalFlag(val) IF val AND $02 THEN RESULT = RESULT + "MultiBatteryDisplay|" //Enables or disables multiple battery display in the system Power Meter. IF val AND $04 THEN RESULT = RESULT + "PasswordLogon|" //Enables or disables requiring password logon when the system resumes from standby or hibernate. IF val AND $01 THEN RESULT = RESULT + "SysTrayBatteryMeter|" //Enables or disables the battery meter icon in the system tray. When this flag is cleared, the battery meter icon is not displayed. IF val AND $10 THEN RESULT = RESULT + "VideoDimDisplay|" //Enables or disables support for dimming the video display when the system changes from running on AC power to running on battery power. IF val AND $08 THEN RESULT = RESULT + "WakeOnRing|" //Enables or disables wake on ring support. FEND FUNCTION ToACLineStatus(val) SELECT val CASE 0 RESULT = "Offline" CASE 1 RESULT = "Online" CASE 255 RESULT = "Unknown status" DEFAULT RESULT = "Not defined" SELEND FEND FUNCTION ToBatteryFlag(val) SELECT val CASE 1 RESULT = "High" //the battery capacity is at more than 66 percent CASE 2 RESULT = "Low" //the battery capacity is at less than 33 percent CASE 4 RESULT = "Critical" //the battery capacity is at less than five percent CASE 8 RESULT = "Charging" CASE 128 RESULT = "No system battery" CASE 255 RESULT = "Unknown status" //unable to read the battery flag information CASE 0 RESULT = "Middle" //The value is zero if the battery is not being charged and the battery capacity is between low and high. DEFAULT RESULT = "Not defined" SELEND FEND PROCEDURE Dump() DIM sps[2], gpp[47], pp[35], i, j IFB GetSystemPowerStatus(sps) THEN PRINT "SYSTEM_POWER_STATUS" i = 0 PRINT " ACLineStatus " + ToACLineStatus(sps[i] MOD $100) PRINT " BatteryFlag " + ToBatteryFlag(INT(sps[i] / $100) MOD $100) PRINT " BatteryLifePercent " + (INT(sps[i] / $10000) MOD $100) PRINT " Reserved " + (INT(sps[i] / $1000000) MOD $100) i = i + 1 PRINT " BatteryLifeTime " + sps[i] i = i + 1 PRINT " BatteryFullLifeTime " + sps[i] PRINT ENDIF IFB GetCurrentPowerPolicies(gpp, pp) THEN PRINT "GLOBAL_POWER_POLICY" PRINT " GLOBAL_USER_POWER_POLICY" i = 0 PRINT " Revision " + gpp[i] + " " + i i = i + 1 PRINT " PowerButtonAc " + ToPowerAction(gpp[i]) + " " + ToPowerActionPolicyFlag(gpp[i+1]) + " " + ToPowerActionPolicyEventCode(gpp[i+2]) i = i + 3 PRINT " PowerButtonDc " + ToPowerAction(gpp[i]) + " " + ToPowerActionPolicyFlag(gpp[i+1]) + " " + ToPowerActionPolicyEventCode(gpp[i+2]) i = i + 3 PRINT " SleepButtonAc " + ToPowerAction(gpp[i]) + " " + ToPowerActionPolicyFlag(gpp[i+1]) + " " + ToPowerActionPolicyEventCode(gpp[i+2]) i = i + 3 PRINT " SleepButtonDc " + ToPowerAction(gpp[i]) + " " + ToPowerActionPolicyFlag(gpp[i+1]) + " " + ToPowerActionPolicyEventCode(gpp[i+2]) i = i + 3 PRINT " LidCloseAc " + ToPowerAction(gpp[i]) + " " + ToPowerActionPolicyFlag(gpp[i+1]) + " " + ToPowerActionPolicyEventCode(gpp[i+2]) i = i + 3 PRINT " LidCloseDc " + ToPowerAction(gpp[i]) + " " + ToPowerActionPolicyFlag(gpp[i+1]) + " " + ToPowerActionPolicyEventCode(gpp[i+2]) i = i + 3 FOR j = 0 TO 3 PRINT " DischargePolicy[" + j + "] " + i PRINT " Enable " + gpp[i] PRINT " BatteryLevel " + gpp[i+1] i = i + 2 PRINT " PowerPolicy " + ToPowerAction(gpp[i]) + " " + ToPowerActionPolicyFlag(gpp[i+1]) + " " + ToPowerActionPolicyEventCode(gpp[i+2]) PRINT " MinSystemState " + ToSystemPowerState(gpp[i+3]) i = i + 4 NEXT PRINT " GlobalFlags " + ToGlobalUserPowerPolicyGlobalFlag(gpp[i]) + " " + i i = i + 1 PRINT " GLOBAL_MACHINE_POWER_POLICY" PRINT " Revision " + gpp[i] + " " + i i = i + 1 PRINT " LidOpenWakeAc " + ToSystemPowerState(gpp[i]) i = i + 1 PRINT " LidOpenWakeDc " + ToSystemPowerState(gpp[i]) i = i + 1 PRINT " BroadcastCapacityResolution " + gpp[i] i = i + 1 PRINT "POWER_POLICY" PRINT " USER_POWER_POLICY" i = 0 PRINT " Revision " + pp[i] + " " + i i = i + 1 PRINT " IdleAc " + ToPowerAction(pp[i]) + " " + ToPowerActionPolicyFlag(pp[i+1]) + " " + ToPowerActionPolicyEventCode(pp[i+2]) i = i + 3 PRINT " IdleDc " + ToPowerAction(pp[i]) + " " + ToPowerActionPolicyFlag(pp[i+1]) + " " + ToPowerActionPolicyEventCode(pp[i+2]) i = i + 3 PRINT " IdleTimeoutAc " + pp[i] + " " + i i = i + 1 PRINT " IdleTimeoutDc " + pp[i] + " " + i i = i + 1 PRINT " IdleSensitivityAc " + (pp[i] MOD $100) PRINT " IdleSensitivityDc " + (INT(pp[i] / $100) MOD $100) PRINT " ThrottlePolicyAc " + (INT(pp[i] / $10000) MOD $100) PRINT " ThrottlePolicyDc " + (INT(pp[i] / $1000000) MOD $100) i = i + 1 PRINT " MaxSleepAc " + ToSystemPowerState(pp[i]) + " " + i i = i + 1 PRINT " MaxSleepDc " + ToSystemPowerState(pp[i]) + " " + i i = i + 1 PRINT " Reserved[2] " + pp[i] + " " + pp[i+1] i = i + 2 PRINT " VideoTimeoutAc " + pp[i] + " " + i i = i + 1 PRINT " VideoTimeoutDc " + pp[i] + " " + i i = i + 1 PRINT " SpindownTimeoutAc " + pp[i] + " " + i i = i + 1 PRINT " SpindownTimeoutDc " + pp[i] + " " + i i = i + 1 PRINT " OptimizeForPowerAc " + (pp[i] MOD $100) PRINT " OptimizeForPowerDc " + (INT(pp[i] / $100) MOD $100) PRINT " FanThrottleToleranceAc " + (INT(pp[i] / $10000) MOD $100) PRINT " FanThrottleToleranceDc " + (INT(pp[i] / $1000000) MOD $100) i = i + 1 PRINT " ForcedThrottleAc " + (pp[i] MOD $100) PRINT " ForcedThrottleDc " + (INT(pp[i] / $100) MOD $100) i = i + 1 PRINT " MACHINE_POWER_POLICY" PRINT " Revision " + pp[i] + " " + i i = i + 1 PRINT " MinSleepAc " + ToSystemPowerState(pp[i]) + " " + i i = i + 1 PRINT " MinSleepDc " + ToSystemPowerState(pp[i]) + " " + i i = i + 1 PRINT " ReducedLatencySleepAc " + ToSystemPowerState(pp[i]) + " " + i i = i + 1 PRINT " ReducedLatencySleepDc " + ToSystemPowerState(pp[i]) + " " + i i = i + 1 PRINT " DozeTimeoutAc " + pp[i] + " " + i i = i + 1 PRINT " DozeTimeoutDc " + pp[i] + " " + i i = i + 1 PRINT " DozeS4TimeoutAc " + pp[i] + " " + i i = i + 1 PRINT " DozeS4TimeoutDc " + pp[i] + " " + i i = i + 1 PRINT " MinThrottleAc " + (pp[i] MOD $100) PRINT " MinThrottleDc " + (INT(pp[i] / $100) MOD $100) // UCHAR pad1[2]; i = i + 1 PRINT " OverThrottledAc " + ToPowerAction(pp[i]) + " " + ToPowerActionPolicyFlag(pp[i+1]) + " " + ToPowerActionPolicyEventCode(pp[i+2]) i = i + 3 PRINT " OverThrottledDc " + ToPowerAction(pp[i]) + " " + ToPowerActionPolicyFlag(pp[i+1]) + " " + ToPowerActionPolicyEventCode(pp[i+2]) i = i + 3 ENDIF FEND TEXTBLOCK _power_policy_memo PowrProf.dll BOOLEAN WINAPI GetCurrentPowerPolicies( __out PGLOBAL_POWER_POLICY pGlobalPowerPolicy, __out PPOWER_POLICY pPowerPolicy ); 192+142 or 144 = DWORD[48] + DWORD[36] typedef struct _GLOBAL_POWER_POLICY { GLOBAL_USER_POWER_POLICY user; GLOBAL_MACHINE_POWER_POLICY mach; } GLOBAL_POWER_POLICY, *PGLOBAL_POWER_POLICY; 192 typedef struct _GLOBAL_USER_POWER_POLICY { ULONG Revision; POWER_ACTION_POLICY PowerButtonAc; POWER_ACTION_POLICY PowerButtonDc; POWER_ACTION_POLICY SleepButtonAc; POWER_ACTION_POLICY SleepButtonDc; POWER_ACTION_POLICY LidCloseAc; POWER_ACTION_POLICY LidCloseDc; SYSTEM_POWER_LEVEL DischargePolicy[NUM_DISCHARGE_POLICIES]; ULONG GlobalFlags; } GLOBAL_USER_POWER_POLICY, *PGLOBAL_USER_POWER_POLICY; 176 typedef struct { POWER_ACTION Action; ULONG Flags; ULONG EventCode; } POWER_ACTION_POLICY, *PPOWER_ACTION_POLICY; 12 typedef enum _POWER_ACTION { PowerActionNone = 0, //No system power action. PowerActionReserved, //Reserved; do not use. PowerActionSleep, //Sleep. PowerActionHibernate, //Hibernate. PowerActionShutdown, //Shutdown. PowerActionShutdownReset, //Shutdown and reset. PowerActionShutdownOff, //Shutdown and power off. PowerActionWarmEject //Warm eject. }POWER_ACTION, *PPOWER_ACTION; POWER_ACTION_POLICY.Flags POWER_ACTION_CRITICAL 0x80000000 Forces a critical suspension. POWER_ACTION_DISABLE_WAKES 0x40000000 Disables all wake events. POWER_ACTION_LIGHTEST_FIRST 0x10000000 Uses the first lightest available sleep state. POWER_ACTION_LOCK_CONSOLE 0x20000000 Requires entry of the system password upon resume from one of the system standby states. POWER_ACTION_OVERRIDE_APPS 0x00000004 Has no effect. Windows Server 2003, Windows XP, and Windows 2000: Ignores applications that do not respond to the PBT_APMQUERYSUSPEND event broadcast in the WM_POWERBROADCAST message. POWER_ACTION_QUERY_ALLOWED 0x00000001 Has no effect. Windows Server 2003, Windows XP, and Windows 2000: Broadcasts a PBT_APMQUERYSUSPEND event to each application to request permission to suspend operation. POWER_ACTION_UI_ALLOWED 0x00000002 Applications can prompt the user for directions on how to prepare for suspension. Sets bit 0 in the Flags parameter passed in the lParam parameter of WM_POWERBROADCAST. POWER_ACTION_POLICY.EventCode POWER_FORCE_TRIGGER_RESET 0x80000000 Clears a user power button press. POWER_LEVEL_USER_NOTIFY_EXEC 0x00000004 Specifies a program to be executed. POWER_LEVEL_USER_NOTIFY_SOUND 0x00000002 User notified using sound. POWER_LEVEL_USER_NOTIFY_TEXT 0x00000001 User notified using the UI. POWER_USER_NOTIFY_BUTTON 0x00000008 Indicates that the power action is in response to a user power button press. POWER_USER_NOTIFY_SHUTDOWN 0x00000010 Indicates a power action of shutdown/off. typedef struct _SYSTEM_POWER_LEVEL { BOOLEAN Enable; UCHAR Spare[3]; ULONG BatteryLevel; POWER_ACTION_POLICY PowerPolicy; SYSTEM_POWER_STATE MinSystemState; } SYSTEM_POWER_LEVEL, *PSYSTEM_POWER_LEVEL; 24 typedef enum _SYSTEM_POWER_STATE { PowerSystemUnspecified = 0, //A lid-open event does not wake the system. PowerSystemWorking, //Specifies system power state S0. PowerSystemSleeping1, //Specifies system power state S1. PowerSystemSleeping2, //Specifies system power state S2. PowerSystemSleeping3, //Specifies system power state S3. PowerSystemHibernate, //Specifies system power state S4 (HIBERNATE). PowerSystemShutdown, //Specifies system power state S5 (OFF). PowerSystemMaximum //Specifies the maximum enumeration value. }SYSTEM_POWER_STATE, *PSYSTEM_POWER_STATE; GLOBAL_USER_POWER_POLICY.GlobalFlags EnableMultiBatteryDisplay 0x02 Enables or disables multiple battery display in the system Power Meter. EnablePasswordLogon 0x04 Enables or disables requiring password logon when the system resumes from standby or hibernate. EnableSysTrayBatteryMeter 0x01 Enables or disables the battery meter icon in the system tray. When this flag is cleared, the battery meter icon is not displayed. EnableVideoDimDisplay 0x10 Enables or disables support for dimming the video display when the system changes from running on AC power to running on battery power. EnableWakeOnRing 0x08 Enables or disables wake on ring support. typedef struct _GLOBAL_MACHINE_POWER_POLICY { ULONG Revision; SYSTEM_POWER_STATE LidOpenWakeAc; SYSTEM_POWER_STATE LidOpenWakeDc; ULONG BroadcastCapacityResolution; } GLOBAL_MACHINE_POWER_POLICY, *PGLOBAL_MACHINE_POWER_POLICY; 16 typedef struct _POWER_POLICY { USER_POWER_POLICY user; MACHINE_POWER_POLICY mach; } POWER_POLICY, *PPOWER_POLICY; 142 or 144 typedef struct _USER_POWER_POLICY { ULONG Revision; POWER_ACTION_POLICY IdleAc; POWER_ACTION_POLICY IdleDc; ULONG IdleTimeoutAc; ULONG IdleTimeoutDc; UCHAR IdleSensitivityAc; UCHAR IdleSensitivityDc; UCHAR ThrottlePolicyAc; UCHAR ThrottlePolicyDc; SYSTEM_POWER_STATE MaxSleepAc; SYSTEM_POWER_STATE MaxSleepDc; ULONG Reserved[2]; ULONG VideoTimeoutAc; ULONG VideoTimeoutDc; ULONG SpindownTimeoutAc; ULONG SpindownTimeoutDc; BOOLEAN OptimizeForPowerAc; BOOLEAN OptimizeForPowerDc; UCHAR FanThrottleToleranceAc; UCHAR FanThrottleToleranceDc; UCHAR ForcedThrottleAc; UCHAR ForcedThrottleDc; } USER_POWER_POLICY, *PUSER_POWER_POLICY; 78 typedef struct _MACHINE_POWER_POLICY { ULONG Revision; SYSTEM_POWER_STATE MinSleepAc; SYSTEM_POWER_STATE MinSleepDc; SYSTEM_POWER_STATE ReducedLatencySleepAc; SYSTEM_POWER_STATE ReducedLatencySleepDc; ULONG DozeTimeoutAc; ULONG DozeTimeoutDc; ULONG DozeS4TimeoutAc; ULONG DozeS4TimeoutDc; UCHAR MinThrottleAc; UCHAR MinThrottleDc; UCHAR pad1[2]; POWER_ACTION_POLICY OverThrottledAc; POWER_ACTION_POLICY OverThrottledDc; } MACHINE_POWER_POLICY, *PMACHINE_POWER_POLICY; 64 kernel32.dll BOOL WINAPI GetSystemPowerStatus( __out LPSYSTEM_POWER_STATUS lpSystemPowerStatus ); typedef struct _SYSTEM_POWER_STATUS { BYTE ACLineStatus; BYTE BatteryFlag; BYTE BatteryLifePercent; BYTE Reserved1; DWORD BatteryLifeTime; DWORD BatteryFullLifeTime; } SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS; ACLineStatus The AC power status. This member can be one of the following values. 0 Offline 1 Online 255 Unknown status BatteryFlag The battery charge status. This member can contain one or more of the following flags. 1 High-the battery capacity is at more than 66 percent 2 Low-the battery capacity is at less than 33 percent 4 Critical-the battery capacity is at less than five percent 8 Charging 128 No system battery 255 Unknown status?unable to read the battery flag information The value is zero if the battery is not being charged and the battery capacity is between low and high. BatteryLifePercent The percentage of full battery charge remaining. This member can be a value in the range 0 to 100, or 255 if status is unknown. Reserved1 Reserved; must be zero. BatteryLifeTime The number of seconds of battery life remaining, or -1 if remaining seconds are unknown. BatteryFullLifeTime The number of seconds of battery life when at full charge, or -1 if full battery lifetime is unknown. ENDTEXTBLOCK ENDMODULE
大作になってますが、PowerPolicyモジュールは、DEF_DLLとGetVideoTimeout関数しかいりません。
サンプルスクリプトが、ディスプレイOFF時間を考慮した、スクリーンセーバー起動お知らせになってます。
ということで、こちらを実行しておくイメージ。
なお、SetThreadExecutionStateをしていた場合には未対応です。