Author Topic: Script to Toggle Laptop/External Displays  (Read 694 times)

Offline ihyfr

  • Jr. Member
  • **
  • Posts: 16
  • טראכט גוט וועט זיין גוט, מיסטאמע
    • bitmote.com
Script to Toggle Laptop/External Displays
« on: November 28, 2012, 12:23:20 AM »

Problem
  I like to switch between my external monitor at home and my netbook screen when I'm out. lxrandr came with my LXDE install but it is a pain to use. To compound the problem, if I do something stupid, like run to school pulling the VGA from my suspended computer, it is quite difficult to get the display to switch back to LVDS. (this has happened more than I would like to admit)

Solution
  Step One: I wrote this bash script with some help from https://wiki.archlinux.org/index.php/Xrandr and http://en.tldp.org/LDP/abs/html/abs-guide.html
Code: [Select]
#!/bin/bash
# Toggles between laptop and external display devices. Sets laptop screen if external device goes missing.

xStatus=`xrandr`
connectedOutputs=$(echo "$xStatus" | grep " connected" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")
disconnectedOutputs=$(echo "$xStatus" | grep " disconnected" | sed -e "s/\([A-Z0-9]\+\) disconnected.*/\1/")
activeOutput=$(echo "$xStatus" | grep -e " connected [^(]" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")
connectionCount=$(echo $connectedOutputs | wc -w)
command="xrandr "

if [[ "$connectionCount" -gt 1 ]]; then
  for display in $connectedOutputs; do
    if [[ $display = $activeOutput ]]; then
      command+="--output $display --off "
    else
      command+="--output $display --auto "
    fi
  done
else
  command+="--output $connectedOutputs --auto "
  for display in $disconnectedOutputs; do
    command+="--output $display --off "
  done
fi

$command

  If there are two connected displays this will turn the active one off and auto configure the other.

  If there is only one connected display, it will activate the connected one and deactivate the disconnected ones (yes, deactivating disconnected devices is necessary).

  If both screen are already active this script does nothing.

  Step Two: Key-binding. First I used chmod a+x to make my script executable. My Fn-F8 key has a toggle display pic on it. I used xev to find the name of that key. Turned out to be XF86Display. I then added the following to the key-bindings in my openbox/rc.xml (lxde-rc.xml actually).

Code: [Select]
    <keybind key="XF86Display">
      <action name="Execute">
        <command>/home/ben/Documents/scripts/toggle-display</command>
      </action>
    </keybind>

  Now, no matter what happens, hitting Fn-F8 does the right thing. Yay!

Offline agmg

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 1923
  • Certified Windows Hater
Re: Script to Toggle Laptop/External Displays
« Reply #1 on: November 28, 2012, 12:48:31 AM »
Interesting. I will definitely give it a try. I have a similar situation when I connect my laptop to my TV to watch movies but I had found another way around it (also with the use of xrandr).

http://www.pclinuxos.com/forum/index.php/topic,103010.msg880059.html#msg880059
For the whole world, you are someone.
For someone, you are the whole world.

Offline ihyfr

  • Jr. Member
  • **
  • Posts: 16
  • טראכט גוט וועט זיין גוט, מיסטאמע
    • bitmote.com
Re: Script to Toggle Laptop/External Displays
« Reply #2 on: November 28, 2012, 04:42:49 PM »
  Thanks for sharing that thread with me. I didn't know I could simply type xrandr --auto. I updated my script with your situation in mind. Try the new one with your TV.

Updated Behavior
  2 connected displays, 1 on, 1 off:  Toggles which is active.
  1 connected display:  Auto configures display.
  2 active displays:  Deactivates display listed first, auto configures other.
  2 inactive displays:  Activates both.

Code: [Select]
#!/bin/bash
# Toggles between laptop and external display devices. Sets laptop screen if external device goes missing.

xStatus=`xrandr`
connectedOutputs=$(echo "$xStatus" | grep " connected" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")
activeOutput=$(echo "$xStatus" | grep -e " connected [^(]" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/" | head -n1)
connectionCount=$(echo $connectedOutputs | wc -w)
command="xrandr "

if [[ "$connectionCount" -gt 1 ]]; then
  for display in $connectedOutputs; do
    if [[ $display = $activeOutput ]]; then
      command+="--output $display --off "
    else
      command+="--output $display --auto "
    fi
  done
else
  command+="--auto"
fi

$command
« Last Edit: November 28, 2012, 05:22:19 PM by ihyfr »

Offline agmg

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 1923
  • Certified Windows Hater
Re: Script to Toggle Laptop/External Displays
« Reply #3 on: December 11, 2012, 08:18:37 AM »
Tried it in my laptop with my TV and works like a charm. Thank you very much ihyfr.  :)

EDIT: doesn't work when Tear Free mode is enabled in Catalyst Control Center. Computer freezes while switching monitors.
« Last Edit: December 17, 2012, 05:25:31 AM by agmg »
For the whole world, you are someone.
For someone, you are the whole world.