store screen in tag
[awesome.git] / event.c
blob3a626ff654bf9e9c9cb659537c0bd9aae957ef25
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;
286 XEvent event;
288 keysym = XKeycodeToKeysym(e->xany.display, (KeyCode) ev->keycode, 0);
290 /* find the right screen for this event */
291 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
292 if(XQueryPointer(e->xany.display, RootWindow(e->xany.display, screen), &dummy, &dummy, &x, &y, &d, &d, &m))
294 /* if screen is 0, we are on first Zaphod screen or on the
295 * only screen in Xinerama, so we can ask for a better screen
296 * number with get_screen_bycoord: we'll get 0 in Zaphod mode
297 * so it's the same, or maybe the real Xinerama screen */
298 if(screen == 0)
299 screen = get_screen_bycoord(x, y);
300 break;
303 for(k = globalconf.keys; k; k = k->next)
304 if(keysym == k->keysym && k->func
305 && CLEANMASK(k->mod) == CLEANMASK(ev->state))
307 k->func(screen, k->arg);
308 break;
310 while(XCheckMaskEvent(globalconf.display, EnterWindowMask, &event));
313 void
314 handle_event_leavenotify(XEvent * e)
316 XCrossingEvent *ev = &e->xcrossing;
317 int screen;
319 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
320 if((ev->window == RootWindow(e->xany.display, screen)) && !ev->same_screen)
321 focus(NULL, ev->same_screen, screen);
324 void
325 handle_event_mappingnotify(XEvent *e)
327 XMappingEvent *ev = &e->xmapping;
328 int screen;
330 XRefreshKeyboardMapping(ev);
331 if(ev->request == MappingKeyboard)
332 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
333 grabkeys(get_phys_screen(screen));
336 void
337 handle_event_maprequest(XEvent *e)
339 static XWindowAttributes wa;
340 XMapRequestEvent *ev = &e->xmaprequest;
341 int screen, x, y, d;
342 unsigned int m;
343 Window dummy;
344 XEvent event;
346 if(!XGetWindowAttributes(e->xany.display, ev->window, &wa))
347 return;
348 if(wa.override_redirect)
349 return;
350 if(!get_client_bywin(globalconf.clients, ev->window))
352 for(screen = 0; wa.screen != ScreenOfDisplay(e->xany.display, screen); screen++);
353 if(screen == 0 && XQueryPointer(e->xany.display, RootWindow(e->xany.display, screen),
354 &dummy, &dummy, &x, &y, &d, &d, &m))
355 screen = get_screen_bycoord(x, y);
356 client_manage(ev->window, &wa, screen);
357 /* do this to keep focused client */
358 while(XCheckMaskEvent(globalconf.display, EnterWindowMask, &event));
362 void
363 handle_event_propertynotify(XEvent * e)
365 Client *c;
366 Window trans;
367 XPropertyEvent *ev = &e->xproperty;
369 if(ev->state == PropertyDelete)
370 return; /* ignore */
371 if((c = get_client_bywin(globalconf.clients, ev->window)))
373 switch (ev->atom)
375 case XA_WM_TRANSIENT_FOR:
376 XGetTransientForHint(e->xany.display, c->win, &trans);
377 if(!c->isfloating && (c->isfloating = (get_client_bywin(globalconf.clients, trans) != NULL)))
378 arrange(c->screen);
379 break;
380 case XA_WM_NORMAL_HINTS:
381 client_updatesizehints(c);
382 break;
383 case XA_WM_HINTS:
384 client_updatewmhints(c);
385 break;
387 if(ev->atom == XA_WM_NAME || ev->atom == XInternAtom(globalconf.display, "_NET_WM_NAME", False))
388 client_updatetitle(c);
392 void
393 handle_event_unmapnotify(XEvent * e)
395 Client *c;
396 XUnmapEvent *ev = &e->xunmap;
397 XEvent event;
399 if((c = get_client_bywin(globalconf.clients, ev->window))
400 && ev->event == RootWindow(e->xany.display, get_phys_screen(c->screen))
401 && ev->send_event && window_getstate(c->win) == NormalState)
402 client_unmanage(c);
403 /* do this to keep focused client */
404 while(XCheckMaskEvent(globalconf.display, EnterWindowMask, &event));
407 void
408 handle_event_shape(XEvent * e)
410 XShapeEvent *ev = (XShapeEvent *) e;
411 Client *c = get_client_bywin(globalconf.clients, ev->window);
413 if(c)
414 window_setshape(get_phys_screen(c->screen), c->win);
417 void
418 handle_event_randr_screen_change_notify(XEvent *e)
420 XRRUpdateConfiguration(e);
421 uicb_exec(0, globalconf.argv);
424 void
425 handle_event_clientmessage(XEvent *e)
427 ewmh_process_client_message(&e->xclient);
430 void
431 grabkeys(int phys_screen)
433 Key *k;
434 KeyCode code;
436 XUngrabKey(globalconf.display, AnyKey, AnyModifier, RootWindow(globalconf.display, phys_screen));
437 for(k = globalconf.keys; k; k = k->next)
439 if((code = XKeysymToKeycode(globalconf.display, k->keysym)) == NoSymbol)
440 continue;
441 XGrabKey(globalconf.display, code, k->mod, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
442 XGrabKey(globalconf.display, code, k->mod | LockMask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
443 XGrabKey(globalconf.display, code, k->mod | globalconf.numlockmask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
444 XGrabKey(globalconf.display, code, k->mod | globalconf.numlockmask | LockMask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
447 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80