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