]> Dogcows Code - chaz/openbox/blob - openbox/mouse.c
expose focus_cycle_client
[chaz/openbox] / openbox / mouse.c
1 #include "openbox.h"
2 #include "config.h"
3 #include "xerror.h"
4 #include "action.h"
5 #include "event.h"
6 #include "client.h"
7 #include "prop.h"
8 #include "grab.h"
9 #include "frame.h"
10 #include "translate.h"
11 #include "mouse.h"
12 #include <glib.h>
13
14 typedef struct {
15 guint state;
16 guint button;
17 GSList *actions[OB_NUM_MOUSE_ACTIONS]; /* lists of Action pointers */
18 } ObMouseBinding;
19
20 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
21 co == OB_FRAME_CONTEXT_FRAME : FALSE)
22 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
23 co == OB_FRAME_CONTEXT_DESKTOP : \
24 co == OB_FRAME_CONTEXT_CLIENT)
25
26 /* Array of GSList*s of PointerBinding*s. */
27 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
28
29 void mouse_grab_for_client(ObClient *client, gboolean grab)
30 {
31 int i;
32 GSList *it;
33
34 for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
35 for (it = bound_contexts[i]; it != NULL; it = g_slist_next(it)) {
36 /* grab/ungrab the button */
37 ObMouseBinding *b = it->data;
38 Window win;
39 int mode;
40 unsigned int mask;
41
42 if (FRAME_CONTEXT(i, client)) {
43 win = client->frame->window;
44 mode = GrabModeAsync;
45 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
46 } else if (CLIENT_CONTEXT(i, client)) {
47 win = client->frame->plate;
48 mode = GrabModeSync; /* this is handled in event */
49 mask = ButtonPressMask; /* can't catch more than this with Sync
50 mode the release event is
51 manufactured in event() */
52 } else continue;
53
54 if (grab)
55 grab_button_full(b->button, b->state, win, mask, mode,
56 OB_CURSOR_NONE);
57 else
58 ungrab_button(b->button, b->state, win);
59 }
60 }
61
62 static void grab_all_clients(gboolean grab)
63 {
64 GList *it;
65
66 for (it = client_list; it != NULL; it = it->next)
67 mouse_grab_for_client(it->data, grab);
68 }
69
70 static void clearall()
71 {
72 int i;
73 GSList *it;
74
75 for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
76 for (it = bound_contexts[i]; it != NULL; it = it->next) {
77 ObMouseBinding *b = it->data;
78 int j;
79
80 for (j = 0; j < OB_NUM_MOUSE_ACTIONS; ++j) {
81 GSList *it;
82
83 for (it = b->actions[j]; it; it = it->next)
84 action_free(it->data);
85 g_slist_free(b->actions[j]);
86 }
87 g_free(b);
88 }
89 g_slist_free(bound_contexts[i]);
90 bound_contexts[i] = NULL;
91 }
92 }
93
94 static gboolean fire_binding(ObMouseAction a, ObFrameContext context,
95 ObClient *c, guint state,
96 guint button, int x, int y)
97 {
98 GSList *it;
99 ObMouseBinding *b;
100
101 for (it = bound_contexts[context]; it != NULL; it = it->next) {
102 b = it->data;
103 if (b->state == state && b->button == button)
104 break;
105 }
106 /* if not bound, then nothing to do! */
107 if (it == NULL) return FALSE;
108
109 for (it = b->actions[a]; it; it = it->next)
110 action_run_mouse(it->data, c, context, state, button, x, y);
111 return TRUE;
112 }
113
114 void mouse_event(ObClient *client, ObFrameContext context, XEvent *e)
115 {
116 static Time ltime;
117 static guint button = 0, state = 0, lbutton = 0;
118
119 static Window lwindow = None;
120 static int px, py;
121 gboolean click = FALSE;
122 gboolean dclick = FALSE;
123
124 switch (e->type) {
125 case ButtonPress:
126 px = e->xbutton.x_root;
127 py = e->xbutton.y_root;
128 button = e->xbutton.button;
129 state = e->xbutton.state;
130
131 fire_binding(OB_MOUSE_ACTION_PRESS, context,
132 client, e->xbutton.state,
133 e->xbutton.button,
134 e->xbutton.x_root, e->xbutton.y_root);
135
136 if (CLIENT_CONTEXT(context, client)) {
137 /* Replay the event, so it goes to the client*/
138 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
139 /* Fall through to the release case! */
140 } else
141 break;
142
143 case ButtonRelease:
144 if (e->xbutton.button == button) {
145 /* clicks are only valid if its released over the window */
146 int junk1, junk2;
147 Window wjunk;
148 guint ujunk, b, w, h;
149 xerror_set_ignore(TRUE);
150 junk1 = XGetGeometry(ob_display, e->xbutton.window,
151 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
152 xerror_set_ignore(FALSE);
153 if (junk1) {
154 if (e->xbutton.x >= (signed)-b &&
155 e->xbutton.y >= (signed)-b &&
156 e->xbutton.x < (signed)(w+b) &&
157 e->xbutton.y < (signed)(h+b)) {
158 click = TRUE;
159 /* double clicks happen if there were 2 in a row! */
160 if (lbutton == button &&
161 lwindow == e->xbutton.window &&
162 e->xbutton.time - config_mouse_dclicktime <=
163 ltime) {
164 dclick = TRUE;
165 lbutton = 0;
166 } else {
167 lbutton = button;
168 lwindow = e->xbutton.window;
169 }
170 } else {
171 lbutton = 0;
172 lwindow = None;
173 }
174 }
175
176 button = 0;
177 state = 0;
178 ltime = e->xbutton.time;
179 }
180 fire_binding(OB_MOUSE_ACTION_RELEASE, context,
181 client, e->xbutton.state,
182 e->xbutton.button,
183 e->xbutton.x_root, e->xbutton.y_root);
184 if (click)
185 fire_binding(OB_MOUSE_ACTION_CLICK, context,
186 client, e->xbutton.state,
187 e->xbutton.button,
188 e->xbutton.x_root,
189 e->xbutton.y_root);
190 if (dclick)
191 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
192 client, e->xbutton.state,
193 e->xbutton.button,
194 e->xbutton.x_root,
195 e->xbutton.y_root);
196 break;
197
198 case MotionNotify:
199 if (button) {
200 if (ABS(e->xmotion.x_root - px) >=
201 config_mouse_threshold ||
202 ABS(e->xmotion.y_root - py) >=
203 config_mouse_threshold) {
204
205 /* You can't drag on buttons */
206 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
207 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
208 context == OB_FRAME_CONTEXT_SHADE ||
209 context == OB_FRAME_CONTEXT_ICONIFY ||
210 context == OB_FRAME_CONTEXT_ICON ||
211 context == OB_FRAME_CONTEXT_CLOSE)
212 break;
213
214 fire_binding(OB_MOUSE_ACTION_MOTION, context,
215 client, state, button, px, py);
216 button = 0;
217 state = 0;
218 }
219 }
220 break;
221
222 default:
223 g_assert_not_reached();
224 }
225 }
226
227 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
228 ObAction *action)
229 {
230 guint state, button;
231 ObFrameContext context;
232 ObMouseBinding *b;
233 GSList *it;
234
235 if (!translate_button(buttonstr, &state, &button)) {
236 g_warning("invalid button '%s'", buttonstr);
237 return FALSE;
238 }
239
240 contextstr = g_ascii_strdown(contextstr, -1);
241 context = frame_context_from_string(contextstr);
242 if (!context) {
243 g_warning("invalid context '%s'", contextstr);
244 g_free(contextstr);
245 return FALSE;
246 }
247 g_free(contextstr);
248
249 for (it = bound_contexts[context]; it != NULL; it = it->next){
250 b = it->data;
251 if (b->state == state && b->button == button) {
252 b->actions[mact] = g_slist_append(b->actions[mact], action);
253 return TRUE;
254 }
255 }
256
257 /* when there are no modifiers in the binding, then the action cannot
258 be interactive */
259 if (!state && action->data.any.interactive) {
260 action->data.any.interactive = FALSE;
261 action->data.inter.final = TRUE;
262 }
263
264 /* add the binding */
265 b = g_new0(ObMouseBinding, 1);
266 b->state = state;
267 b->button = button;
268 b->actions[mact] = g_slist_append(NULL, action);
269 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
270
271 return TRUE;
272 }
273
274 void mouse_startup(gboolean reconfig)
275 {
276 grab_all_clients(TRUE);
277 }
278
279 void mouse_shutdown(gboolean reconfig)
280 {
281 grab_all_clients(FALSE);
282 clearall();
283 }
This page took 0.044644 seconds and 4 git commands to generate.