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