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