rewrite client_manage()
[awesome.git] / event.c
blob20b60e6e2ca8ce4a358471ef7284a3b33a7256e6
1 /*
2 * event.c - event handlers
4 * Copyright © 2007-2008 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 <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 "tag.h"
31 #include "statusbar.h"
32 #include "util.h"
33 #include "window.h"
34 #include "mouse.h"
35 #include "ewmh.h"
36 #include "client.h"
37 #include "widget.h"
38 #include "xutil.h"
39 #include "rules.h"
40 #include "layouts/tile.h"
41 #include "layouts/floating.h"
43 extern AwesomeConf globalconf;
45 static void
46 handle_mouse_button_press(int screen, unsigned int button, unsigned int state,
47 Button *buttons, char *arg)
49 Button *b;
51 for(b = buttons; b; b = b->next)
52 if(button == b->button && CLEANMASK(state) == b->mod && b->func)
54 if(arg)
55 b->func(screen, arg);
56 else
57 b->func(screen, b->arg);
58 return;
62 void
63 handle_event_buttonpress(XEvent *e)
65 int i, screen, x = 0, y = 0;
66 unsigned int udummy;
67 Client *c;
68 Window wdummy;
69 Widget *widget;
70 Statusbar *statusbar;
71 XButtonPressedEvent *ev = &e->xbutton;
73 for(screen = 0; screen < globalconf.nscreens; screen++)
74 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
75 if(statusbar->window == ev->window || statusbar->window == ev->subwindow)
76 switch(statusbar->position)
78 case Top:
79 case Bottom:
80 for(widget = statusbar->widgets; widget; widget = widget->next)
81 if(ev->x >= widget->area.x && ev->x < widget->area.x + widget->area.width
82 && ev->y >= widget->area.y && ev->y < widget->area.y + widget->area.height)
84 widget->button_press(widget, ev);
85 return;
87 break;
88 case Right:
89 for(widget = statusbar->widgets; widget; widget = widget->next)
90 if(ev->y >= widget->area.x && ev->y < widget->area.x + widget->area.width
91 && statusbar->height - ev->x >= widget->area.y
92 && statusbar->height - ev->x < widget->area.y + widget->area.height)
94 widget->button_press(widget, ev);
95 return;
97 break;
98 default:
99 for(widget = statusbar->widgets; widget; widget = widget->next)
100 if(statusbar->width - ev->y >= widget->area.x
101 && statusbar->width - ev->y < widget->area.x + widget->area.width
102 && ev->x >= widget->area.y && ev->x < widget->area.y + widget->area.height)
104 widget->button_press(widget, ev);
105 return;
107 break;
110 if((c = get_client_bywin(globalconf.clients, ev->window)))
112 focus(c, ev->same_screen, c->screen);
113 if(CLEANMASK(ev->state) == NoSymbol
114 && ev->button == Button1)
116 restack(c->screen);
117 XAllowEvents(globalconf.display, ReplayPointer, CurrentTime);
118 window_grabbuttons(get_phys_screen(c->screen), c->win, True, True);
120 else
121 handle_mouse_button_press(c->screen, ev->button, ev->state, globalconf.buttons.client, NULL);
123 else
124 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
125 if(RootWindow(e->xany.display, screen) == ev->window
126 && XQueryPointer(e->xany.display,
127 ev->window, &wdummy,
128 &wdummy, &x, &y, &i,
129 &i, &udummy))
131 screen = get_screen_bycoord(x, y);
132 handle_mouse_button_press(screen, ev->button, ev->state,
133 globalconf.buttons.root, NULL);
134 return;
138 void
139 handle_event_configurerequest(XEvent * e)
141 Client *c;
142 XConfigureRequestEvent *ev = &e->xconfigurerequest;
143 XWindowChanges wc;
144 int old_screen;
145 Area geometry;
147 if((c = get_client_bywin(globalconf.clients, ev->window)))
149 geometry = c->geometry;
151 if(ev->value_mask & CWX)
152 geometry.x = ev->x;
153 if(ev->value_mask & CWY)
154 geometry.y = ev->y;
155 if(ev->value_mask & CWWidth)
156 geometry.width = ev->width;
157 if(ev->value_mask & CWHeight)
158 geometry.height = ev->height;
160 if(geometry.x != c->geometry.x || geometry.y != c->geometry.y
161 || geometry.width != c->geometry.width || geometry.height != c->geometry.height)
163 old_screen = c->screen;
165 client_resize(c, geometry, False);
167 tag_client_with_rule(c, rule_matching_client(c));
169 if(old_screen != c->screen)
171 widget_invalidate_cache(old_screen, WIDGET_CACHE_CLIENTS);
172 arrange(old_screen);
174 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
175 arrange(c->screen);
177 else
178 window_configure(c->win, geometry, c->border);
180 else
182 wc.x = ev->x;
183 wc.y = ev->y;
184 wc.width = ev->width;
185 wc.height = ev->height;
186 wc.border_width = ev->border_width;
187 wc.sibling = ev->above;
188 wc.stack_mode = ev->detail;
189 XConfigureWindow(e->xany.display, ev->window, ev->value_mask, &wc);
193 void
194 handle_event_configurenotify(XEvent * e)
196 XConfigureEvent *ev = &e->xconfigure;
197 int screen;
198 Area area;
200 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
201 if(ev->window == RootWindow(e->xany.display, screen)
202 && (ev->width != DisplayWidth(e->xany.display, screen)
203 || ev->height != DisplayHeight(e->xany.display, screen)))
205 DisplayWidth(e->xany.display, screen) = ev->width;
206 DisplayHeight(e->xany.display, screen) = ev->height;
208 /* update statusbar */
209 area = get_screen_area(screen, NULL, &globalconf.screens[screen].padding);
210 globalconf.screens[screen].statusbar->width = area.width;
212 XResizeWindow(e->xany.display,
213 globalconf.screens[screen].statusbar->window,
214 globalconf.screens[screen].statusbar->width,
215 globalconf.screens[screen].statusbar->height);
217 widget_invalidate_cache(screen, WIDGET_CACHE_ALL);
218 arrange(screen);
222 void
223 handle_event_destroynotify(XEvent * e)
225 Client *c;
226 XDestroyWindowEvent *ev = &e->xdestroywindow;
228 if((c = get_client_bywin(globalconf.clients, ev->window)))
229 client_unmanage(c);
232 void
233 handle_event_enternotify(XEvent * e)
235 Client *c;
236 XCrossingEvent *ev = &e->xcrossing;
237 int screen;
238 Tag **curtags;
240 if(ev->mode != NotifyNormal)
241 return;
242 if((c = get_client_bywin(globalconf.clients, ev->window)) && globalconf.screens[c->screen].sloppy_focus)
244 focus(c, ev->same_screen, c->screen);
245 curtags = get_current_tags(c->screen);
246 if (c->isfloating || curtags[0]->layout->arrange == layout_floating)
247 window_grabbuttons(get_phys_screen(c->screen), c->win, True, False);
248 p_delete(&curtags);
250 else
252 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
253 if(ev->window == RootWindow(e->xany.display, screen))
254 focus(NULL, ev->same_screen, screen);
255 if((c = globalconf.focus->client))
256 window_grabbuttons(c->screen, c->win, False, False);
260 void
261 handle_event_expose(XEvent *e)
263 XExposeEvent *ev = &e->xexpose;
264 int screen;
265 Statusbar *statusbar;
267 if(!ev->count)
268 for(screen = 0; screen < globalconf.nscreens; screen++)
269 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
270 if(statusbar->window == ev->window)
272 statusbar_display(statusbar);
273 return;
277 void
278 handle_event_keypress(XEvent * e)
280 int screen, x, y, d;
281 unsigned int m;
282 KeySym keysym;
283 XKeyEvent *ev = &e->xkey;
284 Window dummy;
285 Key *k;
287 keysym = XKeycodeToKeysym(e->xany.display, (KeyCode) ev->keycode, 0);
289 /* find the right screen for this event */
290 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
291 if(XQueryPointer(e->xany.display, RootWindow(e->xany.display, screen), &dummy, &dummy, &x, &y, &d, &d, &m))
293 /* if screen is 0, we are on first Zaphod screen or on the
294 * only screen in Xinerama, so we can ask for a better screen
295 * number with get_screen_bycoord: we'll get 0 in Zaphod mode
296 * so it's the same, or maybe the real Xinerama screen */
297 if(screen == 0)
298 screen = get_screen_bycoord(x, y);
299 break;
302 for(k = globalconf.keys; k; k = k->next)
303 if(keysym == k->keysym && k->func
304 && CLEANMASK(k->mod) == CLEANMASK(ev->state))
306 k->func(screen, k->arg);
307 break;
311 void
312 handle_event_leavenotify(XEvent * e)
314 XCrossingEvent *ev = &e->xcrossing;
315 int screen;
317 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
318 if((ev->window == RootWindow(e->xany.display, screen)) && !ev->same_screen)
319 focus(NULL, ev->same_screen, screen);
322 void
323 handle_event_mappingnotify(XEvent *e)
325 XMappingEvent *ev = &e->xmapping;
326 int screen;
328 XRefreshKeyboardMapping(ev);
329 if(ev->request == MappingKeyboard)
330 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
331 grabkeys(get_phys_screen(screen));
334 void
335 handle_event_maprequest(XEvent *e)
337 static XWindowAttributes wa;
338 XMapRequestEvent *ev = &e->xmaprequest;
339 int screen, x, y, d;
340 unsigned int m;
341 Window dummy;
342 XEvent event;
344 if(!XGetWindowAttributes(e->xany.display, ev->window, &wa))
345 return;
346 if(wa.override_redirect)
347 return;
348 if(!get_client_bywin(globalconf.clients, ev->window))
350 for(screen = 0; wa.screen != ScreenOfDisplay(e->xany.display, screen); screen++);
351 if(screen == 0 && XQueryPointer(e->xany.display, RootWindow(e->xany.display, screen),
352 &dummy, &dummy, &x, &y, &d, &d, &m))
353 screen = get_screen_bycoord(x, y);
354 client_manage(ev->window, &wa, screen);
355 /* do this to keep focused client */
356 while(XCheckMaskEvent(globalconf.display, EnterWindowMask, &event));
360 void
361 handle_event_propertynotify(XEvent * e)
363 Client *c;
364 Window trans;
365 XPropertyEvent *ev = &e->xproperty;
367 if(ev->state == PropertyDelete)
368 return; /* ignore */
369 if((c = get_client_bywin(globalconf.clients, ev->window)))
371 switch (ev->atom)
373 case XA_WM_TRANSIENT_FOR:
374 XGetTransientForHint(e->xany.display, c->win, &trans);
375 if(!c->isfloating && (c->isfloating = (get_client_bywin(globalconf.clients, trans) != NULL)))
376 arrange(c->screen);
377 break;
378 case XA_WM_NORMAL_HINTS:
379 client_updatesizehints(c);
380 break;
381 case XA_WM_HINTS:
382 client_updatewmhints(c);
383 break;
385 if(ev->atom == XA_WM_NAME || ev->atom == XInternAtom(globalconf.display, "_NET_WM_NAME", False))
386 client_updatetitle(c);
390 void
391 handle_event_unmapnotify(XEvent * e)
393 Client *c;
394 XUnmapEvent *ev = &e->xunmap;
395 XEvent event;
397 if((c = get_client_bywin(globalconf.clients, ev->window))
398 && ev->event == RootWindow(e->xany.display, get_phys_screen(c->screen))
399 && ev->send_event && window_getstate(c->win) == NormalState)
400 client_unmanage(c);
401 /* do this to keep focused client */
402 while(XCheckMaskEvent(globalconf.display, EnterWindowMask, &event));
405 void
406 handle_event_shape(XEvent * e)
408 XShapeEvent *ev = (XShapeEvent *) e;
409 Client *c = get_client_bywin(globalconf.clients, ev->window);
411 if(c)
412 window_setshape(get_phys_screen(c->screen), c->win);
415 void
416 handle_event_randr_screen_change_notify(XEvent *e)
418 XRRUpdateConfiguration(e);
419 uicb_exec(0, globalconf.argv);
422 void
423 handle_event_clientmessage(XEvent *e)
425 ewmh_process_client_message(&e->xclient);
428 void
429 grabkeys(int phys_screen)
431 Key *k;
432 KeyCode code;
434 XUngrabKey(globalconf.display, AnyKey, AnyModifier, RootWindow(globalconf.display, phys_screen));
435 for(k = globalconf.keys; k; k = k->next)
437 if((code = XKeysymToKeycode(globalconf.display, k->keysym)) == NoSymbol)
438 continue;
439 XGrabKey(globalconf.display, code, k->mod, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
440 XGrabKey(globalconf.display, code, k->mod | LockMask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
441 XGrabKey(globalconf.display, code, k->mod | globalconf.numlockmask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
442 XGrabKey(globalconf.display, code, k->mod | globalconf.numlockmask | LockMask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
445 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80