Is There a Linux Command to Determine the Window Ids Associated with a Given Process Id

Is there a linux command to determine the window IDs associated with a given process ID?

xwininfo and xprop permits to retrieve what you want, but it is a little tricky.

xwininfo permits to retrieve all known windows, and xprop to query X about a single window ID for your _NET_WM_PID parameter.

So far, a hacky way to do it would be:

#!/bin/sh

findpid=$1

known_windows=$(xwininfo -root -children|sed -e 's/^ *//'|grep -E "^0x"|awk '{ print $1 }')

for id in ${known_windows}
do
xp=$(xprop -id $id _NET_WM_PID)
if test $? -eq 0; then
pid=$(xprop -id $id _NET_WM_PID|cut -d'=' -f2|tr -d ' ')

if test "x${pid}" = x${findpid}
then
echo "Windows Id: $id"
fi
fi
done

Result:

mycroft:~ $ ./find_windows.sh 1919
Windows Id: 0x1800748
Windows Id: 0x181b221
Windows Id: 0x1803ad5
Windows Id: 0x181f681
Windows Id: 0x181f658
Windows Id: 0x180006d
Windows Id: 0x1800003
Windows Id: 0x1800001
Windows Id: 0x180001e

As you will see, a single process may have a certain number of known windows, even if you see only one on your screen.

Maybe you should get these tools sources in order to make what you want.

How can I get the WID via bash which window the mouse pointer is over?

I cannot test this at the moment, but maybe xdotool offers a way to get the window under the mouse cursor. From man xdotool:

getmouselocation [--shell]

Outputs the x, y, screen, and window id of the mouse cursor.

I assume the mentioned window id belongs to the window under the mouse cursor. To retrieve it, use

declare "$(xdotool getmouselocation --shell | grep WINDOW)"
echo "$WINDOW"

The window id can then be used in other tools like xwininfo -id <wdid> to get more information.

How to get an X11 Window from a Process ID?

The only way I know to do this is to traverse the tree of windows until you find what you're looking for. Traversing isn't hard (just see what xwininfo -root -tree does by looking at xwininfo.c if you need an example).


But how do you identify the window you are looking for? Some applications set a window property called _NET_WM_PID.


I believe that OpenOffice is one of the applications that sets that property (as do most Gnome apps), so you're in luck.

Find Process Name by its Process ID

The basic one, ask tasklist to filter its output and only show the indicated process id information

tasklist /fi "pid eq 4444" 

To only get the process name, the line must be splitted

for /f "delims=," %%a in ('
tasklist /fi "pid eq 4444" /nh /fo:csv
') do echo %%~a

In this case, the list of processes is retrieved without headers (/nh) in csv format (/fo:csv). The commas are used as token delimiters and the first token in the line is the image name

note: In some windows versions (one of them, my case, is the spanish windows xp version), the pid filter in the tasklist does not work. In this case, the filter over the list of processes must be done out of the command

for /f "delims=," %%a in ('
tasklist /fo:csv /nh ^| findstr /b /r /c:"[^,]*,\"4444\","
') do echo %%~a

This will generate the task list and filter it searching for the process id in the second column of the csv output.

edited: alternatively, you can suppose what has been made by the team that translated the OS to spanish. I don't know what can happen in other locales.

tasklist /fi "idp eq 4444" 

How could i detect info for the front most window in X?

To find out the window ID, try:

xprop -root|grep "_NET_CLIENT_LIST_STACKING(WINDOW): window id"

Window properties:

_NET_CLIENT_LIST_STACKING has bottom-to-top stacking order

One way to accomplish this is to parse the output of this command inside your application. The topmost window is the last on the list.

EDIT:

If you need to retrieve the process ID from the window ID, there's a small application here that shows how to do this trick. I successfully compiled it with:

g++ win_procid.cpp -o win_procid -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/libgtop-2.0 -lXtst -lgtop-2.0

I had to install the package libgtop2-dev because I didn't have it on my system.

How can i find all windows with a particular _NET_WM_NAME?

Here is a programmatic approach (in C) :

First, get the window IDs using _NET_CLIENT_LIST property using Atoms and XGetWindowProperty().
Then get the window names using WM_NAME under Atoms, again using XGetWindowProperty(). Now you have the list of all active windows in your system, so you can use strcmp() to check the desired name of the window.

The working code is given here:
https://cboard.cprogramming.com/linux-programming/125534-accessing-windows-x11.html

About XGetWindowProperty()
https://tronche.com/gui/x/xlib/window-information/XGetWindowProperty.html

About Atoms:
https://tronche.com/gui/x/xlib/window-information/properties-and-atoms.html



Related Topics



Leave a reply



Submit