JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
07b533c0438488555f52282607703ba6e3d98ceb
[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, 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,
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                         XResizeWindow(dpy, c->win, c->r[RFloat].width, c->r[RFloat].height);
59                         XGrabServer(dpy);
60                         break;
61                 case ButtonRelease:
62                         resize(c);
63                         XUngrabServer(dpy);
64                         XUngrabPointer(dpy, CurrentTime);
65                         return;
66                 }
67         }
68 }
69
70 void
71 mmove(Client *c)
72 {
73         XEvent ev;
74         int x1, y1, old_cx, old_cy, di;
75         unsigned int dui;
76         Window dummy;
77
78         old_cx = c->r[RFloat].x;
79         old_cy = c->r[RFloat].y;
80         if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
81                                 None, cursor[CurMove], CurrentTime) != GrabSuccess)
82                 return;
83         XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
84         XGrabServer(dpy);
85         for(;;) {
86                 XMaskEvent(dpy, MouseMask, &ev);
87                 switch (ev.type) {
88                 default: break;
89                 case MotionNotify:
90                         XUngrabServer(dpy);
91                         c->r[RFloat].x = old_cx + (ev.xmotion.x - x1);
92                         c->r[RFloat].y = old_cy + (ev.xmotion.y - y1);
93                         XMoveResizeWindow(dpy, c->win, c->r[RFloat].x, c->r[RFloat].y,
94                                         c->r[RFloat].width, c->r[RFloat].height);
95                         XGrabServer(dpy);
96                         break;
97                 case ButtonRelease:
98                         resize(c);
99                         XUngrabServer(dpy);
100                         XUngrabPointer(dpy, CurrentTime);
101                         return;
102                 }
103         }
104 }