Shell – How to write bash script to configure the displays when HDMI is plugged in or unplugged

display-settingshdmishell-scriptxrandr

I have a notebook running Kubuntu Precise (12.04) which I occasionally use for watching videos. When I do, I plug in an HDMI cable connected to an A/V receiver with an HDMI monitor attached to it.

When I watch videos this way, I still need to use the notebook display when I'm interacting with the system to control playback, etc. The text on the HDMI monitor is hard to read from where I sit.

When I plug in the HDMI cable, Kubuntu detects it, but I have to go through a weird dance sequence (that works, but is convoluted) to get it setup correctly every time for both video and audio. To fix this, I'm trying to write a bash script with xrandr to do it right the first time.

I got the basic idea from Peoro's answer to this U&L Q&A titled: A tool for automatically applying RandR configuration when external display is plugged in.

About my script

My script (included below) works, but needs improvement.

It sets the video mode correctly for the HDMI monitor, but the LVDS1 monitor (on the notebook) changes to display only the upper left portion of the desktop – which is a problem because it cuts off window scroll bars on the right and the taskbar on the bottom.

I tried fixing this with --scale, but my first attempt messed things up sufficiently that I had to reboot to get a working display back.

Is there a way to make both displays show the same content, but with each one using it's own separate preferred resolution?

Or, at least, a way to set the notebook display so that the whole desktop is still accessible when the HDMI display is in use?

Since I'm debugging the script, it isn't cleaned up yet. I may want to make it do more later.

My script

#!/bin/bash
## hdmi_set
## Copyleft 11/13/2013 JPmicrosystems
## Adapted from
## https://unix.stackexchange.com/questions/4489/a-tool-for-automatically-applying-randr-configuration-when-external-display-is-p
## Answer by peoro

# setting up new mode for my VGA
##xrandr --newmode "1920x1080" 148.5 1920 2008 2052 2200 1080 1089 1095 1125 +hsync +vsync
##xrandr --addmode VGA1 1920x1080

##source $HOME/bin/bash_trace
# default monitor is LVDS1
MONITOR=LVDS1

# functions to switch from LVDS1 to HDMI and vice versa
function ActivateHDMI {
    echo "Switching to HDMI"
    ##xrandr --output HDMI1 --mode 1920x1080 --dpi 160 --output LVDS1 --off
    ##xrandr --output HDMI1 --same-as LVDS1
    xrandr --output HDMI1 --mode 1920x1080
    xrandr --output LVDS1 --mode 1366x768
    MONITOR=HDMI1
}
function DeactivateHDMI {
    echo "Switching to LVDS1"
    xrandr --output HDMI1 --off --output LVDS1 --auto
    MONITOR=LVDS1
}

# functions to check if VGA is connected and in use
function HDMIActive {
    [ $MONITOR = "HDMI1" ]
}
function HDMIConnected {
    ! xrandr | grep "^HDMI1" | grep disconnected
}

## MONITOR doesn't do anything because it's not preserved between script executions
# actual script
##while true
##do
    if HDMIConnected
    then
        ActivateHDMI
    fi

    if ! HDMIConnected
    then
        DeactivateHDMI
    fi

    ##sleep 1s
##done

Output from xrandr

Here's what xrandr sees:

bigbird@ramdass:~$ xrandr
Screen 0: minimum 320 x 200, current 1366 x 768, maximum 8192 x 8192
LVDS1 connected 1366x768+0+0 (normal left inverted right x axis y axis) 344mm x 194mm
   1366x768       60.0*+
   1360x768       59.8     60.0  
   1024x768       60.0  
   800x600        60.3     56.2  
   640x480        59.9  
VGA1 disconnected (normal left inverted right x axis y axis)
HDMI1 connected (normal left inverted right x axis y axis)
   1920x1080      60.0 +
   1680x1050      60.0  
   1280x1024      60.0  
   1440x900       59.9  
   1280x720       60.0  
   1024x768       60.0  
   800x600        60.3  
   720x480        59.9  
   640x480        60.0  
   720x400        70.1  
DP1 disconnected (normal left inverted right x axis y axis)

Best Answer

You should probably simply use kscreen instead, which should solve all your issues. It will remember the settings of a previously connected screen and will restore them, once you connect it again.

If you still have such issues while still using kscreen, it should be worth a bug report.

As Kubuntu 12.04 is quite old, you probably should have a look at this.

Related Question