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
);
87 static void focus_delay_dest(gpointer data
);
88 static gboolean
focus_delay_cmp(gconstpointer d1
, gconstpointer d2
);
89 static gboolean
focus_delay_func(gpointer data
);
90 static void focus_delay_client_dest(ObClient
*client
, gpointer data
);
92 static gboolean
menu_hide_delay_func(gpointer data
);
94 /* The time for the current event being processed */
95 Time event_curtime
= CurrentTime
;
97 static guint ignore_enter_focus
= 0;
98 static gboolean menu_can_hide
;
99 static gboolean focus_left_screen
= FALSE
;
102 static void ice_handler(gint fd
, gpointer conn
)
105 IceProcessMessages(conn
, NULL
, &b
);
108 static void ice_watch(IceConn conn
, IcePointer data
, Bool opening
,
109 IcePointer
*watch_data
)
114 fd
= IceConnectionNumber(conn
);
115 ob_main_loop_fd_add(ob_main_loop
, fd
, ice_handler
, conn
, NULL
);
117 ob_main_loop_fd_remove(ob_main_loop
, fd
);
123 void event_startup(gboolean reconfig
)
125 if (reconfig
) return;
127 ob_main_loop_x_add(ob_main_loop
, event_process
, NULL
, NULL
);
130 IceAddConnectionWatch(ice_watch
, NULL
);
133 client_add_destructor(focus_delay_client_dest
, NULL
);
136 void event_shutdown(gboolean reconfig
)
138 if (reconfig
) return;
141 IceRemoveConnectionWatch(ice_watch
, NULL
);
144 client_remove_destructor(focus_delay_client_dest
);
147 static Window
event_get_window(XEvent
*e
)
154 window
= RootWindow(ob_display
, ob_screen
);
157 window
= e
->xmap
.window
;
160 window
= e
->xunmap
.window
;
163 window
= e
->xdestroywindow
.window
;
165 case ConfigureRequest
:
166 window
= e
->xconfigurerequest
.window
;
168 case ConfigureNotify
:
169 window
= e
->xconfigure
.window
;
173 if (extensions_xkb
&& e
->type
== extensions_xkb_event_basep
) {
174 switch (((XkbAnyEvent
*)e
)->xkb_type
) {
176 window
= ((XkbBellNotifyEvent
*)e
)->window
;
183 if (extensions_sync
&&
184 e
->type
== extensions_sync_event_basep
+ XSyncAlarmNotify
)
189 window
= e
->xany
.window
;
194 static void event_set_curtime(XEvent
*e
)
196 Time t
= CurrentTime
;
198 /* grab the lasttime and hack up the state */
214 t
= e
->xproperty
.time
;
218 t
= e
->xcrossing
.time
;
222 if (extensions_sync
&&
223 e
->type
== extensions_sync_event_basep
+ XSyncAlarmNotify
)
225 t
= ((XSyncAlarmNotifyEvent
*)e
)->time
;
228 /* if more event types are anticipated, get their timestamp
236 static void event_hack_mods(XEvent
*e
)
239 XkbStateRec xkb_state
;
245 e
->xbutton
.state
= modkeys_only_modifier_masks(e
->xbutton
.state
);
248 e
->xkey
.state
= modkeys_only_modifier_masks(e
->xkey
.state
);
251 e
->xkey
.state
= modkeys_only_modifier_masks(e
->xkey
.state
);
253 if (XkbGetState(ob_display
, XkbUseCoreKbd
, &xkb_state
) == Success
) {
254 e
->xkey
.state
= xkb_state
.compat_state
;
258 /* remove from the state the mask of the modifier key being released,
259 if it is a modifier key being released that is */
260 e
->xkey
.state
&= ~modkeys_keycode_to_mask(e
->xkey
.keycode
);
263 e
->xmotion
.state
= modkeys_only_modifier_masks(e
->xmotion
.state
);
264 /* compress events */
267 while (XCheckTypedWindowEvent(ob_display
, e
->xmotion
.window
,
269 e
->xmotion
.x_root
= ce
.xmotion
.x_root
;
270 e
->xmotion
.y_root
= ce
.xmotion
.y_root
;
277 static gboolean
wanted_focusevent(XEvent
*e
)
279 gint mode
= e
->xfocus
.mode
;
280 gint detail
= e
->xfocus
.detail
;
281 Window win
= e
->xany
.window
;
283 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
)) {
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
)
315 g_assert(e
->type
== FocusOut
);
318 /* These are ones we never want.. */
320 /* This means focus was taken by a keyboard/mouse grab. */
321 if (mode
== NotifyGrab
)
324 /* Focus left the root window revertedto state */
325 if (win
== RootWindow(ob_display
, ob_screen
))
328 /* These are the ones we want.. */
330 /* This means focus moved from a client to the root window */
331 if (detail
== NotifyVirtual
)
333 /* This means focus moved from one client to another */
334 if (detail
== NotifyNonlinearVirtual
)
336 /* This means focus had moved to our frame window and now moved off */
337 if (detail
== NotifyNonlinear
)
345 static Bool
look_for_focusin(Display
*d
, XEvent
*e
, XPointer arg
)
347 return e
->type
== FocusIn
&& wanted_focusevent(e
);
350 static gboolean
event_ignore(XEvent
*e
, ObClient
*client
)
354 if (!wanted_focusevent(e
))
358 if (!wanted_focusevent(e
))
365 static void event_process(const XEvent
*ec
, gpointer data
)
368 ObGroup
*group
= NULL
;
369 ObClient
*client
= NULL
;
371 ObDockApp
*dockapp
= NULL
;
372 ObWindow
*obwin
= NULL
;
374 ObEventData
*ed
= data
;
376 /* make a copy we can mangle */
380 window
= event_get_window(e
);
381 if (!(e
->type
== PropertyNotify
&&
382 (group
= g_hash_table_lookup(group_map
, &window
))))
383 if ((obwin
= g_hash_table_lookup(window_map
, &window
))) {
384 switch (obwin
->type
) {
386 dock
= WINDOW_AS_DOCK(obwin
);
389 dockapp
= WINDOW_AS_DOCKAPP(obwin
);
392 client
= WINDOW_AS_CLIENT(obwin
);
395 case Window_Internal
:
396 /* not to be used for events */
397 g_assert_not_reached();
402 event_set_curtime(e
);
404 if (event_ignore(e
, client
)) {
411 /* deal with it in the kernel */
413 if (menu_frame_visible
&&
414 (e
->type
== EnterNotify
|| e
->type
== LeaveNotify
))
416 /* crossing events for menu */
417 event_handle_menu(e
);
418 } else if (e
->type
== FocusIn
) {
419 if (e
->xfocus
.detail
== NotifyPointerRoot
||
420 e
->xfocus
.detail
== NotifyDetailNone
)
422 ob_debug_type(OB_DEBUG_FOCUS
, "Focus went to pointer root/none\n");
423 /* Focus has been reverted to the root window or nothing.
424 FocusOut events come after UnmapNotify, so we don't need to
425 worry about focusing an invalid window
427 if (!focus_left_screen
)
428 focus_fallback(TRUE
);
429 } else if (e
->xfocus
.detail
== NotifyInferior
) {
430 ob_debug_type(OB_DEBUG_FOCUS
,
431 "Focus went to root or our frame window");
432 /* Focus has been given to the root window. */
433 focus_fallback(TRUE
);
434 } else if (client
&& client
!= focus_client
) {
435 focus_left_screen
= FALSE
;
436 frame_adjust_focus(client
->frame
, TRUE
);
437 focus_set_client(client
);
438 client_calc_layer(client
);
440 } else if (e
->type
== FocusOut
) {
441 gboolean nomove
= FALSE
;
444 ob_debug_type(OB_DEBUG_FOCUS
, "FocusOut Event\n");
446 /* Look for the followup FocusIn */
447 if (!XCheckIfEvent(ob_display
, &ce
, look_for_focusin
, NULL
)) {
448 /* There is no FocusIn, this means focus went to a window that
449 is not being managed, or a window on another screen. */
453 xerror_set_ignore(TRUE
);
454 if (XGetInputFocus(ob_display
, &win
, &i
) != 0 &&
455 XGetGeometry(ob_display
, win
, &root
, &i
,&i
,&u
,&u
,&u
,&u
) != 0 &&
456 root
!= RootWindow(ob_display
, ob_screen
))
458 ob_debug_type(OB_DEBUG_FOCUS
,
459 "Focus went to another screen !\n");
460 focus_left_screen
= TRUE
;
463 ob_debug_type(OB_DEBUG_FOCUS
,
464 "Focus went to a black hole !\n");
465 xerror_set_ignore(FALSE
);
466 /* nothing is focused */
467 focus_set_client(NULL
);
468 } else if (ce
.xany
.window
== e
->xany
.window
) {
469 ob_debug_type(OB_DEBUG_FOCUS
, "Focus didn't go anywhere\n");
470 /* If focus didn't actually move anywhere, there is nothing to do*/
473 /* Focus did move, so process the FocusIn event */
474 ObEventData ed
= { .ignored
= FALSE
};
475 event_process(&ce
, &ed
);
477 /* The FocusIn was ignored, this means it was on a window
478 that isn't a client. */
479 ob_debug_type(OB_DEBUG_FOCUS
,
480 "Focus went to an unmanaged window 0x%x !\n",
482 focus_fallback(TRUE
);
486 if (client
&& !nomove
) {
487 frame_adjust_focus(client
->frame
, FALSE
);
488 /* focus_set_client has already been called for sure */
489 client_calc_layer(client
);
492 event_handle_group(group
, e
);
494 event_handle_client(client
, e
);
496 event_handle_dockapp(dockapp
, e
);
498 event_handle_dock(dock
, e
);
499 else if (window
== RootWindow(ob_display
, ob_screen
))
500 event_handle_root(e
);
501 else if (e
->type
== MapRequest
)
502 client_manage(window
);
503 else if (e
->type
== ConfigureRequest
) {
504 /* unhandled configure requests must be used to configure the
508 xwc
.x
= e
->xconfigurerequest
.x
;
509 xwc
.y
= e
->xconfigurerequest
.y
;
510 xwc
.width
= e
->xconfigurerequest
.width
;
511 xwc
.height
= e
->xconfigurerequest
.height
;
512 xwc
.border_width
= e
->xconfigurerequest
.border_width
;
513 xwc
.sibling
= e
->xconfigurerequest
.above
;
514 xwc
.stack_mode
= e
->xconfigurerequest
.detail
;
516 /* we are not to be held responsible if someone sends us an
518 xerror_set_ignore(TRUE
);
519 XConfigureWindow(ob_display
, window
,
520 e
->xconfigurerequest
.value_mask
, &xwc
);
521 xerror_set_ignore(FALSE
);
524 else if (extensions_sync
&&
525 e
->type
== extensions_sync_event_basep
+ XSyncAlarmNotify
)
527 XSyncAlarmNotifyEvent
*se
= (XSyncAlarmNotifyEvent
*)e
;
528 if (se
->alarm
== moveresize_alarm
&& moveresize_in_progress
)
533 /* user input (action-bound) events */
534 if (e
->type
== ButtonPress
|| e
->type
== ButtonRelease
||
535 e
->type
== MotionNotify
|| e
->type
== KeyPress
||
536 e
->type
== KeyRelease
)
538 gboolean useevent
= TRUE
;
540 if (menu_frame_visible
) {
541 if (event_handle_menu(e
))
542 /* don't use the event if the menu used it, but if the menu
543 didn't use it and it's a keypress that is bound, it will
544 close the menu and be used */
549 /* if the keyboard interactive action uses the event then dont
550 use it for bindings. likewise is moveresize uses the event. */
551 if (!keyboard_process_interactive_grab(e
, &client
) &&
552 !(moveresize_in_progress
&& moveresize_event(e
)))
554 if (moveresize_in_progress
)
555 /* make further actions work on the client being
557 client
= moveresize_client
;
560 menu_can_hide
= FALSE
;
561 ob_main_loop_timeout_add(ob_main_loop
,
562 config_menu_hide_delay
* 1000,
563 menu_hide_delay_func
,
564 NULL
, g_direct_equal
, NULL
);
566 if (e
->type
== ButtonPress
|| e
->type
== ButtonRelease
||
567 e
->type
== MotionNotify
) {
568 mouse_event(client
, e
);
569 } else if (e
->type
== KeyPress
) {
570 keyboard_event((focus_cycle_target
? focus_cycle_target
:
571 (client
? client
: focus_client
)), e
);
576 /* if something happens and it's not from an XEvent, then we don't know
578 event_curtime
= CurrentTime
;
581 static void event_handle_root(XEvent
*e
)
587 ob_debug("Another WM has requested to replace us. Exiting.\n");
592 if (e
->xclient
.format
!= 32) break;
594 msgtype
= e
->xclient
.message_type
;
595 if (msgtype
== prop_atoms
.net_current_desktop
) {
596 guint d
= e
->xclient
.data
.l
[0];
597 if (d
< screen_num_desktops
) {
598 event_curtime
= e
->xclient
.data
.l
[1];
599 ob_debug("SWITCH DESKTOP TIME: %d\n", event_curtime
);
600 screen_set_desktop(d
);
602 } else if (msgtype
== prop_atoms
.net_number_of_desktops
) {
603 guint d
= e
->xclient
.data
.l
[0];
605 screen_set_num_desktops(d
);
606 } else if (msgtype
== prop_atoms
.net_showing_desktop
) {
607 screen_show_desktop(e
->xclient
.data
.l
[0] != 0, TRUE
);
608 } else if (msgtype
== prop_atoms
.ob_control
) {
609 if (e
->xclient
.data
.l
[0] == 1)
611 else if (e
->xclient
.data
.l
[0] == 2)
616 if (e
->xproperty
.atom
== prop_atoms
.net_desktop_names
)
617 screen_update_desktop_names();
618 else if (e
->xproperty
.atom
== prop_atoms
.net_desktop_layout
)
619 screen_update_layout();
621 case ConfigureNotify
:
623 XRRUpdateConfiguration(e
);
632 static void event_handle_group(ObGroup
*group
, XEvent
*e
)
636 g_assert(e
->type
== PropertyNotify
);
638 for (it
= group
->members
; it
; it
= g_slist_next(it
))
639 event_handle_client(it
->data
, e
);
642 void event_enter_client(ObClient
*client
)
644 g_assert(config_focus_follow
);
646 if (client_normal(client
) && client_can_focus(client
)) {
647 if (config_focus_delay
) {
648 ObFocusDelayData
*data
;
650 ob_main_loop_timeout_remove(ob_main_loop
, focus_delay_func
);
652 data
= g_new(ObFocusDelayData
, 1);
653 data
->client
= client
;
654 data
->time
= event_curtime
;
656 ob_main_loop_timeout_add(ob_main_loop
,
659 data
, focus_delay_cmp
, focus_delay_dest
);
661 ObFocusDelayData data
;
662 data
.client
= client
;
663 data
.time
= event_curtime
;
664 focus_delay_func(&data
);
669 static void event_handle_client(ObClient
*client
, XEvent
*e
)
679 /* Wheel buttons don't draw because they are an instant click, so it
680 is a waste of resources to go drawing it. */
681 if (!(e
->xbutton
.button
== 4 || e
->xbutton
.button
== 5)) {
682 con
= frame_context(client
, e
->xbutton
.window
);
683 con
= mouse_button_frame_context(con
, e
->xbutton
.button
);
685 case OB_FRAME_CONTEXT_MAXIMIZE
:
686 client
->frame
->max_press
= (e
->type
== ButtonPress
);
687 framerender_frame(client
->frame
);
689 case OB_FRAME_CONTEXT_CLOSE
:
690 client
->frame
->close_press
= (e
->type
== ButtonPress
);
691 framerender_frame(client
->frame
);
693 case OB_FRAME_CONTEXT_ICONIFY
:
694 client
->frame
->iconify_press
= (e
->type
== ButtonPress
);
695 framerender_frame(client
->frame
);
697 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
698 client
->frame
->desk_press
= (e
->type
== ButtonPress
);
699 framerender_frame(client
->frame
);
701 case OB_FRAME_CONTEXT_SHADE
:
702 client
->frame
->shade_press
= (e
->type
== ButtonPress
);
703 framerender_frame(client
->frame
);
706 /* nothing changes with clicks for any other contexts */
712 con
= frame_context(client
, e
->xcrossing
.window
);
714 case OB_FRAME_CONTEXT_MAXIMIZE
:
715 client
->frame
->max_hover
= FALSE
;
716 frame_adjust_state(client
->frame
);
718 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
719 client
->frame
->desk_hover
= FALSE
;
720 frame_adjust_state(client
->frame
);
722 case OB_FRAME_CONTEXT_SHADE
:
723 client
->frame
->shade_hover
= FALSE
;
724 frame_adjust_state(client
->frame
);
726 case OB_FRAME_CONTEXT_ICONIFY
:
727 client
->frame
->iconify_hover
= FALSE
;
728 frame_adjust_state(client
->frame
);
730 case OB_FRAME_CONTEXT_CLOSE
:
731 client
->frame
->close_hover
= FALSE
;
732 frame_adjust_state(client
->frame
);
734 case OB_FRAME_CONTEXT_FRAME
:
735 ob_debug_type(OB_DEBUG_FOCUS
,
736 "%sNotify mode %d detail %d on %lx\n",
737 (e
->type
== EnterNotify
? "Enter" : "Leave"),
739 e
->xcrossing
.detail
, (client
?client
->window
:0));
740 if (keyboard_interactively_grabbed())
742 if (config_focus_follow
&& config_focus_delay
&&
743 /* leave inferior events can happen when the mouse goes onto
744 the window's border and then into the window before the
746 e
->xcrossing
.detail
!= NotifyInferior
)
748 ob_main_loop_timeout_remove_data(ob_main_loop
,
759 gboolean nofocus
= FALSE
;
761 if (ignore_enter_focus
) {
762 ignore_enter_focus
--;
766 con
= frame_context(client
, e
->xcrossing
.window
);
768 case OB_FRAME_CONTEXT_MAXIMIZE
:
769 client
->frame
->max_hover
= TRUE
;
770 frame_adjust_state(client
->frame
);
772 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
773 client
->frame
->desk_hover
= TRUE
;
774 frame_adjust_state(client
->frame
);
776 case OB_FRAME_CONTEXT_SHADE
:
777 client
->frame
->shade_hover
= TRUE
;
778 frame_adjust_state(client
->frame
);
780 case OB_FRAME_CONTEXT_ICONIFY
:
781 client
->frame
->iconify_hover
= TRUE
;
782 frame_adjust_state(client
->frame
);
784 case OB_FRAME_CONTEXT_CLOSE
:
785 client
->frame
->close_hover
= TRUE
;
786 frame_adjust_state(client
->frame
);
788 case OB_FRAME_CONTEXT_FRAME
:
789 if (keyboard_interactively_grabbed())
791 if (e
->xcrossing
.mode
== NotifyGrab
||
792 e
->xcrossing
.mode
== NotifyUngrab
||
793 /*ignore enters when we're already in the window */
794 e
->xcrossing
.detail
== NotifyInferior
)
796 ob_debug_type(OB_DEBUG_FOCUS
,
797 "%sNotify mode %d detail %d on %lx IGNORED\n",
798 (e
->type
== EnterNotify
? "Enter" : "Leave"),
800 e
->xcrossing
.detail
, client
?client
->window
:0);
802 ob_debug_type(OB_DEBUG_FOCUS
,
803 "%sNotify mode %d detail %d on %lx, "
804 "focusing window: %d\n",
805 (e
->type
== EnterNotify
? "Enter" : "Leave"),
807 e
->xcrossing
.detail
, (client
?client
->window
:0),
809 if (!nofocus
&& config_focus_follow
)
810 event_enter_client(client
);
818 case ConfigureRequest
:
820 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
821 ConfigureRequest
, &ce
)) {
823 /* XXX if this causes bad things.. we can compress config req's
824 with the same mask. */
825 e
->xconfigurerequest
.value_mask
|=
826 ce
.xconfigurerequest
.value_mask
;
827 if (ce
.xconfigurerequest
.value_mask
& CWX
)
828 e
->xconfigurerequest
.x
= ce
.xconfigurerequest
.x
;
829 if (ce
.xconfigurerequest
.value_mask
& CWY
)
830 e
->xconfigurerequest
.y
= ce
.xconfigurerequest
.y
;
831 if (ce
.xconfigurerequest
.value_mask
& CWWidth
)
832 e
->xconfigurerequest
.width
= ce
.xconfigurerequest
.width
;
833 if (ce
.xconfigurerequest
.value_mask
& CWHeight
)
834 e
->xconfigurerequest
.height
= ce
.xconfigurerequest
.height
;
835 if (ce
.xconfigurerequest
.value_mask
& CWBorderWidth
)
836 e
->xconfigurerequest
.border_width
=
837 ce
.xconfigurerequest
.border_width
;
838 if (ce
.xconfigurerequest
.value_mask
& CWStackMode
)
839 e
->xconfigurerequest
.detail
= ce
.xconfigurerequest
.detail
;
842 /* if we are iconic (or shaded (fvwm does this)) ignore the event */
843 if (client
->iconic
|| client
->shaded
) return;
845 /* resize, then move, as specified in the EWMH section 7.7 */
846 if (e
->xconfigurerequest
.value_mask
& (CWWidth
| CWHeight
|
851 if (e
->xconfigurerequest
.value_mask
& CWBorderWidth
)
852 client
->border_width
= e
->xconfigurerequest
.border_width
;
854 x
= (e
->xconfigurerequest
.value_mask
& CWX
) ?
855 e
->xconfigurerequest
.x
: client
->area
.x
;
856 y
= (e
->xconfigurerequest
.value_mask
& CWY
) ?
857 e
->xconfigurerequest
.y
: client
->area
.y
;
858 w
= (e
->xconfigurerequest
.value_mask
& CWWidth
) ?
859 e
->xconfigurerequest
.width
: client
->area
.width
;
860 h
= (e
->xconfigurerequest
.value_mask
& CWHeight
) ?
861 e
->xconfigurerequest
.height
: client
->area
.height
;
863 ob_debug("ConfigureRequest x %d %d y %d %d\n",
864 e
->xconfigurerequest
.value_mask
& CWX
, x
,
865 e
->xconfigurerequest
.value_mask
& CWY
, y
);
867 client_find_onscreen(client
, &x
, &y
, w
, h
, FALSE
);
868 client_configure_full(client
, x
, y
, w
, h
, FALSE
, TRUE
, TRUE
);
871 if (e
->xconfigurerequest
.value_mask
& CWStackMode
) {
872 switch (e
->xconfigurerequest
.detail
) {
875 /* Apps are so rude. And this is totally disconnected from
876 activation/focus. Bleh. */
877 /*client_lower(client);*/
883 /* Apps are so rude. And this is totally disconnected from
884 activation/focus. Bleh. */
885 /*client_raise(client);*/
891 if (client
->ignore_unmaps
) {
892 client
->ignore_unmaps
--;
895 ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
897 client
->window
, e
->xunmap
.event
, e
->xunmap
.from_configure
,
898 client
->ignore_unmaps
);
899 client_unmanage(client
);
902 ob_debug("DestroyNotify for window 0x%x\n", client
->window
);
903 client_unmanage(client
);
906 /* this is when the client is first taken captive in the frame */
907 if (e
->xreparent
.parent
== client
->frame
->plate
) break;
910 This event is quite rare and is usually handled in unmapHandler.
911 However, if the window is unmapped when the reparent event occurs,
912 the window manager never sees it because an unmap event is not sent
913 to an already unmapped window.
916 /* we don't want the reparent event, put it back on the stack for the
917 X server to deal with after we unmanage the window */
918 XPutBackEvent(ob_display
, e
);
920 ob_debug("ReparentNotify for window 0x%x\n", client
->window
);
921 client_unmanage(client
);
924 ob_debug("MapRequest for 0x%lx\n", client
->window
);
925 if (!client
->iconic
) break; /* this normally doesn't happen, but if it
926 does, we don't want it!
927 it can happen now when the window is on
928 another desktop, but we still don't
930 client_activate(client
, FALSE
, TRUE
);
933 /* validate cuz we query stuff off the client here */
934 if (!client_validate(client
)) break;
936 if (e
->xclient
.format
!= 32) return;
938 msgtype
= e
->xclient
.message_type
;
939 if (msgtype
== prop_atoms
.wm_change_state
) {
940 /* compress changes into a single change */
941 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
943 /* XXX: it would be nice to compress ALL messages of a
944 type, not just messages in a row without other
945 message types between. */
946 if (ce
.xclient
.message_type
!= msgtype
) {
947 XPutBackEvent(ob_display
, &ce
);
950 e
->xclient
= ce
.xclient
;
952 client_set_wm_state(client
, e
->xclient
.data
.l
[0]);
953 } else if (msgtype
== prop_atoms
.net_wm_desktop
) {
954 /* compress changes into a single change */
955 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
957 /* XXX: it would be nice to compress ALL messages of a
958 type, not just messages in a row without other
959 message types between. */
960 if (ce
.xclient
.message_type
!= msgtype
) {
961 XPutBackEvent(ob_display
, &ce
);
964 e
->xclient
= ce
.xclient
;
966 if ((unsigned)e
->xclient
.data
.l
[0] < screen_num_desktops
||
967 (unsigned)e
->xclient
.data
.l
[0] == DESKTOP_ALL
)
968 client_set_desktop(client
, (unsigned)e
->xclient
.data
.l
[0],
970 } else if (msgtype
== prop_atoms
.net_wm_state
) {
971 /* can't compress these */
972 ob_debug("net_wm_state %s %ld %ld for 0x%lx\n",
973 (e
->xclient
.data
.l
[0] == 0 ? "Remove" :
974 e
->xclient
.data
.l
[0] == 1 ? "Add" :
975 e
->xclient
.data
.l
[0] == 2 ? "Toggle" : "INVALID"),
976 e
->xclient
.data
.l
[1], e
->xclient
.data
.l
[2],
978 client_set_state(client
, e
->xclient
.data
.l
[0],
979 e
->xclient
.data
.l
[1], e
->xclient
.data
.l
[2]);
980 } else if (msgtype
== prop_atoms
.net_close_window
) {
981 ob_debug("net_close_window for 0x%lx\n", client
->window
);
982 client_close(client
);
983 } else if (msgtype
== prop_atoms
.net_active_window
) {
984 ob_debug("net_active_window for 0x%lx source=%s\n",
986 (e
->xclient
.data
.l
[0] == 0 ? "unknown" :
987 (e
->xclient
.data
.l
[0] == 1 ? "application" :
988 (e
->xclient
.data
.l
[0] == 2 ? "user" : "INVALID"))));
989 /* XXX make use of data.l[2] ! */
990 event_curtime
= e
->xclient
.data
.l
[1];
991 client_activate(client
, FALSE
,
992 (e
->xclient
.data
.l
[0] == 0 ||
993 e
->xclient
.data
.l
[0] == 2));
994 } else if (msgtype
== prop_atoms
.net_wm_moveresize
) {
995 ob_debug("net_wm_moveresize for 0x%lx direction %d\n",
996 client
->window
, e
->xclient
.data
.l
[2]);
997 if ((Atom
)e
->xclient
.data
.l
[2] ==
998 prop_atoms
.net_wm_moveresize_size_topleft
||
999 (Atom
)e
->xclient
.data
.l
[2] ==
1000 prop_atoms
.net_wm_moveresize_size_top
||
1001 (Atom
)e
->xclient
.data
.l
[2] ==
1002 prop_atoms
.net_wm_moveresize_size_topright
||
1003 (Atom
)e
->xclient
.data
.l
[2] ==
1004 prop_atoms
.net_wm_moveresize_size_right
||
1005 (Atom
)e
->xclient
.data
.l
[2] ==
1006 prop_atoms
.net_wm_moveresize_size_right
||
1007 (Atom
)e
->xclient
.data
.l
[2] ==
1008 prop_atoms
.net_wm_moveresize_size_bottomright
||
1009 (Atom
)e
->xclient
.data
.l
[2] ==
1010 prop_atoms
.net_wm_moveresize_size_bottom
||
1011 (Atom
)e
->xclient
.data
.l
[2] ==
1012 prop_atoms
.net_wm_moveresize_size_bottomleft
||
1013 (Atom
)e
->xclient
.data
.l
[2] ==
1014 prop_atoms
.net_wm_moveresize_size_left
||
1015 (Atom
)e
->xclient
.data
.l
[2] ==
1016 prop_atoms
.net_wm_moveresize_move
||
1017 (Atom
)e
->xclient
.data
.l
[2] ==
1018 prop_atoms
.net_wm_moveresize_size_keyboard
||
1019 (Atom
)e
->xclient
.data
.l
[2] ==
1020 prop_atoms
.net_wm_moveresize_move_keyboard
) {
1022 moveresize_start(client
, e
->xclient
.data
.l
[0],
1023 e
->xclient
.data
.l
[1], e
->xclient
.data
.l
[3],
1024 e
->xclient
.data
.l
[2]);
1026 else if ((Atom
)e
->xclient
.data
.l
[2] ==
1027 prop_atoms
.net_wm_moveresize_cancel
)
1028 moveresize_end(TRUE
);
1029 } else if (msgtype
== prop_atoms
.net_moveresize_window
) {
1030 gint grav
, x
, y
, w
, h
;
1032 if (e
->xclient
.data
.l
[0] & 0xff)
1033 grav
= e
->xclient
.data
.l
[0] & 0xff;
1035 grav
= client
->gravity
;
1037 if (e
->xclient
.data
.l
[0] & 1 << 8)
1038 x
= e
->xclient
.data
.l
[1];
1041 if (e
->xclient
.data
.l
[0] & 1 << 9)
1042 y
= e
->xclient
.data
.l
[2];
1045 if (e
->xclient
.data
.l
[0] & 1 << 10)
1046 w
= e
->xclient
.data
.l
[3];
1048 w
= client
->area
.width
;
1049 if (e
->xclient
.data
.l
[0] & 1 << 11)
1050 h
= e
->xclient
.data
.l
[4];
1052 h
= client
->area
.height
;
1054 ob_debug("MOVERESIZE x %d %d y %d %d\n",
1055 e
->xclient
.data
.l
[0] & 1 << 8, x
,
1056 e
->xclient
.data
.l
[0] & 1 << 9, y
);
1057 client_convert_gravity(client
, grav
, &x
, &y
, w
, h
);
1058 client_find_onscreen(client
, &x
, &y
, w
, h
, FALSE
);
1059 client_configure(client
, x
, y
, w
, h
, FALSE
, TRUE
);
1062 case PropertyNotify
:
1063 /* validate cuz we query stuff off the client here */
1064 if (!client_validate(client
)) break;
1066 /* compress changes to a single property into a single change */
1067 while (XCheckTypedWindowEvent(ob_display
, client
->window
,
1071 /* XXX: it would be nice to compress ALL changes to a property,
1072 not just changes in a row without other props between. */
1074 a
= ce
.xproperty
.atom
;
1075 b
= e
->xproperty
.atom
;
1079 if ((a
== prop_atoms
.net_wm_name
||
1080 a
== prop_atoms
.wm_name
||
1081 a
== prop_atoms
.net_wm_icon_name
||
1082 a
== prop_atoms
.wm_icon_name
)
1084 (b
== prop_atoms
.net_wm_name
||
1085 b
== prop_atoms
.wm_name
||
1086 b
== prop_atoms
.net_wm_icon_name
||
1087 b
== prop_atoms
.wm_icon_name
)) {
1090 if (a
== prop_atoms
.net_wm_icon
&&
1091 b
== prop_atoms
.net_wm_icon
)
1094 XPutBackEvent(ob_display
, &ce
);
1098 msgtype
= e
->xproperty
.atom
;
1099 if (msgtype
== XA_WM_NORMAL_HINTS
) {
1100 client_update_normal_hints(client
);
1101 /* normal hints can make a window non-resizable */
1102 client_setup_decor_and_functions(client
);
1103 } else if (msgtype
== XA_WM_HINTS
) {
1104 client_update_wmhints(client
);
1105 } else if (msgtype
== XA_WM_TRANSIENT_FOR
) {
1106 client_update_transient_for(client
);
1107 client_get_type(client
);
1108 /* type may have changed, so update the layer */
1109 client_calc_layer(client
);
1110 client_setup_decor_and_functions(client
);
1111 } else if (msgtype
== prop_atoms
.net_wm_name
||
1112 msgtype
== prop_atoms
.wm_name
||
1113 msgtype
== prop_atoms
.net_wm_icon_name
||
1114 msgtype
== prop_atoms
.wm_icon_name
) {
1115 client_update_title(client
);
1116 } else if (msgtype
== prop_atoms
.wm_class
) {
1117 client_update_class(client
);
1118 } else if (msgtype
== prop_atoms
.wm_protocols
) {
1119 client_update_protocols(client
);
1120 client_setup_decor_and_functions(client
);
1122 else if (msgtype
== prop_atoms
.net_wm_strut
) {
1123 client_update_strut(client
);
1125 else if (msgtype
== prop_atoms
.net_wm_icon
) {
1126 client_update_icons(client
);
1128 else if (msgtype
== prop_atoms
.net_wm_icon_geometry
) {
1129 client_update_icon_geometry(client
);
1131 else if (msgtype
== prop_atoms
.net_wm_user_time
) {
1132 client_update_user_time(client
);
1135 else if (msgtype
== prop_atoms
.net_wm_sync_request_counter
) {
1136 client_update_sync_request_counter(client
);
1139 else if (msgtype
== prop_atoms
.sm_client_id
) {
1140 client_update_sm_client_id(client
);
1142 case ColormapNotify
:
1143 client_update_colormap(client
, e
->xcolormap
.colormap
);
1148 if (extensions_shape
&& e
->type
== extensions_shape_event_basep
) {
1149 client
->shaped
= ((XShapeEvent
*)e
)->shaped
;
1150 frame_adjust_shape(client
->frame
);
1156 static void event_handle_dock(ObDock
*s
, XEvent
*e
)
1160 if (e
->xbutton
.button
== 1)
1161 stacking_raise(DOCK_AS_WINDOW(s
));
1162 else if (e
->xbutton
.button
== 2)
1163 stacking_lower(DOCK_AS_WINDOW(s
));
1174 static void event_handle_dockapp(ObDockApp
*app
, XEvent
*e
)
1178 dock_app_drag(app
, &e
->xmotion
);
1181 if (app
->ignore_unmaps
) {
1182 app
->ignore_unmaps
--;
1185 dock_remove(app
, TRUE
);
1188 dock_remove(app
, FALSE
);
1190 case ReparentNotify
:
1191 dock_remove(app
, FALSE
);
1193 case ConfigureNotify
:
1194 dock_app_configure(app
, e
->xconfigure
.width
, e
->xconfigure
.height
);
1199 static ObMenuFrame
* find_active_menu()
1202 ObMenuFrame
*ret
= NULL
;
1204 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
1213 static ObMenuFrame
* find_active_or_last_menu()
1215 ObMenuFrame
*ret
= NULL
;
1217 ret
= find_active_menu();
1218 if (!ret
&& menu_frame_visible
)
1219 ret
= menu_frame_visible
->data
;
1223 static gboolean
event_handle_menu_keyboard(XEvent
*ev
)
1225 guint keycode
, state
;
1228 gboolean ret
= TRUE
;
1230 keycode
= ev
->xkey
.keycode
;
1231 state
= ev
->xkey
.state
;
1232 unikey
= translate_unichar(keycode
);
1234 frame
= find_active_or_last_menu();
1238 else if (keycode
== ob_keycode(OB_KEY_ESCAPE
) && state
== 0) {
1239 /* Escape closes the active menu */
1240 menu_frame_hide(frame
);
1243 else if (keycode
== ob_keycode(OB_KEY_RETURN
) && (state
== 0 ||
1244 state
== ControlMask
))
1246 /* Enter runs the active item or goes into the submenu.
1247 Control-Enter runs it without closing the menu. */
1249 menu_frame_select_next(frame
->child
);
1251 menu_entry_frame_execute(frame
->selected
, state
, ev
->xkey
.time
);
1254 else if (keycode
== ob_keycode(OB_KEY_LEFT
) && ev
->xkey
.state
== 0) {
1255 /* Left goes to the parent menu */
1256 menu_frame_select(frame
, NULL
, TRUE
);
1259 else if (keycode
== ob_keycode(OB_KEY_RIGHT
) && ev
->xkey
.state
== 0) {
1260 /* Right goes to the selected submenu */
1261 if (frame
->child
) menu_frame_select_next(frame
->child
);
1264 else if (keycode
== ob_keycode(OB_KEY_UP
) && state
== 0) {
1265 menu_frame_select_previous(frame
);
1268 else if (keycode
== ob_keycode(OB_KEY_DOWN
) && state
== 0) {
1269 menu_frame_select_next(frame
);
1272 /* keyboard accelerator shortcuts. */
1273 else if (ev
->xkey
.state
== 0 &&
1274 /* was it a valid key? */
1276 /* don't bother if the menu is empty. */
1281 ObMenuEntryFrame
*found
= NULL
;
1282 guint num_found
= 0;
1284 /* start after the selected one */
1285 start
= frame
->entries
;
1286 if (frame
->selected
) {
1287 for (it
= start
; frame
->selected
!= it
->data
; it
= g_list_next(it
))
1288 g_assert(it
!= NULL
); /* nothing was selected? */
1289 /* next with wraparound */
1290 start
= g_list_next(it
);
1291 if (start
== NULL
) start
= frame
->entries
;
1296 ObMenuEntryFrame
*e
= it
->data
;
1297 gunichar entrykey
= 0;
1299 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1300 e
->entry
->data
.normal
.enabled
)
1301 entrykey
= e
->entry
->data
.normal
.shortcut
;
1302 else if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1303 entrykey
= e
->entry
->data
.submenu
.submenu
->shortcut
;
1305 if (unikey
== entrykey
) {
1306 if (found
== NULL
) found
= e
;
1310 /* next with wraparound */
1311 it
= g_list_next(it
);
1312 if (it
== NULL
) it
= frame
->entries
;
1313 } while (it
!= start
);
1316 if (found
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1319 menu_frame_select(frame
, found
, TRUE
);
1321 menu_entry_frame_execute(found
, state
, ev
->xkey
.time
);
1323 menu_frame_select(frame
, found
, TRUE
);
1325 menu_frame_select_next(frame
->child
);
1336 static gboolean
event_handle_menu(XEvent
*ev
)
1339 ObMenuEntryFrame
*e
;
1340 gboolean ret
= TRUE
;
1344 if ((ev
->xbutton
.button
< 4 || ev
->xbutton
.button
> 5)
1347 if ((e
= menu_entry_frame_under(ev
->xbutton
.x_root
,
1348 ev
->xbutton
.y_root
)))
1349 menu_entry_frame_execute(e
, ev
->xbutton
.state
,
1352 menu_frame_hide_all();
1356 if ((e
= g_hash_table_lookup(menu_frame_map
, &ev
->xcrossing
.window
))) {
1357 if (e
->ignore_enters
)
1360 menu_frame_select(e
->frame
, e
, FALSE
);
1364 if ((e
= g_hash_table_lookup(menu_frame_map
, &ev
->xcrossing
.window
)) &&
1365 (f
= find_active_menu()) && f
->selected
== e
&&
1366 e
->entry
->type
!= OB_MENU_ENTRY_TYPE_SUBMENU
)
1368 menu_frame_select(e
->frame
, NULL
, FALSE
);
1371 if ((e
= menu_entry_frame_under(ev
->xmotion
.x_root
,
1372 ev
->xmotion
.y_root
)))
1373 menu_frame_select(e
->frame
, e
, FALSE
);
1376 ret
= event_handle_menu_keyboard(ev
);
1382 static gboolean
menu_hide_delay_func(gpointer data
)
1384 menu_can_hide
= TRUE
;
1385 return FALSE
; /* no repeat */
1388 static void focus_delay_dest(gpointer data
)
1393 static gboolean
focus_delay_cmp(gconstpointer d1
, gconstpointer d2
)
1395 const ObFocusDelayData
*f1
= d1
;
1396 return f1
->client
== d2
;
1399 static gboolean
focus_delay_func(gpointer data
)
1401 ObFocusDelayData
*d
= data
;
1402 Time old
= event_curtime
;
1404 event_curtime
= d
->time
;
1405 if (focus_client
!= d
->client
) {
1406 if (client_focus(d
->client
) && config_focus_raise
)
1407 client_raise(d
->client
);
1409 event_curtime
= old
;
1410 return FALSE
; /* no repeat */
1413 static void focus_delay_client_dest(ObClient
*client
, gpointer data
)
1415 ob_main_loop_timeout_remove_data(ob_main_loop
, focus_delay_func
,
1419 void event_halt_focus_delay()
1421 ob_main_loop_timeout_remove(ob_main_loop
, focus_delay_func
);
1424 void event_ignore_queued_enters()
1426 GSList
*saved
= NULL
, *it
;
1429 XSync(ob_display
, FALSE
);
1431 /* count the events */
1433 e
= g_new(XEvent
, 1);
1434 if (XCheckTypedEvent(ob_display
, EnterNotify
, e
)) {
1437 win
= g_hash_table_lookup(window_map
, &e
->xany
.window
);
1438 if (win
&& WINDOW_IS_CLIENT(win
))
1439 ++ignore_enter_focus
;
1441 saved
= g_slist_append(saved
, e
);
1447 /* put the events back */
1448 for (it
= saved
; it
; it
= g_slist_next(it
)) {
1449 XPutBackEvent(ob_display
, it
->data
);
1452 g_slist_free(saved
);
1455 gboolean
event_time_after(Time t1
, Time t2
)
1457 g_assert(t1
!= CurrentTime
);
1458 g_assert(t2
!= CurrentTime
);
1461 Timestamp values wrap around (after about 49.7 days). The server, given
1462 its current time is represented by timestamp T, always interprets
1463 timestamps from clients by treating half of the timestamp space as being
1464 later in time than T.
1465 - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
1468 /* TIME_HALF is half of the number space of a Time type variable */
1469 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
1471 if (t2
>= TIME_HALF
)
1472 /* t2 is in the second half so t1 might wrap around and be smaller than
1474 return t1
>= t2
|| t1
< (t2
+ TIME_HALF
);
1476 /* t2 is in the first half so t1 has to come after it */
1477 return t1
>= t2
&& t1
< (t2
+ TIME_HALF
);