dwm-meillo

diff mouse.c @ 18:1efa34c6e1b6

added mouse-based resizals
author Anselm R. Garbe <garbeam@wmii.de>
date Tue, 11 Jul 2006 21:24:10 +0200
parents
children b5510d0c6d43
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mouse.c	Tue Jul 11 21:24:10 2006 +0200
     1.3 @@ -0,0 +1,100 @@
     1.4 +/*
     1.5 + * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
     1.6 + * (C)opyright MMVI Kris Maglione <fbsdaemon@gmail.com>
     1.7 + * See LICENSE file for license details.
     1.8 + */
     1.9 +
    1.10 +#include <stdlib.h>
    1.11 +#include <string.h>
    1.12 +#include <unistd.h>
    1.13 +
    1.14 +#include "wm.h"
    1.15 +
    1.16 +#define ButtonMask      (ButtonPressMask | ButtonReleaseMask)
    1.17 +#define MouseMask       (ButtonMask | PointerMotionMask)
    1.18 +
    1.19 +static void
    1.20 +mmatch(Client *c, int x1, int y1, int x2, int y2)
    1.21 +{
    1.22 +	c->r[RFloat].width = abs(x1 - x2);
    1.23 +	c->r[RFloat].height = abs(y1 - y2);
    1.24 +	c->r[RFloat].width -=
    1.25 +		(c->r[RFloat].width - c->size.base_width) % c->size.width_inc;
    1.26 +	c->r[RFloat].height -=
    1.27 +		(c->r[RFloat].height - c->size.base_height) % c->size.height_inc;
    1.28 +	if(c->size.min_width && c->r[RFloat].width < c->size.min_width)
    1.29 +		c->r[RFloat].width = c->size.min_width;
    1.30 +	if(c->size.min_height && c->r[RFloat].height < c->size.min_height)
    1.31 +		c->r[RFloat].height = c->size.min_height;
    1.32 +	if(c->size.max_width && c->r[RFloat].width > c->size.max_width)
    1.33 +		c->r[RFloat].width = c->size.max_width;
    1.34 +	if(c->size.max_height && c->r[RFloat].height > c->size.max_height)
    1.35 +		c->r[RFloat].height = c->size.max_height;
    1.36 +	c->r[RFloat].x = (x1 <= x2) ? x1 : x1 - c->r[RFloat].width;
    1.37 +	c->r[RFloat].y = (y1 <= y2) ? y1 : y1 - c->r[RFloat].height;
    1.38 +}
    1.39 +
    1.40 +void
    1.41 +mresize(Client *c)
    1.42 +{
    1.43 +	XEvent ev;
    1.44 +	int old_cx, old_cy;
    1.45 +
    1.46 +	old_cx = c->r[RFloat].x;
    1.47 +	old_cy = c->r[RFloat].y;
    1.48 +	if(XGrabPointer(dpy, c->win, False, MouseMask, GrabModeAsync, GrabModeAsync,
    1.49 +				None, cursor[CurResize], CurrentTime) != GrabSuccess)
    1.50 +		return;
    1.51 +	XGrabServer(dpy);
    1.52 +	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
    1.53 +			c->r[RFloat].width, c->r[RFloat].height);
    1.54 +	for(;;) {
    1.55 +		XMaskEvent(dpy, MouseMask, &ev);
    1.56 +		switch(ev.type) {
    1.57 +		default: break;
    1.58 +		case MotionNotify:
    1.59 +			XUngrabServer(dpy);
    1.60 +			mmatch(c, old_cx, old_cy, ev.xmotion.x, ev.xmotion.y);
    1.61 +			resize(c);
    1.62 +			XGrabServer(dpy);
    1.63 +			break;
    1.64 +		case ButtonRelease:
    1.65 +			XUngrabPointer(dpy, CurrentTime);
    1.66 +			return;
    1.67 +		}
    1.68 +	}
    1.69 +}
    1.70 +
    1.71 +void
    1.72 +mmove(Client *c)
    1.73 +{
    1.74 +	XEvent ev;
    1.75 +	int x1, y1, old_cx, old_cy, di;
    1.76 +	unsigned int dui;
    1.77 +	Window dummy;
    1.78 +
    1.79 +	old_cx = c->r[RFloat].x;
    1.80 +	old_cy = c->r[RFloat].y;
    1.81 +	if(XGrabPointer(dpy, c->win, False, MouseMask, GrabModeAsync, GrabModeAsync,
    1.82 +				None, cursor[CurMove], CurrentTime) != GrabSuccess)
    1.83 +		return;
    1.84 +	XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
    1.85 +	XGrabServer(dpy);
    1.86 +	for(;;) {
    1.87 +		XMaskEvent(dpy, MouseMask, &ev);
    1.88 +		switch (ev.type) {
    1.89 +		default: break;
    1.90 +		case MotionNotify:
    1.91 +			XUngrabServer(dpy);
    1.92 +			c->r[RFloat].x = old_cx + (ev.xmotion.x - x1);
    1.93 +			c->r[RFloat].y = old_cy + (ev.xmotion.y - y1);
    1.94 +			resize(c);
    1.95 +			XGrabServer(dpy);
    1.96 +			break;
    1.97 +		case ButtonRelease:
    1.98 +			XUngrabServer(dpy);
    1.99 +			XUngrabPointer(dpy, CurrentTime);
   1.100 +			return;
   1.101 +		}
   1.102 +	}
   1.103 +}