Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 290
Reactormonk Avatar asked Mar 29 '11 08:03

Reactormonk


2 Answers

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.

like image 130
Andy Avatar answered Oct 14 '22 07:10

Andy


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
like image 26
Denilson Sá Maia Avatar answered Oct 14 '22 08:10

Denilson Sá Maia