1 #include "../kernel/dispatch.h"
2 #include "../kernel/screen.h"
3 #include "../kernel/client.h"
4 #include "../kernel/frame.h"
5 #include "../kernel/focus.h"
6 #include "../kernel/stacking.h"
7 #include "../kernel/openbox.h"
8 #include "../kernel/config.h"
10 void plugin_setup_config()
12 config_def_set(config_def_new("focus.followMouse", Config_Bool
,
13 "Focus Follows Mouse",
14 "Focus windows when the mouse pointer "
16 config_def_set(config_def_new("focus.focusNew", Config_Bool
,
18 "Focus windows when they first appear "));
19 config_def_set(config_def_new("focus.warpOnDeskSwitch", Config_Bool
,
20 "Warp Pointer On Desktop Switch",
21 "Warps the pointer to the focused window "
22 "when switching desktops."));
26 static gboolean follow_mouse
= TRUE
;
27 static gboolean warp_on_desk_switch
= FALSE
;
28 static gboolean focus_new
= TRUE
;
30 /*static int skip_enter = 0;*/
32 static gboolean
focus_under_pointer()
39 if (XQueryPointer(ob_display
, ob_root
, &w
, &w
, &x
, &y
, &i
, &i
, &u
))
41 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
43 if (c
->desktop
== screen_desktop
&&
44 RECT_CONTAINS(c
->frame
->area
, x
, y
))
48 return client_normal(it
->data
) && client_focus(it
->data
);
54 static void chew_enter_events()
59 skip the next enter event from the desktop switch so focus
60 doesn't skip briefly to what was under the pointer */
62 /* kill all enter events from prior to the desktop switch, we
63 aren't interested in them if we have found our own target
65 XXX this is rude to other plugins...can this be done
66 better? count the events in the queue? */
67 while (XCheckTypedEvent(ob_display
, EnterNotify
, &e
));
70 XPutBackEvent(ob_display, &e);
77 static void focus_fallback(gboolean switching_desks
)
81 for (it
= focus_order
[screen_desktop
]; it
!= NULL
; it
= it
->next
)
82 if (client_normal(it
->data
) && client_focus(it
->data
)) {
83 if (switching_desks
) {
88 if (warp_on_desk_switch
) {
89 /* I have to do this warp twice! Otherwise windows dont get
90 Enter/Leave events when i warp on a desktop switch! */
91 XWarpPointer(ob_display
, None
, c
->window
, 0, 0, 0, 0,
92 c
->area
.width
/ 2, c
->area
.height
/ 2);
93 XWarpPointer(ob_display
, None
, c
->window
, 0, 0, 0, 0,
94 c
->area
.width
/ 2, c
->area
.height
/ 2);
101 static void focus_desktop()
105 for (it
= g_list_last(stacking_list
); it
!= NULL
; it
= it
->prev
) {
106 Client
*client
= it
->data
;
107 if (client
->type
== Type_Desktop
&& client
->frame
->visible
)
108 if (client_focus(client
))
114 static void event(ObEvent
*e
, void *foo
)
117 case Event_Client_Mapped
:
118 if (focus_new
&& client_normal(e
->data
.c
.client
))
119 client_focus(e
->data
.c
.client
);
122 case Event_Client_Unmapped
:
123 if (ob_state
== State_Exiting
) break;
125 if (client_focused(e
->data
.c
.client
))
126 if (!follow_mouse
|| !focus_under_pointer())
127 focus_fallback(FALSE
);
130 case Event_Client_Desktop
:
131 /* focus the next available target if moving from the current
133 if ((unsigned)e
->data
.c
.num
[1] == screen_desktop
)
134 if (!follow_mouse
|| !focus_under_pointer())
135 focus_fallback(FALSE
);
137 case Event_Ob_Desktop
:
138 focus_fallback(TRUE
);
141 case Event_Ob_ShowDesktop
:
142 if (!e
->data
.o
.num
[0]) { /* hiding the desktop, showing the clients */
143 if (!follow_mouse
|| !focus_under_pointer())
144 focus_fallback(TRUE
);
145 } else /* hiding clients, showing the desktop */
149 case Event_X_EnterNotify
:
151 if (e->data.x.client != NULL)
152 g_message("skipped enter %lx", e->data.x.client->window);
154 g_message("skipped enter 'root'");
158 if (e
->data
.x
.client
!= NULL
&& client_normal(e
->data
.x
.client
))
159 client_focus(e
->data
.x
.client
);
163 g_assert_not_reached();
167 void plugin_startup()
169 dispatch_register(Event_Client_Mapped
|
171 Event_Client_Unmapped
|
172 Event_X_EnterNotify
|
173 Event_Ob_ShowDesktop
,
174 (EventHandler
)event
, NULL
);
177 void plugin_shutdown()
179 dispatch_register(0, (EventHandler
)event
, NULL
);