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"
38 #include "layouts/tile.h"
39 #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
< get_screen_count(); screen
++)
73 for(statusbar
= globalconf
.screens
[screen
].statusbar
; statusbar
; statusbar
= statusbar
->next
)
74 if(statusbar
->window
== ev
->window
)
75 switch(statusbar
->position
)
79 for(widget
= statusbar
->widgets
; widget
; widget
= widget
->next
)
80 if(ev
->x
>= widget
->area
.x
&& ev
->x
< widget
->area
.x
+ widget
->area
.width
81 && ev
->y
>= widget
->area
.y
&& ev
->y
< widget
->area
.y
+ widget
->area
.height
)
83 widget
->button_press(widget
, ev
);
88 for(widget
= statusbar
->widgets
; widget
; widget
= widget
->next
)
89 if(ev
->y
>= widget
->area
.x
&& ev
->y
< widget
->area
.x
+ widget
->area
.width
90 && statusbar
->height
- ev
->x
>= widget
->area
.y
91 && statusbar
->height
- ev
->x
< widget
->area
.y
+ widget
->area
.height
)
93 widget
->button_press(widget
, ev
);
98 for(widget
= statusbar
->widgets
; widget
; widget
= widget
->next
)
99 if(statusbar
->width
- ev
->y
>= widget
->area
.x
100 && statusbar
->width
- ev
->y
< widget
->area
.x
+ widget
->area
.width
101 && ev
->x
>= widget
->area
.y
&& ev
->x
< widget
->area
.y
+ widget
->area
.height
)
103 widget
->button_press(widget
, ev
);
109 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
111 focus(c
, ev
->same_screen
, c
->screen
);
112 if(CLEANMASK(ev
->state
) == NoSymbol
113 && ev
->button
== Button1
)
116 XAllowEvents(globalconf
.display
, ReplayPointer
, CurrentTime
);
117 window_grabbuttons(get_phys_screen(c
->screen
), c
->win
, True
, True
);
120 handle_mouse_button_press(c
->screen
, ev
->button
, ev
->state
, globalconf
.buttons
.client
, NULL
);
123 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
124 if(RootWindow(e
->xany
.display
, screen
) == ev
->window
125 && XQueryPointer(e
->xany
.display
,
130 screen
= get_screen_bycoord(x
, y
);
131 handle_mouse_button_press(screen
, ev
->button
, ev
->state
,
132 globalconf
.buttons
.root
, NULL
);
138 handle_event_configurerequest(XEvent
* e
)
141 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
146 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
148 geometry
= c
->geometry
;
150 if(ev
->value_mask
& CWX
)
152 if(ev
->value_mask
& CWY
)
154 if(ev
->value_mask
& CWWidth
)
155 geometry
.width
= ev
->width
;
156 if(ev
->value_mask
& CWHeight
)
157 geometry
.height
= ev
->height
;
159 if(geometry
.x
!= c
->geometry
.x
|| geometry
.y
!= c
->geometry
.y
160 || geometry
.width
!= c
->geometry
.width
|| geometry
.height
!= c
->geometry
.height
)
162 old_screen
= c
->screen
;
164 client_resize(c
, geometry
, False
);
166 tag_client_with_rules(c
);
168 if(old_screen
!= c
->screen
)
170 widget_invalidate_cache(old_screen
, WIDGET_CACHE_CLIENTS
);
173 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
177 window_configure(c
->win
, geometry
, c
->border
);
183 wc
.width
= ev
->width
;
184 wc
.height
= ev
->height
;
185 wc
.border_width
= ev
->border_width
;
186 wc
.sibling
= ev
->above
;
187 wc
.stack_mode
= ev
->detail
;
188 XConfigureWindow(e
->xany
.display
, ev
->window
, ev
->value_mask
, &wc
);
190 XSync(e
->xany
.display
, False
);
194 handle_event_configurenotify(XEvent
* e
)
196 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
)))
205 DisplayWidth(e
->xany
.display
, screen
) = ev
->width
;
206 DisplayHeight(e
->xany
.display
, screen
) = ev
->height
;
208 /* update statusbar */
209 area
= get_screen_area(screen
, NULL
, &globalconf
.screens
[screen
].padding
);
210 globalconf
.screens
[screen
].statusbar
->width
= area
.width
;
212 XResizeWindow(e
->xany
.display
,
213 globalconf
.screens
[screen
].statusbar
->window
,
214 globalconf
.screens
[screen
].statusbar
->width
,
215 globalconf
.screens
[screen
].statusbar
->height
);
217 widget_invalidate_cache(screen
, WIDGET_CACHE_ALL
);
223 handle_event_destroynotify(XEvent
* e
)
226 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
228 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
233 handle_event_enternotify(XEvent
* e
)
236 XCrossingEvent
*ev
= &e
->xcrossing
;
240 if(ev
->mode
!= NotifyNormal
)
242 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)) && globalconf
.screens
[c
->screen
].sloppy_focus
)
244 focus(c
, ev
->same_screen
, c
->screen
);
245 curtags
= get_current_tags(c
->screen
);
246 if (c
->isfloating
|| curtags
[0]->layout
->arrange
== layout_floating
)
247 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
);
255 if((c
= globalconf
.focus
->client
))
256 window_grabbuttons(c
->screen
, c
->win
, False
, False
);
261 handle_event_expose(XEvent
*e
)
263 XExposeEvent
*ev
= &e
->xexpose
;
265 Statusbar
*statusbar
;
268 for(screen
= 0; screen
< get_screen_count(); screen
++)
269 for(statusbar
= globalconf
.screens
[screen
].statusbar
; statusbar
; statusbar
= statusbar
->next
)
270 if(statusbar
->window
== ev
->window
)
272 statusbar_display(statusbar
);
278 handle_event_keypress(XEvent
* e
)
283 XKeyEvent
*ev
= &e
->xkey
;
287 keysym
= XKeycodeToKeysym(e
->xany
.display
, (KeyCode
) ev
->keycode
, 0);
289 /* find the right screen for this event */
290 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
291 if(XQueryPointer(e
->xany
.display
, RootWindow(e
->xany
.display
, screen
), &dummy
, &dummy
, &x
, &y
, &d
, &d
, &m
))
293 /* if screen is 0, we are on first Zaphod screen or on the
294 * only screen in Xinerama, so we can ask for a better screen
295 * number with get_screen_bycoord: we'll get 0 in Zaphod mode
296 * so it's the same, or maybe the real Xinerama screen */
298 screen
= get_screen_bycoord(x
, y
);
302 for(k
= globalconf
.keys
; k
; k
= k
->next
)
303 if(keysym
== k
->keysym
&& k
->func
304 && CLEANMASK(k
->mod
) == CLEANMASK(ev
->state
))
306 k
->func(screen
, k
->arg
);
312 handle_event_leavenotify(XEvent
* e
)
314 XCrossingEvent
*ev
= &e
->xcrossing
;
317 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
318 if((ev
->window
== RootWindow(e
->xany
.display
, screen
)) && !ev
->same_screen
)
319 focus(NULL
, ev
->same_screen
, screen
);
323 handle_event_mappingnotify(XEvent
* e
)
325 XMappingEvent
*ev
= &e
->xmapping
;
328 XRefreshKeyboardMapping(ev
);
329 if(ev
->request
== MappingKeyboard
)
330 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
331 grabkeys(get_phys_screen(screen
));
335 handle_event_maprequest(XEvent
* e
)
337 static XWindowAttributes wa
;
338 XMapRequestEvent
*ev
= &e
->xmaprequest
;
343 if(!XGetWindowAttributes(e
->xany
.display
, ev
->window
, &wa
))
345 if(wa
.override_redirect
)
347 if(!get_client_bywin(globalconf
.clients
, ev
->window
))
349 for(screen
= 0; wa
.screen
!= ScreenOfDisplay(e
->xany
.display
, screen
); screen
++);
350 if(screen
== 0 && XQueryPointer(e
->xany
.display
, RootWindow(e
->xany
.display
, screen
),
351 &dummy
, &dummy
, &x
, &y
, &d
, &d
, &m
))
352 screen
= get_screen_bycoord(x
, y
);
353 client_manage(ev
->window
, &wa
, screen
);
358 handle_event_propertynotify(XEvent
* e
)
362 XPropertyEvent
*ev
= &e
->xproperty
;
364 if(ev
->state
== PropertyDelete
)
366 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
370 case XA_WM_TRANSIENT_FOR
:
371 XGetTransientForHint(e
->xany
.display
, c
->win
, &trans
);
372 if(!c
->isfloating
&& (c
->isfloating
= (get_client_bywin(globalconf
.clients
, trans
) != NULL
)))
375 case XA_WM_NORMAL_HINTS
:
376 client_updatesizehints(c
);
379 client_updatewmhints(c
);
382 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== XInternAtom(globalconf
.display
, "_NET_WM_NAME", False
))
383 client_updatetitle(c
);
388 handle_event_unmapnotify(XEvent
* e
)
391 XUnmapEvent
*ev
= &e
->xunmap
;
393 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
))
394 && ev
->event
== RootWindow(e
->xany
.display
, get_phys_screen(c
->screen
))
395 && ev
->send_event
&& window_getstate(c
->win
) == NormalState
)
400 handle_event_shape(XEvent
* e
)
402 XShapeEvent
*ev
= (XShapeEvent
*) e
;
403 Client
*c
= get_client_bywin(globalconf
.clients
, ev
->window
);
406 window_setshape(get_phys_screen(c
->screen
), c
->win
);
410 handle_event_randr_screen_change_notify(XEvent
*e
)
412 XRRUpdateConfiguration(e
);
416 handle_event_clientmessage(XEvent
*e
)
418 ewmh_process_client_message(&e
->xclient
);
422 grabkeys(int phys_screen
)
427 XUngrabKey(globalconf
.display
, AnyKey
, AnyModifier
, RootWindow(globalconf
.display
, phys_screen
));
428 for(k
= globalconf
.keys
; k
; k
= k
->next
)
430 if((code
= XKeysymToKeycode(globalconf
.display
, k
->keysym
)) == NoSymbol
)
432 XGrabKey(globalconf
.display
, code
, k
->mod
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
433 XGrabKey(globalconf
.display
, code
, k
->mod
| LockMask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
434 XGrabKey(globalconf
.display
, code
, k
->mod
| globalconf
.numlockmask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
435 XGrabKey(globalconf
.display
, code
, k
->mod
| globalconf
.numlockmask
| LockMask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
438 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80