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