]> icculus.org git repositories - mikachu/openbox.git/blob - plugins/mouse/mouse.c
save the client and context during drags since a pointer grab will change them
[mikachu/openbox.git] / plugins / mouse / mouse.c
1 #include "kernel/openbox.h"
2 #include "kernel/dispatch.h"
3 #include "kernel/action.h"
4 #include "kernel/event.h"
5 #include "kernel/client.h"
6 #include "kernel/grab.h"
7 #include "kernel/parse.h"
8 #include "kernel/frame.h"
9 #include "translate.h"
10 #include "mouse.h"
11 #include "mouseparse.h"
12 #include <glib.h>
13
14 static int threshold;
15 static int dclicktime;
16
17 static void parse_assign(char *name, ParseToken *value)
18 {
19     if (!g_ascii_strcasecmp(name, "dragthreshold")) {
20         if (value->type != TOKEN_INTEGER)
21             yyerror("invalid value");
22         else {
23             if (value->data.integer >= 0)
24                 threshold = value->data.integer;
25         }
26     } else if (!g_ascii_strcasecmp(name, "doubleclicktime")) {
27         if (value->type != TOKEN_INTEGER)
28             yyerror("invalid value");
29         else {
30             if (value->data.integer >= 0)
31                 dclicktime = value->data.integer;
32         }
33     } else
34         yyerror("invalid option");
35     parse_free_token(value);
36 }
37
38 void plugin_setup_config()
39 {
40     threshold = 3;
41     dclicktime = 200;
42     parse_reg_section("mouse", mouseparse, parse_assign);
43 }
44
45 /* Array of GSList*s of PointerBinding*s. */
46 static GSList *bound_contexts[NUM_CONTEXTS];
47
48 static void grab_for_client(Client *client, gboolean grab)
49 {
50     int i;
51     GSList *it;
52
53     for (i = 0; i < NUM_CONTEXTS; ++i)
54         for (it = bound_contexts[i]; it != NULL; it = it->next) {
55             /* grab/ungrab the button */
56             MouseBinding *b = it->data;
57             Window win;
58             int mode;
59             unsigned int mask;
60
61             if (i == Context_Frame) {
62                 win = client->frame->window;
63                 mode = GrabModeAsync;
64                 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
65             } else if (i == Context_Client) {
66                 win = client->frame->plate;
67                 mode = GrabModeSync; /* this is handled in event */
68                 mask = ButtonPressMask; /* can't catch more than this with Sync
69                                            mode the release event is
70                                            manufactured in event() */
71             } else continue;
72
73             if (grab)
74                 grab_button(b->button, b->state, win, mask, mode);
75             else
76                 ungrab_button(b->button, b->state, win);
77         }
78 }
79
80 static void grab_all_clients(gboolean grab)
81 {
82     GList *it;
83
84     for (it = client_list; it != NULL; it = it->next)
85         grab_for_client(it->data, grab);
86 }
87
88 static void clearall()
89 {
90     int i;
91     GSList *it;
92     
93     for(i = 0; i < NUM_CONTEXTS; ++i) {
94         for (it = bound_contexts[i]; it != NULL; it = it->next) {
95             int j;
96
97             MouseBinding *b = it->data;
98             for (j = 0; j < NUM_MOUSEACTION; ++j)
99                 if (b->action[j] != NULL)
100                     action_free(b->action[j]);
101             g_free(b);
102         }
103         g_slist_free(bound_contexts[i]);
104     }
105 }
106
107 static void fire_button(MouseAction a, Context context, Client *c, guint state,
108                         guint button, int x, int y)
109 {
110     GSList *it;
111     MouseBinding *b;
112
113     for (it = bound_contexts[context]; it != NULL; it = it->next) {
114         b = it->data;
115         if (b->state == state && b->button == button)
116             break;
117     }
118     /* if not bound, then nothing to do! */
119     if (it == NULL) return;
120
121     if (b->action[a] != NULL && b->action[a]->func != NULL) {
122         b->action[a]->data.any.c = c;
123
124         g_assert(!(b->action[a]->func == action_move ||
125                    b->action[a]->func == action_resize));
126
127         if (b->action[a]->func == action_showmenu) {
128             b->action[a]->data.showmenu.x = x;
129             b->action[a]->data.showmenu.y = y;
130         }
131
132         b->action[a]->func(&b->action[a]->data);
133     }
134 }
135
136 /* corner should be the opposite corner of the window in which the pointer
137    clicked, Corner_TopLeft if a good default if there is no client 
138    Returns True or False for if a binding existed for the action or not.
139 */
140 static gboolean fire_motion(MouseAction a, Context context, Client *c,
141                             guint state, guint button, int cx, int cy,
142                             int cw, int ch, int dx, int dy,
143                             gboolean final, Corner corner)
144 {
145     GSList *it;
146     MouseBinding *b;
147
148     for (it = bound_contexts[context]; it != NULL; it = it->next) {
149         b = it->data;
150         if (b->state == state && b->button == button)
151                 break;
152     }
153     /* if not bound, then nothing to do! */
154     if (it == NULL) return FALSE;
155
156     if (b->action[a] != NULL && b->action[a]->func != NULL) {
157         b->action[a]->data.any.c = c;
158
159         if (b->action[a]->func == action_move) {
160             b->action[a]->data.move.x = cx + dx;
161             b->action[a]->data.move.y = cy + dy;
162             b->action[a]->data.move.final = final;
163         } else if (b->action[a]->func == action_resize) {
164             b->action[a]->data.resize.corner = corner;
165             switch (corner) {
166             case Corner_TopLeft:
167                 b->action[a]->data.resize.x = cw + dx;
168                 b->action[a]->data.resize.y = ch + dy;
169                 break;
170             case Corner_TopRight:
171                 b->action[a]->data.resize.x = cw - dx;
172                 b->action[a]->data.resize.y = ch + dy;
173                 break;
174             case Corner_BottomLeft:
175                 b->action[a]->data.resize.x = cw + dx;
176                 b->action[a]->data.resize.y = ch - dy;
177                 break;
178             case Corner_BottomRight:
179                 b->action[a]->data.resize.x = cw - dx;
180                 b->action[a]->data.resize.y = ch - dy;
181                 break;
182             }
183             b->action[a]->data.resize.final = final;
184         } else
185             g_assert_not_reached();
186         b->action[a]->func(&b->action[a]->data);
187         return TRUE;
188     }
189     return FALSE;
190 }
191
192 static Corner pick_corner(int x, int y, int cx, int cy, int cw, int ch)
193 {
194     if (x - cx < cw / 2) {
195         if (y - cy < ch / 2)
196             return Corner_BottomRight;
197         else
198             return Corner_TopRight;
199     } else {
200         if (y - cy < ch / 2)
201             return Corner_BottomLeft;
202         else
203             return Corner_TopLeft;
204     }
205 }
206
207 static void event(ObEvent *e, void *foo)
208 {
209     static Time ltime;
210     static int px, py, cx, cy, cw, ch, dx, dy;
211     static guint button = 0, state = 0, lbutton = 0;
212     static gboolean drag = FALSE, drag_used = FALSE;
213     static Corner corner = Corner_TopLeft;
214     static Client *drag_client = NULL;
215     static Context drag_context = Context_None;
216     gboolean click = FALSE;
217     gboolean dclick = FALSE;
218     Context context;
219     
220     switch (e->type) {
221     case Event_Client_Mapped:
222         grab_for_client(e->data.c.client, TRUE);
223         break;
224
225     case Event_Client_Destroy:
226         grab_for_client(e->data.c.client, FALSE);
227         break;
228
229     case Event_X_ButtonPress:
230         context = frame_context(e->data.x.client,
231                                 e->data.x.e->xbutton.window);
232
233         if (!button) {
234             if (e->data.x.client != NULL) {
235                 cx = e->data.x.client->frame->area.x;
236                 cy = e->data.x.client->frame->area.y;
237                 /* use the client size because the frame can be differently
238                    sized (shaded windows) and we want this based on the clients
239                    size */
240                 cw = e->data.x.client->area.width + 
241                     e->data.x.client->frame->size.left +
242                     e->data.x.client->frame->size.right;
243                 ch = e->data.x.client->area.height +
244                     e->data.x.client->frame->size.top +
245                     e->data.x.client->frame->size.bottom;
246                 px = e->data.x.e->xbutton.x_root;
247                 py = e->data.x.e->xbutton.y_root;
248                 corner = pick_corner(px, py, cx, cy, cw, ch);
249             }
250             button = e->data.x.e->xbutton.button;
251             state = e->data.x.e->xbutton.state;
252             drag_context = context;
253             drag_client = e->data.x.client;
254         }
255
256         fire_button(MouseAction_Press, context,
257                     e->data.x.client, e->data.x.e->xbutton.state,
258                     e->data.x.e->xbutton.button,
259                     e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
260
261         if (context == Context_Client) {
262             /* Replay the event, so it goes to the client*/
263             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
264             /* Fall through to the release case! */
265         } else
266             break;
267
268     case Event_X_ButtonRelease:
269         context = frame_context(e->data.x.client,
270                                 e->data.x.e->xbutton.window);
271         if (e->data.x.e->xbutton.button == button) {
272             /* end drags */
273             if (drag_used) {
274                 fire_motion(MouseAction_Motion, drag_context,
275                             drag_client, state, button,
276                             cx, cy, cw, ch, dx, dy, TRUE, corner);
277                 drag = drag_used = FALSE;
278                 drag_context = Context_None;
279                 drag_client = NULL;
280                 
281                 lbutton = 0;
282             } else {
283                 /* clicks are only valid if its released over the window */
284                 int junk;
285                 Window wjunk;
286                 guint ujunk, b, w, h;
287                 XGetGeometry(ob_display, e->data.x.e->xbutton.window,
288                              &wjunk, &junk, &junk, &w, &h, &b, &ujunk);
289                 if (e->data.x.e->xbutton.x >= (signed)-b &&
290                     e->data.x.e->xbutton.y >= (signed)-b &&
291                     e->data.x.e->xbutton.x < (signed)(w+b) &&
292                     e->data.x.e->xbutton.y < (signed)(h+b)) {
293                     click = TRUE;
294                     /* double clicks happen if there were 2 in a row! */
295                     if (lbutton == button &&
296                         e->data.x.e->xbutton.time - dclicktime <= ltime) {
297                         dclick = TRUE;
298                         lbutton = 0;
299                     } else
300                         lbutton = button;
301                 } else
302                     lbutton = 0;
303             }
304
305             button = 0;
306             state = 0;
307             ltime = e->data.x.e->xbutton.time;
308         }
309         fire_button(MouseAction_Release, context,
310                     e->data.x.client, e->data.x.e->xbutton.state,
311                     e->data.x.e->xbutton.button,
312                     e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
313         if (click)
314             fire_button(MouseAction_Click, context,
315                         e->data.x.client, e->data.x.e->xbutton.state,
316                         e->data.x.e->xbutton.button,
317                         e->data.x.e->xbutton.x_root,
318                         e->data.x.e->xbutton.y_root);
319         if (dclick)
320             fire_button(MouseAction_DClick, context,
321                         e->data.x.client, e->data.x.e->xbutton.state,
322                         e->data.x.e->xbutton.button,
323                         e->data.x.e->xbutton.x_root,
324                         e->data.x.e->xbutton.y_root);
325         break;
326
327     case Event_X_MotionNotify:
328         if (button) {
329             dx = e->data.x.e->xmotion.x_root - px;
330             dy = e->data.x.e->xmotion.y_root - py;
331             if (!drag &&
332                 (ABS(dx) >= threshold || ABS(dy) >= threshold)) {
333                 drag = TRUE;
334             }
335             if (drag) {
336                 drag_used = fire_motion(MouseAction_Motion, drag_context,
337                                         drag_client,
338                                         state, button, cx, cy, cw, ch, dx, dy,
339                                         FALSE, corner);
340             }
341         }
342         break;
343
344     default:
345         g_assert_not_reached();
346     }
347 }
348
349 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
350                Action *action)
351 {
352     guint state, button;
353     Context context;
354     MouseBinding *b;
355     GSList *it;
356
357     if (!translate_button(buttonstr, &state, &button)) {
358         g_warning("invalid button '%s'", buttonstr);
359         return FALSE;
360     }
361
362     contextstr = g_ascii_strdown(contextstr, -1);
363     context = frame_context_from_string(contextstr);
364     if (!context) {
365         g_warning("invalid context '%s'", contextstr);
366         g_free(contextstr);
367         return FALSE;
368     }
369     g_free(contextstr);
370
371     for (it = bound_contexts[context]; it != NULL; it = it->next){
372         b = it->data;
373         if (b->state == state && b->button == button) {
374             /* already bound */
375             if (b->action[mact] != NULL) {
376                 g_warning("duplicate binding");
377                 return FALSE;
378             }
379             b->action[mact] = action;
380             return TRUE;
381         }
382     }
383
384     grab_all_clients(FALSE);
385
386     /* add the binding */
387     b = g_new0(MouseBinding, 1);
388     b->state = state;
389     b->button = button;
390     b->action[mact] = action;
391     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
392
393     grab_all_clients(TRUE);
394
395     return TRUE;
396 }
397
398 void plugin_startup()
399 {
400     dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
401                       Event_X_ButtonPress | Event_X_ButtonRelease |
402                       Event_X_MotionNotify, (EventHandler)event, NULL);
403 }
404
405 void plugin_shutdown()
406 {
407     dispatch_register(0, (EventHandler)event, NULL);
408
409     grab_all_clients(FALSE);
410     clearall();
411 }