#!/usr/bin/env bash
. "$HOME/bin/x11-go"
# Place the pager, status bar and system try on the head given.
#
# The head given should be a name recognized by `gohead`. This also sets the
# "primary" head to be the one given.
place_head() {
local head_name="$1"
local x y w h
IFS=$'\t' read -r x y w h < <(gohead query "$head_name" | cut -f2-5)
# This is important because stalonetray bases its positioning on the primary
# RandR monitor.
gohead primary "$head_name" &> /dev/null
# Put the pointer on one of the monitors.
wingo-cmd '(HeadFocus 0)'
wingo-cmd '(MovePointerRelative 0 0)'
place_by_head_geometry "$x" "$y" "$w" "$h"
}
# Places the pager, status bar and system tray based on the head geometry
# given.
#
# This routine doesn't know what head it's being place on, despite the fact
# that dzen2 and stalonetray purportedly support positioning based on RandR
# head number. The problem is that they each use different systems. So we just
# position everything ourselves in the context of the root window geometry.
place_by_head_geometry() {
local mx="$1"
local my="$2"
local mwidth="$3"
local mheight="$4"
local height=35
local font_size=14
local status_width=450
local tray_width=100
if [[ $mheight -gt 1200 ]] || is-media; then
height=50
font_size=21
status_width=650
tray_width=300
fi
local extended_status_width=$((status_width + tray_width))
local pager_width=$((mwidth - extended_status_width))
local y=$((mheight - height + my))
dzen-wingo-workspaces "$font_size" "$mx" "$y" "$pager_width" "$height" &
dzen-laptop-status \
"$font_size" \
"$((mx + pager_width))" "$y" "$extended_status_width" "$height" &
# stalonetray has weird positioning. It's relative to the *primary* RandR
# head, which we set in `pl`. So we give it -0-0 to indicate the
# bottom right corner.
my-stalonetray "0" "0" "$height" &
}
has_arg() {
local search="$1"
shift
local arg
for arg in "$@"; do
if [ "$search" = "$arg" ]; then
return 0
fi
done
return 1
}
# Actually run the necessary `xrandr` command.
if ! gohead set "$@" &> /dev/null; then
exit 1
fi
# Kill any pager stuff we had.
killall dzen-workspaces &> /dev/null
killall dzen2 &> /dev/null
killall stalonetray &> /dev/null
# Look for a special head to place our stuff on first.
for special in main middle; do
if has_arg "$special" "$@"; then
place_head "$special"
exit 0
fi
done
# Otherwise fall back to the first one given.
place_head "$1"