]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus_cycle.c
Show the submenu and move focus there with the enter key when the submenu isn't shown...
[dana/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 "client.h"
23 #include "frame.h"
24 #include "focus.h"
25 #include "screen.h"
26 #include "openbox.h"
27 #include "debug.h"
28
29 #include <X11/Xlib.h>
30 #include <glib.h>
31
32 typedef enum {
33     OB_CYCLE_NONE = 0,
34     OB_CYCLE_NORMAL,
35     OB_CYCLE_DIRECTIONAL
36 } ObCycleType;
37
38 ObClient       *focus_cycle_target = NULL;
39 static ObCycleType focus_cycle_type = OB_CYCLE_NONE;
40 static gboolean focus_cycle_linear;
41 static gboolean focus_cycle_iconic_windows;
42 static gboolean focus_cycle_all_desktops;
43 static gboolean focus_cycle_nonhilite_windows;
44 static gboolean focus_cycle_dock_windows;
45 static gboolean focus_cycle_desktop_windows;
46
47 static ObClient *focus_find_directional(ObClient *c,
48                                         ObDirection dir,
49                                         gboolean dock_windows,
50                                         gboolean desktop_windows);
51
52 void focus_cycle_startup(gboolean reconfig)
53 {
54     if (reconfig) return;
55 }
56
57 void focus_cycle_shutdown(gboolean reconfig)
58 {
59     if (reconfig) return;
60 }
61
62 void focus_cycle_addremove(ObClient *c, gboolean redraw)
63 {
64     if (!focus_cycle_type)
65         return;
66
67     if (focus_cycle_type == OB_CYCLE_DIRECTIONAL) {
68         if (c && focus_cycle_target == c) {
69             focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE,
70                                     TRUE, TRUE, TRUE);
71         }
72     }
73     else if (c && redraw) {
74         gboolean v, s;
75
76         v = focus_cycle_valid(c);
77         s = focus_cycle_popup_is_showing(c);
78
79         if (v != s)
80             focus_cycle_reorder();
81     }
82     else if (redraw) {
83         focus_cycle_reorder();
84     }
85 }
86
87 void focus_cycle_reorder()
88 {
89     if (focus_cycle_type == OB_CYCLE_NORMAL) {
90         focus_cycle_target = focus_cycle_popup_refresh(focus_cycle_target,
91                                                        TRUE,
92                                                        focus_cycle_linear);
93         focus_cycle_update_indicator(focus_cycle_target);
94         if (!focus_cycle_target)
95             focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
96                         TRUE, TRUE, TRUE, TRUE, TRUE);
97     }
98 }
99
100 ObClient* focus_cycle(gboolean forward, gboolean all_desktops,
101                       gboolean nonhilite_windows,
102                       gboolean dock_windows, gboolean desktop_windows,
103                       gboolean linear, gboolean interactive,
104                       gboolean showbar, ObFocusCyclePopupMode mode,
105                       gboolean done, gboolean cancel)
106 {
107     static GList *order = NULL;
108     GList *it, *start, *list;
109     ObClient *ft = NULL;
110     ObClient *ret = NULL;
111
112     if (interactive) {
113         if (cancel) {
114             focus_cycle_target = NULL;
115             goto done_cycle;
116         } else if (done)
117             goto done_cycle;
118
119         if (!focus_order)
120             goto done_cycle;
121
122         if (linear) list = client_list;
123         else        list = focus_order;
124     } else {
125         if (!focus_order)
126             goto done_cycle;
127         list = client_list;
128     }
129
130     if (focus_cycle_target == NULL) {
131         focus_cycle_linear = linear;
132         focus_cycle_iconic_windows = TRUE;
133         focus_cycle_all_desktops = all_desktops;
134         focus_cycle_nonhilite_windows = nonhilite_windows;
135         focus_cycle_dock_windows = dock_windows;
136         focus_cycle_desktop_windows = desktop_windows;
137         start = it = g_list_find(list, focus_client);
138     } else
139         start = it = g_list_find(list, focus_cycle_target);
140
141     if (!start) /* switched desktops or something? */
142         start = it = forward ? g_list_last(list) : g_list_first(list);
143     if (!start) goto done_cycle;
144
145     do {
146         if (forward) {
147             it = it->next;
148             if (it == NULL) it = g_list_first(list);
149         } else {
150             it = it->prev;
151             if (it == NULL) it = g_list_last(list);
152         }
153         ft = it->data;
154         if (focus_cycle_valid(ft)) {
155             if (interactive) {
156                 if (ft != focus_cycle_target) { /* prevents flicker */
157                     focus_cycle_target = ft;
158                     focus_cycle_type = OB_CYCLE_NORMAL;
159                     focus_cycle_draw_indicator(showbar ? ft : NULL);
160                 }
161                 /* same arguments as focus_target_valid */
162                 focus_cycle_popup_show(ft, mode, focus_cycle_linear);
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_linear = FALSE;
305         focus_cycle_iconic_windows = FALSE;
306         focus_cycle_all_desktops = FALSE;
307         focus_cycle_nonhilite_windows = TRUE;
308         focus_cycle_dock_windows = dock_windows;
309         focus_cycle_desktop_windows = desktop_windows;
310     }
311
312     if (!first) first = focus_client;
313
314     if (focus_cycle_target)
315         ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
316                                     desktop_windows);
317     else if (first)
318         ft = focus_find_directional(first, dir, dock_windows, desktop_windows);
319     else {
320         GList *it;
321
322         for (it = focus_order; it; it = g_list_next(it))
323             if (focus_cycle_valid(it->data)) {
324                 ft = it->data;
325                 break;
326             }
327     }
328
329     if (ft && ft != focus_cycle_target) {/* prevents flicker */
330         focus_cycle_target = ft;
331         focus_cycle_type = OB_CYCLE_DIRECTIONAL;
332         if (!interactive)
333             goto done_cycle;
334         focus_cycle_draw_indicator(showbar ? ft : NULL);
335     }
336     if (focus_cycle_target && dialog)
337         /* same arguments as focus_target_valid */
338         focus_cycle_popup_single_show(focus_cycle_target);
339     return focus_cycle_target;
340
341 done_cycle:
342     if (done && !cancel) ret = focus_cycle_target;
343
344     first = NULL;
345     focus_cycle_target = NULL;
346     focus_cycle_type = OB_CYCLE_NONE;
347
348     focus_cycle_draw_indicator(NULL);
349     focus_cycle_popup_single_hide();
350
351     return ret;
352 }
353
354 gboolean focus_cycle_valid(struct _ObClient *client)
355 {
356     return focus_valid_target(client, screen_desktop, TRUE,
357                               focus_cycle_iconic_windows,
358                               focus_cycle_all_desktops,
359                               focus_cycle_nonhilite_windows,
360                               focus_cycle_dock_windows,
361                               focus_cycle_desktop_windows,
362                               FALSE);
363 }