cosmetic
[awesome.git] / event.c
blobe9bbd7539f45e023865d5bb5b896b2cf8b4bc0a6
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 < get_screen_count(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].name, 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].name);
164 else
165 uicb_view(e->xany.display, &dc[screen], &awesomeconf[screen], awesomeconf[screen].tags[i].name);
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].name);
171 else
172 uicb_toggleview(e->xany.display, &dc[screen], &awesomeconf[screen], awesomeconf[screen].tags[i].name);
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(layout_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(layout_floating) && !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(layout_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(layout_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].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 < get_screen_count(e->xany.display); screen++)
330 if(awesomeconf[screen].statusbar.window == ev->window)
331 drawstatusbar(e->xany.display, &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 /* find the right screen for this event */
346 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
347 if(XQueryPointer(e->xany.display, RootWindow(e->xany.display, screen), &dummy, &dummy, &x, &y, &d, &d, &m))
349 /* if screen is 0, we are on first Zaphod screen or on the
350 * only screen in Xinerama, so we can ask for a better screen
351 * number with get_screen_bycoord: we'll get 0 in Zaphod mode
352 * so it's the same, or maybe the real Xinerama screen */
353 if(screen == 0)
354 screen = get_screen_bycoord(e->xany.display, x, y);
355 break;
358 for(i = 0; i < awesomeconf[screen].nkeys; i++)
359 if(keysym == awesomeconf[screen].keys[i].keysym
360 && CLEANMASK(awesomeconf[screen].keys[i].mod, screen) == CLEANMASK(ev->state, screen) && awesomeconf[screen].keys[i].func)
361 awesomeconf[screen].keys[i].func(e->xany.display, &dc[screen], &awesomeconf[screen], awesomeconf[screen].keys[i].arg);
364 void
365 handle_event_leavenotify(XEvent * e, awesome_config *awesomeconf)
367 XCrossingEvent *ev = &e->xcrossing;
368 int screen;
370 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
371 if((ev->window == RootWindow(e->xany.display, screen)) && !ev->same_screen)
372 focus(e->xany.display, &dc[screen], NULL, ev->same_screen, &awesomeconf[screen]);
375 void
376 handle_event_mappingnotify(XEvent * e, awesome_config *awesomeconf)
378 XMappingEvent *ev = &e->xmapping;
379 int screen;
381 XRefreshKeyboardMapping(ev);
382 if(ev->request == MappingKeyboard)
383 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
384 grabkeys(e->xany.display, screen, &awesomeconf[screen]);
387 void
388 handle_event_maprequest(XEvent * e, awesome_config *awesomeconf)
390 static XWindowAttributes wa;
391 XMapRequestEvent *ev = &e->xmaprequest;
392 int screen;
394 if(!XGetWindowAttributes(e->xany.display, ev->window, &wa))
395 return;
396 if(wa.override_redirect)
397 return;
398 if(!getclient(ev->window))
400 for(screen = 0; wa.screen != ScreenOfDisplay(e->xany.display, screen); screen++);
401 manage(e->xany.display, &dc[screen], ev->window, &wa, &awesomeconf[screen]);
405 void
406 handle_event_propertynotify(XEvent * e, awesome_config *awesomeconf)
408 Client *c;
409 Window trans;
410 XPropertyEvent *ev = &e->xproperty;
412 if(ev->state == PropertyDelete)
413 return; /* ignore */
414 if((c = getclient(ev->window)))
416 switch (ev->atom)
418 case XA_WM_TRANSIENT_FOR:
419 XGetTransientForHint(e->xany.display, c->win, &trans);
420 if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
421 arrange(e->xany.display, &dc[c->screen], &awesomeconf[c->screen]);
422 break;
423 case XA_WM_NORMAL_HINTS:
424 updatesizehints(c);
425 break;
427 if(ev->atom == XA_WM_NAME || ev->atom == XInternAtom(c->display, "_NET_WM_NAME", False))
429 updatetitle(c);
430 if(c == sel)
431 drawstatusbar(e->xany.display, &dc[c->screen], &awesomeconf[c->screen]);
436 void
437 handle_event_unmapnotify(XEvent * e, awesome_config *awesomeconf)
439 Client *c;
440 XUnmapEvent *ev = &e->xunmap;
442 if((c = getclient(ev->window))
443 && ev->event == RootWindow(e->xany.display, c->screen) && (ev->send_event || !c->unmapped))
444 unmanage(c, &dc[c->screen], WithdrawnState, &awesomeconf[c->screen]);
447 void
448 handle_event_shape(XEvent * e,
449 awesome_config *awesomeconf __attribute__ ((unused)))
451 XShapeEvent *ev = (XShapeEvent *) e;
452 Client *c = getclient(ev->window);
454 if(c)
455 set_shape(c);
458 void
459 handle_event_randr_screen_change_notify(XEvent *e,
460 awesome_config *awesomeconf __attribute__ ((unused)))
462 XRRUpdateConfiguration(e);
465 void
466 grabkeys(Display *disp, int screen, awesome_config *awesomeconf)
468 int i;
469 KeyCode code;
471 XUngrabKey(disp, AnyKey, AnyModifier, RootWindow(disp, screen));
472 for(i = 0; i < awesomeconf->nkeys; i++)
474 if((code = XKeysymToKeycode(disp, awesomeconf->keys[i].keysym)) == NoSymbol)
475 continue;
476 XGrabKey(disp, code, awesomeconf->keys[i].mod, RootWindow(disp, screen), True, GrabModeAsync, GrabModeAsync);
477 XGrabKey(disp, code, awesomeconf->keys[i].mod | LockMask, RootWindow(disp, screen), True, GrabModeAsync, GrabModeAsync);
478 XGrabKey(disp, code, awesomeconf->keys[i].mod | awesomeconf->numlockmask, RootWindow(disp, screen), True, GrabModeAsync, GrabModeAsync);
479 XGrabKey(disp, code, awesomeconf->keys[i].mod | awesomeconf->numlockmask | LockMask, RootWindow(disp, screen), True,
480 GrabModeAsync, GrabModeAsync);