11 #include "extensions.h"
17 #include <X11/keysym.h>
18 #include <X11/Xatom.h>
19 #ifdef HAVE_SYS_SELECT_H
20 # include <sys/select.h>
23 static void event_process(XEvent *e);
24 static void event_handle_root(XEvent *e);
25 static void event_handle_client(Client *c, XEvent *e);
27 Time event_lasttime = 0;
29 /*! The value of the mask for the NumLock modifier */
30 unsigned int NumLockMask;
31 /*! The value of the mask for the ScrollLock modifier */
32 unsigned int ScrollLockMask;
33 /*! The key codes for the modifier keys */
34 static XModifierKeymap *modmap;
35 /*! Table of the constant modifier masks */
36 static const int mask_table[] = {
37 ShiftMask, LockMask, ControlMask, Mod1Mask,
38 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
40 static int mask_table_size;
44 mask_table_size = sizeof(mask_table) / sizeof(mask_table[0]);
46 /* get lock masks that are defined by the display (not constant) */
47 modmap = XGetModifierMapping(ob_display);
49 if (modmap && modmap->max_keypermod > 0) {
51 const size_t size = mask_table_size * modmap->max_keypermod;
52 /* get the values of the keyboard lock modifiers
53 Note: Caps lock is not retrieved the same way as Scroll and Num
54 lock since it doesn't need to be. */
55 const KeyCode num_lock = XKeysymToKeycode(ob_display, XK_Num_Lock);
56 const KeyCode scroll_lock = XKeysymToKeycode(ob_display,
59 for (cnt = 0; cnt < size; ++cnt) {
60 if (! modmap->modifiermap[cnt]) continue;
62 if (num_lock == modmap->modifiermap[cnt])
63 NumLockMask = mask_table[cnt / modmap->max_keypermod];
64 if (scroll_lock == modmap->modifiermap[cnt])
65 ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
72 XFreeModifiermap(modmap);
84 There are slightly different event retrieval semantics here for
85 local (or high bandwidth) versus remote (or low bandwidth)
86 connections to the display/Xserver.
89 if (!XPending(ob_display))
93 This XSync allows for far more compression of events, which
94 makes things like Motion events perform far far better. Since
95 it also means network traffic for every event instead of every
96 X events (where X is the number retrieved at a time), it
97 probably should not be used for setups where Openbox is
98 running on a remote/low bandwidth display/Xserver.
100 XSync(ob_display, FALSE);
101 if (!XEventsQueued(ob_display, QueuedAlready))
104 XNextEvent(ob_display, &e);
109 timer_dispatch((GTimeVal**)&wait);
110 x_fd = ConnectionNumber(ob_display);
112 FD_SET(x_fd, &selset);
113 select(x_fd + 1, &selset, NULL, NULL, wait);
116 void event_process(XEvent *e)
127 window = e->xmap.window;
130 window = e->xunmap.window;
133 window = e->xdestroywindow.window;
135 case ConfigureRequest:
136 window = e->xconfigurerequest.window;
140 if (extensions_xkb && e->type == extensions_xkb_event_basep) {
141 switch (((XkbAnyEvent*)&e)->xkb_type) {
143 window = ((XkbBellNotifyEvent*)&e)->window;
149 window = e->xany.window;
152 client = g_hash_table_lookup(client_map, &window);
154 /* grab the lasttime and hack up the state */
158 event_lasttime = e->xbutton.time;
159 e->xbutton.state &= ~(LockMask | NumLockMask | ScrollLockMask);
160 /* kill off the Button1Mask etc, only want the modifiers */
161 e->xbutton.state &= (ControlMask | ShiftMask | Mod1Mask |
162 Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
165 event_lasttime = e->xkey.time;
166 e->xkey.state &= ~(LockMask | NumLockMask | ScrollLockMask);
167 /* kill off the Button1Mask etc, only want the modifiers */
168 e->xkey.state &= (ControlMask | ShiftMask | Mod1Mask |
169 Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
170 /* add to the state the mask of the modifier being pressed, if it is
171 a modifier key being pressed (this is a little ugly..) */
172 /* I'm commenting this out cuz i don't want "C-Control_L" being returned. */
173 /* kp = modmap->modifiermap;*/
174 /* for (i = 0; i < mask_table_size; ++i) {*/
175 /* for (k = 0; k < modmap->max_keypermod; ++k) {*/
176 /* if (*kp == e->xkey.keycode) {*/ /* found the keycode */
177 /* add the mask for it */
178 /* e->xkey.state |= mask_table[i];*/
179 /* cause the first loop to break; */
180 /* i = mask_table_size;*/
181 /* break;*/ /* get outta here! */
189 event_lasttime = e->xkey.time;
190 e->xkey.state &= ~(LockMask | NumLockMask | ScrollLockMask);
191 /* kill off the Button1Mask etc, only want the modifiers */
192 e->xkey.state &= (ControlMask | ShiftMask | Mod1Mask |
193 Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
194 /* remove from the state the mask of the modifier being released, if
195 it is a modifier key being released (this is a little ugly..) */
196 kp = modmap->modifiermap;
197 for (i = 0; i < mask_table_size; ++i) {
198 for (k = 0; k < modmap->max_keypermod; ++k) {
199 if (*kp == e->xkey.keycode) { /* found the keycode */
200 /* remove the mask for it */
201 e->xkey.state &= ~mask_table[i];
202 /* cause the first loop to break; */
204 break; /* get outta here! */
211 event_lasttime = e->xmotion.time;
212 e->xmotion.state &= ~(LockMask | NumLockMask | ScrollLockMask);
213 /* kill off the Button1Mask etc, only want the modifiers */
214 e->xmotion.state &= (ControlMask | ShiftMask | Mod1Mask |
215 Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
216 /* compress events */
217 while (XCheckTypedWindowEvent(ob_display, window, e->type, &ce)) {
218 e->xmotion.x_root = ce.xmotion.x_root;
219 e->xmotion.y_root = ce.xmotion.y_root;
223 event_lasttime = e->xproperty.time;
226 g_message("FocusIn on %lx mode %d detail %d", window,
227 e->xfocus.mode, e->xfocus.detail);
228 /* NotifyAncestor is not ignored in FocusIn like it is in FocusOut
229 because of RevertToPointerRoot. If the focus ends up reverting to
230 pointer root on a workspace change, then the FocusIn event that we
231 want will be of type NotifyAncestor. This situation does not occur
232 for FocusOut, so it is safely ignored there.
234 if (e->xfocus.detail == NotifyInferior ||
235 e->xfocus.detail > NotifyNonlinearVirtual ||
237 /* says a client was not found for the event (or a valid FocusIn
240 e->xfocus.window = None;
244 g_message("FocusIn on %lx", window);
247 g_message("FocusOut on %lx mode %d detail %d", window,
248 e->xfocus.mode, e->xfocus.detail);
249 if (e->xfocus.mode == NotifyGrab ||
250 e->xfocus.detail == NotifyInferior ||
251 e->xfocus.detail == NotifyAncestor ||
252 e->xfocus.detail > NotifyNonlinearVirtual) return;
254 g_message("FocusOut on %lx", window);
255 /* Try process a FocusIn first, and if a legit one isn't found, then
256 do the fallback shiznit. */
259 gboolean isfo = FALSE;
261 if (XCheckTypedEvent(ob_display, FocusIn, &fi)) {
264 /* when we have gotten a fi/fo pair, then see if there are any
265 more fo's coming. if there are, then don't fallback just yet
267 if ((isfo = XCheckTypedEvent(ob_display, FocusOut, &fo)))
268 XPutBackEvent(ob_display, &fo);
270 /* secret magic way of event_process telling us that no client
271 was found for the FocusIn event. ^_^ */
272 if (!isfo && fi.xfocus.window == None)
273 focus_fallback(FALSE);
274 if (fi.xfocus.window == e->xfocus.window)
277 focus_fallback(FALSE);
282 event_lasttime = e->xcrossing.time;
283 /* NotifyUngrab occurs when a mouse button is released and the event is
284 caused, like when lowering a window */
285 if (e->xcrossing.mode == NotifyGrab) return;
288 event_lasttime = CurrentTime;
292 /* deal with it in the kernel */
294 event_handle_client(client, e);
295 else if (window == ob_root)
296 event_handle_root(e);
297 else if (e->type == MapRequest)
298 client_manage(window);
299 else if (e->type == ConfigureRequest) {
300 /* unhandled configure requests must be used to configure the
304 xwc.x = e->xconfigurerequest.x;
305 xwc.y = e->xconfigurerequest.y;
306 xwc.width = e->xconfigurerequest.width;
307 xwc.height = e->xconfigurerequest.height;
308 xwc.border_width = e->xconfigurerequest.border_width;
309 xwc.sibling = e->xconfigurerequest.above;
310 xwc.stack_mode = e->xconfigurerequest.detail;
312 /* we are not to be held responsible if someone sends us an
314 xerror_set_ignore(TRUE);
315 XConfigureWindow(ob_display, window,
316 e->xconfigurerequest.value_mask, &xwc);
317 xerror_set_ignore(FALSE);
320 /* dispatch the event to registered handlers */
321 dispatch_x(e, client);
324 static void event_handle_root(XEvent *e)
330 if (e->xclient.format != 32) break;
332 msgtype = e->xclient.message_type;
333 if (msgtype == prop_atoms.net_current_desktop) {
334 unsigned int d = e->xclient.data.l[0];
335 if (d < screen_num_desktops)
336 screen_set_desktop(d);
337 } else if (msgtype == prop_atoms.net_number_of_desktops) {
338 unsigned int d = e->xclient.data.l[0];
340 screen_set_num_desktops(d);
341 } else if (msgtype == prop_atoms.net_showing_desktop) {
342 screen_show_desktop(e->xclient.data.l[0] != 0);
346 if (e->xproperty.atom == prop_atoms.net_desktop_names)
347 screen_update_desktop_names();
348 else if (e->xproperty.atom == prop_atoms.net_desktop_layout)
349 screen_update_layout();
354 static void event_handle_client(Client *client, XEvent *e)
359 ConfigValue focus_follow;
363 focus_set_client(client);
365 g_message("Focus%s on client for %lx", (e->type==FocusIn?"In":"Out"),
367 /* focus state can affect the stacking layer */
368 client_calc_layer(client);
369 engine_frame_adjust_focus(client->frame);
372 if (client_normal(client)) {
373 if (ob_state == State_Starting) {
374 /* move it to the top of the focus order */
375 guint desktop = client->desktop;
376 if (desktop == DESKTOP_ALL) desktop = screen_desktop;
377 focus_order[desktop] = g_list_remove(focus_order[desktop],
379 focus_order[desktop] = g_list_prepend(focus_order[desktop],
382 if (!config_get("focusFollowsMouse",Config_Bool,&focus_follow))
383 g_assert_not_reached();
384 if (focus_follow.bool)
385 client_focus(client);
389 case ConfigureRequest:
391 while (XCheckTypedWindowEvent(ob_display, client->window,
392 ConfigureRequest, &ce)) {
394 /* XXX if this causes bad things.. we can compress config req's
395 with the same mask. */
396 e->xconfigurerequest.value_mask |=
397 ce.xconfigurerequest.value_mask;
398 if (ce.xconfigurerequest.value_mask & CWX)
399 e->xconfigurerequest.x = ce.xconfigurerequest.x;
400 if (ce.xconfigurerequest.value_mask & CWY)
401 e->xconfigurerequest.y = ce.xconfigurerequest.y;
402 if (ce.xconfigurerequest.value_mask & CWWidth)
403 e->xconfigurerequest.width = ce.xconfigurerequest.width;
404 if (ce.xconfigurerequest.value_mask & CWHeight)
405 e->xconfigurerequest.height = ce.xconfigurerequest.height;
406 if (ce.xconfigurerequest.value_mask & CWBorderWidth)
407 e->xconfigurerequest.border_width =
408 ce.xconfigurerequest.border_width;
409 if (ce.xconfigurerequest.value_mask & CWStackMode)
410 e->xconfigurerequest.detail = ce.xconfigurerequest.detail;
412 if (i) g_message("Compressed %d Configures", i);
414 /* if we are iconic (or shaded (fvwm does this)) ignore the event */
415 if (client->iconic || client->shaded) return;
417 if (e->xconfigurerequest.value_mask & CWBorderWidth)
418 client->border_width = e->xconfigurerequest.border_width;
420 /* resize, then move, as specified in the EWMH section 7.7 */
421 if (e->xconfigurerequest.value_mask & (CWWidth | CWHeight |
426 x = (e->xconfigurerequest.value_mask & CWX) ?
427 e->xconfigurerequest.x : client->area.x;
428 y = (e->xconfigurerequest.value_mask & CWY) ?
429 e->xconfigurerequest.y : client->area.y;
430 w = (e->xconfigurerequest.value_mask & CWWidth) ?
431 e->xconfigurerequest.width : client->area.width;
432 h = (e->xconfigurerequest.value_mask & CWHeight) ?
433 e->xconfigurerequest.height : client->area.height;
435 switch (client->gravity) {
436 case NorthEastGravity:
438 corner = Corner_TopRight;
440 case SouthWestGravity:
442 corner = Corner_BottomLeft;
444 case SouthEastGravity:
445 corner = Corner_BottomRight;
447 default: /* NorthWest, Static, etc */
448 corner = Corner_TopLeft;
451 client_configure(client, corner, x, y, w, h, FALSE, FALSE);
454 if (e->xconfigurerequest.value_mask & CWStackMode) {
455 switch (e->xconfigurerequest.detail) {
458 stacking_lower(client);
464 stacking_raise(client);
470 if (client->ignore_unmaps) {
471 client->ignore_unmaps--;
474 client_unmanage(client);
477 client_unmanage(client);
480 /* this is when the client is first taken captive in the frame */
481 if (e->xreparent.parent == client->frame->plate) break;
484 This event is quite rare and is usually handled in unmapHandler.
485 However, if the window is unmapped when the reparent event occurs,
486 the window manager never sees it because an unmap event is not sent
487 to an already unmapped window.
490 /* we don't want the reparent event, put it back on the stack for the
491 X server to deal with after we unmanage the window */
492 XPutBackEvent(ob_display, e);
494 client_unmanage(client);
497 if (!client->iconic) break; /* this normally doesn't happen, but if it
498 does, we don't want it! */
499 if (screen_showing_desktop)
500 screen_show_desktop(FALSE);
501 client_iconify(client, FALSE, TRUE);
502 if (!client->frame->visible)
503 /* if its not visible still, then don't mess with it */
506 client_shade(client, FALSE);
507 client_focus(client);
508 stacking_raise(client);
511 /* validate cuz we query stuff off the client here */
512 if (!client_validate(client)) break;
514 if (e->xclient.format != 32) return;
516 msgtype = e->xclient.message_type;
517 if (msgtype == prop_atoms.wm_change_state) {
518 /* compress changes into a single change */
519 while (XCheckTypedWindowEvent(ob_display, e->type,
520 client->window, &ce)) {
521 /* XXX: it would be nice to compress ALL messages of a
522 type, not just messages in a row without other
523 message types between. */
524 if (ce.xclient.message_type != msgtype) {
525 XPutBackEvent(ob_display, &ce);
528 e->xclient = ce.xclient;
530 client_set_wm_state(client, e->xclient.data.l[0]);
531 } else if (msgtype == prop_atoms.net_wm_desktop) {
532 /* compress changes into a single change */
533 while (XCheckTypedWindowEvent(ob_display, e->type,
534 client->window, &ce)) {
535 /* XXX: it would be nice to compress ALL messages of a
536 type, not just messages in a row without other
537 message types between. */
538 if (ce.xclient.message_type != msgtype) {
539 XPutBackEvent(ob_display, &ce);
542 e->xclient = ce.xclient;
544 if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
545 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
546 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
548 } else if (msgtype == prop_atoms.net_wm_state) {
549 /* can't compress these */
550 g_message("net_wm_state %s %ld %ld for 0x%lx",
551 (e->xclient.data.l[0] == 0 ? "Remove" :
552 e->xclient.data.l[0] == 1 ? "Add" :
553 e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
554 e->xclient.data.l[1], e->xclient.data.l[2],
556 client_set_state(client, e->xclient.data.l[0],
557 e->xclient.data.l[1], e->xclient.data.l[2]);
558 } else if (msgtype == prop_atoms.net_close_window) {
559 g_message("net_close_window for 0x%lx", client->window);
560 client_close(client);
561 } else if (msgtype == prop_atoms.net_active_window) {
562 g_message("net_active_window for 0x%lx", client->window);
563 if (screen_showing_desktop)
564 screen_show_desktop(FALSE);
566 client_iconify(client, FALSE, TRUE);
567 else if (!client->frame->visible)
568 /* if its not visible for other reasons, then don't mess
572 client_shade(client, FALSE);
573 client_focus(client);
574 stacking_raise(client);
578 /* validate cuz we query stuff off the client here */
579 if (!client_validate(client)) break;
581 /* compress changes to a single property into a single change */
582 while (XCheckTypedWindowEvent(ob_display, e->type,
583 client->window, &ce)) {
584 /* XXX: it would be nice to compress ALL changes to a property,
585 not just changes in a row without other props between. */
586 if (ce.xproperty.atom != e->xproperty.atom) {
587 XPutBackEvent(ob_display, &ce);
592 msgtype = e->xproperty.atom;
593 if (msgtype == XA_WM_NORMAL_HINTS) {
594 client_update_normal_hints(client);
595 /* normal hints can make a window non-resizable */
596 client_setup_decor_and_functions(client);
598 else if (msgtype == XA_WM_HINTS)
599 client_update_wmhints(client);
600 else if (msgtype == XA_WM_TRANSIENT_FOR) {
601 client_update_transient_for(client);
602 client_get_type(client);
603 /* type may have changed, so update the layer */
604 client_calc_layer(client);
605 client_setup_decor_and_functions(client);
607 else if (msgtype == prop_atoms.net_wm_name ||
608 msgtype == prop_atoms.wm_name)
609 client_update_title(client);
610 else if (msgtype == prop_atoms.net_wm_icon_name ||
611 msgtype == prop_atoms.wm_icon_name)
612 client_update_icon_title(client);
613 else if (msgtype == prop_atoms.wm_class)
614 client_update_class(client);
615 else if (msgtype == prop_atoms.wm_protocols) {
616 client_update_protocols(client);
617 client_setup_decor_and_functions(client);
619 else if (msgtype == prop_atoms.net_wm_strut)
620 client_update_strut(client);
621 else if (msgtype == prop_atoms.net_wm_icon)
622 client_update_icons(client);
623 else if (msgtype == prop_atoms.kwm_win_icon)
624 client_update_kwm_icon(client);
628 if (extensions_shape && e->type == extensions_shape_event_basep) {
629 client->shaped = ((XShapeEvent*)e)->shaped;
630 engine_frame_adjust_shape(client->frame);