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"
40 #include "layouts/tile.h"
41 #include "layouts/floating.h"
43 extern AwesomeConf globalconf
;
46 handle_mouse_button_press(int screen
, unsigned int button
, unsigned int state
,
47 Button
*buttons
, char *arg
)
51 for(b
= buttons
; b
; b
= b
->next
)
52 if(button
== b
->button
&& CLEANMASK(state
) == b
->mod
&& b
->func
)
57 b
->func(screen
, b
->arg
);
63 handle_event_buttonpress(XEvent
*e
)
65 int i
, screen
, x
= 0, y
= 0;
71 XButtonPressedEvent
*ev
= &e
->xbutton
;
73 for(screen
= 0; screen
< globalconf
.nscreens
; screen
++)
74 for(statusbar
= globalconf
.screens
[screen
].statusbar
; statusbar
; statusbar
= statusbar
->next
)
75 if(statusbar
->window
== ev
->window
|| statusbar
->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
->height
- ev
->x
>= widget
->area
.y
92 && statusbar
->height
- ev
->x
< widget
->area
.y
+ widget
->area
.height
)
94 widget
->button_press(widget
, ev
);
99 for(widget
= statusbar
->widgets
; widget
; widget
= widget
->next
)
100 if(statusbar
->width
- ev
->y
>= widget
->area
.x
101 && statusbar
->width
- ev
->y
< widget
->area
.x
+ widget
->area
.width
102 && ev
->x
>= widget
->area
.y
&& ev
->x
< widget
->area
.y
+ widget
->area
.height
)
104 widget
->button_press(widget
, ev
);
110 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
112 focus(c
, ev
->same_screen
, c
->screen
);
113 if(CLEANMASK(ev
->state
) == NoSymbol
114 && ev
->button
== Button1
)
117 XAllowEvents(globalconf
.display
, ReplayPointer
, CurrentTime
);
118 window_grabbuttons(get_phys_screen(c
->screen
), c
->win
, True
, True
);
121 handle_mouse_button_press(c
->screen
, ev
->button
, ev
->state
, globalconf
.buttons
.client
, NULL
);
124 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
125 if(RootWindow(e
->xany
.display
, screen
) == ev
->window
126 && XQueryPointer(e
->xany
.display
,
131 screen
= get_screen_bycoord(x
, y
);
132 handle_mouse_button_press(screen
, ev
->button
, ev
->state
,
133 globalconf
.buttons
.root
, NULL
);
139 handle_event_configurerequest(XEvent
* e
)
142 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
147 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
149 geometry
= c
->geometry
;
151 if(ev
->value_mask
& CWX
)
153 if(ev
->value_mask
& CWY
)
155 if(ev
->value_mask
& CWWidth
)
156 geometry
.width
= ev
->width
;
157 if(ev
->value_mask
& CWHeight
)
158 geometry
.height
= ev
->height
;
160 if(geometry
.x
!= c
->geometry
.x
|| geometry
.y
!= c
->geometry
.y
161 || geometry
.width
!= c
->geometry
.width
|| geometry
.height
!= c
->geometry
.height
)
163 old_screen
= c
->screen
;
165 client_resize(c
, geometry
, False
);
167 tag_client_with_rule(c
, rule_matching_client(c
));
169 if(old_screen
!= c
->screen
)
171 widget_invalidate_cache(old_screen
, WIDGET_CACHE_CLIENTS
);
174 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
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
);
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
, ev
->same_screen
, 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
< globalconf
.nscreens
; 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
;
288 keysym
= XKeycodeToKeysym(e
->xany
.display
, (KeyCode
) ev
->keycode
, 0);
290 /* find the right screen for this event */
291 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
292 if(XQueryPointer(e
->xany
.display
, RootWindow(e
->xany
.display
, screen
), &dummy
, &dummy
, &x
, &y
, &d
, &d
, &m
))
294 /* if screen is 0, we are on first Zaphod screen or on the
295 * only screen in Xinerama, so we can ask for a better screen
296 * number with get_screen_bycoord: we'll get 0 in Zaphod mode
297 * so it's the same, or maybe the real Xinerama screen */
299 screen
= get_screen_bycoord(x
, y
);
303 for(k
= globalconf
.keys
; k
; k
= k
->next
)
304 if(keysym
== k
->keysym
&& k
->func
305 && CLEANMASK(k
->mod
) == CLEANMASK(ev
->state
))
307 k
->func(screen
, k
->arg
);
310 while(XCheckMaskEvent(globalconf
.display
, EnterWindowMask
, &event
));
314 handle_event_leavenotify(XEvent
* e
)
316 XCrossingEvent
*ev
= &e
->xcrossing
;
319 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
320 if((ev
->window
== RootWindow(e
->xany
.display
, screen
)) && !ev
->same_screen
)
321 focus(NULL
, ev
->same_screen
, screen
);
325 handle_event_mappingnotify(XEvent
*e
)
327 XMappingEvent
*ev
= &e
->xmapping
;
330 XRefreshKeyboardMapping(ev
);
331 if(ev
->request
== MappingKeyboard
)
332 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
333 grabkeys(get_phys_screen(screen
));
337 handle_event_maprequest(XEvent
*e
)
339 static XWindowAttributes wa
;
340 XMapRequestEvent
*ev
= &e
->xmaprequest
;
346 if(!XGetWindowAttributes(e
->xany
.display
, ev
->window
, &wa
))
348 if(wa
.override_redirect
)
350 if(!get_client_bywin(globalconf
.clients
, ev
->window
))
352 for(screen
= 0; wa
.screen
!= ScreenOfDisplay(e
->xany
.display
, screen
); screen
++);
353 if(screen
== 0 && XQueryPointer(e
->xany
.display
, RootWindow(e
->xany
.display
, screen
),
354 &dummy
, &dummy
, &x
, &y
, &d
, &d
, &m
))
355 screen
= get_screen_bycoord(x
, y
);
356 client_manage(ev
->window
, &wa
, screen
);
357 /* do this to keep focused client */
358 while(XCheckMaskEvent(globalconf
.display
, EnterWindowMask
, &event
));
363 handle_event_propertynotify(XEvent
* e
)
367 XPropertyEvent
*ev
= &e
->xproperty
;
369 if(ev
->state
== PropertyDelete
)
371 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
375 case XA_WM_TRANSIENT_FOR
:
376 XGetTransientForHint(e
->xany
.display
, c
->win
, &trans
);
377 if(!c
->isfloating
&& (c
->isfloating
= (get_client_bywin(globalconf
.clients
, trans
) != NULL
)))
380 case XA_WM_NORMAL_HINTS
:
381 client_updatesizehints(c
);
384 client_updatewmhints(c
);
387 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== XInternAtom(globalconf
.display
, "_NET_WM_NAME", False
))
388 client_updatetitle(c
);
393 handle_event_unmapnotify(XEvent
* e
)
396 XUnmapEvent
*ev
= &e
->xunmap
;
399 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
))
400 && ev
->event
== RootWindow(e
->xany
.display
, get_phys_screen(c
->screen
))
401 && ev
->send_event
&& window_getstate(c
->win
) == NormalState
)
403 /* do this to keep focused client */
404 while(XCheckMaskEvent(globalconf
.display
, EnterWindowMask
, &event
));
408 handle_event_shape(XEvent
* e
)
410 XShapeEvent
*ev
= (XShapeEvent
*) e
;
411 Client
*c
= get_client_bywin(globalconf
.clients
, ev
->window
);
414 window_setshape(get_phys_screen(c
->screen
), c
->win
);
418 handle_event_randr_screen_change_notify(XEvent
*e
)
420 XRRUpdateConfiguration(e
);
421 uicb_exec(0, globalconf
.argv
);
425 handle_event_clientmessage(XEvent
*e
)
427 ewmh_process_client_message(&e
->xclient
);
431 grabkeys(int phys_screen
)
436 XUngrabKey(globalconf
.display
, AnyKey
, AnyModifier
, RootWindow(globalconf
.display
, phys_screen
));
437 for(k
= globalconf
.keys
; k
; k
= k
->next
)
439 if((code
= XKeysymToKeycode(globalconf
.display
, k
->keysym
)) == NoSymbol
)
441 XGrabKey(globalconf
.display
, code
, k
->mod
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
442 XGrabKey(globalconf
.display
, code
, k
->mod
| LockMask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
443 XGrabKey(globalconf
.display
, code
, k
->mod
| globalconf
.numlockmask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
444 XGrabKey(globalconf
.display
, code
, k
->mod
| globalconf
.numlockmask
| LockMask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
447 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80