Compare commits

...

3 Commits

Author SHA1 Message Date
Ryan Goniwiecha
f1dd7ceb04 snap to half commands 2026-04-20 14:01:25 -04:00
Ryan Goniwiecha
9618137422 fix first mouse resize 2026-04-19 17:12:51 -04:00
Ryan Goniwiecha
00f6b03503 proper hover on raise 2026-04-19 16:45:28 -04:00
7 changed files with 196 additions and 35 deletions

View File

@@ -116,7 +116,8 @@ main(int argc, char **argv)
pfd[0].events = POLLIN; pfd[0].events = POLLIN;
while (cwm_status == CWM_RUNNING) { while (cwm_status == CWM_RUNNING) {
xev_process(); xev_process();
if (poll(pfd, 1, -1) == -1) { xev_handle_timeout();
if (poll(pfd, 1, xev_pending_timeout()) == -1) {
if (errno != EINTR) if (errno != EINTR)
warn("poll"); warn("poll");
} }

View File

@@ -519,6 +519,7 @@ void screen_updatestackingorder(struct screen_ctx *);
void kbfunc_cwm_status(void *, struct cargs *); void kbfunc_cwm_status(void *, struct cargs *);
void kbfunc_ptrmove(void *, struct cargs *); void kbfunc_ptrmove(void *, struct cargs *);
void kbfunc_client_snap(void *, struct cargs *); void kbfunc_client_snap(void *, struct cargs *);
void kbfunc_client_snaptohalf(void *, struct cargs *);
void kbfunc_client_move(void *, struct cargs *); void kbfunc_client_move(void *, struct cargs *);
void kbfunc_client_resize(void *, struct cargs *); void kbfunc_client_resize(void *, struct cargs *);
void kbfunc_client_close(void *, struct cargs *); void kbfunc_client_close(void *, struct cargs *);
@@ -585,6 +586,8 @@ void conf_screen(struct screen_ctx *);
void conf_group(struct screen_ctx *); void conf_group(struct screen_ctx *);
void xev_process(void); 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_prop(Window, Atom, Atom, long, unsigned char **);
int xu_get_strprop(Window, Atom, char **); int xu_get_strprop(Window, Atom, char **);

View File

@@ -79,6 +79,8 @@ client_init(Window win, struct screen_ctx *sc)
cc->geom.y = wattr.y; cc->geom.y = wattr.y;
cc->geom.w = wattr.width; cc->geom.w = wattr.width;
cc->geom.h = wattr.height; cc->geom.h = wattr.height;
cc->savegeom = cc->geom;
cc->fullgeom = cc->geom;
cc->colormap = wattr.colormap; cc->colormap = wattr.colormap;
cc->obwidth = wattr.border_width; cc->obwidth = wattr.border_width;
cc->bwidth = Conf.bwidth; cc->bwidth = Conf.bwidth;

4
conf.c
View File

@@ -107,6 +107,10 @@ static const struct {
{ FUNC_CC(window-snap-down, client_snap, (CWM_DOWN)) }, { FUNC_CC(window-snap-down, client_snap, (CWM_DOWN)) },
{ FUNC_CC(window-snap-right, client_snap, (CWM_RIGHT)) }, { FUNC_CC(window-snap-right, client_snap, (CWM_RIGHT)) },
{ FUNC_CC(window-snap-left, client_snap, (CWM_LEFT)) }, { FUNC_CC(window-snap-left, client_snap, (CWM_LEFT)) },
{ FUNC_CC(window-snap-up-half, client_snaptohalf, (CWM_UP)) },
{ FUNC_CC(window-snap-down-half, client_snaptohalf, (CWM_DOWN)) },
{ FUNC_CC(window-snap-right-half, client_snaptohalf, (CWM_RIGHT)) },
{ FUNC_CC(window-snap-left-half, client_snaptohalf, (CWM_LEFT)) },
{ FUNC_CC(window-snap-up-right, client_snap, (CWM_UP_RIGHT)) }, { FUNC_CC(window-snap-up-right, client_snap, (CWM_UP_RIGHT)) },
{ FUNC_CC(window-snap-up-left, client_snap, (CWM_UP_LEFT)) }, { FUNC_CC(window-snap-up-left, client_snap, (CWM_UP_LEFT)) },
{ FUNC_CC(window-snap-down-right, client_snap, (CWM_DOWN_RIGHT)) }, { FUNC_CC(window-snap-down-right, client_snap, (CWM_DOWN_RIGHT)) },

View File

@@ -411,6 +411,14 @@ Snap window to bottom edge.
Snap window to right edge. Snap window to right edge.
.It window-snap-left .It window-snap-left
Snap window to left edge. Snap window to left edge.
.It window-snap-up-half
Snap and resize window to the top half of the screen.
.It window-snap-down-half
Snap and resize window to the bottom half of the screen.
.It window-snap-right-half
Snap and resize window to the right half of the screen.
.It window-snap-left-half
Snap and resize window to the left half of the screen.
.It window-snap-up-right .It window-snap-up-right
Snap window to top-right corner. Snap window to top-right corner.
.It window-snap-up-left .It window-snap-up-left

View File

@@ -47,6 +47,7 @@ static void kbfunc_client_move_kb(void *, struct cargs *);
static void kbfunc_client_move_mb(void *, struct cargs *); static void kbfunc_client_move_mb(void *, struct cargs *);
static void kbfunc_client_resize_kb(void *, struct cargs *); static void kbfunc_client_resize_kb(void *, struct cargs *);
static void kbfunc_client_resize_mb(void *, struct cargs *); static void kbfunc_client_resize_mb(void *, struct cargs *);
static Bool kbfunc_discard_configurerequest(Display *, XEvent *, XPointer);
void void
kbfunc_cwm_status(void *ctx, struct cargs *cargs) kbfunc_cwm_status(void *ctx, struct cargs *cargs)
@@ -54,6 +55,17 @@ kbfunc_cwm_status(void *ctx, struct cargs *cargs)
cwm_status = cargs->flag; cwm_status = cargs->flag;
} }
static Bool
kbfunc_discard_configurerequest(Display *dpy, XEvent *ev, XPointer arg)
{
Window *win = (Window *)arg;
(void)dpy;
return (ev->type == ConfigureRequest &&
ev->xconfigurerequest.window == *win);
}
static void static void
kbfunc_amount(int flags, int amt, int *mx, int *my) kbfunc_amount(int flags, int amt, int *mx, int *my)
{ {
@@ -242,6 +254,7 @@ kbfunc_client_resize_mb(void *ctx, struct cargs *cargs)
{ {
struct client_ctx *cc = ctx; struct client_ctx *cc = ctx;
XEvent ev; XEvent ev;
XEvent cev;
Time ltime = 0; Time ltime = 0;
struct screen_ctx *sc = cc->sc; struct screen_ctx *sc = cc->sc;
int resize = 1; int resize = 1;
@@ -282,8 +295,19 @@ kbfunc_client_resize_mb(void *ctx, struct cargs *cargs)
break; break;
} }
} }
if (ltime) if (ltime) {
/*
* A newly mapped client can queue a ConfigureRequest while the
* resize loop is consuming only mouse events. Drop those stale
* requests so the final drag geometry is not immediately
* overwritten when the main event loop resumes.
*/
while (XCheckIfEvent(X_Dpy, &cev,
kbfunc_discard_configurerequest,
(XPointer)&cc->win))
;
client_resize(cc, 1); client_resize(cc, 1);
}
screen_prop_win_destroy(sc); screen_prop_win_destroy(sc);
XUngrabPointer(X_Dpy, CurrentTime); XUngrabPointer(X_Dpy, CurrentTime);
@@ -335,6 +359,62 @@ kbfunc_client_snap(void *ctx, struct cargs *cargs)
client_ptr_inbound(cc, 1); client_ptr_inbound(cc, 1);
} }
void
kbfunc_client_snaptohalf(void *ctx, struct cargs *cargs)
{
struct client_ctx *cc = ctx;
struct screen_ctx *sc = cc->sc;
struct geom area;
int halfw, halfh;
if (cc->flags & CLIENT_FREEZE)
return;
area = screen_area(sc,
cc->geom.x + cc->geom.w / 2,
cc->geom.y + cc->geom.h / 2, 1);
halfw = area.w / 2;
halfh = area.h / 2;
switch (cargs->flag) {
case CWM_UP:
cc->geom.x = area.x;
cc->geom.y = area.y;
cc->geom.w = area.w - (cc->bwidth * 2);
cc->geom.h = halfh - (cc->bwidth * 2);
client_apply_sizehints(cc);
break;
case CWM_DOWN:
cc->geom.x = area.x;
cc->geom.w = area.w - (cc->bwidth * 2);
cc->geom.h = area.h - halfh - (cc->bwidth * 2);
client_apply_sizehints(cc);
cc->geom.y = area.y + area.h - cc->geom.h -
(cc->bwidth * 2);
break;
case CWM_LEFT:
cc->geom.x = area.x;
cc->geom.y = area.y;
cc->geom.w = halfw - (cc->bwidth * 2);
cc->geom.h = area.h - (cc->bwidth * 2);
client_apply_sizehints(cc);
break;
case CWM_RIGHT:
cc->geom.y = area.y;
cc->geom.w = area.w - halfw - (cc->bwidth * 2);
cc->geom.h = area.h - (cc->bwidth * 2);
client_apply_sizehints(cc);
cc->geom.x = area.x + area.w - cc->geom.w -
(cc->bwidth * 2);
break;
default:
return;
}
client_resize(cc, 1);
client_ptr_inbound(cc, 1);
}
void void
kbfunc_client_close(void *ctx, struct cargs *cargs) kbfunc_client_close(void *ctx, struct cargs *cargs)
{ {

129
xevents.c
View File

@@ -30,11 +30,10 @@
#include <err.h> #include <err.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <time.h>
#include "calmwm.h" #include "calmwm.h"
@@ -52,6 +51,12 @@ static void xev_handle_clientmessage(XEvent *);
static void xev_handle_randr(XEvent *); static void xev_handle_randr(XEvent *);
static void xev_handle_mappingnotify(XEvent *); static void xev_handle_mappingnotify(XEvent *);
static void xev_handle_expose(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 *) = { void (*xev_handlers[LASTEvent])(XEvent *) = {
[MapRequest] = xev_handle_maprequest, [MapRequest] = xev_handle_maprequest,
@@ -75,6 +80,79 @@ static KeySym modkeys[] = { XK_Control_L, XK_Control_R,
XK_Super_L, XK_Super_R, XK_Super_L, XK_Super_R,
XK_ISO_Level3_Shift }; 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 static void
xev_handle_maprequest(XEvent *ee) 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 static void
xev_handle_enternotify(XEvent *ee) xev_handle_enternotify(XEvent *ee)
{ {
@@ -261,14 +314,20 @@ xev_handle_enternotify(XEvent *ee)
if ((cc = client_find(e->window)) != NULL) if ((cc = client_find(e->window)) != NULL)
client_set_active(cc); client_set_active(cc);
if (Conf.raiseonhover) { if (!Conf.raiseonhover)
Window *w; return;
w = (Window*)malloc(sizeof(Window));
memcpy(w, &e->window, sizeof(Window));
pthread_t thread_id; if (e->mode != NotifyNormal || e->detail == NotifyInferior)
pthread_create(&thread_id, NULL, delay_raise, w); 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 static void
@@ -285,6 +344,8 @@ xev_handle_buttonpress(XEvent *ee)
if ((sc = screen_find(e->root)) == NULL) if ((sc = screen_find(e->root)) == NULL)
return; return;
xev_cancel_hover_raise();
e->state &= ~IGNOREMODMASK; e->state &= ~IGNOREMODMASK;
TAILQ_FOREACH(mb, &Conf.mousebindq, entry) { TAILQ_FOREACH(mb, &Conf.mousebindq, entry) {
@@ -527,5 +588,7 @@ xev_process(void)
xev_handle_randr(&e); xev_handle_randr(&e);
else if ((e.type < LASTEvent) && (xev_handlers[e.type] != NULL)) else if ((e.type < LASTEvent) && (xev_handlers[e.type] != NULL))
(*xev_handlers[e.type])(&e); (*xev_handlers[e.type])(&e);
xev_handle_timeout();
} }
xev_handle_timeout();
} }