Skip to content

Instantly share code, notes, and snippets.

@eslindsey
Created August 9, 2019 19:39
Show Gist options
  • Save eslindsey/70bbc0080e335b38836fab09d19686d0 to your computer and use it in GitHub Desktop.
Save eslindsey/70bbc0080e335b38836fab09d19686d0 to your computer and use it in GitHub Desktop.
Span an X window across all screens
#!/bin/bash
# USAGE
# span.sh command [arguments]
#
# DESCRIPTION
# Executes command, waits for a new window to appear, then stretches that window across the entire screen area.
#
# REQUIREMENTS
# wmctrl - for finding and controlling X windows
# xrandr - for detecting the size of the screens
# bc - for timing how long it takes a new window to appear
#
# EXAMPLES
# span.sh chromium-browser --kiosk http://lorempixel.com/1280/720
# span.sh chromium-browser --start-fullscreen http://lorempixel.com/1280/720
# Check to make sure we have a command to execute
if [ $# -lt 1 ]; then
echo "Usage: $0 command [arguments]"
echo "Executes command, waits for a new window to appear, then stretches that window across the entire screen area."
exit 1
fi
# Allocate some temporary files
WF1=`tempfile`
WF2=`tempfile`
WF3=`tempfile`
# Obtain initial list of windows, and execute command
wmctrl -l >$WF1
echo "$@"
"$@" &
# Wait for a new window (time out after 10 seconds)
echo "Waiting for a new window..."
i=0
while [ $i -le 40 ]
do
wmctrl -l >$WF2
grep -Fxvf "$WF1" "$WF2" >$WF3
if [ $? -eq 0 ]; then break; fi
sleep 0.25
i=$(( $i + 1 ))
done
rm "$WF1"
rm "$WF2"
if [ $i -gt 40 ]; then
rm "$WF3"
echo "New window took longer than 10 seconds, aborting."
exit 2
fi
# Extract the Window ID of the new window
WID=`cut -d' ' -f 1 "$WF3"`
TIME=$(bc <<<"scale=2; $i / 4")
echo "Found new window ID $WID after $TIME seconds"
rm "$WF3"
# Obtain dimensions of the screen
echo "Detecting screen dimensions..."
SIZE=`xrandr | head -1 | grep -Eo 'current [0-9]+ x [0-9]+'`
EC=$?
if [ $EC -ne 0 ]; then
echo "Problem detecting screen dimensions (exit code $EC)"
exit 3
fi
WIDTH=`echo $SIZE | cut -d' ' -f 2 -`
HEIGHT=`echo $SIZE | cut -d' ' -f 4 -`
echo "Detected screen dimensions ${WIDTH}x${HEIGHT}"
# Reposition the window
echo "Repositioning window..."
wmctrl -ir $WID -b remove,fullscreen
wmctrl -ir $WID -b remove,maximized_vert,maximized_horz
wmctrl -ir $WID -e 0,0,0,$WIDTH,$HEIGHT
@oori
Copy link

oori commented May 27, 2020

Great script, thanks!
How would you get rid of the top chromium bar, using your span.sh method? regardless if I use --kiosk or --full-screen, I get the top info bar (page name and min/max/full buttons on the right). ofcourse, if I launch chromium without span.sh, the top bar isn't there.

@eslindsey
Copy link
Author

Odd, I didn't get that behavior. I do recall playing with the order of the wmctl commands at the end. Let me see if I can dig up my final copy of this script.

@waltsatan
Copy link

I had the same problem, but I found this "toggle-decorations" command-line tool that removes the top info bar: https://gist.github.com/cat-in-136/96ee8e96e81e0cc763d085ed697fe193

toggle-decorations $(wmctrl -lx | grep -E "Chromium" | grep -oE "0x[0-9a-z]{8}")

Chromium is now kiosk-like again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment