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