Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get xid from pid (and vice versa)?

Tags:

linux

x11

pid

How do I get the pid from the xid (X window identifier)? How do I get the list of xids for a given pid?

(Assuming all applications run with DISPLAY=:0, without network transparency being in use)

I'm expecting something like:

  1. Dig in /proc/$pid/fd/ to track connection to X server
  2. Follow that connection, dig in /proc/`pidof X`/fd
  3. Dig inside X as it should know how to map connections to it to windows.
like image 462
Vi. Avatar asked Apr 04 '11 17:04

Vi.


3 Answers

You could use xprop -id <windowid> _NET_WM_PID to get the PID property of the window in question. You should know the window id of the window and not all applications set the _NET_WM_PID atom.

like image 86
Noufal Ibrahim Avatar answered Oct 22 '22 08:10

Noufal Ibrahim


This works for my purposes:

=^_^= izkata@Izein:~$ XID=0x340001c
=^_^= izkata@Izein:~$ printf "%d\n" $XID               # Convert from hex to decimal
54525980
=^_^= izkata@Izein:~$ xdotool getwindowpid 54526066
20639
=^_^= izkata@Izein:~$ ps 20639
  PID TTY      STAT   TIME COMMAND
20639 ?        Ssl  116:25 /usr/lib/firefox/firefox
like image 27
Izkata Avatar answered Oct 22 '22 08:10

Izkata


wmctrl lists windows, their xids and optionally the pid of their process. So assuming there is only one window with your $pid, you could get the corresponding xid with

wmctrl -ulp | tr -s ' ' | cut -d" " -f1,3 | grep $pid | cut -d" " -f1

and conversely, assuming the xid is in hexa,get the pid

wmctrl -ulp | tr -s ' ' | cut -d" " -f1,3 | grep $xid | cut -d" " -f2

.

like image 1
ysalmon Avatar answered Oct 22 '22 08:10

ysalmon