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
.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
);
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
);
113 /* return if no widget match */
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
);
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
= screen_get_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
,
233 (globalconf
.screens
[c
->screen
].sloppy_focus
234 && globalconf
.screens
[c
->screen
].sloppy_focus_raise
));
237 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
238 if(ev
->window
== RootWindow(e
->xany
.display
, screen
))
239 window_root_grabbuttons(screen
);
243 handle_event_expose(XEvent
*e
)
245 XExposeEvent
*ev
= &e
->xexpose
;
247 Statusbar
*statusbar
;
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
);
260 handle_event_keypress(XEvent
* e
)
264 XKeyEvent
*ev
= &e
->xkey
;
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 */
277 screen
= screen_get_bycoord(x
, y
);
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
);
291 handle_event_leavenotify(XEvent
* e
)
293 XCrossingEvent
*ev
= &e
->xcrossing
;
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
);
302 handle_event_mappingnotify(XEvent
*e
)
304 XMappingEvent
*ev
= &e
->xmapping
;
307 XRefreshKeyboardMapping(ev
);
308 if(ev
->request
== MappingKeyboard
)
309 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
310 grabkeys(get_phys_screen(screen
));
314 handle_event_maprequest(XEvent
*e
)
316 static XWindowAttributes wa
;
317 XMapRequestEvent
*ev
= &e
->xmaprequest
;
322 if(!XGetWindowAttributes(e
->xany
.display
, ev
->window
, &wa
))
324 if(wa
.override_redirect
)
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
);
337 handle_event_propertynotify(XEvent
* e
)
341 XPropertyEvent
*ev
= &e
->xproperty
;
343 if(ev
->state
== PropertyDelete
)
345 if((c
= client_get_bywin(globalconf
.clients
, ev
->window
)))
349 case XA_WM_TRANSIENT_FOR
:
350 XGetTransientForHint(e
->xany
.display
, c
->win
, &trans
);
352 && (c
->isfloating
= (client_get_bywin(globalconf
.clients
, trans
) != NULL
)))
353 globalconf
.screens
[c
->screen
].need_arrange
= True
;
355 case XA_WM_NORMAL_HINTS
:
356 client_updatesizehints(c
);
359 client_updatewmhints(c
);
362 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== XInternAtom(globalconf
.display
, "_NET_WM_NAME", False
))
363 client_updatetitle(c
);
368 handle_event_unmapnotify(XEvent
* e
)
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
)
380 handle_event_shape(XEvent
* e
)
382 XShapeEvent
*ev
= (XShapeEvent
*) e
;
383 Client
*c
= client_get_bywin(globalconf
.clients
, ev
->window
);
386 window_setshape(get_phys_screen(c
->screen
), c
->win
);
390 handle_event_randr_screen_change_notify(XEvent
*e
)
392 XRRUpdateConfiguration(e
);
393 uicb_exec(0, globalconf
.argv
);
397 handle_event_clientmessage(XEvent
*e
)
399 ewmh_process_client_message(&e
->xclient
);
403 grabkeys(int phys_screen
)
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)
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