]> icculus.org git repositories - dana/openbox.git/blob - plugins/focus.c
fallback super intelligently with focus when the focused
[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 static int skip_enter = 0;
10
11 static void focus_fallback(gboolean warp)
12 {
13     GList *it;
14
15     for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
16         if (client_focus(it->data)) {
17             if (warp) { /* XXX make this configurable */
18                 XEvent e;
19                 Client *c = it->data;
20
21                 /* skip the next enter event from the desktop switch so focus
22                    doesn't skip briefly to what was under the pointer */
23                 if (XCheckTypedEvent(ob_display, EnterNotify, &e)) {
24                     XPutBackEvent(ob_display, &e);
25                     /* XXX WERE NOT SKIPPING THEM ALL@&*)! */
26                     g_message("Skip");
27                     ++skip_enter;
28                 }
29
30                 /* I have to do this warp twice! Otherwise windows dont get
31                    Enter/Leave events when i warp on a desktop switch! */
32                 XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
33                              c->area.width / 2, c->area.height / 2);
34                 XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
35                              c->area.width / 2, c->area.height / 2);
36             }
37             break;
38         }
39 }
40
41 static void focus_under_pointer()
42 {
43     Window w;
44     int i, x, y;
45     guint u;
46     GList *it;
47
48     if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u))
49     {
50         for (it = stacking_list; it != NULL; it = it->next) {
51             Client *c = it->data;
52             if (c->desktop == screen_desktop &&
53                 RECT_CONTAINS(c->frame->area, x, y))
54                 break;
55         }
56         if (it != NULL) {
57             client_focus(it->data);
58             return;
59         }
60     }
61     focus_fallback(FALSE);
62 }
63
64 static void events(ObEvent *e, void *foo)
65 {
66     switch (e->type) {
67     case Event_Client_Mapped:
68         /* focus new normal windows */
69         if (client_normal(e->data.c.client))
70             client_focus(e->data.c.client);
71         break;
72
73     case Event_Client_Unmapped:
74         if (ob_state == State_Exiting) break;
75
76         if (e->data.c.client->focused) {
77             /* if sloppy focus... */
78             focus_under_pointer();
79
80             /* otherwise... */
81             /*
82               /\* nothing is left with focus! *\/
83               if (focus_client == NULL) 
84               /\* focus the next available target *\/
85               focus_fallback(screen_desktop, FALSE);
86             */
87         }
88         break;
89
90     case Event_Ob_Desktop:
91         /* focus the next available target if moving from the current
92            desktop. */
93         if ((unsigned)e->data.o.num[1] == screen_desktop)
94             focus_fallback(TRUE);
95         break;
96
97     case Event_X_EnterNotify:
98         if (skip_enter)
99             --skip_enter;
100         else if (e->data.x.client && client_normal(e->data.x.client))
101             client_focus(e->data.x.client);
102         break;
103
104     default:
105         g_assert_not_reached();
106     }
107 }
108
109 void plugin_startup()
110 {
111     dispatch_register(Event_Client_Mapped | 
112                       Event_Ob_Desktop | 
113                       Event_Client_Unmapped |
114                       Event_X_EnterNotify,
115                       (EventHandler)events, NULL);
116 }
117
118 void plugin_shutdown()
119 {
120     dispatch_register(0, (EventHandler)events, NULL);
121 }