diff options
-rwxr-xr-x | focustest.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/focustest.c b/focustest.c new file mode 100755 index 0000000..3c9c775 --- /dev/null +++ b/focustest.c @@ -0,0 +1,52 @@ +#include <X11/Xlib.h> +#include <X11/Xutil.h> + +int main() { + Display *display; + int screen; + unsigned long white, black; + Window mainWindow, focusWindow; + XWMHints *hints; + Atom wmProtocols, wmTakeFocus; + XEvent e; + + display = XOpenDisplay(NULL); + if (!display) return 1; + screen = DefaultScreen(display); + white = WhitePixel(display, screen); + black = BlackPixel(display, screen); + + mainWindow = XCreateSimpleWindow(display, DefaultRootWindow(display), + 100, 100, 300, 200, 0, white, white); + focusWindow = XCreateSimpleWindow(display, mainWindow, + -1, -1, 1, 1, 0, white, white); + + hints = XAllocWMHints(); + hints->flags = InputHint; + hints->input = False; + XSetWMHints(display, mainWindow, hints); + + wmProtocols = XInternAtom(display, "WM_PROTOCOLS", False); + wmTakeFocus = XInternAtom(display, "WM_TAKE_FOCUS", False); + XSetWMProtocols(display, mainWindow, &wmTakeFocus, 1); + + XSelectInput(display, focusWindow, FocusChangeMask); + + XMapWindow(display, focusWindow); + XMapWindow(display, mainWindow); + + for(;;) { + XNextEvent(display, &e); + if (e.type == ClientMessage && + e.xclient.message_type == wmProtocols && + e.xclient.data.l[0] == wmTakeFocus) { + XSetInputFocus(display, focusWindow, RevertToNone, e.xclient.data.l[1]); + } else if (e.type == FocusIn) { + XSetWindowBackground(display, mainWindow, black); + XClearWindow(display, mainWindow); + } else if (e.type == FocusOut) { + XSetWindowBackground(display, mainWindow, white); + XClearWindow(display, mainWindow); + } + } +} |