add show_all option to tasklist
[awesome.git] / event.c
blob0057d01c83328bb9f84098bb7c0097d4e6c52f98
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 "layouts/tile.h"
38 #include "layouts/floating.h"
41 extern AwesomeConf globalconf;
43 static void
44 handle_mouse_button_press(int screen, unsigned int button, unsigned int state,
45 Button *buttons, char *arg)
47 Button *b;
49 for(b = buttons; b; b = b->next)
50 if(button == b->button && CLEANMASK(state) == b->mod && b->func)
52 if(arg)
53 b->func(screen, arg);
54 else
55 b->func(screen, b->arg);
56 return;
60 void
61 handle_event_buttonpress(XEvent *e)
63 int i, screen, x = 0, y = 0;
64 unsigned int udummy;
65 Client *c;
66 Window wdummy;
67 Widget *widget;
68 Statusbar *statusbar;
69 XButtonPressedEvent *ev = &e->xbutton;
71 for(screen = 0; screen < get_screen_count(); screen++)
72 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
73 if(statusbar->window == ev->window)
74 switch(statusbar->position)
76 case Top:
77 case Bottom:
78 for(widget = statusbar->widgets; widget; widget = widget->next)
79 if(ev->x >= widget->area.x && ev->x < widget->area.x + widget->area.width
80 && ev->y >= widget->area.y && ev->y < widget->area.y + widget->area.height)
82 widget->button_press(widget, ev);
83 return;
85 break;
86 case Right:
87 for(widget = statusbar->widgets; widget; widget = widget->next)
88 if(ev->y >= widget->area.x && ev->y < widget->area.x + widget->area.width
89 && statusbar->height - ev->x >= widget->area.y
90 && statusbar->height - ev->x < widget->area.y + widget->area.height)
92 widget->button_press(widget, ev);
93 return;
95 break;
96 default:
97 for(widget = statusbar->widgets; widget; widget = widget->next)
98 if(statusbar->width - ev->y >= widget->area.x
99 && statusbar->width - ev->y < widget->area.x + widget->area.width
100 && ev->x >= widget->area.y && ev->x < widget->area.y + widget->area.height)
102 widget->button_press(widget, ev);
103 return;
105 break;
108 if((c = get_client_bywin(globalconf.clients, ev->window)))
110 focus(c, ev->same_screen, c->screen);
111 if(CLEANMASK(ev->state) == NoSymbol
112 && ev->button == Button1)
114 restack(c->screen);
115 XAllowEvents(globalconf.display, ReplayPointer, CurrentTime);
116 window_grabbuttons(get_phys_screen(c->screen), c->win, True, True);
118 else
119 handle_mouse_button_press(c->screen, ev->button, ev->state, globalconf.buttons.client, NULL);
121 else
122 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
123 if(RootWindow(e->xany.display, screen) == ev->window
124 && XQueryPointer(e->xany.display,
125 ev->window, &wdummy,
126 &wdummy, &x, &y, &i,
127 &i, &udummy))
129 screen = get_screen_bycoord(x, y);
130 handle_mouse_button_press(screen, ev->button, ev->state,
131 globalconf.buttons.root, NULL);
132 return;
136 void
137 handle_event_configurerequest(XEvent * e)
139 Client *c;
140 XConfigureRequestEvent *ev = &e->xconfigurerequest;
141 XWindowChanges wc;
142 int old_screen;
143 Area geometry;
145 if((c = get_client_bywin(globalconf.clients, ev->window)))
147 geometry = c->geometry;
149 if(ev->value_mask & CWX)
150 geometry.x = ev->x;
151 if(ev->value_mask & CWY)
152 geometry.y = ev->y;
153 if(ev->value_mask & CWWidth)
154 geometry.width = ev->width;
155 if(ev->value_mask & CWHeight)
156 geometry.height = ev->height;
158 if(geometry.x != c->geometry.x || geometry.y != c->geometry.y
159 || geometry.width != c->geometry.width || geometry.height != c->geometry.height)
161 old_screen = c->screen;
163 client_resize(c, geometry, False);
165 tag_client_with_rules(c);
167 if(old_screen != c->screen)
169 statusbar_draw_all(old_screen);
170 arrange(old_screen);
172 statusbar_draw_all(c->screen);
173 arrange(c->screen);
175 else
176 window_configure(c->win, geometry, c->border);
178 else
180 wc.x = ev->x;
181 wc.y = ev->y;
182 wc.width = ev->width;
183 wc.height = ev->height;
184 wc.border_width = ev->border_width;
185 wc.sibling = ev->above;
186 wc.stack_mode = ev->detail;
187 XConfigureWindow(e->xany.display, ev->window, ev->value_mask, &wc);
189 XSync(e->xany.display, False);
192 void
193 handle_event_configurenotify(XEvent * e)
195 XConfigureEvent *ev = &e->xconfigure;
196 int screen;
197 Area area;
199 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
200 if(ev->window == RootWindow(e->xany.display, screen)
201 && (ev->width != DisplayWidth(e->xany.display, screen)
202 || ev->height != DisplayHeight(e->xany.display, screen)))
204 DisplayWidth(e->xany.display, screen) = ev->width;
205 DisplayHeight(e->xany.display, screen) = ev->height;
207 /* update statusbar */
208 area = get_screen_area(screen, NULL, &globalconf.screens[screen].padding);
209 globalconf.screens[screen].statusbar->width = area.width;
211 XResizeWindow(e->xany.display,
212 globalconf.screens[screen].statusbar->window,
213 globalconf.screens[screen].statusbar->width,
214 globalconf.screens[screen].statusbar->height);
216 statusbar_draw_all(screen);
217 arrange(screen);
221 void
222 handle_event_destroynotify(XEvent * e)
224 Client *c;
225 XDestroyWindowEvent *ev = &e->xdestroywindow;
227 if((c = get_client_bywin(globalconf.clients, ev->window)))
228 client_unmanage(c);
231 void
232 handle_event_enternotify(XEvent * e)
234 Client *c;
235 XCrossingEvent *ev = &e->xcrossing;
236 int screen;
237 Tag **curtags;
239 if(ev->mode != NotifyNormal)
240 return;
241 if((c = get_client_bywin(globalconf.clients, ev->window)) && globalconf.screens[c->screen].sloppy_focus)
243 focus(c, ev->same_screen, c->screen);
244 curtags = get_current_tags(c->screen);
245 if (c->isfloating || curtags[0]->layout->arrange == layout_floating)
246 window_grabbuttons(get_phys_screen(c->screen), c->win, True, False);
247 p_delete(&curtags);
249 else {
250 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
251 if(ev->window == RootWindow(e->xany.display, screen))
252 focus(NULL, True, screen);
253 if ((c = globalconf.focus->client))
254 window_grabbuttons(c->screen, c->win, False, False);
258 void
259 handle_event_expose(XEvent *e)
261 XExposeEvent *ev = &e->xexpose;
262 int screen;
263 Statusbar *statusbar;
265 if(!ev->count)
266 for(screen = 0; screen < get_screen_count(); screen++)
267 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
268 if(statusbar->window == ev->window)
270 statusbar_display(statusbar);
271 return;
275 void
276 handle_event_keypress(XEvent * e)
278 int screen, x, y, d;
279 unsigned int m;
280 KeySym keysym;
281 XKeyEvent *ev = &e->xkey;
282 Window dummy;
283 Key *k;
285 keysym = XKeycodeToKeysym(e->xany.display, (KeyCode) ev->keycode, 0);
287 /* find the right screen for this event */
288 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
289 if(XQueryPointer(e->xany.display, RootWindow(e->xany.display, screen), &dummy, &dummy, &x, &y, &d, &d, &m))
291 /* if screen is 0, we are on first Zaphod screen or on the
292 * only screen in Xinerama, so we can ask for a better screen
293 * number with get_screen_bycoord: we'll get 0 in Zaphod mode
294 * so it's the same, or maybe the real Xinerama screen */
295 if(screen == 0)
296 screen = get_screen_bycoord(x, y);
297 break;
300 for(k = globalconf.keys; k; k = k->next)
301 if(keysym == k->keysym && k->func
302 && CLEANMASK(k->mod) == CLEANMASK(ev->state))
304 k->func(screen, k->arg);
305 break;
309 void
310 handle_event_leavenotify(XEvent * e)
312 XCrossingEvent *ev = &e->xcrossing;
313 int screen;
315 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
316 if((ev->window == RootWindow(e->xany.display, screen)) && !ev->same_screen)
317 focus(NULL, ev->same_screen, screen);
320 void
321 handle_event_mappingnotify(XEvent * e)
323 XMappingEvent *ev = &e->xmapping;
324 int screen;
326 XRefreshKeyboardMapping(ev);
327 if(ev->request == MappingKeyboard)
328 for(screen = 0; screen < ScreenCount(e->xany.display); screen++)
329 grabkeys(get_phys_screen(screen));
332 void
333 handle_event_maprequest(XEvent * e)
335 static XWindowAttributes wa;
336 XMapRequestEvent *ev = &e->xmaprequest;
337 int screen, x, y, d;
338 unsigned int m;
339 Window dummy;
341 if(!XGetWindowAttributes(e->xany.display, ev->window, &wa))
342 return;
343 if(wa.override_redirect)
344 return;
345 if(!get_client_bywin(globalconf.clients, ev->window))
347 for(screen = 0; wa.screen != ScreenOfDisplay(e->xany.display, screen); screen++);
348 if(screen == 0 && XQueryPointer(e->xany.display, RootWindow(e->xany.display, screen),
349 &dummy, &dummy, &x, &y, &d, &d, &m))
350 screen = get_screen_bycoord(x, y);
351 client_manage(ev->window, &wa, screen);
355 void
356 handle_event_propertynotify(XEvent * e)
358 Client *c;
359 Window trans;
360 XPropertyEvent *ev = &e->xproperty;
362 if(ev->state == PropertyDelete)
363 return; /* ignore */
364 if((c = get_client_bywin(globalconf.clients, ev->window)))
366 switch (ev->atom)
368 case XA_WM_TRANSIENT_FOR:
369 XGetTransientForHint(e->xany.display, c->win, &trans);
370 if(!c->isfloating && (c->isfloating = (get_client_bywin(globalconf.clients, trans) != NULL)))
371 arrange(c->screen);
372 break;
373 case XA_WM_NORMAL_HINTS:
374 client_updatesizehints(c);
375 break;
376 case XA_WM_HINTS:
377 client_updatewmhints(c);
378 statusbar_draw_all(c->screen);
379 break;
381 if(ev->atom == XA_WM_NAME || ev->atom == XInternAtom(globalconf.display, "_NET_WM_NAME", False))
383 client_updatetitle(c);
384 if(c == globalconf.focus->client)
385 statusbar_draw_all(c->screen);
390 void
391 handle_event_unmapnotify(XEvent * e)
393 Client *c;
394 XUnmapEvent *ev = &e->xunmap;
396 if((c = get_client_bywin(globalconf.clients, ev->window))
397 && ev->event == RootWindow(e->xany.display, get_phys_screen(c->screen))
398 && ev->send_event && window_getstate(c->win) == NormalState)
399 client_unmanage(c);
402 void
403 handle_event_shape(XEvent * e)
405 XShapeEvent *ev = (XShapeEvent *) e;
406 Client *c = get_client_bywin(globalconf.clients, ev->window);
408 if(c)
409 window_setshape(get_phys_screen(c->screen), c->win);
412 void
413 handle_event_randr_screen_change_notify(XEvent *e)
415 XRRUpdateConfiguration(e);
418 void
419 handle_event_clientmessage(XEvent *e)
421 ewmh_process_client_message(&e->xclient);
424 void
425 grabkeys(int phys_screen)
427 Key *k;
428 KeyCode code;
430 XUngrabKey(globalconf.display, AnyKey, AnyModifier, RootWindow(globalconf.display, phys_screen));
431 for(k = globalconf.keys; k; k = k->next)
433 if((code = XKeysymToKeycode(globalconf.display, k->keysym)) == NoSymbol)
434 continue;
435 XGrabKey(globalconf.display, code, k->mod, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
436 XGrabKey(globalconf.display, code, k->mod | LockMask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
437 XGrabKey(globalconf.display, code, k->mod | globalconf.numlockmask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
438 XGrabKey(globalconf.display, code, k->mod | globalconf.numlockmask | LockMask, RootWindow(globalconf.display, phys_screen), True, GrabModeAsync, GrabModeAsync);
441 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80