rm a bunch of prints. make switching to an empty desktop not kill focus
[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     }
71
72     old = focus_client;
73     focus_client = client;
74
75     /* move to the top of the list */
76     if (client != NULL) {
77         desktop = client->desktop;
78         if (desktop == DESKTOP_ALL) desktop = screen_desktop;
79         focus_order[desktop] = g_list_remove(focus_order[desktop], client);
80         focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
81     }
82
83     /* set the NET_ACTIVE_WINDOW hint */
84     active = client ? client->window : None;
85     PROP_SET32(ob_root, net_active_window, window, active);
86
87     if (focus_client != NULL)
88         dispatch_client(Event_Client_Focus, focus_client, 0, 0);
89     if (old != NULL)
90         dispatch_client(Event_Client_Unfocus, old, 0, 0);
91 }
92
93 static gboolean focus_under_pointer()
94 {
95     Window w;
96     int i, x, y;
97     guint u;
98     GList *it;
99
100     if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u)) {
101         for (it = stacking_list; it != NULL; it = it->next) {
102             Client *c = it->data;
103             if (c->desktop == screen_desktop &&
104                 RECT_CONTAINS(c->frame->area, x, y))
105                 break;
106         }
107         if (it != NULL)
108             return client_normal(it->data) && client_focus(it->data);
109     }
110     return FALSE;
111 }
112
113 void focus_fallback(gboolean switching_desks)
114 {
115     ConfigValue focus_follow;
116     GList *it;
117     gboolean under = FALSE;
118
119     if (switching_desks) {
120         /* don't skip any windows when switching desktops */
121         focus_client = NULL;
122     } else {
123         if (!config_get("focusFollowsMouse", Config_Bool, &focus_follow))
124             g_assert_not_reached();
125         if (focus_follow.bool)
126             under = focus_under_pointer();
127     }
128
129     if (!under) {
130         for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
131             if (it->data != focus_client && client_normal(it->data))
132                 if (client_focus(it->data))
133                     break;
134         if (it == NULL) /* nothing to focus */
135             focus_set_client(NULL);
136     }
137 }