3 #include "framerender.h"
19 Client
*focus_client
= NULL
;
20 GList
**focus_order
= NULL
; /* these lists are created when screen_startup
21 sets the number of desktops */
23 Window focus_backup
= None
;
25 static Client
*focus_cycle_target
= NULL
;
26 static Popup
*focus_cycle_popup
= NULL
;
30 /* create the window which gets focus when no clients get it. Have to
31 make it override-redirect so we don't try manage it, since it is
33 XSetWindowAttributes attrib
;
36 focus_cycle_popup
= popup_new(TRUE
);
38 attrib
.override_redirect
= TRUE
;
39 focus_backup
= XCreateWindow(ob_display
, ob_root
,
41 CopyFromParent
, InputOutput
, CopyFromParent
,
42 CWOverrideRedirect
, &attrib
);
43 XMapRaised(ob_display
, focus_backup
);
45 /* start with nothing focused */
46 focus_set_client(NULL
);
53 for (i
= 0; i
< screen_num_desktops
; ++i
)
54 g_list_free(focus_order
[i
]);
58 popup_free(focus_cycle_popup
);
59 focus_cycle_popup
= NULL
;
61 XDestroyWindow(ob_display
, focus_backup
);
63 /* reset focus to root */
64 XSetInputFocus(ob_display
, PointerRoot
, RevertToPointerRoot
,
68 static void push_to_top(Client
*client
)
72 desktop
= client
->desktop
;
73 if (desktop
== DESKTOP_ALL
) desktop
= screen_desktop
;
74 focus_order
[desktop
] = g_list_remove(focus_order
[desktop
], client
);
75 focus_order
[desktop
] = g_list_prepend(focus_order
[desktop
], client
);
78 void focus_set_client(Client
*client
)
83 /* uninstall the old colormap, and install the new one */
84 screen_install_colormap(focus_client
, FALSE
);
85 screen_install_colormap(client
, TRUE
);
88 /* when nothing will be focused, send focus to the backup target */
89 XSetInputFocus(ob_display
, focus_backup
, RevertToPointerRoot
,
91 XSync(ob_display
, FALSE
);
94 /* in the middle of cycling..? kill it. */
95 if (focus_cycle_target
)
96 focus_cycle(TRUE
, TRUE
, TRUE
, TRUE
);
99 focus_client
= client
;
101 /* move to the top of the list */
105 /* set the NET_ACTIVE_WINDOW hint, but preserve it on shutdown */
106 if (ob_state
!= State_Exiting
) {
107 active
= client
? client
->window
: None
;
108 PROP_SET32(ob_root
, net_active_window
, window
, active
);
111 if (focus_client
!= NULL
)
112 dispatch_client(Event_Client_Focus
, focus_client
, 0, 0);
114 dispatch_client(Event_Client_Unfocus
, old
, 0, 0);
117 static gboolean
focus_under_pointer()
122 if (ob_pointer_pos(&x
, &y
)) {
123 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
124 Client
*c
= it
->data
;
125 if (c
->desktop
== screen_desktop
&&
126 RECT_CONTAINS(c
->frame
->area
, x
, y
))
130 return client_normal(it
->data
) && client_focus(it
->data
);
135 /* finds the first transient that isn't 'skip' and ensure's that client_normal
137 static Client
*find_transient_recursive(Client
*c
, Client
*top
, Client
*skip
)
142 for (it
= c
->transients
; it
; it
= it
->next
) {
143 if (it
->data
== top
) return NULL
;
144 ret
= find_transient_recursive(it
->data
, top
, skip
);
145 if (ret
&& ret
!= skip
&& client_normal(ret
)) return ret
;
146 if (it
->data
!= skip
&& client_normal(it
->data
)) return it
->data
;
151 static gboolean
focus_fallback_transient(Client
*top
, Client
*old
)
153 Client
*target
= find_transient_recursive(top
, top
, old
);
155 /* make sure client_normal is true always */
156 if (!client_normal(top
))
158 target
= top
; /* no transient, keep the top */
160 return client_focus(target
);
163 void focus_fallback(FallbackType type
)
170 /* unfocus any focused clients.. they can be focused by Pointer events
171 and such, and then when I try focus them, I won't get a FocusIn event
174 focus_set_client(NULL
);
176 if (!(type
== Fallback_Desktop
?
177 config_focus_last_on_desktop
: config_focus_last
)) {
178 if (config_focus_follow
) focus_under_pointer();
182 if (type
== Fallback_Unfocusing
&& old
) {
183 /* try for transient relations */
184 if (old
->transient_for
) {
185 if (old
->transient_for
== TRAN_GROUP
) {
186 for (it
= focus_order
[screen_desktop
]; it
; it
= it
->next
) {
189 for (sit
= old
->group
->members
; sit
; sit
= sit
->next
)
190 if (sit
->data
== it
->data
)
191 if (focus_fallback_transient(sit
->data
, old
))
195 if (focus_fallback_transient(old
->transient_for
, old
))
200 /* try for group relations */
204 for (it
= focus_order
[screen_desktop
]; it
!= NULL
; it
= it
->next
)
205 for (sit
= old
->group
->members
; sit
; sit
= sit
->next
)
206 if (sit
->data
== it
->data
)
207 if (sit
->data
!= old
&& client_normal(sit
->data
))
208 if (client_focus(sit
->data
))
213 for (it
= focus_order
[screen_desktop
]; it
!= NULL
; it
= it
->next
)
214 if (type
!= Fallback_Unfocusing
|| it
->data
!= old
)
215 if (client_normal(it
->data
) &&
216 /* dont fall back to 'anonymous' fullscreen windows. theres no
217 checks for this is in transient/group fallbacks. */
218 !((Client
*)it
->data
)->fullscreen
&&
219 client_focus(it
->data
))
222 /* nothing to focus */
223 focus_set_client(NULL
);
226 static void popup_cycle(Client
*c
, gboolean show
)
229 popup_hide(focus_cycle_popup
);
233 a
= screen_area(c
->desktop
);
234 popup_position(focus_cycle_popup
, CenterGravity
,
235 a
->x
+ a
->width
/ 2, a
->y
+ a
->height
/ 2);
236 /* popup_size(focus_cycle_popup, a->height/2, a->height/16);
237 popup_show(focus_cycle_popup, c->title,
238 client_icon(c, a->height/16, a->height/16));
240 /* XXX the size and the font extents need to be related on some level
242 popup_size(focus_cycle_popup
, 320, 48);
244 /* use the transient's parent's title/icon */
245 while (c
->transient_for
&& c
->transient_for
!= TRAN_GROUP
)
246 c
= c
->transient_for
;
248 popup_show(focus_cycle_popup
, (c
->iconic
? c
->icon_title
: c
->title
),
249 client_icon(c
, 48, 48));
253 Client
*focus_cycle(gboolean forward
, gboolean linear
, gboolean done
,
256 static Client
*first
= NULL
;
257 static Client
*t
= NULL
;
258 static GList
*order
= NULL
;
259 GList
*it
, *start
, *list
;
263 /*if (first) client_focus(first); XXX*/
264 if (focus_cycle_target
)
265 frame_adjust_focus(focus_cycle_target
->frame
, FALSE
);
267 frame_adjust_focus(focus_client
->frame
, TRUE
);
270 if (focus_cycle_target
)
271 client_activate(focus_cycle_target
);
274 if (!first
) first
= focus_client
;
275 if (!focus_cycle_target
) focus_cycle_target
= focus_client
;
277 if (linear
) list
= client_list
;
278 else list
= focus_order
[screen_desktop
];
280 start
= it
= g_list_find(list
, focus_cycle_target
);
281 if (!start
) /* switched desktops or something? */
282 start
= it
= forward
? g_list_last(list
) : g_list_first(list
);
283 if (!start
) goto done_cycle
;
288 if (it
== NULL
) it
= g_list_first(list
);
291 if (it
== NULL
) it
= g_list_last(list
);
293 /*ft = client_focus_target(it->data);*/
295 if (ft
->transients
== NULL
&& /*ft == it->data &&*/client_normal(ft
) &&
296 (ft
->can_focus
|| ft
->focus_notify
) &&
297 (ft
->desktop
== screen_desktop
|| ft
->desktop
== DESKTOP_ALL
)) {
298 if (ft
!= focus_cycle_target
) { /* prevents flicker */
299 if (focus_cycle_target
)
300 frame_adjust_focus(focus_cycle_target
->frame
, FALSE
);
301 focus_cycle_target
= ft
;
302 frame_adjust_focus(focus_cycle_target
->frame
, TRUE
);
304 popup_cycle(ft
, config_focus_popup
);
307 } while (it
!= start
);
312 focus_cycle_target
= NULL
;
315 popup_cycle(ft
, FALSE
);
319 void focus_order_add_new(Client
*c
)
324 focus_order_to_top(c
);
327 if (d
== DESKTOP_ALL
) {
328 for (i
= 0; i
< screen_num_desktops
; ++i
) {
329 if (focus_order
[i
] && ((Client
*)focus_order
[i
]->data
)->iconic
)
330 focus_order
[i
] = g_list_insert(focus_order
[i
], c
, 0);
332 focus_order
[i
] = g_list_insert(focus_order
[i
], c
, 1);
335 if (focus_order
[d
] && ((Client
*)focus_order
[d
]->data
)->iconic
)
336 focus_order
[d
] = g_list_insert(focus_order
[d
], c
, 0);
338 focus_order
[d
] = g_list_insert(focus_order
[d
], c
, 1);
342 void focus_order_remove(Client
*c
)
347 if (d
== DESKTOP_ALL
) {
348 for (i
= 0; i
< screen_num_desktops
; ++i
)
349 focus_order
[i
] = g_list_remove(focus_order
[i
], c
);
351 focus_order
[d
] = g_list_remove(focus_order
[d
], c
);
354 static void to_top(Client
*c
, guint d
)
356 focus_order
[d
] = g_list_remove(focus_order
[d
], c
);
358 focus_order
[d
] = g_list_prepend(focus_order
[d
], c
);
362 /* insert before first iconic window */
363 for (it
= focus_order
[d
];
364 it
&& !((Client
*)it
->data
)->iconic
; it
= it
->next
);
365 g_list_insert_before(focus_order
[d
], it
, c
);
369 void focus_order_to_top(Client
*c
)
374 if (d
== DESKTOP_ALL
) {
375 for (i
= 0; i
< screen_num_desktops
; ++i
)
381 static void to_bottom(Client
*c
, guint d
)
383 focus_order
[d
] = g_list_remove(focus_order
[d
], c
);
385 focus_order
[d
] = g_list_append(focus_order
[d
], c
);
389 /* insert before first iconic window */
390 for (it
= focus_order
[d
];
391 it
&& !((Client
*)it
->data
)->iconic
; it
= it
->next
);
392 g_list_insert_before(focus_order
[d
], it
, c
);
396 void focus_order_to_bottom(Client
*c
)
401 if (d
== DESKTOP_ALL
) {
402 for (i
= 0; i
< screen_num_desktops
; ++i
)