A shell script to determine which physical screen it's running on
I was working on a program to generate storyboard files from movies, and wanted the storyboard canvas to track the screen size. In order to do that I needed to know which screen the program was running on, since I generally have two on my system (the laptop screen and an external monitor.)
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 |
#!/bin/bash # Generate a window name as random as I can make it X_NAME="$(echo "$(date +%t)-$RANDOM" | sha1sum | cut -c1-8)" # Start xlogo, iconified xlogo -iconic -name $X_NAME & XLOGO_PID=$! sleep 0.1 # Give xlogo enough time to set up its window # Determine the xlogo's window position using 'xwininfo' and the following two # lines from its output: # Absolute upper-left X: 3337 # Absolute upper-left Y: 29 # The awk program prints "XLOGO_X=# XLOGO_Y=#" on stdout, which is then passed # to 'eval' to set up environment variables of the same name eval "$(xwininfo -name $X_NAME | awk '/Absolute upper-left/ { match($0, /([XY]):/, a); print "XLOGO_" a[1] "=" $4 }')" # Use xrandr to get the screens and their positions; for example: # eDP1 connected primary 1600x900+1920+0 (normal left inverted right x axis y axis) 382mm x 214mm # HDMI1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 598mm x 336mm # Send this information along with XLOGO_X and XLOGO_Y to determine the screen # that xlogo is running on, then write "SCREEN_WIDTH=# SCREEN_HEIGHT=#" on # stdout. stdout is run through 'eval' to set up environment variables of the # same name. eval "$(xrandr | awk -v xlogo_x=$XLOGO_X -v xlogo_y=$XLOGO_Y '/^[^ ]+ *connected/ { # Get <height>x<width>+<xstart>+<ystart> match($0, /([0-9]+)x([0-9]+)\+([0-9]+)\+([0-9]+)/, a) x_start = a[3]; x_end = a[1] + a[3] y_start = a[4]; y_end = a[2] + a[4] if (xlogo_x >= x_start && xlogo_x < x_end && xlogo_y >= y_start && xlogo_y < y_end) { print "SCREEN_WIDTH=" a[1] " SCREEN_HEIGHT=" a[2] exit } }')" kill $XLOGO_PID &>/dev/null # Print the results for the edification of the user echo "Width=$SCREEN_WIDTH Height=$SCREEN_HEIGHT" |