9 #include "framerender.h"
11 #include "moveresize.h"
13 #include "extensions.h"
18 #include <X11/keysym.h>
19 #include <X11/Xatom.h>
20 #ifdef HAVE_SYS_SELECT_H
21 # include <sys/select.h>
24 static void event_process(XEvent *e);
25 static void event_handle_root(XEvent *e);
26 static void event_handle_client(Client *c, XEvent *e);
27 static void event_handle_menu(Menu *menu, XEvent *e);
29 Time event_lasttime = 0;
31 /*! The value of the mask for the NumLock modifier */
32 unsigned int NumLockMask;
33 /*! The value of the mask for the ScrollLock modifier */
34 unsigned int ScrollLockMask;
35 /*! The key codes for the modifier keys */
36 static XModifierKeymap *modmap;
37 /*! Table of the constant modifier masks */
38 static const int mask_table[] = {
39 ShiftMask, LockMask, ControlMask, Mod1Mask,
40 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
42 static int mask_table_size;
46 mask_table_size = sizeof(mask_table) / sizeof(mask_table[0]);
48 /* get lock masks that are defined by the display (not constant) */
49 modmap = XGetModifierMapping(ob_display);
51 if (modmap && modmap->max_keypermod > 0) {
53 const size_t size = mask_table_size * modmap->max_keypermod;
54 /* get the values of the keyboard lock modifiers
55 Note: Caps lock is not retrieved the same way as Scroll and Num
56 lock since it doesn't need to be. */
57 const KeyCode num_lock = XKeysymToKeycode(ob_display, XK_Num_Lock);
58 const KeyCode scroll_lock = XKeysymToKeycode(ob_display,
61 for (cnt = 0; cnt < size; ++cnt) {
62 if (! modmap->modifiermap[cnt]) continue;
64 if (num_lock == modmap->modifiermap[cnt])
65 NumLockMask = mask_table[cnt / modmap->max_keypermod];
66 if (scroll_lock == modmap->modifiermap[cnt])
67 ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
74 XFreeModifiermap(modmap);
83 gboolean had_event = FALSE;
87 There are slightly different event retrieval semantics here for
88 local (or high bandwidth) versus remote (or low bandwidth)
89 connections to the display/Xserver.
92 if (!XPending(ob_display))
96 This XSync allows for far more compression of events, which
97 makes things like Motion events perform far far better. Since
98 it also means network traffic for every event instead of every
99 X events (where X is the number retrieved at a time), it
100 probably should not be used for setups where Openbox is
101 running on a remote/low bandwidth display/Xserver.
103 XSync(ob_display, FALSE);
104 if (!XEventsQueued(ob_display, QueuedAlready))
107 XNextEvent(ob_display, &e);
114 timer_dispatch((GTimeVal**)&wait);
115 x_fd = ConnectionNumber(ob_display);
117 FD_SET(x_fd, &selset);
118 select(x_fd + 1, &selset, NULL, NULL, wait);
122 static Window event_get_window(XEvent *e)
129 window = e->xmap.window;
132 window = e->xunmap.window;
135 window = e->xdestroywindow.window;
137 case ConfigureRequest:
138 window = e->xconfigurerequest.window;
142 if (extensions_xkb && e->type == extensions_xkb_event_basep) {
143 switch (((XkbAnyEvent*)&e)->xkb_type) {
145 window = ((XkbBellNotifyEvent*)&e)->window;
151 window = e->xany.window;
156 static void event_set_lasttime(XEvent *e)
158 /* grab the lasttime and hack up the state */
162 event_lasttime = e->xbutton.time;
165 event_lasttime = e->xkey.time;
168 event_lasttime = e->xkey.time;
171 event_lasttime = e->xmotion.time;
174 event_lasttime = e->xproperty.time;
178 event_lasttime = e->xcrossing.time;
181 event_lasttime = CurrentTime;
186 #define STRIP_MODS(s) \
187 s &= ~(LockMask | NumLockMask | ScrollLockMask), \
188 /* kill off the Button1Mask etc, only want the modifiers */ \
189 s &= (ControlMask | ShiftMask | Mod1Mask | \
190 Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask) \
192 static void event_hack_mods(XEvent *e)
200 STRIP_MODS(e->xbutton.state);
203 STRIP_MODS(e->xkey.state);
206 STRIP_MODS(e->xkey.state);
207 /* remove from the state the mask of the modifier being released, if
208 it is a modifier key being released (this is a little ugly..) */
209 kp = modmap->modifiermap;
210 for (i = 0; i < mask_table_size; ++i) {
211 for (k = 0; k < modmap->max_keypermod; ++k) {
212 if (*kp == e->xkey.keycode) { /* found the keycode */
213 /* remove the mask for it */
214 e->xkey.state &= ~mask_table[i];
215 /* cause the first loop to break; */
217 break; /* get outta here! */
224 STRIP_MODS(e->xmotion.state);
225 /* compress events */
228 while (XCheckTypedWindowEvent(ob_display, e->xmotion.window,
230 e->xmotion.x_root = ce.xmotion.x_root;
231 e->xmotion.y_root = ce.xmotion.y_root;
238 static gboolean event_ignore(XEvent *e, Client *client)
243 g_message("FocusIn on %lx mode %d detail %d", window,
244 e->xfocus.mode, e->xfocus.detail);
246 /* NotifyAncestor is not ignored in FocusIn like it is in FocusOut
247 because of RevertToPointerRoot. If the focus ends up reverting to
248 pointer root on a workspace change, then the FocusIn event that we
249 want will be of type NotifyAncestor. This situation does not occur
250 for FocusOut, so it is safely ignored there.
252 if (e->xfocus.detail == NotifyInferior ||
253 e->xfocus.detail > NotifyNonlinearVirtual ||
255 /* says a client was not found for the event (or a valid FocusIn
258 e->xfocus.window = None;
263 g_message("FocusIn on %lx", window);
268 g_message("FocusOut on %lx mode %d detail %d", window,
269 e->xfocus.mode, e->xfocus.detail);
271 if (e->xfocus.mode == NotifyGrab ||
272 e->xfocus.detail == NotifyInferior ||
273 e->xfocus.detail == NotifyAncestor ||
274 e->xfocus.detail > NotifyNonlinearVirtual) return TRUE;
277 g_message("FocusOut on %lx", window);
279 /* Try process a FocusIn first, and if a legit one isn't found, then
280 do the fallback shiznit. */
283 gboolean isfo = FALSE;
285 if (XCheckTypedEvent(ob_display, FocusIn, &fi)) {
288 /* when we have gotten a fi/fo pair, then see if there are any
289 more fo's coming. if there are, then don't fallback just yet
291 if ((isfo = XCheckTypedEvent(ob_display, FocusOut, &fo)))
292 XPutBackEvent(ob_display, &fo);
294 /* secret magic way of event_process telling us that no client
295 was found for the FocusIn event. ^_^ */
296 if (!isfo && fi.xfocus.window == None)
297 focus_fallback(Fallback_NoFocus);
298 if (fi.xfocus.window == e->xfocus.window)
301 focus_fallback(Fallback_NoFocus);
306 /* NotifyUngrab occurs when a mouse button is released and the event is
307 caused, like when lowering a window */
308 /* NotifyVirtual occurs when ungrabbing the pointer */
309 if (e->xcrossing.mode == NotifyGrab ||
310 e->xcrossing.detail == NotifyInferior ||
311 e->xcrossing.detail == NotifyVirtual)
318 static void event_process(XEvent *e)
324 window = event_get_window(e);
325 if (!(client = g_hash_table_lookup(client_map, &window)))
326 menu = g_hash_table_lookup(menu_map, &window);
327 event_set_lasttime(e);
329 if (event_ignore(e, client))
332 /* deal with it in the kernel */
334 event_handle_menu(menu, e);
337 event_handle_client(client, e);
338 else if (window == ob_root)
339 event_handle_root(e);
340 else if (e->type == MapRequest)
341 client_manage(window);
342 else if (e->type == ConfigureRequest) {
343 /* unhandled configure requests must be used to configure the
347 xwc.x = e->xconfigurerequest.x;
348 xwc.y = e->xconfigurerequest.y;
349 xwc.width = e->xconfigurerequest.width;
350 xwc.height = e->xconfigurerequest.height;
351 xwc.border_width = e->xconfigurerequest.border_width;
352 xwc.sibling = e->xconfigurerequest.above;
353 xwc.stack_mode = e->xconfigurerequest.detail;
355 /* we are not to be held responsible if someone sends us an
357 xerror_set_ignore(TRUE);
358 XConfigureWindow(ob_display, window,
359 e->xconfigurerequest.value_mask, &xwc);
360 xerror_set_ignore(FALSE);
363 if (moveresize_in_progress)
364 if (e->type == MotionNotify || e->type == ButtonRelease ||
365 e->type == ButtonPress ||
366 e->type == KeyPress || e->type == KeyRelease) {
368 return; /* no dispatch! */
371 /* user input (action-bound) events */
373 if (e->type == ButtonPress || e->type == ButtonRelease ||
374 e->type == MotionNotify)
375 mouse_event(e, client);
376 else if (e->type == KeyPress || e->type == KeyRelease)
380 /* dispatch the event to registered handlers */
381 dispatch_x(e, client);
384 static void event_handle_root(XEvent *e)
390 if (e->xclient.format != 32) break;
392 msgtype = e->xclient.message_type;
393 if (msgtype == prop_atoms.net_current_desktop) {
394 unsigned int d = e->xclient.data.l[0];
395 if (d < screen_num_desktops)
396 screen_set_desktop(d);
397 } else if (msgtype == prop_atoms.net_number_of_desktops) {
398 unsigned int d = e->xclient.data.l[0];
400 screen_set_num_desktops(d);
401 } else if (msgtype == prop_atoms.net_showing_desktop) {
402 screen_show_desktop(e->xclient.data.l[0] != 0);
406 if (e->xproperty.atom == prop_atoms.net_desktop_names)
407 screen_update_desktop_names();
408 else if (e->xproperty.atom == prop_atoms.net_desktop_layout)
409 screen_update_layout();
414 static void event_handle_client(Client *client, XEvent *e)
423 switch (frame_context(client, e->xbutton.window)) {
424 case Context_Maximize:
425 client->frame->max_press = (e->type == ButtonPress);
426 framerender_frame(client->frame);
429 client->frame->close_press = (e->type == ButtonPress);
430 framerender_frame(client->frame);
432 case Context_Iconify:
433 client->frame->iconify_press = (e->type == ButtonPress);
434 framerender_frame(client->frame);
436 case Context_AllDesktops:
437 client->frame->desk_press = (e->type == ButtonPress);
438 framerender_frame(client->frame);
441 client->frame->shade_press = (e->type == ButtonPress);
442 framerender_frame(client->frame);
445 /* nothing changes with clicks for any other contexts */
450 focus_set_client(client);
453 g_message("Focus%s on client for %lx", (e->type==FocusIn?"In":"Out"),
456 /* focus state can affect the stacking layer */
457 client_calc_layer(client);
458 frame_adjust_focus(client->frame);
461 if (client_normal(client)) {
462 if (ob_state == State_Starting) {
463 /* move it to the top of the focus order */
464 guint desktop = client->desktop;
465 if (desktop == DESKTOP_ALL) desktop = screen_desktop;
466 focus_order[desktop] = g_list_remove(focus_order[desktop],
468 focus_order[desktop] = g_list_prepend(focus_order[desktop],
470 } else if (config_focus_follow) {
472 g_message("EnterNotify on %lx, focusing window",
475 client_focus(client);
479 case ConfigureRequest:
481 while (XCheckTypedWindowEvent(ob_display, client->window,
482 ConfigureRequest, &ce)) {
484 /* XXX if this causes bad things.. we can compress config req's
485 with the same mask. */
486 e->xconfigurerequest.value_mask |=
487 ce.xconfigurerequest.value_mask;
488 if (ce.xconfigurerequest.value_mask & CWX)
489 e->xconfigurerequest.x = ce.xconfigurerequest.x;
490 if (ce.xconfigurerequest.value_mask & CWY)
491 e->xconfigurerequest.y = ce.xconfigurerequest.y;
492 if (ce.xconfigurerequest.value_mask & CWWidth)
493 e->xconfigurerequest.width = ce.xconfigurerequest.width;
494 if (ce.xconfigurerequest.value_mask & CWHeight)
495 e->xconfigurerequest.height = ce.xconfigurerequest.height;
496 if (ce.xconfigurerequest.value_mask & CWBorderWidth)
497 e->xconfigurerequest.border_width =
498 ce.xconfigurerequest.border_width;
499 if (ce.xconfigurerequest.value_mask & CWStackMode)
500 e->xconfigurerequest.detail = ce.xconfigurerequest.detail;
503 /* if we are iconic (or shaded (fvwm does this)) ignore the event */
504 if (client->iconic || client->shaded) return;
506 if (e->xconfigurerequest.value_mask & CWBorderWidth)
507 client->border_width = e->xconfigurerequest.border_width;
509 /* resize, then move, as specified in the EWMH section 7.7 */
510 if (e->xconfigurerequest.value_mask & (CWWidth | CWHeight |
515 x = (e->xconfigurerequest.value_mask & CWX) ?
516 e->xconfigurerequest.x : client->area.x;
517 y = (e->xconfigurerequest.value_mask & CWY) ?
518 e->xconfigurerequest.y : client->area.y;
519 w = (e->xconfigurerequest.value_mask & CWWidth) ?
520 e->xconfigurerequest.width : client->area.width;
521 h = (e->xconfigurerequest.value_mask & CWHeight) ?
522 e->xconfigurerequest.height : client->area.height;
524 switch (client->gravity) {
525 case NorthEastGravity:
527 corner = Corner_TopRight;
529 case SouthWestGravity:
531 corner = Corner_BottomLeft;
533 case SouthEastGravity:
534 corner = Corner_BottomRight;
536 default: /* NorthWest, Static, etc */
537 corner = Corner_TopLeft;
540 client_configure(client, corner, x, y, w, h, FALSE, FALSE);
543 if (e->xconfigurerequest.value_mask & CWStackMode) {
544 switch (e->xconfigurerequest.detail) {
547 stacking_lower(client);
553 stacking_raise(client);
559 if (client->ignore_unmaps) {
560 client->ignore_unmaps--;
563 client_unmanage(client);
566 client_unmanage(client);
569 /* this is when the client is first taken captive in the frame */
570 if (e->xreparent.parent == client->frame->plate) break;
573 This event is quite rare and is usually handled in unmapHandler.
574 However, if the window is unmapped when the reparent event occurs,
575 the window manager never sees it because an unmap event is not sent
576 to an already unmapped window.
579 /* we don't want the reparent event, put it back on the stack for the
580 X server to deal with after we unmanage the window */
581 XPutBackEvent(ob_display, e);
583 client_unmanage(client);
586 g_message("MapRequest for 0x%lx", client->window);
587 if (!client->iconic) break; /* this normally doesn't happen, but if it
588 does, we don't want it! */
589 if (screen_showing_desktop)
590 screen_show_desktop(FALSE);
591 client_iconify(client, FALSE, TRUE);
592 if (!client->frame->visible)
593 /* if its not visible still, then don't mess with it */
596 client_shade(client, FALSE);
597 client_focus(client);
598 stacking_raise(client);
601 /* validate cuz we query stuff off the client here */
602 if (!client_validate(client)) break;
604 if (e->xclient.format != 32) return;
606 msgtype = e->xclient.message_type;
607 if (msgtype == prop_atoms.wm_change_state) {
608 /* compress changes into a single change */
609 while (XCheckTypedWindowEvent(ob_display, e->type,
610 client->window, &ce)) {
611 /* XXX: it would be nice to compress ALL messages of a
612 type, not just messages in a row without other
613 message types between. */
614 if (ce.xclient.message_type != msgtype) {
615 XPutBackEvent(ob_display, &ce);
618 e->xclient = ce.xclient;
620 client_set_wm_state(client, e->xclient.data.l[0]);
621 } else if (msgtype == prop_atoms.net_wm_desktop) {
622 /* compress changes into a single change */
623 while (XCheckTypedWindowEvent(ob_display, e->type,
624 client->window, &ce)) {
625 /* XXX: it would be nice to compress ALL messages of a
626 type, not just messages in a row without other
627 message types between. */
628 if (ce.xclient.message_type != msgtype) {
629 XPutBackEvent(ob_display, &ce);
632 e->xclient = ce.xclient;
634 if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
635 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
636 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
638 } else if (msgtype == prop_atoms.net_wm_state) {
639 /* can't compress these */
640 g_message("net_wm_state %s %ld %ld for 0x%lx",
641 (e->xclient.data.l[0] == 0 ? "Remove" :
642 e->xclient.data.l[0] == 1 ? "Add" :
643 e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
644 e->xclient.data.l[1], e->xclient.data.l[2],
646 client_set_state(client, e->xclient.data.l[0],
647 e->xclient.data.l[1], e->xclient.data.l[2]);
648 } else if (msgtype == prop_atoms.net_close_window) {
649 g_message("net_close_window for 0x%lx", client->window);
650 client_close(client);
651 } else if (msgtype == prop_atoms.net_active_window) {
652 g_message("net_active_window for 0x%lx", client->window);
653 if (screen_showing_desktop)
654 screen_show_desktop(FALSE);
656 client_iconify(client, FALSE, TRUE);
657 else if (!client->frame->visible)
658 /* if its not visible for other reasons, then don't mess
662 client_shade(client, FALSE);
663 client_focus(client);
664 stacking_raise(client);
665 } else if (msgtype == prop_atoms.net_wm_moveresize) {
666 g_message("net_wm_moveresize for 0x%lx", client->window);
667 if ((Atom)e->xclient.data.l[2] ==
668 prop_atoms.net_wm_moveresize_size_topleft ||
669 (Atom)e->xclient.data.l[2] ==
670 prop_atoms.net_wm_moveresize_size_top ||
671 (Atom)e->xclient.data.l[2] ==
672 prop_atoms.net_wm_moveresize_size_topright ||
673 (Atom)e->xclient.data.l[2] ==
674 prop_atoms.net_wm_moveresize_size_right ||
675 (Atom)e->xclient.data.l[2] ==
676 prop_atoms.net_wm_moveresize_size_right ||
677 (Atom)e->xclient.data.l[2] ==
678 prop_atoms.net_wm_moveresize_size_bottomright ||
679 (Atom)e->xclient.data.l[2] ==
680 prop_atoms.net_wm_moveresize_size_bottom ||
681 (Atom)e->xclient.data.l[2] ==
682 prop_atoms.net_wm_moveresize_size_bottomleft ||
683 (Atom)e->xclient.data.l[2] ==
684 prop_atoms.net_wm_moveresize_size_left ||
685 (Atom)e->xclient.data.l[2] ==
686 prop_atoms.net_wm_moveresize_move ||
687 (Atom)e->xclient.data.l[2] ==
688 prop_atoms.net_wm_moveresize_size_keyboard ||
689 (Atom)e->xclient.data.l[2] ==
690 prop_atoms.net_wm_moveresize_move_keyboard) {
692 g_message("client %lx x %d y %d button %d corner %d",
693 client, e->xclient.data.l[0],
694 e->xclient.data.l[1], e->xclient.data.l[3],
695 e->xclient.data.l[2]);
696 moveresize_start(client, e->xclient.data.l[0],
697 e->xclient.data.l[1], e->xclient.data.l[3],
698 e->xclient.data.l[2]);
700 } else if (msgtype == prop_atoms.net_moveresize_window) {
701 int oldg = client->gravity;
702 int tmpg, x, y, w, h;
704 if (e->xclient.data.l[0] & 0xff)
705 tmpg = e->xclient.data.l[0] & 0xff;
709 if (e->xclient.data.l[0] & 1 << 8)
710 x = e->xclient.data.l[1];
713 if (e->xclient.data.l[0] & 1 << 9)
714 y = e->xclient.data.l[2];
717 if (e->xclient.data.l[0] & 1 << 10)
718 w = e->xclient.data.l[3];
721 if (e->xclient.data.l[0] & 1 << 11)
722 h = e->xclient.data.l[4];
725 client->gravity = tmpg;
726 client_configure(client, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
727 client->gravity = oldg;
731 /* validate cuz we query stuff off the client here */
732 if (!client_validate(client)) break;
734 /* compress changes to a single property into a single change */
735 while (XCheckTypedWindowEvent(ob_display, e->type,
736 client->window, &ce)) {
737 /* XXX: it would be nice to compress ALL changes to a property,
738 not just changes in a row without other props between. */
739 if (ce.xproperty.atom != e->xproperty.atom) {
740 XPutBackEvent(ob_display, &ce);
745 msgtype = e->xproperty.atom;
746 if (msgtype == XA_WM_NORMAL_HINTS) {
747 client_update_normal_hints(client);
748 /* normal hints can make a window non-resizable */
749 client_setup_decor_and_functions(client);
751 else if (msgtype == XA_WM_HINTS)
752 client_update_wmhints(client);
753 else if (msgtype == XA_WM_TRANSIENT_FOR) {
754 client_update_transient_for(client);
755 client_get_type(client);
756 /* type may have changed, so update the layer */
757 client_calc_layer(client);
758 client_setup_decor_and_functions(client);
760 else if (msgtype == prop_atoms.net_wm_name ||
761 msgtype == prop_atoms.wm_name)
762 client_update_title(client);
763 else if (msgtype == prop_atoms.net_wm_icon_name ||
764 msgtype == prop_atoms.wm_icon_name)
765 client_update_icon_title(client);
766 else if (msgtype == prop_atoms.wm_class)
767 client_update_class(client);
768 else if (msgtype == prop_atoms.wm_protocols) {
769 client_update_protocols(client);
770 client_setup_decor_and_functions(client);
772 else if (msgtype == prop_atoms.net_wm_strut)
773 client_update_strut(client);
774 else if (msgtype == prop_atoms.net_wm_icon)
775 client_update_icons(client);
776 else if (msgtype == prop_atoms.kwm_win_icon)
777 client_update_kwm_icon(client);
781 if (extensions_shape && e->type == extensions_shape_event_basep) {
782 client->shaped = ((XShapeEvent*)e)->shaped;
783 frame_adjust_shape(client->frame);
789 static void event_handle_menu(Menu *menu, XEvent *e)
793 g_message("EVENT %d", e->type);
796 if (e->xbutton.button == 3)
800 if (!menu->shown) break;
802 /* grab_pointer_window(FALSE, None, menu->frame);*/
804 entry = menu_find_entry(menu, e->xbutton.window);
808 guint ujunk, b, w, h;
809 XGetGeometry(ob_display, e->xbutton.window,
810 &wjunk, &junk, &junk, &w, &h, &b, &ujunk);
811 if (e->xbutton.x >= (signed)-b &&
812 e->xbutton.y >= (signed)-b &&
813 e->xbutton.x < (signed)(w+b) &&
814 e->xbutton.y < (signed)(h+b)) {
815 menu_entry_fire(entry);
821 g_message("enter/leave");
822 entry = menu_find_entry(menu, e->xcrossing.window);
824 entry->hilite = e->type == EnterNotify;
825 menu_entry_render(entry);