x11

How to convert a X11 window id to a process id


I'm working on a small application and I need to find the PID of a process given the X11 window ID of its main window or child windows. I saw examples for doing such a conversion using _NET_WM_PID, but I cannot figure out how to do it without using it. The reason for not using _NET_WM_PID is that it's not implemented in all the available window managers and my application needs to work on any one of them (or at least on most of them). Could somebody help me please and give me some suggestion/directions on how to solve this issue? Thank you!


Solution

  • In general, it's not possible to find out the PID of a process that created a window. It may be that the process is running remotely on a machine, and it may be that the machine doesn't even have the notion of processes and PIDs.

    If you don't trust anybody stored this information when the client was originally created, you will need to trace the connections yourself. Find out what kind of connection (socket etc) the client was using, find out where that connection ends, and find out which process holds that end. How to do that (and whether it is possible at all) is highly operating system dependent.