Skip to content

Instantly share code, notes, and snippets.

@hoelzro
Created May 21, 2019 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoelzro/01e2579ed651c17c49806e5b4d0eb8f6 to your computer and use it in GitHub Desktop.
Save hoelzro/01e2579ed651c17c49806e5b4d0eb8f6 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e -u
EXTERNAL_SCREEN='DP-2-1'
INTERNAL_SCREEN='eDP-1'
# XXX persist windows & pids to a file in case things go *real* wrong?
DRY_RUN=${DRY_RUN:-0}
function die() {
echo $1 >&2
exit 1
}
function active-display() {
xrandr | perl -nle 'print $1 if /^(\S+)\s+\bconnected\b\s+(?:primary\s+)?\d+x\d+/'
}
function restore-tags() {
quoted_filename=$(echo 'print(string.format("%q", arg[1]))' | lua - "$1")
awesome-client <<END_LUA
local filename = $quoted_filename
local iterate = require('awful.client').iterate
local f, err = io.open(filename, 'r')
if not f then
alert("couldn't open state file: " .. tostring(err))
return
end
for line in f:lines() do
local window_id, desktop = line:match '(0x%x+)%s+(%d+)'
if window_id and desktop then
window_id = tonumber(window_id)
local tag = tonumber(desktop) + 1
for c in iterate(function(c) return c.window == window_id end) do
c:move_to_tag(tags[1][tag])
end
end
end
f:close()
END_LUA
}
if ! xrandr | grep -q "$EXTERNAL_SCREEN connected" ; then
die "External monitor not connected!"
fi
current=$(active-display)
if [[ $DRY_RUN -ne 0 ]] ; then
echo "dry run - bailing out"
exit 0
fi
window_state=$(mktemp)
wmctrl -l > "$window_state"
if [[ $current == "$EXTERNAL_SCREEN" ]]; then
xrandr --output "$INTERNAL_SCREEN" --scale 0.5x0.5 --auto
xrandr --output "$EXTERNAL_SCREEN" --off || true # for some reason we get a BadMatch?
elif [[ $current == "$INTERNAL_SCREEN" ]] ; then
xrandr --output "$EXTERNAL_SCREEN" --auto
xrandr --output "$INTERNAL_SCREEN" --off
fi
sleep 3
echo 'awesome.restart()' | awesome-client || true # awesome won't reply, since it's restarting
sleep 3 # wait for awesome to set itself up
restore-tags "$window_state"
rm "$window_state"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment