Created
          October 5, 2012 16:17 
        
      - 
      
- 
        Save nl0x/3840785 to your computer and use it in GitHub Desktop. 
    Automatically select screen
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/bin/bash | |
| # Usage: scr [local|external|clone] | |
| # (no option) → try external, else local | |
| # local → local display | |
| # external → external + local if closed, else external only | |
| # clone → same content, local display scaled to fit external | |
| export DISPLAY=`ck-list-sessions|grep 'x11-display = '|sed "s/.*\(:[0-9]\)'/\1/"` | |
| LOCAL=LVDS1 | |
| WIDTH= | |
| HEIGHT= | |
| screensize () { | |
| splitsize `xrandr -q --current|grep "^$1" -A1|tail -n1|sed "s/\([0-9]\+\)x\([0-9]\+\).*/\1x\2/"` | |
| } | |
| splitsize () { | |
| WIDTH=`echo $1|cut -dx -f1` | |
| HEIGHT=`echo $1|cut -dx -f2` | |
| } | |
| # figure out whether an external monitor is connected | |
| EXTERNAL=`xrandr --current|grep ' connected'|cut -d' ' -f1|grep -v $LOCAL|head -n1` | |
| SCALE='1x1' | |
| if test -n "$EXTERNAL"; then | |
| MODE=external | |
| cat /proc/acpi/button/lid/LID/state | grep -q closed && MODE=external-only | |
| screensize $LOCAL | |
| w1=$WIDTH | |
| h1=$HEIGHT | |
| screensize $EXTERNAL | |
| w2=$WIDTH | |
| h2=$HEIGHT | |
| SCALE=`echo "s1=$w2/$w1;s2=$h2/$h1;if(s1>s2) s1 else s2"|bc -l` | |
| SCALEX=`echo "$w2/$w1"|bc -l` | |
| SCALEY=`echo "$h2/$h1"|bc -l` | |
| else | |
| # no external output connected | |
| MODE=local | |
| fi | |
| case $1 in | |
| external) | |
| if test -n "$EXTERNAL";then | |
| MODE=external | |
| else | |
| echo "No external screen found." | |
| exit 1 | |
| fi | |
| ;; | |
| clone) | |
| if test -n "$EXTERNAL";then | |
| MODE=clone | |
| else | |
| echo "No external screen found." | |
| exit 1 | |
| fi | |
| ;; | |
| local) | |
| MODE=local | |
| esac | |
| case $MODE in | |
| external-only) | |
| xrandr --output $LOCAL --off --output $EXTERNAL --auto --scale 1x1 | |
| ;; | |
| external) | |
| xrandr --output $LOCAL --auto --scale 1x1 --output $EXTERNAL --auto --scale 1x1 --left-of $LOCAL | |
| ;; | |
| clone) | |
| xrandr --output $LOCAL --auto --scale ${SCALEX}x${SCALEY} --output $EXTERNAL --auto --same-as $LOCAL | |
| ;; | |
| local) | |
| xrandr --output $LOCAL --auto --scale 1x1 --output VGA1 --off --output HDMI1 --off --output DP1 --off --output DP2 --off --output DP3 --off | |
| ;; | |
| esac | |
| xrefresh | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment