From: Michael D. Lowis Date: Fri, 13 Mar 2020 16:34:43 +0000 (-0400) Subject: tweaked mouse handling on resize and added different cursors for frame and root windows X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=f9af6c7a6fa4c8311bb0b3c95cd30e8d582b341d;p=proto%2Fanvil.git tweaked mouse handling on resize and added different cursors for frame and root windows --- diff --git a/anvil.c b/anvil.c index 0540e1e..bf0647d 100644 --- a/anvil.c +++ b/anvil.c @@ -16,6 +16,16 @@ static inline int PRESSED(int mods, int btn) return ((mods & (1 << (btn + 7))) == (1 << (btn + 7))); } +static void warp_mouse(Client* c) +{ + XWarpPointer(X.disp, None, c->frame, 0, 0, 0, 0, + c->w + 2*BORDER_WIDTH - BORDER_WIDTH/2, + c->h + 2*BORDER_WIDTH + TITLE_HEIGHT - BORDER_WIDTH/2 + ); + X.start_x = c->x + c->w + 4; + X.start_y = c->y + c->h + 4; +} + static void xbtnpress(XEvent* e) { XButtonEvent* ev = &(e->xbutton); @@ -29,12 +39,7 @@ static void xbtnpress(XEvent* e) if (ev->y > (TITLE_HEIGHT + BORDER_WIDTH)) { X.reshaping = 1; - XWarpPointer(X.disp, None, ev->window, 0, 0, 0, 0, - c->w + 2*BORDER_WIDTH, - c->h + 2*BORDER_WIDTH + TITLE_HEIGHT - ); - X.start_x = c->x + c->w + 2*BORDER_WIDTH; - X.start_y = c->y + c->h + 2*BORDER_WIDTH + TITLE_HEIGHT; + warp_mouse(c); } } else if (ev->button == Button2) { client_close(c); @@ -52,16 +57,12 @@ static void xbtnrelease(XEvent* e) static void xbtnmotion(XEvent* e) { + /* make sure we get just the latest event */ XMotionEvent *ev = &e->xmotion; - while (XCheckTypedWindowEvent(X.disp, ev->window, ev->type, e)) - { - /* Keep grabbing them till we have no more to grab */ - } - + while (XCheckTypedWindowEvent(X.disp, ev->window, ev->type, e)); Client* c = client_get(ev->window); if (c && (ev->window == c->frame)) { - /* TODO: only move window if Button1 held */ if (PRESSED(ev->state, Button1)) { if (!X.reshaping) @@ -96,6 +97,10 @@ static void xconfigrequest(XEvent* e) c->w = wc.width; c->h = wc.height; client_resize(c, 0, 0); + if (X.reshaping && Focused == c) + { + warp_mouse(c); + } } } @@ -170,8 +175,7 @@ static void xkeypress(XEvent* e) int main(void) { - XColor color, exact; - + XColor color, red, white, exact; /* Initialize X server*/ check( (X.disp = XOpenDisplay(0)) != NULL, "could not open display"); @@ -186,6 +190,15 @@ int main(void) client_initall(); keys_init(); + /* configure mouse cursors */ + Colormap cmap = DefaultColormap(X.disp, X.screen); + X.csr_root = XCreateFontCursor(X.disp, XC_left_ptr); + XAllocNamedColor(X.disp, cmap, "red", &red, &exact); + XAllocNamedColor(X.disp, cmap, "white", &white, &exact); + X.csr_point = XCreateFontCursor(X.disp, XC_left_ptr); + XRecolorCursor(X.disp, X.csr_point, &red, &white); + XDefineCursor(X.disp, X.root, X.csr_root); + /* setup event handlers */ X.eventfns[ButtonPress] = xbtnpress; X.eventfns[ButtonRelease] = xbtnrelease; diff --git a/anvil.h b/anvil.h index 6812b11..7b454a0 100644 --- a/anvil.h +++ b/anvil.h @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,7 @@ typedef struct { unsigned long black, white, gray; int start_x, start_y; int reshaping; + Cursor csr_root, csr_point; void (*eventfns[LASTEvent])(XEvent*); } XConf; diff --git a/client.c b/client.c index 09bfc14..99a4c14 100644 --- a/client.c +++ b/client.c @@ -74,6 +74,7 @@ void client_draw(Client* c) { XSetWindowBackground(X.disp, c->frame, (Focused == c) ? X.black : X.gray); XClearWindow(X.disp, c->frame); + XDefineCursor(X.disp, c->frame, X.csr_point); // int quarter = (border + titleHeight()) / 4; // /* Draw window title. */