]> icculus.org git repositories - dana/openbox.git/blob - plugins/focus.c
dont reposition windows who know what they want
[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 focus_fallback(gboolean switching_desks)
39 {
40     GList *it;
41
42     for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
43         if (client_normal(it->data) && client_focus(it->data)) {
44             if (switching_desks) {
45                 XEvent e;
46                 Client *c = it->data;
47
48                 /* XXX... not anymore
49                    skip the next enter event from the desktop switch so focus
50                    doesn't skip briefly to what was under the pointer */
51
52                 /* kill all enter events from prior to the desktop switch, we
53                    aren't interested in them if we have found our own target
54                    to focus.
55                    XXX this is rude to other plugins...can this be done
56                    better? count the events in the queue? */
57                 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
58 /*                    XPutBackEvent(ob_display, &e);
59                     g_message("skip");
60                     ++skip_enter;
61                     }*/
62
63                 if (warp_on_desk_switch) {
64                     /* I have to do this warp twice! Otherwise windows dont get
65                        Enter/Leave events when i warp on a desktop switch! */
66                     XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
67                                  c->area.width / 2, c->area.height / 2);
68                     XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
69                                  c->area.width / 2, c->area.height / 2);
70                 }
71             }
72             break;
73         }
74 }
75
76 static void event(ObEvent *e, void *foo)
77 {
78     switch (e->type) {
79     case Event_Client_Mapped:
80         if (focus_new && client_normal(e->data.c.client))
81             client_focus(e->data.c.client);
82         break;
83
84     case Event_Client_Unmapped:
85         if (ob_state == State_Exiting) break;
86
87         if (client_focused(e->data.c.client))
88             if (!follow_mouse || !focus_under_pointer())
89                 focus_fallback(FALSE);
90         break;
91
92     case Event_Client_Desktop:
93         /* focus the next available target if moving from the current
94            desktop. */
95         if ((unsigned)e->data.c.num[1] == screen_desktop)
96             if (!follow_mouse || !focus_under_pointer())
97                 focus_fallback(FALSE);
98
99     case Event_Ob_Desktop:
100         focus_fallback(TRUE);
101         break;
102
103     case Event_X_EnterNotify:
104 /*        if (skip_enter) {
105             if (e->data.x.client != NULL)
106                 g_message("skipped enter %lx", e->data.x.client->window);
107             else
108                 g_message("skipped enter 'root'");
109             --skip_enter;
110         }
111         else*/
112         if (e->data.x.client != NULL && client_normal(e->data.x.client))
113             client_focus(e->data.x.client);
114         break;
115
116     default:
117         g_assert_not_reached();
118     }
119 }
120
121 void plugin_startup()
122 {
123     dispatch_register(Event_Client_Mapped | 
124                       Event_Ob_Desktop | 
125                       Event_Client_Unmapped |
126                       Event_X_EnterNotify,
127                       (EventHandler)event, NULL);
128 }
129
130 void plugin_shutdown()
131 {
132     dispatch_register(0, (EventHandler)event, NULL);
133 }