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 if(get_current_layout(c
->screen
)->arrange
!= layout_floating
)
164 c
->isfloating
= True
;
165 client_resize(c
, geometry
, False
);
167 tag_client_with_rules(c
);
169 if(old_screen
!= c
->screen
)
171 statusbar_draw_all(old_screen
);
174 statusbar_draw_all(c
->screen
);
178 window_configure(c
->win
, geometry
, c
->border
);
184 wc
.width
= ev
->width
;
185 wc
.height
= ev
->height
;
186 wc
.border_width
= ev
->border_width
;
187 wc
.sibling
= ev
->above
;
188 wc
.stack_mode
= ev
->detail
;
189 XConfigureWindow(e
->xany
.display
, ev
->window
, ev
->value_mask
, &wc
);
191 XSync(e
->xany
.display
, False
);
195 handle_event_configurenotify(XEvent
* e
)
197 XConfigureEvent
*ev
= &e
->xconfigure
;
201 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
202 if(ev
->window
== RootWindow(e
->xany
.display
, screen
)
203 && (ev
->width
!= DisplayWidth(e
->xany
.display
, screen
)
204 || ev
->height
!= DisplayHeight(e
->xany
.display
, screen
)))
206 DisplayWidth(e
->xany
.display
, screen
) = ev
->width
;
207 DisplayHeight(e
->xany
.display
, screen
) = ev
->height
;
209 /* update statusbar */
210 area
= get_screen_area(screen
, NULL
, &globalconf
.screens
[screen
].padding
);
211 globalconf
.screens
[screen
].statusbar
->width
= area
.width
;
213 XResizeWindow(e
->xany
.display
,
214 globalconf
.screens
[screen
].statusbar
->window
,
215 globalconf
.screens
[screen
].statusbar
->width
,
216 globalconf
.screens
[screen
].statusbar
->height
);
218 statusbar_draw_all(screen
);
224 handle_event_destroynotify(XEvent
* e
)
227 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
229 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
234 handle_event_enternotify(XEvent
* e
)
237 XCrossingEvent
*ev
= &e
->xcrossing
;
241 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
243 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)) && globalconf
.screens
[c
->screen
].sloppy_focus
)
245 focus(c
, ev
->same_screen
, c
->screen
);
246 curtags
= get_current_tags(c
->screen
);
247 if (c
->isfloating
|| curtags
[0]->layout
->arrange
== layout_floating
)
248 window_grabbuttons(get_phys_screen(c
->screen
), c
->win
, True
, False
);
252 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
253 if(ev
->window
== RootWindow(e
->xany
.display
, screen
))
254 focus(NULL
, True
, screen
);
258 handle_event_expose(XEvent
*e
)
260 XExposeEvent
*ev
= &e
->xexpose
;
262 Statusbar
*statusbar
;
265 for(screen
= 0; screen
< get_screen_count(); screen
++)
266 for(statusbar
= globalconf
.screens
[screen
].statusbar
; statusbar
; statusbar
= statusbar
->next
)
267 if(statusbar
->window
== ev
->window
)
269 statusbar_display(statusbar
);
275 handle_event_keypress(XEvent
* e
)
280 XKeyEvent
*ev
= &e
->xkey
;
284 keysym
= XKeycodeToKeysym(e
->xany
.display
, (KeyCode
) ev
->keycode
, 0);
286 /* find the right screen for this event */
287 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
288 if(XQueryPointer(e
->xany
.display
, RootWindow(e
->xany
.display
, screen
), &dummy
, &dummy
, &x
, &y
, &d
, &d
, &m
))
290 /* if screen is 0, we are on first Zaphod screen or on the
291 * only screen in Xinerama, so we can ask for a better screen
292 * number with get_screen_bycoord: we'll get 0 in Zaphod mode
293 * so it's the same, or maybe the real Xinerama screen */
295 screen
= get_screen_bycoord(x
, y
);
299 for(k
= globalconf
.keys
; k
; k
= k
->next
)
300 if(keysym
== k
->keysym
&& k
->func
301 && CLEANMASK(k
->mod
) == CLEANMASK(ev
->state
))
303 k
->func(screen
, k
->arg
);
309 handle_event_leavenotify(XEvent
* e
)
311 XCrossingEvent
*ev
= &e
->xcrossing
;
314 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
315 if((ev
->window
== RootWindow(e
->xany
.display
, screen
)) && !ev
->same_screen
)
316 focus(NULL
, ev
->same_screen
, screen
);
320 handle_event_mappingnotify(XEvent
* e
)
322 XMappingEvent
*ev
= &e
->xmapping
;
325 XRefreshKeyboardMapping(ev
);
326 if(ev
->request
== MappingKeyboard
)
327 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
328 grabkeys(get_phys_screen(screen
));
332 handle_event_maprequest(XEvent
* e
)
334 static XWindowAttributes wa
;
335 XMapRequestEvent
*ev
= &e
->xmaprequest
;
340 if(!XGetWindowAttributes(e
->xany
.display
, ev
->window
, &wa
))
342 if(wa
.override_redirect
)
344 if(!get_client_bywin(globalconf
.clients
, ev
->window
))
346 for(screen
= 0; wa
.screen
!= ScreenOfDisplay(e
->xany
.display
, screen
); screen
++);
347 if(screen
== 0 && XQueryPointer(e
->xany
.display
, RootWindow(e
->xany
.display
, screen
),
348 &dummy
, &dummy
, &x
, &y
, &d
, &d
, &m
))
349 screen
= get_screen_bycoord(x
, y
);
350 client_manage(ev
->window
, &wa
, screen
);
355 handle_event_propertynotify(XEvent
* e
)
359 XPropertyEvent
*ev
= &e
->xproperty
;
361 if(ev
->state
== PropertyDelete
)
363 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
367 case XA_WM_TRANSIENT_FOR
:
368 XGetTransientForHint(e
->xany
.display
, c
->win
, &trans
);
369 if(!c
->isfloating
&& (c
->isfloating
= (get_client_bywin(globalconf
.clients
, trans
) != NULL
)))
372 case XA_WM_NORMAL_HINTS
:
373 client_updatesizehints(c
);
376 client_updatewmhints(c
);
377 statusbar_draw_all(c
->screen
);
380 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== XInternAtom(globalconf
.display
, "_NET_WM_NAME", False
))
382 client_updatetitle(c
);
383 if(c
== globalconf
.focus
->client
)
384 statusbar_draw_all(c
->screen
);
390 handle_event_unmapnotify(XEvent
* e
)
393 XUnmapEvent
*ev
= &e
->xunmap
;
395 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
))
396 && ev
->event
== RootWindow(e
->xany
.display
, get_phys_screen(c
->screen
))
397 && ev
->send_event
&& window_getstate(c
->win
) == NormalState
)
402 handle_event_shape(XEvent
* e
)
404 XShapeEvent
*ev
= (XShapeEvent
*) e
;
405 Client
*c
= get_client_bywin(globalconf
.clients
, ev
->window
);
408 window_setshape(get_phys_screen(c
->screen
), c
->win
);
412 handle_event_randr_screen_change_notify(XEvent
*e
)
414 XRRUpdateConfiguration(e
);
418 handle_event_clientmessage(XEvent
*e
)
420 ewmh_process_client_message(&e
->xclient
);
424 grabkeys(int phys_screen
)
429 XUngrabKey(globalconf
.display
, AnyKey
, AnyModifier
, RootWindow(globalconf
.display
, phys_screen
));
430 for(k
= globalconf
.keys
; k
; k
= k
->next
)
432 if((code
= XKeysymToKeycode(globalconf
.display
, k
->keysym
)) == NoSymbol
)
434 XGrabKey(globalconf
.display
, code
, k
->mod
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
435 XGrabKey(globalconf
.display
, code
, k
->mod
| LockMask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
436 XGrabKey(globalconf
.display
, code
, k
->mod
| globalconf
.numlockmask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
437 XGrabKey(globalconf
.display
, code
, k
->mod
| globalconf
.numlockmask
| LockMask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
440 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80