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"
37 #include "framerender.h"
39 #include "moveresize.h"
42 #include "extensions.h"
43 #include "translate.h"
46 #include <X11/keysym.h>
47 #include <X11/Xatom.h>
50 #ifdef HAVE_SYS_SELECT_H
51 # include <sys/select.h>
57 # include <unistd.h> /* for usleep() */
60 # include <X11/XKBlib.h>
64 #include <X11/ICE/ICElib.h>
78 static void event_process(const XEvent
*e
, gpointer data
);
79 static void event_handle_root(XEvent
*e
);
80 static gboolean
event_handle_menu_keyboard(XEvent
*e
);
81 static gboolean
event_handle_menu(XEvent
*e
);
82 static void event_handle_dock(ObDock
*s
, XEvent
*e
);
83 static void event_handle_dockapp(ObDockApp
*app
, XEvent
*e
);
84 static void event_handle_client(ObClient
*c
, XEvent
*e
);
85 static void event_handle_group(ObGroup
*g
, XEvent
*e
);
86 static void event_handle_user_input(ObClient
*client
, XEvent
*e
);
88 static void focus_delay_dest(gpointer data
);
89 static gboolean
focus_delay_cmp(gconstpointer d1
, gconstpointer d2
);
90 static gboolean
focus_delay_func(gpointer data
);
91 static void focus_delay_client_dest(ObClient
*client
, gpointer data
);
93 static gboolean
menu_hide_delay_func(gpointer data
);
95 /* The time for the current event being processed */
96 Time event_curtime
= CurrentTime
;
98 static guint ignore_enter_focus
= 0;
99 static gboolean menu_can_hide
;
100 static gboolean focus_left_screen
= FALSE
;
103 static void ice_handler(gint fd
, gpointer conn
)
106 IceProcessMessages(conn
, NULL
, &b
);
109 static void ice_watch(IceConn conn
, IcePointer data
, Bool opening
,
110 IcePointer
*watch_data
)
115 fd
= IceConnectionNumber(conn
);
116 ob_main_loop_fd_add(ob_main_loop
, fd
, ice_handler
, conn
, NULL
);
118 ob_main_loop_fd_remove(ob_main_loop
, fd
);
124 void event_startup(gboolean reconfig
)
126 if (reconfig
) return;
128 ob_main_loop_x_add(ob_main_loop
, event_process
, NULL
, NULL
);
131 IceAddConnectionWatch(ice_watch
, NULL
);
134 client_add_destructor(focus_delay_client_dest
, NULL
);
137 void event_shutdown(gboolean reconfig
)
139 if (reconfig
) return;
142 IceRemoveConnectionWatch(ice_watch
, NULL
);
145 client_remove_destructor(focus_delay_client_dest
);
148 static Window
event_get_window(XEvent
*e
)
155 window
= RootWindow(ob_display
, ob_screen
);
158 window
= e
->xmap
.window
;
161 window
= e
->xunmap
.window
;
164 window
= e
->xdestroywindow
.window
;
166 case ConfigureRequest
:
167 window
= e
->xconfigurerequest
.window
;
169 case ConfigureNotify
:
170 window
= e
->xconfigure
.window
;
174 if (extensions_xkb
&& e
->type
== extensions_xkb_event_basep
) {
175 switch (((XkbAnyEvent
*)e
)->xkb_type
) {
177 window
= ((XkbBellNotifyEvent
*)e
)->window
;
184 if (extensions_sync
&&
185 e
->type
== extensions_sync_event_basep
+ XSyncAlarmNotify
)
190 window
= e
->xany
.window
;
195 static void event_set_curtime(XEvent
*e
)
197 Time t
= CurrentTime
;
199 /* grab the lasttime and hack up the state */
215 t
= e
->xproperty
.time
;
219 t
= e
->xcrossing
.time
;
223 if (extensions_sync
&&
224 e
->type
== extensions_sync_event_basep
+ XSyncAlarmNotify
)
226 t
= ((XSyncAlarmNotifyEvent
*)e
)->time
;
229 /* if more event types are anticipated, get their timestamp
237 static void event_hack_mods(XEvent
*e
)
240 XkbStateRec xkb_state
;
246 e
->xbutton
.state
= modkeys_only_modifier_masks(e
->xbutton
.state
);
249 e
->xkey
.state
= modkeys_only_modifier_masks(e
->xkey
.state
);
252 e
->xkey
.state
= modkeys_only_modifier_masks(e
->xkey
.state
);
254 if (XkbGetState(ob_display
, XkbUseCoreKbd
, &xkb_state
) == Success
) {
255 e
->xkey
.state
= xkb_state
.compat_state
;
259 /* remove from the state the mask of the modifier key being released,
260 if it is a modifier key being released that is */
261 e
->xkey
.state
&= ~modkeys_keycode_to_mask(e
->xkey
.keycode
);
264 e
->xmotion
.state
= modkeys_only_modifier_masks(e
->xmotion
.state
);
265 /* compress events */
268 while (XCheckTypedWindowEvent(ob_display
, e
->xmotion
.window
,
270 e
->xmotion
.x_root
= ce
.xmotion
.x_root
;
271 e
->xmotion
.y_root
= ce
.xmotion
.y_root
;
278 static gboolean
wanted_focusevent(XEvent
*e
, gboolean in_client_only
)
280 gint mode
= e
->xfocus
.mode
;
281 gint detail
= e
->xfocus
.detail
;
282 Window win
= e
->xany
.window
;
284 if (e
->type
== FocusIn
) {
285 /* These are ones we never want.. */
287 /* This means focus was given by a keyboard/mouse grab. */
288 if (mode
== NotifyGrab
)
290 /* This means focus was given back from a keyboard/mouse grab. */
291 if (mode
== NotifyUngrab
)
294 /* These are the ones we want.. */
296 if (win
== RootWindow(ob_display
, ob_screen
) && !in_client_only
) {
297 /* This means focus reverted off of a client */
298 if (detail
== NotifyPointerRoot
|| detail
== NotifyDetailNone
||
299 detail
== NotifyInferior
)
305 /* This means focus moved from the root window to a client */
306 if (detail
== NotifyVirtual
)
308 /* This means focus moved from one client to another */
309 if (detail
== NotifyNonlinearVirtual
)
311 /* This means focus moved to the frame window */
312 if (detail
== NotifyInferior
&& !in_client_only
)
318 g_assert(e
->type
== FocusOut
);
320 /* These are ones we never want.. */
322 /* This means focus was taken by a keyboard/mouse grab. */
323 if (mode
== NotifyGrab
)
326 /* Focus left the root window revertedto state */
327 if (win
== RootWindow(ob_display
, ob_screen
))
330 /* These are the ones we want.. */
332 /* This means focus moved from a client to the root window */
333 if (detail
== NotifyVirtual
)
335 /* This means focus moved from one client to another */
336 if (detail
== NotifyNonlinearVirtual
)
338 /* This means focus had moved to our frame window and now moved off */
339 if (detail
== NotifyNonlinear
)
347 static Bool
look_for_focusin(Display
*d
, XEvent
*e
, XPointer arg
)
349 return e
->type
== FocusIn
&& wanted_focusevent(e
, FALSE
);
352 static Bool
look_for_focusin_client(Display
*d
, XEvent
*e
, XPointer arg
)
354 return e
->type
== FocusIn
&& wanted_focusevent(e
, TRUE
);
357 static void print_focusevent(XEvent
*e
)
359 gint mode
= e
->xfocus
.mode
;
360 gint detail
= e
->xfocus
.detail
;
361 Window win
= e
->xany
.window
;
362 const gchar
*modestr
, *detailstr
;
365 case NotifyNormal
: modestr
="NotifyNormal"; break;
366 case NotifyGrab
: modestr
="NotifyGrab"; break;
367 case NotifyUngrab
: modestr
="NotifyUngrab"; break;
368 case NotifyWhileGrabbed
: modestr
="NotifyWhileGrabbed"; break;
371 case NotifyAncestor
: detailstr
="NotifyAncestor"; break;
372 case NotifyVirtual
: detailstr
="NotifyVirtual"; break;
373 case NotifyInferior
: detailstr
="NotifyInferior"; break;
374 case NotifyNonlinear
: detailstr
="NotifyNonlinear"; break;
375 case NotifyNonlinearVirtual
: detailstr
="NotifyNonlinearVirtual"; break;
376 case NotifyPointer
: detailstr
="NotifyPointer"; break;
377 case NotifyPointerRoot
: detailstr
="NotifyPointerRoot"; break;
378 case NotifyDetailNone
: detailstr
="NotifyDetailNone"; break;
383 ob_debug_type(OB_DEBUG_FOCUS
, "Focus%s 0x%x mode=%s detail=%s\n",
384 (e
->xfocus
.type
== FocusIn
? "In" : "Out"),
390 static gboolean
event_ignore(XEvent
*e
, ObClient
*client
)
395 if (!wanted_focusevent(e
, FALSE
))
400 if (!wanted_focusevent(e
, FALSE
))
407 static void event_process(const XEvent
*ec
, gpointer data
)
410 ObGroup
*group
= NULL
;
411 ObClient
*client
= NULL
;
413 ObDockApp
*dockapp
= NULL
;
414 ObWindow
*obwin
= NULL
;
416 ObEventData
*ed
= data
;
418 /* make a copy we can mangle */
422 window
= event_get_window(e
);
423 if (!(e
->type
== PropertyNotify
&&
424 (group
= g_hash_table_lookup(group_map
, &window
))))
425 if ((obwin
= g_hash_table_lookup(window_map
, &window
))) {
426 switch (obwin
->type
) {
428 dock
= WINDOW_AS_DOCK(obwin
);
431 dockapp
= WINDOW_AS_DOCKAPP(obwin
);
434 client
= WINDOW_AS_CLIENT(obwin
);
437 case Window_Internal
:
438 /* not to be used for events */
439 g_assert_not_reached();
444 event_set_curtime(e
);
446 if (event_ignore(e
, client
)) {
453 /* deal with it in the kernel */
455 if (menu_frame_visible
&&
456 (e
->type
== EnterNotify
|| e
->type
== LeaveNotify
))
458 /* crossing events for menu */
459 event_handle_menu(e
);
460 } else if (e
->type
== FocusIn
) {
461 if (e
->xfocus
.detail
== NotifyPointerRoot
||
462 e
->xfocus
.detail
== NotifyDetailNone
||
463 e
->xfocus
.detail
== NotifyInferior
)
466 ob_debug_type(OB_DEBUG_FOCUS
,
467 "Focus went to pointer root/none or to our frame "
470 /* If another FocusIn is in the queue then don't fallback yet. This
471 fixes the fun case of:
472 window map -> send focusin
473 window unmap -> get focusout
474 window map -> send focusin
475 get first focus out -> fall back to something (new window
476 hasn't received focus yet, so something else) -> send focusin
477 which means the "something else" is the last thing to get a
478 focusin sent to it, so the new window doesn't end up with focus.
480 But if the other focus in is something like PointerRoot then we
481 still want to fall back.
483 if (XCheckIfEvent(ob_display
, &ce
, look_for_focusin_client
, NULL
)){
484 XPutBackEvent(ob_display
, &ce
);
485 ob_debug_type(OB_DEBUG_FOCUS
,
486 " but another FocusIn is coming\n");
488 /* Focus has been reverted to the root window, nothing, or to
491 FocusOut events come after UnmapNotify, so we don't need to
492 worry about focusing an invalid window
495 /* In this case we know focus is in our screen */
496 if (e
->xfocus
.detail
== NotifyInferior
)
497 focus_left_screen
= FALSE
;
499 if (!focus_left_screen
)
500 focus_fallback(TRUE
);
502 } else if (client
&& client
!= focus_client
) {
503 focus_left_screen
= FALSE
;
504 frame_adjust_focus(client
->frame
, TRUE
);
505 focus_set_client(client
);
506 client_calc_layer(client
);
508 } else if (e
->type
== FocusOut
) {
509 gboolean nomove
= FALSE
;
512 /* Look for the followup FocusIn */
513 if (!XCheckIfEvent(ob_display
, &ce
, look_for_focusin
, NULL
)) {
514 /* There is no FocusIn, this means focus went to a window that
515 is not being managed, or a window on another screen. */
519 xerror_set_ignore(TRUE
);
520 if (XGetInputFocus(ob_display
, &win
, &i
) != 0 &&
521 XGetGeometry(ob_display
, win
, &root
, &i
,&i
,&u
,&u
,&u
,&u
) != 0 &&
522 root
!= RootWindow(ob_display
, ob_screen
))
524 ob_debug_type(OB_DEBUG_FOCUS
,
525 "Focus went to another screen !\n");
526 focus_left_screen
= TRUE
;
529 ob_debug_type(OB_DEBUG_FOCUS
,
530 "Focus went to a black hole !\n");
531 xerror_set_ignore(FALSE
);
532 /* nothing is focused */
533 focus_set_client(NULL
);
534 } else if (ce
.xany
.window
== e
->xany
.window
) {
535 ob_debug_type(OB_DEBUG_FOCUS
, "Focus didn't go anywhere\n");
536 /* If focus didn't actually move anywhere, there is nothing to do*/
539 /* Focus did move, so process the FocusIn event */
540 ObEventData ed
= { .ignored
= FALSE
};
541 event_process(&ce
, &ed
);
543 /* The FocusIn was ignored, this means it was on a window
544 that isn't a client. */
545 ob_debug_type(OB_DEBUG_FOCUS
,
546 "Focus went to an unmanaged window 0x%x !\n",
548 focus_fallback(TRUE
);
552 if (client
&& !nomove
) {
553 frame_adjust_focus(client
->frame
, FALSE
);
554 /* focus_set_client has already been called for sure */
555 client_calc_layer(client
);
558 event_handle_group(group
, e
);
560 event_handle_client(client
, e
);
562 event_handle_dockapp(dockapp
, e
);
564 event_handle_dock(dock
, e
);
565 else if (window
== RootWindow(ob_display
, ob_screen
))
566 event_handle_root(e
);
567 else if (e
->type
== MapRequest
)
568 client_manage(window
);
569 else if (e
->type
== ConfigureRequest
) {
570 /* unhandled configure requests must be used to configure the
574 xwc
.x
= e
->xconfigurerequest
.x
;
575 xwc
.y
= e
->xconfigurerequest
.y
;
576 xwc
.width
= e
->xconfigurerequest
.width
;
577 xwc
.height
= e
->xconfigurerequest
.height
;
578 xwc
.border_width
= e
->xconfigurerequest
.border_width
;
579 xwc
.sibling
= e
->xconfigurerequest
.above
;
580 xwc
.stack_mode
= e
->xconfigurerequest
.detail
;
582 /* we are not to be held responsible if someone sends us an
584 xerror_set_ignore(TRUE
);
585 XConfigureWindow(ob_display
, window
,
586 e
->xconfigurerequest
.value_mask
, &xwc
);
587 xerror_set_ignore(FALSE
);
590 else if (extensions_sync
&&
591 e
->type
== extensions_sync_event_basep
+ XSyncAlarmNotify
)
593 XSyncAlarmNotifyEvent
*se
= (XSyncAlarmNotifyEvent
*)e
;
594 if (se
->alarm
== moveresize_alarm
&& moveresize_in_progress
)
599 if (e
->type
== ButtonPress
|| e
->type
== ButtonRelease
||
600 e
->type
== MotionNotify
|| e
->type
== KeyPress
||
601 e
->type
== KeyRelease
)
603 event_handle_user_input(client
, e
);
606 /* if something happens and it's not from an XEvent, then we don't know
608 event_curtime
= CurrentTime
;
611 static void event_handle_root(XEvent
*e
)
617 ob_debug("Another WM has requested to replace us. Exiting.\n");
622 if (e
->xclient
.format
!= 32) break;
624 msgtype
= e
->xclient
.message_type
;
625 if (msgtype
== prop_atoms
.net_current_desktop
) {
626 guint d
= e
->xclient
.data
.l
[0];
627 if (d
< screen_num_desktops
) {
628 event_curtime
= e
->xclient
.data
.l
[1];
629 if (event_curtime
== 0)
630 ob_debug_type(OB_DEBUG_APP_BUGS
,
631 "_NET_CURRENT_DESKTOP message is missing "
633 screen_set_desktop(d
, TRUE
);
635 } else if (msgtype
== prop_atoms
.net_number_of_desktops
) {
636 guint d
= e
->xclient
.data
.l
[0];
638 screen_set_num_desktops(d
);
639 } else if (msgtype
== prop_atoms
.net_showing_desktop
) {
640 screen_show_desktop(e
->xclient
.data
.l
[0] != 0, TRUE
);
641 } else if (msgtype
== prop_atoms
.openbox_control
) {
642 if (e
->xclient
.data
.l
[0] == 1)
644 else if (e
->xclient
.data
.l
[0] == 2)
649 if (e
->xproperty
.atom
== prop_atoms
.net_desktop_names
)
650 screen_update_desktop_names();
651 else if (e
->xproperty
.atom
== prop_atoms
.net_desktop_layout
)
652 screen_update_layout();
654 case ConfigureNotify
:
656 XRRUpdateConfiguration(e
);
665 static void event_handle_group(ObGroup
*group
, XEvent
*e
)
669 g_assert(e
->type
== PropertyNotify
);
671 for (it
= group
->members
; it
; it
= g_slist_next(it
))
672 event_handle_client(it
->data
, e
);
675 void event_enter_client(ObClient
*client
)
677 g_assert(config_focus_follow
);
679 if (client_normal(client
) && client_can_focus(client
)) {
680 if (config_focus_delay
) {
681 ObFocusDelayData
*data
;
683 ob_main_loop_timeout_remove(ob_main_loop
, focus_delay_func
);
685 data
= g_new(ObFocusDelayData
, 1);
686 data
->client
= client
;
687 data
->time
= event_curtime
;
689 ob_main_loop_timeout_add(ob_main_loop
,
692 data
, focus_delay_cmp
, focus_delay_dest
);
694 ObFocusDelayData data
;
695 data
.client
= client
;
696 data
.time
= event_curtime
;
697 focus_delay_func(&data
);
702 static void event_handle_client(ObClient
*client
, XEvent
*e
)
711 /* Wheel buttons don't draw because they are an instant click, so it
712 is a waste of resources to go drawing it. */
713 if (!(e
->xbutton
.button
== 4 || e
->xbutton
.button
== 5)) {
714 con
= frame_context(client
, e
->xbutton
.window
);
715 con
= mouse_button_frame_context(con
, e
->xbutton
.button
);
717 case OB_FRAME_CONTEXT_MAXIMIZE
:
718 client
->frame
->max_press
= (e
->type
== ButtonPress
);
719 framerender_frame(client
->frame
);
721 case OB_FRAME_CONTEXT_CLOSE
:
722 client
->frame
->close_press
= (e
->type
== ButtonPress
);
723 framerender_frame(client
->frame
);
725 case OB_FRAME_CONTEXT_ICONIFY
:
726 client
->frame
->iconify_press
= (e
->type
== ButtonPress
);
727 framerender_frame(client
->frame
);
729 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
730 client
->frame
->desk_press
= (e
->type
== ButtonPress
);
731 framerender_frame(client
->frame
);
733 case OB_FRAME_CONTEXT_SHADE
:
734 client
->frame
->shade_press
= (e
->type
== ButtonPress
);
735 framerender_frame(client
->frame
);
738 /* nothing changes with clicks for any other contexts */
744 con
= frame_context(client
, e
->xcrossing
.window
);
746 case OB_FRAME_CONTEXT_MAXIMIZE
:
747 client
->frame
->max_hover
= FALSE
;
748 frame_adjust_state(client
->frame
);
750 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
751 client
->frame
->desk_hover
= FALSE
;
752 frame_adjust_state(client
->frame
);
754 case OB_FRAME_CONTEXT_SHADE
:
755 client
->frame
->shade_hover
= FALSE
;
756 frame_adjust_state(client
->frame
);
758 case OB_FRAME_CONTEXT_ICONIFY
:
759 client
->frame
->iconify_hover
= FALSE
;
760 frame_adjust_state(client
->frame
);
762 case OB_FRAME_CONTEXT_CLOSE
:
763 client
->frame
->close_hover
= FALSE
;
764 frame_adjust_state(client
->frame
);
766 case OB_FRAME_CONTEXT_FRAME
:
767 /* When the mouse leaves an animating window, don't use the
768 corresponding enter events. Pretend like the animating window
769 doesn't even exist..! */
770 if (frame_iconify_animating(client
->frame
))
771 event_ignore_queued_enters();
773 ob_debug_type(OB_DEBUG_FOCUS
,
774 "%sNotify mode %d detail %d on %lx\n",
775 (e
->type
== EnterNotify
? "Enter" : "Leave"),
777 e
->xcrossing
.detail
, (client
?client
->window
:0));
778 if (keyboard_interactively_grabbed())
780 if (config_focus_follow
&& config_focus_delay
&&
781 /* leave inferior events can happen when the mouse goes onto
782 the window's border and then into the window before the
784 e
->xcrossing
.detail
!= NotifyInferior
)
786 ob_main_loop_timeout_remove_data(ob_main_loop
,
797 gboolean nofocus
= FALSE
;
799 if (ignore_enter_focus
) {
800 ignore_enter_focus
--;
804 con
= frame_context(client
, e
->xcrossing
.window
);
806 case OB_FRAME_CONTEXT_MAXIMIZE
:
807 client
->frame
->max_hover
= TRUE
;
808 frame_adjust_state(client
->frame
);
810 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
811 client
->frame
->desk_hover
= TRUE
;
812 frame_adjust_state(client
->frame
);
814 case OB_FRAME_CONTEXT_SHADE
:
815 client
->frame
->shade_hover
= TRUE
;
816 frame_adjust_state(client
->frame
);
818 case OB_FRAME_CONTEXT_ICONIFY
:
819 client
->frame
->iconify_hover
= TRUE
;
820 frame_adjust_state(client
->frame
);
822 case OB_FRAME_CONTEXT_CLOSE
:
823 client
->frame
->close_hover
= TRUE
;
824 frame_adjust_state(client
->frame
);
826 case OB_FRAME_CONTEXT_FRAME
:
827 if (keyboard_interactively_grabbed())
829 if (e
->xcrossing
.mode
== NotifyGrab
||
830 e
->xcrossing
.mode
== NotifyUngrab
||
831 /*ignore enters when we're already in the window */
832 e
->xcrossing
.detail
== NotifyInferior
)
834 ob_debug_type(OB_DEBUG_FOCUS
,
835 "%sNotify mode %d detail %d on %lx IGNORED\n",
836 (e
->type
== EnterNotify
? "Enter" : "Leave"),
838 e
->xcrossing
.detail
, client
?client
->window
:0);
840 ob_debug_type(OB_DEBUG_FOCUS
,
841 "%sNotify mode %d detail %d on %lx, "
842 "focusing window: %d\n",
843 (e
->type
== EnterNotify
? "Enter" : "Leave"),
845 e
->xcrossing
.detail
, (client
?client
->window
:0),
847 if (!nofocus
&& config_focus_follow
)
848 event_enter_client(client
);
856 case ConfigureRequest
:
857 /* dont compress these unless you're going to watch for property
858 notifies in between (these can change what the configure would
860 also you can't compress stacking events
863 ob_debug("ConfigureRequest desktop %d wmstate %d vis %d\n",
864 screen_desktop
, client
->wmstate
, client
->frame
->visible
);
866 /* don't allow clients to move shaded windows (fvwm does this) */
867 if (client
->shaded
) {
868 e
->xconfigurerequest
.value_mask
&= ~CWX
;
869 e
->xconfigurerequest
.value_mask
&= ~CWY
;
872 /* resize, then move, as specified in the EWMH section 7.7 */
873 if (e
->xconfigurerequest
.value_mask
& (CWWidth
| CWHeight
|
878 if (e
->xconfigurerequest
.value_mask
& CWBorderWidth
)
879 client
->border_width
= e
->xconfigurerequest
.border_width
;
881 x
= (e
->xconfigurerequest
.value_mask
& CWX
) ?
882 e
->xconfigurerequest
.x
: client
->area
.x
;
883 y
= (e
->xconfigurerequest
.value_mask
& CWY
) ?
884 e
->xconfigurerequest
.y
: client
->area
.y
;
885 w
= (e
->xconfigurerequest
.value_mask
& CWWidth
) ?
886 e
->xconfigurerequest
.width
: client
->area
.width
;
887 h
= (e
->xconfigurerequest
.value_mask
& CWHeight
) ?
888 e
->xconfigurerequest
.height
: client
->area
.height
;
890 ob_debug("ConfigureRequest x %d %d y %d %d\n",
891 e
->xconfigurerequest
.value_mask
& CWX
, x
,
892 e
->xconfigurerequest
.value_mask
& CWY
, y
);
894 /* check for broken apps moving to their root position
896 XXX remove this some day...that would be nice. right now all
897 kde apps do this when they try activate themselves on another
898 desktop. eg. open amarok window on desktop 1, switch to desktop
899 2, click amarok tray icon. it will move by its decoration size.
901 if (x
!= client
->area
.x
&&
902 x
== (client
->frame
->area
.x
+ client
->frame
->size
.left
-
903 (gint
)client
->border_width
) &&
904 y
!= client
->area
.y
&&
905 y
== (client
->frame
->area
.y
+ client
->frame
->size
.top
-
906 (gint
)client
->border_width
))
908 ob_debug_type(OB_DEBUG_APP_BUGS
,
909 "Application %s is trying to move via "
910 "ConfigureRequest to it's root window position "
911 "but it is not using StaticGravity\n",
918 client_find_onscreen(client
, &x
, &y
, w
, h
, FALSE
);
919 client_configure_full(client
, x
, y
, w
, h
, FALSE
, TRUE
, TRUE
);
922 if (e
->xconfigurerequest
.value_mask
& CWStackMode
) {
923 switch (e
->xconfigurerequest
.detail
) {
926 /* Apps are so rude. And this is totally disconnected from
927 activation/focus. Bleh. */
928 /*client_lower(client);*/
934 /* Apps are so rude. And this is totally disconnected from
935 activation/focus. Bleh. */
936 /*client_raise(client);*/
942 if (client
->ignore_unmaps
) {
943 client
->ignore_unmaps
--;
946 ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
948 client
->window
, e
->xunmap
.event
, e
->xunmap
.from_configure
,
949 client
->ignore_unmaps
);
950 client_unmanage(client
);
953 ob_debug("DestroyNotify for window 0x%x\n", client
->window
);
954 client_unmanage(client
);
957 /* this is when the client is first taken captive in the frame */
958 if (e
->xreparent
.parent
== client
->frame
->plate
) break;
961 This event is quite rare and is usually handled in unmapHandler.
962 However, if the window is unmapped when the reparent event occurs,
963 the window manager never sees it because an unmap event is not sent
964 to an already unmapped window.
967 /* we don't want the reparent event, put it back on the stack for the
968 X server to deal with after we unmanage the window */
969 XPutBackEvent(ob_display
, e
);
971 ob_debug("ReparentNotify for window 0x%x\n", client
->window
);
972 client_unmanage(client
);
975 ob_debug("MapRequest for 0x%lx\n", client
->window
);
976 if (!client
->iconic
) break; /* this normally doesn't happen, but if it
977 does, we don't want it!
978 it can happen now when the window is on
979 another desktop, but we still don't
981 client_activate(client
, FALSE
, TRUE
);
984 /* validate cuz we query stuff off the client here */
985 if (!client_validate(client
)) break;
987 if (e
->xclient
.format
!= 32) return;
989 msgtype
= e
->xclient
.message_type
;
990 if (msgtype
== prop_atoms
.wm_change_state
) {
991 /* compress changes into a single change */
992 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
994 /* XXX: it would be nice to compress ALL messages of a
995 type, not just messages in a row without other
996 message types between. */
997 if (ce
.xclient
.message_type
!= msgtype
) {
998 XPutBackEvent(ob_display
, &ce
);
1001 e
->xclient
= ce
.xclient
;
1003 client_set_wm_state(client
, e
->xclient
.data
.l
[0]);
1004 } else if (msgtype
== prop_atoms
.net_wm_desktop
) {
1005 /* compress changes into a single change */
1006 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
1008 /* XXX: it would be nice to compress ALL messages of a
1009 type, not just messages in a row without other
1010 message types between. */
1011 if (ce
.xclient
.message_type
!= msgtype
) {
1012 XPutBackEvent(ob_display
, &ce
);
1015 e
->xclient
= ce
.xclient
;
1017 if ((unsigned)e
->xclient
.data
.l
[0] < screen_num_desktops
||
1018 (unsigned)e
->xclient
.data
.l
[0] == DESKTOP_ALL
)
1019 client_set_desktop(client
, (unsigned)e
->xclient
.data
.l
[0],
1021 } else if (msgtype
== prop_atoms
.net_wm_state
) {
1022 /* can't compress these */
1023 ob_debug("net_wm_state %s %ld %ld for 0x%lx\n",
1024 (e
->xclient
.data
.l
[0] == 0 ? "Remove" :
1025 e
->xclient
.data
.l
[0] == 1 ? "Add" :
1026 e
->xclient
.data
.l
[0] == 2 ? "Toggle" : "INVALID"),
1027 e
->xclient
.data
.l
[1], e
->xclient
.data
.l
[2],
1029 client_set_state(client
, e
->xclient
.data
.l
[0],
1030 e
->xclient
.data
.l
[1], e
->xclient
.data
.l
[2]);
1031 } else if (msgtype
== prop_atoms
.net_close_window
) {
1032 ob_debug("net_close_window for 0x%lx\n", client
->window
);
1033 client_close(client
);
1034 } else if (msgtype
== prop_atoms
.net_active_window
) {
1035 ob_debug("net_active_window for 0x%lx source=%s\n",
1037 (e
->xclient
.data
.l
[0] == 0 ? "unknown" :
1038 (e
->xclient
.data
.l
[0] == 1 ? "application" :
1039 (e
->xclient
.data
.l
[0] == 2 ? "user" : "INVALID"))));
1040 /* XXX make use of data.l[2] !? */
1041 event_curtime
= e
->xclient
.data
.l
[1];
1042 ob_debug_type(OB_DEBUG_APP_BUGS
,
1043 "_NET_ACTIVE_WINDOW message for window %s is "
1044 "missing a timestamp\n", client
->title
);
1045 client_activate(client
, FALSE
,
1046 (e
->xclient
.data
.l
[0] == 0 ||
1047 e
->xclient
.data
.l
[0] == 2));
1048 } else if (msgtype
== prop_atoms
.net_wm_moveresize
) {
1049 ob_debug("net_wm_moveresize for 0x%lx direction %d\n",
1050 client
->window
, e
->xclient
.data
.l
[2]);
1051 if ((Atom
)e
->xclient
.data
.l
[2] ==
1052 prop_atoms
.net_wm_moveresize_size_topleft
||
1053 (Atom
)e
->xclient
.data
.l
[2] ==
1054 prop_atoms
.net_wm_moveresize_size_top
||
1055 (Atom
)e
->xclient
.data
.l
[2] ==
1056 prop_atoms
.net_wm_moveresize_size_topright
||
1057 (Atom
)e
->xclient
.data
.l
[2] ==
1058 prop_atoms
.net_wm_moveresize_size_right
||
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_bottomright
||
1063 (Atom
)e
->xclient
.data
.l
[2] ==
1064 prop_atoms
.net_wm_moveresize_size_bottom
||
1065 (Atom
)e
->xclient
.data
.l
[2] ==
1066 prop_atoms
.net_wm_moveresize_size_bottomleft
||
1067 (Atom
)e
->xclient
.data
.l
[2] ==
1068 prop_atoms
.net_wm_moveresize_size_left
||
1069 (Atom
)e
->xclient
.data
.l
[2] ==
1070 prop_atoms
.net_wm_moveresize_move
||
1071 (Atom
)e
->xclient
.data
.l
[2] ==
1072 prop_atoms
.net_wm_moveresize_size_keyboard
||
1073 (Atom
)e
->xclient
.data
.l
[2] ==
1074 prop_atoms
.net_wm_moveresize_move_keyboard
) {
1076 moveresize_start(client
, e
->xclient
.data
.l
[0],
1077 e
->xclient
.data
.l
[1], e
->xclient
.data
.l
[3],
1078 e
->xclient
.data
.l
[2]);
1080 else if ((Atom
)e
->xclient
.data
.l
[2] ==
1081 prop_atoms
.net_wm_moveresize_cancel
)
1082 moveresize_end(TRUE
);
1083 } else if (msgtype
== prop_atoms
.net_moveresize_window
) {
1084 gint grav
, x
, y
, w
, h
;
1086 if (e
->xclient
.data
.l
[0] & 0xff)
1087 grav
= e
->xclient
.data
.l
[0] & 0xff;
1089 grav
= client
->gravity
;
1091 if (e
->xclient
.data
.l
[0] & 1 << 8)
1092 x
= e
->xclient
.data
.l
[1];
1095 if (e
->xclient
.data
.l
[0] & 1 << 9)
1096 y
= e
->xclient
.data
.l
[2];
1099 if (e
->xclient
.data
.l
[0] & 1 << 10)
1100 w
= e
->xclient
.data
.l
[3];
1102 w
= client
->area
.width
;
1103 if (e
->xclient
.data
.l
[0] & 1 << 11)
1104 h
= e
->xclient
.data
.l
[4];
1106 h
= client
->area
.height
;
1108 ob_debug("MOVERESIZE x %d %d y %d %d\n",
1109 e
->xclient
.data
.l
[0] & 1 << 8, x
,
1110 e
->xclient
.data
.l
[0] & 1 << 9, y
);
1111 client_convert_gravity(client
, grav
, &x
, &y
, w
, h
);
1112 client_find_onscreen(client
, &x
, &y
, w
, h
, FALSE
);
1113 client_configure(client
, x
, y
, w
, h
, FALSE
, TRUE
);
1116 case PropertyNotify
:
1117 /* validate cuz we query stuff off the client here */
1118 if (!client_validate(client
)) break;
1120 /* compress changes to a single property into a single change */
1121 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
1125 /* XXX: it would be nice to compress ALL changes to a property,
1126 not just changes in a row without other props between. */
1128 a
= ce
.xproperty
.atom
;
1129 b
= e
->xproperty
.atom
;
1133 if ((a
== prop_atoms
.net_wm_name
||
1134 a
== prop_atoms
.wm_name
||
1135 a
== prop_atoms
.net_wm_icon_name
||
1136 a
== prop_atoms
.wm_icon_name
)
1138 (b
== prop_atoms
.net_wm_name
||
1139 b
== prop_atoms
.wm_name
||
1140 b
== prop_atoms
.net_wm_icon_name
||
1141 b
== prop_atoms
.wm_icon_name
)) {
1144 if (a
== prop_atoms
.net_wm_icon
&&
1145 b
== prop_atoms
.net_wm_icon
)
1148 XPutBackEvent(ob_display
, &ce
);
1152 msgtype
= e
->xproperty
.atom
;
1153 if (msgtype
== XA_WM_NORMAL_HINTS
) {
1154 client_update_normal_hints(client
);
1155 /* normal hints can make a window non-resizable */
1156 client_setup_decor_and_functions(client
);
1157 } else if (msgtype
== XA_WM_HINTS
) {
1158 client_update_wmhints(client
);
1159 } else if (msgtype
== XA_WM_TRANSIENT_FOR
) {
1160 client_update_transient_for(client
);
1161 client_get_type(client
);
1162 /* type may have changed, so update the layer */
1163 client_calc_layer(client
);
1164 client_setup_decor_and_functions(client
);
1165 } else if (msgtype
== prop_atoms
.net_wm_name
||
1166 msgtype
== prop_atoms
.wm_name
||
1167 msgtype
== prop_atoms
.net_wm_icon_name
||
1168 msgtype
== prop_atoms
.wm_icon_name
) {
1169 client_update_title(client
);
1170 } else if (msgtype
== prop_atoms
.wm_protocols
) {
1171 client_update_protocols(client
);
1172 client_setup_decor_and_functions(client
);
1174 else if (msgtype
== prop_atoms
.net_wm_strut
) {
1175 client_update_strut(client
);
1177 else if (msgtype
== prop_atoms
.net_wm_icon
) {
1178 client_update_icons(client
);
1180 else if (msgtype
== prop_atoms
.net_wm_icon_geometry
) {
1181 client_update_icon_geometry(client
);
1183 else if (msgtype
== prop_atoms
.net_wm_user_time
) {
1184 client_update_user_time(client
);
1187 else if (msgtype
== prop_atoms
.net_wm_sync_request_counter
) {
1188 client_update_sync_request_counter(client
);
1191 case ColormapNotify
:
1192 client_update_colormap(client
, e
->xcolormap
.colormap
);
1197 if (extensions_shape
&& e
->type
== extensions_shape_event_basep
) {
1198 client
->shaped
= ((XShapeEvent
*)e
)->shaped
;
1199 frame_adjust_shape(client
->frame
);
1205 static void event_handle_dock(ObDock
*s
, XEvent
*e
)
1209 if (e
->xbutton
.button
== 1)
1210 stacking_raise(DOCK_AS_WINDOW(s
));
1211 else if (e
->xbutton
.button
== 2)
1212 stacking_lower(DOCK_AS_WINDOW(s
));
1223 static void event_handle_dockapp(ObDockApp
*app
, XEvent
*e
)
1227 dock_app_drag(app
, &e
->xmotion
);
1230 if (app
->ignore_unmaps
) {
1231 app
->ignore_unmaps
--;
1234 dock_remove(app
, TRUE
);
1237 dock_remove(app
, FALSE
);
1239 case ReparentNotify
:
1240 dock_remove(app
, FALSE
);
1242 case ConfigureNotify
:
1243 dock_app_configure(app
, e
->xconfigure
.width
, e
->xconfigure
.height
);
1248 static ObMenuFrame
* find_active_menu()
1251 ObMenuFrame
*ret
= NULL
;
1253 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
1262 static ObMenuFrame
* find_active_or_last_menu()
1264 ObMenuFrame
*ret
= NULL
;
1266 ret
= find_active_menu();
1267 if (!ret
&& menu_frame_visible
)
1268 ret
= menu_frame_visible
->data
;
1272 static gboolean
event_handle_menu_keyboard(XEvent
*ev
)
1274 guint keycode
, state
;
1277 gboolean ret
= TRUE
;
1279 keycode
= ev
->xkey
.keycode
;
1280 state
= ev
->xkey
.state
;
1281 unikey
= translate_unichar(keycode
);
1283 frame
= find_active_or_last_menu();
1287 else if (keycode
== ob_keycode(OB_KEY_ESCAPE
) && state
== 0) {
1288 /* Escape closes the active menu */
1289 menu_frame_hide(frame
);
1292 else if (keycode
== ob_keycode(OB_KEY_RETURN
) && (state
== 0 ||
1293 state
== ControlMask
))
1295 /* Enter runs the active item or goes into the submenu.
1296 Control-Enter runs it without closing the menu. */
1298 menu_frame_select_next(frame
->child
);
1300 menu_entry_frame_execute(frame
->selected
, state
, ev
->xkey
.time
);
1303 else if (keycode
== ob_keycode(OB_KEY_LEFT
) && ev
->xkey
.state
== 0) {
1304 /* Left goes to the parent menu */
1305 menu_frame_select(frame
, NULL
, TRUE
);
1308 else if (keycode
== ob_keycode(OB_KEY_RIGHT
) && ev
->xkey
.state
== 0) {
1309 /* Right goes to the selected submenu */
1310 if (frame
->child
) menu_frame_select_next(frame
->child
);
1313 else if (keycode
== ob_keycode(OB_KEY_UP
) && state
== 0) {
1314 menu_frame_select_previous(frame
);
1317 else if (keycode
== ob_keycode(OB_KEY_DOWN
) && state
== 0) {
1318 menu_frame_select_next(frame
);
1321 /* keyboard accelerator shortcuts. */
1322 else if (ev
->xkey
.state
== 0 &&
1323 /* was it a valid key? */
1325 /* don't bother if the menu is empty. */
1330 ObMenuEntryFrame
*found
= NULL
;
1331 guint num_found
= 0;
1333 /* start after the selected one */
1334 start
= frame
->entries
;
1335 if (frame
->selected
) {
1336 for (it
= start
; frame
->selected
!= it
->data
; it
= g_list_next(it
))
1337 g_assert(it
!= NULL
); /* nothing was selected? */
1338 /* next with wraparound */
1339 start
= g_list_next(it
);
1340 if (start
== NULL
) start
= frame
->entries
;
1345 ObMenuEntryFrame
*e
= it
->data
;
1346 gunichar entrykey
= 0;
1348 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1349 entrykey
= e
->entry
->data
.normal
.shortcut
;
1350 else if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1351 entrykey
= e
->entry
->data
.submenu
.submenu
->shortcut
;
1353 if (unikey
== entrykey
) {
1354 if (found
== NULL
) found
= e
;
1358 /* next with wraparound */
1359 it
= g_list_next(it
);
1360 if (it
== NULL
) it
= frame
->entries
;
1361 } while (it
!= start
);
1364 if (found
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1367 menu_frame_select(frame
, found
, TRUE
);
1368 usleep(50000); /* highlight the item for a short bit so the
1369 user can see what happened */
1370 menu_entry_frame_execute(found
, state
, ev
->xkey
.time
);
1372 menu_frame_select(frame
, found
, TRUE
);
1374 menu_frame_select_next(frame
->child
);
1385 static gboolean
event_handle_menu(XEvent
*ev
)
1388 ObMenuEntryFrame
*e
;
1389 gboolean ret
= TRUE
;
1393 if ((ev
->xbutton
.button
< 4 || ev
->xbutton
.button
> 5)
1396 if ((e
= menu_entry_frame_under(ev
->xbutton
.x_root
,
1397 ev
->xbutton
.y_root
)))
1398 menu_entry_frame_execute(e
, ev
->xbutton
.state
,
1401 menu_frame_hide_all();
1405 if ((e
= g_hash_table_lookup(menu_frame_map
, &ev
->xcrossing
.window
))) {
1406 if (e
->ignore_enters
)
1409 menu_frame_select(e
->frame
, e
, FALSE
);
1413 if ((e
= g_hash_table_lookup(menu_frame_map
, &ev
->xcrossing
.window
)) &&
1414 (f
= find_active_menu()) && f
->selected
== e
&&
1415 e
->entry
->type
!= OB_MENU_ENTRY_TYPE_SUBMENU
)
1417 menu_frame_select(e
->frame
, NULL
, FALSE
);
1420 if ((e
= menu_entry_frame_under(ev
->xmotion
.x_root
,
1421 ev
->xmotion
.y_root
)))
1422 menu_frame_select(e
->frame
, e
, FALSE
);
1425 ret
= event_handle_menu_keyboard(ev
);
1431 static void event_handle_user_input(ObClient
*client
, XEvent
*e
)
1433 g_assert(e
->type
== ButtonPress
|| e
->type
== ButtonRelease
||
1434 e
->type
== MotionNotify
|| e
->type
== KeyPress
||
1435 e
->type
== KeyRelease
);
1437 if (menu_frame_visible
) {
1438 if (event_handle_menu(e
))
1439 /* don't use the event if the menu used it, but if the menu
1440 didn't use it and it's a keypress that is bound, it will
1441 close the menu and be used */
1445 /* if the keyboard interactive action uses the event then dont
1446 use it for bindings. likewise is moveresize uses the event. */
1447 if (!keyboard_process_interactive_grab(e
, &client
) &&
1448 !(moveresize_in_progress
&& moveresize_event(e
)))
1450 if (moveresize_in_progress
)
1451 /* make further actions work on the client being
1453 client
= moveresize_client
;
1455 menu_can_hide
= FALSE
;
1456 ob_main_loop_timeout_add(ob_main_loop
,
1457 config_menu_hide_delay
* 1000,
1458 menu_hide_delay_func
,
1459 NULL
, g_direct_equal
, NULL
);
1461 if (e
->type
== ButtonPress
||
1462 e
->type
== ButtonRelease
||
1463 e
->type
== MotionNotify
)
1465 /* the frame may not be "visible" but they can still click on it
1466 in the case where it is animating before disappearing */
1467 if (!client
|| !frame_iconify_animating(client
->frame
))
1468 mouse_event(client
, e
);
1469 } else if (e
->type
== KeyPress
) {
1470 keyboard_event((focus_cycle_target
? focus_cycle_target
:
1471 (client
? client
: focus_client
)), e
);
1476 static gboolean
menu_hide_delay_func(gpointer data
)
1478 menu_can_hide
= TRUE
;
1479 return FALSE
; /* no repeat */
1482 static void focus_delay_dest(gpointer data
)
1487 static gboolean
focus_delay_cmp(gconstpointer d1
, gconstpointer d2
)
1489 const ObFocusDelayData
*f1
= d1
;
1490 return f1
->client
== d2
;
1493 static gboolean
focus_delay_func(gpointer data
)
1495 ObFocusDelayData
*d
= data
;
1496 Time old
= event_curtime
;
1498 event_curtime
= d
->time
;
1499 if (focus_client
!= d
->client
) {
1500 if (client_focus(d
->client
) && config_focus_raise
)
1501 client_raise(d
->client
);
1503 event_curtime
= old
;
1504 return FALSE
; /* no repeat */
1507 static void focus_delay_client_dest(ObClient
*client
, gpointer data
)
1509 ob_main_loop_timeout_remove_data(ob_main_loop
, focus_delay_func
,
1513 void event_halt_focus_delay()
1515 ob_main_loop_timeout_remove(ob_main_loop
, focus_delay_func
);
1518 void event_ignore_queued_enters()
1520 GSList
*saved
= NULL
, *it
;
1523 XSync(ob_display
, FALSE
);
1525 /* count the events */
1527 e
= g_new(XEvent
, 1);
1528 if (XCheckTypedEvent(ob_display
, EnterNotify
, e
)) {
1531 win
= g_hash_table_lookup(window_map
, &e
->xany
.window
);
1532 if (win
&& WINDOW_IS_CLIENT(win
))
1533 ++ignore_enter_focus
;
1535 saved
= g_slist_append(saved
, e
);
1541 /* put the events back */
1542 for (it
= saved
; it
; it
= g_slist_next(it
)) {
1543 XPutBackEvent(ob_display
, it
->data
);
1546 g_slist_free(saved
);
1549 gboolean
event_time_after(Time t1
, Time t2
)
1551 g_assert(t1
!= CurrentTime
);
1552 g_assert(t2
!= CurrentTime
);
1555 Timestamp values wrap around (after about 49.7 days). The server, given
1556 its current time is represented by timestamp T, always interprets
1557 timestamps from clients by treating half of the timestamp space as being
1558 later in time than T.
1559 - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
1562 /* TIME_HALF is half of the number space of a Time type variable */
1563 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
1565 if (t2
>= TIME_HALF
)
1566 /* t2 is in the second half so t1 might wrap around and be smaller than
1568 return t1
>= t2
|| t1
< (t2
+ TIME_HALF
);
1570 /* t2 is in the first half so t1 has to come after it */
1571 return t1
>= t2
&& t1
< (t2
+ TIME_HALF
);