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>
31 #include "statusbar.h"
37 #include "layouts/tile.h"
38 #include "layouts/floating.h"
41 extern AwesomeConf globalconf
;
44 handle_mouse_button_press(int screen
, unsigned int button
, unsigned int state
,
45 Button
*buttons
, char *arg
)
49 for(b
= buttons
; b
; b
= b
->next
)
50 if(button
== b
->button
&& CLEANMASK(state
) == b
->mod
&& b
->func
)
55 b
->func(screen
, b
->arg
);
61 handle_event_buttonpress(XEvent
*e
)
63 int i
, screen
, x
= 0, y
= 0;
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
)
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
);
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
);
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
);
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
)
115 XAllowEvents(globalconf
.display
, ReplayPointer
, CurrentTime
);
116 window_grabbuttons(get_phys_screen(c
->screen
), c
->win
, True
, True
);
119 handle_mouse_button_press(c
->screen
, ev
->button
, ev
->state
, globalconf
.buttons
.client
, NULL
);
122 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
123 if(RootWindow(e
->xany
.display
, screen
) == ev
->window
124 && XQueryPointer(e
->xany
.display
,
129 screen
= get_screen_bycoord(x
, y
);
130 handle_mouse_button_press(screen
, ev
->button
, ev
->state
,
131 globalconf
.buttons
.root
, NULL
);
137 handle_event_configurerequest(XEvent
* e
)
140 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
145 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
147 geometry
= c
->geometry
;
149 if(ev
->value_mask
& CWX
)
151 if(ev
->value_mask
& CWY
)
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
);
172 statusbar_draw_all(c
->screen
);
176 window_configure(c
->win
, geometry
, c
->border
);
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
);
193 handle_event_configurenotify(XEvent
* e
)
195 XConfigureEvent
*ev
= &e
->xconfigure
;
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
);
222 handle_event_destroynotify(XEvent
* e
)
225 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
227 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
232 handle_event_enternotify(XEvent
* e
)
235 XCrossingEvent
*ev
= &e
->xcrossing
;
239 if(ev
->mode
!= NotifyNormal
)
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
);
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
);
259 handle_event_expose(XEvent
*e
)
261 XExposeEvent
*ev
= &e
->xexpose
;
263 Statusbar
*statusbar
;
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
);
276 handle_event_keypress(XEvent
* e
)
281 XKeyEvent
*ev
= &e
->xkey
;
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 */
296 screen
= get_screen_bycoord(x
, y
);
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
);
310 handle_event_leavenotify(XEvent
* e
)
312 XCrossingEvent
*ev
= &e
->xcrossing
;
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
);
321 handle_event_mappingnotify(XEvent
* e
)
323 XMappingEvent
*ev
= &e
->xmapping
;
326 XRefreshKeyboardMapping(ev
);
327 if(ev
->request
== MappingKeyboard
)
328 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
329 grabkeys(get_phys_screen(screen
));
333 handle_event_maprequest(XEvent
* e
)
335 static XWindowAttributes wa
;
336 XMapRequestEvent
*ev
= &e
->xmaprequest
;
341 if(!XGetWindowAttributes(e
->xany
.display
, ev
->window
, &wa
))
343 if(wa
.override_redirect
)
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
);
356 handle_event_propertynotify(XEvent
* e
)
360 XPropertyEvent
*ev
= &e
->xproperty
;
362 if(ev
->state
== PropertyDelete
)
364 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
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
)))
373 case XA_WM_NORMAL_HINTS
:
374 client_updatesizehints(c
);
377 client_updatewmhints(c
);
378 statusbar_draw_all(c
->screen
);
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
);
391 handle_event_unmapnotify(XEvent
* e
)
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
)
403 handle_event_shape(XEvent
* e
)
405 XShapeEvent
*ev
= (XShapeEvent
*) e
;
406 Client
*c
= get_client_bywin(globalconf
.clients
, ev
->window
);
409 window_setshape(get_phys_screen(c
->screen
), c
->win
);
413 handle_event_randr_screen_change_notify(XEvent
*e
)
415 XRRUpdateConfiguration(e
);
419 handle_event_clientmessage(XEvent
*e
)
421 ewmh_process_client_message(&e
->xclient
);
425 grabkeys(int phys_screen
)
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
)
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