如何从进程ID获取X11窗口?
时间:2020-03-06 14:54:13 来源:igfitidea点击:
在Linux下,我的C ++应用程序使用fork()和execv()启动OpenOffice的多个实例,以便查看一些PowerPoint幻灯片。这部分有效。
接下来,我希望能够将OpenOffice窗口移动到显示器上的特定位置。我可以使用XMoveResizeWindow()函数来做到这一点,但是我需要为每个实例找到Window。
我有每个实例的进程ID,如何从中找到X11窗口?
更新感谢安迪的建议,我已经完成了这一工作。我将代码发布到此处,以便与Stack Overflow社区共享。
不幸的是,Open Office似乎没有设置_NET_WM_PID属性,因此这最终不能解决我的问题,但确实可以回答问题。
// Attempt to identify a window by name or attribute. // by Adam Pierce <[email protected]> #include <X11/Xlib.h> #include <X11/Xatom.h> #include <iostream> #include <list> using namespace std; class WindowsMatchingPid { public: WindowsMatchingPid(Display *display, Window wRoot, unsigned long pid) : _display(display) , _pid(pid) { // Get the PID property atom. _atomPID = XInternAtom(display, "_NET_WM_PID", True); if(_atomPID == None) { cout << "No such atom" << endl; return; } search(wRoot); } const list<Window> &result() const { return _result; } private: unsigned long _pid; Atom _atomPID; Display *_display; list<Window> _result; void search(Window w) { // Get the PID for the current Window. Atom type; int format; unsigned long nItems; unsigned long bytesAfter; unsigned char *propPID = 0; if(Success == XGetWindowProperty(_display, w, _atomPID, 0, 1, False, XA_CARDINAL, &type, &format, &nItems, &bytesAfter, &propPID)) { if(propPID != 0) { // If the PID matches, add this window to the result set. if(_pid == *((unsigned long *)propPID)) _result.push_back(w); XFree(propPID); } } // Recurse into child windows. Window wRoot; Window wParent; Window *wChild; unsigned nChildren; if(0 != XQueryTree(_display, w, &wRoot, &wParent, &wChild, &nChildren)) { for(unsigned i = 0; i < nChildren; i++) search(wChild[i]); } } }; int main(int argc, char **argv) { if(argc < 2) return 1; int pid = atoi(argv[1]); cout << "Searching for windows associated with PID " << pid << endl; // Start with the root window. Display *display = XOpenDisplay(0); WindowsMatchingPid match(display, XDefaultRootWindow(display), pid); // Print the result. const list<Window> &result = match.result(); for(list<Window>::const_iterator it = result.begin(); it != result.end(); it++) cout << "Window #" << (unsigned long)(*it) << endl; return 0; }
解决方案
我们确定我们具有每个实例的进程ID吗?我对OOo的经验是,尝试运行OOo的第二个实例仅与OOo的第一个实例相反,并告诉它打开添加文件。
我认为我们将需要使用X的消息发送功能来很好地询问X的窗口。我希望OOo在某个地方记录其报道。
没有好办法。我看到的唯一真正的选择是:
- 我们可以在进程的地址空间中四处查看以找到连接信息和窗口ID。
- 我们可以尝试使用netstat或者lsof或者ipcs将连接映射到Xserver,然后(以某种方式!至少需要root用户)查看其连接信息以找到它们。
- 生成实例时,我们可以等到另一个窗口被映射后,假设它是正确的窗口,然后继续前进。
检查/ proc / PID / environ是否包含一个名为WINDOWID的变量