blob: 39e3367a18b348fca7bbdf19ec4138df43b27df9 (
plain) (
blame)
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
|
#!/bin/sh
# Display mode manager: laptop-off ↔ side-by-side.
# (no arg) toggle between modes (F7 / Super+x d).
# apply re-apply the saved mode (used by display-watcher after
# hotplug). First boot defaults to laptop-off.
# init force laptop-off; used by the hotplug watcher when a
# newly-plugged external should preempt whatever was saved.
#
# State is persisted as a sway-include file at $OUTPUTS_CONF so that
# sway's own config parser applies the layout natively on every reload
# — no side-by-side flash before override. The script also issues live
# swaymsg commands so toggles are immediate without needing a reload.
STATE_FILE="${XDG_RUNTIME_DIR:-/tmp}/display-mode"
OUTPUTS_CONF="${XDG_CONFIG_HOME:-$HOME/.config}/sway/outputs.conf"
write_conf() {
# $1 = mode (laptop-off | side-by-side | laptop-only)
# remaining args are the sway `output ...` directives, one per arg.
{
printf '# Auto-generated by display-toggle.sh; do not edit.\n'
printf '# Current mode: %s\n' "$1"
shift
for line in "$@"; do printf '%s\n' "$line"; done
} >"$OUTPUTS_CONF"
}
OUTPUTS=$(swaymsg -t get_outputs -r)
LAPTOP=$(echo "$OUTPUTS" | jq -r '[.[] | select(.name | test("^eDP")) | .name] | first // empty')
EXTERNAL=$(echo "$OUTPUTS" | jq -r '[.[] | select(.name | test("^eDP") | not) | .name] | first // empty')
if [ -z "$EXTERNAL" ]; then
# No external connected: make sure the laptop screen is on. This recovers
# from an earlier "laptop-off" state after the external was unplugged.
if [ -n "$LAPTOP" ]; then
swaymsg output "$LAPTOP" enable pos 0 0 || true
echo "laptop-only" >"$STATE_FILE"
write_conf "laptop-only" "output $LAPTOP enable pos 0 0"
fi
[ -z "${1:-}" ] && notify-send "Display" "No external display connected"
exit 0
fi
[ -z "$LAPTOP" ] && exit 0
LAPTOP_WIDTH=$(echo "$OUTPUTS" | jq -r ".[] | select(.name == \"$LAPTOP\") | .current_mode.width // .modes[0].width")
[ -z "$LAPTOP_WIDTH" ] && LAPTOP_WIDTH=1920
if [ "${1:-}" = "apply" ]; then
NEXT=$(cat "$STATE_FILE" 2>/dev/null || echo "laptop-off")
elif [ "${1:-}" = "init" ]; then
NEXT="laptop-off"
else
CURRENT=$(cat "$STATE_FILE" 2>/dev/null || echo "laptop-off")
case "$CURRENT" in
laptop-off) NEXT="side-by-side" ;;
*) NEXT="laptop-off" ;;
esac
fi
case "$NEXT" in
laptop-off)
swaymsg output "$LAPTOP" disable || true
swaymsg output "$EXTERNAL" enable || true
swaymsg workspace number 1 || true
echo "laptop-off" >"$STATE_FILE"
write_conf "laptop-off" \
"output $LAPTOP disable" \
"output $EXTERNAL enable"
[ -z "${1:-}" ] && notify-send "Display" "Laptop screen off"
;;
side-by-side)
swaymsg output "$LAPTOP" enable pos 0 0 || true
swaymsg output "$EXTERNAL" enable pos "$LAPTOP_WIDTH" 0 || true
echo "side-by-side" >"$STATE_FILE"
write_conf "side-by-side" \
"output $LAPTOP enable pos 0 0" \
"output $EXTERNAL enable pos $LAPTOP_WIDTH 0"
[ -z "${1:-}" ] && notify-send "Display" "Side by side"
;;
esac
|