-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapploop.ahk
More file actions
341 lines (299 loc) · 8.41 KB
/
apploop.ahk
File metadata and controls
341 lines (299 loc) · 8.41 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#Requires AutoHotkey v2.0
#SingleInstance force
TraySetIcon(A_ScriptDir "\icon\ahkocean16.ico")
SetTitleMatchMode("RegEx")
CoordMode("ToolTip")
homeassistantToken := Fileread("secrets\homeassistant.txt") ; load the token from file
#Include includes\Peep.v2.ahk
; if not admin, start as admin
; taken from https://www.autohotkey.com/boards/viewtopic.php?p=523250#p523250
if (!A_IsAdmin) {
try {
Run("*RunAs `"" A_ScriptFullPath "`"")
ExitApp()
}
catch {
MsgBox("Couldn't run " A_ScriptName " as admin! Some things may not work")
ExitApp()
}
}
apps := [{
app: "ahk_exe Blitz\.exe$",
; affinity: binary("10001000"),
priority: -1,
flags: []
}, {
app: "ahk_exe LeagueClientUx\.exe$",
flags: [runblitz]
}, {
app: "ahk_exe League of Legends\.exe$",
flags: [closelbm]
}, {
; most unreal engine games end in win65-shipping
app: "ahk_exe .*-Win64-Shipping\.exe$",
flags: [closelbm],
},]
cpumask(cpuList*) {
mask := 0
for cpu in cpuList {
mask |= (1 << cpu)
}
return mask
}
binary(binStr) {
dec := 0
len := StrLen(binStr)
for i, c in StrSplit(binStr) {
dec := (dec << 1) | (c = "1" ? 1 : 0)
}
return dec
}
; collect unique flags from all apps
states := []
seen := Map()
for app in apps {
for flag in app.flags {
if !seen.Has(flag) {
seen[flag] := true
states.Push(flag)
}
}
}
seen := Map()
closeobs(state := true) {
; MsgBox(state)
}
closelbm(state := true) {
closeapp(state,
"LBM",
["LittleBigMouse.Hook.exe", "LittleBigMouse.Ui.Avalonia.exe"],
["`"C:\Program Files\LittleBigMouse\LittleBigMouse.Ui.Avalonia.exe`" --start"]
)
}
runblitz(state := true) {
closeapp(!state, "blitz", ["Blitz.exe"], [A_AppData "\..\Local\Programs\Blitz\Blitz.exe"], false, true)
}
closeapp(state := true, name := "", closelist := [], runlist := [], hideLaunch := false, forceClose := false) {
closelist := closelist
runlist := runlist
if (name == "" && runlist.Length)
name := runlist[1]
if (state) {
CenteredTooltip("Closing " name)
for app in closelist {
cmd := "taskkill"
if forceClose
cmd .= " /f"
cmd .= " /im " app
try {
Run(cmd, , "Hide")
} catch as err {
}
}
} else {
CenteredTooltip("Launching " name)
for app in runlist {
try {
Run(app, , hideLaunch ? "Hide" : "")
} catch as err {
MsgBox("Failed to launch: " app "`n" err.Message)
}
}
}
}
enabledstates := Map()
for state in states {
enabledstates[state] := false
}
SetTimer(checkapps, 5000)
checkapps()
checkapps() {
tempStates := Map()
for flag in enabledstates {
tempStates[flag] := false
}
; mark a flag active if any app using it is running
for app in apps {
if (WinExist(app.app)) {
; set flags
for flag in app.flags {
tempStates[flag] := true
}
}
; set priority and affinity
if (app.HasOwnProp("priority") && app.priority != 0) {
SetPriority(app.app, app.priority)
}
if (app.HasOwnProp("affinity") && app.affinity != 0) {
SetAffinity(app.app, app.affinity)
}
}
; compare with previous states and trigger on change
for flag in enabledstates {
prev := enabledstates[flag]
curr := tempStates[flag]
if (curr != prev) {
; run function
flag.Call(curr)
enabledstates[flag] := curr
}
}
}
SetPriority(appname, Level := "Normal") {
static pidcache := Map()
prio := "Normal"
if !IsNumber(Level)
norm := StrLower(StrReplace(StrReplace(Level, " ", ""), "_", ""))
else
norm := String(Level)
switch norm {
case "-2", "l", "low":
prio := "Low"
case "-1", "b", "belownormal":
prio := "BelowNormal"
case "0", "n", "normal":
prio := "Normal"
case "1", "a", "abovenormal":
prio := "AboveNormal"
case "2", "h", "high":
prio := "High"
case "3", "r", "realtime":
prio := "Realtime"
Default:
prio := "Normal"
}
DetectHiddenWindows(true)
hwnds := WinGetList(appname)
pids := Map()
for hwnd in hwnds {
pid := WinGetPID("ahk_id " hwnd)
if (pid && !pids.Has(pid)) {
pids[pid] := true
}
}
exeName := ""
if RegExMatch(appname, ".*ahk_exe\s+([^\s\\]+).*", &m) {
exeName := m[1]
}
if (!exeName) {
MsgBox("Cannot extract exe name from: " appname)
return
}
exepids := GetPIDsByExeName(exeName)
for pid in exepids {
pids[pid] := true
}
for pid in pids {
if pidcache.Has(pid) {
lastPrio := pidcache[pid]
} else {
lastPrio := ""
}
if (lastPrio != prio) {
try {
ProcessSetPriority(prio, pid)
pidcache[pid] := prio
CenteredTooltip("changing priority of " appname " to " prio "`n" pid)
}
}
}
DetectHiddenWindows(false)
}
GetPIDsByExeName(exeName) {
pids := []
wmi := ComObjGet("winmgmts:")
query := "Select ProcessId from Win32_Process Where Name='" exeName ".exe'"
processes := wmi.ExecQuery(query)
for proc in processes {
pids.Push(proc.ProcessId)
}
return pids
}
SetAffinity(appname, affinityMask) {
static pidcache := Map()
exeName := ""
if RegExMatch(appname, ".*ahk_exe\s+([^\s\\]+).*", &m)
exeName := m[1]
if !exeName {
; MsgBox("Cannot extract exe name from: " appname)
return
}
DetectHiddenWindows(true)
pids := Map()
try {
hwnds := WinGetList(appname)
for hwnd in hwnds {
pid := WinGetPID("ahk_id " hwnd)
if (pid && !pids.Has(pid)) {
pids[pid] := true
}
}
}
exepids := GetPIDsByExeName(exeName)
for pid in exepids {
pids[pid] := true
}
for pid in pids {
if pidcache.Has(pid) {
lastAffinity := pidcache[pid]
} else {
lastAffinity := ""
}
if (lastAffinity != affinityMask) {
CenteredTooltip("changing affinity of " exeName " to " affinityMask "`nPID: " pid)
; set affinity using DllCall
hProcess := DllCall("OpenProcess", "UInt", 0x0200 | 0x0400, "Int", false, "UInt", pid, "Ptr")
if hProcess {
DllCall("SetProcessAffinityMask", "Ptr", hProcess, "Ptr", affinityMask)
DllCall("CloseHandle", "Ptr", hProcess)
pidcache[pid] := affinityMask
}
}
}
DetectHiddenWindows(false)
}
CenteredTooltip(text, time := 2500, radius := 500) {
static tooltipID := 0
static activeIDs := Map()
id := 0
loop 20 {
idx := (tooltipID + A_Index) > 20 ? (tooltipID + A_Index) - 20 : (tooltipID + A_Index)
if (!activeIDs.Has(idx)) {
id := idx
break
}
}
if (!id)
id := 1
tooltipID := id
activeIDs[id] := true
angle := (id - 1) * (2 * 3.14159265 / 20)
x := (A_ScreenWidth / 2) + radius * Cos(angle)
y := (A_ScreenHeight / 2) + radius * Sin(angle)
ToolTip(text, x, y, id)
SetTimer(() => cleartooltip(), -time)
cleartooltip() {
ToolTip(, , , id)
activeIDs.Delete(id)
}
}
#HotIf WinActive(A_ScriptName " ahk_exe Code.exe")
~^s:: {
; Send("^s")
ToolTip("Reloading " A_ScriptName ".", A_ScreenWidth / 2, A_ScreenHeight / 2)
Sleep(1000)
Reload()
; MsgBox("reloading !")
return
}
homeassistantRequest(requestJSON, url, wait := false) {
; get token from variable earlier
global homeassistantToken
if (wait) {
RunWait(A_ComSpec " /C " "curl -X POST -H `"Authorization: Bearer " homeassistantToken "`" -H `"Content-Type: application/json`" -d `"" requestJSON "`" http://homeassistant.local:8123/api/" url, ,
"hide")
} else {
Run(A_ComSpec " /C " "curl -X POST -H `"Authorization: Bearer " homeassistantToken "`" -H `"Content-Type: application/json`" -d `"" requestJSON "`" http://homeassistant.local:8123/api/" url, ,
"hide")
}
}