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