Closed yyy33 closed 1 year ago
Focus change events are not (always) sent to the root window. You need help from the window manager. EWMH specifies that the property _NET_ACTIVE_WINDOW
on the root window is updated when the focus changes.
Tipp for experimenting with X11 events: Run xev -root
(or just xev
if you want to know about events sent to a "normal" window).
whenever there is a window getting focus I can get the title and class of that window and perform some action.
I'll try to hack something up.
_NET_WM_NAME
is also from EWMH. This code uses from_utf8
on the class of the window, even though ICCCM specifies that this is encoded in Latin1. But "usually" everyone uses ASCII for this, anyway.
Also, for laziness, I just used get_input_focus()
to get the right window. I am not actually sure that this is correct. It might be necessary to actually use the value of _NET_ACTIVE_WINDOW
since the input focus could be given to a child window of this window. However, for now I don't care enough to actually do that. (Welcome to X11 where everything is complicated...).
Edit: Well... okay. To actually read _NET_ACTIVE_WINDOW
, replace let focus = conn.get_input_focus()?.reply()?.focus;
with:
let focus = conn
.get_property(
false,
root,
atoms._NET_ACTIVE_WINDOW,
AtomEnum::WINDOW,
0,
1,
)?
.reply()?
.value32()
.ok_or("_NET_ACTIVE_WINDOW has incorrect format")?
.next()
.ok_or("_NET_ACTIVE_WINDOW is empty")?;
Focus change events are not (always) sent to the root window. You need help from the window manager. EWMH specifies that the property
_NET_ACTIVE_WINDOW
on the root window is updated when the focus changes.Tipp for experimenting with X11 events: Run
xev -root
(or justxev
if you want to know about events sent to a "normal" window).
Thank you so much, I managed to run the code you gave, to the desired effect.
Welcome to X11 where everything is complicated...
Lol, it is a complicated x11, and I spent a day searching around and checking the official documents to solve the problem.
Hi I m writing a program and I hope whenever there is a window getting focus I can get the title and class of that window and perform some action.
The code below is what I wrote according to the prompts from the code from chatgpt and the sample folder it can run without error but when I change the window focus it doesn t have any output.
Please help me, thank you