add code to fallback 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, 0, InputOnly,
32                                  CopyFromParent, CWOverrideRedirect, &attrib);
33     XMapRaised(ob_display, focus_backup);
34
35     /* start with nothing focused */
36     focus_set_client(NULL);
37 }
38
39 void focus_shutdown()
40 {
41     guint i;
42
43     for (i = 0; i < screen_num_desktops; ++i)
44         g_list_free(focus_order[i]);
45     g_free(focus_order);
46     focus_order = NULL;
47
48     XDestroyWindow(ob_display, focus_backup);
49
50     /* reset focus to root */
51     XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
52                    event_lasttime);
53 }
54
55 void focus_set_client(Client *client)
56 {
57     Window active;
58     Client *old;
59     guint desktop;
60
61     if (client == focus_client) return;
62
63     /* uninstall the old colormap, and install the new one */
64     screen_install_colormap(focus_client, FALSE);
65     screen_install_colormap(client, TRUE);
66
67     if (client == NULL) {
68         /* when nothing will be focused, send focus to the backup target */
69         XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
70                        event_lasttime);
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     {
103         for (it = stacking_list; it != NULL; it = it->next) {
104             Client *c = it->data;
105             if (c->desktop == screen_desktop &&
106                 RECT_CONTAINS(c->frame->area, x, y))
107                 break;
108         }
109         if (it != NULL) {
110             g_message("fallback (pointer) trying %lx", ((Client*)it->data)->window);
111             return client_normal(it->data) && client_focus(it->data);
112         }
113     }
114     return FALSE;
115 }
116
117 void focus_fallback(gboolean switching_desks)
118 {
119     ConfigValue focus_follow;
120     GList *it;
121     gboolean fallback = TRUE;
122
123     if (!switching_desks) {
124         if (!config_get("focusFollowsMouse", Config_Bool, &focus_follow))
125             g_assert_not_reached();
126         if (focus_follow.bool)
127             fallback = !focus_under_pointer();
128     }
129
130     if (fallback) {
131         for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
132             if (it->data != focus_client && client_normal(it->data)) {
133                 g_message("fallback trying %lx", ((Client*)it->data)->window);
134                 if (client_focus(it->data))
135                     break;
136             }
137     }
138
139     if (it == NULL) /* nothing to focus */
140         focus_set_client(NULL);
141 }