8

I own a Thinkpad with an UltraBase (a docking station). My operating system is Ubuntu 10.10. Now, I would like to react on the "docking" event by setting the resolution of my external display correctly. Here is what I've done already:

$ udevadm info -a -p /sys/devices/platform/dock.0

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

looking at device '/devices/platform/dock.0':
  KERNEL=="dock.0"
  SUBSYSTEM=="platform"
  DRIVER==""
  ATTR{modalias}=="platform:dock"
  ATTR{docked}=="0"
  ATTR{flags}=="16"
  ATTR{uid}=="0"
  ATTR{type}=="dock_station"

looking at parent device '/devices/platform':
  KERNELS=="platform"
  SUBSYSTEMS==""
  DRIVERS==""

Then, I've created a udev-rule:

$ cat /etc/udev/rules.d/99-docking.rules
KERNEL=="dock.0", ATTR{docked}=="1", RUN+="/usr/local/sbin/dock.sh"

The script /usr/local/sbin/dock.sh is as follows:

#!/bin/sh
# turn external display on, internal off
echo "hello world" >> "/home/hoppe/udev.out"
/usr/bin/xrandr --output LVDS1 --off
/usr/bin/xrandr --output DP2 --mode "1920x1080"
echo "hello world" >> "/home/hoppe/udev.out"
exit

Both echo-commands are execute without any problems. Also, while calling the script manually, the resolution is set correctly. What have I done wrong?

peterph
  • 30,838

1 Answers1

10

xrandr is executed, but fails. You should read its error messages, they would tell you what's wrong. You could just put all the output from your script in the log file, by adding this line just after the #! line:

exec >/home/hoppe/udev.out 2>&1

You'll find that the message is:

Can't open display 

Like any other X program, xrandr talks to the X server indicated by the DISPLAY environment variable. You need to tell it which display to talk to. At any time, you may have several X servers running, some talking to hardware, some displaying remotely (e.g. VNC), some not having any visible display (e.g. xvfb), etc. Finding which X displays are connected to a particular piece of hardware is not easy, but in practice, just assume that :0 is connected to hardware and all others are not. So put this in your script:

export DISPLAY=:0

Once you've sorted out the DISPLAY issue, you'll need to authorize that script to access your display. This isn't automatic even if the script is running as root because X is network-transparent: the local root must only be allowed to access local displays, and there is no special mechanism for that, the usual cookie mechanism applies for root as well. This is covered in Can I launch a graphical program on another user's desktop as root?.

  • 5
    Thanks very much. Your last link did solve the problem. I've just had to add the following to lines to dock.sh:
    export XAUTHORITY=/home/$USERNAME/.Xauthority export DISPLAY=:0
    – labrassbandito Jun 12 '11 at 12:31