diff --git a/calmwm.c b/calmwm.c index 39f53d8..1aa1cfe 100644 --- a/calmwm.c +++ b/calmwm.c @@ -116,7 +116,8 @@ main(int argc, char **argv) pfd[0].events = POLLIN; while (cwm_status == CWM_RUNNING) { xev_process(); - if (poll(pfd, 1, -1) == -1) { + xev_handle_timeout(); + if (poll(pfd, 1, xev_pending_timeout()) == -1) { if (errno != EINTR) warn("poll"); } diff --git a/calmwm.h b/calmwm.h index 2f94935..992bc03 100644 --- a/calmwm.h +++ b/calmwm.h @@ -585,6 +585,8 @@ void conf_screen(struct screen_ctx *); void conf_group(struct screen_ctx *); void xev_process(void); +int xev_pending_timeout(void); +void xev_handle_timeout(void); int xu_get_prop(Window, Atom, Atom, long, unsigned char **); int xu_get_strprop(Window, Atom, char **); diff --git a/xevents.c b/xevents.c index 1c50d31..1d5750d 100644 --- a/xevents.c +++ b/xevents.c @@ -30,11 +30,10 @@ #include #include #include -#include #include #include #include -#include +#include #include "calmwm.h" @@ -52,6 +51,12 @@ static void xev_handle_clientmessage(XEvent *); static void xev_handle_randr(XEvent *); static void xev_handle_mappingnotify(XEvent *); static void xev_handle_expose(XEvent *); +static void xev_cancel_hover_raise(void); +static void xev_schedule_hover_raise(Window); +static long long xev_now_ms(void); + +static Window hover_raise_win = None; +static long long hover_raise_at = -1; void (*xev_handlers[LASTEvent])(XEvent *) = { [MapRequest] = xev_handle_maprequest, @@ -75,6 +80,79 @@ static KeySym modkeys[] = { XK_Control_L, XK_Control_R, XK_Super_L, XK_Super_R, XK_ISO_Level3_Shift }; +static long long +xev_now_ms(void) +{ + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + return ((long long)ts.tv_sec * 1000LL + ts.tv_nsec / 1000000LL); +} + +static void +xev_cancel_hover_raise(void) +{ + hover_raise_win = None; + hover_raise_at = -1; +} + +static void +xev_schedule_hover_raise(Window win) +{ + if (!Conf.raiseonhover || Conf.raisedelay < 0) { + xev_cancel_hover_raise(); + return; + } + + hover_raise_win = win; + hover_raise_at = xev_now_ms() + Conf.raisedelay; +} + +int +xev_pending_timeout(void) +{ + long long now, remaining; + + if (hover_raise_win == None) + return (-1); + + now = xev_now_ms(); + remaining = hover_raise_at - now; + if (remaining <= 0) + return (0); + if (remaining > INT_MAX) + return (INT_MAX); + return ((int)remaining); +} + +void +xev_handle_timeout(void) +{ + struct client_ctx *cc; + Window win; + Window rwin, cwin; + int rx, ry, wx, wy; + unsigned int mask; + + if (hover_raise_win == None || xev_now_ms() < hover_raise_at) + return; + + win = hover_raise_win; + xev_cancel_hover_raise(); + + if ((cc = client_find(win)) == NULL || !(cc->flags & CLIENT_ACTIVE)) + return; + + if (!XQueryPointer(X_Dpy, cc->sc->rootwin, &rwin, &cwin, + &rx, &ry, &wx, &wy, &mask)) + return; + + if (cwin == cc->win) { + client_raise(cc); + XFlush(X_Dpy); + } +} + static void xev_handle_maprequest(XEvent *ee) { @@ -223,31 +301,6 @@ xev_handle_propertynotify(XEvent *ee) } } -static void* -delay_raise(void* data) -{ - struct client_ctx *cc; - Window *w = (Window*)data; - - int delay = Conf.raisedelay * 1000; - usleep(delay); - if ((cc = client_find(*w)) != NULL) { - if (cc->flags & CLIENT_ACTIVE) { - client_raise(cc); - } - } - free(w); - - XEvent e; - - XNextEvent(X_Dpy, &e); - if ((e.type - Conf.xrandr_event_base) == RRScreenChangeNotify) - xev_handle_randr(&e); - else if ((e.type < LASTEvent) && (xev_handlers[e.type] != NULL)) - (*xev_handlers[e.type])(&e); - pthread_exit(NULL); -} - static void xev_handle_enternotify(XEvent *ee) { @@ -261,14 +314,20 @@ xev_handle_enternotify(XEvent *ee) if ((cc = client_find(e->window)) != NULL) client_set_active(cc); - if (Conf.raiseonhover) { - Window *w; - w = (Window*)malloc(sizeof(Window)); - memcpy(w, &e->window, sizeof(Window)); + if (!Conf.raiseonhover) + return; - pthread_t thread_id; - pthread_create(&thread_id, NULL, delay_raise, w); + if (e->mode != NotifyNormal || e->detail == NotifyInferior) + return; + + if (e->state & (Button1Mask | Button2Mask | Button3Mask | + Button4Mask | Button5Mask)) { + xev_cancel_hover_raise(); + return; } + + if (cc != NULL) + xev_schedule_hover_raise(cc->win); } static void @@ -285,6 +344,8 @@ xev_handle_buttonpress(XEvent *ee) if ((sc = screen_find(e->root)) == NULL) return; + xev_cancel_hover_raise(); + e->state &= ~IGNOREMODMASK; TAILQ_FOREACH(mb, &Conf.mousebindq, entry) { @@ -527,5 +588,7 @@ xev_process(void) xev_handle_randr(&e); else if ((e.type < LASTEvent) && (xev_handlers[e.type] != NULL)) (*xev_handlers[e.type])(&e); + xev_handle_timeout(); } + xev_handle_timeout(); }