-
Notifications
You must be signed in to change notification settings - Fork 0
/
IdleSched.txt
135 lines (106 loc) · 3.54 KB
/
IdleSched.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#NoEnv
;#Warn
;#NoTrayIcon
#KeyHistory 0
#SingleInstance force
ListLines Off
CoordMode, Tooltip, Screen
#Persistent
;-----------
;Monitor Power state @ https://www.autohotkey.com/boards/viewtopic.php?t=70645
;Nvidia NVAPI @ https://github.com/jNizM/AHK_NVIDIA_NvAPI
;-----------
$MonActive = 0
$IdleTimer := 0
$IdleWait := 600
GUID_CONSOLE_DISPLAY_STATE := "6FE69556-704A-47A0-8F24-C28D936FDA47"
settimer, scheduler, 1000
OnExit(Func("UnRegisterNotification").Bind(RegisterPowerSettingNotification(GUID_CONSOLE_DISPLAY_STATE)))
OnMessage(0x218, "WM_POWERBROADCAST")
#Include NVApi.ahk
FetchTasks()
{
$Tasks := {}
(service := ComObjCreate("Schedule.Service")).Connect()
for task in service.GetFolder("\").GetTasks(1)
{
if !task.Definition.Settings.RunOnlyIfIdle || !task.Definition.Settings.Enabled || !task.Definition.Settings.AllowDemandStart
continue
$Time := 0
Loop, Parse, % RegExReplace(task.Definition.Settings.IdleSettings.IdleDuration,"(\d+\D)","$0 "),%A_Space%, PT
$Time += Ceil($LF := A_LoopField) * (InStr($LF,"M")?60:InStr($LF,"H")?3600:InStr($LF,"D")?86400:1)
$Tasks.push({Task: Task, Name: Task.Name, IdleDuration: $Time, StopOnIdleEnd: task.Definition.Settings.IdleSettings.StopOnIdleEnd})
}
return $Tasks
}
RegisterPowerSettingNotification(GUID)
{
static DEVICE_NOTIFY_WINDOW_HANDLE := 0
VarSetCapacity(UUID, 16, 0)
DllCall("Rpcrt4\UuidFromString", Str, GUID, Ptr, &UUID)
Return DllCall("RegisterPowerSettingNotification", Ptr, A_ScriptHwnd, Ptr, &UUID, UInt, DEVICE_NOTIFY_WINDOW_HANDLE, Ptr)
}
UnRegisterNotification(handle)
{
DllCall("UnregisterPowerSettingNotification", Ptr, handle)
}
WM_POWERBROADCAST(wp, lp)
{
global $MonActive ;0=OFF/1=ON/2=EarlyOFFNoticeBy5Sec
if ($MonActive := NumGet(lp+20,0,"UChar")) > 1
return
;loop, % ($MonActive + 1)
; SoundBeep, 225, 100
}
return
IniParse(Path,Section="") ;Parse Sections into Key/Value pairs
{
if !Section
Section :=
$out:={}
IniRead, In, %Path%, %Section%
Loop, parse, In, `n, `r
if !Section
$out[A_LoopField] := IniParse(Path,A_LoopField)
else
$Temp:=StrSplit(A_LoopField,"="), $out[$Temp[1]]:=$Temp[2]
return $out
}
scheduler:
;Idle state always resets 1 cycle before the timer to detect current Idle transition
$IdleTimer := $IsIdle?$IdleTimer+1:0
$isIdle := ((Floor(A_TimeIdle/1000) > $IdleWait) && NvAPI.GPU_GetDynamicPstatesInfoEx().GPU.percentage <= 5)
|| !DllCall("User32\OpenInputDesktop","int",0*0,"int",0*0,"int",0x0001L*1) || !$MonActive
if $isIdle ;Entering IDLE: Poll timed idle tasks
{
if !$TaskList
$TaskList := FetchTasks()
for k, v in $TaskList
if ($IdleTimer ~= v.IdleDuration) && (v.Task.State < 4)
{
v.Task.RunEx("",0x2,DllCall("ProcessIdToSessionId", "UInt", DllCall("GetCurrentProcessId"), "UInt*", SessionId),"")
;msgbox % "Attempting to start " . v.Name
}
if $IdleTimer = 0
Run, TaskStart.bat,, hide
}
else if $IdleTimer ;pulling out of idle
{
for k, v in $TaskList
if V.StopOnIdleEnd && v.Task.State ~= 4
v.Task.Stop(0)
$TaskList :=
Run, TaskEnd.bat,, hide
; SoundBeep, 500, 100
}
;tooltip, % "$IsIdle: " . $IsIdle . " @ " . $IdleTimer
;tooltip, % "GPUUse: " . NvAPI.GPU_GetDynamicPstatesInfoEx().GPU.percentage
return
if !$Idle && $isIdle
Run, Taskend.bat,, hide
if !$isIdle
{
if $Idle = 1
Run, Taskstart.bat,, hide
}
return