allow to refresh layout when resizing to see in live what's happening
[awesome.git] / mouse.c
blob02a98a5634e29b736075242415a70613ac0fd450
1 /*
2 * mouse.c - mouse managing
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <math.h>
24 #include "mouse.h"
25 #include "screen.h"
26 #include "layout.h"
27 #include "tag.h"
28 #include "event.h"
29 #include "window.h"
30 #include "client.h"
31 #include "layouts/floating.h"
32 #include "layouts/tile.h"
33 #include "common/util.h"
35 extern AwesomeConf globalconf;
37 /** Move client with mouse
38 * \param screen Screen ID
39 * \param arg Unused
40 * \ingroup ui_callback
42 void
43 uicb_client_movemouse(int screen, char *arg __attribute__ ((unused)))
45 int x, y, ocx, ocy, di, phys_screen, newscreen;
46 unsigned int dui;
47 Window dummy, child;
48 XEvent ev;
49 Area area, geometry;
50 Client *c = globalconf.focus->client, *target;
51 Layout *layout = get_current_layout(screen);
53 if(!c)
54 return;
56 /* go above everybody */
57 XMapRaised(globalconf.display, c->win);
59 area = get_screen_area(c->screen,
60 globalconf.screens[screen].statusbar,
61 &globalconf.screens[screen].padding);
63 ocx = geometry.x = c->geometry.x;
64 ocy = geometry.y = c->geometry.y;
65 phys_screen = get_phys_screen(c->screen);
66 if(XGrabPointer(globalconf.display,
67 RootWindow(globalconf.display, phys_screen),
68 False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
69 RootWindow(globalconf.display, phys_screen),
70 globalconf.cursor[CurMove], CurrentTime) != GrabSuccess)
71 return;
72 XQueryPointer(globalconf.display,
73 RootWindow(globalconf.display, phys_screen),
74 &dummy, &dummy, &x, &y, &di, &di, &dui);
75 c->ismax = False;
76 for(;;)
78 XMaskEvent(globalconf.display, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
79 switch (ev.type)
81 case ButtonRelease:
82 if(!c->isfloating && layout->arrange != layout_floating)
84 XQueryPointer(globalconf.display, RootWindow(globalconf.display, phys_screen),
85 &dummy, &child, &x, &y, &di, &di, &dui);
86 if((newscreen = get_screen_bycoord(x, y)) != screen)
88 move_client_to_screen(c, newscreen, True);
89 globalconf.screens[screen].need_arrange = True;
90 globalconf.screens[newscreen].need_arrange = True;
92 if((target = get_client_bywin(globalconf.clients, child)) && target != c)
94 client_list_swap(&globalconf.clients, c, target);
95 globalconf.screens[screen].need_arrange = True;
98 XUngrabPointer(globalconf.display, CurrentTime);
99 return;
100 case ConfigureRequest:
101 handle_event_configurerequest(&ev);
102 break;
103 case Expose:
104 handle_event_expose(&ev);
105 break;
106 case MapRequest:
107 handle_event_maprequest(&ev);
108 break;
109 case EnterNotify:
110 handle_event_enternotify(&ev);
111 break;
112 case MotionNotify:
113 if(c->isfloating || layout->arrange == layout_floating)
115 geometry.x = ocx + (ev.xmotion.x - x);
116 geometry.y = ocy + (ev.xmotion.y - y);
117 if(abs(geometry.x) < globalconf.screens[screen].snap + area.x && geometry.x > area.x)
118 geometry.x = area.x;
119 else if(abs((area.x + area.width) - (geometry.x + c->geometry.width + 2 * c->border))
120 < globalconf.screens[screen].snap)
121 geometry.x = area.x + area.width - c->geometry.width - 2 * c->border;
122 if(abs(geometry.y) < globalconf.screens[screen].snap + area.y && geometry.y > area.y)
123 geometry.y = area.y;
124 else if(abs((area.y + area.height) - (geometry.y + c->geometry.height + 2 * c->border))
125 < globalconf.screens[screen].snap)
126 geometry.y = area.y + area.height - c->geometry.height - 2 * c->border;
127 geometry.width = c->geometry.width;
128 geometry.height = c->geometry.height;
129 client_resize(c, geometry, False);
130 while(XCheckMaskEvent(globalconf.display, PointerMotionMask, &ev));
132 break;
137 /** Resize client with mouse
138 * \param screen Screen ID
139 * \param arg Unused
140 * \ingroup ui_callback
142 void
143 uicb_client_resizemouse(int screen, char *arg __attribute__ ((unused)))
145 int ocx = 0, ocy = 0, n;
146 XEvent ev;
147 Client *c = globalconf.focus->client;
148 Tag **curtags = get_current_tags(screen);
149 Layout *layout = curtags[0]->layout;
150 Area area = { 0, 0, 0, 0 }, geometry;
151 double mwfact;
153 /* only handle floating and tiled layouts */
154 if(c && !c->isfixed)
156 if(layout->arrange == layout_floating || c->isfloating)
158 ocx = c->geometry.x;
159 ocy = c->geometry.y;
160 c->ismax = False;
162 else if (layout->arrange == layout_tile || layout->arrange == layout_tileleft
163 || layout->arrange == layout_tilebottom || layout->arrange == layout_tiletop)
165 for(n = 0, c = globalconf.clients; c; c = c->next)
166 if(IS_TILED(c, screen))
167 n++;
169 if(n <= curtags[0]->nmaster) return;
171 for(c = globalconf.clients; c && !IS_TILED(c, screen); c = c->next);
172 if(!c) return;
174 area = get_screen_area(screen,
175 globalconf.screens[c->screen].statusbar,
176 &globalconf.screens[c->screen].padding);
179 else
180 return;
182 if(XGrabPointer(globalconf.display, RootWindow(globalconf.display,
183 get_phys_screen(c->screen)),
184 False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
185 RootWindow(globalconf.display,get_phys_screen(c->screen)),
186 globalconf.cursor[CurResize], CurrentTime) != GrabSuccess)
187 return;
189 if(curtags[0]->layout->arrange == layout_tileleft)
190 XWarpPointer(globalconf.display, None, c->win, 0, 0, 0, 0, 0, c->geometry.height + c->border - 1);
191 else if(curtags[0]->layout->arrange == layout_tilebottom)
192 XWarpPointer(globalconf.display, None, c->win, 0, 0, 0, 0, c->geometry.width + c->border - 1, c->geometry.height + c->border - 1);
193 else if(curtags[0]->layout->arrange == layout_tiletop)
194 XWarpPointer(globalconf.display, None, c->win, 0, 0, 0, 0, c->geometry.width + c->border - 1, 0);
195 else
196 XWarpPointer(globalconf.display, None, c->win, 0, 0, 0, 0, c->geometry.width + c->border - 1, c->geometry.height + c->border - 1);
198 for(;;)
200 XMaskEvent(globalconf.display, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
201 switch (ev.type)
203 case ButtonRelease:
204 XUngrabPointer(globalconf.display, CurrentTime);
205 return;
206 case ConfigureRequest:
207 handle_event_configurerequest(&ev);
208 break;
209 case Expose:
210 handle_event_expose(&ev);
211 break;
212 case MapRequest:
213 handle_event_maprequest(&ev);
214 break;
215 case MotionNotify:
216 if(layout->arrange == layout_floating || c->isfloating)
218 if((geometry.width = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
219 geometry.width = 1;
220 if((geometry.height = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
221 geometry.height = 1;
222 geometry.x = c->geometry.x;
223 geometry.y = c->geometry.y;
224 client_resize(c, geometry, True);
226 else if(layout->arrange == layout_tile || layout->arrange == layout_tileleft
227 || layout->arrange == layout_tiletop || layout->arrange == layout_tilebottom)
229 if(layout->arrange == layout_tile)
230 mwfact = (double) (ev.xmotion.x - area.x) / area.width;
231 else if(curtags[0]->layout->arrange == layout_tileleft)
232 mwfact = 1 - (double) (ev.xmotion.x - area.x) / area.width;
233 else if(curtags[0]->layout->arrange == layout_tilebottom)
234 mwfact = (double) (ev.xmotion.y - area.y) / area.height;
235 else
236 mwfact = 1 - (double) (ev.xmotion.y - area.y) / area.height;
237 if(mwfact < 0.1) mwfact = 0.1;
238 else if(mwfact > 0.9) mwfact = 0.9;
239 if(fabs(curtags[0]->mwfact - mwfact) >= 0.01)
241 curtags[0]->mwfact = mwfact;
242 globalconf.screens[screen].need_arrange = True;
243 layout_refresh();
244 while(XCheckMaskEvent(globalconf.display, PointerMotionMask, &ev));
248 break;
251 p_delete(&curtags);
254 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80