change draw_color_new() proto to fill the struct and return status
[awesome.git] / event.c
blobd0ce38b60a0f95ebb0a6433da6f4c9dac5b5ea11
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.width - ev->x >= widget->area.y
92 && statusbar->sw->geometry.width - 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.height - ev->y >= widget->area.x
102 && statusbar->sw->geometry.height - 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, True);
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 = screen_get_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,
233 (globalconf.screens[c->screen].sloppy_focus
234 && globalconf.screens[c->screen].sloppy_focus_raise));
236 else
237 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
238 if(ev->window == RootWindow(e->xany.display, screen))
239 window_root_grabbuttons(screen);
242 void
243 handle_event_expose(XEvent *e)
245 XExposeEvent *ev = &e->xexpose;
246 int screen;
247 Statusbar *statusbar;
249 if(!ev->count)
250 for(screen = 0; screen < globalconf.nscreen; screen++)
251 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
252 if(statusbar->sw->window == ev->window)
254 statusbar_display(statusbar);
255 return;
259 void
260 handle_event_keypress(XEvent * e)
262 int screen, x, y, d;
263 unsigned int m;
264 XKeyEvent *ev = &e->xkey;
265 Window dummy;
266 Key *k;
268 /* find the right screen for this event */
269 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
270 if(XQueryPointer(e->xany.display, RootWindow(e->xany.display, screen), &dummy, &dummy, &x, &y, &d, &d, &m))
272 /* if screen is 0, we are on first Zaphod screen or on the
273 * only screen in Xinerama, so we can ask for a better screen
274 * number with screen_get_bycoord: we'll get 0 in Zaphod mode
275 * so it's the same, or maybe the real Xinerama screen */
276 if(screen == 0)
277 screen = screen_get_bycoord(x, y);
278 break;
281 for(k = globalconf.keys; k; k = k->next)
282 if(ev->keycode == k->keycode &&
283 k->func && CLEANMASK(k->mod) == CLEANMASK(ev->state))
285 k->func(screen, k->arg);
286 break;
290 void
291 handle_event_leavenotify(XEvent * e)
293 XCrossingEvent *ev = &e->xcrossing;
294 int screen;
296 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
297 if((ev->window == RootWindow(e->xany.display, screen)) && !ev->same_screen)
298 client_focus(NULL, screen, True);
301 void
302 handle_event_mappingnotify(XEvent *e)
304 XMappingEvent *ev = &e->xmapping;
305 int screen;
307 XRefreshKeyboardMapping(ev);
308 if(ev->request == MappingKeyboard)
309 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
310 grabkeys(get_phys_screen(screen));
313 void
314 handle_event_maprequest(XEvent *e)
316 static XWindowAttributes wa;
317 XMapRequestEvent *ev = &e->xmaprequest;
318 int screen, x, y, d;
319 unsigned int m;
320 Window dummy;
322 if(!XGetWindowAttributes(e->xany.display, ev->window, &wa))
323 return;
324 if(wa.override_redirect)
325 return;
326 if(!client_get_bywin(globalconf.clients, ev->window))
328 for(screen = 0; wa.screen != ScreenOfDisplay(e->xany.display, screen); screen++);
329 if(screen == 0 && XQueryPointer(e->xany.display, RootWindow(e->xany.display, screen),
330 &dummy, &dummy, &x, &y, &d, &d, &m))
331 screen = screen_get_bycoord(x, y);
332 client_manage(ev->window, &wa, screen);
336 void
337 handle_event_propertynotify(XEvent * e)
339 Client *c;
340 Window trans;
341 XPropertyEvent *ev = &e->xproperty;
343 if(ev->state == PropertyDelete)
344 return; /* ignore */
345 if((c = client_get_bywin(globalconf.clients, ev->window)))
347 switch (ev->atom)
349 case XA_WM_TRANSIENT_FOR:
350 XGetTransientForHint(e->xany.display, c->win, &trans);
351 if(!c->isfloating
352 && (c->isfloating = (client_get_bywin(globalconf.clients, trans) != NULL)))
353 globalconf.screens[c->screen].need_arrange = True;
354 break;
355 case XA_WM_NORMAL_HINTS:
356 client_updatesizehints(c);
357 break;
358 case XA_WM_HINTS:
359 client_updatewmhints(c);
360 break;
362 if(ev->atom == XA_WM_NAME || ev->atom == XInternAtom(globalconf.display, "_NET_WM_NAME", False))
363 client_updatetitle(c);
367 void
368 handle_event_unmapnotify(XEvent * e)
370 Client *c;
371 XUnmapEvent *ev = &e->xunmap;
373 if((c = client_get_bywin(globalconf.clients, ev->window))
374 && ev->event == RootWindow(e->xany.display, get_phys_screen(c->screen))
375 && ev->send_event && window_getstate(c->win) == NormalState)
376 client_unmanage(c);
379 void
380 handle_event_shape(XEvent * e)
382 XShapeEvent *ev = (XShapeEvent *) e;
383 Client *c = client_get_bywin(globalconf.clients, ev->window);
385 if(c)
386 window_setshape(get_phys_screen(c->screen), c->win);
389 void
390 handle_event_randr_screen_change_notify(XEvent *e)
392 XRRUpdateConfiguration(e);
393 uicb_exec(0, globalconf.argv);
396 void
397 handle_event_clientmessage(XEvent *e)
399 ewmh_process_client_message(&e->xclient);
402 void
403 grabkeys(int phys_screen)
405 Key *k;
406 KeyCode code;
408 XUngrabKey(globalconf.display, AnyKey, AnyModifier, RootWindow(globalconf.display, phys_screen));
409 for(k = globalconf.keys; k; k = k->next)
411 if((code = k->keycode) == 0)
412 continue;
413 XGrabKey(globalconf.display, code, k->mod, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
414 XGrabKey(globalconf.display, code, k->mod | LockMask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
415 XGrabKey(globalconf.display, code, k->mod | globalconf.numlockmask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
416 XGrabKey(globalconf.display, code, k->mod | globalconf.numlockmask | LockMask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
419 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80