change define
[awesome.git] / event.c
blobdce9bffad2e2cc7498e7e7bd531cc5ff09269ab3
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 "window.h"
33 #include "mouse.h"
34 #include "ewmh.h"
35 #include "client.h"
36 #include "widget.h"
37 #include "xutil.h"
38 #include "rules.h"
39 #include "layouts/tile.h"
40 #include "layouts/floating.h"
42 extern AwesomeConf globalconf;
44 static void
45 handle_mouse_button_press(int screen, unsigned int button, unsigned int state,
46 Button *buttons, char *arg)
48 Button *b;
50 for(b = buttons; b; b = b->next)
51 if(button == b->button && CLEANMASK(state) == b->mod && b->func)
53 if(arg)
54 b->func(screen, arg);
55 else
56 b->func(screen, b->arg);
57 return;
61 void
62 handle_event_buttonpress(XEvent *e)
64 int i, screen, x = 0, y = 0;
65 unsigned int udummy;
66 Client *c;
67 Window wdummy;
68 Widget *widget;
69 Statusbar *statusbar;
70 XButtonPressedEvent *ev = &e->xbutton;
72 for(screen = 0; screen < globalconf.nscreen; screen++)
73 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
74 if(statusbar->sw->window == ev->window || statusbar->sw->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->sw->geometry.height - ev->x >= widget->area.y
92 && statusbar->sw->geometry.height - ev->x
93 < widget->area.y + widget->area.height)
95 widget->button_press(widget, ev);
96 return;
98 break;
99 case Left:
100 for(widget = statusbar->widgets; widget; widget = widget->next)
101 if(statusbar->sw->geometry.width - ev->y >= widget->area.x
102 && statusbar->sw->geometry.width - ev->y
103 < widget->area.x + widget->area.width
104 && ev->x >= widget->area.y && ev->x < widget->area.y + widget->area.height)
106 widget->button_press(widget, ev);
107 return;
109 break;
110 case Off:
111 break;
113 /* return if no widget match */
114 return;
117 if((c = client_get_bywin(globalconf.clients, ev->window)))
119 client_focus(c, c->screen, False);
120 if(CLEANMASK(ev->state) == NoSymbol
121 && ev->button == Button1)
123 XAllowEvents(globalconf.display, ReplayPointer, CurrentTime);
124 window_grabbuttons(get_phys_screen(c->screen), c->win);
126 else
127 handle_mouse_button_press(c->screen, ev->button, ev->state, globalconf.buttons.client, NULL);
129 else
130 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
131 if(RootWindow(e->xany.display, screen) == ev->window
132 && XQueryPointer(e->xany.display,
133 ev->window, &wdummy,
134 &wdummy, &x, &y, &i,
135 &i, &udummy))
137 screen = get_screen_bycoord(x, y);
138 handle_mouse_button_press(screen, ev->button, ev->state,
139 globalconf.buttons.root, NULL);
140 return;
144 void
145 handle_event_configurerequest(XEvent * e)
147 Client *c;
148 XConfigureRequestEvent *ev = &e->xconfigurerequest;
149 XWindowChanges wc;
150 int old_screen;
151 Area geometry;
153 if((c = client_get_bywin(globalconf.clients, ev->window)))
155 geometry = c->geometry;
157 if(ev->value_mask & CWX)
158 geometry.x = ev->x;
159 if(ev->value_mask & CWY)
160 geometry.y = ev->y;
161 if(ev->value_mask & CWWidth)
162 geometry.width = ev->width;
163 if(ev->value_mask & CWHeight)
164 geometry.height = ev->height;
166 if(geometry.x != c->geometry.x || geometry.y != c->geometry.y
167 || geometry.width != c->geometry.width || geometry.height != c->geometry.height)
169 old_screen = c->screen;
171 client_resize(c, geometry, False);
173 tag_client_with_rule(c, rule_matching_client(c));
175 if(!c->isfloating)
176 globalconf.screens[c->screen].need_arrange = True;
178 else
179 window_configure(c->win, geometry, c->border);
181 else
183 wc.x = ev->x;
184 wc.y = ev->y;
185 wc.width = ev->width;
186 wc.height = ev->height;
187 wc.border_width = ev->border_width;
188 wc.sibling = ev->above;
189 wc.stack_mode = ev->detail;
190 XConfigureWindow(e->xany.display, ev->window, ev->value_mask, &wc);
194 void
195 handle_event_configurenotify(XEvent * e)
197 XConfigureEvent *ev = &e->xconfigure;
198 int screen;
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)))
204 /* it's not that we panic, but restart */
205 uicb_exec(0, globalconf.argv);
208 void
209 handle_event_destroynotify(XEvent * e)
211 Client *c;
212 XDestroyWindowEvent *ev = &e->xdestroywindow;
214 if((c = client_get_bywin(globalconf.clients, ev->window)))
215 client_unmanage(c);
218 void
219 handle_event_enternotify(XEvent * e)
221 Client *c;
222 XCrossingEvent *ev = &e->xcrossing;
223 int screen;
225 if(ev->mode != NotifyNormal)
226 return;
228 if((c = client_get_bywin(globalconf.clients, ev->window)))
230 window_grabbuttons(get_phys_screen(c->screen), c->win);
231 if(globalconf.screens[c->screen].sloppy_focus)
232 client_focus(c, c->screen, True);
234 else
235 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
236 if(ev->window == RootWindow(e->xany.display, screen))
237 window_root_grabbuttons(screen);
240 void
241 handle_event_expose(XEvent *e)
243 XExposeEvent *ev = &e->xexpose;
244 int screen;
245 Statusbar *statusbar;
247 if(!ev->count)
248 for(screen = 0; screen < globalconf.nscreen; screen++)
249 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
250 if(statusbar->sw->window == ev->window)
252 statusbar_display(statusbar);
253 return;
257 void
258 handle_event_keypress(XEvent * e)
260 int screen, x, y, d;
261 unsigned int m;
262 KeySym keysym;
263 XKeyEvent *ev = &e->xkey;
264 Window dummy;
265 Key *k;
267 keysym = XKeycodeToKeysym(e->xany.display, (KeyCode) ev->keycode, 0);
269 /* find the right screen for this event */
270 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
271 if(XQueryPointer(e->xany.display, RootWindow(e->xany.display, screen), &dummy, &dummy, &x, &y, &d, &d, &m))
273 /* if screen is 0, we are on first Zaphod screen or on the
274 * only screen in Xinerama, so we can ask for a better screen
275 * number with get_screen_bycoord: we'll get 0 in Zaphod mode
276 * so it's the same, or maybe the real Xinerama screen */
277 if(screen == 0)
278 screen = get_screen_bycoord(x, y);
279 break;
282 for(k = globalconf.keys; k; k = k->next)
283 if(keysym == k->keysym && k->func
284 && CLEANMASK(k->mod) == CLEANMASK(ev->state))
286 k->func(screen, k->arg);
287 break;
291 void
292 handle_event_leavenotify(XEvent * e)
294 XCrossingEvent *ev = &e->xcrossing;
295 int screen;
297 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
298 if((ev->window == RootWindow(e->xany.display, screen)) && !ev->same_screen)
299 client_focus(NULL, screen, False);
302 void
303 handle_event_mappingnotify(XEvent *e)
305 XMappingEvent *ev = &e->xmapping;
306 int screen;
308 XRefreshKeyboardMapping(ev);
309 if(ev->request == MappingKeyboard)
310 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
311 grabkeys(get_phys_screen(screen));
314 void
315 handle_event_maprequest(XEvent *e)
317 static XWindowAttributes wa;
318 XMapRequestEvent *ev = &e->xmaprequest;
319 int screen, x, y, d;
320 unsigned int m;
321 Window dummy;
323 if(!XGetWindowAttributes(e->xany.display, ev->window, &wa))
324 return;
325 if(wa.override_redirect)
326 return;
327 if(!client_get_bywin(globalconf.clients, ev->window))
329 for(screen = 0; wa.screen != ScreenOfDisplay(e->xany.display, screen); screen++);
330 if(screen == 0 && XQueryPointer(e->xany.display, RootWindow(e->xany.display, screen),
331 &dummy, &dummy, &x, &y, &d, &d, &m))
332 screen = get_screen_bycoord(x, y);
333 client_manage(ev->window, &wa, screen);
337 void
338 handle_event_propertynotify(XEvent * e)
340 Client *c;
341 Window trans;
342 XPropertyEvent *ev = &e->xproperty;
344 if(ev->state == PropertyDelete)
345 return; /* ignore */
346 if((c = client_get_bywin(globalconf.clients, ev->window)))
348 switch (ev->atom)
350 case XA_WM_TRANSIENT_FOR:
351 XGetTransientForHint(e->xany.display, c->win, &trans);
352 if(!c->isfloating
353 && (c->isfloating = (client_get_bywin(globalconf.clients, trans) != NULL)))
354 globalconf.screens[c->screen].need_arrange = True;
355 break;
356 case XA_WM_NORMAL_HINTS:
357 client_updatesizehints(c);
358 break;
359 case XA_WM_HINTS:
360 client_updatewmhints(c);
361 break;
363 if(ev->atom == XA_WM_NAME || ev->atom == XInternAtom(globalconf.display, "_NET_WM_NAME", False))
364 client_updatetitle(c);
368 void
369 handle_event_unmapnotify(XEvent * e)
371 Client *c;
372 XUnmapEvent *ev = &e->xunmap;
374 if((c = client_get_bywin(globalconf.clients, ev->window))
375 && ev->event == RootWindow(e->xany.display, get_phys_screen(c->screen))
376 && ev->send_event && window_getstate(c->win) == NormalState)
377 client_unmanage(c);
380 void
381 handle_event_shape(XEvent * e)
383 XShapeEvent *ev = (XShapeEvent *) e;
384 Client *c = client_get_bywin(globalconf.clients, ev->window);
386 if(c)
387 window_setshape(get_phys_screen(c->screen), c->win);
390 void
391 handle_event_randr_screen_change_notify(XEvent *e)
393 XRRUpdateConfiguration(e);
394 uicb_exec(0, globalconf.argv);
397 void
398 handle_event_clientmessage(XEvent *e)
400 ewmh_process_client_message(&e->xclient);
403 void
404 grabkeys(int phys_screen)
406 Key *k;
407 KeyCode code;
409 XUngrabKey(globalconf.display, AnyKey, AnyModifier, RootWindow(globalconf.display, phys_screen));
410 for(k = globalconf.keys; k; k = k->next)
412 if((code = XKeysymToKeycode(globalconf.display, k->keysym)) == NoSymbol)
413 continue;
414 XGrabKey(globalconf.display, code, k->mod, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
415 XGrabKey(globalconf.display, code, k->mod | LockMask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
416 XGrabKey(globalconf.display, code, k->mod | globalconf.numlockmask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
417 XGrabKey(globalconf.display, code, k->mod | globalconf.numlockmask | LockMask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
420 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80