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