2 * event.c - event handlers
4 * Copyright © 2007 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>
33 #include "statusbar.h"
35 #include "layouts/tile.h"
36 #include "layouts/floating.h"
39 extern DC
*dc
; /* global draw context */
40 extern Client
*clients
, *sel
; /* global client list */
42 #define CLEANMASK(mask, screen) (mask & ~(awesomeconf[screen].numlockmask | LockMask))
43 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
50 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
55 movemouse(Client
* c
, awesome_config
*awesomeconf
)
57 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
63 si
= get_screen_info(c
->display
, c
->screen
, awesomeconf
->statusbar
, &x1
);
67 if(XGrabPointer(c
->display
, RootWindow(c
->display
, c
->screen
), False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
68 None
, dc
[c
->screen
].cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
70 XQueryPointer(c
->display
, RootWindow(c
->display
, c
->screen
), &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
73 XMaskEvent(c
->display
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
77 XUngrabPointer(c
->display
, CurrentTime
);
79 case ConfigureRequest
:
82 handle_event_maprequest(&ev
, awesomeconf
);
85 XSync(c
->display
, False
);
86 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
87 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
88 if(abs(si
[c
->screen
].x_org
+ nx
) < awesomeconf
->snap
)
89 nx
= si
[c
->screen
].x_org
;
90 else if(abs((si
[c
->screen
].x_org
+ si
[c
->screen
].width
) - (nx
+ c
->w
+ 2 * c
->border
)) < awesomeconf
->snap
)
91 nx
= si
[c
->screen
].x_org
+ si
[c
->screen
].width
- c
->w
- 2 * c
->border
;
92 if(abs(si
[c
->screen
].y_org
- ny
) < awesomeconf
->snap
)
93 ny
= si
[c
->screen
].y_org
;
94 else if(abs((si
[c
->screen
].y_org
+ si
[c
->screen
].height
) - (ny
+ c
->h
+ 2 * c
->border
)) < awesomeconf
->snap
)
95 ny
= si
[c
->screen
].y_org
+ si
[c
->screen
].height
- c
->h
- 2 * c
->border
;
96 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
104 resizemouse(Client
* c
, awesome_config
*awesomeconf
)
112 if(XGrabPointer(c
->display
, RootWindow(c
->display
, c
->screen
), False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
113 None
, dc
[c
->screen
].cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
116 XWarpPointer(c
->display
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
119 XMaskEvent(c
->display
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
123 XWarpPointer(c
->display
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
124 XUngrabPointer(c
->display
, CurrentTime
);
125 while(XCheckMaskEvent(c
->display
, EnterWindowMask
, &ev
));
127 case ConfigureRequest
:
130 handle_event_maprequest(&ev
, awesomeconf
);
133 XSync(c
->display
, False
);
134 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
136 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
138 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
145 handle_event_buttonpress(XEvent
* e
, awesome_config
*awesomeconf
)
149 XButtonPressedEvent
*ev
= &e
->xbutton
;
151 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
152 if(awesomeconf
[screen
].statusbar
.window
== ev
->window
)
155 for(i
= 0; i
< awesomeconf
[screen
].ntags
; i
++)
157 x
+= textw(dc
[screen
].font
.set
, dc
[screen
].font
.xfont
, awesomeconf
[screen
].tags
[i
], dc
[screen
].font
.height
);
160 if(ev
->button
== Button1
)
162 if(ev
->state
& awesomeconf
[screen
].modkey
)
163 uicb_tag(e
->xany
.display
, &dc
[screen
], &awesomeconf
[screen
], awesomeconf
[screen
].tags
[i
]);
165 uicb_view(e
->xany
.display
, &dc
[screen
], &awesomeconf
[screen
], awesomeconf
[screen
].tags
[i
]);
167 else if(ev
->button
== Button3
)
169 if(ev
->state
& awesomeconf
[screen
].modkey
)
170 uicb_toggletag(e
->xany
.display
, &dc
[screen
], &awesomeconf
[screen
], awesomeconf
[screen
].tags
[i
]);
172 uicb_toggleview(e
->xany
.display
, &dc
[screen
], &awesomeconf
[screen
], awesomeconf
[screen
].tags
[i
]);
177 if((ev
->x
< x
+ awesomeconf
[screen
].statusbar
.width
) && ev
->button
== Button1
)
178 uicb_setlayout(e
->xany
.display
, &dc
[screen
], &awesomeconf
[screen
], NULL
);
182 if((c
= getclient(ev
->window
)))
184 focus(c
->display
, &dc
[c
->screen
], c
, ev
->same_screen
, &awesomeconf
[c
->screen
]);
185 if(CLEANMASK(ev
->state
, c
->screen
) != awesomeconf
[c
->screen
].modkey
)
187 if(ev
->button
== Button1
)
189 if(!IS_ARRANGE(floating
) && !c
->isfloating
)
190 uicb_togglefloating(e
->xany
.display
, &dc
[c
->screen
], &awesomeconf
[c
->screen
], NULL
);
192 restack(e
->xany
.display
, &dc
[c
->screen
], &awesomeconf
[c
->screen
]);
193 movemouse(c
, &awesomeconf
[c
->screen
]);
195 else if(ev
->button
== Button2
)
197 if(IS_ARRANGE(tile
) && !c
->isfixed
&& c
->isfloating
)
198 uicb_togglefloating(e
->xany
.display
, &dc
[c
->screen
], &awesomeconf
[c
->screen
], NULL
);
200 uicb_zoom(e
->xany
.display
, &dc
[c
->screen
], &awesomeconf
[c
->screen
], NULL
);
202 else if(ev
->button
== Button3
)
204 if(!IS_ARRANGE(floating
) && !c
->isfloating
)
205 uicb_togglefloating(e
->xany
.display
, &dc
[c
->screen
], &awesomeconf
[c
->screen
], NULL
);
207 restack(e
->xany
.display
, &dc
[c
->screen
], &awesomeconf
[c
->screen
]);
208 resizemouse(c
, &awesomeconf
[c
->screen
]);
212 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
213 if(RootWindow(e
->xany
.display
, screen
) == ev
->window
)
215 if(ev
->button
== Button4
)
216 uicb_tag_viewnext(e
->xany
.display
, &dc
[screen
], &awesomeconf
[screen
], NULL
);
217 else if(ev
->button
== Button5
)
218 uicb_tag_viewprev(e
->xany
.display
, &dc
[screen
], &awesomeconf
[screen
], NULL
);
223 handle_event_configurerequest(XEvent
* e
, awesome_config
*awesomeconf
)
226 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
229 if((c
= getclient(ev
->window
)))
232 if(ev
->value_mask
& CWBorderWidth
)
233 c
->border
= ev
->border_width
;
234 if(c
->isfixed
|| c
->isfloating
|| IS_ARRANGE(floating
))
236 if(ev
->value_mask
& CWX
)
238 if(ev
->value_mask
& CWY
)
240 if(ev
->value_mask
& CWWidth
)
242 if(ev
->value_mask
& CWHeight
)
244 if((c
->x
+ c
->w
) > DisplayWidth(c
->display
, c
->screen
) && c
->isfloating
)
245 c
->x
= DisplayWidth(c
->display
, c
->screen
) / 2 - c
->w
/ 2; /* center in x direction */
246 if((c
->y
+ c
->h
) > DisplayHeight(c
->display
, c
->screen
) && c
->isfloating
)
247 c
->y
= DisplayHeight(c
->display
, c
->screen
) / 2 - c
->h
/ 2; /* center in y direction */
248 if((ev
->value_mask
& (CWX
| CWY
)) && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
250 if(isvisible(c
, c
->screen
, awesomeconf
[c
->screen
].selected_tags
, awesomeconf
[c
->screen
].ntags
))
251 XMoveResizeWindow(e
->xany
.display
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
260 wc
.width
= ev
->width
;
261 wc
.height
= ev
->height
;
262 wc
.border_width
= ev
->border_width
;
263 wc
.sibling
= ev
->above
;
264 wc
.stack_mode
= ev
->detail
;
265 XConfigureWindow(e
->xany
.display
, ev
->window
, ev
->value_mask
, &wc
);
267 XSync(e
->xany
.display
, False
);
271 handle_event_configurenotify(XEvent
* e
, awesome_config
*awesomeconf
)
273 XConfigureEvent
*ev
= &e
->xconfigure
;
276 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
277 if(ev
->window
== RootWindow(e
->xany
.display
, screen
)
278 && (ev
->width
!= DisplayWidth(e
->xany
.display
, screen
)
279 || ev
->height
!= DisplayHeight(e
->xany
.display
, screen
)))
281 DisplayWidth(e
->xany
.display
, screen
) = ev
->width
;
282 DisplayHeight(e
->xany
.display
, screen
) = ev
->height
;
283 XFreePixmap(e
->xany
.display
, awesomeconf
[screen
].statusbar
.drawable
);
284 awesomeconf
[screen
].statusbar
.drawable
= XCreatePixmap(e
->xany
.display
, RootWindow(e
->xany
.display
, screen
),
285 DisplayWidth(e
->xany
.display
, screen
),
286 awesomeconf
[screen
].statusbar
.height
,
287 DefaultDepth(e
->xany
.display
, screen
));
288 XResizeWindow(e
->xany
.display
, awesomeconf
[screen
].statusbar
.window
,
289 DisplayWidth(e
->xany
.display
, screen
), awesomeconf
[screen
].statusbar
.height
);
290 updatebarpos(e
->xany
.display
, awesomeconf
[screen
].statusbar
);
291 arrange(e
->xany
.display
, &dc
[screen
], &awesomeconf
[screen
]);
296 handle_event_destroynotify(XEvent
* e
, awesome_config
*awesomeconf
)
299 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
301 if((c
= getclient(ev
->window
)))
302 unmanage(c
, &dc
[c
->screen
], WithdrawnState
, &awesomeconf
[c
->screen
]);
306 handle_event_enternotify(XEvent
* e
, awesome_config
*awesomeconf
)
309 XCrossingEvent
*ev
= &e
->xcrossing
;
312 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
314 if((c
= getclient(ev
->window
)))
315 focus(c
->display
, &dc
[c
->screen
], c
, ev
->same_screen
, &awesomeconf
[c
->screen
]);
317 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
318 if(ev
->window
== RootWindow(e
->xany
.display
, screen
))
319 focus(e
->xany
.display
, &dc
[screen
], NULL
, True
, &awesomeconf
[screen
]);
323 handle_event_expose(XEvent
* e
, awesome_config
*awesomeconf
)
325 XExposeEvent
*ev
= &e
->xexpose
;
329 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
330 if(awesomeconf
[screen
].statusbar
.window
== ev
->window
)
331 drawstatusbar(e
->xany
.display
, screen
, &dc
[screen
], &awesomeconf
[screen
]);
335 handle_event_keypress(XEvent
* e
, awesome_config
*awesomeconf
)
337 int i
, screen
, x
, y
, d
;
340 XKeyEvent
*ev
= &e
->xkey
;
343 keysym
= XKeycodeToKeysym(e
->xany
.display
, (KeyCode
) ev
->keycode
, 0);
345 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
346 if(XQueryPointer(e
->xany
.display
, RootWindow(e
->xany
.display
, screen
), &dummy
, &dummy
, &y
, &x
, &d
, &d
, &m
))
347 for(i
= 0; i
< awesomeconf
[screen
].nkeys
; i
++)
348 if(keysym
== awesomeconf
[screen
].keys
[i
].keysym
349 && CLEANMASK(awesomeconf
[screen
].keys
[i
].mod
, screen
) == CLEANMASK(ev
->state
, screen
) && awesomeconf
[screen
].keys
[i
].func
)
350 awesomeconf
[screen
].keys
[i
].func(e
->xany
.display
, &dc
[screen
], &awesomeconf
[screen
], awesomeconf
[screen
].keys
[i
].arg
);
354 handle_event_leavenotify(XEvent
* e
, awesome_config
*awesomeconf
)
356 XCrossingEvent
*ev
= &e
->xcrossing
;
359 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
360 if((ev
->window
== RootWindow(e
->xany
.display
, screen
)) && !ev
->same_screen
)
361 focus(e
->xany
.display
, &dc
[screen
], NULL
, ev
->same_screen
, &awesomeconf
[screen
]);
365 handle_event_mappingnotify(XEvent
* e
, awesome_config
*awesomeconf
)
367 XMappingEvent
*ev
= &e
->xmapping
;
370 XRefreshKeyboardMapping(ev
);
371 if(ev
->request
== MappingKeyboard
)
372 for(screen
= 0; screen
< ScreenCount(e
->xany
.display
); screen
++)
373 grabkeys(e
->xany
.display
, screen
, &awesomeconf
[screen
]);
377 handle_event_maprequest(XEvent
* e
, awesome_config
*awesomeconf
)
379 static XWindowAttributes wa
;
380 XMapRequestEvent
*ev
= &e
->xmaprequest
;
383 if(!XGetWindowAttributes(e
->xany
.display
, ev
->window
, &wa
))
385 if(wa
.override_redirect
)
387 if(!getclient(ev
->window
))
389 for(screen
= 0; wa
.screen
!= ScreenOfDisplay(e
->xany
.display
, screen
); screen
++);
390 manage(e
->xany
.display
, &dc
[screen
], ev
->window
, &wa
, &awesomeconf
[screen
]);
395 handle_event_propertynotify(XEvent
* e
, awesome_config
*awesomeconf
)
399 XPropertyEvent
*ev
= &e
->xproperty
;
401 if(ev
->state
== PropertyDelete
)
403 if((c
= getclient(ev
->window
)))
407 case XA_WM_TRANSIENT_FOR
:
408 XGetTransientForHint(e
->xany
.display
, c
->win
, &trans
);
409 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
410 arrange(e
->xany
.display
, &dc
[c
->screen
], &awesomeconf
[c
->screen
]);
412 case XA_WM_NORMAL_HINTS
:
416 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== XInternAtom(c
->display
, "_NET_WM_NAME", False
))
420 drawstatusbar(e
->xany
.display
, c
->screen
, &dc
[c
->screen
], &awesomeconf
[c
->screen
]);
426 handle_event_unmapnotify(XEvent
* e
, awesome_config
*awesomeconf
)
429 XUnmapEvent
*ev
= &e
->xunmap
;
431 if((c
= getclient(ev
->window
))
432 && ev
->event
== RootWindow(e
->xany
.display
, c
->screen
) && (ev
->send_event
|| !c
->unmapped
))
433 unmanage(c
, &dc
[c
->screen
], WithdrawnState
, &awesomeconf
[c
->screen
]);
437 handle_event_shape(XEvent
* e
,
438 awesome_config
*awesomeconf
__attribute__ ((unused
)))
440 XShapeEvent
*ev
= (XShapeEvent
*) e
;
441 Client
*c
= getclient(ev
->window
);
448 handle_event_randr_screen_change_notify(XEvent
*e
,
449 awesome_config
*awesomeconf
__attribute__ ((unused
)))
451 XRRUpdateConfiguration(e
);
455 grabkeys(Display
*disp
, int screen
, awesome_config
*awesomeconf
)
460 XUngrabKey(disp
, AnyKey
, AnyModifier
, RootWindow(disp
, screen
));
461 for(i
= 0; i
< awesomeconf
->nkeys
; i
++)
463 if((code
= XKeysymToKeycode(disp
, awesomeconf
->keys
[i
].keysym
)) == NoSymbol
)
465 XGrabKey(disp
, code
, awesomeconf
->keys
[i
].mod
, RootWindow(disp
, screen
), True
, GrabModeAsync
, GrabModeAsync
);
466 XGrabKey(disp
, code
, awesomeconf
->keys
[i
].mod
| LockMask
, RootWindow(disp
, screen
), True
, GrabModeAsync
, GrabModeAsync
);
467 XGrabKey(disp
, code
, awesomeconf
->keys
[i
].mod
| awesomeconf
->numlockmask
, RootWindow(disp
, screen
), True
, GrabModeAsync
, GrabModeAsync
);
468 XGrabKey(disp
, code
, awesomeconf
->keys
[i
].mod
| awesomeconf
->numlockmask
| LockMask
, RootWindow(disp
, screen
), True
,
469 GrabModeAsync
, GrabModeAsync
);