]> Dogcows Code - chaz/openbox/blob - openbox/mouse.c
missing new left and right contexts
[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-2007 Dana 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 "gettext.h"
32
33 #include <glib.h>
34
35 typedef struct {
36 guint state;
37 guint button;
38 GSList *actions[OB_NUM_MOUSE_ACTIONS]; /* lists of Action pointers */
39 } ObMouseBinding;
40
41 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
42 co == OB_FRAME_CONTEXT_FRAME : FALSE)
43 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
44 co == OB_FRAME_CONTEXT_DESKTOP : \
45 co == OB_FRAME_CONTEXT_CLIENT)
46
47 /* Array of GSList*s of ObMouseBinding*s. */
48 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
49
50 ObFrameContext mouse_button_frame_context(ObFrameContext context,
51 guint button)
52 {
53 GSList *it;
54 ObFrameContext x = context;
55
56 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
57 ObMouseBinding *b = it->data;
58
59 if (b->button == button)
60 return context;
61 }
62
63 switch (context) {
64 case OB_FRAME_CONTEXT_NONE:
65 case OB_FRAME_CONTEXT_DESKTOP:
66 case OB_FRAME_CONTEXT_CLIENT:
67 case OB_FRAME_CONTEXT_TITLEBAR:
68 case OB_FRAME_CONTEXT_FRAME:
69 case OB_FRAME_CONTEXT_MOVE_RESIZE:
70 case OB_FRAME_CONTEXT_LEFT:
71 case OB_FRAME_CONTEXT_RIGHT:
72 break;
73 case OB_FRAME_CONTEXT_BOTTOM:
74 case OB_FRAME_CONTEXT_BLCORNER:
75 case OB_FRAME_CONTEXT_BRCORNER:
76 x = OB_FRAME_CONTEXT_BOTTOM;
77 break;
78 case OB_FRAME_CONTEXT_TLCORNER:
79 case OB_FRAME_CONTEXT_TRCORNER:
80 case OB_FRAME_CONTEXT_TOP:
81 case OB_FRAME_CONTEXT_MAXIMIZE:
82 case OB_FRAME_CONTEXT_ALLDESKTOPS:
83 case OB_FRAME_CONTEXT_SHADE:
84 case OB_FRAME_CONTEXT_ICONIFY:
85 case OB_FRAME_CONTEXT_ICON:
86 case OB_FRAME_CONTEXT_CLOSE:
87 x = OB_FRAME_CONTEXT_TITLEBAR;
88 break;
89 case OB_FRAME_NUM_CONTEXTS:
90 g_assert_not_reached();
91 }
92
93 return x;
94 }
95
96 void mouse_grab_for_client(ObClient *client, gboolean grab)
97 {
98 gint i;
99 GSList *it;
100
101 for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
102 for (it = bound_contexts[i]; it; it = g_slist_next(it)) {
103 /* grab/ungrab the button */
104 ObMouseBinding *b = it->data;
105 Window win;
106 gint mode;
107 guint mask;
108
109 if (FRAME_CONTEXT(i, client)) {
110 win = client->frame->window;
111 mode = GrabModeAsync;
112 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
113 } else if (CLIENT_CONTEXT(i, client)) {
114 win = client->frame->plate;
115 mode = GrabModeSync; /* this is handled in event */
116 mask = ButtonPressMask; /* can't catch more than this with Sync
117 mode the release event is
118 manufactured in event() */
119 } else continue;
120
121 if (grab)
122 grab_button_full(b->button, b->state, win, mask, mode,
123 OB_CURSOR_NONE);
124 else
125 ungrab_button(b->button, b->state, win);
126 }
127 }
128
129 static void grab_all_clients(gboolean grab)
130 {
131 GList *it;
132
133 for (it = client_list; it; it = g_list_next(it))
134 mouse_grab_for_client(it->data, grab);
135 }
136
137 void mouse_unbind_all()
138 {
139 gint i;
140 GSList *it;
141
142 for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
143 for (it = bound_contexts[i]; it; it = g_slist_next(it)) {
144 ObMouseBinding *b = it->data;
145 gint j;
146
147 for (j = 0; j < OB_NUM_MOUSE_ACTIONS; ++j) {
148 GSList *it;
149
150 for (it = b->actions[j]; it; it = g_slist_next(it))
151 action_unref(it->data);
152 g_slist_free(b->actions[j]);
153 }
154 g_free(b);
155 }
156 g_slist_free(bound_contexts[i]);
157 bound_contexts[i] = NULL;
158 }
159 }
160
161 static gboolean fire_binding(ObMouseAction a, ObFrameContext context,
162 ObClient *c, guint state,
163 guint button, gint x, gint y, Time time)
164 {
165 GSList *it;
166 ObMouseBinding *b;
167
168 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
169 b = it->data;
170 if (b->state == state && b->button == button)
171 break;
172 }
173 /* if not bound, then nothing to do! */
174 if (it == NULL) return FALSE;
175
176 action_run_mouse(b->actions[a], c, context, state, button, x, y, time);
177 return TRUE;
178 }
179
180 void mouse_event(ObClient *client, XEvent *e)
181 {
182 static Time ltime;
183 static guint button = 0, state = 0, lbutton = 0;
184 static Window lwindow = None;
185 static gint px, py, pwx = -1, pwy = -1;
186
187 ObFrameContext context;
188 gboolean click = FALSE;
189 gboolean dclick = FALSE;
190
191 switch (e->type) {
192 case ButtonPress:
193 context = frame_context(client, e->xbutton.window,
194 e->xbutton.x, e->xbutton.y);
195 context = mouse_button_frame_context(context, e->xbutton.button);
196
197 px = e->xbutton.x_root;
198 py = e->xbutton.y_root;
199 if (!button) pwx = e->xbutton.x;
200 if (!button) pwy = e->xbutton.y;
201 button = e->xbutton.button;
202 state = e->xbutton.state;
203
204 fire_binding(OB_MOUSE_ACTION_PRESS, context,
205 client, e->xbutton.state,
206 e->xbutton.button,
207 e->xbutton.x_root, e->xbutton.y_root,
208 e->xbutton.time);
209
210 /* if the bindings grab the pointer, there won't be a ButtonRelease
211 event for us */
212 if (grab_on_pointer())
213 button = 0;
214
215 if (CLIENT_CONTEXT(context, client)) {
216 /* Replay the event, so it goes to the client*/
217 XAllowEvents(ob_display, ReplayPointer, event_curtime);
218 /* Fall through to the release case! */
219 } else
220 break;
221
222 case ButtonRelease:
223 /* use where the press occured in the window */
224 context = frame_context(client, e->xbutton.window, pwx, pwy);
225 context = mouse_button_frame_context(context, e->xbutton.button);
226
227 if (e->xbutton.button == button)
228 pwx = pwy = -1;
229
230 if (e->xbutton.button == button) {
231 /* clicks are only valid if its released over the window */
232 gint junk1, junk2;
233 Window wjunk;
234 guint ujunk, b, w, h;
235 /* this can cause errors to occur when the window closes */
236 xerror_set_ignore(TRUE);
237 junk1 = XGetGeometry(ob_display, e->xbutton.window,
238 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
239 xerror_set_ignore(FALSE);
240 if (junk1) {
241 if (e->xbutton.x >= (signed)-b &&
242 e->xbutton.y >= (signed)-b &&
243 e->xbutton.x < (signed)(w+b) &&
244 e->xbutton.y < (signed)(h+b)) {
245 click = TRUE;
246 /* double clicks happen if there were 2 in a row! */
247 if (lbutton == button &&
248 lwindow == e->xbutton.window &&
249 e->xbutton.time - config_mouse_dclicktime <=
250 ltime) {
251 dclick = TRUE;
252 lbutton = 0;
253 } else {
254 lbutton = button;
255 lwindow = e->xbutton.window;
256 }
257 } else {
258 lbutton = 0;
259 lwindow = None;
260 }
261 }
262
263 button = 0;
264 state = 0;
265 ltime = e->xbutton.time;
266 }
267 fire_binding(OB_MOUSE_ACTION_RELEASE, context,
268 client, e->xbutton.state,
269 e->xbutton.button,
270 e->xbutton.x_root,
271 e->xbutton.y_root,
272 e->xbutton.time);
273 if (click)
274 fire_binding(OB_MOUSE_ACTION_CLICK, context,
275 client, e->xbutton.state,
276 e->xbutton.button,
277 e->xbutton.x_root,
278 e->xbutton.y_root,
279 e->xbutton.time);
280 if (dclick)
281 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
282 client, e->xbutton.state,
283 e->xbutton.button,
284 e->xbutton.x_root,
285 e->xbutton.y_root,
286 e->xbutton.time);
287 break;
288
289 case MotionNotify:
290 if (button) {
291 context = frame_context(client, e->xmotion.window, pwx, pwy);
292 context = mouse_button_frame_context(context, button);
293
294 if (ABS(e->xmotion.x_root - px) >= config_mouse_threshold ||
295 ABS(e->xmotion.y_root - py) >= config_mouse_threshold) {
296
297 /* You can't drag on buttons */
298 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
299 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
300 context == OB_FRAME_CONTEXT_SHADE ||
301 context == OB_FRAME_CONTEXT_ICONIFY ||
302 context == OB_FRAME_CONTEXT_ICON ||
303 context == OB_FRAME_CONTEXT_CLOSE)
304 break;
305
306 fire_binding(OB_MOUSE_ACTION_MOTION, context,
307 client, state, button, px, py, e->xmotion.time);
308 button = 0;
309 state = 0;
310 }
311 }
312 break;
313
314 default:
315 g_assert_not_reached();
316 }
317 }
318
319 gboolean mouse_bind(const gchar *buttonstr, const gchar *contextstr,
320 ObMouseAction mact, ObAction *action)
321 {
322 guint state, button;
323 ObFrameContext context;
324 ObMouseBinding *b;
325 GSList *it;
326
327 if (!translate_button(buttonstr, &state, &button)) {
328 g_message(_("Invalid button '%s' in mouse binding"), buttonstr);
329 return FALSE;
330 }
331
332 context = frame_context_from_string(contextstr);
333 if (!context) {
334 g_message(_("Invalid context '%s' in mouse binding"), contextstr);
335 return FALSE;
336 }
337
338 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
339 b = it->data;
340 if (b->state == state && b->button == button) {
341 b->actions[mact] = g_slist_append(b->actions[mact], action);
342 return TRUE;
343 }
344 }
345
346 /* when there are no modifiers in the binding, then the action cannot
347 be interactive */
348 if (!state && action->data.any.interactive) {
349 action->data.any.interactive = FALSE;
350 action->data.inter.final = TRUE;
351 }
352
353 /* add the binding */
354 b = g_new0(ObMouseBinding, 1);
355 b->state = state;
356 b->button = button;
357 b->actions[mact] = g_slist_append(NULL, action);
358 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
359
360 return TRUE;
361 }
362
363 void mouse_startup(gboolean reconfig)
364 {
365 grab_all_clients(TRUE);
366 }
367
368 void mouse_shutdown(gboolean reconfig)
369 {
370 grab_all_clients(FALSE);
371 mouse_unbind_all();
372 }
This page took 0.050134 seconds and 5 git commands to generate.