如何从进程ID获取X11窗口?

Ada*_*rce 53 x11

在Linux下,我的C++应用程序使用fork()和execv()来启动OpenOffice的多个实例,以便查看一些powerpoint幻灯片放映.这部分有效.

接下来,我希望能够将OpenOffice窗口移动到显示器上的特定位置.我可以使用XMoveResizeWindow()函数执行此操作,但我需要为每个实例找到Window.

我有每个实例的进程ID,如何从中找到X11窗口?


更新 - 感谢Andy的建议,我已将其撤下.我在这里发布代码以与Stack Overflow社区共享它.

不幸的是,Open Office似乎没有设置_NET_WM_PID属性,所以这并不能最终解决我的问题,但它确实回答了这个问题.

// Attempt to identify a window by name or attribute.
// by Adam Pierce <adam@doctort.org>

#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;
}
Run Code Online (Sandbox Code Playgroud)

and*_*ndy 22

我知道这样做的唯一方法是遍历窗户树,直到找到你要找的东西.遍历并不难(如果你需要一个例子,只需看看xwininfo -root -tree通过查看xwininfo.c来做什么).

但是,您如何识别您正在寻找的窗口? 某些应用程序设置了一个名为_NET_WM_PID的窗口属性.

我相信OpenOffice 设置该属性的应用程序之一(就像大多数Gnome应用程序一样),所以你很幸运.


小智 12

检查/ proc/PID/environ是否包含名为WINDOWID的变量

  • 这个“WINDOWID”是由进程的父进程(或父进程之一)设置的,而不是由进程本身设置的。孩子可以在那里确定其父母的“WINDOWID”。 (4认同)

小智 10

派对迟到了.但是:早在2004年,Harald Welte发布了一个代码片段,它通过LD_PRELOAD包装XCreateWindow()调用,并将进程ID存储在_NET_WM_PID中.这可确保创建的每个窗口都有一个PID条目.

http://www.mail-archive.com/devel@xfree86.org/msg05806.html

  • 这是一个非常好的(尽管有点 hacky)解决方案(考虑到 X11 的设计似乎是为了让窗口管理器的生活尽可能悲惨......) (2认同)

Gau*_*ier 7

尝试安装xdotool,然后:

#!/bin/bash
# --any and --name present only as a work-around, see: https://github.com/jordansissel/xdotool/issues/14
ids=$(xdotool search --any --pid "$1" --name "dummy")
Run Code Online (Sandbox Code Playgroud)

我确实得到了很多ID。当使用长命令完成时,我使用它来将终端窗口设置为紧急,程序seturgent. 我只是遍历我从中获得的所有 idxdotool并运行seturgent它们。