]> Dogcows Code - chaz/openbox/blob - openbox/mouse.c
wow... this is a big commit...
[chaz/openbox] / openbox / mouse.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 mouse.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003 Ben Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "openbox.h"
21 #include "config.h"
22 #include "xerror.h"
23 #include "action.h"
24 #include "event.h"
25 #include "client.h"
26 #include "prop.h"
27 #include "grab.h"
28 #include "frame.h"
29 #include "translate.h"
30 #include "mouse.h"
31 #include <glib.h>
32
33 typedef struct {
34 guint state;
35 guint button;
36 GSList *actions[OB_NUM_MOUSE_ACTIONS]; /* lists of Action pointers */
37 } ObMouseBinding;
38
39 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
40 co == OB_FRAME_CONTEXT_FRAME : FALSE)
41 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
42 co == OB_FRAME_CONTEXT_DESKTOP : \
43 co == OB_FRAME_CONTEXT_CLIENT)
44
45 /* Array of GSList*s of ObMouseBinding*s. */
46 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
47
48 ObFrameContext mouse_button_frame_context(ObFrameContext context,
49 guint button)
50 {
51 GSList *it;
52 ObFrameContext x = context;
53
54 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
55 ObMouseBinding *b = it->data;
56
57 if (b->button == button)
58 return context;
59 }
60
61 switch (context) {
62 case OB_FRAME_CONTEXT_NONE:
63 case OB_FRAME_CONTEXT_DESKTOP:
64 case OB_FRAME_CONTEXT_CLIENT:
65 case OB_FRAME_CONTEXT_TITLEBAR:
66 case OB_FRAME_CONTEXT_HANDLE:
67 case OB_FRAME_CONTEXT_FRAME:
68 case OB_FRAME_CONTEXT_MOVE_RESIZE:
69 break;
70 case OB_FRAME_CONTEXT_BLCORNER:
71 case OB_FRAME_CONTEXT_BRCORNER:
72 x = OB_FRAME_CONTEXT_HANDLE;
73 break;
74 case OB_FRAME_CONTEXT_TLCORNER:
75 case OB_FRAME_CONTEXT_TRCORNER:
76 case OB_FRAME_CONTEXT_MAXIMIZE:
77 case OB_FRAME_CONTEXT_ALLDESKTOPS:
78 case OB_FRAME_CONTEXT_SHADE:
79 case OB_FRAME_CONTEXT_ICONIFY:
80 case OB_FRAME_CONTEXT_ICON:
81 case OB_FRAME_CONTEXT_CLOSE:
82 x = OB_FRAME_CONTEXT_TITLEBAR;
83 break;
84 case OB_FRAME_NUM_CONTEXTS:
85 g_assert_not_reached();
86 }
87
88 return x;
89 }
90
91 void mouse_grab_for_client(ObClient *client, gboolean grab)
92 {
93 gint i;
94 GSList *it;
95
96 for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
97 for (it = bound_contexts[i]; it; it = g_slist_next(it)) {
98 /* grab/ungrab the button */
99 ObMouseBinding *b = it->data;
100 Window win;
101 gint mode;
102 guint mask;
103
104 if (FRAME_CONTEXT(i, client)) {
105 win = client->frame->window;
106 mode = GrabModeAsync;
107 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
108 } else if (CLIENT_CONTEXT(i, client)) {
109 win = client->frame->plate;
110 mode = GrabModeSync; /* this is handled in event */
111 mask = ButtonPressMask; /* can't catch more than this with Sync
112 mode the release event is
113 manufactured in event() */
114 } else continue;
115
116 if (grab)
117 grab_button_full(b->button, b->state, win, mask, mode,
118 OB_CURSOR_NONE);
119 else
120 ungrab_button(b->button, b->state, win);
121 }
122 }
123
124 static void grab_all_clients(gboolean grab)
125 {
126 GList *it;
127
128 for (it = client_list; it; it = g_list_next(it))
129 mouse_grab_for_client(it->data, grab);
130 }
131
132 void mouse_unbind_all()
133 {
134 gint i;
135 GSList *it;
136
137 for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
138 for (it = bound_contexts[i]; it; it = g_slist_next(it)) {
139 ObMouseBinding *b = it->data;
140 gint j;
141
142 for (j = 0; j < OB_NUM_MOUSE_ACTIONS; ++j) {
143 GSList *it;
144
145 for (it = b->actions[j]; it; it = g_slist_next(it))
146 action_unref(it->data);
147 g_slist_free(b->actions[j]);
148 }
149 g_free(b);
150 }
151 g_slist_free(bound_contexts[i]);
152 bound_contexts[i] = NULL;
153 }
154 }
155
156 static gboolean fire_binding(ObMouseAction a, ObFrameContext context,
157 ObClient *c, guint state,
158 guint button, gint x, gint y, Time time)
159 {
160 GSList *it;
161 ObMouseBinding *b;
162
163 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
164 b = it->data;
165 if (b->state == state && b->button == button)
166 break;
167 }
168 /* if not bound, then nothing to do! */
169 if (it == NULL) return FALSE;
170
171 action_run_mouse(b->actions[a], c, context, state, button, x, y, time);
172 return TRUE;
173 }
174
175 void mouse_event(ObClient *client, XEvent *e)
176 {
177 static Time ltime;
178 static guint button = 0, state = 0, lbutton = 0;
179 static Window lwindow = None;
180 static gint px, py;
181
182 ObFrameContext context;
183 gboolean click = FALSE;
184 gboolean dclick = FALSE;
185
186 switch (e->type) {
187 case ButtonPress:
188 context = frame_context(client, e->xany.window);
189 context = mouse_button_frame_context(context, e->xbutton.button);
190
191 px = e->xbutton.x_root;
192 py = e->xbutton.y_root;
193 button = e->xbutton.button;
194 state = e->xbutton.state;
195
196 fire_binding(OB_MOUSE_ACTION_PRESS, context,
197 client, e->xbutton.state,
198 e->xbutton.button,
199 e->xbutton.x_root, e->xbutton.y_root,
200 e->xbutton.time);
201
202 if (CLIENT_CONTEXT(context, client)) {
203 /* Replay the event, so it goes to the client*/
204 XAllowEvents(ob_display, ReplayPointer, event_curtime);
205 /* Fall through to the release case! */
206 } else
207 break;
208
209 case ButtonRelease:
210 context = frame_context(client, e->xany.window);
211 context = mouse_button_frame_context(context, e->xbutton.button);
212
213 if (e->xbutton.button == button) {
214 /* clicks are only valid if its released over the window */
215 gint junk1, junk2;
216 Window wjunk;
217 guint ujunk, b, w, h;
218 /* this can cause errors to occur when the window closes */
219 xerror_set_ignore(TRUE);
220 junk1 = XGetGeometry(ob_display, e->xbutton.window,
221 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
222 xerror_set_ignore(FALSE);
223 if (junk1) {
224 if (e->xbutton.x >= (signed)-b &&
225 e->xbutton.y >= (signed)-b &&
226 e->xbutton.x < (signed)(w+b) &&
227 e->xbutton.y < (signed)(h+b)) {
228 click = TRUE;
229 /* double clicks happen if there were 2 in a row! */
230 if (lbutton == button &&
231 lwindow == e->xbutton.window &&
232 e->xbutton.time - config_mouse_dclicktime <=
233 ltime) {
234 dclick = TRUE;
235 lbutton = 0;
236 } else {
237 lbutton = button;
238 lwindow = e->xbutton.window;
239 }
240 } else {
241 lbutton = 0;
242 lwindow = None;
243 }
244 }
245
246 button = 0;
247 state = 0;
248 ltime = e->xbutton.time;
249 }
250 fire_binding(OB_MOUSE_ACTION_RELEASE, context,
251 client, e->xbutton.state,
252 e->xbutton.button,
253 e->xbutton.x_root, e->xbutton.y_root,
254 e->xbutton.time);
255 if (click)
256 fire_binding(OB_MOUSE_ACTION_CLICK, context,
257 client, e->xbutton.state,
258 e->xbutton.button,
259 e->xbutton.x_root,
260 e->xbutton.y_root,
261 e->xbutton.time);
262 if (dclick)
263 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
264 client, e->xbutton.state,
265 e->xbutton.button,
266 e->xbutton.x_root,
267 e->xbutton.y_root,
268 e->xbutton.time);
269 break;
270
271 case MotionNotify:
272 if (button) {
273 context = frame_context(client, e->xany.window);
274 context = mouse_button_frame_context(context, button);
275
276 if (ABS(e->xmotion.x_root - px) >=
277 config_mouse_threshold ||
278 ABS(e->xmotion.y_root - py) >=
279 config_mouse_threshold) {
280
281 /* You can't drag on buttons */
282 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
283 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
284 context == OB_FRAME_CONTEXT_SHADE ||
285 context == OB_FRAME_CONTEXT_ICONIFY ||
286 context == OB_FRAME_CONTEXT_ICON ||
287 context == OB_FRAME_CONTEXT_CLOSE)
288 break;
289
290 fire_binding(OB_MOUSE_ACTION_MOTION, context,
291 client, state, button, px, py, e->xmotion.time);
292 button = 0;
293 state = 0;
294 }
295 }
296 break;
297
298 default:
299 g_assert_not_reached();
300 }
301 }
302
303 gboolean mouse_bind(const gchar *buttonstr, const gchar *contextstr,
304 ObMouseAction mact, ObAction *action)
305 {
306 guint state, button;
307 ObFrameContext context;
308 ObMouseBinding *b;
309 GSList *it;
310
311 if (!translate_button(buttonstr, &state, &button)) {
312 g_warning("invalid button '%s'", buttonstr);
313 return FALSE;
314 }
315
316 context = frame_context_from_string(contextstr);
317 if (!context) {
318 g_warning("invalid context '%s'", contextstr);
319 return FALSE;
320 }
321
322 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
323 b = it->data;
324 if (b->state == state && b->button == button) {
325 b->actions[mact] = g_slist_append(b->actions[mact], action);
326 return TRUE;
327 }
328 }
329
330 /* when there are no modifiers in the binding, then the action cannot
331 be interactive */
332 if (!state && action->data.any.interactive) {
333 action->data.any.interactive = FALSE;
334 action->data.inter.final = TRUE;
335 }
336
337 /* add the binding */
338 b = g_new0(ObMouseBinding, 1);
339 b->state = state;
340 b->button = button;
341 b->actions[mact] = g_slist_append(NULL, action);
342 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
343
344 return TRUE;
345 }
346
347 void mouse_startup(gboolean reconfig)
348 {
349 grab_all_clients(TRUE);
350 }
351
352 void mouse_shutdown(gboolean reconfig)
353 {
354 grab_all_clients(FALSE);
355 mouse_unbind_all();
356 }
This page took 0.051023 seconds and 5 git commands to generate.