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