]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus.c
only cycle to visible windows
[dana/openbox.git] / openbox / focus.c
1 #include "event.h"
2 #include "openbox.h"
3 #include "client.h"
4 #include "config.h"
5 #include "frame.h"
6 #include "screen.h"
7 #include "group.h"
8 #include "prop.h"
9 #include "dispatch.h"
10 #include "focus.h"
11 #include "parse.h"
12 #include "stacking.h"
13
14 #include <X11/Xlib.h>
15 #include <glib.h>
16
17 Client *focus_client = NULL;
18 GList **focus_order = NULL; /* these lists are created when screen_startup
19                                sets the number of desktops */
20
21 Window focus_backup = None;
22
23 static gboolean noreorder = 0;
24
25 void focus_startup()
26 {
27     /* create the window which gets focus when no clients get it. Have to
28        make it override-redirect so we don't try manage it, since it is
29        mapped. */
30     XSetWindowAttributes attrib;
31
32     focus_client = NULL;
33
34     attrib.override_redirect = TRUE;
35     focus_backup = XCreateWindow(ob_display, ob_root,
36                                  -100, -100, 1, 1, 0,
37                                  CopyFromParent, InputOutput, CopyFromParent,
38                                  CWOverrideRedirect, &attrib);
39     XMapWindow(ob_display, focus_backup);
40     stacking_raise_internal(focus_backup);
41
42     /* start with nothing focused */
43     focus_set_client(NULL);
44 }
45
46 void focus_shutdown()
47 {
48     guint i;
49
50     for (i = 0; i < screen_num_desktops; ++i)
51         g_list_free(focus_order[i]);
52     g_free(focus_order);
53     focus_order = NULL;
54
55     XDestroyWindow(ob_display, focus_backup);
56
57     /* reset focus to root */
58     XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
59                    event_lasttime);
60 }
61
62 static void push_to_top(Client *client)
63 {
64     guint desktop;
65
66     desktop = client->desktop;
67     if (desktop == DESKTOP_ALL) desktop = screen_desktop;
68     focus_order[desktop] = g_list_remove(focus_order[desktop], client);
69     focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
70 }
71
72 void focus_set_client(Client *client)
73 {
74     Window active;
75     Client *old;
76
77     /* uninstall the old colormap, and install the new one */
78     screen_install_colormap(focus_client, FALSE);
79     screen_install_colormap(client, TRUE);
80
81     if (client == NULL) {
82         /* when nothing will be focused, send focus to the backup target */
83         XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
84                        event_lasttime);
85         XSync(ob_display, FALSE);
86     }
87
88     old = focus_client;
89     focus_client = client;
90
91     /* move to the top of the list */
92     if (noreorder)
93         --noreorder;
94     else if (client != NULL)
95         push_to_top(client);
96
97     /* set the NET_ACTIVE_WINDOW hint */
98     active = client ? client->window : None;
99     PROP_SET32(ob_root, net_active_window, window, active);
100
101     if (focus_client != NULL)
102         dispatch_client(Event_Client_Focus, focus_client, 0, 0);
103     if (old != NULL)
104         dispatch_client(Event_Client_Unfocus, old, 0, 0);
105 }
106
107 static gboolean focus_under_pointer()
108 {
109     Window w;
110     int i, x, y;
111     guint u;
112     GList *it;
113
114     if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u)) {
115         for (it = stacking_list; it != NULL; it = it->next) {
116             Client *c = it->data;
117             if (c->desktop == screen_desktop &&
118                 RECT_CONTAINS(c->frame->area, x, y))
119                 break;
120         }
121         if (it != NULL)
122             return client_normal(it->data) && client_focus(it->data);
123     }
124     return FALSE;
125 }
126
127 void focus_fallback(FallbackType type)
128 {
129     GList *it;
130     Client *old = NULL;
131
132     old = focus_client;
133
134     /* unfocus any focused clients.. they can be focused by Pointer events
135        and such, and then when I try focus them, I won't get a FocusIn event
136        at all for them.
137     */
138     focus_set_client(NULL);
139
140     if (!(type == Fallback_Desktop ?
141           config_focus_last_on_desktop : config_focus_last)) {
142         if (config_focus_follow) focus_under_pointer();
143         return;
144     }
145
146     if (type == Fallback_Unfocusing && old && old->transient_for) {
147         if (old->transient_for == TRAN_GROUP) {
148             for (it = focus_order[screen_desktop]; it != NULL; it = it->next) {
149                 GSList *sit;
150
151                 for (sit = old->group->members; sit; sit = sit->next)
152                     if (sit->data == it->data && client_focus(sit->data))
153                         return;
154             }
155         } else {
156             if (client_normal(old->transient_for))
157                 if (client_focus(old->transient_for))
158                     return;
159         }
160     }
161
162     for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
163         if (type != Fallback_Unfocusing || it->data != old)
164             if (client_normal(it->data) &&
165                 ((Client*)it->data)->frame->visible &&
166                 client_focus(it->data))
167                 return;
168
169     /* nothing to focus */
170     focus_set_client(NULL);
171 }
172
173 Client *focus_cycle(gboolean forward, gboolean linear, gboolean done,
174                  gboolean cancel)
175 {
176     static Client *first = NULL;
177     static Client *t = NULL;
178     static GList *order = NULL;
179     GList *it, *start, *list;
180     Client *ft;
181
182     if (cancel) {
183         if (first) client_focus(first);
184         goto done_cycle;
185     } else if (done) {
186         if (focus_client) {
187             push_to_top(focus_client); /* move to top of focus_order */
188             stacking_raise(focus_client);
189         }
190         goto done_cycle;
191     }
192     if (!first) first = focus_client;
193
194     if (linear) list = client_list;
195     else        list = focus_order[screen_desktop];
196
197     start = it = g_list_find(list, focus_client);
198     if (!start) /* switched desktops or something? */
199         start = it = forward ? g_list_last(list) : g_list_first(list);
200     if (!start) goto done_cycle;
201
202     do {
203         if (forward) {
204             it = it->next;
205             if (it == NULL) it = list;
206         } else {
207             it = it->prev;
208             if (it == NULL) it = g_list_last(list);
209         }
210         ft = client_focus_target(it->data);
211         if (ft == it->data && focus_client != ft && client_normal(ft) &&
212             ft->frame->visible && client_focus(ft)) {
213             noreorder++; /* avoid reordering the focus_order */
214             return ft;
215         }
216     } while (it != start);
217     return NULL;
218
219 done_cycle:
220     t = NULL;
221     first = NULL;
222     g_list_free(order);
223     order = NULL;
224     return NULL;
225 }