Skip to content

Instantly share code, notes, and snippets.

@DerSaidin
Created December 13, 2020 17:51
Show Gist options
  • Save DerSaidin/c585b48c0b180f7eeb5e2423464a11d5 to your computer and use it in GitHub Desktop.
Save DerSaidin/c585b48c0b180f7eeb5e2423464a11d5 to your computer and use it in GitHub Desktop.
swaywm: Second monitor off on startup

My computer is connected to a monitor and a TV. I only use the TV as a screen some of the time. I want it to start disabled, and have a key bind to toggle it on.

In ~/.config/sway/config

# Toggle TV display (not usually in use)
bindsym $mod+t exec ~/.config/sway/output_toggle_name.sh $output-tv

# Configure monitors.
exec ~/.config/sway/startup.sh

~/.config/sway/startup.sh

#!/usr/bin/env bash

SWAY_CONFIG_DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

# `jq -e` will exit with exitcode=1 if final value is false.

# If the TV is the only output (start up with only TV plugged in), do not disable it.
if swaymsg -t get_outputs -r | jq -e 'length == 1' ; then {
    echo "Only 1 monitor"
    exit 0
}
fi

# Disable based on model, so it works the same plugged into any HDMI port.
# Find model from `swaymsg -t get_tree`
${SWAY_CONFIG_DIRECTORY}/output_disable_model.sh "SAMSUNG"

~/.config/sway/output_disable_model.sh

#!/usr/bin/env bash

OUTPUT_MODEL=$1

# `jq -e` will exit with exitcode=1 if final value is false.

# https://www.reddit.com/r/swaywm/comments/bxsp97/swaywm_hotplug_external_displaysmonitors_udev/

if swaymsg -t get_outputs -r | jq -e '.[] | select(.model=="'${OUTPUT_MODEL}'") | .active' ; then {
    OUTPUT_NAME=`swaymsg -t get_outputs -r | jq -e '.[] | select(.model=="'${OUTPUT_MODEL}'") | .name'`
    swaymsg output ${OUTPUT_NAME} disable
    echo "Disabled"
}
fi

~/.config/sway/output_toggle_name.sh

#!/usr/bin/env bash

SECONDARY_OUTPUT=$1

# `jq -e` will exit with exitcode=1 if final value is false.

# https://www.reddit.com/r/swaywm/comments/bxsp97/swaywm_hotplug_external_displaysmonitors_udev/

if swaymsg -t get_outputs -r | jq -e '.[] | select(.name=="'${SECONDARY_OUTPUT}'") | .active' ; then {
    swaymsg output ${SECONDARY_OUTPUT} disable
    echo "Disabled"
} else {
    swaymsg output ${SECONDARY_OUTPUT} enable
    echo "Enabled"
}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment