build 'openbox3' instead of 'ob3'
[dana/openbox.git] / plugins / focus.c
1 #include "../kernel/dispatch.h"
2 #include "../kernel/screen.h"
3 #include "../kernel/client.h"
4 #include "../kernel/frame.h"
5 #include "../kernel/focus.h"
6 #include "../kernel/stacking.h"
7 #include "../kernel/openbox.h"
8
9 /* config options */
10 static gboolean follow_mouse = TRUE;
11 static gboolean warp_on_desk_switch = FALSE;
12 static gboolean focus_new = TRUE;
13
14 /*static int skip_enter = 0;*/
15
16 static gboolean focus_under_pointer()
17 {
18     Window w;
19     int i, x, y;
20     guint u;
21     GList *it;
22
23     if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u))
24     {
25         for (it = stacking_list; it != NULL; it = it->next) {
26             Client *c = it->data;
27             if (c->desktop == screen_desktop &&
28                 RECT_CONTAINS(c->frame->area, x, y))
29                 break;
30         }
31         if (it != NULL) {
32             return client_normal(it->data) && client_focus(it->data);
33         }
34     }
35     return FALSE;
36 }
37
38 static void chew_enter_events()
39 {
40     XEvent e;
41
42     /* XXX... not anymore
43        skip the next enter event from the desktop switch so focus
44        doesn't skip briefly to what was under the pointer */
45
46     /* kill all enter events from prior to the desktop switch, we
47        aren't interested in them if we have found our own target
48        to focus.
49        XXX this is rude to other plugins...can this be done
50        better? count the events in the queue? */
51     while (XCheckTypedEvent(ob_display, EnterNotify, &e));
52 /*
53     {
54         XPutBackEvent(ob_display, &e);
55         g_message("skip");
56         ++skip_enter;
57     }
58 */
59 }
60
61 static void focus_fallback(gboolean switching_desks)
62 {
63     GList *it;
64
65     for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
66         if (client_normal(it->data) && client_focus(it->data)) {
67             if (switching_desks) {
68                 Client *c = it->data;
69
70                 chew_enter_events();
71
72                 if (warp_on_desk_switch) {
73                     /* I have to do this warp twice! Otherwise windows dont get
74                        Enter/Leave events when i warp on a desktop switch! */
75                     XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
76                                  c->area.width / 2, c->area.height / 2);
77                     XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
78                                  c->area.width / 2, c->area.height / 2);
79                 }
80             }
81             break;
82         }
83 }
84
85 static void focus_desktop()
86 {
87     GList *it;
88
89     for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
90         Client *client = it->data;
91         if (client->type == Type_Desktop && client->frame->visible)
92             if (client_focus(client))
93                 break;
94     }
95     chew_enter_events();
96 }
97
98 static void event(ObEvent *e, void *foo)
99 {
100     switch (e->type) {
101     case Event_Client_Mapped:
102         if (focus_new && client_normal(e->data.c.client))
103             client_focus(e->data.c.client);
104         break;
105
106     case Event_Client_Unmapped:
107         if (ob_state == State_Exiting) break;
108
109         if (client_focused(e->data.c.client))
110             if (!follow_mouse || !focus_under_pointer())
111                 focus_fallback(FALSE);
112         break;
113
114     case Event_Client_Desktop:
115         /* focus the next available target if moving from the current
116            desktop. */
117         if ((unsigned)e->data.c.num[1] == screen_desktop)
118             if (!follow_mouse || !focus_under_pointer())
119                 focus_fallback(FALSE);
120
121     case Event_Ob_Desktop:
122         focus_fallback(TRUE);
123         break;
124
125     case Event_Ob_ShowDesktop:
126         if (!e->data.o.num[0]) { /* hiding the desktop, showing the clients */
127             if (!follow_mouse || !focus_under_pointer())
128                 focus_fallback(TRUE);
129         } else /* hiding clients, showing the desktop */
130             focus_desktop();
131         break;
132
133     case Event_X_EnterNotify:
134 /*        if (skip_enter) {
135             if (e->data.x.client != NULL)
136                 g_message("skipped enter %lx", e->data.x.client->window);
137             else
138                 g_message("skipped enter 'root'");
139             --skip_enter;
140         }
141         else*/
142         if (e->data.x.client != NULL && client_normal(e->data.x.client))
143             client_focus(e->data.x.client);
144         break;
145
146     default:
147         g_assert_not_reached();
148     }
149 }
150
151 void plugin_startup()
152 {
153     dispatch_register(Event_Client_Mapped | 
154                       Event_Ob_Desktop | 
155                       Event_Client_Unmapped |
156                       Event_X_EnterNotify |
157                       Event_Ob_ShowDesktop,
158                       (EventHandler)event, NULL);
159 }
160
161 void plugin_shutdown()
162 {
163     dispatch_register(0, (EventHandler)event, NULL);
164 }