add udev rule for external display

didi_X8 picture didi_X8 · Oct 8, 2012 · Viewed 8.1k times · Source

I wrote a small shell script configuring attached external displays with xrandr.

# cat /home/didi/bin/monitor_autoswitcher.sh 
#!/bin/bash

xrandr | grep "HDMI1 connected"
if [[ $? == 0 ]]; then
  # is connected
  xrandr --output HDMI1 --right-of LVDS1 --auto
else
  # not connected
  xrandr --output HDMI1 --auto
fi

xrandr | grep "VGA1 connected"
if [[ $? == 0 ]]; then
  # is connected
  xrandr --output VGA1 --right-of LVDS1 --auto
else
  # not connected
  xrandr --output VGA1 --auto
fi

That works. Now I want to have it triggered automatically and found out that this can be done with udev. I tried

udevadm monitor

which, when plugging in an external displays outputs

KERNEL[465828.240250] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [465828.243549] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)

and when plugging it out

KERNEL[465836.844209] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [465836.847445] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)

Also good.

Then I added an udev rule:

# cat 40-external-display.rules 
SUBSYSTEM=="drm", ACTION=="change", RUN+="/home/didi/bin/monitor_autoswitcher.sh"

and restarted udev

service udev restart

Unfortunately, still nothing happens when plugging in/out the display. The script monitor_autoswitcher.sh definitely works, because invoking it manually after plugging does what it should.

What's missing?

Answer

Paul Archer picture Paul Archer · Jan 10, 2013

This looks like pretty much the same thing. The only real difference I see is that the script sets the DISPLAY variable, which may be key.

http://ruedigergad.com/2012/01/28/hotplug-an-external-screen-to-your-laptop-on-linux/