only let bind Move and Resize to Drags
[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 #include "../kernel/config.h"
9
10 void plugin_setup_config()
11 {
12     ConfigValue val;
13
14     config_def_set(config_def_new("focus.followMouse", Config_Bool,
15                                   "Focus Follows Mouse",
16                                   "Focus windows when the mouse pointer "
17                                   "enters them."));
18     val.bool = TRUE;
19     config_set("focus.followMouse", Config_Bool, val);
20     config_def_set(config_def_new("focus.focusNew", Config_Bool,
21                                   "Focus New Windows",
22                                   "Focus windows when they first appear."));
23     val.bool = TRUE;
24     config_set("focus.focusNew", Config_Bool, val);
25 /*
26     config_def_set(config_def_new("focus.warpOnDeskSwitch", Config_Bool,
27                                   "Warp Pointer On Desktop Switch",
28                                   "Warps the pointer to the focused window "
29                                   "when switching desktops."));
30     config_set("focus.warpOnDeskSwitch", Config_Bool, FALSE);
31 */
32 }
33
34 /*static int skip_enter = 0;*/
35
36 static gboolean focus_under_pointer()
37 {
38     Window w;
39     int i, x, y;
40     guint u;
41     GList *it;
42
43     if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u))
44     {
45         for (it = stacking_list; it != NULL; it = it->next) {
46             Client *c = it->data;
47             if (c->desktop == screen_desktop &&
48                 RECT_CONTAINS(c->frame->area, x, y))
49                 break;
50         }
51         if (it != NULL) {
52             return client_normal(it->data) && client_focus(it->data);
53         }
54     }
55     return FALSE;
56 }
57
58 static void chew_enter_events()
59 {
60     XEvent e;
61
62     /* XXX... not anymore
63        skip the next enter event from the desktop switch so focus
64        doesn't skip briefly to what was under the pointer */
65
66     /* kill all enter events from prior to the desktop switch, we
67        aren't interested in them if we have found our own target
68        to focus.
69        XXX this is rude to other plugins...can this be done
70        better? count the events in the queue? */
71     while (XCheckTypedEvent(ob_display, EnterNotify, &e));
72 /*
73     {
74         XPutBackEvent(ob_display, &e);
75         g_message("skip");
76         ++skip_enter;
77     }
78 */
79 }
80
81 static void focus_fallback(gboolean switching_desks)
82 {
83     GList *it;
84
85     for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
86         if (client_normal(it->data) && client_focus(it->data)) {
87             if (switching_desks) {
88                 Client *c = it->data;
89
90                 chew_enter_events();
91
92                 if (FALSE /*warp_on_desk_switch*/) {
93                     /* I have to do this warp twice! Otherwise windows dont get
94                        Enter/Leave events when i warp on a desktop switch! */
95                     XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
96                                  c->area.width / 2, c->area.height / 2);
97                     XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
98                                  c->area.width / 2, c->area.height / 2);
99                 }
100             }
101             break;
102         }
103 }
104
105 static void focus_desktop()
106 {
107     GList *it;
108
109     for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
110         Client *client = it->data;
111         if (client->type == Type_Desktop && client->frame->visible)
112             if (client_focus(client))
113                 break;
114     }
115     chew_enter_events();
116 }
117
118 static void event(ObEvent *e, void *foo)
119 {
120     ConfigValue follow_mouse, focus_new;
121     
122     if (!config_get("focus.followMouse", Config_Bool, &follow_mouse))
123         g_assert_not_reached();
124
125     switch (e->type) {
126     case Event_Client_Mapped:
127         if (!config_get("focus.focusNew", Config_Bool, &focus_new))
128             g_assert_not_reached();
129         if (focus_new.bool && client_normal(e->data.c.client))
130             client_focus(e->data.c.client);
131         break;
132
133     case Event_Client_Unmapped:
134         if (ob_state == State_Exiting) break;
135
136         if (client_focused(e->data.c.client))
137             if (!follow_mouse.bool || !focus_under_pointer())
138                 focus_fallback(FALSE);
139         break;
140
141     case Event_Client_Desktop:
142         /* focus the next available target if moving from the current
143            desktop. */
144         if ((unsigned)e->data.c.num[1] == screen_desktop)
145             if (!follow_mouse.bool || !focus_under_pointer())
146                 focus_fallback(FALSE);
147
148     case Event_Ob_Desktop:
149         focus_fallback(TRUE);
150         break;
151
152     case Event_Ob_ShowDesktop:
153         if (!e->data.o.num[0]) { /* hiding the desktop, showing the clients */
154             if (!follow_mouse.bool || !focus_under_pointer())
155                 focus_fallback(TRUE);
156         } else /* hiding clients, showing the desktop */
157             focus_desktop();
158         break;
159
160     case Event_X_EnterNotify:
161 /*        if (skip_enter) {
162             if (e->data.x.client != NULL)
163                 g_message("skipped enter %lx", e->data.x.client->window);
164             else
165                 g_message("skipped enter 'root'");
166             --skip_enter;
167         }
168         else*/
169         if (follow_mouse.bool)
170             if (e->data.x.client != NULL && client_normal(e->data.x.client))
171                 client_focus(e->data.x.client);
172         break;
173
174     default:
175         g_assert_not_reached();
176     }
177 }
178
179 void plugin_startup()
180 {
181     dispatch_register(Event_Client_Mapped | 
182                       Event_Ob_Desktop | 
183                       Event_Client_Unmapped |
184                       Event_X_EnterNotify |
185                       Event_Ob_ShowDesktop,
186                       (EventHandler)event, NULL);
187 }
188
189 void plugin_shutdown()
190 {
191     dispatch_register(0, (EventHandler)event, NULL);
192 }