XOpenDisplay 由于 udev 事件而失败
我试图像这样打开 X 显示:
disp = XOpenDisplay(NULL);
当我从用户 shell 运行它时,它工作正常,但如果它从 udev 事件(Ubuntu 10.10)运行,函数调用将返回 NULL。我想知道这是否与不同的环境有关,因此也尝试了 XOpenDisplay(":0.0") 但无济于事。
有谁知道为什么会发生这种情况?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XOpenDisplay(NULL)
将检查DISPLAY
环境变量,该变量通常不在 udev 上下文中设置。为了能够使用XOpenDisplay(something)
(包括something=NULL
),您需要 X 服务器实例的访问密钥。如果您想使用
XOpenDisplay
或任何使用它的图形程序,则需要将XAUTHORITY
环境变量设置为密钥文件的位置。如果此环境变量为空,则使用~/.Xauthority
作为后备。尝试从 udev 启动一些图形程序是一个坏主意(请阅读:您的方法和设计有问题):您不知道 X 显示编号。您也无法猜测,因为很可能有不止一台 X 服务器处于活动状态。然后您需要拥有其密钥文件,而该文件并不总是能够确定或获取。 udev 可以以 root 身份运行,但也有一些东西,比如使用 root_squash 挂载 NFS,以及人们可以用来重新定位密钥文件的
XAUTHORITY
变量。如今图形问题的工作方式是用户或他/她正在使用的桌面环境必须启动专门侦听某些事件的后台程序。这样你就不能简单地侵入人们的屏幕,而必须遵守他们的事件通知系统。
XOpenDisplay(NULL)
would inspect theDISPLAY
environment variable, which usually is not set in udev context. To be able to useXOpenDisplay(something)
(includingsomething=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 useXOpenDisplay
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 theXAUTHORITY
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.