forked from dillacorn/smtty
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmtty
More file actions
1471 lines (1276 loc) · 37.8 KB
/
smtty
File metadata and controls
1471 lines (1276 loc) · 37.8 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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# smtty - Steam Machine launcher (gamescope-only)
# Can be run from a bare TTY or from an X11/Wayland session (Hyprland, Sway, KDE, GNOME, etc.).
# Recommended install location:
# - $HOME/.local/bin/smtty (user)
# - /usr/local/bin/smtty (system-wide)
#
# Modes:
# Default:
# Interactive launcher for Steam Gamepad UI (Big Picture) inside gamescope.
#
# -O
# Interactively generate ready-to-paste Steam per-game launch options (nested gamescope, no Big Picture) and exit.
# Lets you choose:
# - gamescope with or without Steam integration (-e)
# - background/unfocused FPS limit (-o, from saved config)
# - flatpak vs native Steam prefix
#
# -n
# New interactive setup for smtty config. Ignores any existing config.
#
# -l
# Use last saved smtty config immediately (no questions).
# With -l, smtty never kills Steam unless you also pass -S.
#
# -p
# Print saved config summary and exit.
#
# -k
# Kill any running gamescope processes and exit.
#
# -d
# Detach gamescope (run in background, return to shell).
#
# -S
# Kill any running Steam client before launching gamescope (no prompt).
set -euo pipefail
# ---------------- colors / logging ----------------
RED=$'\033[0;31m'
YELLOW=$'\033[1;33m'
GREEN=$'\033[0;32m'
BLUE=$'\033[0;34m'
NC=$'\033[0m'
print_error() {
printf '%s[ERROR]%s %s\n' "$RED" "$NC" "$*" >&2
}
print_warn() {
printf '%s[WARN]%s %s\n' "$YELLOW" "$NC" "$*"
}
print_info() {
printf '%s[INFO]%s %s\n' "$BLUE" "$NC" "$*"
}
print_success() {
printf '%s[OK]%s %s\n' "$GREEN" "$NC" "$*"
}
# ---------------- Steam command (Gamepad UI) ----------------
# Optional override:
# STEAM_CMD_OVERRIDE='steam -gamepadui -nochatui'
if [[ "${STEAM_CMD_OVERRIDE-}" != "" ]]; then
read -ra STEAM_CMD_ARR <<<"$STEAM_CMD_OVERRIDE"
else
STEAM_CMD_ARR=(steam -gamepadui)
fi
# Steam installation type (set during detection)
STEAM_TYPE="" # "native" or "flatpak"
# Session mode: "kms" (bare VT) or "nested" (X11/Wayland compositor)
SESSION_MODE="kms"
# ---------------- config paths ----------------
CONFIG_DIR="$HOME/.config/smtty"
CONFIG_FILE="$CONFIG_DIR/config"
AUDIO_STATE_FILE="$CONFIG_DIR/audio_state"
# ---------------- state ----------------
DRM_SHORT=""
DRM_NATIVE_MODE=""
NATIVE_W=0
NATIVE_H=0
GAME_W=0
GAME_H=0
STRETCH_FLAG="none" # "none" | "stretch"
G_FPS=0 # gamescope -r (target display refresh, Hz; 0 = unlimited)
G_FPS_BG=0 # gamescope -o (unfocused refresh, Hz; 0 = disabled / omit -o)
# VRR / HDR / cursor flags
ADAPTIVE_SYNC=0 # 0/1 -> --adaptive-sync
ENABLE_GAMESCOPE_HDR=0 # 0/1 -> --hdr-enabled/ITM
GAMESCOPE_HDR_NITS=1000 # used with --hdr-itm-target-nits
FORCE_GRAB_CURSOR=0 # 0/1 -> --force-grab-cursor
# PipeWire debug control for gamescope
# "inherit" = do not set PIPEWIRE_DEBUG (respect system / environment)
# "0" = errors only (quiet)
# "1" = errors + warnings
# "2" = info
# "3" = verbose debug
PIPEWIRE_DEBUG_MODE="2"
# LD_PRELOAD handling for gamescope/Steam
# "inherit" = do not modify LD_PRELOAD
# "clear" = export LD_PRELOAD="" before launching gamescope
LD_PRELOAD_MODE="clear"
# Temporary audio switching (PulseAudio / PipeWire via pactl)
AUDIO_SWITCH_ENABLED=0 # 0/1: switch default sink while gamescope runs
AUDIO_SINK_NAME="" # sink name to use when AUDIO_SWITCH_ENABLED=1
MISSING_CMDS=()
HAVE_CONFIG=0
# flags
FLAG_HELP=0
FLAG_NEW=0
FLAG_LAST=0
FLAG_PRINT=0
FLAG_KILL=0
FLAG_DETACH=0
FLAG_LAUNCH_OPTS=0 # -O: generate Steam launch options
FLAG_KILL_STEAM=0 # -S: kill Steam client before launching (no prompt)
# ---------------- helpers ----------------
require_cmd() {
local cmd=$1
if ! command -v "$cmd" >/dev/null 2>&1; then
MISSING_CMDS+=("$cmd")
fi
}
prompt_select() {
local prompt=$1
shift
local count=$#
local i=1
local opt
for opt in "$@"; do
printf ' [%d] %s\n' "$i" "$opt"
i=$((i + 1))
done
local sel
while :; do
read -rp "$prompt " sel || exit 1
if [[ ! "$sel" =~ ^[0-9]+$ ]]; then
echo "Enter a number."
continue
fi
if (( sel >= 1 && sel <= count )); then
REPLY=$sel
return 0
fi
echo "Out of range."
done
}
prompt_select_default() {
local prompt=$1
local default_index=$2
shift 2
local count=$#
local i=1
local opt
for opt in "$@"; do
printf ' [%d] %s\n' "$i" "$opt"
i=$((i + 1))
done
local sel
while :; do
read -rp "$prompt [$default_index]: " sel || exit 1
if [[ -z "$sel" ]]; then
sel=$default_index
fi
if [[ ! "$sel" =~ ^[0-9]+$ ]]; then
echo "Enter a number."
continue
fi
if (( sel >= 1 && sel <= count )); then
REPLY=$sel
return 0
fi
echo "Out of range."
done
}
read_default() {
local prompt=$1
local def=$2
local val
read -rp "$prompt [$def]: " val || exit 1
if [[ -z "$val" ]]; then
printf '%s\n' "$def"
else
printf '%s\n' "$val"
fi
}
print_help() {
cat <<EOF
smtty - Steam Machine launcher (gamescope-only)
Usage:
smtty [options]
Options:
-h Show this help and exit.
-n New interactive setup, ignore saved config.
-l Use last saved config immediately (no questions).
With -l, smtty never kills Steam unless you also pass -S.
-p Print saved config summary and exit.
-k Kill any running gamescope processes and exit.
-d Detach: start gamescope/Steam in the background and return immediately.
-S Kill any running Steam client before launching gamescope (no prompt).
-O Interactively generate ready-to-paste Steam per-game launch options and exit.
Environment:
STEAM_CMD_OVERRIDE='steam -gamepadui'
Override the Steam command used (default is auto-chosen native/flatpak).
PIPEWIRE_DEBUG
If smtty PipeWire mode is "inherit", this is passed through unchanged.
Otherwise, smtty overrides it with the selected numeric level.
LD_PRELOAD
If smtty LD_PRELOAD mode is "inherit", this is passed through unchanged.
If mode is "clear", smtty exports LD_PRELOAD="" before gamescope.
Config:
$CONFIG_FILE
Stores DRM output, resolution, stretch flag, gamescope display rate,
unfocused background rate, VRR / HDR settings, PipeWire debug level,
LD_PRELOAD handling, Steam type, and optional audio sink switching.
Notes:
- Can be run from a bare TTY (KMS mode) or from inside a compositor
(Hyprland, Sway, KDE, GNOME, etc.). Resolution and scaling are always
applied via gamescope runtime flags, not permanent display config writes.
- By default, smtty will always ask whether to use last settings when
a config exists, unless you explicitly pass -l.
EOF
}
audio_sink_display_name() {
local name=$1
if [[ -z "$name" ]]; then
printf '%s\n' ""
return 0
fi
if ! command -v pactl >/dev/null 2>&1; then
printf '%s\n' "$name"
return 0
fi
local desc
desc=$(pactl list sinks 2>/dev/null | awk -v target="$name" '
$1 == "Name:" {
gsub("\r","")
current = $2
}
current == target && $1 == "Description:" {
$1 = ""
sub(/^ +/, "")
gsub("\r","")
print
exit
}
current == target && $1 == "device.description" && $2 == "=" {
out = ""
for (i = 3; i <= NF; ++i) {
if (i > 3) out = out " "
out = out $i
}
gsub("\r","", out)
print out
exit
}
current == target && $1 == "node.description" && $2 == "=" {
out = ""
for (i = 3; i <= NF; ++i) {
if (i > 3) out = out " "
out = out $i
}
gsub("\r","", out)
print out
exit
}
')
if [[ -n "$desc" ]]; then
desc=${desc#\"}
desc=${desc%\"}
printf '%s\n' "$desc"
else
printf '%s\n' "$name"
fi
}
print_config_summary() {
if (( HAVE_CONFIG == 0 )); then
echo "No saved config at $CONFIG_FILE"
return
fi
echo "Saved smtty config ($CONFIG_FILE):"
echo " Steam type: ${STEAM_TYPE:-native}"
echo " DRM output: $DRM_SHORT"
echo " Native mode: $DRM_NATIVE_MODE"
echo " Game resolution: ${GAME_W}x${GAME_H}"
echo " Stretch flag: $STRETCH_FLAG"
echo " gamescope display rate: $G_FPS Hz (0 = unlimited, -r)"
echo " gamescope unfocused rate: $G_FPS_BG Hz (0 = disabled, -o omitted)"
echo " gamescope adaptive-sync: $ADAPTIVE_SYNC"
echo " gamescope HDR enabled: $ENABLE_GAMESCOPE_HDR"
echo " gamescope HDR target nits $GAMESCOPE_HDR_NITS"
echo " gamescope force grab: $FORCE_GRAB_CURSOR"
echo " PipeWire debug mode: $PIPEWIRE_DEBUG_MODE (inherit or 0-3)"
echo " LD_PRELOAD mode: ${LD_PRELOAD_MODE:-clear} (inherit or clear)"
echo " Audio switch enabled: $AUDIO_SWITCH_ENABLED"
if (( AUDIO_SWITCH_ENABLED )); then
local sink_desc
sink_desc=$(audio_sink_display_name "$AUDIO_SINK_NAME")
echo " Audio sink name: ${AUDIO_SINK_NAME:-"(unset)"}"
echo " Audio sink description: ${sink_desc:-"(unresolved)"}"
fi
}
save_config() {
mkdir -p "$CONFIG_DIR"
cat >"$CONFIG_FILE" <<EOF
DRM_SHORT=$DRM_SHORT
DRM_NATIVE_MODE=$DRM_NATIVE_MODE
NATIVE_W=$NATIVE_W
NATIVE_H=$NATIVE_H
GAME_W=$GAME_W
GAME_H=$GAME_H
STRETCH_FLAG=$STRETCH_FLAG
G_FPS=$G_FPS
G_FPS_BG=$G_FPS_BG
STEAM_TYPE=$STEAM_TYPE
ADAPTIVE_SYNC=$ADAPTIVE_SYNC
ENABLE_GAMESCOPE_HDR=$ENABLE_GAMESCOPE_HDR
GAMESCOPE_HDR_NITS=$GAMESCOPE_HDR_NITS
FORCE_GRAB_CURSOR=$FORCE_GRAB_CURSOR
PIPEWIRE_DEBUG_MODE=$PIPEWIRE_DEBUG_MODE
LD_PRELOAD_MODE=$LD_PRELOAD_MODE
AUDIO_SWITCH_ENABLED=$AUDIO_SWITCH_ENABLED
AUDIO_SINK_NAME=$AUDIO_SINK_NAME
EOF
}
load_config() {
if [[ -f "$CONFIG_FILE" ]]; then
# shellcheck source=/dev/null
. "$CONFIG_FILE"
HAVE_CONFIG=1
fi
PIPEWIRE_DEBUG_MODE=${PIPEWIRE_DEBUG_MODE:-2}
FORCE_GRAB_CURSOR=${FORCE_GRAB_CURSOR:-0}
LD_PRELOAD_MODE=${LD_PRELOAD_MODE:-clear}
G_FPS_BG=${G_FPS_BG:-0}
AUDIO_SWITCH_ENABLED=${AUDIO_SWITCH_ENABLED:-0}
AUDIO_SINK_NAME=${AUDIO_SINK_NAME:-""}
}
detect_session_mode() {
if [[ -n "${WAYLAND_DISPLAY-}" || -n "${DISPLAY-}" ]]; then
SESSION_MODE="nested"
else
SESSION_MODE="kms"
fi
}
# ---------------- output / resolution selection ----------------
choose_drm_output() {
echo "Detected *connected* DRM outputs:"
echo
local entries=()
local idx=1
local status_path status dir modes_path short native_line
for status_path in /sys/class/drm/card*-*/status; do
[[ -f "$status_path" ]] || continue
status=$(<"$status_path")
[[ "$status" == "connected" ]] || continue
dir=${status_path%/status}
modes_path="$dir/modes"
[[ -f "$modes_path" ]] || continue
short=${dir##*/}
short=${short#card*-}
native_line="unknown"
local max_area=0
local mline mw mh area
while IFS= read -r mline; do
[[ -z "$mline" ]] && continue
if [[ "$mline" =~ ([0-9]+)x([0-9]+) ]]; then
mw=${BASH_REMATCH[1]}
mh=${BASH_REMATCH[2]}
area=$(( mw * mh ))
if (( area > max_area )); then
max_area=$area
native_line="$mline"
fi
fi
done <"$modes_path"
entries+=("$short:$native_line")
printf ' [%d] %s (native: %s)\n' "$idx" "$short" "$native_line"
idx=$((idx + 1))
done
if ((${#entries[@]} == 0)); then
print_error "No *connected* DRM outputs found under /sys/class/drm."
exit 1
fi
local choice
while :; do
read -rp "Select output [1-${#entries[@]}]: " choice || exit 1
if [[ ! "$choice" =~ ^[0-9]+$ ]]; then
echo "Enter a number."
continue
fi
if (( choice >= 1 && choice <= ${#entries[@]} )); then
break
fi
echo "Out of range."
done
local chosen=${entries[choice-1]}
DRM_SHORT=${chosen%%:*}
DRM_NATIVE_MODE=${chosen#*:}
local base=${DRM_NATIVE_MODE%%@*}
if [[ "$base" =~ ^([0-9]+)x([0-9]+)$ ]]; then
NATIVE_W=${BASH_REMATCH[1]}
NATIVE_H=${BASH_REMATCH[2]}
else
print_error "Failed to parse native mode \"$DRM_NATIVE_MODE\"; expected WxH[@Hz]."
exit 1
fi
echo
echo "Using DRM output: $DRM_SHORT (native ${NATIVE_W}x${NATIVE_H})"
echo "Session mode: $SESSION_MODE (kms = bare VT, nested = under compositor)"
echo
if [[ "$SESSION_MODE" == "nested" ]]; then
echo "Note: running under a compositor. gamescope will be nested as a window"
echo " or fullscreen surface. Physical monitor resolution is not changed,"
echo " but gamescope still controls internal and outer resolutions/scaling."
echo
fi
}
choose_resolution_profile() {
GAME_W=$NATIVE_W
GAME_H=$NATIVE_H
STRETCH_FLAG="none"
local custom_profile=0
if [[ $NATIVE_W -eq 1920 && $NATIVE_H -eq 1080 ]]; then
echo "Resolution profile for 1080p 16:9:"
prompt_select_default "Choose profile" 1 \
"Native 1920x1080" \
"1352x1080 (4:3 base)" \
"1680x1050 (16:10 base)" \
"Custom"
case $REPLY in
1) GAME_W=1920; GAME_H=1080 ;;
2) GAME_W=1352; GAME_H=1080 ;;
3) GAME_W=1680; GAME_H=1050 ;;
4) custom_profile=1 ;;
esac
elif [[ $NATIVE_W -eq 2560 && $NATIVE_H -eq 1440 ]]; then
echo "Resolution profile for 1440p 16:9:"
prompt_select_default "Choose profile" 1 \
"Native 2560x1440" \
"1920x1440 (4:3 base)" \
"1920x1200 (16:10 base)" \
"Custom"
case $REPLY in
1) GAME_W=2560; GAME_H=1440 ;;
2) GAME_W=1920; GAME_H=1440 ;;
3) GAME_W=1920; GAME_H=1200 ;;
4) custom_profile=1 ;;
esac
elif [[ $NATIVE_W -eq 3840 && $NATIVE_H -eq 2160 ]]; then
echo "Resolution profile for 4K 16:9:"
prompt_select_default "Choose profile" 1 \
"Native 3840x2160" \
"2560x1440 (downscale base)" \
"1920x1440 (4:3 base)" \
"1920x1200 (16:10 base)" \
"Custom"
case $REPLY in
1) GAME_W=3840; GAME_H=2160 ;;
2) GAME_W=2560; GAME_H=1440 ;;
3) GAME_W=1920; GAME_H=1440 ;;
4) GAME_W=1920; GAME_H=1200 ;;
5) custom_profile=1 ;;
esac
fi
echo
echo "Scaling / stretch mode for outer ${NATIVE_W}x${NATIVE_H}:"
prompt_select_default "How should gamescope scale the game image" 1 \
"No stretch (preserve aspect, letterbox/pillarbox)" \
"Stretch to fill (gamescope -S stretch)"
case $REPLY in
1) STRETCH_FLAG="none" ;;
2) STRETCH_FLAG="stretch" ;;
*) STRETCH_FLAG="none" ;;
esac
echo
local prompt_label
if (( custom_profile )); then
prompt_label="Custom GAME resolution WxH (gamescope -w/-h)"
else
prompt_label="Game resolution WxH (gamescope -w/-h)"
fi
local default_str="${GAME_W}x${GAME_H}"
local resp
resp=$(read_default "$prompt_label" "$default_str")
if [[ "$resp" =~ ^([0-9]+)x([0-9]+)$ ]]; then
GAME_W=${BASH_REMATCH[1]}
GAME_H=${BASH_REMATCH[2]}
else
echo "Invalid format; keeping $default_str."
fi
echo
echo "Game resolution: ${GAME_W}x${GAME_H}"
echo "Stretch flag: ${STRETCH_FLAG}"
echo
}
choose_gamescope_rate() {
local val
val=$(read_default "Target display refresh for gamescope (-r, Hz, 0 = unlimited)" "$G_FPS")
if [[ ! "$val" =~ ^[0-9]+$ ]]; then
echo "Invalid value; using 0."
val=0
fi
G_FPS=$val
echo
local raw
read -rp "Nested unfocused refresh (-o, Hz, empty = disabled): " raw || exit 1
if [[ -z "$raw" ]]; then
G_FPS_BG=0
elif [[ "$raw" =~ ^[0-9]+$ ]]; then
G_FPS_BG=$raw
else
echo "Invalid value; disabling -o."
G_FPS_BG=0
fi
echo
}
choose_vrr_hdr_and_steamos() {
echo "Optional gamescope features:"
# Strict 0/1 validation for VRR
local vrr
while :; do
vrr=$(read_default "Enable gamescope adaptive sync / VRR? (0 = no, 1 = yes)" "$ADAPTIVE_SYNC")
case "$vrr" in
0|1)
ADAPTIVE_SYNC=$vrr
break
;;
*)
echo "Enter 0 or 1."
;;
esac
done
# Strict 0/1 validation for HDR
local hdr
while :; do
hdr=$(read_default "Enable gamescope HDR output? (0 = no, 1 = yes)" "$ENABLE_GAMESCOPE_HDR")
case "$hdr" in
0|1)
break
;;
*)
echo "Enter 0 or 1."
;;
esac
done
if [[ "$hdr" == "1" ]]; then
ENABLE_GAMESCOPE_HDR=1
local nits
nits=$(read_default "HDR ITM target nits (gamescope --hdr-itm-target-nits)" "$GAMESCOPE_HDR_NITS")
if [[ "$nits" =~ ^[0-9]+$ ]]; then
GAMESCOPE_HDR_NITS=$nits
else
echo "Invalid nits value; keeping $GAMESCOPE_HDR_NITS."
fi
else
ENABLE_GAMESCOPE_HDR=0
fi
echo
echo "Cursor grab:"
echo " Mostly useful on bare TTY (KMS). In nested sessions it can make"
echo " mouse input worse if games and overlays already manage grabs."
# Strict 0/1 validation for cursor grab
local fg
while :; do
fg=$(read_default "Force gamescope cursor grab? (0 = no, 1 = yes)" "$FORCE_GRAB_CURSOR")
case "$fg" in
0|1)
FORCE_GRAB_CURSOR=$fg
break
;;
*)
echo "Enter 0 or 1."
;;
esac
done
echo
}
choose_pipewire_debug() {
echo "PipeWire / gamescope log verbosity:"
echo " This controls PIPEWIRE_DEBUG for the gamescope process only."
echo " Lower values reduce spam like 'pipewire: warning: out of buffers'."
echo
echo " [1] Inherit system default (do not set PIPEWIRE_DEBUG)"
echo " [2] Quiet: errors only [PIPEWIRE_DEBUG=0]"
echo " [3] Errors + warnings [PIPEWIRE_DEBUG=1]"
echo " [4] Info (more detail) [PIPEWIRE_DEBUG=2]"
echo " [5] Verbose debug [PIPEWIRE_DEBUG=3]"
local default_choice
case "$PIPEWIRE_DEBUG_MODE" in
inherit) default_choice=1 ;;
0) default_choice=2 ;;
1) default_choice=3 ;;
2) default_choice=4 ;;
3) default_choice=5 ;;
*) default_choice=4 ;;
esac
local sel
sel=$(read_default "Choose PipeWire debug level (1-5)" "$default_choice")
if [[ ! "$sel" =~ ^[1-5]$ ]]; then
echo "Invalid selection; keeping current PipeWire debug mode: $PIPEWIRE_DEBUG_MODE"
else
case "$sel" in
1) PIPEWIRE_DEBUG_MODE="inherit" ;;
2) PIPEWIRE_DEBUG_MODE="0" ;;
3) PIPEWIRE_DEBUG_MODE="1" ;;
4) PIPEWIRE_DEBUG_MODE="2" ;;
5) PIPEWIRE_DEBUG_MODE="3" ;;
esac
fi
echo
echo "PipeWire debug mode set to: $PIPEWIRE_DEBUG_MODE"
echo " inherit = smtty leaves PIPEWIRE_DEBUG alone"
echo " 0 = only errors (very quiet)"
echo " 1 = errors + warnings"
echo " 2 = info"
echo " 3 = verbose debug"
echo
}
choose_ld_preload_mode() {
echo "LD_PRELOAD handling for gamescope / Steam:"
echo " Clearing LD_PRELOAD avoids inherited overlays/shims (MangoHud, vkBasalt,"
echo " etc.) from injecting into gamescope or the game."
echo
echo " [1] Inherit current LD_PRELOAD (do nothing)"
echo " [2] Clear LD_PRELOAD for gamescope and Steam"
local default_choice
case "$LD_PRELOAD_MODE" in
inherit) default_choice=1 ;;
clear) default_choice=2 ;;
*) default_choice=2 ;;
esac
local sel
sel=$(read_default "Choose LD_PRELOAD mode (1-2)" "$default_choice")
case "$sel" in
1) LD_PRELOAD_MODE="inherit" ;;
2) LD_PRELOAD_MODE="clear" ;;
*)
echo "Invalid selection; keeping current LD_PRELOAD mode: $LD_PRELOAD_MODE"
;;
esac
echo
echo "LD_PRELOAD mode set to: $LD_PRELOAD_MODE"
echo " inherit = pass through existing LD_PRELOAD"
echo " clear = export LD_PRELOAD=\"\" before gamescope"
echo
}
choose_audio_switch() {
echo "Temporary audio output switching (PulseAudio / PipeWire via pactl):"
if ! command -v pactl >/dev/null 2>&1; then
echo " pactl not found; audio device switching will be disabled."
AUDIO_SWITCH_ENABLED=0
AUDIO_SINK_NAME=""
echo
return
fi
# Always default to 0 (disabled) in the prompt, regardless of prior config.
local en
en=$(read_default "Enable automatic audio output switch while gamescope is running? (0 = no, 1 = yes)" 0)
if [[ "$en" == "1" ]]; then
AUDIO_SWITCH_ENABLED=1
else
AUDIO_SWITCH_ENABLED=0
AUDIO_SINK_NAME=""
echo
return
fi
mapfile -t sink_lines < <(pactl list short sinks 2>/dev/null || true)
if ((${#sink_lines[@]} == 0)); then
echo " No sinks found; disabling audio switching."
AUDIO_SWITCH_ENABLED=0
AUDIO_SINK_NAME=""
echo
return
fi
local -a sink_names=()
local -a sink_descs=()
local line name idx
for line in "${sink_lines[@]}"; do
# index name driver sample-rate channels format
read -r idx name _ <<<"$line"
if [[ -z "$name" ]]; then
continue
fi
sink_names+=("$name")
sink_descs+=("$(audio_sink_display_name "$name")")
done
if ((${#sink_names[@]} == 0)); then
echo " No valid sinks parsed; disabling audio switching."
AUDIO_SWITCH_ENABLED=0
AUDIO_SINK_NAME=""
echo
return
fi
echo
echo "Available audio outputs (PulseAudio / PipeWire sinks):"
local i
for ((i = 0; i < ${#sink_names[@]}; i++)); do
local desc="${sink_descs[i]}"
local raw="${sink_names[i]}"
if [[ -n "$desc" && "$desc" != "$raw" ]]; then
printf ' [%d] %s (%s)\n' "$((i + 1))" "$desc" "$raw"
else
printf ' [%d] %s\n' "$((i + 1))" "$raw"
fi
done
local choice
while :; do
read -rp "Choose sink to use while gamescope is running [1-${#sink_names[@]}]: " choice || exit 1
if [[ "$choice" =~ ^[0-9]+$ ]] && (( choice >= 1 && choice <= ${#sink_names[@]} )); then
break
fi
echo "Enter a number between 1 and ${#sink_names[@]}."
done
AUDIO_SINK_NAME=${sink_names[choice-1]}
echo
echo "Audio switcher will use:"
echo " Description: ${sink_descs[choice-1]}"
echo " Name: ${AUDIO_SINK_NAME}"
echo
}
get_default_sink() {
local current=""
current=$(pactl get-default-sink 2>/dev/null || true)
if [[ -z "$current" ]]; then
current=$(pactl info 2>/dev/null | awk -F': ' '/Default Sink:/ {print $2; exit}')
fi
printf '%s\n' "$current"
}
audio_switch_can_run() {
if (( AUDIO_SWITCH_ENABLED != 1 )); then
return 1
fi
if [[ -z "$AUDIO_SINK_NAME" ]]; then
return 1
fi
if ! command -v pactl >/dev/null 2>&1; then
return 1
fi
return 0
}
audio_switch_start_for_pid() {
local pid=$1
if ! audio_switch_can_run; then
return 0
fi
local original
original=$(get_default_sink)
if [[ -z "$original" ]]; then
print_warn "Could not determine current default sink; skipping audio switch."
return 0
fi
if [[ "$original" == "$AUDIO_SINK_NAME" ]]; then
return 0
fi
if ! pactl list short sinks 2>/dev/null | awk '{print $2}' | grep -qx "$AUDIO_SINK_NAME"; then
print_warn "Configured audio sink '$AUDIO_SINK_NAME' not found; skipping audio switch."
return 0
fi
print_info "Switching default audio sink: $original -> $AUDIO_SINK_NAME"
if ! pactl set-default-sink "$AUDIO_SINK_NAME" 2>/dev/null; then
print_warn "Failed to switch default sink; leaving audio unchanged."
return 0
fi
mkdir -p "$CONFIG_DIR"
cat >"$AUDIO_STATE_FILE" <<EOF
AUDIO_ACTIVE=1
AUDIO_ORIGINAL_SINK=$original
AUDIO_TARGET_SINK=$AUDIO_SINK_NAME
EOF
(
while kill -0 "$pid" 2>/dev/null; do
sleep 1
done
print_info "Restoring default audio sink to $original"
pactl set-default-sink "$original" >/dev/null 2>&1 || true
rm -f "$AUDIO_STATE_FILE"
) &
}
audio_restore_from_state() {
if ! command -v pactl >/dev/null 2>&1; then
return 0
fi
if [[ ! -f "$AUDIO_STATE_FILE" ]]; then
return 0
fi
# shellcheck source=/dev/null
. "$AUDIO_STATE_FILE"
local original="${AUDIO_ORIGINAL_SINK:-}"
local target="${AUDIO_TARGET_SINK:-}"
if [[ -z "$original" || -z "$target" ]]; then
rm -f "$AUDIO_STATE_FILE"
return 0
fi
local current
current=$(get_default_sink)
if [[ -z "$current" ]]; then
rm -f "$AUDIO_STATE_FILE"
return 0
fi
if [[ "$current" != "$target" ]]; then
# User changed output manually; do not override.
rm -f "$AUDIO_STATE_FILE"
return 0
fi
print_info "Restoring default audio sink to $original (via smtty -k)"
pactl set-default-sink "$original" >/dev/null 2>&1 || true
rm -f "$AUDIO_STATE_FILE"
}
print_steam_launch_options() {
if (( HAVE_CONFIG == 0 )); then
echo "No saved config; run 'smtty -n -O' first."
exit 1
fi
echo "Steam per-game launch options generator"
echo
echo "Enter your current Steam launch options for this game."
echo "Example: gamemoderun %command% -novid +fps_max 0 -high -dx12"
read -rp "Current launch options (empty = just %command%): " CURRENT_LAUNCH_OPTS || exit 1
if [[ -z "$CURRENT_LAUNCH_OPTS" ]]; then
CURRENT_LAUNCH_OPTS='%command%'
else
if [[ "$CURRENT_LAUNCH_OPTS" != *"%command%"* ]]; then
echo "Note: your launch options did not contain %command%; appending at the end."
CURRENT_LAUNCH_OPTS="$CURRENT_LAUNCH_OPTS %command%"
fi
fi
echo
# Decide whether to prefix with flatpak-spawn --host
local use_flatpak_prefix=0
local ans
if [[ "${STEAM_TYPE:-native}" == "flatpak" ]]; then
echo "smtty config detected Steam Flatpak."
read -rp "Prefix with 'flatpak-spawn --host' for this game? [Y/n]: " ans || exit 1
case "$ans" in
[nN]*) use_flatpak_prefix=0 ;;
*) use_flatpak_prefix=1 ;;
esac
else
read -rp "Is this game using Steam Flatpak? [y/N]: " ans || exit 1
case "$ans" in
[yY]*) use_flatpak_prefix=1 ;;
*) use_flatpak_prefix=0 ;;
esac
fi
echo
# Overlay / Steam integration choice (-e)
local use_steam_integration=0
echo "Steam overlay / integration mode for this game:"
prompt_select "Choose integration mode:" \
"No special Steam integration (no -e; more compatible, overlay may be weaker)" \
"Steam integration (-e; tighter overlay wiring, can break some setups)"
case "$REPLY" in
2) use_steam_integration=1 ;;
*) use_steam_integration=0 ;;
esac
echo
# Make sure SESSION_MODE matches the environment this launch line is for.
detect_session_mode
# Build gamescope args to match normal smtty behavior (minus -e).
local args=()
args+=(-f)
args+=(-W "$NATIVE_W" -H "$NATIVE_H")
args+=(-w "$GAME_W" -h "$GAME_H")
# In KMS, include explicit DRM output.
if [[ "$SESSION_MODE" == "kms" && -n "$DRM_SHORT" ]]; then
args+=(-O "$DRM_SHORT")
fi
# Foreground FPS limit.
if (( G_FPS > 0 )); then
args+=(-r "$G_FPS")
fi
# Background / unfocused FPS limit.
if (( G_FPS_BG > 0 )); then
args+=(-o "$G_FPS_BG")
fi
# Stretch mode.
if [[ "$STRETCH_FLAG" == "stretch" ]]; then
args+=(-S stretch)
fi