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