]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus.c
remove debug print
[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 }
93
94 void focus_set_client(Client *client)
95 {
96     Window active;
97     Client *old;
98
99     /* uninstall the old colormap, and install the new one */
100     screen_install_colormap(focus_client, FALSE);
101     screen_install_colormap(client, TRUE);
102
103     if (client == NULL) {
104         /* when nothing will be focused, send focus to the backup target */
105         XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
106                        event_lasttime);
107         XSync(ob_display, FALSE);
108     }
109
110     old = focus_client;
111     focus_client = client;
112
113     /* move to the top of the list */
114     if (noreorder)
115         --noreorder;
116     else if (client != NULL)
117         push_to_top(client);
118
119     /* set the NET_ACTIVE_WINDOW hint */
120     active = client ? client->window : None;
121     PROP_SET32(ob_root, net_active_window, window, active);
122
123     if (focus_client != NULL)
124         dispatch_client(Event_Client_Focus, focus_client, 0, 0);
125     if (old != NULL)
126         dispatch_client(Event_Client_Unfocus, old, 0, 0);
127 }
128
129 static gboolean focus_under_pointer()
130 {
131     Window w;
132     int i, x, y;
133     guint u;
134     GList *it;
135
136     if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u)) {
137         for (it = stacking_list; it != NULL; it = it->next) {
138             Client *c = it->data;
139             if (c->desktop == screen_desktop &&
140                 RECT_CONTAINS(c->frame->area, x, y))
141                 break;
142         }
143         if (it != NULL)
144             return client_normal(it->data) && client_focus(it->data);
145     }
146     return FALSE;
147 }
148
149 void focus_fallback(gboolean switching_desks)
150 {
151     GList *it;
152     gboolean under = FALSE;
153     Client *old = NULL;
154
155     old = focus_client;
156
157     /* unfocus any focused clients.. they can be focused by Pointer events
158        and such, and then when I try focus them, I won't get a FocusIn event
159        at all for them.
160     */
161     focus_set_client(NULL);
162
163     if (switching_desks) {
164         /* don't skip any windows when switching desktops */
165         old = NULL;
166     } else {
167         if (focus_follow)
168             under = focus_under_pointer();
169     }
170
171     if (!under) {
172         for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
173             if (it->data != old && client_normal(it->data))
174                 if (client_focus(it->data))
175                     break;
176         if (it == NULL) /* nothing to focus */
177             focus_set_client(NULL);
178     }
179 }
180
181 void focus_cycle(gboolean forward, gboolean linear, gboolean done,
182                  gboolean cancel)
183 {
184     static Client *first = NULL;
185     static Client *t = NULL;
186     static GList *order = NULL;
187     GList *it, *start, *list;
188     Client *ft;
189
190     if (cancel) {
191         if (first) client_focus(first);
192         goto done_cycle;
193     } else if (done) {
194         if (focus_client) {
195             push_to_top(focus_client); /* move to top of focus_order */
196             stacking_raise(focus_client);
197         }
198         goto done_cycle;
199     }
200     if (!first) first = focus_client;
201
202     if (linear) list = client_list;
203     else        list = focus_order[screen_desktop];
204
205     start = it = g_list_find(list, focus_client);
206     if (!start) goto done_cycle; /* switched desktops or something? */
207
208     do {
209         if (forward) {
210             it = it->next;
211             if (it == NULL) it = list;
212         } else {
213             it = it->prev;
214             if (it == NULL) it = g_list_last(list);
215         }
216         ft = client_focus_target(it->data);
217         if (ft == it->data && focus_client != ft && client_focusable(ft)) {
218             if (client_focus(ft)) {
219                 noreorder++; /* avoid reordering the focus_order */
220                 break;
221             }
222         }
223     } while (it != start);
224     return;
225
226 done_cycle:
227     t = NULL;
228     first = NULL;
229     g_list_free(order);
230     order = NULL;
231 }