sync after focusing
[dana/openbox.git] / openbox / focus.c
1 #include "event.h"
2 #include "config.h"
3 #include "openbox.h"
4 #include "client.h"
5 #include "frame.h"
6 #include "screen.h"
7 #include "prop.h"
8 #include "dispatch.h"
9 #include "focus.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
20 void focus_startup()
21 {
22     /* create the window which gets focus when no clients get it. Have to
23        make it override-redirect so we don't try manage it, since it is
24        mapped. */
25     XSetWindowAttributes attrib;
26
27     focus_client = NULL;
28
29     attrib.override_redirect = TRUE;
30     focus_backup = XCreateWindow(ob_display, ob_root,
31                                  -100, -100, 1, 1, 0,
32                                  CopyFromParent, InputOutput, CopyFromParent,
33                                  CWOverrideRedirect, &attrib);
34     XMapRaised(ob_display, focus_backup);
35
36     /* start with nothing focused */
37     focus_set_client(NULL);
38 }
39
40 void focus_shutdown()
41 {
42     guint i;
43
44     for (i = 0; i < screen_num_desktops; ++i)
45         g_list_free(focus_order[i]);
46     g_free(focus_order);
47     focus_order = NULL;
48
49     XDestroyWindow(ob_display, focus_backup);
50
51     /* reset focus to root */
52     XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
53                    event_lasttime);
54 }
55
56 void focus_set_client(Client *client)
57 {
58     Window active;
59     Client *old;
60     guint desktop;
61
62     /* uninstall the old colormap, and install the new one */
63     screen_install_colormap(focus_client, FALSE);
64     screen_install_colormap(client, TRUE);
65
66     if (client == NULL) {
67         /* when nothing will be focused, send focus to the backup target */
68         XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
69                        event_lasttime);
70         XSync(ob_display, FALSE);
71     }
72
73     old = focus_client;
74     focus_client = client;
75
76     /* move to the top of the list */
77     if (client != NULL) {
78         desktop = client->desktop;
79         if (desktop == DESKTOP_ALL) desktop = screen_desktop;
80         focus_order[desktop] = g_list_remove(focus_order[desktop], client);
81         focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
82     }
83
84     /* set the NET_ACTIVE_WINDOW hint */
85     active = client ? client->window : None;
86     PROP_SET32(ob_root, net_active_window, window, active);
87
88     if (focus_client != NULL)
89         dispatch_client(Event_Client_Focus, focus_client, 0, 0);
90     if (old != NULL)
91         dispatch_client(Event_Client_Unfocus, old, 0, 0);
92 }
93
94 static gboolean focus_under_pointer()
95 {
96     Window w;
97     int i, x, y;
98     guint u;
99     GList *it;
100
101     if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u)) {
102         for (it = stacking_list; it != NULL; it = it->next) {
103             Client *c = it->data;
104             if (c->desktop == screen_desktop &&
105                 RECT_CONTAINS(c->frame->area, x, y))
106                 break;
107         }
108         if (it != NULL)
109             return client_normal(it->data) && client_focus(it->data);
110     }
111     return FALSE;
112 }
113
114 void focus_fallback(gboolean switching_desks)
115 {
116     ConfigValue focus_follow;
117     GList *it;
118     gboolean under = FALSE;
119
120     if (switching_desks) {
121         /* don't skip any windows when switching desktops */
122         focus_client = NULL;
123     } else {
124         if (!config_get("focusFollowsMouse", Config_Bool, &focus_follow))
125             g_assert_not_reached();
126         if (focus_follow.bool)
127             under = focus_under_pointer();
128     }
129
130     if (!under) {
131         for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
132             if (it->data != focus_client && client_normal(it->data))
133                 if (client_focus(it->data))
134                     break;
135         if (it == NULL) /* nothing to focus */
136             focus_set_client(NULL);
137     }
138 }