]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus.c
move the focus_order lists into the kernel
[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;
12
13 Window focus_backup = None;
14
15 void focus_set_client(Client *client);
16
17 void focus_startup()
18 {
19     guint i;
20
21     /* create the window which gets focus when no clients get it. Have to
22        make it override-redirect so we don't try manage it, since it is
23        mapped. */
24     XSetWindowAttributes attrib;
25
26     attrib.override_redirect = TRUE;
27     focus_backup = XCreateWindow(ob_display, ob_root,
28                                  -100, -100, 1, 1, 0, 0, InputOnly,
29                                  CopyFromParent, CWOverrideRedirect, &attrib);
30     XMapRaised(ob_display, focus_backup);
31
32     focus_order = g_new(GList*, screen_num_desktops);
33     for (i = 0; i < screen_num_desktops; ++i)
34         focus_order[i] = NULL;
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
48     /* reset focus to root */
49     XSetInputFocus(ob_display, PointerRoot, RevertToNone, CurrentTime);
50 }
51
52 void focus_set_client(Client *client)
53 {
54     Window active;
55     Client *old;
56     guint desktop;
57
58     /* uninstall the old colormap, and install the new one */
59     screen_install_colormap(focus_client, FALSE);
60     screen_install_colormap(client, TRUE);
61
62
63     if (client == NULL) {
64         /* when nothing will be focused, send focus to the backup target */
65         XSetInputFocus(ob_display, focus_backup, RevertToNone, CurrentTime);
66     }
67
68     old = focus_client;
69     focus_client = client;
70
71     /* move to the top of the list */
72     if (client != NULL) {
73         desktop = client->desktop;
74         if (desktop == DESKTOP_ALL) desktop = screen_desktop;
75         focus_order[desktop] = g_list_remove(focus_order[desktop], client);
76         focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
77     }
78
79     /* set the NET_ACTIVE_WINDOW hint */
80     active = client ? client->window : None;
81     PROP_SET32(ob_root, net_active_window, window, active);
82
83     if (focus_client != NULL)
84         dispatch_client(Event_Client_Focus, focus_client, 0, 0);
85     if (old != NULL)
86         dispatch_client(Event_Client_Unfocus, old, 0, 0);
87 }