1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 event.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
32 #include "menuframe.h"
36 #include "framerender.h"
38 #include "moveresize.h"
41 #include "extensions.h"
42 #include "translate.h"
45 #include <X11/keysym.h>
46 #include <X11/Xatom.h>
49 #ifdef HAVE_SYS_SELECT_H
50 # include <sys/select.h>
56 # include <X11/XKBlib.h>
60 #include <X11/ICE/ICElib.h>
74 static void event_process(const XEvent
*e
, gpointer data
);
75 static void event_handle_root(XEvent
*e
);
76 static void event_handle_menu_shortcut(XEvent
*e
);
77 static void event_handle_menu(XEvent
*e
);
78 static void event_handle_dock(ObDock
*s
, XEvent
*e
);
79 static void event_handle_dockapp(ObDockApp
*app
, XEvent
*e
);
80 static void event_handle_client(ObClient
*c
, XEvent
*e
);
81 static void event_handle_group(ObGroup
*g
, XEvent
*e
);
83 static void focus_delay_dest(gpointer data
);
84 static gboolean
focus_delay_cmp(gconstpointer d1
, gconstpointer d2
);
85 static gboolean
focus_delay_func(gpointer data
);
86 static void focus_delay_client_dest(ObClient
*client
, gpointer data
);
88 static gboolean
menu_hide_delay_func(gpointer data
);
90 /* The time for the current event being processed */
91 Time event_curtime
= CurrentTime
;
93 /*! The value of the mask for the NumLock modifier */
95 /*! The value of the mask for the ScrollLock modifier */
97 /*! The key codes for the modifier keys */
98 static XModifierKeymap
*modmap
;
99 /*! Table of the constant modifier masks */
100 static const gint mask_table
[] = {
101 ShiftMask
, LockMask
, ControlMask
, Mod1Mask
,
102 Mod2Mask
, Mod3Mask
, Mod4Mask
, Mod5Mask
104 static gint mask_table_size
;
106 static guint ignore_enter_focus
= 0;
108 static gboolean menu_can_hide
;
111 static void ice_handler(gint fd
, gpointer conn
)
114 IceProcessMessages(conn
, NULL
, &b
);
117 static void ice_watch(IceConn conn
, IcePointer data
, Bool opening
,
118 IcePointer
*watch_data
)
123 fd
= IceConnectionNumber(conn
);
124 ob_main_loop_fd_add(ob_main_loop
, fd
, ice_handler
, conn
, NULL
);
126 ob_main_loop_fd_remove(ob_main_loop
, fd
);
132 void event_startup(gboolean reconfig
)
134 if (reconfig
) return;
136 mask_table_size
= sizeof(mask_table
) / sizeof(mask_table
[0]);
138 /* get lock masks that are defined by the display (not constant) */
139 modmap
= XGetModifierMapping(ob_display
);
141 if (modmap
&& modmap
->max_keypermod
> 0) {
143 const size_t size
= mask_table_size
* modmap
->max_keypermod
;
144 /* get the values of the keyboard lock modifiers
145 Note: Caps lock is not retrieved the same way as Scroll and Num
146 lock since it doesn't need to be. */
147 const KeyCode num_lock
= XKeysymToKeycode(ob_display
, XK_Num_Lock
);
148 const KeyCode scroll_lock
= XKeysymToKeycode(ob_display
,
151 for (cnt
= 0; cnt
< size
; ++cnt
) {
152 if (! modmap
->modifiermap
[cnt
]) continue;
154 if (num_lock
== modmap
->modifiermap
[cnt
])
155 NumLockMask
= mask_table
[cnt
/ modmap
->max_keypermod
];
156 if (scroll_lock
== modmap
->modifiermap
[cnt
])
157 ScrollLockMask
= mask_table
[cnt
/ modmap
->max_keypermod
];
161 ob_main_loop_x_add(ob_main_loop
, event_process
, NULL
, NULL
);
164 IceAddConnectionWatch(ice_watch
, NULL
);
167 client_add_destructor(focus_delay_client_dest
, NULL
);
170 void event_shutdown(gboolean reconfig
)
172 if (reconfig
) return;
175 IceRemoveConnectionWatch(ice_watch
, NULL
);
178 client_remove_destructor(focus_delay_client_dest
);
179 XFreeModifiermap(modmap
);
182 static Window
event_get_window(XEvent
*e
)
189 window
= RootWindow(ob_display
, ob_screen
);
192 window
= e
->xmap
.window
;
195 window
= e
->xunmap
.window
;
198 window
= e
->xdestroywindow
.window
;
200 case ConfigureRequest
:
201 window
= e
->xconfigurerequest
.window
;
203 case ConfigureNotify
:
204 window
= e
->xconfigure
.window
;
208 if (extensions_xkb
&& e
->type
== extensions_xkb_event_basep
) {
209 switch (((XkbAnyEvent
*)e
)->xkb_type
) {
211 window
= ((XkbBellNotifyEvent
*)e
)->window
;
218 if (extensions_sync
&&
219 e
->type
== extensions_sync_event_basep
+ XSyncAlarmNotify
)
224 window
= e
->xany
.window
;
229 static void event_set_curtime(XEvent
*e
)
231 Time t
= CurrentTime
;
233 /* grab the lasttime and hack up the state */
249 t
= e
->xproperty
.time
;
253 t
= e
->xcrossing
.time
;
257 if (extensions_sync
&&
258 e
->type
== extensions_sync_event_basep
+ XSyncAlarmNotify
)
260 t
= ((XSyncAlarmNotifyEvent
*)e
)->time
;
263 /* if more event types are anticipated, get their timestamp
271 #define STRIP_MODS(s) \
272 s &= ~(LockMask | NumLockMask | ScrollLockMask), \
273 /* kill off the Button1Mask etc, only want the modifiers */ \
274 s &= (ControlMask | ShiftMask | Mod1Mask | \
275 Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask) \
277 static void event_hack_mods(XEvent *e)
280 XkbStateRec xkb_state
;
288 STRIP_MODS(e
->xbutton
.state
);
291 STRIP_MODS(e
->xkey
.state
);
294 STRIP_MODS(e
->xkey
.state
);
295 /* remove from the state the mask of the modifier being released, if
296 it is a modifier key being released (this is a little ugly..) */
298 if (XkbGetState(ob_display
, XkbUseCoreKbd
, &xkb_state
) == Success
) {
299 e
->xkey
.state
= xkb_state
.compat_state
;
303 kp
= modmap
->modifiermap
;
304 for (i
= 0; i
< mask_table_size
; ++i
) {
305 for (k
= 0; k
< modmap
->max_keypermod
; ++k
) {
306 if (*kp
== e
->xkey
.keycode
) { /* found the keycode */
307 /* remove the mask for it */
308 e
->xkey
.state
&= ~mask_table
[i
];
309 /* cause the first loop to break; */
311 break; /* get outta here! */
318 STRIP_MODS(e
->xmotion
.state
);
319 /* compress events */
322 while (XCheckTypedWindowEvent(ob_display
, e
->xmotion
.window
,
324 e
->xmotion
.x_root
= ce
.xmotion
.x_root
;
325 e
->xmotion
.y_root
= ce
.xmotion
.y_root
;
332 static gboolean
wanted_focusevent(XEvent
*e
)
334 gint mode
= e
->xfocus
.mode
;
335 gint detail
= e
->xfocus
.detail
;
336 Window win
= e
->xany
.window
;
338 if (e
->type
== FocusIn
) {
340 /* These are ones we never want.. */
342 /* This means focus was given by a keyboard/mouse grab. */
343 if (mode
== NotifyGrab
)
345 /* This means focus was given back from a keyboard/mouse grab. */
346 if (mode
== NotifyUngrab
)
349 /* These are the ones we want.. */
351 if (win
== RootWindow(ob_display
, ob_screen
)) {
352 /* This means focus reverted off of a client */
353 if (detail
== NotifyPointerRoot
|| detail
== NotifyDetailNone
||
354 detail
== NotifyInferior
)
360 /* This means focus moved from the root window to a client */
361 if (detail
== NotifyVirtual
)
363 /* This means focus moved from one client to another */
364 if (detail
== NotifyNonlinearVirtual
)
370 g_assert(e
->type
== FocusOut
);
373 /* These are ones we never want.. */
375 /* This means focus was taken by a keyboard/mouse grab. */
376 if (mode
== NotifyGrab
)
379 /* Focus left the root window revertedto state */
380 if (win
== RootWindow(ob_display
, ob_screen
))
383 /* These are the ones we want.. */
385 /* This means focus moved from a client to the root window */
386 if (detail
== NotifyVirtual
)
388 /* This means focus moved from one client to another */
389 if (detail
== NotifyNonlinearVirtual
)
391 /* This means focus had moved to our frame window and now moved off */
392 if (detail
== NotifyNonlinear
)
400 static Bool
look_for_focusin(Display
*d
, XEvent
*e
, XPointer arg
)
402 return e
->type
== FocusIn
&& wanted_focusevent(e
);
405 static gboolean
event_ignore(XEvent
*e
, ObClient
*client
)
409 if (!wanted_focusevent(e
))
413 if (!wanted_focusevent(e
))
420 static void event_process(const XEvent
*ec
, gpointer data
)
423 ObGroup
*group
= NULL
;
424 ObClient
*client
= NULL
;
426 ObDockApp
*dockapp
= NULL
;
427 ObWindow
*obwin
= NULL
;
429 ObEventData
*ed
= data
;
431 /* make a copy we can mangle */
435 window
= event_get_window(e
);
436 if (!(e
->type
== PropertyNotify
&&
437 (group
= g_hash_table_lookup(group_map
, &window
))))
438 if ((obwin
= g_hash_table_lookup(window_map
, &window
))) {
439 switch (obwin
->type
) {
441 dock
= WINDOW_AS_DOCK(obwin
);
444 dockapp
= WINDOW_AS_DOCKAPP(obwin
);
447 client
= WINDOW_AS_CLIENT(obwin
);
450 case Window_Internal
:
451 /* not to be used for events */
452 g_assert_not_reached();
457 event_set_curtime(e
);
459 if (event_ignore(e
, client
)) {
466 /* deal with it in the kernel */
468 if (menu_frame_visible
&&
469 (e
->type
== EnterNotify
|| e
->type
== LeaveNotify
))
471 /* crossing events for menu */
472 event_handle_menu(e
);
473 } else if (e
->type
== FocusIn
) {
474 if (client
&& client
!= focus_client
) {
475 frame_adjust_focus(client
->frame
, TRUE
);
476 focus_set_client(client
);
477 client_calc_layer(client
);
479 } else if (e
->type
== FocusOut
) {
480 gboolean nomove
= FALSE
;
483 ob_debug_type(OB_DEBUG_FOCUS
, "FocusOut Event\n");
485 /* Look for the followup FocusIn */
486 if (!XCheckIfEvent(ob_display
, &ce
, look_for_focusin
, NULL
)) {
487 /* There is no FocusIn, this means focus went to a window that
488 is not being managed, or a window on another screen. */
489 ob_debug_type(OB_DEBUG_FOCUS
, "Focus went to a black hole !\n");
490 /* nothing is focused */
491 focus_set_client(NULL
);
492 } else if (ce
.xany
.window
== e
->xany
.window
) {
493 ob_debug_type(OB_DEBUG_FOCUS
, "Focus didn't go anywhere\n");
494 /* If focus didn't actually move anywhere, there is nothing to do*/
496 } else if (ce
.xfocus
.detail
== NotifyPointerRoot
||
497 ce
.xfocus
.detail
== NotifyDetailNone
||
498 ce
.xfocus
.detail
== NotifyInferior
) {
499 ob_debug_type(OB_DEBUG_FOCUS
, "Focus went to root\n");
500 /* Focus has been reverted to the root window or nothing
501 FocusOut events come after UnmapNotify, so we don't need to
502 worry about focusing an invalid window
504 focus_fallback(TRUE
);
506 /* Focus did move, so process the FocusIn event */
507 ObEventData ed
= { .ignored
= FALSE
};
508 event_process(&ce
, &ed
);
510 /* The FocusIn was ignored, this means it was on a window
511 that isn't a client. */
512 ob_debug_type(OB_DEBUG_FOCUS
,
513 "Focus went to an unmanaged window 0x%x !\n",
515 focus_fallback(TRUE
);
519 if (client
&& !nomove
) {
520 frame_adjust_focus(client
->frame
, FALSE
);
521 /* focus_set_client has already been called for sure */
522 client_calc_layer(client
);
525 event_handle_group(group
, e
);
527 event_handle_client(client
, e
);
529 event_handle_dockapp(dockapp
, e
);
531 event_handle_dock(dock
, e
);
532 else if (window
== RootWindow(ob_display
, ob_screen
))
533 event_handle_root(e
);
534 else if (e
->type
== MapRequest
)
535 client_manage(window
);
536 else if (e
->type
== ConfigureRequest
) {
537 /* unhandled configure requests must be used to configure the
541 xwc
.x
= e
->xconfigurerequest
.x
;
542 xwc
.y
= e
->xconfigurerequest
.y
;
543 xwc
.width
= e
->xconfigurerequest
.width
;
544 xwc
.height
= e
->xconfigurerequest
.height
;
545 xwc
.border_width
= e
->xconfigurerequest
.border_width
;
546 xwc
.sibling
= e
->xconfigurerequest
.above
;
547 xwc
.stack_mode
= e
->xconfigurerequest
.detail
;
549 /* we are not to be held responsible if someone sends us an
551 xerror_set_ignore(TRUE
);
552 XConfigureWindow(ob_display
, window
,
553 e
->xconfigurerequest
.value_mask
, &xwc
);
554 xerror_set_ignore(FALSE
);
557 else if (extensions_sync
&&
558 e
->type
== extensions_sync_event_basep
+ XSyncAlarmNotify
)
560 XSyncAlarmNotifyEvent
*se
= (XSyncAlarmNotifyEvent
*)e
;
561 if (se
->alarm
== moveresize_alarm
&& moveresize_in_progress
)
566 /* user input (action-bound) events */
567 if (e
->type
== ButtonPress
|| e
->type
== ButtonRelease
||
568 e
->type
== MotionNotify
|| e
->type
== KeyPress
||
569 e
->type
== KeyRelease
)
571 if (menu_frame_visible
)
572 event_handle_menu(e
);
574 if (!keyboard_process_interactive_grab(e
, &client
)) {
575 if (moveresize_in_progress
) {
578 /* make further actions work on the client being
580 client
= moveresize_client
;
583 menu_can_hide
= FALSE
;
584 ob_main_loop_timeout_add(ob_main_loop
,
585 config_menu_hide_delay
* 1000,
586 menu_hide_delay_func
,
587 NULL
, g_direct_equal
, NULL
);
589 if (e
->type
== ButtonPress
|| e
->type
== ButtonRelease
||
590 e
->type
== MotionNotify
) {
591 mouse_event(client
, e
);
592 } else if (e
->type
== KeyPress
) {
593 keyboard_event((focus_cycle_target
? focus_cycle_target
:
594 (client
? client
: focus_client
)), e
);
599 /* if something happens and it's not from an XEvent, then we don't know
601 event_curtime
= CurrentTime
;
604 static void event_handle_root(XEvent
*e
)
610 ob_debug("Another WM has requested to replace us. Exiting.\n");
615 if (e
->xclient
.format
!= 32) break;
617 msgtype
= e
->xclient
.message_type
;
618 if (msgtype
== prop_atoms
.net_current_desktop
) {
619 guint d
= e
->xclient
.data
.l
[0];
620 if (d
< screen_num_desktops
) {
621 event_curtime
= e
->xclient
.data
.l
[1];
622 ob_debug("SWITCH DESKTOP TIME: %d\n", event_curtime
);
623 screen_set_desktop(d
);
625 } else if (msgtype
== prop_atoms
.net_number_of_desktops
) {
626 guint d
= e
->xclient
.data
.l
[0];
628 screen_set_num_desktops(d
);
629 } else if (msgtype
== prop_atoms
.net_showing_desktop
) {
630 screen_show_desktop(e
->xclient
.data
.l
[0] != 0);
631 } else if (msgtype
== prop_atoms
.ob_control
) {
632 if (e
->xclient
.data
.l
[0] == 1)
634 else if (e
->xclient
.data
.l
[0] == 2)
639 if (e
->xproperty
.atom
== prop_atoms
.net_desktop_names
)
640 screen_update_desktop_names();
641 else if (e
->xproperty
.atom
== prop_atoms
.net_desktop_layout
)
642 screen_update_layout();
644 case ConfigureNotify
:
646 XRRUpdateConfiguration(e
);
655 static void event_handle_group(ObGroup
*group
, XEvent
*e
)
659 g_assert(e
->type
== PropertyNotify
);
661 for (it
= group
->members
; it
; it
= g_slist_next(it
))
662 event_handle_client(it
->data
, e
);
665 void event_enter_client(ObClient
*client
)
667 g_assert(config_focus_follow
);
669 if (client_normal(client
) && client_can_focus(client
)) {
670 if (config_focus_delay
) {
671 ObFocusDelayData
*data
;
673 ob_main_loop_timeout_remove(ob_main_loop
, focus_delay_func
);
675 data
= g_new(ObFocusDelayData
, 1);
676 data
->client
= client
;
677 data
->time
= event_curtime
;
679 ob_main_loop_timeout_add(ob_main_loop
,
682 data
, focus_delay_cmp
, focus_delay_dest
);
684 ObFocusDelayData data
;
685 data
.client
= client
;
686 data
.time
= event_curtime
;
687 focus_delay_func(&data
);
692 static void event_handle_client(ObClient
*client
, XEvent
*e
)
702 /* Wheel buttons don't draw because they are an instant click, so it
703 is a waste of resources to go drawing it. */
704 if (!(e
->xbutton
.button
== 4 || e
->xbutton
.button
== 5)) {
705 con
= frame_context(client
, e
->xbutton
.window
);
706 con
= mouse_button_frame_context(con
, e
->xbutton
.button
);
708 case OB_FRAME_CONTEXT_MAXIMIZE
:
709 client
->frame
->max_press
= (e
->type
== ButtonPress
);
710 framerender_frame(client
->frame
);
712 case OB_FRAME_CONTEXT_CLOSE
:
713 client
->frame
->close_press
= (e
->type
== ButtonPress
);
714 framerender_frame(client
->frame
);
716 case OB_FRAME_CONTEXT_ICONIFY
:
717 client
->frame
->iconify_press
= (e
->type
== ButtonPress
);
718 framerender_frame(client
->frame
);
720 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
721 client
->frame
->desk_press
= (e
->type
== ButtonPress
);
722 framerender_frame(client
->frame
);
724 case OB_FRAME_CONTEXT_SHADE
:
725 client
->frame
->shade_press
= (e
->type
== ButtonPress
);
726 framerender_frame(client
->frame
);
729 /* nothing changes with clicks for any other contexts */
735 con
= frame_context(client
, e
->xcrossing
.window
);
737 case OB_FRAME_CONTEXT_MAXIMIZE
:
738 client
->frame
->max_hover
= FALSE
;
739 frame_adjust_state(client
->frame
);
741 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
742 client
->frame
->desk_hover
= FALSE
;
743 frame_adjust_state(client
->frame
);
745 case OB_FRAME_CONTEXT_SHADE
:
746 client
->frame
->shade_hover
= FALSE
;
747 frame_adjust_state(client
->frame
);
749 case OB_FRAME_CONTEXT_ICONIFY
:
750 client
->frame
->iconify_hover
= FALSE
;
751 frame_adjust_state(client
->frame
);
753 case OB_FRAME_CONTEXT_CLOSE
:
754 client
->frame
->close_hover
= FALSE
;
755 frame_adjust_state(client
->frame
);
757 case OB_FRAME_CONTEXT_FRAME
:
758 ob_debug_type(OB_DEBUG_FOCUS
,
759 "%sNotify mode %d detail %d on %lx\n",
760 (e
->type
== EnterNotify
? "Enter" : "Leave"),
762 e
->xcrossing
.detail
, (client
?client
->window
:0));
763 if (keyboard_interactively_grabbed())
765 if (config_focus_follow
&& config_focus_delay
&&
766 /* leave inferior events can happen when the mouse goes onto
767 the window's border and then into the window before the
769 e
->xcrossing
.detail
!= NotifyInferior
)
771 ob_main_loop_timeout_remove_data(ob_main_loop
,
782 gboolean nofocus
= FALSE
;
784 if (ignore_enter_focus
) {
785 ignore_enter_focus
--;
789 con
= frame_context(client
, e
->xcrossing
.window
);
791 case OB_FRAME_CONTEXT_MAXIMIZE
:
792 client
->frame
->max_hover
= TRUE
;
793 frame_adjust_state(client
->frame
);
795 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
796 client
->frame
->desk_hover
= TRUE
;
797 frame_adjust_state(client
->frame
);
799 case OB_FRAME_CONTEXT_SHADE
:
800 client
->frame
->shade_hover
= TRUE
;
801 frame_adjust_state(client
->frame
);
803 case OB_FRAME_CONTEXT_ICONIFY
:
804 client
->frame
->iconify_hover
= TRUE
;
805 frame_adjust_state(client
->frame
);
807 case OB_FRAME_CONTEXT_CLOSE
:
808 client
->frame
->close_hover
= TRUE
;
809 frame_adjust_state(client
->frame
);
811 case OB_FRAME_CONTEXT_FRAME
:
812 if (keyboard_interactively_grabbed())
814 if (e
->xcrossing
.mode
== NotifyGrab
||
815 e
->xcrossing
.mode
== NotifyUngrab
||
816 /*ignore enters when we're already in the window */
817 e
->xcrossing
.detail
== NotifyInferior
)
819 ob_debug_type(OB_DEBUG_FOCUS
,
820 "%sNotify mode %d detail %d on %lx IGNORED\n",
821 (e
->type
== EnterNotify
? "Enter" : "Leave"),
823 e
->xcrossing
.detail
, client
?client
->window
:0);
825 ob_debug_type(OB_DEBUG_FOCUS
,
826 "%sNotify mode %d detail %d on %lx, "
827 "focusing window: %d\n",
828 (e
->type
== EnterNotify
? "Enter" : "Leave"),
830 e
->xcrossing
.detail
, (client
?client
->window
:0),
832 if (!nofocus
&& config_focus_follow
)
833 event_enter_client(client
);
841 case ConfigureRequest
:
843 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
844 ConfigureRequest
, &ce
)) {
846 /* XXX if this causes bad things.. we can compress config req's
847 with the same mask. */
848 e
->xconfigurerequest
.value_mask
|=
849 ce
.xconfigurerequest
.value_mask
;
850 if (ce
.xconfigurerequest
.value_mask
& CWX
)
851 e
->xconfigurerequest
.x
= ce
.xconfigurerequest
.x
;
852 if (ce
.xconfigurerequest
.value_mask
& CWY
)
853 e
->xconfigurerequest
.y
= ce
.xconfigurerequest
.y
;
854 if (ce
.xconfigurerequest
.value_mask
& CWWidth
)
855 e
->xconfigurerequest
.width
= ce
.xconfigurerequest
.width
;
856 if (ce
.xconfigurerequest
.value_mask
& CWHeight
)
857 e
->xconfigurerequest
.height
= ce
.xconfigurerequest
.height
;
858 if (ce
.xconfigurerequest
.value_mask
& CWBorderWidth
)
859 e
->xconfigurerequest
.border_width
=
860 ce
.xconfigurerequest
.border_width
;
861 if (ce
.xconfigurerequest
.value_mask
& CWStackMode
)
862 e
->xconfigurerequest
.detail
= ce
.xconfigurerequest
.detail
;
865 /* if we are iconic (or shaded (fvwm does this)) ignore the event */
866 if (client
->iconic
|| client
->shaded
) return;
868 /* resize, then move, as specified in the EWMH section 7.7 */
869 if (e
->xconfigurerequest
.value_mask
& (CWWidth
| CWHeight
|
875 if (e
->xconfigurerequest
.value_mask
& CWBorderWidth
)
876 client
->border_width
= e
->xconfigurerequest
.border_width
;
878 x
= (e
->xconfigurerequest
.value_mask
& CWX
) ?
879 e
->xconfigurerequest
.x
: client
->area
.x
;
880 y
= (e
->xconfigurerequest
.value_mask
& CWY
) ?
881 e
->xconfigurerequest
.y
: client
->area
.y
;
882 w
= (e
->xconfigurerequest
.value_mask
& CWWidth
) ?
883 e
->xconfigurerequest
.width
: client
->area
.width
;
884 h
= (e
->xconfigurerequest
.value_mask
& CWHeight
) ?
885 e
->xconfigurerequest
.height
: client
->area
.height
;
891 client
->frame
->size
.left
+ client
->frame
->size
.right
;
893 client
->frame
->size
.top
+ client
->frame
->size
.bottom
;
894 /* make this rude for size-only changes but not for position
896 gboolean moving
= ((e
->xconfigurerequest
.value_mask
& CWX
) ||
897 (e
->xconfigurerequest
.value_mask
& CWY
));
899 client_find_onscreen(client
, &newx
, &newy
, fw
, fh
,
901 if (e
->xconfigurerequest
.value_mask
& CWX
)
903 if (e
->xconfigurerequest
.value_mask
& CWY
)
907 switch (client
->gravity
) {
908 case NorthEastGravity
:
910 corner
= OB_CORNER_TOPRIGHT
;
912 case SouthWestGravity
:
914 corner
= OB_CORNER_BOTTOMLEFT
;
916 case SouthEastGravity
:
917 corner
= OB_CORNER_BOTTOMRIGHT
;
919 default: /* NorthWest, Static, etc */
920 corner
= OB_CORNER_TOPLEFT
;
923 client_configure_full(client
, corner
, x
, y
, w
, h
, FALSE
, TRUE
,
927 if (e
->xconfigurerequest
.value_mask
& CWStackMode
) {
928 switch (e
->xconfigurerequest
.detail
) {
931 /* Apps are so rude. And this is totally disconnected from
932 activation/focus. Bleh. */
933 /*client_lower(client);*/
939 /* Apps are so rude. And this is totally disconnected from
940 activation/focus. Bleh. */
941 /*client_raise(client);*/
947 if (client
->ignore_unmaps
) {
948 client
->ignore_unmaps
--;
951 ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
953 client
->window
, e
->xunmap
.event
, e
->xunmap
.from_configure
,
954 client
->ignore_unmaps
);
955 client_unmanage(client
);
958 ob_debug("DestroyNotify for window 0x%x\n", client
->window
);
959 client_unmanage(client
);
962 /* this is when the client is first taken captive in the frame */
963 if (e
->xreparent
.parent
== client
->frame
->plate
) break;
966 This event is quite rare and is usually handled in unmapHandler.
967 However, if the window is unmapped when the reparent event occurs,
968 the window manager never sees it because an unmap event is not sent
969 to an already unmapped window.
972 /* we don't want the reparent event, put it back on the stack for the
973 X server to deal with after we unmanage the window */
974 XPutBackEvent(ob_display
, e
);
976 ob_debug("ReparentNotify for window 0x%x\n", client
->window
);
977 client_unmanage(client
);
980 ob_debug("MapRequest for 0x%lx\n", client
->window
);
981 if (!client
->iconic
) break; /* this normally doesn't happen, but if it
982 does, we don't want it!
983 it can happen now when the window is on
984 another desktop, but we still don't
986 client_activate(client
, FALSE
, TRUE
);
989 /* validate cuz we query stuff off the client here */
990 if (!client_validate(client
)) break;
992 if (e
->xclient
.format
!= 32) return;
994 msgtype
= e
->xclient
.message_type
;
995 if (msgtype
== prop_atoms
.wm_change_state
) {
996 /* compress changes into a single change */
997 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
999 /* XXX: it would be nice to compress ALL messages of a
1000 type, not just messages in a row without other
1001 message types between. */
1002 if (ce
.xclient
.message_type
!= msgtype
) {
1003 XPutBackEvent(ob_display
, &ce
);
1006 e
->xclient
= ce
.xclient
;
1008 client_set_wm_state(client
, e
->xclient
.data
.l
[0]);
1009 } else if (msgtype
== prop_atoms
.net_wm_desktop
) {
1010 /* compress changes into a single change */
1011 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
1013 /* XXX: it would be nice to compress ALL messages of a
1014 type, not just messages in a row without other
1015 message types between. */
1016 if (ce
.xclient
.message_type
!= msgtype
) {
1017 XPutBackEvent(ob_display
, &ce
);
1020 e
->xclient
= ce
.xclient
;
1022 if ((unsigned)e
->xclient
.data
.l
[0] < screen_num_desktops
||
1023 (unsigned)e
->xclient
.data
.l
[0] == DESKTOP_ALL
)
1024 client_set_desktop(client
, (unsigned)e
->xclient
.data
.l
[0],
1026 } else if (msgtype
== prop_atoms
.net_wm_state
) {
1027 /* can't compress these */
1028 ob_debug("net_wm_state %s %ld %ld for 0x%lx\n",
1029 (e
->xclient
.data
.l
[0] == 0 ? "Remove" :
1030 e
->xclient
.data
.l
[0] == 1 ? "Add" :
1031 e
->xclient
.data
.l
[0] == 2 ? "Toggle" : "INVALID"),
1032 e
->xclient
.data
.l
[1], e
->xclient
.data
.l
[2],
1034 client_set_state(client
, e
->xclient
.data
.l
[0],
1035 e
->xclient
.data
.l
[1], e
->xclient
.data
.l
[2]);
1036 } else if (msgtype
== prop_atoms
.net_close_window
) {
1037 ob_debug("net_close_window for 0x%lx\n", client
->window
);
1038 client_close(client
);
1039 } else if (msgtype
== prop_atoms
.net_active_window
) {
1040 ob_debug("net_active_window for 0x%lx source=%s\n",
1042 (e
->xclient
.data
.l
[0] == 0 ? "unknown" :
1043 (e
->xclient
.data
.l
[0] == 1 ? "application" :
1044 (e
->xclient
.data
.l
[0] == 2 ? "user" : "INVALID"))));
1045 /* XXX make use of data.l[2] ! */
1046 event_curtime
= e
->xclient
.data
.l
[1];
1047 client_activate(client
, FALSE
,
1048 (e
->xclient
.data
.l
[0] == 0 ||
1049 e
->xclient
.data
.l
[0] == 2));
1050 } else if (msgtype
== prop_atoms
.net_wm_moveresize
) {
1051 ob_debug("net_wm_moveresize for 0x%lx direction %d\n",
1052 client
->window
, e
->xclient
.data
.l
[2]);
1053 if ((Atom
)e
->xclient
.data
.l
[2] ==
1054 prop_atoms
.net_wm_moveresize_size_topleft
||
1055 (Atom
)e
->xclient
.data
.l
[2] ==
1056 prop_atoms
.net_wm_moveresize_size_top
||
1057 (Atom
)e
->xclient
.data
.l
[2] ==
1058 prop_atoms
.net_wm_moveresize_size_topright
||
1059 (Atom
)e
->xclient
.data
.l
[2] ==
1060 prop_atoms
.net_wm_moveresize_size_right
||
1061 (Atom
)e
->xclient
.data
.l
[2] ==
1062 prop_atoms
.net_wm_moveresize_size_right
||
1063 (Atom
)e
->xclient
.data
.l
[2] ==
1064 prop_atoms
.net_wm_moveresize_size_bottomright
||
1065 (Atom
)e
->xclient
.data
.l
[2] ==
1066 prop_atoms
.net_wm_moveresize_size_bottom
||
1067 (Atom
)e
->xclient
.data
.l
[2] ==
1068 prop_atoms
.net_wm_moveresize_size_bottomleft
||
1069 (Atom
)e
->xclient
.data
.l
[2] ==
1070 prop_atoms
.net_wm_moveresize_size_left
||
1071 (Atom
)e
->xclient
.data
.l
[2] ==
1072 prop_atoms
.net_wm_moveresize_move
||
1073 (Atom
)e
->xclient
.data
.l
[2] ==
1074 prop_atoms
.net_wm_moveresize_size_keyboard
||
1075 (Atom
)e
->xclient
.data
.l
[2] ==
1076 prop_atoms
.net_wm_moveresize_move_keyboard
) {
1078 moveresize_start(client
, e
->xclient
.data
.l
[0],
1079 e
->xclient
.data
.l
[1], e
->xclient
.data
.l
[3],
1080 e
->xclient
.data
.l
[2]);
1082 else if ((Atom
)e
->xclient
.data
.l
[2] ==
1083 prop_atoms
.net_wm_moveresize_cancel
)
1084 moveresize_end(TRUE
);
1085 } else if (msgtype
== prop_atoms
.net_moveresize_window
) {
1086 gint oldg
= client
->gravity
;
1087 gint tmpg
, x
, y
, w
, h
;
1089 if (e
->xclient
.data
.l
[0] & 0xff)
1090 tmpg
= e
->xclient
.data
.l
[0] & 0xff;
1094 if (e
->xclient
.data
.l
[0] & 1 << 8)
1095 x
= e
->xclient
.data
.l
[1];
1098 if (e
->xclient
.data
.l
[0] & 1 << 9)
1099 y
= e
->xclient
.data
.l
[2];
1102 if (e
->xclient
.data
.l
[0] & 1 << 10)
1103 w
= e
->xclient
.data
.l
[3];
1105 w
= client
->area
.width
;
1106 if (e
->xclient
.data
.l
[0] & 1 << 11)
1107 h
= e
->xclient
.data
.l
[4];
1109 h
= client
->area
.height
;
1110 client
->gravity
= tmpg
;
1116 client
->frame
->size
.left
+ client
->frame
->size
.right
;
1118 client
->frame
->size
.top
+ client
->frame
->size
.bottom
;
1119 client_find_onscreen(client
, &newx
, &newy
, fw
, fh
,
1120 client_normal(client
));
1121 if (e
->xclient
.data
.l
[0] & 1 << 8)
1123 if (e
->xclient
.data
.l
[0] & 1 << 9)
1127 client_configure(client
, OB_CORNER_TOPLEFT
,
1128 x
, y
, w
, h
, FALSE
, TRUE
);
1130 client
->gravity
= oldg
;
1133 case PropertyNotify
:
1134 /* validate cuz we query stuff off the client here */
1135 if (!client_validate(client
)) break;
1137 /* compress changes to a single property into a single change */
1138 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
1142 /* XXX: it would be nice to compress ALL changes to a property,
1143 not just changes in a row without other props between. */
1145 a
= ce
.xproperty
.atom
;
1146 b
= e
->xproperty
.atom
;
1150 if ((a
== prop_atoms
.net_wm_name
||
1151 a
== prop_atoms
.wm_name
||
1152 a
== prop_atoms
.net_wm_icon_name
||
1153 a
== prop_atoms
.wm_icon_name
)
1155 (b
== prop_atoms
.net_wm_name
||
1156 b
== prop_atoms
.wm_name
||
1157 b
== prop_atoms
.net_wm_icon_name
||
1158 b
== prop_atoms
.wm_icon_name
)) {
1161 if (a
== prop_atoms
.net_wm_icon
&&
1162 b
== prop_atoms
.net_wm_icon
)
1165 XPutBackEvent(ob_display
, &ce
);
1169 msgtype
= e
->xproperty
.atom
;
1170 if (msgtype
== XA_WM_NORMAL_HINTS
) {
1171 client_update_normal_hints(client
);
1172 /* normal hints can make a window non-resizable */
1173 client_setup_decor_and_functions(client
);
1174 } else if (msgtype
== XA_WM_HINTS
) {
1175 client_update_wmhints(client
);
1176 } else if (msgtype
== XA_WM_TRANSIENT_FOR
) {
1177 client_update_transient_for(client
);
1178 client_get_type(client
);
1179 /* type may have changed, so update the layer */
1180 client_calc_layer(client
);
1181 client_setup_decor_and_functions(client
);
1182 } else if (msgtype
== prop_atoms
.net_wm_name
||
1183 msgtype
== prop_atoms
.wm_name
||
1184 msgtype
== prop_atoms
.net_wm_icon_name
||
1185 msgtype
== prop_atoms
.wm_icon_name
) {
1186 client_update_title(client
);
1187 } else if (msgtype
== prop_atoms
.wm_class
) {
1188 client_update_class(client
);
1189 } else if (msgtype
== prop_atoms
.wm_protocols
) {
1190 client_update_protocols(client
);
1191 client_setup_decor_and_functions(client
);
1193 else if (msgtype
== prop_atoms
.net_wm_strut
) {
1194 client_update_strut(client
);
1196 else if (msgtype
== prop_atoms
.net_wm_icon
) {
1197 client_update_icons(client
);
1199 else if (msgtype
== prop_atoms
.net_wm_user_time
) {
1200 client_update_user_time(client
);
1203 else if (msgtype
== prop_atoms
.net_wm_sync_request_counter
) {
1204 client_update_sync_request_counter(client
);
1207 else if (msgtype
== prop_atoms
.sm_client_id
) {
1208 client_update_sm_client_id(client
);
1210 case ColormapNotify
:
1211 client_update_colormap(client
, e
->xcolormap
.colormap
);
1216 if (extensions_shape
&& e
->type
== extensions_shape_event_basep
) {
1217 client
->shaped
= ((XShapeEvent
*)e
)->shaped
;
1218 frame_adjust_shape(client
->frame
);
1224 static void event_handle_dock(ObDock
*s
, XEvent
*e
)
1228 if (e
->xbutton
.button
== 1)
1229 stacking_raise(DOCK_AS_WINDOW(s
));
1230 else if (e
->xbutton
.button
== 2)
1231 stacking_lower(DOCK_AS_WINDOW(s
));
1242 static void event_handle_dockapp(ObDockApp
*app
, XEvent
*e
)
1246 dock_app_drag(app
, &e
->xmotion
);
1249 if (app
->ignore_unmaps
) {
1250 app
->ignore_unmaps
--;
1253 dock_remove(app
, TRUE
);
1256 dock_remove(app
, FALSE
);
1258 case ReparentNotify
:
1259 dock_remove(app
, FALSE
);
1261 case ConfigureNotify
:
1262 dock_app_configure(app
, e
->xconfigure
.width
, e
->xconfigure
.height
);
1267 static ObMenuFrame
* find_active_menu()
1270 ObMenuFrame
*ret
= NULL
;
1272 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
1281 static ObMenuFrame
* find_active_or_last_menu()
1283 ObMenuFrame
*ret
= NULL
;
1285 ret
= find_active_menu();
1286 if (!ret
&& menu_frame_visible
)
1287 ret
= menu_frame_visible
->data
;
1291 static void event_handle_menu_shortcut(XEvent
*ev
)
1293 gunichar unikey
= 0;
1297 ObMenuEntryFrame
*found
= NULL
;
1298 guint num_found
= 0;
1302 if ((key
= translate_keycode(ev
->xkey
.keycode
)) == NULL
)
1304 unikey
= g_utf8_get_char_validated(key
, -1);
1305 if (unikey
== (gunichar
)-1 || unikey
== (gunichar
)-2 || unikey
== 0)
1309 if ((frame
= find_active_or_last_menu()) == NULL
)
1313 if (!frame
->entries
)
1314 return; /* nothing in the menu anyways */
1316 /* start after the selected one */
1317 start
= frame
->entries
;
1318 if (frame
->selected
) {
1319 for (it
= start
; frame
->selected
!= it
->data
; it
= g_list_next(it
))
1320 g_assert(it
!= NULL
); /* nothing was selected? */
1321 /* next with wraparound */
1322 start
= g_list_next(it
);
1323 if (start
== NULL
) start
= frame
->entries
;
1328 ObMenuEntryFrame
*e
= it
->data
;
1329 gunichar entrykey
= 0;
1331 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1332 entrykey
= e
->entry
->data
.normal
.shortcut
;
1333 else if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1334 entrykey
= e
->entry
->data
.submenu
.submenu
->shortcut
;
1336 if (unikey
== entrykey
) {
1337 if (found
== NULL
) found
= e
;
1341 /* next with wraparound */
1342 it
= g_list_next(it
);
1343 if (it
== NULL
) it
= frame
->entries
;
1344 } while (it
!= start
);
1347 if (found
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1350 menu_frame_select(frame
, found
, TRUE
);
1352 menu_entry_frame_execute(found
, ev
->xkey
.state
,
1355 menu_frame_select(frame
, found
, TRUE
);
1357 menu_frame_select_next(frame
->child
);
1362 static void event_handle_menu(XEvent
*ev
)
1365 ObMenuEntryFrame
*e
;
1369 if (menu_can_hide
) {
1370 if ((e
= menu_entry_frame_under(ev
->xbutton
.x_root
,
1371 ev
->xbutton
.y_root
)))
1372 menu_entry_frame_execute(e
, ev
->xbutton
.state
,
1375 menu_frame_hide_all();
1379 if ((e
= g_hash_table_lookup(menu_frame_map
, &ev
->xcrossing
.window
))) {
1380 if (e
->ignore_enters
)
1383 menu_frame_select(e
->frame
, e
, FALSE
);
1387 if ((e
= g_hash_table_lookup(menu_frame_map
, &ev
->xcrossing
.window
)) &&
1388 (f
= find_active_menu()) && f
->selected
== e
&&
1389 e
->entry
->type
!= OB_MENU_ENTRY_TYPE_SUBMENU
)
1391 menu_frame_select(e
->frame
, NULL
, FALSE
);
1394 if ((e
= menu_entry_frame_under(ev
->xmotion
.x_root
,
1395 ev
->xmotion
.y_root
)))
1396 menu_frame_select(e
->frame
, e
, FALSE
);
1399 if (ev
->xkey
.keycode
== ob_keycode(OB_KEY_ESCAPE
))
1400 if ((f
= find_active_or_last_menu()) && f
->parent
)
1401 menu_frame_select(f
, NULL
, TRUE
);
1403 menu_frame_hide_all();
1404 else if (ev
->xkey
.keycode
== ob_keycode(OB_KEY_RETURN
)) {
1406 if ((f
= find_active_menu())) {
1408 menu_frame_select_next(f
->child
);
1410 menu_entry_frame_execute(f
->selected
, ev
->xkey
.state
,
1413 } else if (ev
->xkey
.keycode
== ob_keycode(OB_KEY_LEFT
)) {
1415 if ((f
= find_active_or_last_menu()))
1416 menu_frame_select(f
, NULL
, TRUE
);
1417 } else if (ev
->xkey
.keycode
== ob_keycode(OB_KEY_RIGHT
)) {
1419 if ((f
= find_active_menu()) && f
->child
)
1420 menu_frame_select_next(f
->child
);
1421 } else if (ev
->xkey
.keycode
== ob_keycode(OB_KEY_UP
)) {
1423 if ((f
= find_active_or_last_menu()))
1424 menu_frame_select_previous(f
);
1425 } else if (ev
->xkey
.keycode
== ob_keycode(OB_KEY_DOWN
)) {
1427 if ((f
= find_active_or_last_menu()))
1428 menu_frame_select_next(f
);
1430 event_handle_menu_shortcut(ev
);
1435 static gboolean
menu_hide_delay_func(gpointer data
)
1437 menu_can_hide
= TRUE
;
1438 return FALSE
; /* no repeat */
1441 static void focus_delay_dest(gpointer data
)
1446 static gboolean
focus_delay_cmp(gconstpointer d1
, gconstpointer d2
)
1448 const ObFocusDelayData
*f1
= d1
;
1449 return f1
->client
== d2
;
1452 static gboolean
focus_delay_func(gpointer data
)
1454 ObFocusDelayData
*d
= data
;
1455 Time old
= event_curtime
;
1457 event_curtime
= d
->time
;
1458 if (focus_client
!= d
->client
) {
1459 if (client_focus(d
->client
) && config_focus_raise
)
1460 client_raise(d
->client
);
1462 event_curtime
= old
;
1463 return FALSE
; /* no repeat */
1466 static void focus_delay_client_dest(ObClient
*client
, gpointer data
)
1468 ob_main_loop_timeout_remove_data(ob_main_loop
, focus_delay_func
,
1472 void event_halt_focus_delay()
1474 ob_main_loop_timeout_remove(ob_main_loop
, focus_delay_func
);
1477 void event_ignore_queued_enters()
1479 GSList
*saved
= NULL
, *it
;
1482 XSync(ob_display
, FALSE
);
1484 /* count the events */
1486 e
= g_new(XEvent
, 1);
1487 if (XCheckTypedEvent(ob_display
, EnterNotify
, e
)) {
1490 win
= g_hash_table_lookup(window_map
, &e
->xany
.window
);
1491 if (win
&& WINDOW_IS_CLIENT(win
))
1492 ++ignore_enter_focus
;
1494 saved
= g_slist_append(saved
, e
);
1500 /* put the events back */
1501 for (it
= saved
; it
; it
= g_slist_next(it
)) {
1502 XPutBackEvent(ob_display
, it
->data
);
1505 g_slist_free(saved
);
1508 gboolean
event_time_after(Time t1
, Time t2
)
1510 g_assert(t1
!= CurrentTime
);
1511 g_assert(t2
!= CurrentTime
);
1514 Timestamp values wrap around (after about 49.7 days). The server, given
1515 its current time is represented by timestamp T, always interprets
1516 timestamps from clients by treating half of the timestamp space as being
1517 later in time than T.
1518 - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
1521 /* TIME_HALF is half of the number space of a Time type variable */
1522 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
1524 if (t2
>= TIME_HALF
)
1525 /* t2 is in the second half so t1 might wrap around and be smaller than
1527 return t1
>= t2
|| t1
< (t2
+ TIME_HALF
);
1529 /* t2 is in the first half so t1 has to come after it */
1530 return t1
>= t2
&& t1
< (t2
+ TIME_HALF
);