[dev] xid of a current window

68 views
Skip to first unread message

Swiatoslaw Gal

unread,
Mar 25, 2012, 7:37:43 AM3/25/12
to d...@suckless.org
How can I get a xid of a focused window? Like with xprop, but
without user interaction. Say, I want to write an program
that would log the xid of a currently focused window every second.

Sincerely,
s.

Connor Lane Smith

unread,
Mar 25, 2012, 7:45:14 AM3/25/12
to dev mail list
On 25 March 2012 12:42, Anselm R Garbe <gar...@gmail.com> wrote:
> http://tronche.com/gui/x/xlib/input/XGetInputFocus.html

Note that a Window is an integer, specifically the window's XID.

cls

Ivan Kanakarakis

unread,
Mar 25, 2012, 8:20:45 AM3/25/12
to dev mail list
if you don't want to write something of your own, then

$ xprop -root _NET_ACTIVE_WINDOW
> _NET_ACTIVE_WINDOW(WINDOW): window id # 0x400050

or

$ xdotool getactivewindow
> 4194384

will work, as long as your wm supports and correctly sets _NET_ACTIVE_WINDOW

note that xdotool outputs the id as an int,
to get the same with xprop do something like:

$ xprop -root _NET_ACTIVE_WINDOW | cut -d# -f2 | xargs printf "%d\n"
> 4194384

some apps work with both formats, others require int
xwininfo can also give you the window id, and with -int it will be in int form.


--
*Ivan c00kiemon5ter V Kanakarakis *

Ivan Kanakarakis

unread,
Mar 25, 2012, 9:33:23 AM3/25/12
to dev mail list
A quick look into the tip of dwm, shows that it is the case for dwm.
It doesn't set the _NET_ACTIVE_WINDOW property correctly,
although the atom is there, and included in _NET_SUPPORTED.

To fix that, one must call (supposing c is
the Client that holds the current window) :

XChangeProperty(dpy, root, netatom[NetActiveWindow], XA_WINDOW,
32, PropModeReplace, (unsigned char*)&c->window, 1);

everytime the current window changes.
also one should delete the property if there is no active window
(either no window on the screen, or no window focused)

On 25/03/2012, Swiatoslaw Gal <swiatos...@univie.ac.at> wrote:
> ---------- From Ivan Kanakarakis 25-03-2012 at 15:20 ----------


>> if you don't want to write something of your own, then
>>
>> $ xprop -root _NET_ACTIVE_WINDOW
>> > _NET_ACTIVE_WINDOW(WINDOW): window id # 0x400050
>

> xprop -root shows me the values of the folowing fields:
> CUT_BUFFER0(STRING)
> WM_NAME(STRING)
> _NET_SUPPORTED(ATOM)
> ESETROOT_PMAP_ID(PIXMAP)
> _XROOTPMAP_ID(PIXMAP)
> RESOURCE_MANAGER(STRING)
> _XKB_RULES_NAMES(STRING)
> XFree86_VT(INTEGER)
> XFree86_DDC_EDID1_RAWDATA(INTEGER)
>
>> $ xdotool getactivewindow
> XGetWindowProperty[_NET_ACTIVE_WINDOW] failed (code=1)
> xdo_get_active_window reported an error


>
>> will work, as long as your wm supports and correctly sets
>> _NET_ACTIVE_WINDOW

> I assume that this is not the case.
> I am using dwm. Does it matter?
>
> s.

Andreas Amann

unread,
Mar 25, 2012, 9:56:57 AM3/25/12
to dev mail list

On Sun, Mar 25, 2012 at 04:33:23PM +0300, Ivan Kanakarakis wrote:
> A quick look into the tip of dwm, shows that it is the case for dwm.
> It doesn't set the _NET_ACTIVE_WINDOW property correctly,
> although the atom is there, and included in _NET_SUPPORTED.
>
> To fix that, one must call (supposing c is
> the Client that holds the current window) :
>
> XChangeProperty(dpy, root, netatom[NetActiveWindow], XA_WINDOW,
> 32, PropModeReplace, (unsigned char*)&c->window, 1);
>
> everytime the current window changes.
> also one should delete the property if there is no active window
> (either no window on the screen, or no window focused)

Attached is a patch which does that. It works for me.

If you want support for NetActiveWindow, you probably also want support
for NetClientList. The second patch implements that.

Andreas

netactivewindow
netclientlist

Anselm R Garbe

unread,
Mar 25, 2012, 11:50:50 AM3/25/12
to dev mail list
On 25 March 2012 15:56, Andreas Amann <am...@physik.tu-berlin.de> wrote:
> Attached is a patch which does that.  It works for me.
>
> If you want support for NetActiveWindow, you probably also want support
> for NetClientList.  The second patch implements that.

Thanks, I applied both patches. Even though I really more and more hat
this whole EWMH cruft. Since there was basic EWMH support in dwm,
things became more unreadable...

Cheers,
Anselm

Swiatoslaw Gal

unread,
Mar 25, 2012, 12:41:27 PM3/25/12
to dev mail list
> Thanks, I applied both patches. Even though I really more and more hat
> this whole EWMH cruft. Since there was basic EWMH support in dwm,
> things became more unreadable...

Then I start to feel guilty of my question.
I've got my answer, and I do not know if anyone really needs
_NET_WM_WINDOW_OPACITY to be handled by dwm.

Thank you for the answers anyway,
s.

Swiatoslaw Gal

unread,
Mar 25, 2012, 1:47:36 PM3/25/12
to dev mail list
> You could use the focused window instead (which was what you
> originally asked about):
>
> $ xdotool getwindowfocus
> 48234505

Yes, this works fine (without any today's patches).
But the very first answer: XGetInputFocus was _exactly_
what I was looking for.

Is there a reason that a _window_manager_ provides EHWHMS...
whatever support? This can be done with an external program,
am I wrong?

s.

Andreas Amann

unread,
Mar 25, 2012, 3:10:21 PM3/25/12
to dev mail list

The reason why I wrote these patches was that external programs do not work
reliably without support from the WM. In particular "xdotool
getwindowfocus" is quite frustrating. See also the "xdotool" manpage
itself:

"getactivewindow: Output the current active window. This command is often
more reliable than getwindowfocus."

Apart from that I agree with Anselm that EWMH is a pain...

Andreas

Reply all
Reply to author
Forward
0 new messages