bug fix: fix bug when resizing with mouse
[awesome.git] / event.c
blob246b4fd83df81523679464f24ca5fb5b0e8a61fc
1 /*
2 * event.c - event handlers
3 *
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 *
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 <X11/keysym.h>
23 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
25 #include <X11/extensions/shape.h>
26 #include <X11/extensions/Xrandr.h>
28 #include "screen.h"
29 #include "event.h"
30 #include "layout.h"
31 #include "tag.h"
32 #include "draw.h"
33 #include "statusbar.h"
34 #include "util.h"
35 #include "layouts/tile.h"
36 #include "layouts/floating.h"
38 /* extern */
39 extern DC *dc; /* global draw context */
40 extern Client *clients, *sel; /* global client list */
42 #define CLEANMASK(mask, screen) (mask & ~(awesomeconf[screen].numlockmask | LockMask))
43 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
45 static Client *
46 getclient(Window w)
48 Client *c;
50 for(c = clients; c && c->win != w; c = c->next);
51 return c;
54 static void
55 movemouse(Client * c, awesome_config *awesomeconf)
57 int x1, y1, ocx, ocy, di, nx, ny;
58 unsigned int dui;
59 Window dummy;
60 XEvent ev;
61 ScreenInfo *si;
63 si = get_screen_info(c->display, c->screen, awesomeconf->statusbar, &x1);
65 ocx = nx = c->x;
66 ocy = ny = c->y;
67 if(XGrabPointer(c->display, RootWindow(c->display, c->screen), False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
68 None, dc[c->screen].cursor[CurMove], CurrentTime) != GrabSuccess)
69 return;
70 XQueryPointer(c->display, RootWindow(c->display, c->screen), &dummy, &dummy, &x1, &y1, &di, &di, &dui);
71 for(;;)
73 XMaskEvent(c->display, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
74 switch (ev.type)
76 case ButtonRelease:
77 XUngrabPointer(c->display, CurrentTime);
78 return;
79 case ConfigureRequest:
80 case Expose:
81 case MapRequest:
82 handle_event_maprequest(&ev, awesomeconf);
83 break;
84 case MotionNotify:
85 XSync(c->display, False);
86 nx = ocx + (ev.xmotion.x - x1);
87 ny = ocy + (ev.xmotion.y - y1);
88 if(abs(si[c->screen].x_org + nx) < awesomeconf->snap)
89 nx = si[c->screen].x_org;
90 else if(abs((si[c->screen].x_org + si[c->screen].width) - (nx + c->w + 2 * c->border)) < awesomeconf->snap)
91 nx = si[c->screen].x_org + si[c->screen].width - c->w - 2 * c->border;
92 if(abs(si[c->screen].y_org - ny) < awesomeconf->snap)
93 ny = si[c->screen].y_org;
94 else if(abs((si[c->screen].y_org + si[c->screen].height) - (ny + c->h + 2 * c->border)) < awesomeconf->snap)
95 ny = si[c->screen].y_org + si[c->screen].height - c->h - 2 * c->border;
96 resize(c, nx, ny, c->w, c->h, False);
97 break;
100 XFree(si);
103 static void
104 resizemouse(Client * c, awesome_config *awesomeconf)
106 int ocx, ocy;
107 int nw, nh;
108 XEvent ev;
110 ocx = c->x;
111 ocy = c->y;
112 if(XGrabPointer(c->display, RootWindow(c->display, c->screen), False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
113 None, dc[c->screen].cursor[CurResize], CurrentTime) != GrabSuccess)
114 return;
115 c->ismax = False;
116 XWarpPointer(c->display, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
117 for(;;)
119 XMaskEvent(c->display, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
120 switch (ev.type)
122 case ButtonRelease:
123 XWarpPointer(c->display, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
124 XUngrabPointer(c->display, CurrentTime);
125 while(XCheckMaskEvent(c->display, EnterWindowMask, &ev));
126 return;
127 case ConfigureRequest:
128 case Expose:
129 case MapRequest:
130 handle_event_maprequest(&ev, awesomeconf);
131 break;
132 case MotionNotify:
133 XSync(c->display, False);
134 if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
135 nw = 1;
136 if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
137 nh = 1;
138 resize(c, c->x, c->y, nw, nh, True);
139 break;
144 void
145 handle_event_buttonpress(XEvent * e, awesome_config *awesomeconf)
147 int i, screen;
148 Client *c;
149 XButtonPressedEvent *ev = &e->xbutton;
151 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
152 if(awesomeconf[screen].statusbar.window == ev->window)
154 int x = 0;
155 for(i = 0; i < awesomeconf[screen].ntags; i++)
157 x += textw(dc[screen].font.set, dc[screen].font.xfont, awesomeconf[screen].tags[i], dc[screen].font.height);
158 if(ev->x < x)
160 if(ev->button == Button1)
162 if(ev->state & awesomeconf[screen].modkey)
163 uicb_tag(e->xany.display, &dc[screen], &awesomeconf[screen], awesomeconf[screen].tags[i]);
164 else
165 uicb_view(e->xany.display, &dc[screen], &awesomeconf[screen], awesomeconf[screen].tags[i]);
167 else if(ev->button == Button3)
169 if(ev->state & awesomeconf[screen].modkey)
170 uicb_toggletag(e->xany.display, &dc[screen], &awesomeconf[screen], awesomeconf[screen].tags[i]);
171 else
172 uicb_toggleview(e->xany.display, &dc[screen], &awesomeconf[screen], awesomeconf[screen].tags[i]);
174 return;
177 if((ev->x < x + awesomeconf[screen].statusbar.width) && ev->button == Button1)
178 uicb_setlayout(e->xany.display, &dc[screen], &awesomeconf[screen], NULL);
179 return;
182 if((c = getclient(ev->window)))
184 focus(c->display, &dc[c->screen], c, ev->same_screen, &awesomeconf[c->screen]);
185 if(CLEANMASK(ev->state, c->screen) != awesomeconf[c->screen].modkey)
186 return;
187 if(ev->button == Button1)
189 if(!IS_ARRANGE(floating) && !c->isfloating)
190 uicb_togglefloating(e->xany.display, &dc[c->screen], &awesomeconf[c->screen], NULL);
191 else
192 restack(e->xany.display, &dc[c->screen], &awesomeconf[c->screen]);
193 movemouse(c, &awesomeconf[c->screen]);
195 else if(ev->button == Button2)
197 if(IS_ARRANGE(tile) && !c->isfixed && c->isfloating)
198 uicb_togglefloating(e->xany.display, &dc[c->screen], &awesomeconf[c->screen], NULL);
199 else
200 uicb_zoom(e->xany.display, &dc[c->screen], &awesomeconf[c->screen], NULL);
202 else if(ev->button == Button3)
204 if(!IS_ARRANGE(floating) && !c->isfloating)
205 uicb_togglefloating(e->xany.display, &dc[c->screen], &awesomeconf[c->screen], NULL);
206 else
207 restack(e->xany.display, &dc[c->screen], &awesomeconf[c->screen]);
208 resizemouse(c, &awesomeconf[c->screen]);
211 else if(!sel)
212 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
213 if(RootWindow(e->xany.display, screen) == ev->window)
215 if(ev->button == Button4)
216 uicb_tag_viewnext(e->xany.display, &dc[screen], &awesomeconf[screen], NULL);
217 else if(ev->button == Button5)
218 uicb_tag_viewprev(e->xany.display, &dc[screen], &awesomeconf[screen], NULL);
222 void
223 handle_event_configurerequest(XEvent * e, awesome_config *awesomeconf)
225 Client *c;
226 XConfigureRequestEvent *ev = &e->xconfigurerequest;
227 XWindowChanges wc;
229 if((c = getclient(ev->window)))
231 c->ismax = False;
232 if(ev->value_mask & CWBorderWidth)
233 c->border = ev->border_width;
234 if(c->isfixed || c->isfloating || IS_ARRANGE(floating))
236 if(ev->value_mask & CWX)
237 c->x = ev->x;
238 if(ev->value_mask & CWY)
239 c->y = ev->y;
240 if(ev->value_mask & CWWidth)
241 c->w = ev->width;
242 if(ev->value_mask & CWHeight)
243 c->h = ev->height;
244 if((c->x + c->w) > DisplayWidth(c->display, c->screen) && c->isfloating)
245 c->x = DisplayWidth(c->display, c->screen) / 2 - c->w / 2; /* center in x direction */
246 if((c->y + c->h) > DisplayHeight(c->display, c->screen) && c->isfloating)
247 c->y = DisplayHeight(c->display, c->screen) / 2 - c->h / 2; /* center in y direction */
248 if((ev->value_mask & (CWX | CWY)) && !(ev->value_mask & (CWWidth | CWHeight)))
249 configure(c);
250 if(isvisible(c, c->screen, awesomeconf[c->screen].selected_tags, awesomeconf[c->screen].ntags))
251 XMoveResizeWindow(e->xany.display, c->win, c->x, c->y, c->w, c->h);
253 else
254 configure(c);
256 else
258 wc.x = ev->x;
259 wc.y = ev->y;
260 wc.width = ev->width;
261 wc.height = ev->height;
262 wc.border_width = ev->border_width;
263 wc.sibling = ev->above;
264 wc.stack_mode = ev->detail;
265 XConfigureWindow(e->xany.display, ev->window, ev->value_mask, &wc);
267 XSync(e->xany.display, False);
270 void
271 handle_event_configurenotify(XEvent * e, awesome_config *awesomeconf)
273 XConfigureEvent *ev = &e->xconfigure;
274 int screen;
276 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
277 if(ev->window == RootWindow(e->xany.display, screen)
278 && (ev->width != DisplayWidth(e->xany.display, screen)
279 || ev->height != DisplayHeight(e->xany.display, screen)))
281 DisplayWidth(e->xany.display, screen) = ev->width;
282 DisplayHeight(e->xany.display, screen) = ev->height;
283 XFreePixmap(e->xany.display, awesomeconf[screen].statusbar.drawable);
284 awesomeconf[screen].statusbar.drawable = XCreatePixmap(e->xany.display, RootWindow(e->xany.display, screen),
285 DisplayWidth(e->xany.display, screen),
286 awesomeconf[screen].statusbar.height,
287 DefaultDepth(e->xany.display, screen));
288 XResizeWindow(e->xany.display, awesomeconf[screen].statusbar.window,
289 DisplayWidth(e->xany.display, screen), awesomeconf[screen].statusbar.height);
290 updatebarpos(e->xany.display, awesomeconf[screen].statusbar);
291 arrange(e->xany.display, &dc[screen], &awesomeconf[screen]);
295 void
296 handle_event_destroynotify(XEvent * e, awesome_config *awesomeconf)
298 Client *c;
299 XDestroyWindowEvent *ev = &e->xdestroywindow;
301 if((c = getclient(ev->window)))
302 unmanage(c, &dc[c->screen], WithdrawnState, &awesomeconf[c->screen]);
305 void
306 handle_event_enternotify(XEvent * e, awesome_config *awesomeconf)
308 Client *c;
309 XCrossingEvent *ev = &e->xcrossing;
310 int screen;
312 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
313 return;
314 if((c = getclient(ev->window)))
315 focus(c->display, &dc[c->screen], c, ev->same_screen, &awesomeconf[c->screen]);
316 else
317 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
318 if(ev->window == RootWindow(e->xany.display, screen))
319 focus(e->xany.display, &dc[screen], NULL, True, &awesomeconf[screen]);
322 void
323 handle_event_expose(XEvent * e, awesome_config *awesomeconf)
325 XExposeEvent *ev = &e->xexpose;
326 int screen;
328 if(!ev->count)
329 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
330 if(awesomeconf[screen].statusbar.window == ev->window)
331 drawstatusbar(e->xany.display, screen, &dc[screen], &awesomeconf[screen]);
334 void
335 handle_event_keypress(XEvent * e, awesome_config *awesomeconf)
337 int i, screen, x, y, d;
338 unsigned int m;
339 KeySym keysym;
340 XKeyEvent *ev = &e->xkey;
341 Window dummy;
343 keysym = XKeycodeToKeysym(e->xany.display, (KeyCode) ev->keycode, 0);
345 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
346 if(XQueryPointer(e->xany.display, RootWindow(e->xany.display, screen), &dummy, &dummy, &y, &x, &d, &d, &m))
347 for(i = 0; i < awesomeconf[screen].nkeys; i++)
348 if(keysym == awesomeconf[screen].keys[i].keysym
349 && CLEANMASK(awesomeconf[screen].keys[i].mod, screen) == CLEANMASK(ev->state, screen) && awesomeconf[screen].keys[i].func)
350 awesomeconf[screen].keys[i].func(e->xany.display, &dc[screen], &awesomeconf[screen], awesomeconf[screen].keys[i].arg);
353 void
354 handle_event_leavenotify(XEvent * e, awesome_config *awesomeconf)
356 XCrossingEvent *ev = &e->xcrossing;
357 int screen;
359 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
360 if((ev->window == RootWindow(e->xany.display, screen)) && !ev->same_screen)
361 focus(e->xany.display, &dc[screen], NULL, ev->same_screen, &awesomeconf[screen]);
364 void
365 handle_event_mappingnotify(XEvent * e, awesome_config *awesomeconf)
367 XMappingEvent *ev = &e->xmapping;
368 int screen;
370 XRefreshKeyboardMapping(ev);
371 if(ev->request == MappingKeyboard)
372 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
373 grabkeys(e->xany.display, screen, &awesomeconf[screen]);
376 void
377 handle_event_maprequest(XEvent * e, awesome_config *awesomeconf)
379 static XWindowAttributes wa;
380 XMapRequestEvent *ev = &e->xmaprequest;
381 int screen;
383 if(!XGetWindowAttributes(e->xany.display, ev->window, &wa))
384 return;
385 if(wa.override_redirect)
386 return;
387 if(!getclient(ev->window))
389 for(screen = 0; wa.screen != ScreenOfDisplay(e->xany.display, screen); screen++);
390 manage(e->xany.display, &dc[screen], ev->window, &wa, &awesomeconf[screen]);
394 void
395 handle_event_propertynotify(XEvent * e, awesome_config *awesomeconf)
397 Client *c;
398 Window trans;
399 XPropertyEvent *ev = &e->xproperty;
401 if(ev->state == PropertyDelete)
402 return; /* ignore */
403 if((c = getclient(ev->window)))
405 switch (ev->atom)
407 case XA_WM_TRANSIENT_FOR:
408 XGetTransientForHint(e->xany.display, c->win, &trans);
409 if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
410 arrange(e->xany.display, &dc[c->screen], &awesomeconf[c->screen]);
411 break;
412 case XA_WM_NORMAL_HINTS:
413 updatesizehints(c);
414 break;
416 if(ev->atom == XA_WM_NAME || ev->atom == XInternAtom(c->display, "_NET_WM_NAME", False))
418 updatetitle(c);
419 if(c == sel)
420 drawstatusbar(e->xany.display, c->screen, &dc[c->screen], &awesomeconf[c->screen]);
425 void
426 handle_event_unmapnotify(XEvent * e, awesome_config *awesomeconf)
428 Client *c;
429 XUnmapEvent *ev = &e->xunmap;
431 if((c = getclient(ev->window))
432 && ev->event == RootWindow(e->xany.display, c->screen) && (ev->send_event || !c->unmapped))
433 unmanage(c, &dc[c->screen], WithdrawnState, &awesomeconf[c->screen]);
436 void
437 handle_event_shape(XEvent * e,
438 awesome_config *awesomeconf __attribute__ ((unused)))
440 XShapeEvent *ev = (XShapeEvent *) e;
441 Client *c = getclient(ev->window);
443 if(c)
444 set_shape(c);
447 void
448 handle_event_randr_screen_change_notify(XEvent *e,
449 awesome_config *awesomeconf __attribute__ ((unused)))
451 XRRUpdateConfiguration(e);
454 void
455 grabkeys(Display *disp, int screen, awesome_config *awesomeconf)
457 int i;
458 KeyCode code;
460 XUngrabKey(disp, AnyKey, AnyModifier, RootWindow(disp, screen));
461 for(i = 0; i < awesomeconf->nkeys; i++)
463 if((code = XKeysymToKeycode(disp, awesomeconf->keys[i].keysym)) == NoSymbol)
464 continue;
465 XGrabKey(disp, code, awesomeconf->keys[i].mod, RootWindow(disp, screen), True, GrabModeAsync, GrabModeAsync);
466 XGrabKey(disp, code, awesomeconf->keys[i].mod | LockMask, RootWindow(disp, screen), True, GrabModeAsync, GrabModeAsync);
467 XGrabKey(disp, code, awesomeconf->keys[i].mod | awesomeconf->numlockmask, RootWindow(disp, screen), True, GrabModeAsync, GrabModeAsync);
468 XGrabKey(disp, code, awesomeconf->keys[i].mod | awesomeconf->numlockmask | LockMask, RootWindow(disp, screen), True,
469 GrabModeAsync, GrabModeAsync);