How to create a callback for "monitor plugged" on an intel graphics?

Reactormonk picture Reactormonk · Mar 29, 2011 · Viewed 20.8k times · Source

I've got an eeepc with an intel graphics. I'd like to hook a script to the event of a monitor plugged via VGA. How to do that?

Answer

Andy picture Andy · Apr 19, 2011

As a crude solution, you may be able to poll on sysfs. On my laptop I have:

$ cat /sys/class/drm/card0-LVDS-1/status
connected

$ cat /sys/class/drm/card0-VGA-1/status
disconnected

I'm guessing this requires kernel DRM and possibly KMS.

To see if you can trigger something automatically, you could run udevadm monitor --property, and watch while you are (dis-)connecting the monitor to see if events are reported.

With my radeon, I get an event the first time I connect a VGA monitor, but no events on subsequent disconnects and reconnects. The event should look something like (using yours as an example):

KERNEL[1303765357.560848] change /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV_LOG=0
ACTION=change
DEVPATH=/devices/pci0000:00/0000:00:02.0/drm/card0
SUBSYSTEM=drm
HOTPLUG=1
DEVNAME=dri/card0
DEVTYPE=drm_minor
SEQNUM=2943
MAJOR=226
MINOR=0

Unfortunately there's not a lot to match against, but as long as there's only one video card in the picture that's not too important. Find where udev gets rules from on your system (probably /etc/udev/rules.d/), and create a 99-monitor-hotplug.rules file with:

ACTION=="change", SUBSYSTEM=="drm", ENV{HOTPLUG}=="1", RUN+="/root/hotplug.sh"

udev will then run hotplug.sh when a display is connected. As a test, I put the following in /root/hotplug.sh (don't forget to make this script executable):

#!/bin/sh

for output in DVI-I-1 LVDS-1 VGA-1; do
        echo $output >> /root/hotplug.log
        cat /sys/class/drm/card0-$output/status >> /root/hotplug.log
done

With that, I got an entry in hotplug.log after I connected an external display. Even filtering for ACTION=="change", I still got some events on boot, so you may want to take that into account somehow in your script.