summaryrefslogtreecommitdiff
path: root/x-focus-manager.c
diff options
context:
space:
mode:
authorPatrick Haller <patrick@haller.ws>2011-03-02 07:44:47 +0800
committerPatrick Haller <patrick@haller.ws>2011-03-02 07:44:47 +0800
commitb6a9a652c5e9c6b763270d9e32b77310a6ba4bb4 (patch)
tree3fe83fd25c1bcc84aa711b691c32ded5cbaa7c37 /x-focus-manager.c
parent5502d7862f299f535800b4037f20fd0021d856e4 (diff)
updated to use UnmapNotify and DestroyNotify
Diffstat (limited to 'x-focus-manager.c')
-rw-r--r--x-focus-manager.c145
1 files changed, 44 insertions, 101 deletions
diff --git a/x-focus-manager.c b/x-focus-manager.c
index ef7df94..69c9e96 100644
--- a/x-focus-manager.c
+++ b/x-focus-manager.c
@@ -1,130 +1,73 @@
+#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
+#include <X11/cursorfont.h>
-const char unselected_border_color[] = "#cccccc";
-const char selected_border_color[] = "#0066ff";
+/* some clients fail to reset focus on exit
+ * we watch for that, and fix it
+ *
+ * instead of this, we also could use Elliott Hughes' clock
+ * to launch x-alt-tab
+ */
-typedef struct {
- unsigned long unselected_border_color;
- unsigned long selected_border_color;
-} Config;
-typedef struct {
- Display *display;
- int screen;
- int is_other_wm_live;
- char *argv0;
-} State;
+void reset_focus(Display *d, XEvent *e);
-State state;
-Config config;
-
-void buttonpress(XEvent *e);
-
-static void (*handler[LASTEvent]) (XEvent *) = {
- [ButtonPress] = buttonpress
+static void (*handler[LASTEvent]) (Display *d, XEvent *) = {
+ [UnmapNotify] = reset_focus,
+ [DestroyNotify] = reset_focus
};
/// fun on down
void
-die(const char *errstr, ...) {
- va_list ap;
-
- fprintf(stderr, "%s: ", state.argv0);
- va_start(ap, errstr);
- vfprintf(stderr, errstr, ap);
- va_end(ap);
- fprintf(stderr, "\n");
- exit(EXIT_FAILURE);
-}
-
-int
-xerror(Display *display, XErrorEvent *ee) {
- if(ee->error_code == BadWindow
- || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
- || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
- || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
- || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
- || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
- || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
- || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
- || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
- return 0;
- die("fatal error: request code=%d, error code=%d",
- ee->request_code, ee->error_code);
- return 1;
+reset_focus(Display *d, XEvent *e) {
+ XSetInputFocus(d, PointerRoot , RevertToPointerRoot, CurrentTime);
}
int
-xerrorstart(Display *display, XErrorEvent *ee) {
- state.is_other_wm_live = True;
- return -1;
-}
-
-void
-check_other_wm() {
- state.is_other_wm_live = False;
- XSetErrorHandler(xerrorstart);
- /* this causes an error if some other window manager is running */
- XSelectInput(state.display, DefaultRootWindow(state.display), SubstructureRedirectMask);
- XSync(state.display, False);
- if(state.is_other_wm_live)
- die("another window manager is already running");
- XSetErrorHandler(xerror);
- XSync(state.display, False);
-}
-
-unsigned long
-get_color(const char *colstr) {
- Colormap cmap = DefaultColormap(state.display, state.screen);
- XColor color;
-
- if(!XAllocNamedColor(state.display, cmap, colstr, &color, &color))
- die("error, cannot allocate color '%s'\n", colstr);
- return color.pixel;
-}
-
-void
-buttonpress(XEvent *e) {
- XButtonPressedEvent *ev = &e->xbutton;
-
- XSetWindowBorder(state.display, ev->window, config.selected_border_color);
- XSetInputFocus(state.display, ev->window, RevertToPointerRoot, CurrentTime);
+interruptibleXNextEvent(Display *dpy, XEvent *event) {
+ fd_set fds;
+ int rc;
+ int dpy_fd = ConnectionNumber(dpy);
+ for (;;) {
+ if (XPending(dpy)) {
+ XNextEvent(dpy, event);
+ return 1;
+ }
+ FD_ZERO(&fds);
+ FD_SET(dpy_fd, &fds);
+ rc = select(dpy_fd + 1, &fds, NULL, NULL, NULL);
+ if (rc < 0) {
+ if (errno == EINTR) {
+ return 0;
+ }
+ // LOG_ERROR("interruptibleXNextEvent(): select()\n");
+ }
+ }
}
-/*
-XSelectInput(display, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
-wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
- |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
- |PropertyChangeMask;
-XChangeWindowAttributes(display, root, CWEventMask|CWCursor, &wa);
-XSelectInput(display, root, wa.event_mask);
-*/
-
-
int
main(int argc, char **argv) {
+ Display *dpy;
XEvent e;
+ XSetWindowAttributes wa;
- state.argv0 = argv[0];
+ if(!(dpy = XOpenDisplay(NULL)))
+ return 1;
- if(!(state.display = XOpenDisplay(NULL)))
- die("Could not open display!");
- state.screen = 0;
- check_other_wm();
+ wa.cursor = XCreateFontCursor(dpy, XC_left_ptr);
+ wa.event_mask = SubstructureNotifyMask|StructureNotifyMask;
+ XChangeWindowAttributes(dpy, DefaultRootWindow(dpy) , CWEventMask|CWCursor, &wa);
- config.selected_border_color = get_color(selected_border_color);
- config.unselected_border_color = get_color(unselected_border_color);
-
- while(!XNextEvent(state.display, &e)) {
+ for(;;) {
+ if (! interruptibleXNextEvent(dpy, &e))
+ continue;
if(handler[e.type])
- handler[e.type](&e); /* call handler */
+ handler[e.type](dpy, &e);
}
-
- return 0;
}