Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add udev rule for external display

Tags:

linux

xrandr

udev

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?

like image 732
didi_X8 Avatar asked Oct 08 '12 13:10

didi_X8


People also ask

How do I monitor udev?

The main configuration file for udev is /etc/udev/udev. conf, and to control the runtime behavior the udev daemon, you can use the udevadm utility. To display received kernel events (uevents) and udev events (which udev sends out after rule processing), run udevadm with the monitor command.

Where do udev rules go?

RULES FILES. The udev rules are read from the files located in the system rules directory /lib/udev/rules. d, the volatile runtime directory /run/udev/rules. d and the local administration directory /etc/udev/rules.


1 Answers

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/

like image 103
Paul Archer Avatar answered Sep 25 '22 01:09

Paul Archer