Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

In /etc/acpi/ there are several scripts that can be customized to do something when a particular event happens on your physical computer, like the lid.sh script which is called when the laptop lid is opened or closed, but I'm looking for a way to detect when something is plugged into or disconnected from the Mini DisplayPort, so that I can run a command.

I don't see any such script in there (unless I've missed something).

The command would be to configure the multi-monitor setup automatically using xrandr. This used to happen automatically in Gnome (using ~/.config/monitors.xml I believe) but I've now switched to xmonad completely and it doesn't automatically detect this.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Yes it should be possible using udev.

I have asked/answered here a similar question (change sound output on HDMI (dis)connect). First you will need to create a udev rule like this:

SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/update_screen_config"

It should be on the same location as in my answer but you can change the name. To know exactly which SUBSYSTEM and ACTION you need use udevadm and connect your DP. In my system, connecting the HDMI screen I get:

$ udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent
(here I connected my screen)
KERNEL[16383.092226] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [16383.281930] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
(disconnected my screen)
KERNEL[16389.092226] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [16389.281930] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)

The second part is the creation of /usr/local/bin/update_screen_config (you can give it other name or location). I have used a Python script but you can use anything you want, since it can be executed. This will be the script that is called when the cable is connected/disconnected, so it must find the current state and use xrandr to do what you need. See how I made the detection of the state of the screen and if it suits your needs.

Note: this script is executed as root by udev, so test it as a normal user before you put him on udev.

share|improve this answer
    
'udevadm monitor' only works the first time I plug it in. It doesn't report unplug events or subsequent plugging in events. So my custom script doesn't seem to be getting called more than once. Any ideas? I'm on Ubuntu 12.10 –  user779159 Mar 21 '13 at 12:38
    
Can you provide the output? I have tried with HDMI on Quantal and everything works fine. Also note that the event emitted should be the same when you connect or disconnect (see the updated answer) –  Salem Mar 21 '13 at 13:34
    
I get the exact same as you: "KERNEL[----------] change /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)" and "UDEV [----------] change /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)" but only the first time I plug it in. When I unplug, nothing. And when I re-plug, again nothing. Do you get the same events when you plug in again after unplugging? So if you plug in 5 times and unplug 5 times, you'll get 10 sets of KERNEL and UDEV? Could it have something to do with Mini DisplayPort vs HDMI? –  user779159 Mar 23 '13 at 13:53
    
Yes, if I do what you said I would get 10 messages. That's weird... If you don't get those events then maybe the only solution is use pooling (instead of that script being activated on connect/disc it will be active always and periodically check the state and act accordingly). –  Salem Mar 23 '13 at 16:58
    
Ok, marking as solved. I think it's just a hardware issue on my end, because I don't see any other reason why the events would not come (unless you have any other idea on what might be wrong)? Otherwise I guess I'll have to resort to polling –  user779159 Mar 23 '13 at 17:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.