-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathx3dctl
More file actions
executable file
·168 lines (132 loc) · 2.94 KB
/
Copy pathx3dctl
File metadata and controls
executable file
·168 lines (132 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
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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_NAME="x3dctl"
QUIET=0
VERBOSE=0
NO_IRQ=0
log() {
if [[ "$QUIET" -eq 0 ]]; then
echo "[$SCRIPT_NAME] $*" >&2
fi
}
vlog() {
if [[ "$QUIET" -eq 0 && "$VERBOSE" -eq 1 ]]; then
echo "[$SCRIPT_NAME] $*" >&2
fi
}
die() {
echo "[$SCRIPT_NAME] ERROR: $*" >&2
exit 1
}
HELPER="$(command -v x3dctl-helper || true)"
[[ -n "$HELPER" && -x "$HELPER" ]] || die "x3dctl-helper not found in PATH"
helper_can_control() {
"$HELPER" can-control >/dev/null 2>&1
}
run_helper() {
if helper_can_control; then
if "$HELPER" "$@"; then
return 0
fi
vlog "Direct helper execution failed; falling back to sudo"
fi
sudo "$HELPER" "$@"
}
# Global Flag Parsing
while [[ "${1:-}" == -* ]]; do
case "$1" in
-q|--quiet) QUIET=1; shift ;;
-v|--verbose) VERBOSE=1; shift ;;
--no-irq) NO_IRQ=1; shift ;;
*) die "Unknown flag: $1" ;;
esac
done
if [[ $# -eq 0 ]]; then
die "Usage:
$SCRIPT_NAME run <command...>
$SCRIPT_NAME gaming [command...]
$SCRIPT_NAME performance [command...]
$SCRIPT_NAME toggle [command...]
$SCRIPT_NAME status"
fi
COMMAND="$1"
shift
# Helpers
get_current_mode() {
"$HELPER" status | awk '/^Mode:/ {print $2}'
}
toggle_mode() {
case "$(get_current_mode)" in
cache) echo "frequency" ;;
frequency) echo "cache" ;;
*) die "Unknown current mode" ;;
esac
}
apply_profile_to_self() {
local profile="$1"
vlog "Applying profile: $profile"
run_helper apply "$$" "$profile"
}
# Command Dispatch
case "$COMMAND" in
run)
[[ $# -eq 0 ]] && die "Usage: run <command...>"
APP_KEY="$*"
vlog "Querying profile for: $APP_KEY"
PROFILE=$("$HELPER" query "$APP_KEY" 2>/dev/null || true)
if [[ -z "$PROFILE" ]]; then
vlog "No profile found; defaulting to 'gaming'"
PROFILE="gaming"
fi
vlog "Applying profile: $PROFILE"
run_helper apply "$$" "$PROFILE" || exit 1
exec "$@"
;;
gaming)
if [[ "$NO_IRQ" -eq 1 ]]; then
run_helper cache --no-irq || exit 1
else
run_helper cache || exit 1
fi
log "Switched to cache"
if [[ $# -gt 0 ]]; then
apply_profile_to_self "gaming"
exec "$@"
fi
;;
performance)
if [[ "$NO_IRQ" -eq 1 ]]; then
run_helper frequency --no-irq || exit 1
else
run_helper frequency || exit 1
fi
log "Switched to frequency"
if [[ $# -gt 0 ]]; then
apply_profile_to_self "frequency"
exec "$@"
fi
;;
toggle)
NEW_MODE=$(toggle_mode)
if [[ "$NO_IRQ" -eq 1 ]]; then
run_helper "$NEW_MODE" --no-irq || exit 1
else
run_helper "$NEW_MODE" || exit 1
fi
log "Switched to $NEW_MODE"
if [[ $# -gt 0 ]]; then
if [[ "$NEW_MODE" == "cache" ]]; then
apply_profile_to_self "gaming"
else
apply_profile_to_self "frequency"
fi
exec "$@"
fi
;;
status)
"$HELPER" status
;;
*)
die "Unknown command: $COMMAND"
;;
esac