问题
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?
回答1:
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.
回答2:
You have three options:
- Poll on a specific entry in sysfs.
- Use inotify to detect changes in sysfs.
- Use a netlink socket with NETLINK_KOBJECT_UEVENT and listen for a
change
uevent for the device you want.
In any of the ways mentioned, you're still going to have to poll in one way or another, so I'd just go with the first option.
回答3:
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:
- How can I run code whenever a USB device is (un)plugged, without requiring root permissions?
- How can I listen for 'usb device inserted' events in Linux, in Python?
- pyudev documentation - Asynchronous monitoring
回答4:
Thanks sebastianwagner!
With this information, I've been able to successfully boot my Kodi media center with the TV turned off.
Indeed, when the TV is off, the Intel driver doesn't want to set up a mode and I got a blank screen when I later powered on the TV.
The Kodi log showed the following line:
WARNING: CXRandR::Query - output HDMI1 has no current mode, assuming disconnected
So I created the following line in /etc/udev/rules.d/99-monitor-hotplug.rules :
ACTION=="change", SUBSYSTEM=="drm", ENV{HOTPLUG}=="1", RUN+="/usr/sbin/hotplugtv.sh"
Content of /usr/sbin/hotplugtv.sh (my X server is running as root) :
#!/bin/bash
export DISPLAY=:0
export XAUTHORITY=/root/.Xauthority
/bin/date 2>&1 >> /var/log/hotplugtv.log;
if [[ $(cat /sys/class/drm/card0-HDMI-A-1/status | grep -Ec "^connected") -eq 1 ]]; then
echo "TV connected!" >> /var/log/hotplugtv.log;
/bin/sleep 2s;
/usr/bin/xrandr --verbose --output HDMI1 --auto 2>&1 >> /var/log/hotplugtv.log;
else
echo "TV disconnected!" >> /var/log/hotplugtv.log;
fi
Don't forget to reload udev rules when you make any change to your script (this was driving me crazy!):
udevadm control --reload-rules
Be careful to disable any screen saver in Kodi because they stay activated forever when you finally power up the TV. On the other hand energy saving / DPMS seems to work fine.
回答5:
Assuming you're running X, a script can parse the output of xrandr to see what monitors are connected. This should work with any graphics card. This is the same tool you'll probably use to make use of the change in your script.
It doesn't solve the issue with notifications to automatically run a script. While I don't have a great generic solution, a common case is detecting when an external monitor is present because a laptop is connected to a docking station. In this case, you can have your script trigger off of something else that triggers when connecting to the docking station such as the addition or removal of a USB keyboard or ethernet.
来源:https://stackoverflow.com/questions/5469828/how-to-create-a-callback-for-monitor-plugged-on-an-intel-graphics