oops
[dana/openbox.git] / openbox / focus.c
1 #include "openbox.h"
2 #include "client.h"
3 #include "screen.h"
4 #include "prop.h"
5 #include "dispatch.h"
6
7 #include <X11/Xlib.h>
8 #include <glib.h>
9
10 Client *focus_client = NULL;
11 GList **focus_order = NULL; /* these lists are created when screen_startup
12                                sets the number of desktops */
13
14 Window focus_backup = None;
15
16 void focus_set_client(Client *client);
17
18 void focus_startup()
19 {
20     /* create the window which gets focus when no clients get it. Have to
21        make it override-redirect so we don't try manage it, since it is
22        mapped. */
23     XSetWindowAttributes attrib;
24
25     attrib.override_redirect = TRUE;
26     focus_backup = XCreateWindow(ob_display, ob_root,
27                                  -100, -100, 1, 1, 0, 0, InputOnly,
28                                  CopyFromParent, CWOverrideRedirect, &attrib);
29     XMapRaised(ob_display, focus_backup);
30
31     /* start with nothing focused */
32     focus_set_client(NULL);
33 }
34
35 void focus_shutdown()
36 {
37     guint i;
38
39     for (i = 0; i < screen_num_desktops; ++i)
40         g_list_free(focus_order[i]);
41     g_free(focus_order);
42
43     XDestroyWindow(ob_display, focus_backup);
44
45     /* reset focus to root */
46     XSetInputFocus(ob_display, PointerRoot, RevertToNone, CurrentTime);
47 }
48
49 void focus_set_client(Client *client)
50 {
51     Window active;
52     Client *old;
53     guint desktop;
54
55     /* uninstall the old colormap, and install the new one */
56     screen_install_colormap(focus_client, FALSE);
57     screen_install_colormap(client, TRUE);
58
59
60     if (client == NULL) {
61         /* when nothing will be focused, send focus to the backup target */
62         XSetInputFocus(ob_display, focus_backup, RevertToNone, CurrentTime);
63     }
64
65     old = focus_client;
66     focus_client = client;
67
68     /* move to the top of the list */
69     if (client != NULL) {
70         desktop = client->desktop;
71         if (desktop == DESKTOP_ALL) desktop = screen_desktop;
72         focus_order[desktop] = g_list_remove(focus_order[desktop], client);
73         focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
74     }
75
76     /* set the NET_ACTIVE_WINDOW hint */
77     active = client ? client->window : None;
78     PROP_SET32(ob_root, net_active_window, window, active);
79
80     if (focus_client != NULL)
81         dispatch_client(Event_Client_Focus, focus_client, 0, 0);
82     if (old != NULL)
83         dispatch_client(Event_Client_Unfocus, old, 0, 0);
84 }