Skip to content

Instantly share code, notes, and snippets.

@jstimpfle
Created January 31, 2017 22:19
Show Gist options
  • Save jstimpfle/3ffc4d09587c544d39e6ce930fa81c5b to your computer and use it in GitHub Desktop.
Save jstimpfle/3ffc4d09587c544d39e6ce930fa81c5b to your computer and use it in GitHub Desktop.
Skeleton for debugging focus problems with X11 window managers
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
Display *dpy;
Window rootwin;
Atom atom_WM_NAME;
Window appwin;
Window childwin;
static void fill_rect(Window win, unsigned long color, int x, int y, int width, int height)
{
XGCValues values;
unsigned long valuemask;
GC gc;
valuemask = GCForeground;
values.foreground = color;
gc = XCreateGC(dpy, win, valuemask, &values);
XClearWindow(dpy, win);
XFillRectangle(dpy, win, gc, x, y, width, height);
XFreeGC(dpy, gc);
}
static void set_class_hint(Window win, const char *res_name, const char *res_class)
{
XClassHint *hint = XAllocClassHint();
hint->res_name = (char *) res_name;
hint->res_class = (char *) res_class;
XSetClassHint(dpy, win, hint);
XFree(hint);
}
static void set_wm_name(Window win, const char *wmname)
{
XTextProperty prop;
XmbTextListToTextProperty(dpy, (char **)&wmname, 1, XStringStyle, &prop);
XSetWMName(dpy, win, &prop);
XFree(prop.value);
}
static void set_wm_hints(Window win)
{
XWMHints *wmhints = XAllocWMHints();
wmhints->flags = InputHint;
wmhints->input = True;
XSetWMHints(dpy, win, wmhints);
XFree(wmhints);
}
static void set_transient_for(Window win, Window owner)
{
XSetTransientForHint(dpy, win, owner);
}
int main(void)
{
dpy = XOpenDisplay(NULL);
rootwin = DefaultRootWindow(dpy);
atom_WM_NAME = XInternAtom(dpy, "WM_NAME", False);
appwin = XCreateSimpleWindow(
dpy, rootwin, 200, 200, 400, 400, 1,
XBlackPixel(dpy, rootwin),
XBlackPixel(dpy, rootwin));
childwin = XCreateSimpleWindow(
dpy, appwin, 250, 250, 300, 300, 1,
XBlackPixel(dpy, rootwin),
XBlackPixel(dpy, rootwin));
XSelectInput(dpy, appwin, ExposureMask);
XSelectInput(dpy, childwin, ExposureMask);
set_wm_name(appwin, "MyTestProg");
set_class_hint(appwin, "foo", "bar");
set_wm_hints(appwin);
set_wm_name(childwin, "child window");
set_transient_for(childwin, appwin);
XMapWindow(dpy, appwin);
XMapWindow(dpy, childwin);
XSync(dpy, False);
while (1) {
XEvent xev;
XNextEvent(dpy, &xev);
switch (xev.type) {
case Expose:
fill_rect(appwin, 0xBEBEBE, 50, 50, 1000, 1000);
fill_rect(childwin, 0x00FF00, 0, 0, 1000, 1000);
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment