Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get an X11 Window from a Process ID?
    text
    copied!<p>Under Linux, my C++ application is using fork() and execv() to launch multiple instances of OpenOffice so as to view some powerpoint slide shows. This part works.</p> <p>Next I want to be able to move the OpenOffice windows to specific locations on the display. I can do that with the XMoveResizeWindow() function but I need to find the Window for each instance.</p> <p>I have the process ID of each instance, how can I find the X11 Window from that ?</p> <hr> <p><strong>UPDATE</strong> - Thanks to Andy's suggestion, I have pulled this off. I'm posting the code here to share it with the Stack Overflow community.</p> <p>Unfortunately Open Office does not seem to set the _NET_WM_PID property so this doesn't ultimately solve my problem but it does answer the question.</p> <pre><code>// Attempt to identify a window by name or attribute. // by Adam Pierce &lt;adam@doctort.org&gt; #include &lt;X11/Xlib.h&gt; #include &lt;X11/Xatom.h&gt; #include &lt;iostream&gt; #include &lt;list&gt; 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 &lt;&lt; "No such atom" &lt;&lt; endl; return; } search(wRoot); } const list&lt;Window&gt; &amp;result() const { return _result; } private: unsigned long _pid; Atom _atomPID; Display *_display; list&lt;Window&gt; _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, &amp;type, &amp;format, &amp;nItems, &amp;bytesAfter, &amp;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, &amp;wRoot, &amp;wParent, &amp;wChild, &amp;nChildren)) { for(unsigned i = 0; i &lt; nChildren; i++) search(wChild[i]); } } }; int main(int argc, char **argv) { if(argc &lt; 2) return 1; int pid = atoi(argv[1]); cout &lt;&lt; "Searching for windows associated with PID " &lt;&lt; pid &lt;&lt; endl; // Start with the root window. Display *display = XOpenDisplay(0); WindowsMatchingPid match(display, XDefaultRootWindow(display), pid); // Print the result. const list&lt;Window&gt; &amp;result = match.result(); for(list&lt;Window&gt;::const_iterator it = result.begin(); it != result.end(); it++) cout &lt;&lt; "Window #" &lt;&lt; (unsigned long)(*it) &lt;&lt; endl; return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload