I am trying to open the X display like so:
disp = XOpenDisplay(NULL);
When I run this from my users shell it works fine but if it is run from a udev event (Ubuntu 10.10) the function call returns NULL. I wondered if this has something to do with the differing environment so have tried XOpenDisplay(":0.0") also but no avail.
Does anyone know why this happens?
XOpenDisplay(NULL)
would inspect the DISPLAY
environment variable, which usually is not set in udev context. To be able to use XOpenDisplay(something)
(including something=NULL
), you need the access key to the X server instance.
The XAUTHORITY
environment variable is to be set to the location of the key file if you want to use XOpenDisplay
or any graphical program that makes use of it. If this env var is empty, ~/.Xauthority
is used as a fallback.
Trying to start some graphical program from udev is a bad idea (read: something is wrong in your approach and design): You don't know the X display number. You cannot guess it either, because there may be very well more than one X server active. And then you need to have its key file, which is not always possible to determine or obtain either. udev may run as root, but there are things like NFS mounts with root_squash
, and the XAUTHORITY
variable with which people can relocate their key file.
The way graphical problems work these days is that the user, or the desktop environment s/he is using, has to start a background program specifically listening for certain events. Just so that you cannot simply intrude on people's screens, but have to abide by their event notification system.
精彩评论