1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 focus.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.
24 #include "framerender.h"
34 #include "render/render.h"
40 #define FOCUS_INDICATOR_WIDTH 6
42 ObClient
*focus_client
= NULL
;
43 GList
*focus_order
= NULL
;
44 ObClient
*focus_cycle_target
= NULL
;
46 /*! This variable is used for focus fallback. If we fallback to a window, we
47 set this to the window. And when focus goes somewhere after that, it will
48 be set to NULL. If between falling back to that window and something
49 getting focused, the window gets unmanaged, then if there are no incoming
50 FocusIn events, we fallback again because focus has just gotten itself lost.
52 static ObClient
*focus_tried
= NULL
;
58 InternalWindow bottom
;
61 RrAppearance
*a_focus_indicator
;
64 static ObIconPopup
*focus_cycle_popup
;
66 static gboolean
valid_focus_target(ObClient
*ft
,
67 gboolean all_desktops
,
68 gboolean dock_windows
,
69 gboolean desktop_windows
);
70 static void focus_cycle_destroy_notify(ObClient
*client
, gpointer data
);
71 static void focus_tried_hide_notify(ObClient
*client
, gpointer data
);
73 static Window
createWindow(Window parent
, gulong mask
,
74 XSetWindowAttributes
*attrib
)
76 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, 0,
77 RrDepth(ob_rr_inst
), InputOutput
,
78 RrVisual(ob_rr_inst
), mask
, attrib
);
82 void focus_startup(gboolean reconfig
)
84 focus_cycle_popup
= icon_popup_new(TRUE
);
87 XSetWindowAttributes attr
;
89 client_add_destroy_notify(focus_cycle_destroy_notify
, NULL
);
90 client_add_destroy_notify(focus_tried_hide_notify
, NULL
);
91 client_add_hide_notify(focus_tried_hide_notify
, NULL
);
93 /* start with nothing focused */
96 focus_indicator
.top
.obwin
.type
= Window_Internal
;
97 focus_indicator
.left
.obwin
.type
= Window_Internal
;
98 focus_indicator
.right
.obwin
.type
= Window_Internal
;
99 focus_indicator
.bottom
.obwin
.type
= Window_Internal
;
101 attr
.override_redirect
= True
;
102 attr
.background_pixel
= BlackPixel(ob_display
, ob_screen
);
103 focus_indicator
.top
.win
=
104 createWindow(RootWindow(ob_display
, ob_screen
),
105 CWOverrideRedirect
| CWBackPixel
, &attr
);
106 focus_indicator
.left
.win
=
107 createWindow(RootWindow(ob_display
, ob_screen
),
108 CWOverrideRedirect
| CWBackPixel
, &attr
);
109 focus_indicator
.right
.win
=
110 createWindow(RootWindow(ob_display
, ob_screen
),
111 CWOverrideRedirect
| CWBackPixel
, &attr
);
112 focus_indicator
.bottom
.win
=
113 createWindow(RootWindow(ob_display
, ob_screen
),
114 CWOverrideRedirect
| CWBackPixel
, &attr
);
116 stacking_add(INTERNAL_AS_WINDOW(&focus_indicator
.top
));
117 stacking_add(INTERNAL_AS_WINDOW(&focus_indicator
.left
));
118 stacking_add(INTERNAL_AS_WINDOW(&focus_indicator
.right
));
119 stacking_add(INTERNAL_AS_WINDOW(&focus_indicator
.bottom
));
121 color_white
= RrColorNew(ob_rr_inst
, 0xff, 0xff, 0xff);
123 a_focus_indicator
= RrAppearanceNew(ob_rr_inst
, 4);
124 a_focus_indicator
->surface
.grad
= RR_SURFACE_SOLID
;
125 a_focus_indicator
->surface
.relief
= RR_RELIEF_FLAT
;
126 a_focus_indicator
->surface
.primary
= RrColorNew(ob_rr_inst
,
128 a_focus_indicator
->texture
[0].type
= RR_TEXTURE_LINE_ART
;
129 a_focus_indicator
->texture
[0].data
.lineart
.color
= color_white
;
130 a_focus_indicator
->texture
[1].type
= RR_TEXTURE_LINE_ART
;
131 a_focus_indicator
->texture
[1].data
.lineart
.color
= color_white
;
132 a_focus_indicator
->texture
[2].type
= RR_TEXTURE_LINE_ART
;
133 a_focus_indicator
->texture
[2].data
.lineart
.color
= color_white
;
134 a_focus_indicator
->texture
[3].type
= RR_TEXTURE_LINE_ART
;
135 a_focus_indicator
->texture
[3].data
.lineart
.color
= color_white
;
139 void focus_shutdown(gboolean reconfig
)
141 icon_popup_free(focus_cycle_popup
);
144 client_remove_destroy_notify(focus_cycle_destroy_notify
);
145 client_remove_destroy_notify(focus_tried_hide_notify
);
146 client_remove_hide_notify(focus_tried_hide_notify
);
148 /* reset focus to root */
149 XSetInputFocus(ob_display
, PointerRoot
, RevertToNone
, CurrentTime
);
151 RrColorFree(color_white
);
153 RrAppearanceFree(a_focus_indicator
);
155 XDestroyWindow(ob_display
, focus_indicator
.top
.win
);
156 XDestroyWindow(ob_display
, focus_indicator
.left
.win
);
157 XDestroyWindow(ob_display
, focus_indicator
.right
.win
);
158 XDestroyWindow(ob_display
, focus_indicator
.bottom
.win
);
162 static void push_to_top(ObClient
*client
)
164 focus_order
= g_list_remove(focus_order
, client
);
165 focus_order
= g_list_prepend(focus_order
, client
);
168 void focus_set_client(ObClient
*client
)
172 ob_debug_type(OB_DEBUG_FOCUS
,
173 "focus_set_client 0x%lx\n", client
? client
->window
: 0);
175 /* uninstall the old colormap, and install the new one */
176 screen_install_colormap(focus_client
, FALSE
);
177 screen_install_colormap(client
, TRUE
);
179 /* in the middle of cycling..? kill it. CurrentTime is fine, time won't
182 if (focus_cycle_target
)
183 focus_cycle(TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
);
185 focus_client
= client
;
187 if (client
!= NULL
) {
188 /* move to the top of the list */
190 /* remove hiliting from the window when it gets focused */
191 client_hilite(client
, FALSE
);
194 /* set the NET_ACTIVE_WINDOW hint, but preserve it on shutdown */
195 if (ob_state() != OB_STATE_EXITING
) {
196 active
= client
? client
->window
: None
;
197 PROP_SET32(RootWindow(ob_display
, ob_screen
),
198 net_active_window
, window
, active
);
202 focus_tried
= NULL
; /* focus isn't "trying" to go anywhere now */
205 static ObClient
* focus_fallback_target(gboolean allow_refocus
, ObClient
*old
)
208 ObClient
*target
= NULL
;
209 ObClient
*desktop
= NULL
;
211 ob_debug_type(OB_DEBUG_FOCUS
, "trying pointer stuff\n");
212 if (config_focus_follow
&& !config_focus_last
)
214 if ((target
= client_under_pointer()))
215 if (allow_refocus
|| target
!= old
)
216 if (client_normal(target
) && client_can_focus(target
)) {
217 ob_debug_type(OB_DEBUG_FOCUS
, "found in pointer stuff\n");
223 /* try for group relations */
227 for (it
= focus_order
[screen_desktop
]; it
; it
= g_list_next(it
))
228 for (sit
= old
->group
->members
; sit
; sit
= g_slist_next(sit
))
229 if (sit
->data
== it
->data
)
230 if (sit
->data
!= old
&& client_normal(sit
->data
))
231 if (client_can_focus(sit
->data
))
236 ob_debug_type(OB_DEBUG_FOCUS
, "trying omnipresentness\n");
237 if (allow_refocus
&& old
&& old
->desktop
== DESKTOP_ALL
&&
244 ob_debug_type(OB_DEBUG_FOCUS
, "trying the focus order\n");
245 for (it
= focus_order
; it
; it
= g_list_next(it
))
246 if (allow_refocus
|| it
->data
!= old
) {
247 ObClient
*c
= it
->data
;
248 /* fallback focus to a window if:
249 1. it is actually focusable, cuz if it's not then we're sending
250 focus off to nothing. this includes if it is visible right now
251 2. it is on the current desktop. this ignores omnipresent
252 windows, which are problematic in their own rite.
253 3. it is a normal type window, don't fall back onto a dock or
254 a splashscreen or a desktop window (save the desktop as a
255 backup fallback though)
257 if (client_can_focus(c
))
259 if (c
->desktop
== screen_desktop
&& client_normal(c
)) {
260 ob_debug_type(OB_DEBUG_FOCUS
, "found in focus order\n");
262 } else if (c
->type
== OB_CLIENT_TYPE_DESKTOP
&&
268 /* as a last resort fallback to the desktop window if there is one.
269 (if there's more than one, then the one most recently focused.)
271 ob_debug_type(OB_DEBUG_FOCUS
, "found desktop: \n", !!desktop
);
275 ObClient
* focus_fallback(gboolean allow_refocus
)
281 new = focus_fallback_target(allow_refocus
, focus_client
);
283 /* unfocus any focused clients.. they can be focused by Pointer events
284 and such, and then when we try focus them, we won't get a FocusIn
285 event at all for them. */
290 /* remember that we tried to send focus here */
299 /* Install our own colormap */
300 if (focus_client
!= NULL
) {
301 screen_install_colormap(focus_client
, FALSE
);
302 screen_install_colormap(NULL
, TRUE
);
305 /* Don't set focus_client to NULL here. It will be set to NULL when the
306 FocusOut event comes. Otherwise, if we focus nothing and then focus the
307 same window again, The focus code says nothing changed, but focus_client
308 ends up being NULL anyways.
312 focus_tried
= NULL
; /* focus isn't "trying" to go anywhere now */
314 /* if there is a grab going on, then we need to cancel it. if we move
315 focus during the grab, applications will get NotifyWhileGrabbed events
318 actions should not rely on being able to move focus during an
321 if (keyboard_interactively_grabbed())
322 keyboard_interactive_cancel();
324 /* when nothing will be focused, send focus to the backup target */
325 XSetInputFocus(ob_display
, screen_support_win
, RevertToPointerRoot
,
329 static gchar
*popup_get_name(ObClient
*c
, ObClient
**nametarget
)
333 const gchar
*desk
= NULL
;
336 /* find our highest direct parent, including non-normal windows */
337 for (p
= c
; p
->transient_for
&& p
->transient_for
!= OB_TRAN_GROUP
;
338 p
= p
->transient_for
);
340 if (c
->desktop
!= DESKTOP_ALL
&& c
->desktop
!= screen_desktop
)
341 desk
= screen_desktop_names
[c
->desktop
];
343 /* use the transient's parent's title/icon if we don't have one */
344 if (p
!= c
&& !strcmp("", (c
->iconic
? c
->icon_title
: c
->title
)))
345 title
= g_strdup(p
->iconic
? p
->icon_title
: p
->title
);
348 title
= g_strdup(c
->iconic
? c
->icon_title
: c
->title
);
351 ret
= g_strdup_printf("%s [%s]", title
, desk
);
358 /* set this only if we're returning true and they asked for it */
359 if (ret
&& nametarget
) *nametarget
= p
;
363 static void popup_cycle(ObClient
*c
, gboolean show
,
364 gboolean all_desktops
, gboolean dock_windows
,
365 gboolean desktop_windows
)
367 gchar
*showtext
= NULL
;
368 ObClient
*showtarget
;
371 icon_popup_hide(focus_cycle_popup
);
375 /* do this stuff only when the dialog is first showing */
376 if (!focus_cycle_popup
->popup
->mapped
&&
377 !focus_cycle_popup
->popup
->delay_mapped
)
381 GList
*targets
= NULL
, *it
;
384 /* position the popup */
385 a
= screen_physical_area_monitor(0);
386 icon_popup_position(focus_cycle_popup
, CenterGravity
,
387 a
->x
+ a
->width
/ 2, a
->y
+ a
->height
/ 2);
388 icon_popup_height(focus_cycle_popup
, POPUP_HEIGHT
);
389 icon_popup_min_width(focus_cycle_popup
, POPUP_WIDTH
);
390 icon_popup_max_width(focus_cycle_popup
,
391 MAX(a
->width
/3, POPUP_WIDTH
));
394 /* make its width to be the width of all the possible titles */
396 /* build a list of all the valid focus targets */
397 for (it
= focus_order
; it
; it
= g_list_next(it
)) {
398 ObClient
*ft
= it
->data
;
399 if (valid_focus_target(ft
, all_desktops
, dock_windows
402 targets
= g_list_prepend(targets
, ft
);
406 /* make it null terminated so we can use g_strfreev */
407 names
= g_new(char*, n
+1);
408 for (it
= targets
, i
= 0; it
; it
= g_list_next(it
), ++i
) {
409 ObClient
*ft
= it
->data
, *t
;
410 names
[i
] = popup_get_name(ft
, &t
);
412 /* little optimization.. save this text and client, so we dont
413 have to get it again */
415 showtext
= g_strdup(names
[i
]);
421 icon_popup_text_width_to_strings(focus_cycle_popup
, names
, n
);
426 if (!showtext
) showtext
= popup_get_name(c
, &showtarget
);
427 icon_popup_show(focus_cycle_popup
, showtext
,
428 client_icon(showtarget
, 48, 48));
432 static void focus_cycle_destroy_notify(ObClient
*client
, gpointer data
)
434 /* end cycling if the target disappears. CurrentTime is fine, time won't
437 if (focus_cycle_target
== client
)
438 focus_cycle(TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
);
441 void focus_cycle_draw_indicator()
443 if (!focus_cycle_target
) {
444 XUnmapWindow(ob_display
, focus_indicator
.top
.win
);
445 XUnmapWindow(ob_display
, focus_indicator
.left
.win
);
446 XUnmapWindow(ob_display
, focus_indicator
.right
.win
);
447 XUnmapWindow(ob_display
, focus_indicator
.bottom
.win
);
449 /* kill enter events cause by this unmapping */
450 event_ignore_queued_enters();
453 if (focus_cycle_target)
454 frame_adjust_focus(focus_cycle_target->frame, FALSE);
455 frame_adjust_focus(focus_cycle_target->frame, TRUE);
460 wt
= wl
= wr
= wb
= FOCUS_INDICATOR_WIDTH
;
462 x
= focus_cycle_target
->frame
->area
.x
;
463 y
= focus_cycle_target
->frame
->area
.y
;
464 w
= focus_cycle_target
->frame
->area
.width
;
467 XMoveResizeWindow(ob_display
, focus_indicator
.top
.win
,
469 a_focus_indicator
->texture
[0].data
.lineart
.x1
= 0;
470 a_focus_indicator
->texture
[0].data
.lineart
.y1
= h
-1;
471 a_focus_indicator
->texture
[0].data
.lineart
.x2
= 0;
472 a_focus_indicator
->texture
[0].data
.lineart
.y2
= 0;
473 a_focus_indicator
->texture
[1].data
.lineart
.x1
= 0;
474 a_focus_indicator
->texture
[1].data
.lineart
.y1
= 0;
475 a_focus_indicator
->texture
[1].data
.lineart
.x2
= w
-1;
476 a_focus_indicator
->texture
[1].data
.lineart
.y2
= 0;
477 a_focus_indicator
->texture
[2].data
.lineart
.x1
= w
-1;
478 a_focus_indicator
->texture
[2].data
.lineart
.y1
= 0;
479 a_focus_indicator
->texture
[2].data
.lineart
.x2
= w
-1;
480 a_focus_indicator
->texture
[2].data
.lineart
.y2
= h
-1;
481 a_focus_indicator
->texture
[3].data
.lineart
.x1
= (wl
-1);
482 a_focus_indicator
->texture
[3].data
.lineart
.y1
= h
-1;
483 a_focus_indicator
->texture
[3].data
.lineart
.x2
= w
- wr
;
484 a_focus_indicator
->texture
[3].data
.lineart
.y2
= h
-1;
485 RrPaint(a_focus_indicator
, focus_indicator
.top
.win
,
488 x
= focus_cycle_target
->frame
->area
.x
;
489 y
= focus_cycle_target
->frame
->area
.y
;
491 h
= focus_cycle_target
->frame
->area
.height
;
493 XMoveResizeWindow(ob_display
, focus_indicator
.left
.win
,
495 a_focus_indicator
->texture
[0].data
.lineart
.x1
= w
-1;
496 a_focus_indicator
->texture
[0].data
.lineart
.y1
= 0;
497 a_focus_indicator
->texture
[0].data
.lineart
.x2
= 0;
498 a_focus_indicator
->texture
[0].data
.lineart
.y2
= 0;
499 a_focus_indicator
->texture
[1].data
.lineart
.x1
= 0;
500 a_focus_indicator
->texture
[1].data
.lineart
.y1
= 0;
501 a_focus_indicator
->texture
[1].data
.lineart
.x2
= 0;
502 a_focus_indicator
->texture
[1].data
.lineart
.y2
= h
-1;
503 a_focus_indicator
->texture
[2].data
.lineart
.x1
= 0;
504 a_focus_indicator
->texture
[2].data
.lineart
.y1
= h
-1;
505 a_focus_indicator
->texture
[2].data
.lineart
.x2
= w
-1;
506 a_focus_indicator
->texture
[2].data
.lineart
.y2
= h
-1;
507 a_focus_indicator
->texture
[3].data
.lineart
.x1
= w
-1;
508 a_focus_indicator
->texture
[3].data
.lineart
.y1
= wt
-1;
509 a_focus_indicator
->texture
[3].data
.lineart
.x2
= w
-1;
510 a_focus_indicator
->texture
[3].data
.lineart
.y2
= h
- wb
;
511 RrPaint(a_focus_indicator
, focus_indicator
.left
.win
,
514 x
= focus_cycle_target
->frame
->area
.x
+
515 focus_cycle_target
->frame
->area
.width
- wr
;
516 y
= focus_cycle_target
->frame
->area
.y
;
518 h
= focus_cycle_target
->frame
->area
.height
;
520 XMoveResizeWindow(ob_display
, focus_indicator
.right
.win
,
522 a_focus_indicator
->texture
[0].data
.lineart
.x1
= 0;
523 a_focus_indicator
->texture
[0].data
.lineart
.y1
= 0;
524 a_focus_indicator
->texture
[0].data
.lineart
.x2
= w
-1;
525 a_focus_indicator
->texture
[0].data
.lineart
.y2
= 0;
526 a_focus_indicator
->texture
[1].data
.lineart
.x1
= w
-1;
527 a_focus_indicator
->texture
[1].data
.lineart
.y1
= 0;
528 a_focus_indicator
->texture
[1].data
.lineart
.x2
= w
-1;
529 a_focus_indicator
->texture
[1].data
.lineart
.y2
= h
-1;
530 a_focus_indicator
->texture
[2].data
.lineart
.x1
= w
-1;
531 a_focus_indicator
->texture
[2].data
.lineart
.y1
= h
-1;
532 a_focus_indicator
->texture
[2].data
.lineart
.x2
= 0;
533 a_focus_indicator
->texture
[2].data
.lineart
.y2
= h
-1;
534 a_focus_indicator
->texture
[3].data
.lineart
.x1
= 0;
535 a_focus_indicator
->texture
[3].data
.lineart
.y1
= wt
-1;
536 a_focus_indicator
->texture
[3].data
.lineart
.x2
= 0;
537 a_focus_indicator
->texture
[3].data
.lineart
.y2
= h
- wb
;
538 RrPaint(a_focus_indicator
, focus_indicator
.right
.win
,
541 x
= focus_cycle_target
->frame
->area
.x
;
542 y
= focus_cycle_target
->frame
->area
.y
+
543 focus_cycle_target
->frame
->area
.height
- wb
;
544 w
= focus_cycle_target
->frame
->area
.width
;
547 XMoveResizeWindow(ob_display
, focus_indicator
.bottom
.win
,
549 a_focus_indicator
->texture
[0].data
.lineart
.x1
= 0;
550 a_focus_indicator
->texture
[0].data
.lineart
.y1
= 0;
551 a_focus_indicator
->texture
[0].data
.lineart
.x2
= 0;
552 a_focus_indicator
->texture
[0].data
.lineart
.y2
= h
-1;
553 a_focus_indicator
->texture
[1].data
.lineart
.x1
= 0;
554 a_focus_indicator
->texture
[1].data
.lineart
.y1
= h
-1;
555 a_focus_indicator
->texture
[1].data
.lineart
.x2
= w
-1;
556 a_focus_indicator
->texture
[1].data
.lineart
.y2
= h
-1;
557 a_focus_indicator
->texture
[2].data
.lineart
.x1
= w
-1;
558 a_focus_indicator
->texture
[2].data
.lineart
.y1
= h
-1;
559 a_focus_indicator
->texture
[2].data
.lineart
.x2
= w
-1;
560 a_focus_indicator
->texture
[2].data
.lineart
.y2
= 0;
561 a_focus_indicator
->texture
[3].data
.lineart
.x1
= wl
-1;
562 a_focus_indicator
->texture
[3].data
.lineart
.y1
= 0;
563 a_focus_indicator
->texture
[3].data
.lineart
.x2
= w
- wr
;
564 a_focus_indicator
->texture
[3].data
.lineart
.y2
= 0;
565 RrPaint(a_focus_indicator
, focus_indicator
.bottom
.win
,
568 XMapWindow(ob_display
, focus_indicator
.top
.win
);
569 XMapWindow(ob_display
, focus_indicator
.left
.win
);
570 XMapWindow(ob_display
, focus_indicator
.right
.win
);
571 XMapWindow(ob_display
, focus_indicator
.bottom
.win
);
575 static gboolean
has_valid_group_siblings_on_desktop(ObClient
*ft
,
576 gboolean all_desktops
)
581 if (!ft
->group
) return FALSE
;
583 for (it
= ft
->group
->members
; it
; it
= g_slist_next(it
)) {
584 ObClient
*c
= it
->data
;
585 /* check that it's not a helper window to avoid infinite recursion */
586 if (c
!= ft
&& !client_helper(c
) &&
587 valid_focus_target(c
, all_desktops
, FALSE
, FALSE
))
595 /*! @param allow_helpers This is used for calling itself recursively while
596 checking helper windows. */
597 static gboolean
valid_focus_target(ObClient
*ft
,
598 gboolean all_desktops
,
599 gboolean dock_windows
,
600 gboolean desktop_windows
)
604 /* it's on this desktop unless you want all desktops.
606 do this check first because it will usually filter out the most
608 ok
= (all_desktops
|| ft
->desktop
== screen_desktop
||
609 ft
->desktop
== DESKTOP_ALL
);
611 /* the window can receive focus somehow */
612 ok
= ok
&& (ft
->can_focus
|| ft
->focus_notify
);
614 /* it's the right type of window */
615 if (dock_windows
|| desktop_windows
)
616 ok
= ok
&& ((dock_windows
&& ft
->type
== OB_CLIENT_TYPE_DOCK
) ||
617 (desktop_windows
&& ft
->type
== OB_CLIENT_TYPE_DESKTOP
));
619 /* normal non-helper windows are valid targets */
621 ((client_normal(ft
) && !client_helper(ft
))
623 /* helper windows are valid targets it... */
624 (client_helper(ft
) &&
625 /* ...a window in its group already has focus ... */
626 ((focus_client
&& ft
->group
== focus_client
->group
) ||
627 /* ... or if there are no other windows in its group
628 that can be cycled to instead */
629 !has_valid_group_siblings_on_desktop(ft
, all_desktops
))));
631 /* it's not set to skip the taskbar (unless it is a type that would be
632 expected to set this hint */
633 ok
= ok
&& ((ft
->type
== OB_CLIENT_TYPE_DOCK
||
634 ft
->type
== OB_CLIENT_TYPE_DESKTOP
||
635 ft
->type
== OB_CLIENT_TYPE_TOOLBAR
||
636 ft
->type
== OB_CLIENT_TYPE_MENU
||
637 ft
->type
== OB_CLIENT_TYPE_UTILITY
) ||
640 /* it's not going to just send fous off somewhere else (modal window) */
641 ok
= ok
&& ft
== client_focus_target(ft
);
646 void focus_cycle(gboolean forward
, gboolean all_desktops
,
647 gboolean dock_windows
, gboolean desktop_windows
,
648 gboolean linear
, gboolean interactive
,
649 gboolean dialog
, gboolean done
, gboolean cancel
)
651 static ObClient
*first
= NULL
;
652 static ObClient
*t
= NULL
;
653 static GList
*order
= NULL
;
654 GList
*it
, *start
, *list
;
659 focus_cycle_target
= NULL
;
667 if (!first
) first
= focus_client
;
669 if (linear
) list
= client_list
;
670 else list
= focus_order
;
676 if (!focus_cycle_target
) focus_cycle_target
= focus_client
;
678 start
= it
= g_list_find(list
, focus_cycle_target
);
679 if (!start
) /* switched desktops or something? */
680 start
= it
= forward
? g_list_last(list
) : g_list_first(list
);
681 if (!start
) goto done_cycle
;
686 if (it
== NULL
) it
= g_list_first(list
);
689 if (it
== NULL
) it
= g_list_last(list
);
692 if (valid_focus_target(ft
, all_desktops
, dock_windows
,
696 if (ft
!= focus_cycle_target
) { /* prevents flicker */
697 focus_cycle_target
= ft
;
698 focus_cycle_draw_indicator();
700 /* same arguments as valid_focus_target */
701 popup_cycle(ft
, dialog
, all_desktops
, dock_windows
,
704 } else if (ft
!= focus_cycle_target
) {
705 focus_cycle_target
= ft
;
710 } while (it
!= start
);
713 if (done
&& focus_cycle_target
)
714 client_activate(focus_cycle_target
, FALSE
, TRUE
);
718 focus_cycle_target
= NULL
;
723 focus_cycle_draw_indicator();
724 popup_cycle(ft
, FALSE
, FALSE
, FALSE
, FALSE
);
730 /* this be mostly ripped from fvwm */
731 static ObClient
*focus_find_directional(ObClient
*c
, ObDirection dir
,
732 gboolean dock_windows
,
733 gboolean desktop_windows
)
735 gint my_cx
, my_cy
, his_cx
, his_cy
;
738 gint score
, best_score
;
739 ObClient
*best_client
, *cur
;
745 /* first, find the centre coords of the currently focused window */
746 my_cx
= c
->frame
->area
.x
+ c
->frame
->area
.width
/ 2;
747 my_cy
= c
->frame
->area
.y
+ c
->frame
->area
.height
/ 2;
752 for(it
= g_list_first(client_list
); it
; it
= g_list_next(it
)) {
755 /* the currently selected window isn't interesting */
758 if (cur
->type
== OB_CLIENT_TYPE_DOCK
&& !dock_windows
)
760 if (cur
->type
== OB_CLIENT_TYPE_DESKTOP
&& !desktop_windows
)
762 if (!client_normal(cur
) &&
763 cur
->type
!= OB_CLIENT_TYPE_DOCK
&&
764 cur
->type
!= OB_CLIENT_TYPE_DESKTOP
)
766 /* using c->desktop instead of screen_desktop doesn't work if the
767 * current window was omnipresent, hope this doesn't have any other
769 if(screen_desktop
!= cur
->desktop
&& cur
->desktop
!= DESKTOP_ALL
)
773 if(!(client_focus_target(cur
) == cur
&&
774 client_can_focus(cur
)))
777 /* find the centre coords of this window, from the
778 * currently focused window's point of view */
779 his_cx
= (cur
->frame
->area
.x
- my_cx
)
780 + cur
->frame
->area
.width
/ 2;
781 his_cy
= (cur
->frame
->area
.y
- my_cy
)
782 + cur
->frame
->area
.height
/ 2;
784 if(dir
== OB_DIRECTION_NORTHEAST
|| dir
== OB_DIRECTION_SOUTHEAST
||
785 dir
== OB_DIRECTION_SOUTHWEST
|| dir
== OB_DIRECTION_NORTHWEST
) {
787 /* Rotate the diagonals 45 degrees counterclockwise.
788 * To do this, multiply the matrix /+h +h\ with the
789 * vector (x y). \-h +h/
790 * h = sqrt(0.5). We can set h := 1 since absolute
791 * distance doesn't matter here. */
792 tx
= his_cx
+ his_cy
;
793 his_cy
= -his_cx
+ his_cy
;
798 case OB_DIRECTION_NORTH
:
799 case OB_DIRECTION_SOUTH
:
800 case OB_DIRECTION_NORTHEAST
:
801 case OB_DIRECTION_SOUTHWEST
:
802 offset
= (his_cx
< 0) ? -his_cx
: his_cx
;
803 distance
= ((dir
== OB_DIRECTION_NORTH
||
804 dir
== OB_DIRECTION_NORTHEAST
) ?
807 case OB_DIRECTION_EAST
:
808 case OB_DIRECTION_WEST
:
809 case OB_DIRECTION_SOUTHEAST
:
810 case OB_DIRECTION_NORTHWEST
:
811 offset
= (his_cy
< 0) ? -his_cy
: his_cy
;
812 distance
= ((dir
== OB_DIRECTION_WEST
||
813 dir
== OB_DIRECTION_NORTHWEST
) ?
818 /* the target must be in the requested direction */
822 /* Calculate score for this window. The smaller the better. */
823 score
= distance
+ offset
;
825 /* windows more than 45 degrees off the direction are
826 * heavily penalized and will only be chosen if nothing
827 * else within a million pixels */
828 if(offset
> distance
)
831 if(best_score
== -1 || score
< best_score
)
839 void focus_directional_cycle(ObDirection dir
, gboolean dock_windows
,
840 gboolean desktop_windows
, gboolean interactive
,
841 gboolean dialog
, gboolean done
, gboolean cancel
)
843 static ObClient
*first
= NULL
;
850 focus_cycle_target
= NULL
;
858 if (!first
) first
= focus_client
;
859 if (!focus_cycle_target
) focus_cycle_target
= focus_client
;
861 if (focus_cycle_target
)
862 ft
= focus_find_directional(focus_cycle_target
, dir
, dock_windows
,
867 for (it
= focus_order
; it
; it
= g_list_next(it
))
868 if (valid_focus_target(it
->data
, FALSE
, dock_windows
,
874 if (ft
!= focus_cycle_target
) {/* prevents flicker */
875 focus_cycle_target
= ft
;
876 focus_cycle_draw_indicator();
879 if (focus_cycle_target
) {
880 /* same arguments as valid_focus_target */
881 popup_cycle(focus_cycle_target
, dialog
, FALSE
, dock_windows
,
889 if (done
&& focus_cycle_target
)
890 client_activate(focus_cycle_target
, FALSE
, TRUE
);
893 focus_cycle_target
= NULL
;
895 focus_cycle_draw_indicator();
896 popup_cycle(ft
, FALSE
, FALSE
, FALSE
, FALSE
);
901 void focus_order_add_new(ObClient
*c
)
904 focus_order_to_top(c
);
906 g_assert(!g_list_find(focus_order
, c
));
907 /* if there are any iconic windows, put this above them in the order,
908 but if there are not, then put it under the currently focused one */
909 if (focus_order
&& ((ObClient
*)focus_order
->data
)->iconic
)
910 focus_order
= g_list_insert(focus_order
, c
, 0);
912 focus_order
= g_list_insert(focus_order
, c
, 1);
916 void focus_order_remove(ObClient
*c
)
918 focus_order
= g_list_remove(focus_order
, c
);
921 void focus_order_to_top(ObClient
*c
)
923 focus_order
= g_list_remove(focus_order
, c
);
925 focus_order
= g_list_prepend(focus_order
, c
);
929 /* insert before first iconic window */
930 for (it
= focus_order
;
931 it
&& !((ObClient
*)it
->data
)->iconic
; it
= g_list_next(it
));
932 focus_order
= g_list_insert_before(focus_order
, it
, c
);
936 void focus_order_to_bottom(ObClient
*c
)
938 focus_order
= g_list_remove(focus_order
, c
);
940 focus_order
= g_list_append(focus_order
, c
);
944 /* insert before first iconic window */
945 for (it
= focus_order
;
946 it
&& !((ObClient
*)it
->data
)->iconic
; it
= g_list_next(it
));
947 focus_order
= g_list_insert_before(focus_order
, it
, c
);
951 ObClient
*focus_order_find_first(guint desktop
)
954 for (it
= focus_order
; it
; it
= g_list_next(it
)) {
955 ObClient
*c
= it
->data
;
956 if (c
->desktop
== desktop
|| c
->desktop
== DESKTOP_ALL
)
962 static void focus_tried_hide_notify(ObClient
*client
, gpointer data
)
966 if (client
== focus_tried
) {
967 /* we were trying to focus this window but it's gone */
971 ob_debug_type(OB_DEBUG_FOCUS
, "Tried to focus window 0x%x and it "
972 "is being unmanaged:\n");
973 if (XCheckIfEvent(ob_display
, &ce
, event_look_for_focusin_client
,NULL
))
975 XPutBackEvent(ob_display
, &ce
);
976 ob_debug_type(OB_DEBUG_FOCUS
, " but another FocusIn is coming\n");
978 ob_debug_type(OB_DEBUG_FOCUS
, " so falling back focus again.\n");
979 focus_fallback(TRUE
);