-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (93 loc) · 2.94 KB
/
main.go
File metadata and controls
103 lines (93 loc) · 2.94 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
package main
import (
"log"
"os/exec"
"strings"
"syscall"
"time"
"MSIAfterburnerScript/config"
"MSIAfterburnerScript/watcher"
)
// runAfterburner executes the MSI Afterburner command.
func runAfterburner(exe, arg string) {
cmd := exec.Command(exe, arg)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
if err := cmd.Start(); err != nil {
log.Printf("Failed to launch Afterburner with profile %s: %v", arg, err)
} else {
log.Printf("Successfully applied Afterburner profile: %s", arg)
}
}
// checkStateAndApplyProfile is the core logic for determining and applying a profile.
// It now uses the Overrides map in the config as the sole list of targets.
func checkStateAndApplyProfile(cfg *config.Config, currentProfile *string) {
// The list of targets is now the keys of the Overrides map.
// The watcher will prioritize the foreground application.
activeTarget, isActive := watcher.FirstActiveTarget(cfg.Overrides)
var desiredProfile string
if isActive {
profile := cfg.Overrides[activeTarget]
if profile != "" {
desiredProfile = profile
} else {
desiredProfile = cfg.ProfileOn
}
} else {
desiredProfile = cfg.ProfileOff
}
if desiredProfile != *currentProfile {
log.Printf("State change detected. Desired profile: %s.", desiredProfile)
if isActive {
log.Printf("Reason: Active target '%s' found.", activeTarget)
} else {
log.Printf("Reason: No active targets found.")
}
runAfterburner(cfg.AfterburnerPath, desiredProfile)
*currentProfile = desiredProfile
}
}
// startPollingMode runs the application by checking for targets on a timer.
func startPollingMode(cfg config.Config) {
log.Println("Starting in Polling Mode.")
var currentProfile string
checkStateAndApplyProfile(&cfg, ¤tProfile)
ticker := time.NewTicker(time.Duration(cfg.DelaySeconds) * time.Second)
defer ticker.Stop()
for range ticker.C {
reloadedCfg := config.Load()
cfg.ProfileOn = reloadedCfg.ProfileOn
cfg.ProfileOff = reloadedCfg.ProfileOff
cfg.Overrides = reloadedCfg.Overrides
cfg.AfterburnerPath = reloadedCfg.AfterburnerPath
checkStateAndApplyProfile(&cfg, ¤tProfile)
}
}
// startEventMode runs the application by listening for system events.
func startEventMode(cfg config.Config) {
log.Println("Starting in Event-Driven Mode.")
var currentProfile string
eventHandler := func() {
reloadedCfg := config.Load()
cfg.ProfileOn = reloadedCfg.ProfileOn
cfg.ProfileOff = reloadedCfg.ProfileOff
cfg.Overrides = reloadedCfg.Overrides
cfg.AfterburnerPath = reloadedCfg.AfterburnerPath
checkStateAndApplyProfile(&cfg, ¤tProfile)
}
eventHandler()
watcher.StartEventWatcher(eventHandler)
select {}
}
func main() {
log.SetFlags(log.Ltime)
cfg := config.Load()
// log.Println("Configuration loaded.")
switch strings.ToLower(cfg.MonitoringMode) {
case "poll":
startPollingMode(cfg)
case "event":
startEventMode(cfg)
default:
log.Fatalf("Invalid monitoring_mode %q in config.json", cfg.MonitoringMode)
}
}