all the old ob2 mouse bindings are workin
[dana/openbox.git] / plugins / focus.c
1 #include "../kernel/dispatch.h"
2 #include "../kernel/screen.h"
3 #include "../kernel/client.h"
4 #include "../kernel/focus.h"
5 #include "../kernel/stacking.h"
6 #include "../kernel/openbox.h"
7
8 static int skip_enter = 0;
9
10 static void focus_fallback(guint desk, gboolean warp)
11 {
12     GList *it;
13
14     for (it = focus_order[desk]; it != NULL; it = it->next)
15         if (client_focus(it->data)) {
16             if (warp) { /* XXX make this configurable */
17                 XEvent e;
18                 Client *c = it->data;
19
20                 /* skip the next enter event from the desktop switch so focus
21                    doesn't skip briefly to what was under the pointer */
22                 if (XCheckTypedEvent(ob_display, EnterNotify, &e)) {
23                     XPutBackEvent(ob_display, &e);
24                     /* XXX WERE NOT SKIPPING THEM ALL@&*)! */
25                     g_message("Skip");
26                     ++skip_enter;
27                 }
28
29                 /* I have to do this warp twice! Otherwise windows dont get
30                    Enter/Leave events when i warp on a desktop switch! */
31                 XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
32                              c->area.width / 2, c->area.height / 2);
33                 XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
34                              c->area.width / 2, c->area.height / 2);
35             }
36             break;
37         }
38 }
39
40 static void events(ObEvent *e, void *foo)
41 {
42     switch (e->type) {
43     case Event_Client_Mapped:
44         /* focus new normal windows */
45         if (client_normal(e->data.c.client))
46             client_focus(e->data.c.client);
47         break;
48
49     case Event_Ob_Desktop:
50         /* focus the next available target */
51         focus_fallback(e->data.o.num[0], TRUE);
52         break;
53
54     case Event_Client_Unfocus:
55         /* dont do this shit with sloppy focus... */
56         /*
57         /\* nothing is left with focus! *\/
58         if (focus_client == NULL) 
59             /\* focus the next available target *\/
60             focus_fallback(screen_desktop, FALSE);
61         */
62         break;
63
64     case Event_X_EnterNotify:
65         if (skip_enter)
66             --skip_enter;
67         else if (e->data.x.client && client_normal(e->data.x.client))
68             client_focus(e->data.x.client);
69         break;
70
71     default:
72         g_assert_not_reached();
73     }
74 }
75
76 void plugin_startup()
77 {
78     dispatch_register(Event_Client_Mapped | 
79                       Event_Ob_Desktop | 
80                       Event_Client_Unfocus |
81                       Event_X_EnterNotify,
82                       (EventHandler)events, NULL);
83 }
84
85 void plugin_shutdown()
86 {
87     dispatch_register(0, (EventHandler)events, NULL);
88 }