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
)
81 widget
->button_press(widget
, ev
);
86 for(widget
= statusbar
->widgets
; widget
; widget
= widget
->next
)
87 if(ev
->y
>= widget
->area
.x
&& ev
->y
<= widget
->area
.x
+ widget
->area
.width
)
89 widget
->button_press(widget
, ev
);
94 for(widget
= statusbar
->widgets
; widget
; widget
= widget
->next
)
95 if(statusbar
->width
- ev
->y
>= widget
->area
.x
96 && statusbar
->width
- ev
->y
<= widget
->area
.x
+ widget
->area
.width
)
98 widget
->button_press(widget
, ev
);
104 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
106 focus(c
, ev
->same_screen
, c
->screen
);
107 if(CLEANMASK(ev
->state
) == NoSymbol
108 && ev
->button
== Button1
)
111 XAllowEvents(globalconf
.display
, ReplayPointer
, CurrentTime
);
112 window_grabbuttons(get_phys_screen(c
->screen
), c
->win
, True
, True
);
115 handle_mouse_button_press(c
->screen
, ev
->button
, ev
->state
, globalconf
.buttons
.client
, NULL
);
118 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
119 if(RootWindow(e
->xany
.display
, screen
) == ev
->window
120 && XQueryPointer(e
->xany
.display
,
125 screen
= get_screen_bycoord(x
, y
);
126 handle_mouse_button_press(screen
, ev
->button
, ev
->state
,
127 globalconf
.buttons
.root
, NULL
);
133 handle_event_configurerequest(XEvent
* e
)
136 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
140 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
144 if(ev
->value_mask
& CWX
)
145 c
->rx
= c
->x
= ev
->x
- c
->border
;
146 if(ev
->value_mask
& CWY
)
147 c
->ry
= c
->y
= ev
->y
- c
->border
;
148 if(ev
->value_mask
& CWWidth
)
149 c
->rw
= c
->w
= ev
->width
;
150 if(ev
->value_mask
& CWHeight
)
151 c
->rh
= c
->h
= ev
->height
;
152 if((ev
->value_mask
& (CWX
| CWY
)) && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
153 window_configure(c
->win
, c
->x
, c
->y
, c
->w
, c
->h
, c
->border
);
154 /* recompute screen */
155 old_screen
= c
->screen
;
156 c
->screen
= get_screen_bycoord(c
->x
, c
->y
);
157 if(old_screen
!= c
->screen
)
159 move_client_to_screen(c
, c
->screen
, False
);
160 statusbar_draw_all(old_screen
);
161 statusbar_draw_all(c
->screen
);
163 tag_client_with_rules(c
);
164 XMoveResizeWindow(e
->xany
.display
, c
->win
, c
->rx
, c
->ry
, c
->rw
, c
->rh
);
168 window_configure(c
->win
, c
->x
, c
->y
, c
->w
, c
->h
, c
->border
);
174 wc
.width
= ev
->width
;
175 wc
.height
= ev
->height
;
176 wc
.border_width
= ev
->border_width
;
177 wc
.sibling
= ev
->above
;
178 wc
.stack_mode
= ev
->detail
;
179 XConfigureWindow(e
->xany
.display
, ev
->window
, ev
->value_mask
, &wc
);
181 XSync(e
->xany
.display
, False
);
185 handle_event_configurenotify(XEvent
* e
)
187 XConfigureEvent
*ev
= &e
->xconfigure
;
191 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
192 if(ev
->window
== RootWindow(e
->xany
.display
, screen
)
193 && (ev
->width
!= DisplayWidth(e
->xany
.display
, screen
)
194 || ev
->height
!= DisplayHeight(e
->xany
.display
, screen
)))
196 DisplayWidth(e
->xany
.display
, screen
) = ev
->width
;
197 DisplayHeight(e
->xany
.display
, screen
) = ev
->height
;
199 /* update statusbar */
200 area
= get_screen_area(screen
, NULL
, &globalconf
.screens
[screen
].padding
);
201 globalconf
.screens
[screen
].statusbar
->width
= area
.width
;
203 XResizeWindow(e
->xany
.display
,
204 globalconf
.screens
[screen
].statusbar
->window
,
205 globalconf
.screens
[screen
].statusbar
->width
,
206 globalconf
.screens
[screen
].statusbar
->height
);
208 statusbar_draw_all(screen
);
214 handle_event_destroynotify(XEvent
* e
)
217 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
219 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
220 client_unmanage(c
, WithdrawnState
);
224 handle_event_enternotify(XEvent
* e
)
227 XCrossingEvent
*ev
= &e
->xcrossing
;
231 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
233 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)) && globalconf
.screens
[c
->screen
].sloppy_focus
)
235 focus(c
, ev
->same_screen
, c
->screen
);
236 curtags
= get_current_tags(c
->screen
);
237 if (c
->isfloating
|| curtags
[0]->layout
->arrange
== layout_floating
)
238 window_grabbuttons(get_phys_screen(c
->screen
), c
->win
, True
, False
);
242 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
243 if(ev
->window
== RootWindow(e
->xany
.display
, screen
))
244 focus(NULL
, True
, screen
);
248 handle_event_expose(XEvent
*e
)
250 XExposeEvent
*ev
= &e
->xexpose
;
252 Statusbar
*statusbar
;
255 for(screen
= 0; screen
< get_screen_count(); screen
++)
256 for(statusbar
= globalconf
.screens
[screen
].statusbar
; statusbar
; statusbar
= statusbar
->next
)
257 if(statusbar
->window
== ev
->window
)
259 statusbar_display(statusbar
);
265 handle_event_keypress(XEvent
* e
)
270 XKeyEvent
*ev
= &e
->xkey
;
274 keysym
= XKeycodeToKeysym(e
->xany
.display
, (KeyCode
) ev
->keycode
, 0);
276 /* find the right screen for this event */
277 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
278 if(XQueryPointer(e
->xany
.display
, RootWindow(e
->xany
.display
, screen
), &dummy
, &dummy
, &x
, &y
, &d
, &d
, &m
))
280 /* if screen is 0, we are on first Zaphod screen or on the
281 * only screen in Xinerama, so we can ask for a better screen
282 * number with get_screen_bycoord: we'll get 0 in Zaphod mode
283 * so it's the same, or maybe the real Xinerama screen */
285 screen
= get_screen_bycoord(x
, y
);
289 for(k
= globalconf
.keys
; k
; k
= k
->next
)
290 if(keysym
== k
->keysym
&& k
->func
291 && CLEANMASK(k
->mod
) == CLEANMASK(ev
->state
))
293 k
->func(screen
, k
->arg
);
299 handle_event_leavenotify(XEvent
* e
)
301 XCrossingEvent
*ev
= &e
->xcrossing
;
304 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
305 if((ev
->window
== RootWindow(e
->xany
.display
, screen
)) && !ev
->same_screen
)
306 focus(NULL
, ev
->same_screen
, screen
);
310 handle_event_mappingnotify(XEvent
* e
)
312 XMappingEvent
*ev
= &e
->xmapping
;
315 XRefreshKeyboardMapping(ev
);
316 if(ev
->request
== MappingKeyboard
)
317 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
318 grabkeys(get_phys_screen(screen
));
322 handle_event_maprequest(XEvent
* e
)
324 static XWindowAttributes wa
;
325 XMapRequestEvent
*ev
= &e
->xmaprequest
;
330 if(!XGetWindowAttributes(e
->xany
.display
, ev
->window
, &wa
))
332 if(wa
.override_redirect
)
334 if(!get_client_bywin(globalconf
.clients
, ev
->window
))
336 for(screen
= 0; wa
.screen
!= ScreenOfDisplay(e
->xany
.display
, screen
); screen
++);
337 if(screen
== 0 && XQueryPointer(e
->xany
.display
, RootWindow(e
->xany
.display
, screen
),
338 &dummy
, &dummy
, &x
, &y
, &d
, &d
, &m
))
339 screen
= get_screen_bycoord(x
, y
);
340 client_manage(ev
->window
, &wa
, screen
);
345 handle_event_propertynotify(XEvent
* e
)
349 XPropertyEvent
*ev
= &e
->xproperty
;
351 if(ev
->state
== PropertyDelete
)
353 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
357 case XA_WM_TRANSIENT_FOR
:
358 XGetTransientForHint(e
->xany
.display
, c
->win
, &trans
);
359 if(!c
->isfloating
&& (c
->isfloating
= (get_client_bywin(globalconf
.clients
, trans
) != NULL
)))
362 case XA_WM_NORMAL_HINTS
:
363 client_updatesizehints(c
);
366 client_updatewmhints(c
);
367 statusbar_draw_all(c
->screen
);
370 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== XInternAtom(globalconf
.display
, "_NET_WM_NAME", False
))
372 client_updatetitle(c
);
373 if(c
== globalconf
.focus
->client
)
374 statusbar_draw_all(c
->screen
);
380 handle_event_unmapnotify(XEvent
* e
)
383 XUnmapEvent
*ev
= &e
->xunmap
;
385 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
))
386 && ev
->event
== RootWindow(e
->xany
.display
, get_phys_screen(c
->screen
))
387 && ev
->send_event
&& window_getstate(c
->win
) == NormalState
)
388 client_unmanage(c
, WithdrawnState
);
392 handle_event_shape(XEvent
* e
)
394 XShapeEvent
*ev
= (XShapeEvent
*) e
;
395 Client
*c
= get_client_bywin(globalconf
.clients
, ev
->window
);
398 window_setshape(get_phys_screen(c
->screen
), c
->win
);
402 handle_event_randr_screen_change_notify(XEvent
*e
)
404 XRRUpdateConfiguration(e
);
408 handle_event_clientmessage(XEvent
*e
)
410 ewmh_process_client_message(&e
->xclient
);
414 grabkeys(int phys_screen
)
419 XUngrabKey(globalconf
.display
, AnyKey
, AnyModifier
, RootWindow(globalconf
.display
, phys_screen
));
420 for(k
= globalconf
.keys
; k
; k
= k
->next
)
422 if((code
= XKeysymToKeycode(globalconf
.display
, k
->keysym
)) == NoSymbol
)
424 XGrabKey(globalconf
.display
, code
, k
->mod
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
425 XGrabKey(globalconf
.display
, code
, k
->mod
| LockMask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
426 XGrabKey(globalconf
.display
, code
, k
->mod
| globalconf
.numlockmask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
427 XGrabKey(globalconf
.display
, code
, k
->mod
| globalconf
.numlockmask
| LockMask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
430 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80