JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added mouse-based resizals
[dwm.git] / mouse.c
1 /*
2  * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3  * (C)opyright MMVI Kris Maglione <fbsdaemon@gmail.com>
4  * See LICENSE file for license details.
5  */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10
11 #include "wm.h"
12
13 #define ButtonMask      (ButtonPressMask | ButtonReleaseMask)
14 #define MouseMask       (ButtonMask | PointerMotionMask)
15
16 static void
17 mmatch(Client *c, int x1, int y1, int x2, int y2)
18 {
19         c->r[RFloat].width = abs(x1 - x2);
20         c->r[RFloat].height = abs(y1 - y2);
21         c->r[RFloat].width -=
22                 (c->r[RFloat].width - c->size.base_width) % c->size.width_inc;
23         c->r[RFloat].height -=
24                 (c->r[RFloat].height - c->size.base_height) % c->size.height_inc;
25         if(c->size.min_width && c->r[RFloat].width < c->size.min_width)
26                 c->r[RFloat].width = c->size.min_width;
27         if(c->size.min_height && c->r[RFloat].height < c->size.min_height)
28                 c->r[RFloat].height = c->size.min_height;
29         if(c->size.max_width && c->r[RFloat].width > c->size.max_width)
30                 c->r[RFloat].width = c->size.max_width;
31         if(c->size.max_height && c->r[RFloat].height > c->size.max_height)
32                 c->r[RFloat].height = c->size.max_height;
33         c->r[RFloat].x = (x1 <= x2) ? x1 : x1 - c->r[RFloat].width;
34         c->r[RFloat].y = (y1 <= y2) ? y1 : y1 - c->r[RFloat].height;
35 }
36
37 void
38 mresize(Client *c)
39 {
40         XEvent ev;
41         int old_cx, old_cy;
42
43         old_cx = c->r[RFloat].x;
44         old_cy = c->r[RFloat].y;
45         if(XGrabPointer(dpy, c->win, False, MouseMask, GrabModeAsync, GrabModeAsync,
46                                 None, cursor[CurResize], CurrentTime) != GrabSuccess)
47                 return;
48         XGrabServer(dpy);
49         XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
50                         c->r[RFloat].width, c->r[RFloat].height);
51         for(;;) {
52                 XMaskEvent(dpy, MouseMask, &ev);
53                 switch(ev.type) {
54                 default: break;
55                 case MotionNotify:
56                         XUngrabServer(dpy);
57                         mmatch(c, old_cx, old_cy, ev.xmotion.x, ev.xmotion.y);
58                         resize(c);
59                         XGrabServer(dpy);
60                         break;
61                 case ButtonRelease:
62                         XUngrabPointer(dpy, CurrentTime);
63                         return;
64                 }
65         }
66 }
67
68 void
69 mmove(Client *c)
70 {
71         XEvent ev;
72         int x1, y1, old_cx, old_cy, di;
73         unsigned int dui;
74         Window dummy;
75
76         old_cx = c->r[RFloat].x;
77         old_cy = c->r[RFloat].y;
78         if(XGrabPointer(dpy, c->win, False, MouseMask, GrabModeAsync, GrabModeAsync,
79                                 None, cursor[CurMove], CurrentTime) != GrabSuccess)
80                 return;
81         XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
82         XGrabServer(dpy);
83         for(;;) {
84                 XMaskEvent(dpy, MouseMask, &ev);
85                 switch (ev.type) {
86                 default: break;
87                 case MotionNotify:
88                         XUngrabServer(dpy);
89                         c->r[RFloat].x = old_cx + (ev.xmotion.x - x1);
90                         c->r[RFloat].y = old_cy + (ev.xmotion.y - y1);
91                         resize(c);
92                         XGrabServer(dpy);
93                         break;
94                 case ButtonRelease:
95                         XUngrabServer(dpy);
96                         XUngrabPointer(dpy, CurrentTime);
97                         return;
98                 }
99         }
100 }