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