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