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