Getting pid and details for topmost window

前端 未结 9 993
醉酒成梦
醉酒成梦 2020-12-04 13:08

Does anyone know how to get the PID of the top active window and then how to get the properties of the window using the PID? I mean properties like process name, program nam

相关标签:
9条回答
  • 2020-12-04 13:28

    One of things about X is that it's network transparent. It's quite possible that the actual window being displayed at the top (which has focus) is running on a machine other than your own in which case, the process id of the process running inside the window will make no sense on your machine.

    Can you elaborate a little on what you want to do? I think there are some missing details here. Ideally, you should work at the X level rather than at the machine specific one.

    0 讨论(0)
  • 2020-12-04 13:33

    xlib's XGetInputFocus will tell you which window has focus, which is probably more interesting than which is topmost. Cf. Xfree86's XGetInputFocus manpage.

    If it's really the topmost window, and not the window-with-focus you're after, well, I don't think there is a simple call to do that, because xlib doesn't seem to offer any way of querying the global stacking order, which is the data structure that tells you which windows are in front of which others.

    Once you have the right window id, xprop will list the pid under _NET_WM_PID_ - though not all windows have such a property...

    Postscript More thoughts; long time since I've thought about xlib...

    To summarise:

    1. X does not offer any reliable association between window ids and pids, and as Noufal observes, the windows served on an X desktop may come from many different machines, and two remote clients might happen to use the same PID, since it is only unique per machine. Cf. How to get an X11 Window from a Process ID?

    2. X does not seem to offer an interface asking which is the topmost window, or whether one window occludes another. Likewise with privileged access... Cf. How to identify top-level X11 windows using xlib?

    3. Commonly available window managers and Qt don't give you privileged access to X

    4. Without both a way of finding the topmost window, and a reliable association of the window id to the matching pid, we can't solve the question.

    5. We can find which window has focus, and this is probably what we want. But again, without the wid to pid map ...

    So, sorry, it looks like it can't be done.

    0 讨论(0)
  • 2020-12-04 13:34

    I am voting up Michel Kogan’s answer, and adding this concise summary of it:

    ps -o pid,comm,args $(xprop -id $(xprop -root -f _NET_ACTIVE_WINDOW 0x " \$0\\n" _NET_ACTIVE_WINDOW | awk "{print \$2}") -f _NET_WM_PID 0c " \$0\\n" _NET_WM_PID | awk "{print \$2}")
    

    The above will show the following for the currently active window: PID, command name (only the executable name), command with all its arguments.

    0 讨论(0)
提交回复
热议问题