forked from Axenide/Ambxst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.sh
More file actions
executable file
·598 lines (537 loc) · 16.4 KB
/
cli.sh
File metadata and controls
executable file
·598 lines (537 loc) · 16.4 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#!/usr/bin/env bash
# Ambxst CLI - It was needed, so here it is. lol
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Use environment variables if set by flake, otherwise fall back to PATH
QS_BIN="${AMBXST_QS:-qs}"
NIXGL_BIN="${AMBXST_NIXGL:-}"
if [ -z "${QML2_IMPORT_PATH:-}" ]; then
if command -v qs >/dev/null 2>&1; then
true
fi
fi
# If QML2_IMPORT_PATH is set (by wrapper or dev shell), ensure QML_IMPORT_PATH matches
if [ -n "${QML2_IMPORT_PATH:-}" ] && [ -z "${QML_IMPORT_PATH:-}" ]; then
export QML_IMPORT_PATH="$QML2_IMPORT_PATH"
fi
show_help() {
cat <<EOF
Ambxst CLI - Desktop Environment Control
Usage: ambxst [COMMAND]
Commands:
(none) Launch Ambxst
update Update Ambxst
refresh Refresh local/dev profile (for developers)
lock Activate lockscreen
brightness <percent> [monitor] Set brightness (0-100)
brightness +/-<delta> [monitor] Adjust brightness relatively
brightness -s [monitor] Save current brightness
brightness -r [monitor] Restore saved brightness
brightness -l List monitors and their brightness
help Show this help message
version, -v, --version Show Ambxst version
goodbye Uninstall Ambxst :(
install <target> Install compositor config (hyprland)
remove <target> Remove compositor config (hyprland)
Examples:
ambxst brightness 75 Set all monitors to 75%
ambxst brightness 50 HDMI-A-1 Set HDMI-A-1 to 50%
ambxst brightness +10 Increase brightness by 10%
ambxst brightness -5 HDMI-A-1 Decrease HDMI-A-1 brightness by 5%
ambxst brightness 10 -s Save current, then set all to 10%
ambxst brightness -s HDMI-A-1 Save current brightness of HDMI-A-1
ambxst brightness -r Restore saved brightness
EOF
}
AMBXST_HYPR_SOURCE="source = ~/.local/share/ambxst/hyprland.conf"
AMBXST_HYPR_BLOCK=$(
cat <<'EOF'
# Ambxst
source = ~/.local/share/ambxst/hyprland.conf
# OVERRIDES
# Down here you can write or source anything that you want to override from Ambxst's settings.
EOF
)
append_ambxst_hyprland_block() {
local conf="$1"
if [ -f "$conf" ] && grep -qF "$AMBXST_HYPR_SOURCE" "$conf"; then
echo "Ambxst Hyprland block already present in $conf"
return 0
fi
if [ -f "$conf" ] && [ -s "$conf" ]; then
printf "\n%s\n" "$AMBXST_HYPR_BLOCK" >>"$conf"
else
printf "%s\n" "$AMBXST_HYPR_BLOCK" >"$conf"
fi
echo "Added Ambxst Hyprland block to $conf"
}
remove_ambxst_hyprland_block() {
local conf="$1"
if [ ! -f "$conf" ]; then
echo "$conf does not exist"
return 0
fi
awk -v source="$AMBXST_HYPR_SOURCE" '
function is_remove(line) {
return line == source \
|| line == "# Ambxst" \
|| line == "# OVERRIDES" \
|| line == "# Down here you can write or source anything that you want to override from Ambxst'\''s settings."
}
{
lines[NR] = $0
}
END {
for (i = 1; i <= NR; i++) {
line = lines[i]
nextline = (i < NR) ? lines[i + 1] : ""
if (is_remove(line)) {
continue
}
if (line == "" && (is_remove(lines[i - 1]) || is_remove(nextline))) {
continue
}
print line
}
}
' "$conf" >"${conf}.tmp" && mv "${conf}.tmp" "$conf"
echo "Removed Ambxst Hyprland block from $conf"
}
find_ambxst_pid() {
# Try to find QuickShell process running shell.qml
# QuickShell binary can be named 'qs' or 'quickshell'
local pid
# First try with full path (production/flake mode)
pid=$(pgrep -f "qs.*${SCRIPT_DIR}/shell.qml" 2>/dev/null | head -1)
if [ -z "$pid" ]; then
pid=$(pgrep -f "quickshell.*${SCRIPT_DIR}/shell.qml" 2>/dev/null | head -1)
fi
# If not found, try with relative path (development mode)
if [ -z "$pid" ]; then
pid=$(pgrep -f "qs.*shell.qml" 2>/dev/null | head -1)
fi
if [ -z "$pid" ]; then
pid=$(pgrep -f "quickshell.*shell.qml" 2>/dev/null | head -1)
fi
# Last resort: find any qs/quickshell process in this directory
if [ -z "$pid" ]; then
pid=$(pgrep -a "qs" 2>/dev/null | grep -F "$SCRIPT_DIR" | awk '{print $1}' | head -1)
fi
if [ -z "$pid" ]; then
pid=$(pgrep -a quickshell 2>/dev/null | grep -F "$SCRIPT_DIR" | awk '{print $1}' | head -1)
fi
echo "$pid"
}
find_ambxst_pid_cached() {
# Optimized PID lookup: check cache file first, then fall back to pgrep
local pid_file="/tmp/ambxst.pid"
local pid=""
# Check if cache file exists and process is alive
if [ -f "$pid_file" ]; then
pid=$(<"$pid_file" 2>/dev/null)
# Verify process still exists using kill -0 (no signal, just test)
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
echo "$pid"
return 0
fi
# PID is stale, remove cache file
rm -f "$pid_file"
fi
# Fallback: use expensive pgrep search
pid=$(find_ambxst_pid)
echo "$pid"
}
restart_ambxst() {
PID=$(find_ambxst_pid_cached)
if [ -n "$PID" ]; then
echo "Stopping Ambxst (PID $PID)..."
kill "$PID"
# Wait for process to exit
while kill -0 "$PID" 2>/dev/null; do
sleep 0.1
done
fi
echo "Starting Ambxst..."
# Relaunch the script in background
nohup "$0" >/dev/null 2>&1 &
}
case "${1:-}" in
update)
echo "Updating Ambxst..."
curl -fsSL get.axeni.de/ambxst | sh
restart_ambxst
;;
refresh)
echo "Refreshing Ambxst profile..."
exec nix profile upgrade Ambxst --refresh --impure
;;
run)
CMD="${2:-}"
PIPE="/tmp/ambxst_ipc.pipe"
if [ -z "$CMD" ]; then
echo "Error: No command specified for run"
exit 1
fi
# Fast path: Write directly to pipe if it exists (Zero latency)
if [ -p "$PIPE" ]; then
echo "$CMD" >"$PIPE" &
exit 0
fi
# Fallback path: Use QS IPC with cached PID lookup
PID=$(find_ambxst_pid_cached)
if [ -z "$PID" ]; then
echo "Error: Ambxst is not running"
exit 1
fi
qs ipc --pid "$PID" call ambxst run "$CMD" 2>/dev/null || {
echo "Error: Could not run command '$CMD'"
exit 1
}
;;
lock)
PID=$(find_ambxst_pid_cached)
if [ -z "$PID" ]; then
echo "Error: Ambxst is not running"
exit 1
fi
qs ipc --pid "$PID" call ambxst run lockscreen 2>/dev/null || {
echo "Error: Could not activate lockscreen"
exit 1
}
;;
reload)
restart_ambxst
;;
quit)
PID=$(find_ambxst_pid_cached)
if [ -n "$PID" ]; then
echo "Stopping Ambxst (PID $PID)..."
kill "$PID"
else
echo "Ambxst is not running"
fi
;;
screen)
SUB="${2:-}"
if [ "$SUB" = "off" ]; then
if command -v hyprctl &>/dev/null; then
hyprctl dispatch dpms off
else
notify-send "Screen Off" "Not supported on this compositor yet"
fi
elif [ "$SUB" = "on" ]; then
if command -v hyprctl &>/dev/null; then
hyprctl dispatch dpms on
else
notify-send "Screen On" "Not supported on this compositor yet"
fi
else
echo "Usage: ambxst screen [on|off]"
exit 1
fi
;;
suspend)
if command -v systemctl &>/dev/null; then
systemctl suspend
elif command -v loginctl &>/dev/null; then
loginctl suspend
else
# Fallback to D-Bus
dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.Suspend boolean:true
fi
;;
brightness)
PID=$(find_ambxst_pid_cached)
if [ -z "$PID" ]; then
echo "Error: Ambxst is not running"
exit 1
fi
BRIGHTNESS_SAVE_FILE="/tmp/ambxst_brightness_saved.txt"
# Parse arguments
ARG2="${2:-}"
ARG3="${3:-}"
ARG4="${4:-}"
# Handle list flag
if [ "$ARG2" = "-l" ] || [ "$ARG2" = "--list" ]; then
echo "Monitors:"
if command -v hyprctl &>/dev/null; then
hyprctl monitors -j 2>/dev/null | jq -r '.[] | " \(.name)"' || {
echo "Error: Could not list monitors"
exit 1
}
else
echo "Error: hyprctl not found"
exit 1
fi
exit 0
fi
# Handle restore flag
if [ "$ARG2" = "-r" ] || [ "$ARG2" = "--restore" ]; then
if [ ! -f "$BRIGHTNESS_SAVE_FILE" ]; then
echo "Error: No saved brightness found. Use -s to save first."
exit 1
fi
MONITOR="${ARG3:-}"
if [ -z "$MONITOR" ]; then
# Restore all monitors
while IFS=: read -r name value; do
if [ -n "$name" ] && [ -n "$value" ]; then
NORMALIZED=$(awk "BEGIN {printf \"%.2f\", $value / 100}")
qs ipc --pid "$PID" call brightness set "$NORMALIZED" "$name" 2>/dev/null || {
echo "Warning: Could not restore brightness for $name"
}
fi
done <"$BRIGHTNESS_SAVE_FILE"
echo "Restored brightness for all monitors"
else
# Restore specific monitor
VALUE=$(grep "^${MONITOR}:" "$BRIGHTNESS_SAVE_FILE" | cut -d: -f2)
if [ -z "$VALUE" ]; then
echo "Error: No saved brightness for monitor $MONITOR"
exit 1
fi
NORMALIZED=$(awk "BEGIN {printf \"%.2f\", $VALUE / 100}")
qs ipc --pid "$PID" call brightness set "$NORMALIZED" "$MONITOR" 2>/dev/null || {
echo "Error: Could not restore brightness for $MONITOR"
exit 1
}
echo "Restored brightness for $MONITOR to ${VALUE}%"
fi
exit 0
fi
# Parse value and monitor/flags
VALUE=""
MONITOR=""
SAVE_FLAG=false
RELATIVE_MODE=false
RELATIVE_DELTA=0
if [[ "$ARG2" =~ ^[0-9]+$ ]]; then
VALUE="$ARG2"
if [ "$ARG3" = "-s" ] || [ "$ARG3" = "--save" ]; then
SAVE_FLAG=true
elif [ -n "$ARG3" ] && [ "$ARG3" != "-s" ] && [ "$ARG3" != "--save" ]; then
MONITOR="$ARG3"
if [ "$ARG4" = "-s" ] || [ "$ARG4" = "--save" ]; then
SAVE_FLAG=true
fi
fi
elif [[ "$ARG2" =~ ^[+-][0-9]+$ ]]; then
# Relative mode: +10 or -5
RELATIVE_MODE=true
RELATIVE_DELTA="$ARG2"
if [ -n "$ARG3" ] && [ "$ARG3" != "-s" ] && [ "$ARG3" != "--save" ]; then
MONITOR="$ARG3"
if [ "$ARG4" = "-s" ] || [ "$ARG4" = "--save" ]; then
SAVE_FLAG=true
fi
elif [ "$ARG3" = "-s" ] || [ "$ARG3" = "--save" ]; then
SAVE_FLAG=true
fi
elif [ "$ARG2" = "-s" ] || [ "$ARG2" = "--save" ]; then
# Just save, no value change
MONITOR="${ARG3:-}"
if [ -z "$MONITOR" ]; then
# Save all monitors
bash "${SCRIPT_DIR}/scripts/brightness_list.sh" >"${BRIGHTNESS_SAVE_FILE}.tmp" 2>/dev/null || {
echo "Warning: Could not query current brightness"
}
if [ -f "${BRIGHTNESS_SAVE_FILE}.tmp" ]; then
while IFS=: read -r name bright method; do
if [ -n "$name" ] && [ -n "$bright" ]; then
echo "${name}:${bright}"
fi
done <"${BRIGHTNESS_SAVE_FILE}.tmp" >"$BRIGHTNESS_SAVE_FILE"
rm -f "${BRIGHTNESS_SAVE_FILE}.tmp"
echo "Saved current brightness for all monitors"
fi
else
# Save specific monitor
CURRENT_LINE=$(bash "${SCRIPT_DIR}/scripts/brightness_list.sh" 2>/dev/null | grep "^${MONITOR}:")
if [ -z "$CURRENT_LINE" ]; then
echo "Error: Monitor $MONITOR not found"
exit 1
fi
CURRENT=$(echo "$CURRENT_LINE" | cut -d: -f2)
if [ -f "$BRIGHTNESS_SAVE_FILE" ]; then
grep -v "^${MONITOR}:" "$BRIGHTNESS_SAVE_FILE" >"${BRIGHTNESS_SAVE_FILE}.tmp" 2>/dev/null || true
echo "${MONITOR}:${CURRENT}" >>"${BRIGHTNESS_SAVE_FILE}.tmp"
mv "${BRIGHTNESS_SAVE_FILE}.tmp" "$BRIGHTNESS_SAVE_FILE"
else
echo "${MONITOR}:${CURRENT}" >"$BRIGHTNESS_SAVE_FILE"
fi
echo "Saved current brightness for $MONITOR (${CURRENT}%)"
fi
exit 0
else
echo "Error: Invalid brightness value. Must be 0-100 or +/-delta."
echo "Run 'ambxst help' for usage information"
exit 1
fi
# Handle relative mode - use IPC adjust function directly
if [ "$RELATIVE_MODE" = true ]; then
# Convert delta to 0-1 range
NORMALIZED_DELTA=$(awk "BEGIN {printf \"%.2f\", $RELATIVE_DELTA / 100}")
if [ -z "$MONITOR" ]; then
qs ipc --pid "$PID" call brightness adjust "$NORMALIZED_DELTA" "" 2>/dev/null || {
echo "Error: Could not adjust brightness"
exit 1
}
echo "Adjusted brightness by ${RELATIVE_DELTA}% for all monitors"
else
qs ipc --pid "$PID" call brightness adjust "$NORMALIZED_DELTA" "$MONITOR" 2>/dev/null || {
echo "Error: Could not adjust brightness for $MONITOR"
exit 1
}
echo "Adjusted brightness by ${RELATIVE_DELTA}% for $MONITOR"
fi
exit 0
fi
# Validate brightness range
if [ "$VALUE" -lt 0 ] || [ "$VALUE" -gt 100 ]; then
echo "Error: Brightness must be between 0 and 100"
exit 1
fi
# Save current brightness if requested
if [ "$SAVE_FLAG" = true ]; then
if [ -z "$MONITOR" ]; then
# Save all monitors - we need to get current brightness
# For simplicity, we'll use a helper script to query current brightness
bash "${SCRIPT_DIR}/scripts/brightness_list.sh" >"${BRIGHTNESS_SAVE_FILE}.tmp" 2>/dev/null || {
echo "Warning: Could not query current brightness"
}
# Convert format from name:brightness:method to name:brightness
if [ -f "${BRIGHTNESS_SAVE_FILE}.tmp" ]; then
while IFS=: read -r name bright method; do
if [ -n "$name" ] && [ -n "$bright" ]; then
echo "${name}:${bright}"
fi
done <"${BRIGHTNESS_SAVE_FILE}.tmp" >"$BRIGHTNESS_SAVE_FILE"
rm -f "${BRIGHTNESS_SAVE_FILE}.tmp"
echo "Saved current brightness for all monitors"
fi
else
# Save specific monitor
CURRENT_LINE=$(bash "${SCRIPT_DIR}/scripts/brightness_list.sh" 2>/dev/null | grep "^${MONITOR}:")
if [ -z "$CURRENT_LINE" ]; then
echo "Error: Monitor $MONITOR not found"
exit 1
fi
CURRENT=$(echo "$CURRENT_LINE" | cut -d: -f2)
# Update or append to save file
if [ -f "$BRIGHTNESS_SAVE_FILE" ]; then
grep -v "^${MONITOR}:" "$BRIGHTNESS_SAVE_FILE" >"${BRIGHTNESS_SAVE_FILE}.tmp" 2>/dev/null || true
echo "${MONITOR}:${CURRENT}" >>"${BRIGHTNESS_SAVE_FILE}.tmp"
mv "${BRIGHTNESS_SAVE_FILE}.tmp" "$BRIGHTNESS_SAVE_FILE"
else
echo "${MONITOR}:${CURRENT}" >"$BRIGHTNESS_SAVE_FILE"
fi
echo "Saved current brightness for $MONITOR (${CURRENT}%)"
fi
fi
# Set brightness
NORMALIZED=$(awk "BEGIN {printf \"%.2f\", $VALUE / 100}")
if [ -z "$MONITOR" ]; then
# Set all monitors
qs ipc --pid "$PID" call brightness set "$NORMALIZED" "" 2>/dev/null || {
echo "Error: Could not set brightness"
exit 1
}
echo "Set brightness to ${VALUE}% for all monitors"
else
# Set specific monitor
qs ipc --pid "$PID" call brightness set "$NORMALIZED" "$MONITOR" 2>/dev/null || {
echo "Error: Could not set brightness for $MONITOR"
exit 1
}
echo "Set brightness to ${VALUE}% for $MONITOR"
fi
;;
version | -v | --version)
echo "Ambxst $(cat "${SCRIPT_DIR}/version")"
;;
install)
TARGET="${2:-}"
if [ "$TARGET" = "hyprland" ]; then
HYPR_CONF="$HOME/.config/hypr/hyprland.conf"
# Create directory if needed
mkdir -p "$HOME/.config/hypr"
append_ambxst_hyprland_block "$HYPR_CONF"
else
echo "Error: Unknown target '$TARGET'. Supported: hyprland"
exit 1
fi
;;
remove)
TARGET="${2:-}"
if [ "$TARGET" = "hyprland" ]; then
HYPR_CONF="$HOME/.config/hypr/hyprland.conf"
remove_ambxst_hyprland_block "$HYPR_CONF"
else
echo "Error: Unknown target '$TARGET'. Supported: hyprland"
exit 1
fi
;;
goodbye)
echo "Uninstalling Ambxst..."
read -p "Are you sure? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Uninstall aborted."
exit 0
fi
if [ -f /etc/NIXOS ]; then
if nix profile list 2>/dev/null | grep -q "Ambxst"; then
echo "Removing from nix profile..."
nix profile remove Ambxst
elif command -v ambxst >/dev/null 2>&1; then
echo "Ambxst was declared in this system. Please remove it from your configuration in order to uninstall."
else
echo "Ambxst is not installed."
fi
exit 0
fi
read -p "Remove configuration files? (y/N): " -n 1 -r
echo
REMOVE_CONFIG=false
if [[ $REPLY =~ ^[Yy]$ ]]; then
REMOVE_CONFIG=true
fi
rm -rf "$HOME/.local/src/ambxst"
rm -rf "$HOME/.local/share/ambxst"
rm -rf "$HOME/.local/state/ambxst"
if [ "$REMOVE_CONFIG" = true ]; then
rm -rf "$HOME/.config/ambxst"
echo "Configuration files removed."
fi
echo "Ambxst uninstalled. :("
;;
help | --help | -h)
show_help
;;
"")
# Run daemon priority script (backgrounded to not block startup)
bash "${SCRIPT_DIR}/scripts/daemon_priority.sh" &
# Set QS_ICON_THEME environment variable
if command -v gsettings >/dev/null 2>&1; then
export QS_ICON_THEME=$(gsettings get org.gnome.desktop.interface icon-theme | tr -d "'")
else
echo "DEBUG: gsettings not found in PATH" >&2
fi
# Force Qt6CT
export QT_QPA_PLATFORMTHEME=qt6ct
# Cache this script's PID before exec (for fast PID lookups in future CLI calls)
echo $$ >/tmp/ambxst.pid
# Launch QuickShell with the main shell.qml
# If NIXGL_BIN is set (NixOS/Nix setup), use it. Otherwise, just run qs directly.
if [ -n "$NIXGL_BIN" ]; then
exec "$NIXGL_BIN" "$QS_BIN" -p "${SCRIPT_DIR}/shell.qml"
else
exec qs -p "${SCRIPT_DIR}/shell.qml"
fi
;;
*)
echo "Error: Unknown command '$1'"
echo "Run 'ambxst help' for usage information"
exit 1
;;
esac