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