don't stick textbox size to the first textsize sent via widget_tell
[awesome.git] / mouse.c
blob0089c2daa58713588de246ba7585cc2f8cc64675
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 "util.h"
29 #include "event.h"
30 #include "window.h"
31 #include "statusbar.h"
32 #include "layouts/floating.h"
33 #include "layouts/tile.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 x1, y, ocx, ocy, di, nx, ny;
46 unsigned int dui;
47 Window dummy;
48 XEvent ev;
49 Area area;
50 Client *c = globalconf.focus->client;
51 Tag **curtags = get_current_tags(screen);
53 if(!c || (c && c->isfixed))
54 return;
56 if((curtags[0]->layout->arrange != layout_floating)
57 && !c->isfloating)
58 uicb_client_togglefloating(screen, NULL);
59 else
60 restack(screen);
62 p_delete(&curtags);
64 area = get_screen_area(c->screen,
65 globalconf.screens[screen].statusbar,
66 &globalconf.screens[screen].padding);
68 ocx = nx = c->x;
69 ocy = ny = c->y;
70 if(XGrabPointer(globalconf.display,
71 RootWindow(globalconf.display, c->phys_screen),
72 False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
73 None, globalconf.cursor[CurMove], CurrentTime) != GrabSuccess)
74 return;
75 XQueryPointer(globalconf.display,
76 RootWindow(globalconf.display, c->phys_screen),
77 &dummy, &dummy, &x1, &y, &di, &di, &dui);
78 c->ismax = False;
79 statusbar_draw_all(c->screen);
80 for(;;)
82 XMaskEvent(globalconf.display, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
83 switch (ev.type)
85 case ButtonRelease:
86 XUngrabPointer(globalconf.display, CurrentTime);
87 return;
88 case ConfigureRequest:
89 handle_event_configurerequest(&ev);
90 break;
91 case Expose:
92 handle_event_expose(&ev);
93 break;
94 case MapRequest:
95 handle_event_maprequest(&ev);
96 break;
97 case MotionNotify:
98 nx = ocx + (ev.xmotion.x - x1);
99 ny = ocy + (ev.xmotion.y - y);
100 if(abs(nx) < globalconf.screens[screen].snap + area.x && nx > area.x)
101 nx = area.x;
102 else if(abs((area.x + area.width) - (nx + c->w + 2 * c->border)) < globalconf.screens[screen].snap)
103 nx = area.x + area.width - c->w - 2 * c->border;
104 if(abs(ny) < globalconf.screens[screen].snap + area.y && ny > area.y)
105 ny = area.y;
106 else if(abs((area.y + area.height) - (ny + c->h + 2 * c->border)) < globalconf.screens[screen].snap)
107 ny = area.y + area.height - c->h - 2 * c->border;
108 client_resize(c, nx, ny, c->w, c->h, False, False);
109 while(XCheckMaskEvent(globalconf.display, PointerMotionMask, &ev));
110 break;
115 /** Resize client with mouse
116 * \param screen Screen ID
117 * \param arg Unused
118 * \ingroup ui_callback
120 void
121 uicb_client_resizemouse(int screen, char *arg __attribute__ ((unused)))
123 int ocx, ocy, nw, nh, n;
124 XEvent ev;
125 Client *c = globalconf.focus->client;
126 Tag **curtags = get_current_tags(screen);
127 Area area;
128 double mwfact;
130 /* only handle floating and tiled layouts */
131 if(c && !c->isfixed)
133 if((curtags[0]->layout->arrange == layout_floating) || c->isfloating)
135 restack(screen);
136 ocx = c->x;
137 ocy = c->y;
138 c->ismax = False;
140 else if (curtags[0]->layout->arrange == layout_tile
141 || curtags[0]->layout->arrange == layout_tileleft)
143 for(n = 0, c = globalconf.clients; c; c = c->next)
144 if(IS_TILED(c, screen))
145 n++;
147 if(n <= curtags[0]->nmaster) return;
149 for(c = globalconf.clients; c && !IS_TILED(c, screen); c = c->next);
150 if(!c) return;
152 area = get_screen_area(screen,
153 globalconf.screens[c->screen].statusbar,
154 &globalconf.screens[c->screen].padding);
157 else
158 return;
160 if(XGrabPointer(globalconf.display, RootWindow(globalconf.display, c->phys_screen),
161 False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
162 None, globalconf.cursor[CurResize], CurrentTime) != GrabSuccess)
163 return;
165 if(curtags[0]->layout->arrange == layout_tileleft)
166 XWarpPointer(globalconf.display, None, c->win, 0, 0, 0, 0, 0, c->h + c->border - 1);
167 else
168 XWarpPointer(globalconf.display, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
170 for(;;)
172 XMaskEvent(globalconf.display, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
173 switch (ev.type)
175 case ButtonRelease:
176 XUngrabPointer(globalconf.display, CurrentTime);
177 while(XCheckMaskEvent(globalconf.display, EnterWindowMask, &ev));
178 return;
179 case ConfigureRequest:
180 handle_event_configurerequest(&ev);
181 break;
182 case Expose:
183 handle_event_expose(&ev);
184 break;
185 case MapRequest:
186 handle_event_maprequest(&ev);
187 break;
188 case MotionNotify:
189 if(curtags[0]->layout->arrange == layout_floating || c->isfloating)
191 if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
192 nw = 1;
193 if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
194 nh = 1;
195 client_resize(c, c->x, c->y, nw, nh, True, False);
197 else if(curtags[0]->layout->arrange == layout_tile
198 || curtags[0]->layout->arrange == layout_tileleft)
200 if(curtags[0]->layout->arrange == layout_tile)
201 mwfact = (double) (ev.xmotion.x - area.x) / area.width;
202 else
203 mwfact = 1 - (double) (ev.xmotion.x - area.x) / area.width;
204 if(mwfact < 0.1) mwfact = 0.1;
205 else if(mwfact > 0.9) mwfact = 0.9;
206 if(fabs(curtags[0]->mwfact - mwfact) >= 0.05)
208 curtags[0]->mwfact = mwfact;
209 arrange(screen);
210 /* drop possibly arrived events while we where arrange()ing */
211 while(XCheckMaskEvent(globalconf.display, PointerMotionMask, &ev));
215 break;
218 p_delete(&curtags);
222 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80