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 Ben 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"
44 #include <X11/keysym.h>
45 #include <X11/Xatom.h>
48 #ifdef HAVE_SYS_SELECT_H
49 # include <sys/select.h>
55 # include <X11/XKBlib.h>
59 #include <X11/ICE/ICElib.h>
73 static void event_process(const XEvent
*e
, gpointer data
);
74 static void event_handle_root(XEvent
*e
);
75 static void event_handle_menu(XEvent
*e
);
76 static void event_handle_dock(ObDock
*s
, XEvent
*e
);
77 static void event_handle_dockapp(ObDockApp
*app
, XEvent
*e
);
78 static void event_handle_client(ObClient
*c
, XEvent
*e
);
79 static void event_handle_group(ObGroup
*g
, XEvent
*e
);
81 static void focus_delay_dest(gpointer data
);
82 static gboolean
focus_delay_cmp(gconstpointer d1
, gconstpointer d2
);
83 static gboolean
focus_delay_func(gpointer data
);
84 static void focus_delay_client_dest(ObClient
*client
, gpointer data
);
86 static gboolean
menu_hide_delay_func(gpointer data
);
88 /* The time for the current event being processed */
89 Time event_curtime
= CurrentTime
;
91 /*! The value of the mask for the NumLock modifier */
93 /*! The value of the mask for the ScrollLock modifier */
95 /*! The key codes for the modifier keys */
96 static XModifierKeymap
*modmap
;
97 /*! Table of the constant modifier masks */
98 static const gint mask_table
[] = {
99 ShiftMask
, LockMask
, ControlMask
, Mod1Mask
,
100 Mod2Mask
, Mod3Mask
, Mod4Mask
, Mod5Mask
102 static gint mask_table_size
;
104 static guint ignore_enter_focus
= 0;
106 static gboolean menu_can_hide
;
109 static void ice_handler(gint fd
, gpointer conn
)
112 IceProcessMessages(conn
, NULL
, &b
);
115 static void ice_watch(IceConn conn
, IcePointer data
, Bool opening
,
116 IcePointer
*watch_data
)
121 fd
= IceConnectionNumber(conn
);
122 ob_main_loop_fd_add(ob_main_loop
, fd
, ice_handler
, conn
, NULL
);
124 ob_main_loop_fd_remove(ob_main_loop
, fd
);
130 void event_startup(gboolean reconfig
)
132 if (reconfig
) return;
134 mask_table_size
= sizeof(mask_table
) / sizeof(mask_table
[0]);
136 /* get lock masks that are defined by the display (not constant) */
137 modmap
= XGetModifierMapping(ob_display
);
139 if (modmap
&& modmap
->max_keypermod
> 0) {
141 const size_t size
= mask_table_size
* modmap
->max_keypermod
;
142 /* get the values of the keyboard lock modifiers
143 Note: Caps lock is not retrieved the same way as Scroll and Num
144 lock since it doesn't need to be. */
145 const KeyCode num_lock
= XKeysymToKeycode(ob_display
, XK_Num_Lock
);
146 const KeyCode scroll_lock
= XKeysymToKeycode(ob_display
,
149 for (cnt
= 0; cnt
< size
; ++cnt
) {
150 if (! modmap
->modifiermap
[cnt
]) continue;
152 if (num_lock
== modmap
->modifiermap
[cnt
])
153 NumLockMask
= mask_table
[cnt
/ modmap
->max_keypermod
];
154 if (scroll_lock
== modmap
->modifiermap
[cnt
])
155 ScrollLockMask
= mask_table
[cnt
/ modmap
->max_keypermod
];
159 ob_main_loop_x_add(ob_main_loop
, event_process
, NULL
, NULL
);
162 IceAddConnectionWatch(ice_watch
, NULL
);
165 client_add_destructor(focus_delay_client_dest
, NULL
);
168 void event_shutdown(gboolean reconfig
)
170 if (reconfig
) return;
173 IceRemoveConnectionWatch(ice_watch
, NULL
);
176 client_remove_destructor(focus_delay_client_dest
);
177 XFreeModifiermap(modmap
);
180 static Window
event_get_window(XEvent
*e
)
187 window
= RootWindow(ob_display
, ob_screen
);
190 window
= e
->xmap
.window
;
193 window
= e
->xunmap
.window
;
196 window
= e
->xdestroywindow
.window
;
198 case ConfigureRequest
:
199 window
= e
->xconfigurerequest
.window
;
201 case ConfigureNotify
:
202 window
= e
->xconfigure
.window
;
206 if (extensions_xkb
&& e
->type
== extensions_xkb_event_basep
) {
207 switch (((XkbAnyEvent
*)e
)->xkb_type
) {
209 window
= ((XkbBellNotifyEvent
*)e
)->window
;
215 window
= e
->xany
.window
;
220 static void event_set_curtime(XEvent
*e
)
222 Time t
= CurrentTime
;
224 /* grab the lasttime and hack up the state */
240 t
= e
->xproperty
.time
;
244 t
= e
->xcrossing
.time
;
247 /* if more event types are anticipated, get their timestamp
255 #define STRIP_MODS(s) \
256 s &= ~(LockMask | NumLockMask | ScrollLockMask), \
257 /* kill off the Button1Mask etc, only want the modifiers */ \
258 s &= (ControlMask | ShiftMask | Mod1Mask | \
259 Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask) \
261 static void event_hack_mods(XEvent *e)
264 XkbStateRec xkb_state
;
272 STRIP_MODS(e
->xbutton
.state
);
275 STRIP_MODS(e
->xkey
.state
);
278 STRIP_MODS(e
->xkey
.state
);
279 /* remove from the state the mask of the modifier being released, if
280 it is a modifier key being released (this is a little ugly..) */
282 if (XkbGetState(ob_display
, XkbUseCoreKbd
, &xkb_state
) == Success
) {
283 e
->xkey
.state
= xkb_state
.compat_state
;
287 kp
= modmap
->modifiermap
;
288 for (i
= 0; i
< mask_table_size
; ++i
) {
289 for (k
= 0; k
< modmap
->max_keypermod
; ++k
) {
290 if (*kp
== e
->xkey
.keycode
) { /* found the keycode */
291 /* remove the mask for it */
292 e
->xkey
.state
&= ~mask_table
[i
];
293 /* cause the first loop to break; */
295 break; /* get outta here! */
302 STRIP_MODS(e
->xmotion
.state
);
303 /* compress events */
306 while (XCheckTypedWindowEvent(ob_display
, e
->xmotion
.window
,
308 e
->xmotion
.x_root
= ce
.xmotion
.x_root
;
309 e
->xmotion
.y_root
= ce
.xmotion
.y_root
;
316 static gboolean
wanted_focusevent(XEvent
*e
)
318 gint mode
= e
->xfocus
.mode
;
319 gint detail
= e
->xfocus
.detail
;
320 Window win
= e
->xany
.window
;
322 if (e
->type
== FocusIn
) {
324 /* These are ones we never want.. */
326 /* This means focus was given by a keyboard/mouse grab. */
327 if (mode
== NotifyGrab
)
329 /* This means focus was given back from a keyboard/mouse grab. */
330 if (mode
== NotifyUngrab
)
333 /* These are the ones we want.. */
335 if (win
== RootWindow(ob_display
, ob_screen
)) {
336 /* This means focus reverted off of a client */
337 if (detail
== NotifyPointerRoot
|| detail
== NotifyDetailNone
||
338 detail
== NotifyInferior
)
344 /* This means focus moved from the root window to a client */
345 if (detail
== NotifyVirtual
)
347 /* This means focus moved from one client to another */
348 if (detail
== NotifyNonlinearVirtual
)
354 g_assert(e
->type
== FocusOut
);
357 /* These are ones we never want.. */
359 /* This means focus was taken by a keyboard/mouse grab. */
360 if (mode
== NotifyGrab
)
363 /* Focus left the root window revertedto state */
364 if (win
== RootWindow(ob_display
, ob_screen
))
367 /* These are the ones we want.. */
369 /* This means focus moved from a client to the root window */
370 if (detail
== NotifyVirtual
)
372 /* This means focus moved from one client to another */
373 if (detail
== NotifyNonlinearVirtual
)
381 static Bool
look_for_focusin(Display
*d
, XEvent
*e
, XPointer arg
)
383 return e
->type
== FocusIn
&& wanted_focusevent(e
);
386 static gboolean
event_ignore(XEvent
*e
, ObClient
*client
)
390 if (!wanted_focusevent(e
))
394 if (!wanted_focusevent(e
))
401 static void event_process(const XEvent
*ec
, gpointer data
)
404 ObGroup
*group
= NULL
;
405 ObClient
*client
= NULL
;
407 ObDockApp
*dockapp
= NULL
;
408 ObWindow
*obwin
= NULL
;
410 ObEventData
*ed
= data
;
412 /* make a copy we can mangle */
416 window
= event_get_window(e
);
417 if (!(e
->type
== PropertyNotify
&&
418 (group
= g_hash_table_lookup(group_map
, &window
))))
419 if ((obwin
= g_hash_table_lookup(window_map
, &window
))) {
420 switch (obwin
->type
) {
422 dock
= WINDOW_AS_DOCK(obwin
);
425 dockapp
= WINDOW_AS_DOCKAPP(obwin
);
428 client
= WINDOW_AS_CLIENT(obwin
);
431 case Window_Internal
:
432 /* not to be used for events */
433 g_assert_not_reached();
438 event_set_curtime(e
);
440 if (event_ignore(e
, client
)) {
447 /* deal with it in the kernel */
449 if (menu_frame_visible
&&
450 (e
->type
== EnterNotify
|| e
->type
== LeaveNotify
))
452 /* crossing events for menu */
453 event_handle_menu(e
);
454 } else if (e
->type
== FocusIn
) {
455 if (client
&& client
!= focus_client
) {
456 frame_adjust_focus(client
->frame
, TRUE
);
457 focus_set_client(client
);
458 client_calc_layer(client
);
460 } else if (e
->type
== FocusOut
) {
461 gboolean nomove
= FALSE
;
464 ob_debug_type(OB_DEBUG_FOCUS
, "FocusOut Event\n");
466 /* Look for the followup FocusIn */
467 if (!XCheckIfEvent(ob_display
, &ce
, look_for_focusin
, NULL
)) {
468 /* There is no FocusIn, this means focus went to a window that
469 is not being managed, or a window on another screen. */
470 ob_debug_type(OB_DEBUG_FOCUS
, "Focus went to a black hole !\n");
471 /* nothing is focused */
472 focus_set_client(NULL
);
473 } else if (ce
.xany
.window
== e
->xany
.window
) {
474 ob_debug_type(OB_DEBUG_FOCUS
, "Focus didn't go anywhere\n");
475 /* If focus didn't actually move anywhere, there is nothing to do*/
477 } else if (ce
.xfocus
.detail
== NotifyPointerRoot
||
478 ce
.xfocus
.detail
== NotifyDetailNone
||
479 ce
.xfocus
.detail
== NotifyInferior
) {
480 ob_debug_type(OB_DEBUG_FOCUS
, "Focus went to root\n");
481 /* Focus has been reverted to the root window or nothing
482 FocusOut events come after UnmapNotify, so we don't need to
483 worry about focusing an invalid window
485 focus_fallback(TRUE
);
487 /* Focus did move, so process the FocusIn event */
488 ObEventData ed
= { .ignored
= FALSE
};
489 event_process(&ce
, &ed
);
491 /* The FocusIn was ignored, this means it was on a window
492 that isn't a client. */
493 ob_debug_type(OB_DEBUG_FOCUS
,
494 "Focus went to an unmanaged window 0x%x !\n",
496 focus_fallback(TRUE
);
500 if (client
&& !nomove
) {
501 frame_adjust_focus(client
->frame
, FALSE
);
502 /* focus_set_client has already been called for sure */
503 client_calc_layer(client
);
506 event_handle_group(group
, e
);
508 event_handle_client(client
, e
);
510 event_handle_dockapp(dockapp
, e
);
512 event_handle_dock(dock
, e
);
513 else if (window
== RootWindow(ob_display
, ob_screen
))
514 event_handle_root(e
);
515 else if (e
->type
== MapRequest
)
516 client_manage(window
);
517 else if (e
->type
== ConfigureRequest
) {
518 /* unhandled configure requests must be used to configure the
522 xwc
.x
= e
->xconfigurerequest
.x
;
523 xwc
.y
= e
->xconfigurerequest
.y
;
524 xwc
.width
= e
->xconfigurerequest
.width
;
525 xwc
.height
= e
->xconfigurerequest
.height
;
526 xwc
.border_width
= e
->xconfigurerequest
.border_width
;
527 xwc
.sibling
= e
->xconfigurerequest
.above
;
528 xwc
.stack_mode
= e
->xconfigurerequest
.detail
;
530 /* we are not to be held responsible if someone sends us an
532 xerror_set_ignore(TRUE
);
533 XConfigureWindow(ob_display
, window
,
534 e
->xconfigurerequest
.value_mask
, &xwc
);
535 xerror_set_ignore(FALSE
);
538 /* user input (action-bound) events */
539 if (e
->type
== ButtonPress
|| e
->type
== ButtonRelease
||
540 e
->type
== MotionNotify
|| e
->type
== KeyPress
||
541 e
->type
== KeyRelease
)
543 if (menu_frame_visible
)
544 event_handle_menu(e
);
546 if (!keyboard_process_interactive_grab(e
, &client
)) {
547 if (moveresize_in_progress
) {
550 /* make further actions work on the client being
552 client
= moveresize_client
;
555 menu_can_hide
= FALSE
;
556 ob_main_loop_timeout_add(ob_main_loop
,
557 config_menu_hide_delay
* 1000,
558 menu_hide_delay_func
,
559 NULL
, g_direct_equal
, NULL
);
561 if (e
->type
== ButtonPress
|| e
->type
== ButtonRelease
||
562 e
->type
== MotionNotify
) {
563 mouse_event(client
, e
);
564 } else if (e
->type
== KeyPress
) {
565 keyboard_event((focus_cycle_target
? focus_cycle_target
:
571 /* if something happens and it's not from an XEvent, then we don't know
573 event_curtime
= CurrentTime
;
576 static void event_handle_root(XEvent
*e
)
582 ob_debug("Another WM has requested to replace us. Exiting.\n");
587 if (e
->xclient
.format
!= 32) break;
589 msgtype
= e
->xclient
.message_type
;
590 if (msgtype
== prop_atoms
.net_current_desktop
) {
591 guint d
= e
->xclient
.data
.l
[0];
592 if (d
< screen_num_desktops
) {
593 event_curtime
= e
->xclient
.data
.l
[1];
594 ob_debug("SWITCH DESKTOP TIME: %d\n", event_curtime
);
595 screen_set_desktop(d
);
597 } else if (msgtype
== prop_atoms
.net_number_of_desktops
) {
598 guint d
= e
->xclient
.data
.l
[0];
600 screen_set_num_desktops(d
);
601 } else if (msgtype
== prop_atoms
.net_showing_desktop
) {
602 screen_show_desktop(e
->xclient
.data
.l
[0] != 0);
603 } else if (msgtype
== prop_atoms
.ob_control
) {
604 if (e
->xclient
.data
.l
[0] == 1)
606 else if (e
->xclient
.data
.l
[0] == 2)
611 if (e
->xproperty
.atom
== prop_atoms
.net_desktop_names
)
612 screen_update_desktop_names();
613 else if (e
->xproperty
.atom
== prop_atoms
.net_desktop_layout
)
614 screen_update_layout();
616 case ConfigureNotify
:
618 XRRUpdateConfiguration(e
);
627 static void event_handle_group(ObGroup
*group
, XEvent
*e
)
631 g_assert(e
->type
== PropertyNotify
);
633 for (it
= group
->members
; it
; it
= g_slist_next(it
))
634 event_handle_client(it
->data
, e
);
637 void event_enter_client(ObClient
*client
)
639 g_assert(config_focus_follow
);
641 if (client_normal(client
) && client_can_focus(client
)) {
642 if (config_focus_delay
) {
643 ObFocusDelayData
*data
;
645 ob_main_loop_timeout_remove(ob_main_loop
, focus_delay_func
);
647 data
= g_new(ObFocusDelayData
, 1);
648 data
->client
= client
;
649 data
->time
= event_curtime
;
651 ob_main_loop_timeout_add(ob_main_loop
,
654 data
, focus_delay_cmp
, focus_delay_dest
);
656 ObFocusDelayData data
;
657 data
.client
= client
;
658 data
.time
= event_curtime
;
659 focus_delay_func(&data
);
664 static void event_handle_client(ObClient
*client
, XEvent
*e
)
672 case VisibilityNotify
:
673 client
->frame
->obscured
= e
->xvisibility
.state
!= VisibilityUnobscured
;
677 /* Wheel buttons don't draw because they are an instant click, so it
678 is a waste of resources to go drawing it. */
679 if (!(e
->xbutton
.button
== 4 || e
->xbutton
.button
== 5)) {
680 con
= frame_context(client
, e
->xbutton
.window
);
681 con
= mouse_button_frame_context(con
, e
->xbutton
.button
);
683 case OB_FRAME_CONTEXT_MAXIMIZE
:
684 client
->frame
->max_press
= (e
->type
== ButtonPress
);
685 framerender_frame(client
->frame
);
687 case OB_FRAME_CONTEXT_CLOSE
:
688 client
->frame
->close_press
= (e
->type
== ButtonPress
);
689 framerender_frame(client
->frame
);
691 case OB_FRAME_CONTEXT_ICONIFY
:
692 client
->frame
->iconify_press
= (e
->type
== ButtonPress
);
693 framerender_frame(client
->frame
);
695 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
696 client
->frame
->desk_press
= (e
->type
== ButtonPress
);
697 framerender_frame(client
->frame
);
699 case OB_FRAME_CONTEXT_SHADE
:
700 client
->frame
->shade_press
= (e
->type
== ButtonPress
);
701 framerender_frame(client
->frame
);
704 /* nothing changes with clicks for any other contexts */
710 con
= frame_context(client
, e
->xcrossing
.window
);
712 case OB_FRAME_CONTEXT_MAXIMIZE
:
713 client
->frame
->max_hover
= FALSE
;
714 frame_adjust_state(client
->frame
);
716 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
717 client
->frame
->desk_hover
= FALSE
;
718 frame_adjust_state(client
->frame
);
720 case OB_FRAME_CONTEXT_SHADE
:
721 client
->frame
->shade_hover
= FALSE
;
722 frame_adjust_state(client
->frame
);
724 case OB_FRAME_CONTEXT_ICONIFY
:
725 client
->frame
->iconify_hover
= FALSE
;
726 frame_adjust_state(client
->frame
);
728 case OB_FRAME_CONTEXT_CLOSE
:
729 client
->frame
->close_hover
= FALSE
;
730 frame_adjust_state(client
->frame
);
732 case OB_FRAME_CONTEXT_FRAME
:
733 if (keyboard_interactively_grabbed())
735 if (config_focus_follow
&& config_focus_delay
)
736 ob_main_loop_timeout_remove_data(ob_main_loop
,
746 gboolean nofocus
= FALSE
;
748 if (ignore_enter_focus
) {
749 ignore_enter_focus
--;
753 con
= frame_context(client
, e
->xcrossing
.window
);
755 case OB_FRAME_CONTEXT_MAXIMIZE
:
756 client
->frame
->max_hover
= TRUE
;
757 frame_adjust_state(client
->frame
);
759 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
760 client
->frame
->desk_hover
= TRUE
;
761 frame_adjust_state(client
->frame
);
763 case OB_FRAME_CONTEXT_SHADE
:
764 client
->frame
->shade_hover
= TRUE
;
765 frame_adjust_state(client
->frame
);
767 case OB_FRAME_CONTEXT_ICONIFY
:
768 client
->frame
->iconify_hover
= TRUE
;
769 frame_adjust_state(client
->frame
);
771 case OB_FRAME_CONTEXT_CLOSE
:
772 client
->frame
->close_hover
= TRUE
;
773 frame_adjust_state(client
->frame
);
775 case OB_FRAME_CONTEXT_FRAME
:
776 if (keyboard_interactively_grabbed())
778 if (e
->xcrossing
.mode
== NotifyGrab
||
779 e
->xcrossing
.mode
== NotifyUngrab
)
781 ob_debug_type(OB_DEBUG_FOCUS
,
782 "%sNotify mode %d detail %d on %lx IGNORED\n",
783 (e
->type
== EnterNotify
? "Enter" : "Leave"),
785 e
->xcrossing
.detail
, client
?client
->window
:0);
787 ob_debug_type(OB_DEBUG_FOCUS
,
788 "%sNotify mode %d detail %d on %lx, "
789 "focusing window: %d\n",
790 (e
->type
== EnterNotify
? "Enter" : "Leave"),
792 e
->xcrossing
.detail
, (client
?client
->window
:0),
794 if (!nofocus
&& config_focus_follow
)
795 event_enter_client(client
);
803 case ConfigureRequest
:
805 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
806 ConfigureRequest
, &ce
)) {
808 /* XXX if this causes bad things.. we can compress config req's
809 with the same mask. */
810 e
->xconfigurerequest
.value_mask
|=
811 ce
.xconfigurerequest
.value_mask
;
812 if (ce
.xconfigurerequest
.value_mask
& CWX
)
813 e
->xconfigurerequest
.x
= ce
.xconfigurerequest
.x
;
814 if (ce
.xconfigurerequest
.value_mask
& CWY
)
815 e
->xconfigurerequest
.y
= ce
.xconfigurerequest
.y
;
816 if (ce
.xconfigurerequest
.value_mask
& CWWidth
)
817 e
->xconfigurerequest
.width
= ce
.xconfigurerequest
.width
;
818 if (ce
.xconfigurerequest
.value_mask
& CWHeight
)
819 e
->xconfigurerequest
.height
= ce
.xconfigurerequest
.height
;
820 if (ce
.xconfigurerequest
.value_mask
& CWBorderWidth
)
821 e
->xconfigurerequest
.border_width
=
822 ce
.xconfigurerequest
.border_width
;
823 if (ce
.xconfigurerequest
.value_mask
& CWStackMode
)
824 e
->xconfigurerequest
.detail
= ce
.xconfigurerequest
.detail
;
827 /* if we are iconic (or shaded (fvwm does this)) ignore the event */
828 if (client
->iconic
|| client
->shaded
) return;
830 /* resize, then move, as specified in the EWMH section 7.7 */
831 if (e
->xconfigurerequest
.value_mask
& (CWWidth
| CWHeight
|
837 if (e
->xconfigurerequest
.value_mask
& CWBorderWidth
)
838 client
->border_width
= e
->xconfigurerequest
.border_width
;
840 x
= (e
->xconfigurerequest
.value_mask
& CWX
) ?
841 e
->xconfigurerequest
.x
: client
->area
.x
;
842 y
= (e
->xconfigurerequest
.value_mask
& CWY
) ?
843 e
->xconfigurerequest
.y
: client
->area
.y
;
844 w
= (e
->xconfigurerequest
.value_mask
& CWWidth
) ?
845 e
->xconfigurerequest
.width
: client
->area
.width
;
846 h
= (e
->xconfigurerequest
.value_mask
& CWHeight
) ?
847 e
->xconfigurerequest
.height
: client
->area
.height
;
853 client
->frame
->size
.left
+ client
->frame
->size
.right
;
855 client
->frame
->size
.top
+ client
->frame
->size
.bottom
;
856 /* make this rude for size-only changes but not for position
858 gboolean moving
= ((e
->xconfigurerequest
.value_mask
& CWX
) ||
859 (e
->xconfigurerequest
.value_mask
& CWY
));
861 client_find_onscreen(client
, &newx
, &newy
, fw
, fh
,
863 if (e
->xconfigurerequest
.value_mask
& CWX
)
865 if (e
->xconfigurerequest
.value_mask
& CWY
)
869 switch (client
->gravity
) {
870 case NorthEastGravity
:
872 corner
= OB_CORNER_TOPRIGHT
;
874 case SouthWestGravity
:
876 corner
= OB_CORNER_BOTTOMLEFT
;
878 case SouthEastGravity
:
879 corner
= OB_CORNER_BOTTOMRIGHT
;
881 default: /* NorthWest, Static, etc */
882 corner
= OB_CORNER_TOPLEFT
;
885 client_configure_full(client
, corner
, x
, y
, w
, h
, FALSE
, TRUE
,
889 if (e
->xconfigurerequest
.value_mask
& CWStackMode
) {
890 switch (e
->xconfigurerequest
.detail
) {
893 /* Apps are so rude. And this is totally disconnected from
894 activation/focus. Bleh. */
895 /*client_lower(client);*/
901 /* Apps are so rude. And this is totally disconnected from
902 activation/focus. Bleh. */
903 /*client_raise(client);*/
909 if (client
->ignore_unmaps
) {
910 client
->ignore_unmaps
--;
913 ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
915 client
->window
, e
->xunmap
.event
, e
->xunmap
.from_configure
,
916 client
->ignore_unmaps
);
917 client_unmanage(client
);
920 ob_debug("DestroyNotify for window 0x%x\n", client
->window
);
921 client_unmanage(client
);
924 /* this is when the client is first taken captive in the frame */
925 if (e
->xreparent
.parent
== client
->frame
->plate
) break;
928 This event is quite rare and is usually handled in unmapHandler.
929 However, if the window is unmapped when the reparent event occurs,
930 the window manager never sees it because an unmap event is not sent
931 to an already unmapped window.
934 /* we don't want the reparent event, put it back on the stack for the
935 X server to deal with after we unmanage the window */
936 XPutBackEvent(ob_display
, e
);
938 ob_debug("ReparentNotify for window 0x%x\n", client
->window
);
939 client_unmanage(client
);
942 ob_debug("MapRequest for 0x%lx\n", client
->window
);
943 if (!client
->iconic
) break; /* this normally doesn't happen, but if it
944 does, we don't want it!
945 it can happen now when the window is on
946 another desktop, but we still don't
948 client_activate(client
, FALSE
, TRUE
);
951 /* validate cuz we query stuff off the client here */
952 if (!client_validate(client
)) break;
954 if (e
->xclient
.format
!= 32) return;
956 msgtype
= e
->xclient
.message_type
;
957 if (msgtype
== prop_atoms
.wm_change_state
) {
958 /* compress changes into a single change */
959 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
961 /* XXX: it would be nice to compress ALL messages of a
962 type, not just messages in a row without other
963 message types between. */
964 if (ce
.xclient
.message_type
!= msgtype
) {
965 XPutBackEvent(ob_display
, &ce
);
968 e
->xclient
= ce
.xclient
;
970 client_set_wm_state(client
, e
->xclient
.data
.l
[0]);
971 } else if (msgtype
== prop_atoms
.net_wm_desktop
) {
972 /* compress changes into a single change */
973 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
975 /* XXX: it would be nice to compress ALL messages of a
976 type, not just messages in a row without other
977 message types between. */
978 if (ce
.xclient
.message_type
!= msgtype
) {
979 XPutBackEvent(ob_display
, &ce
);
982 e
->xclient
= ce
.xclient
;
984 if ((unsigned)e
->xclient
.data
.l
[0] < screen_num_desktops
||
985 (unsigned)e
->xclient
.data
.l
[0] == DESKTOP_ALL
)
986 client_set_desktop(client
, (unsigned)e
->xclient
.data
.l
[0],
988 } else if (msgtype
== prop_atoms
.net_wm_state
) {
989 /* can't compress these */
990 ob_debug("net_wm_state %s %ld %ld for 0x%lx\n",
991 (e
->xclient
.data
.l
[0] == 0 ? "Remove" :
992 e
->xclient
.data
.l
[0] == 1 ? "Add" :
993 e
->xclient
.data
.l
[0] == 2 ? "Toggle" : "INVALID"),
994 e
->xclient
.data
.l
[1], e
->xclient
.data
.l
[2],
996 client_set_state(client
, e
->xclient
.data
.l
[0],
997 e
->xclient
.data
.l
[1], e
->xclient
.data
.l
[2]);
998 } else if (msgtype
== prop_atoms
.net_close_window
) {
999 ob_debug("net_close_window for 0x%lx\n", client
->window
);
1000 client_close(client
);
1001 } else if (msgtype
== prop_atoms
.net_active_window
) {
1002 ob_debug("net_active_window for 0x%lx source=%s\n",
1004 (e
->xclient
.data
.l
[0] == 0 ? "unknown" :
1005 (e
->xclient
.data
.l
[0] == 1 ? "application" :
1006 (e
->xclient
.data
.l
[0] == 2 ? "user" : "INVALID"))));
1007 /* XXX make use of data.l[2] ! */
1008 event_curtime
= e
->xclient
.data
.l
[1];
1009 client_activate(client
, FALSE
,
1010 (e
->xclient
.data
.l
[0] == 0 ||
1011 e
->xclient
.data
.l
[0] == 2));
1012 } else if (msgtype
== prop_atoms
.net_wm_moveresize
) {
1013 ob_debug("net_wm_moveresize for 0x%lx direction %d\n",
1014 client
->window
, e
->xclient
.data
.l
[2]);
1015 if ((Atom
)e
->xclient
.data
.l
[2] ==
1016 prop_atoms
.net_wm_moveresize_size_topleft
||
1017 (Atom
)e
->xclient
.data
.l
[2] ==
1018 prop_atoms
.net_wm_moveresize_size_top
||
1019 (Atom
)e
->xclient
.data
.l
[2] ==
1020 prop_atoms
.net_wm_moveresize_size_topright
||
1021 (Atom
)e
->xclient
.data
.l
[2] ==
1022 prop_atoms
.net_wm_moveresize_size_right
||
1023 (Atom
)e
->xclient
.data
.l
[2] ==
1024 prop_atoms
.net_wm_moveresize_size_right
||
1025 (Atom
)e
->xclient
.data
.l
[2] ==
1026 prop_atoms
.net_wm_moveresize_size_bottomright
||
1027 (Atom
)e
->xclient
.data
.l
[2] ==
1028 prop_atoms
.net_wm_moveresize_size_bottom
||
1029 (Atom
)e
->xclient
.data
.l
[2] ==
1030 prop_atoms
.net_wm_moveresize_size_bottomleft
||
1031 (Atom
)e
->xclient
.data
.l
[2] ==
1032 prop_atoms
.net_wm_moveresize_size_left
||
1033 (Atom
)e
->xclient
.data
.l
[2] ==
1034 prop_atoms
.net_wm_moveresize_move
||
1035 (Atom
)e
->xclient
.data
.l
[2] ==
1036 prop_atoms
.net_wm_moveresize_size_keyboard
||
1037 (Atom
)e
->xclient
.data
.l
[2] ==
1038 prop_atoms
.net_wm_moveresize_move_keyboard
) {
1040 moveresize_start(client
, e
->xclient
.data
.l
[0],
1041 e
->xclient
.data
.l
[1], e
->xclient
.data
.l
[3],
1042 e
->xclient
.data
.l
[2]);
1044 else if ((Atom
)e
->xclient
.data
.l
[2] ==
1045 prop_atoms
.net_wm_moveresize_cancel
)
1046 moveresize_end(TRUE
);
1047 } else if (msgtype
== prop_atoms
.net_moveresize_window
) {
1048 gint oldg
= client
->gravity
;
1049 gint tmpg
, x
, y
, w
, h
;
1051 if (e
->xclient
.data
.l
[0] & 0xff)
1052 tmpg
= e
->xclient
.data
.l
[0] & 0xff;
1056 if (e
->xclient
.data
.l
[0] & 1 << 8)
1057 x
= e
->xclient
.data
.l
[1];
1060 if (e
->xclient
.data
.l
[0] & 1 << 9)
1061 y
= e
->xclient
.data
.l
[2];
1064 if (e
->xclient
.data
.l
[0] & 1 << 10)
1065 w
= e
->xclient
.data
.l
[3];
1067 w
= client
->area
.width
;
1068 if (e
->xclient
.data
.l
[0] & 1 << 11)
1069 h
= e
->xclient
.data
.l
[4];
1071 h
= client
->area
.height
;
1072 client
->gravity
= tmpg
;
1078 client
->frame
->size
.left
+ client
->frame
->size
.right
;
1080 client
->frame
->size
.top
+ client
->frame
->size
.bottom
;
1081 client_find_onscreen(client
, &newx
, &newy
, fw
, fh
,
1082 client_normal(client
));
1083 if (e
->xclient
.data
.l
[0] & 1 << 8)
1085 if (e
->xclient
.data
.l
[0] & 1 << 9)
1089 client_configure(client
, OB_CORNER_TOPLEFT
,
1090 x
, y
, w
, h
, FALSE
, TRUE
);
1092 client
->gravity
= oldg
;
1095 case PropertyNotify
:
1096 /* validate cuz we query stuff off the client here */
1097 if (!client_validate(client
)) break;
1099 /* compress changes to a single property into a single change */
1100 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
1104 /* XXX: it would be nice to compress ALL changes to a property,
1105 not just changes in a row without other props between. */
1107 a
= ce
.xproperty
.atom
;
1108 b
= e
->xproperty
.atom
;
1112 if ((a
== prop_atoms
.net_wm_name
||
1113 a
== prop_atoms
.wm_name
||
1114 a
== prop_atoms
.net_wm_icon_name
||
1115 a
== prop_atoms
.wm_icon_name
)
1117 (b
== prop_atoms
.net_wm_name
||
1118 b
== prop_atoms
.wm_name
||
1119 b
== prop_atoms
.net_wm_icon_name
||
1120 b
== prop_atoms
.wm_icon_name
)) {
1123 if (a
== prop_atoms
.net_wm_icon
&&
1124 b
== prop_atoms
.net_wm_icon
)
1127 XPutBackEvent(ob_display
, &ce
);
1131 msgtype
= e
->xproperty
.atom
;
1132 if (msgtype
== XA_WM_NORMAL_HINTS
) {
1133 client_update_normal_hints(client
);
1134 /* normal hints can make a window non-resizable */
1135 client_setup_decor_and_functions(client
);
1136 } else if (msgtype
== XA_WM_HINTS
) {
1137 client_update_wmhints(client
);
1138 } else if (msgtype
== XA_WM_TRANSIENT_FOR
) {
1139 client_update_transient_for(client
);
1140 client_get_type(client
);
1141 /* type may have changed, so update the layer */
1142 client_calc_layer(client
);
1143 client_setup_decor_and_functions(client
);
1144 } else if (msgtype
== prop_atoms
.net_wm_name
||
1145 msgtype
== prop_atoms
.wm_name
||
1146 msgtype
== prop_atoms
.net_wm_icon_name
||
1147 msgtype
== prop_atoms
.wm_icon_name
) {
1148 client_update_title(client
);
1149 } else if (msgtype
== prop_atoms
.wm_class
) {
1150 client_update_class(client
);
1151 } else if (msgtype
== prop_atoms
.wm_protocols
) {
1152 client_update_protocols(client
);
1153 client_setup_decor_and_functions(client
);
1155 else if (msgtype
== prop_atoms
.net_wm_strut
) {
1156 client_update_strut(client
);
1158 else if (msgtype
== prop_atoms
.net_wm_icon
) {
1159 client_update_icons(client
);
1161 else if (msgtype
== prop_atoms
.net_wm_user_time
) {
1162 client_update_user_time(client
);
1164 else if (msgtype
== prop_atoms
.sm_client_id
) {
1165 client_update_sm_client_id(client
);
1170 if (extensions_shape
&& e
->type
== extensions_shape_event_basep
) {
1171 client
->shaped
= ((XShapeEvent
*)e
)->shaped
;
1172 frame_adjust_shape(client
->frame
);
1178 static void event_handle_dock(ObDock
*s
, XEvent
*e
)
1182 if (e
->xbutton
.button
== 1)
1183 stacking_raise(DOCK_AS_WINDOW(s
));
1184 else if (e
->xbutton
.button
== 2)
1185 stacking_lower(DOCK_AS_WINDOW(s
));
1196 static void event_handle_dockapp(ObDockApp
*app
, XEvent
*e
)
1200 dock_app_drag(app
, &e
->xmotion
);
1203 if (app
->ignore_unmaps
) {
1204 app
->ignore_unmaps
--;
1207 dock_remove(app
, TRUE
);
1210 dock_remove(app
, FALSE
);
1212 case ReparentNotify
:
1213 dock_remove(app
, FALSE
);
1215 case ConfigureNotify
:
1216 dock_app_configure(app
, e
->xconfigure
.width
, e
->xconfigure
.height
);
1221 ObMenuFrame
* find_active_menu()
1224 ObMenuFrame
*ret
= NULL
;
1226 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
1235 ObMenuFrame
* find_active_or_last_menu()
1237 ObMenuFrame
*ret
= NULL
;
1239 ret
= find_active_menu();
1240 if (!ret
&& menu_frame_visible
)
1241 ret
= menu_frame_visible
->data
;
1245 static void event_handle_menu(XEvent
*ev
)
1248 ObMenuEntryFrame
*e
;
1252 if (menu_can_hide
) {
1253 if ((e
= menu_entry_frame_under(ev
->xbutton
.x_root
,
1254 ev
->xbutton
.y_root
)))
1255 menu_entry_frame_execute(e
, ev
->xbutton
.state
,
1258 menu_frame_hide_all();
1262 if ((e
= g_hash_table_lookup(menu_frame_map
, &ev
->xcrossing
.window
))) {
1263 if (e
->ignore_enters
)
1266 menu_frame_select(e
->frame
, e
);
1270 if ((e
= g_hash_table_lookup(menu_frame_map
, &ev
->xcrossing
.window
)) &&
1271 (f
= find_active_menu()) && f
->selected
== e
&&
1272 e
->entry
->type
!= OB_MENU_ENTRY_TYPE_SUBMENU
)
1274 menu_frame_select(e
->frame
, NULL
);
1277 if ((e
= menu_entry_frame_under(ev
->xmotion
.x_root
,
1278 ev
->xmotion
.y_root
)))
1279 menu_frame_select(e
->frame
, e
);
1282 if (ev
->xkey
.keycode
== ob_keycode(OB_KEY_ESCAPE
))
1283 menu_frame_hide_all();
1284 else if (ev
->xkey
.keycode
== ob_keycode(OB_KEY_RETURN
)) {
1286 if ((f
= find_active_menu()))
1287 menu_entry_frame_execute(f
->selected
, ev
->xkey
.state
,
1289 } else if (ev
->xkey
.keycode
== ob_keycode(OB_KEY_LEFT
)) {
1291 if ((f
= find_active_or_last_menu()) && f
->parent
)
1292 menu_frame_select(f
, NULL
);
1293 } else if (ev
->xkey
.keycode
== ob_keycode(OB_KEY_RIGHT
)) {
1295 if ((f
= find_active_or_last_menu()) && f
->child
)
1296 menu_frame_select_next(f
->child
);
1297 } else if (ev
->xkey
.keycode
== ob_keycode(OB_KEY_UP
)) {
1299 if ((f
= find_active_or_last_menu()))
1300 menu_frame_select_previous(f
);
1301 } else if (ev
->xkey
.keycode
== ob_keycode(OB_KEY_DOWN
)) {
1303 if ((f
= find_active_or_last_menu()))
1304 menu_frame_select_next(f
);
1310 static gboolean
menu_hide_delay_func(gpointer data
)
1312 menu_can_hide
= TRUE
;
1313 return FALSE
; /* no repeat */
1316 static void focus_delay_dest(gpointer data
)
1321 static gboolean
focus_delay_cmp(gconstpointer d1
, gconstpointer d2
)
1323 const ObFocusDelayData
*f1
= d1
;
1324 return f1
->client
== d2
;
1327 static gboolean
focus_delay_func(gpointer data
)
1329 ObFocusDelayData
*d
= data
;
1330 Time old
= event_curtime
;
1332 event_curtime
= d
->time
;
1333 if (focus_client
!= d
->client
) {
1334 if (client_focus(d
->client
) && config_focus_raise
)
1335 client_raise(d
->client
);
1337 event_curtime
= old
;
1338 return FALSE
; /* no repeat */
1341 static void focus_delay_client_dest(ObClient
*client
, gpointer data
)
1343 ob_main_loop_timeout_remove_data(ob_main_loop
, focus_delay_func
,
1347 void event_halt_focus_delay()
1349 ob_main_loop_timeout_remove(ob_main_loop
, focus_delay_func
);
1352 void event_ignore_queued_enters()
1354 GSList
*saved
= NULL
, *it
;
1357 XSync(ob_display
, FALSE
);
1359 /* count the events */
1361 e
= g_new(XEvent
, 1);
1362 if (XCheckTypedEvent(ob_display
, EnterNotify
, e
)) {
1365 win
= g_hash_table_lookup(window_map
, &e
->xany
.window
);
1366 if (win
&& WINDOW_IS_CLIENT(win
))
1367 ++ignore_enter_focus
;
1369 saved
= g_slist_append(saved
, e
);
1375 /* put the events back */
1376 for (it
= saved
; it
; it
= g_slist_next(it
)) {
1377 XPutBackEvent(ob_display
, it
->data
);
1380 g_slist_free(saved
);
1383 gboolean
event_time_after(Time t1
, Time t2
)
1385 g_assert(t1
!= CurrentTime
);
1386 g_assert(t2
!= CurrentTime
);
1389 Timestamp values wrap around (after about 49.7 days). The server, given
1390 its current time is represented by timestamp T, always interprets
1391 timestamps from clients by treating half of the timestamp space as being
1392 later in time than T.
1393 - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
1396 /* TIME_HALF is half of the number space of a Time type variable */
1397 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
1399 if (t2
>= TIME_HALF
)
1400 /* t2 is in the second half so t1 might wrap around and be smaller than
1402 return t1
>= t2
|| t1
< (t2
+ TIME_HALF
);
1404 /* t2 is in the first half so t1 has to come after it */
1405 return t1
>= t2
&& t1
< (t2
+ TIME_HALF
);