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
;
144 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
148 if(ev
->value_mask
& CWX
)
149 c
->rx
= c
->x
= ev
->x
- c
->border
;
150 if(ev
->value_mask
& CWY
)
151 c
->ry
= c
->y
= ev
->y
- c
->border
;
152 if(ev
->value_mask
& CWWidth
)
153 c
->rw
= c
->w
= ev
->width
;
154 if(ev
->value_mask
& CWHeight
)
155 c
->rh
= c
->h
= ev
->height
;
156 if((ev
->value_mask
& (CWX
| CWY
)) && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
157 window_configure(c
->win
, c
->x
, c
->y
, c
->w
, c
->h
, c
->border
);
158 /* recompute screen */
159 old_screen
= c
->screen
;
160 c
->screen
= get_screen_bycoord(c
->x
, c
->y
);
161 if(old_screen
!= c
->screen
)
163 move_client_to_screen(c
, c
->screen
, False
);
164 statusbar_draw_all(old_screen
);
165 statusbar_draw_all(c
->screen
);
167 tag_client_with_rules(c
);
168 XMoveResizeWindow(e
->xany
.display
, c
->win
, c
->rx
, c
->ry
, c
->rw
, c
->rh
);
172 window_configure(c
->win
, c
->x
, c
->y
, c
->w
, c
->h
, c
->border
);
178 wc
.width
= ev
->width
;
179 wc
.height
= ev
->height
;
180 wc
.border_width
= ev
->border_width
;
181 wc
.sibling
= ev
->above
;
182 wc
.stack_mode
= ev
->detail
;
183 XConfigureWindow(e
->xany
.display
, ev
->window
, ev
->value_mask
, &wc
);
185 XSync(e
->xany
.display
, False
);
189 handle_event_configurenotify(XEvent
* e
)
191 XConfigureEvent
*ev
= &e
->xconfigure
;
195 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
196 if(ev
->window
== RootWindow(e
->xany
.display
, screen
)
197 && (ev
->width
!= DisplayWidth(e
->xany
.display
, screen
)
198 || ev
->height
!= DisplayHeight(e
->xany
.display
, screen
)))
200 DisplayWidth(e
->xany
.display
, screen
) = ev
->width
;
201 DisplayHeight(e
->xany
.display
, screen
) = ev
->height
;
203 /* update statusbar */
204 area
= get_screen_area(screen
, NULL
, &globalconf
.screens
[screen
].padding
);
205 globalconf
.screens
[screen
].statusbar
->width
= area
.width
;
207 XResizeWindow(e
->xany
.display
,
208 globalconf
.screens
[screen
].statusbar
->window
,
209 globalconf
.screens
[screen
].statusbar
->width
,
210 globalconf
.screens
[screen
].statusbar
->height
);
212 statusbar_draw_all(screen
);
218 handle_event_destroynotify(XEvent
* e
)
221 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
223 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
224 client_unmanage(c
, WithdrawnState
);
228 handle_event_enternotify(XEvent
* e
)
231 XCrossingEvent
*ev
= &e
->xcrossing
;
235 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
237 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)) && globalconf
.screens
[c
->screen
].sloppy_focus
)
239 focus(c
, ev
->same_screen
, c
->screen
);
240 curtags
= get_current_tags(c
->screen
);
241 if (c
->isfloating
|| curtags
[0]->layout
->arrange
== layout_floating
)
242 window_grabbuttons(get_phys_screen(c
->screen
), c
->win
, True
, False
);
246 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
247 if(ev
->window
== RootWindow(e
->xany
.display
, screen
))
248 focus(NULL
, True
, screen
);
252 handle_event_expose(XEvent
*e
)
254 XExposeEvent
*ev
= &e
->xexpose
;
256 Statusbar
*statusbar
;
259 for(screen
= 0; screen
< get_screen_count(); screen
++)
260 for(statusbar
= globalconf
.screens
[screen
].statusbar
; statusbar
; statusbar
= statusbar
->next
)
261 if(statusbar
->window
== ev
->window
)
263 statusbar_display(statusbar
);
269 handle_event_keypress(XEvent
* e
)
274 XKeyEvent
*ev
= &e
->xkey
;
278 keysym
= XKeycodeToKeysym(e
->xany
.display
, (KeyCode
) ev
->keycode
, 0);
280 /* find the right screen for this event */
281 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
282 if(XQueryPointer(e
->xany
.display
, RootWindow(e
->xany
.display
, screen
), &dummy
, &dummy
, &x
, &y
, &d
, &d
, &m
))
284 /* if screen is 0, we are on first Zaphod screen or on the
285 * only screen in Xinerama, so we can ask for a better screen
286 * number with get_screen_bycoord: we'll get 0 in Zaphod mode
287 * so it's the same, or maybe the real Xinerama screen */
289 screen
= get_screen_bycoord(x
, y
);
293 for(k
= globalconf
.keys
; k
; k
= k
->next
)
294 if(keysym
== k
->keysym
&& k
->func
295 && CLEANMASK(k
->mod
) == CLEANMASK(ev
->state
))
297 k
->func(screen
, k
->arg
);
303 handle_event_leavenotify(XEvent
* e
)
305 XCrossingEvent
*ev
= &e
->xcrossing
;
308 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
309 if((ev
->window
== RootWindow(e
->xany
.display
, screen
)) && !ev
->same_screen
)
310 focus(NULL
, ev
->same_screen
, screen
);
314 handle_event_mappingnotify(XEvent
* e
)
316 XMappingEvent
*ev
= &e
->xmapping
;
319 XRefreshKeyboardMapping(ev
);
320 if(ev
->request
== MappingKeyboard
)
321 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
322 grabkeys(get_phys_screen(screen
));
326 handle_event_maprequest(XEvent
* e
)
328 static XWindowAttributes wa
;
329 XMapRequestEvent
*ev
= &e
->xmaprequest
;
334 if(!XGetWindowAttributes(e
->xany
.display
, ev
->window
, &wa
))
336 if(wa
.override_redirect
)
338 if(!get_client_bywin(globalconf
.clients
, ev
->window
))
340 for(screen
= 0; wa
.screen
!= ScreenOfDisplay(e
->xany
.display
, screen
); screen
++);
341 if(screen
== 0 && XQueryPointer(e
->xany
.display
, RootWindow(e
->xany
.display
, screen
),
342 &dummy
, &dummy
, &x
, &y
, &d
, &d
, &m
))
343 screen
= get_screen_bycoord(x
, y
);
344 client_manage(ev
->window
, &wa
, screen
);
349 handle_event_propertynotify(XEvent
* e
)
353 XPropertyEvent
*ev
= &e
->xproperty
;
355 if(ev
->state
== PropertyDelete
)
357 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
)))
361 case XA_WM_TRANSIENT_FOR
:
362 XGetTransientForHint(e
->xany
.display
, c
->win
, &trans
);
363 if(!c
->isfloating
&& (c
->isfloating
= (get_client_bywin(globalconf
.clients
, trans
) != NULL
)))
366 case XA_WM_NORMAL_HINTS
:
367 client_updatesizehints(c
);
370 client_updatewmhints(c
);
371 statusbar_draw_all(c
->screen
);
374 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== XInternAtom(globalconf
.display
, "_NET_WM_NAME", False
))
376 client_updatetitle(c
);
377 if(c
== globalconf
.focus
->client
)
378 statusbar_draw_all(c
->screen
);
384 handle_event_unmapnotify(XEvent
* e
)
387 XUnmapEvent
*ev
= &e
->xunmap
;
389 if((c
= get_client_bywin(globalconf
.clients
, ev
->window
))
390 && ev
->event
== RootWindow(e
->xany
.display
, get_phys_screen(c
->screen
))
391 && ev
->send_event
&& window_getstate(c
->win
) == NormalState
)
392 client_unmanage(c
, WithdrawnState
);
396 handle_event_shape(XEvent
* e
)
398 XShapeEvent
*ev
= (XShapeEvent
*) e
;
399 Client
*c
= get_client_bywin(globalconf
.clients
, ev
->window
);
402 window_setshape(get_phys_screen(c
->screen
), c
->win
);
406 handle_event_randr_screen_change_notify(XEvent
*e
)
408 XRRUpdateConfiguration(e
);
412 handle_event_clientmessage(XEvent
*e
)
414 ewmh_process_client_message(&e
->xclient
);
418 grabkeys(int phys_screen
)
423 XUngrabKey(globalconf
.display
, AnyKey
, AnyModifier
, RootWindow(globalconf
.display
, phys_screen
));
424 for(k
= globalconf
.keys
; k
; k
= k
->next
)
426 if((code
= XKeysymToKeycode(globalconf
.display
, k
->keysym
)) == NoSymbol
)
428 XGrabKey(globalconf
.display
, code
, k
->mod
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
429 XGrabKey(globalconf
.display
, code
, k
->mod
| LockMask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
430 XGrabKey(globalconf
.display
, code
, k
->mod
| globalconf
.numlockmask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
431 XGrabKey(globalconf
.display
, code
, k
->mod
| globalconf
.numlockmask
| LockMask
, RootWindow(globalconf
.display
, phys_screen
), True
, GrabModeAsync
, GrabModeAsync
);
434 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80