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?
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.
This other answer is on the right path: you want to listen to DRM events from udev
.
I've implemented a Python script that runs some code when either USB devices or external displays are (un)plugged. I'm including below a minimal version of that script (untested):
#!/usr/bin/env python3 import pyudev def udev_event_received(device): ... # Your code here! context = pyudev.Context() monitor_drm = pyudev.Monitor.from_netlink(context) monitor_drm.filter_by(subsystem='drm') observer_drm = pyudev.MonitorObserver(monitor_drm, callback=udev_event_received, daemon=False) observer_drm.start() # This will prevent the program from finishing: observer_drm.join()
See also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With