allow specifying that some focus events should be ignored for the focus order list
[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
11 #include <X11/Xlib.h>
12 #include <glib.h>
13
14 Client *focus_client = NULL;
15 GList **focus_order = NULL; /* these lists are created when screen_startup
16                                sets the number of desktops */
17
18 Window focus_backup = None;
19 gboolean focus_new = TRUE;
20 gboolean focus_follow = TRUE;
21 int focus_ignore_in = 0;
22
23 static void parse_assign(char *name, ParseToken *value)
24 {
25     if (!g_ascii_strcasecmp(name, "focusnew")) {
26         if (value->type != TOKEN_BOOL)
27             yyerror("invalid value");
28         else {
29             focus_new = value->data.bool;
30         }
31     } else if (!g_ascii_strcasecmp(name, "followmouse")) {
32         if (value->type != TOKEN_BOOL)
33             yyerror("invalid value");
34         else {
35             focus_follow = value->data.bool;
36         }
37     } else
38         yyerror("invalid option");
39     parse_free_token(value);
40 }
41
42 void focus_startup()
43 {
44     /* create the window which gets focus when no clients get it. Have to
45        make it override-redirect so we don't try manage it, since it is
46        mapped. */
47     XSetWindowAttributes attrib;
48
49     focus_client = NULL;
50     focus_new = TRUE;
51     focus_follow = TRUE;
52
53     attrib.override_redirect = TRUE;
54     focus_backup = XCreateWindow(ob_display, ob_root,
55                                  -100, -100, 1, 1, 0,
56                                  CopyFromParent, InputOutput, CopyFromParent,
57                                  CWOverrideRedirect, &attrib);
58     XMapRaised(ob_display, focus_backup);
59
60     /* start with nothing focused */
61     focus_set_client(NULL);
62
63     parse_reg_section("focus", NULL, parse_assign);
64 }
65
66 void focus_shutdown()
67 {
68     guint i;
69
70     for (i = 0; i < screen_num_desktops; ++i)
71         g_list_free(focus_order[i]);
72     g_free(focus_order);
73     focus_order = NULL;
74
75     XDestroyWindow(ob_display, focus_backup);
76
77     /* reset focus to root */
78     XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
79                    event_lasttime);
80 }
81
82 void focus_set_client(Client *client)
83 {
84     Window active;
85     Client *old;
86     guint desktop;
87
88     /* uninstall the old colormap, and install the new one */
89     screen_install_colormap(focus_client, FALSE);
90     screen_install_colormap(client, TRUE);
91
92     if (client == NULL) {
93         /* when nothing will be focused, send focus to the backup target */
94         XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
95                        event_lasttime);
96         XSync(ob_display, FALSE);
97     }
98
99     old = focus_client;
100     focus_client = client;
101
102     /* move to the top of the list */
103     if (focus_ignore_in) {
104         g_assert(focus_ignore_in > 0);
105         --focus_ignore_in;
106     } else if (client != NULL) {
107         desktop = client->desktop;
108         if (desktop == DESKTOP_ALL) desktop = screen_desktop;
109         focus_order[desktop] = g_list_remove(focus_order[desktop], client);
110         focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
111     }
112
113     /* set the NET_ACTIVE_WINDOW hint */
114     active = client ? client->window : None;
115     PROP_SET32(ob_root, net_active_window, window, active);
116
117     if (focus_client != NULL)
118         dispatch_client(Event_Client_Focus, focus_client, 0, 0);
119     if (old != NULL)
120         dispatch_client(Event_Client_Unfocus, old, 0, 0);
121 }
122
123 static gboolean focus_under_pointer()
124 {
125     Window w;
126     int i, x, y;
127     guint u;
128     GList *it;
129
130     if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u)) {
131         for (it = stacking_list; it != NULL; it = it->next) {
132             Client *c = it->data;
133             if (c->desktop == screen_desktop &&
134                 RECT_CONTAINS(c->frame->area, x, y))
135                 break;
136         }
137         if (it != NULL)
138             return client_normal(it->data) && client_focus(it->data);
139     }
140     return FALSE;
141 }
142
143 void focus_fallback(gboolean switching_desks)
144 {
145     GList *it;
146     gboolean under = FALSE;
147     Client *old = NULL;
148
149     old = focus_client;
150
151     /* unfocus any focused clients.. they can be focused by Pointer events
152        and such, and then when I try focus them, I won't get a FocusIn event
153        at all for them.
154     */
155     focus_set_client(NULL);
156
157     if (switching_desks) {
158         /* don't skip any windows when switching desktops */
159         old = NULL;
160     } else {
161         if (focus_follow)
162             under = focus_under_pointer();
163     }
164
165     if (!under) {
166         for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
167             if (it->data != old && client_normal(it->data))
168                 if (client_focus(it->data))
169                     break;
170         if (it == NULL) /* nothing to focus */
171             focus_set_client(NULL);
172     }
173 }