Make the focus cycle indicator follow target fallback in the popup
[mikachu/openbox.git] / openbox / focus_cycle.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    focus_cycle.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
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.
11
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.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "focus_cycle.h"
21 #include "focus_cycle_indicator.h"
22 #include "focus_cycle_popup.h"
23 #include "client.h"
24 #include "frame.h"
25 #include "focus.h"
26 #include "screen.h"
27 #include "openbox.h"
28 #include "debug.h"
29
30 #include <X11/Xlib.h>
31 #include <glib.h>
32
33 typedef enum {
34     OB_CYCLE_NONE = 0,
35     OB_CYCLE_NORMAL,
36     OB_CYCLE_DIRECTIONAL
37 } ObCycleType;
38
39 ObClient       *focus_cycle_target = NULL;
40 static ObCycleType focus_cycle_type = OB_CYCLE_NONE;
41 static gboolean focus_cycle_iconic_windows;
42 static gboolean focus_cycle_all_desktops;
43 static gboolean focus_cycle_dock_windows;
44 static gboolean focus_cycle_desktop_windows;
45
46 static ObClient *focus_find_directional(ObClient *c,
47                                         ObDirection dir,
48                                         gboolean dock_windows,
49                                         gboolean desktop_windows);
50
51 void focus_cycle_startup(gboolean reconfig)
52 {
53     if (reconfig) return;
54 }
55
56 void focus_cycle_shutdown(gboolean reconfig)
57 {
58     if (reconfig) return;
59 }
60
61 void focus_cycle_addremove(ObClient *c, gboolean redraw)
62 {
63     if (!focus_cycle_type)
64         return;
65
66     if (focus_cycle_type == OB_CYCLE_DIRECTIONAL) {
67         if (c && focus_cycle_target == c) {
68             focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE,
69                                     TRUE, TRUE, TRUE);
70         }
71     }
72     else if (c && redraw) {
73         gboolean v, s;
74
75         v = focus_cycle_valid(c);
76         s = focus_cycle_popup_is_showing(c);
77
78         if (v != s)
79             focus_cycle_reorder();
80     }
81     else if (redraw) {
82         focus_cycle_reorder();
83     }
84 }
85
86 void focus_cycle_reorder()
87 {
88     if (focus_cycle_type == OB_CYCLE_NORMAL) {
89         focus_cycle_target = focus_cycle_popup_refresh(focus_cycle_target,
90                                                        TRUE);
91         focus_cycle_update_indicator(focus_cycle_target);
92         if (!focus_cycle_target)
93             focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE,
94                         TRUE, TRUE, TRUE, TRUE, TRUE);
95     }
96 }
97
98 ObClient* focus_cycle(gboolean forward, gboolean all_desktops,
99                       gboolean dock_windows, gboolean desktop_windows,
100                       gboolean linear, gboolean interactive,
101                       gboolean showbar, gboolean dialog,
102                       gboolean done, gboolean cancel)
103 {
104     static GList *order = NULL;
105     GList *it, *start, *list;
106     ObClient *ft = NULL;
107     ObClient *ret = NULL;
108
109     if (interactive) {
110         if (cancel) {
111             focus_cycle_target = NULL;
112             goto done_cycle;
113         } else if (done)
114             goto done_cycle;
115
116         if (!focus_order)
117             goto done_cycle;
118
119         if (linear) list = client_list;
120         else        list = focus_order;
121     } else {
122         if (!focus_order)
123             goto done_cycle;
124         list = client_list;
125     }
126
127     if (focus_cycle_target == NULL) {
128         focus_cycle_iconic_windows = TRUE;
129         focus_cycle_all_desktops = all_desktops;
130         focus_cycle_dock_windows = dock_windows;
131         focus_cycle_desktop_windows = desktop_windows;
132         start = it = g_list_find(list, focus_client);
133     } else
134         start = it = g_list_find(list, focus_cycle_target);
135
136     if (!start) /* switched desktops or something? */
137         start = it = forward ? g_list_last(list) : g_list_first(list);
138     if (!start) goto done_cycle;
139
140     do {
141         if (forward) {
142             it = it->next;
143             if (it == NULL) it = g_list_first(list);
144         } else {
145             it = it->prev;
146             if (it == NULL) it = g_list_last(list);
147         }
148         ft = it->data;
149         if (focus_cycle_valid(ft)) {
150             if (interactive) {
151                 if (ft != focus_cycle_target) { /* prevents flicker */
152                     focus_cycle_target = ft;
153                     focus_cycle_type = OB_CYCLE_NORMAL;
154                     focus_cycle_draw_indicator(showbar ? ft : NULL);
155                 }
156                 if (dialog)
157                     /* same arguments as focus_target_valid */
158                     focus_cycle_popup_show(ft,
159                                            focus_cycle_iconic_windows,
160                                            focus_cycle_all_desktops,
161                                            focus_cycle_dock_windows,
162                                            focus_cycle_desktop_windows);
163                 return focus_cycle_target;
164             } else if (ft != focus_cycle_target) {
165                 focus_cycle_target = ft;
166                 focus_cycle_type = OB_CYCLE_NORMAL;
167                 done = TRUE;
168                 break;
169             }
170         }
171     } while (it != start);
172
173 done_cycle:
174     if (done && !cancel) ret = focus_cycle_target;
175
176     focus_cycle_target = NULL;
177     focus_cycle_type = OB_CYCLE_NONE;
178     g_list_free(order);
179     order = NULL;
180
181     if (interactive) {
182         focus_cycle_draw_indicator(NULL);
183         focus_cycle_popup_hide();
184     }
185
186     return ret;
187 }
188
189 /* this be mostly ripped from fvwm */
190 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
191                                         gboolean dock_windows,
192                                         gboolean desktop_windows)
193 {
194     gint my_cx, my_cy, his_cx, his_cy;
195     gint offset = 0;
196     gint distance = 0;
197     gint score, best_score;
198     ObClient *best_client, *cur;
199     GList *it;
200
201     if (!client_list)
202         return NULL;
203
204     /* first, find the centre coords of the currently focused window */
205     my_cx = c->frame->area.x + c->frame->area.width / 2;
206     my_cy = c->frame->area.y + c->frame->area.height / 2;
207
208     best_score = -1;
209     best_client = c;
210
211     for (it = g_list_first(client_list); it; it = g_list_next(it)) {
212         cur = it->data;
213
214         /* the currently selected window isn't interesting */
215         if (cur == c)
216             continue;
217         if (!focus_cycle_valid(it->data))
218             continue;
219
220         /* find the centre coords of this window, from the
221          * currently focused window's point of view */
222         his_cx = (cur->frame->area.x - my_cx)
223             + cur->frame->area.width / 2;
224         his_cy = (cur->frame->area.y - my_cy)
225             + cur->frame->area.height / 2;
226
227         if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
228             dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
229         {
230             gint tx;
231             /* Rotate the diagonals 45 degrees counterclockwise.
232              * To do this, multiply the matrix /+h +h\ with the
233              * vector (x y).                   \-h +h/
234              * h = sqrt(0.5). We can set h := 1 since absolute
235              * distance doesn't matter here. */
236             tx = his_cx + his_cy;
237             his_cy = -his_cx + his_cy;
238             his_cx = tx;
239         }
240
241         switch (dir) {
242         case OB_DIRECTION_NORTH:
243         case OB_DIRECTION_SOUTH:
244         case OB_DIRECTION_NORTHEAST:
245         case OB_DIRECTION_SOUTHWEST:
246             offset = (his_cx < 0) ? -his_cx : his_cx;
247             distance = ((dir == OB_DIRECTION_NORTH ||
248                          dir == OB_DIRECTION_NORTHEAST) ?
249                         -his_cy : his_cy);
250             break;
251         case OB_DIRECTION_EAST:
252         case OB_DIRECTION_WEST:
253         case OB_DIRECTION_SOUTHEAST:
254         case OB_DIRECTION_NORTHWEST:
255             offset = (his_cy < 0) ? -his_cy : his_cy;
256             distance = ((dir == OB_DIRECTION_WEST ||
257                          dir == OB_DIRECTION_NORTHWEST) ?
258                         -his_cx : his_cx);
259             break;
260         }
261
262         /* the target must be in the requested direction */
263         if (distance <= 0)
264             continue;
265
266         /* Calculate score for this window.  The smaller the better. */
267         score = distance + offset;
268
269         /* windows more than 45 degrees off the direction are
270          * heavily penalized and will only be chosen if nothing
271          * else within a million pixels */
272         if (offset > distance)
273             score += 1000000;
274
275         if (best_score == -1 || score < best_score) {
276             best_client = cur;
277             best_score = score;
278         }
279     }
280
281     return best_client;
282 }
283
284 ObClient* focus_directional_cycle(ObDirection dir, gboolean dock_windows,
285                                   gboolean desktop_windows,
286                                   gboolean interactive,
287                                   gboolean showbar, gboolean dialog,
288                                   gboolean done, gboolean cancel)
289 {
290     static ObClient *first = NULL;
291     ObClient *ft = NULL;
292     ObClient *ret = NULL;
293
294     if (cancel) {
295         focus_cycle_target = NULL;
296         goto done_cycle;
297     } else if (done && interactive)
298         goto done_cycle;
299
300     if (!focus_order)
301         goto done_cycle;
302
303     if (focus_cycle_target == NULL) {
304         focus_cycle_iconic_windows = FALSE;
305         focus_cycle_all_desktops = FALSE;
306         focus_cycle_dock_windows = dock_windows;
307         focus_cycle_desktop_windows = desktop_windows;
308     }
309
310     if (!first) first = focus_client;
311
312     if (focus_cycle_target)
313         ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
314                                     desktop_windows);
315     else if (first)
316         ft = focus_find_directional(first, dir, dock_windows, desktop_windows);
317     else {
318         GList *it;
319
320         for (it = focus_order; it; it = g_list_next(it))
321             if (focus_cycle_valid(it->data)) {
322                 ft = it->data;
323                 break;
324             }
325     }
326
327     if (ft && ft != focus_cycle_target) {/* prevents flicker */
328         focus_cycle_target = ft;
329         focus_cycle_type = OB_CYCLE_DIRECTIONAL;
330         if (!interactive)
331             goto done_cycle;
332         focus_cycle_draw_indicator(showbar ? ft : NULL);
333     }
334     if (focus_cycle_target && dialog)
335         /* same arguments as focus_target_valid */
336         focus_cycle_popup_single_show(focus_cycle_target,
337                                       focus_cycle_iconic_windows,
338                                       focus_cycle_all_desktops,
339                                       focus_cycle_dock_windows,
340                                       focus_cycle_desktop_windows);
341     return focus_cycle_target;
342
343 done_cycle:
344     if (done && !cancel) ret = focus_cycle_target;
345
346     first = NULL;
347     focus_cycle_target = NULL;
348     focus_cycle_type = OB_CYCLE_NONE;
349
350     focus_cycle_draw_indicator(NULL);
351     focus_cycle_popup_single_hide();
352
353     return ret;
354 }
355
356 gboolean focus_cycle_valid(struct _ObClient *client)
357 {
358     return focus_valid_target(client, TRUE,
359                               focus_cycle_iconic_windows,
360                               focus_cycle_all_desktops,
361                               focus_cycle_dock_windows,
362                               focus_cycle_desktop_windows,
363                               FALSE);
364 }