I'm writing a program in C++ (Linux,XWindow) that is supposed to modify an active window's title. More specifically, append certain text to it.
Is it possible to get a Window handle of an active window (even if it is not related to this program) to use it in XFetchName and XStoreName? Or, maybe there is some other way to modify a certain window's title?
Thank you.
update 1:
I used
xprop -root | grep ^_NET_ACTIVE_WINDOW | cut -c41-49
and it returns the correct handle. Thank you for this advice. This is what I used before
xdpyinfo | grep focus: | cut -c16-24
Looks like it returns a number that is greater than the actual handle by 1.
A new question arises now. It seems that XStoreName modifies WM_NAME. I have Unity desktop, and it seems that it reads _NET_WM_NAME instead. How can I modify this one as well?
update 2: found an answer
Atom Atom_name = XInternAtom(xdisplay,"_NET_WM_NAME",false);
Atom Atom_utf_type = XInternAtom(xdisplay,"UTF8_STRING",false);
XChangeProperty(xdisplay,window_handle,Atom_name,Atom_utf_type,8,PropModeReplace,(unsigned char*)new_name,strlen(new_name));
seems to work fine for now
There is no restriction on working with foreign, unrelated windows, so XFetchName
and XStoreName
will work (i.e. they will access WM_NAME
property, which may have the desired effect... or not).
As of determining the current window, you can start with the code sample here:
How to know which window has focus and how to change it? Give attention to the non-accepted answer: _NET_ACTIVE_WINDOW
property of the root window (when wm sets it) is the most sensible value for current window you can get.
Note that there is _NET_WM_NAME
property of type UTF8_STRING
, which is likely to be used by modern window manager in preference to WM_NAME
when both of them are set. Not all applications set it (on my current desktop I see that firefox does, xterm does not). You should be prepared to modify _NET_WM_NAME
as well if you want to modify the title.
And of course, the application may decide to reset its title at any time. You can subscribe to XPropertyNotify
event if you want to notice it and update the title. Ensure to have some workaround for applications that might do the same (i.e. get XPropertyNotify
on title updates and reset it back to what they want).