Given a process iD of XX, I'd like to have a list of any window id's where _NET_WM_PID = XX. Even better would be the oldest still active window id if possible.
I'm very new to linux, but what I'm trying to do is create a script that would take a command line, and see if there's a windows already open belonging to a process invoked with that same command line. If so, just set focus to that window, otherwise execute the command line to get a new process going. My intention is to use this in my ubuntu desktop, where I'll hook this script into my easystroke mouse gesture commands, so that, for example, every time I gesture for gmail I don't get a brand new gmail session, I just get brought to my existing gmail chrome app window. Perhaps there's a much easier way to go about all this, but I haven't found my way to it yet.
With help, I've figured out how find a PID for a command line with pgrep and how to set focus to a window handle with wmctrl, but I'm stuck on getting from PID to window ID.
The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.
Press Ctrl+Shift+Esc on the keyboard. Go to the Processes tab. Right-click the header of the table and select PID in the context menu.
You can use the “-r” command with -u, -g, and -G options to display real id instead of effective id on the terminal.
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.
you can look up PIDs with wmctrl too, as a matter of fact, and I think that's a better way to do it. xwininfo will return all sorts of entities which appear to be windows, but you won't really find them on your desktop.
If you do man wmctrl , you'll find that wmctrl -l lists all windows that are actually visible on your desktop with (most importantly) their window ids and titles. -p adds PIDs and -x will add window classes.
As the manual says ( RTFM, right? :D), wmctrl can also search through some of these and activate a window that matches the search. However, I have no idea what determines which one of all possible matches will be returned. On the other hand, you can use the provided listing function to write a wrapper that does the searching better and possibly based on some other properties (such as the timestamp of the last access to the window) which you can get by querying the provided win id to xprop, for example.
These lines of code below return the most recent instance a mate-terminal class window:
XTIME="_NET_WM_USER_TIME" #a shorter name for xprop query that shoul return timestamps export TMPDIR=/dev/shm #save tmp files to memory to make it faster LST=`mktemp` #tmp file to store our listing wmctrl -lx | awk -F' ' '{printf("%s\t%s \t",$1,$3); for(i=5;i<=NF;i++) printf("%s",$i); printf("\n") }' > $LST #pretty-print our listing of windows into the tmp file #To each line of listing, prepend a timestamp acquired via an xprop call #Use awk to find a line whose 3rd column (winclass) matches the window class "mate-terminal.Mate-terminal" and among those that do, find the one whose timestamp is the largest while read LINE; do ID=`echo "$LINE"|cut -f 1`; TIME=`xprop -id $ID $XTIME`; TIME="${TIME/* = /}"; echo -e "$TIME\t$LINE" ; done <$LST ) | awk -v s="mate-terminal.Mate-terminal" '$3 == s {if($1>max){max=$1;line=$0};};END{print line}' rm $LST #delete tmp file
Anyhow, for the thing you describe you are building—if I were you, I would find out what class of windows your desired command generates and then base my search on that, rather than on PIDs. Alternatively, you could presume that command CMD will possibly generate windows with a class name that includes CMD.
After you have found your line, you should use the window id
to activate the window via wmctrl.
Hope this helps.
A side note: I've found that xdotool can do searches based on class names and window titles too, but it is extremely slow. On my computer, this bash script (that calls quite a couple of external utilites) is 10 times as fast as the compiled alternative that is xdotool :P.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With