JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
577f23cb22f4cda96a932e769c80b204790450aa
[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->w = abs(x1 - x2);
20         c->h = abs(y1 - y2);
21         if(c->incw)
22                 c->w -= (c->w - c->basew) % c->incw;
23         if(c->inch)
24                 c->h -= (c->h - c->baseh) % c->inch;
25         if(c->minw && c->w < c->minw)
26                 c->w = c->minw;
27         if(c->minh && c->h < c->minh)
28                 c->h = c->minh;
29         if(c->maxw && c->w > c->maxw)
30                 c->w = c->maxw;
31         if(c->maxh && c->h > c->maxh)
32                 c->h = c->maxh;
33         c->x = (x1 <= x2) ? x1 : x1 - c->w;
34         c->y = (y1 <= y2) ? y1 : y1 - c->h;
35 }
36
37 void
38 mresize(Client *c)
39 {
40         XEvent ev;
41         int old_cx, old_cy;
42
43         old_cx = c->x;
44         old_cy = c->y;
45         if(XGrabPointer(dpy, root, 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, c->w, c->h);
50         for(;;) {
51                 XMaskEvent(dpy, MouseMask, &ev);
52                 switch(ev.type) {
53                 default: break;
54                 case MotionNotify:
55                         XUngrabServer(dpy);
56                         mmatch(c, old_cx, old_cy, ev.xmotion.x, ev.xmotion.y);
57                         XResizeWindow(dpy, c->win, c->w, c->h);
58                         XGrabServer(dpy);
59                         break;
60                 case ButtonRelease:
61                         resize(c);
62                         XUngrabServer(dpy);
63                         XUngrabPointer(dpy, CurrentTime);
64                         return;
65                 }
66         }
67 }
68
69 void
70 mmove(Client *c)
71 {
72         XEvent ev;
73         int x1, y1, old_cx, old_cy, di;
74         unsigned int dui;
75         Window dummy;
76
77         old_cx = c->x;
78         old_cy = c->y;
79         if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
80                                 None, cursor[CurMove], CurrentTime) != GrabSuccess)
81                 return;
82         XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
83         XGrabServer(dpy);
84         for(;;) {
85                 XMaskEvent(dpy, MouseMask, &ev);
86                 switch (ev.type) {
87                 default: break;
88                 case MotionNotify:
89                         XUngrabServer(dpy);
90                         c->x = old_cx + (ev.xmotion.x - x1);
91                         c->y = old_cy + (ev.xmotion.y - y1);
92                         XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
93                         XGrabServer(dpy);
94                         break;
95                 case ButtonRelease:
96                         resize(c);
97                         XUngrabServer(dpy);
98                         XUngrabPointer(dpy, CurrentTime);
99                         return;
100                 }
101         }
102 }