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"
39 #include "layouts/tile.h"
40 #include "layouts/floating.h"
42 extern AwesomeConf globalconf
;
45 handle_mouse_button_press(int screen
, unsigned int button
, unsigned int state
,
46 Button
*buttons
, char *arg
)
50 for(b
= buttons
; b
; b
= b
->next
)
51 if(button
== b
->button
&& CLEANMASK(state
) == b
->mod
&& b
->func
)
56 b
->func(screen
, b
->arg
);
62 handle_event_buttonpress(XEvent
*e
)
64 int i
, screen
, x
= 0, y
= 0;
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
)
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
);
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
);
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
);
113 /* return if no widget match */
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
);
127 handle_mouse_button_press(c
->screen
, ev
->button
, ev
->state
, globalconf
.buttons
.client
, NULL
);
130 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
131 if(RootWindow(e
->xany
.display
, screen
) == ev
->window
132 && XQueryPointer(e
->xany
.display
,
137 screen
= get_screen_bycoord(x
, y
);
138 handle_mouse_button_press(screen
, ev
->button
, ev
->state
,
139 globalconf
.buttons
.root
, NULL
);
145 handle_event_configurerequest(XEvent
* e
)
148 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
153 if((c
= client_get_bywin(globalconf
.clients
, ev
->window
)))
155 geometry
= c
->geometry
;
157 if(ev
->value_mask
& CWX
)
159 if(ev
->value_mask
& CWY
)
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
));
176 globalconf
.screens
[c
->screen
].need_arrange
= True
;
179 window_configure(c
->win
, geometry
, c
->border
);
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
);
195 handle_event_configurenotify(XEvent
* e
)
197 XConfigureEvent
*ev
= &e
->xconfigure
;
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
);
209 handle_event_destroynotify(XEvent
* e
)
212 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
214 if((c
= client_get_bywin(globalconf
.clients
, ev
->window
)))
219 handle_event_enternotify(XEvent
* e
)
222 XCrossingEvent
*ev
= &e
->xcrossing
;
225 if(ev
->mode
!= NotifyNormal
)
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
);
235 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
236 if(ev
->window
== RootWindow(e
->xany
.display
, screen
))
237 window_root_grabbuttons(screen
);
241 handle_event_expose(XEvent
*e
)
243 XExposeEvent
*ev
= &e
->xexpose
;
245 Statusbar
*statusbar
;
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
);
258 handle_event_keypress(XEvent
* e
)
263 XKeyEvent
*ev
= &e
->xkey
;
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 */
278 screen
= get_screen_bycoord(x
, y
);
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
);
292 handle_event_leavenotify(XEvent
* e
)
294 XCrossingEvent
*ev
= &e
->xcrossing
;
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
);
303 handle_event_mappingnotify(XEvent
*e
)
305 XMappingEvent
*ev
= &e
->xmapping
;
308 XRefreshKeyboardMapping(ev
);
309 if(ev
->request
== MappingKeyboard
)
310 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
311 grabkeys(get_phys_screen(screen
));
315 handle_event_maprequest(XEvent
*e
)
317 static XWindowAttributes wa
;
318 XMapRequestEvent
*ev
= &e
->xmaprequest
;
323 if(!XGetWindowAttributes(e
->xany
.display
, ev
->window
, &wa
))
325 if(wa
.override_redirect
)
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
);
338 handle_event_propertynotify(XEvent
* e
)
342 XPropertyEvent
*ev
= &e
->xproperty
;
344 if(ev
->state
== PropertyDelete
)
346 if((c
= client_get_bywin(globalconf
.clients
, ev
->window
)))
350 case XA_WM_TRANSIENT_FOR
:
351 XGetTransientForHint(e
->xany
.display
, c
->win
, &trans
);
353 && (c
->isfloating
= (client_get_bywin(globalconf
.clients
, trans
) != NULL
)))
354 globalconf
.screens
[c
->screen
].need_arrange
= True
;
356 case XA_WM_NORMAL_HINTS
:
357 client_updatesizehints(c
);
360 client_updatewmhints(c
);
363 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== XInternAtom(globalconf
.display
, "_NET_WM_NAME", False
))
364 client_updatetitle(c
);
369 handle_event_unmapnotify(XEvent
* e
)
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
)
381 handle_event_shape(XEvent
* e
)
383 XShapeEvent
*ev
= (XShapeEvent
*) e
;
384 Client
*c
= client_get_bywin(globalconf
.clients
, ev
->window
);
387 window_setshape(get_phys_screen(c
->screen
), c
->win
);
391 handle_event_randr_screen_change_notify(XEvent
*e
)
393 XRRUpdateConfiguration(e
);
394 uicb_exec(0, globalconf
.argv
);
398 handle_event_clientmessage(XEvent
*e
)
400 ewmh_process_client_message(&e
->xclient
);
404 grabkeys(int phys_screen
)
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
)
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