Got help from stackoverflow and solved it with python instead:
import Xlib.display
def get_active_window():
display = Xlib.display.Display()
window = display.get_input_focus().focus
#wmname = window.get_wm_name()
wmclass = window.get_wm_class()
if wmclass is None: #or wmname is None:
window = window.query_tree().parent
wmclass = window.get_wm_class()
#wmname = window.get_wm_name()
winclass=wmclass[1]
#print winclass
return winclass
The C code definitely has absolutely nothing to do with the window manager.
I understand this makes no sense. But can't think of other variables.
EDIT: damn, the variable is the window type itself. Got successful fetches for Skype, Keepassx; segmentation fault for Chromium, Geany and Keepass2.
]]>Ah, you're trying to get atoms only set by some WMs. Xprop will work fine, but it can only get the information that is there. I forgot that I don't think xprop by itself has a way to get the focus window.
For the C code, XGetWindowProperty will work, but the error from any "non-terminal" windows is that there are child windows. You need to use XQueryTree in a loop to "drill down" to the deepest child window.
Here is an example from one of my projects to get the window the mouse is currently over - you'll need to revise to change XQueryPointer to XQueryTree:
XQueryPointer(dpy,root,&ev.xbutton.root,&ev.xbutton.window,&ev.xbutton.x_root,&ev.xbutton.y_root,&ev.xbutton.x,&ev.xbutton.y,&ev.xbutton.state); ev.xbutton.subwindow = ev.xbutton.window; while(ev.xbutton.subwindow) { ev.xbutton.window = ev.xbutton.subwindow; XQueryPointer(dpy,ev.xbutton.window,&ev.xbutton.root,&ev.xbutton.subwindow,&ev.xbutton.x_root,&ev.xbutton.y_root,&ev.xbutton.x,&ev.xbutton.y,&ev.xbutton.state); }
EDIT: this may not actually be needed - your code works fine here as is.
Okay, then it explains the erroneous behavior.
Works for you? Interesting. If anyone using DWM as well could confirm this, that would be nice.
]]>For the C code, XGetWindowProperty will work, but the error from any "non-terminal" windows is that there are child windows. You need to use XQueryTree in a loop to "drill down" to the deepest child window.
Here is an example from one of my projects to get the window the mouse is currently over - you'll need to revise to change XQueryPointer to XQueryTree:
XQueryPointer(dpy,root,&ev.xbutton.root,&ev.xbutton.window,&ev.xbutton.x_root,&ev.xbutton.y_root,&ev.xbutton.x,&ev.xbutton.y,&ev.xbutton.state);
ev.xbutton.subwindow = ev.xbutton.window;
while(ev.xbutton.subwindow) {
ev.xbutton.window = ev.xbutton.subwindow;
XQueryPointer(dpy,ev.xbutton.window,&ev.xbutton.root,&ev.xbutton.subwindow,&ev.xbutton.x_root,&ev.xbutton.y_root,&ev.xbutton.x,&ev.xbutton.y,&ev.xbutton.state);
}
EDIT: this may not actually be needed - your code works fine here as is.
]]>xprop -id $(xprop -root -f _NET_ACTIVE_WINDOW 0x " \$0\\n" _NET_ACTIVE_WINDOW | awk "{print \$2}") | awk '/WM_CLASS/{print $4}'
works for class retrieval under compiz, but returns "xprop: error: Invalid window id format: not." in DWM
Also, if I were to fetch class by win ID
xprop -id WINID
then it works only for terminal window as well. Returns nothing for other windows' IDs.
]]>My window manager is *far* less compliant with any of those standards than dwm is, and xprop works just fine.
But if you want to do it in C, check the xlib documentation for XGetWindowProperty. I don't understand any of the talk in this thread about window managers: if you are using xlib to query the x server directly, the window manager is completely irrelevant - you don't even need to have a window manager running.
The WM is only relevant if you are using tools like xdotool.
]]>I managed to patch this piece of code, but it fetches only the terminal window class; if any other window is made active, 'Segmentation fault' gets printed to stdout instead:
/* xgetfocus.c - link against libX11 */
#include <X11/Xlib.h>
#include <stdio.h>
#include <X11/Xutil.h>
void win_class(Display* d, Window w){
Status s;
XClassHint* class;
class = XAllocClassHint();
s = XGetClassHint(d, w, class);
printf("%s\n", class->res_class);
}
int main(int argc, char *argv[]) {
Display *d;
Window w;
char *window_name;
int revert;
d= XOpenDisplay(NULL);
XGetInputFocus(d, &w, &revert);
XFetchName(d, w, &window_name);
printf("%s\n", window_name);
win_class(d, w);
return 0;
}
EDIT: (why I'm even after this)
Window class is needed for a bash script which translates inputs depending on the window. For instance, if the window belongs to 'Chromium' class, then Altkey + Button6 would trigger
xdotool key --clearmodifiers alt+Left
in order to move back in history. Some other classes require different input and so on.
]]>xprop -id $(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) _NET_WM_NAME
Using xdotool:
xdotool getactivewindow getwindowname
You can also use xdotool to run commands on changes in focus:
xdotool search . behave %@ focus getwindowname
You can then either use the `exec` xdotool command to run external commands, or pipe the above into a `while read` loop. Note however that if you wish to do the latter there is a problem with the printf buffers in the current version (2.20101012.3049), but it is fixed in VCS.
Enjoy.
]]>[
(Did you put a '&' after calling 'urxvt' in autostart.sh?)
doh
that fixes it
thanks, guys. getting closer...
]]>.xinit is just:
/usr/bin/wminput -d -c boxee-buttons 00:19:1D:7C:C2:E1 &
exec openbox-sessionedit:
openbox configs are from
$ cp /etc/xdg/openbox/{rc.xml,menu.xml,autostart.sh} ~/.config/openboxand then I added "urxvt" to the bottom of autostart.
I do get a working urxvt, but none of the openbox keyboard commands that i just looked up seem to work....
Could you please make sure that Openbox is *running*.
(Did you put a '&' after calling 'urxvt' in autostart.sh?)
]]>greyscale wrote:You can get the windowid of the focused window with xdotool, feed that to xprop and dig out the title.
Here's a example that echo's the title (needs bash 4 to run)Nah, bash regex exists as far back as 3.2, but there's some small but odd differences in behavior during the ensuing dot releases. Your snippet (which is quite handy) runs just fine on my bash3 binary.
Thx, good to know.
Always used the more old-school way with sed etc until recent and read some writing on the net warn about it needing bash 4.
But might not have been the regexp they meant either since that article covered a couple of different topics on bash.
edit:
openbox configs are from
$ cp /etc/xdg/openbox/{rc.xml,menu.xml,autostart.sh} ~/.config/openbox
and then I added "urxvt" to the bottom of autostart.
I do get a working urxvt, but none of the openbox keyboard commands that i just looked up seem to work....
]]>Additionally, the error you get while running `xdotool getwindowfocus` is quite strange, and it doesn't rely on any EWMH adherence. Do you have anything special installed or weird that is running? Are you using a DE in addition to Openbox, or just plain Openbox? If the latter, what does your .xinitrc and/or ~/.config/openbox/autostart.sh look like?
]]>