keep event_lasttime at the last time an event has come in so far, don't regress when...
[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/frame.h"
9 #include "parser/parse.h"
10 #include "translate.h"
11 #include "mouse.h"
12 #include <glib.h>
13
14 static int threshold;
15 static int dclicktime;
16 /*
17
18 <context name="Titlebar"> 
19   <mousebind button="Left" action="Press">
20     <action name="Raise"></action>
21   </mousebind>
22 </context>
23
24 */
25
26 static void parse_xml(xmlDocPtr doc, xmlNodePtr node, void *d)
27 {
28     xmlNodePtr n, nbut, nact;
29     char *buttonstr;
30     char *contextstr;
31     MouseAction mact;
32     Action *action;
33
34     node = node->xmlChildrenNode;
35     
36     if ((n = parse_find_node("dragThreshold", node)))
37         threshold = parse_int(doc, n);
38     if ((n = parse_find_node("doubleClickTime", node)))
39         dclicktime = parse_int(doc, n);
40
41     n = parse_find_node("context", node);
42     while (n) {
43         if (!parse_attr_string("name", n, &contextstr))
44             goto next_n;
45         nbut = parse_find_node("mousebind", n->xmlChildrenNode);
46         while (nbut) {
47             if (!parse_attr_string("button", nbut, &buttonstr))
48                 goto next_nbut;
49             if (parse_attr_contains("press", nbut, "action"))
50                 mact = MouseAction_Press;
51             else if (parse_attr_contains("release", nbut, "action"))
52                 mact = MouseAction_Release;
53             else if (parse_attr_contains("click", nbut, "action"))
54                 mact = MouseAction_Click;
55             else if (parse_attr_contains("doubleclick", nbut,"action"))
56                 mact = MouseAction_DClick;
57             else if (parse_attr_contains("drag", nbut, "action"))
58                 mact = MouseAction_Motion;
59             else
60                 goto next_nbut;
61             nact = parse_find_node("action", nbut->xmlChildrenNode);
62             while (nact) {
63                 if ((action = action_parse(doc, nact))) {
64                     /* validate that its okay for a mouse binding*/
65                     if (mact == MouseAction_Motion) {
66                         if (action->func != action_moveresize ||
67                             action->data.moveresize.corner ==
68                             prop_atoms.net_wm_moveresize_move_keyboard ||
69                             action->data.moveresize.corner ==
70                             prop_atoms.net_wm_moveresize_size_keyboard) {
71                             action_free(action);
72                             action = NULL;
73                         }
74                     } else {
75                         if (action->func == action_moveresize &&
76                             action->data.moveresize.corner !=
77                             prop_atoms.net_wm_moveresize_move_keyboard &&
78                             action->data.moveresize.corner !=
79                             prop_atoms.net_wm_moveresize_size_keyboard) {
80                             action_free(action);
81                             action = NULL;
82                         }
83                     }
84                     if (action)
85                         mbind(buttonstr, contextstr, mact, action);
86                 }
87                 nact = parse_find_node("action", nact->next);
88             }
89             g_free(buttonstr);
90         next_nbut:
91             nbut = parse_find_node("mousebind", nbut->next);
92         }
93         g_free(contextstr);
94     next_n:
95         n = parse_find_node("context", n->next);
96     }
97 }
98
99 void plugin_setup_config()
100 {
101     threshold = 3;
102     dclicktime = 200;
103     parse_register("mouse", parse_xml, NULL);
104 }
105
106 /* Array of GSList*s of PointerBinding*s. */
107 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
108
109 static void grab_for_client(ObClient *client, gboolean grab)
110 {
111     int i;
112     GSList *it;
113
114     for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
115         for (it = bound_contexts[i]; it != NULL; it = it->next) {
116             /* grab/ungrab the button */
117             MouseBinding *b = it->data;
118             Window win;
119             int mode;
120             unsigned int mask;
121
122             if (i == OB_FRAME_CONTEXT_FRAME) {
123                 win = client->frame->window;
124                 mode = GrabModeAsync;
125                 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
126             } else if (i == OB_FRAME_CONTEXT_CLIENT) {
127                 win = client->frame->plate;
128                 mode = GrabModeSync; /* this is handled in event */
129                 mask = ButtonPressMask; /* can't catch more than this with Sync
130                                            mode the release event is
131                                            manufactured in event() */
132             } else continue;
133
134             if (grab)
135                 grab_button_full(b->button, b->state, win, mask, mode, None);
136             else
137                 ungrab_button(b->button, b->state, win);
138         }
139 }
140
141 static void grab_all_clients(gboolean grab)
142 {
143     GList *it;
144
145     for (it = client_list; it != NULL; it = it->next)
146         grab_for_client(it->data, grab);
147 }
148
149 static void clearall()
150 {
151     int i;
152     GSList *it;
153     
154     for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
155         for (it = bound_contexts[i]; it != NULL; it = it->next) {
156             int j;
157
158             MouseBinding *b = it->data;
159             for (j = 0; j < NUM_MOUSEACTION; ++j) {
160                 GSList *it;
161                 for (it = b->actions[j]; it; it = it->next) {
162                     action_free(it->data);
163                 }
164                 g_slist_free(b->actions[j]);
165             }
166             g_free(b);
167         }
168         g_slist_free(bound_contexts[i]);
169     }
170 }
171
172 static void fire_button(MouseAction a, ObFrameContext context,
173                         ObClient *c, guint state,
174                         guint button, int x, int y)
175 {
176     GSList *it;
177     MouseBinding *b;
178
179     for (it = bound_contexts[context]; it != NULL; it = it->next) {
180         b = it->data;
181         if (b->state == state && b->button == button)
182             break;
183     }
184     /* if not bound, then nothing to do! */
185     if (it == NULL) return;
186
187     for (it = b->actions[a]; it; it = it->next) {
188         Action *act = it->data;
189         if (act->func != NULL) {
190             act->data.any.c = c;
191
192             g_assert(act->func != action_moveresize);
193
194             if (act->func == action_showmenu) {
195                 act->data.showmenu.x = x;
196                 act->data.showmenu.y = y;
197             }
198
199             act->func(&act->data);
200         }
201     }
202 }
203
204 static void fire_motion(MouseAction a, ObFrameContext context, ObClient *c,
205                         guint state, guint button, int x_root, int y_root,
206                         guint32 corner)
207 {
208     GSList *it;
209     MouseBinding *b;
210
211     for (it = bound_contexts[context]; it != NULL; it = it->next) {
212         b = it->data;
213         if (b->state == state && b->button == button)
214                 break;
215     }
216     /* if not bound, then nothing to do! */
217     if (it == NULL) return;
218
219     for (it = b->actions[a]; it; it = it->next) {
220         Action *act = it->data;
221         if (act->func != NULL) {
222             act->data.any.c = c;
223
224             if (act->func == action_moveresize) {
225                 act->data.moveresize.x = x_root;
226                 act->data.moveresize.y = y_root;
227                 act->data.moveresize.button = button;
228                 if (!(act->data.moveresize.corner ==
229                       prop_atoms.net_wm_moveresize_move ||
230                       act->data.moveresize.corner ==
231                       prop_atoms.net_wm_moveresize_move_keyboard ||
232                       act->data.moveresize.corner ==
233                       prop_atoms.net_wm_moveresize_size_keyboard))
234                     act->data.moveresize.corner = corner;
235             } else
236                 g_assert_not_reached();
237
238             act->func(&act->data);
239         }
240     }
241 }
242
243 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
244 {
245     if (x - cx < cw / 2) {
246         if (y - cy < ch / 2)
247             return prop_atoms.net_wm_moveresize_size_topleft;
248         else
249             return prop_atoms.net_wm_moveresize_size_bottomleft;
250     } else {
251         if (y - cy < ch / 2)
252             return prop_atoms.net_wm_moveresize_size_topright;
253         else
254             return prop_atoms.net_wm_moveresize_size_bottomright;
255     }
256 }
257
258 static void event(ObEvent *e, void *foo)
259 {
260     static Time ltime;
261     static guint button = 0, state = 0, lbutton = 0;
262     static Window lwindow = None;
263     static int px, py;
264     gboolean click = FALSE;
265     gboolean dclick = FALSE;
266     ObFrameContext context;
267     
268     switch (e->type) {
269     case Event_Client_Mapped:
270         grab_for_client(e->data.c.client, TRUE);
271         break;
272
273     case Event_Client_Destroy:
274         grab_for_client(e->data.c.client, FALSE);
275         break;
276
277     case Event_X_ButtonPress:
278         context = frame_context(e->data.x.client,
279                                 e->data.x.e->xbutton.window);
280
281         px = e->data.x.e->xbutton.x_root;
282         py = e->data.x.e->xbutton.y_root;
283         button = e->data.x.e->xbutton.button;
284         state = e->data.x.e->xbutton.state;
285
286         fire_button(MouseAction_Press, context,
287                     e->data.x.client, e->data.x.e->xbutton.state,
288                     e->data.x.e->xbutton.button,
289                     e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
290
291         if (context == OB_FRAME_CONTEXT_CLIENT) {
292             /* Replay the event, so it goes to the client*/
293             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
294             /* Fall through to the release case! */
295         } else
296             break;
297
298     case Event_X_ButtonRelease:
299         context = frame_context(e->data.x.client,
300                                 e->data.x.e->xbutton.window);
301         if (e->data.x.e->xbutton.button == button) {
302             /* clicks are only valid if its released over the window */
303             int junk1, junk2;
304             Window wjunk;
305             guint ujunk, b, w, h;
306             XGetGeometry(ob_display, e->data.x.e->xbutton.window,
307                          &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
308             if (e->data.x.e->xbutton.x >= (signed)-b &&
309                 e->data.x.e->xbutton.y >= (signed)-b &&
310                 e->data.x.e->xbutton.x < (signed)(w+b) &&
311                 e->data.x.e->xbutton.y < (signed)(h+b)) {
312                 click = TRUE;
313                 /* double clicks happen if there were 2 in a row! */
314                 if (lbutton == button &&
315                     lwindow == e->data.x.e->xbutton.window &&
316                     e->data.x.e->xbutton.time - dclicktime <= ltime) {
317                     dclick = TRUE;
318                     lbutton = 0;
319                 } else {
320                     lbutton = button;
321                     lwindow = e->data.x.e->xbutton.window;
322                 }
323             } else {
324                 lbutton = 0;
325                 lwindow = None;
326             }
327
328             button = 0;
329             state = 0;
330             ltime = e->data.x.e->xbutton.time;
331         }
332         fire_button(MouseAction_Release, context,
333                     e->data.x.client, e->data.x.e->xbutton.state,
334                     e->data.x.e->xbutton.button,
335                     e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
336         if (click)
337             fire_button(MouseAction_Click, context,
338                         e->data.x.client, e->data.x.e->xbutton.state,
339                         e->data.x.e->xbutton.button,
340                         e->data.x.e->xbutton.x_root,
341                         e->data.x.e->xbutton.y_root);
342         if (dclick)
343             fire_button(MouseAction_DClick, context,
344                         e->data.x.client, e->data.x.e->xbutton.state,
345                         e->data.x.e->xbutton.button,
346                         e->data.x.e->xbutton.x_root,
347                         e->data.x.e->xbutton.y_root);
348         break;
349
350     case Event_X_MotionNotify:
351         if (button) {
352             if (ABS(e->data.x.e->xmotion.x_root - px) >= threshold ||
353                 ABS(e->data.x.e->xmotion.y_root - py) >= threshold) {
354                 guint32 corner;
355
356                 context = frame_context(e->data.x.client,
357                                         e->data.x.e->xmotion.window);
358
359                 /* You can't drag on buttons */
360                 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
361                     context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
362                     context == OB_FRAME_CONTEXT_SHADE ||
363                     context == OB_FRAME_CONTEXT_ICONIFY ||
364                     context == OB_FRAME_CONTEXT_ICON ||
365                     context == OB_FRAME_CONTEXT_CLOSE)
366                     break;
367
368                 if (!e->data.x.client)
369                     corner = prop_atoms.net_wm_moveresize_size_bottomright;
370                 else
371                     corner =
372                         pick_corner(e->data.x.e->xmotion.x_root,
373                                     e->data.x.e->xmotion.y_root,
374                                     e->data.x.client->frame->area.x,
375                                     e->data.x.client->frame->area.y,
376                                     /* use the client size because the frame
377                                        can be differently sized (shaded
378                                        windows) and we want this based on the
379                                        clients size */
380                                     e->data.x.client->area.width +
381                                     e->data.x.client->frame->size.left +
382                                     e->data.x.client->frame->size.right,
383                                     e->data.x.client->area.height +
384                                     e->data.x.client->frame->size.top +
385                                     e->data.x.client->frame->size.bottom);
386                 fire_motion(MouseAction_Motion, context,
387                             e->data.x.client, state, button, px, py, corner);
388                 button = 0;
389                 state = 0;
390             }
391         }
392         break;
393
394     default:
395         g_assert_not_reached();
396     }
397 }
398
399 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
400                Action *action)
401 {
402     guint state, button;
403     ObFrameContext context;
404     MouseBinding *b;
405     GSList *it;
406
407     if (!translate_button(buttonstr, &state, &button)) {
408         g_warning("invalid button '%s'", buttonstr);
409         return FALSE;
410     }
411
412     contextstr = g_ascii_strdown(contextstr, -1);
413     context = frame_context_from_string(contextstr);
414     if (!context) {
415         g_warning("invalid context '%s'", contextstr);
416         g_free(contextstr);
417         return FALSE;
418     }
419     g_free(contextstr);
420
421     for (it = bound_contexts[context]; it != NULL; it = it->next){
422         b = it->data;
423         if (b->state == state && b->button == button) {
424             b->actions[mact] = g_slist_append(b->actions[mact], action);
425             return TRUE;
426         }
427     }
428
429     grab_all_clients(FALSE);
430
431     /* add the binding */
432     b = g_new0(MouseBinding, 1);
433     b->state = state;
434     b->button = button;
435     b->actions[mact] = g_slist_append(NULL, action);
436     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
437
438     grab_all_clients(TRUE);
439
440     return TRUE;
441 }
442
443 void plugin_startup()
444 {
445     dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
446                       Event_X_ButtonPress | Event_X_ButtonRelease |
447                       Event_X_MotionNotify, (EventHandler)event, NULL);
448 }
449
450 void plugin_shutdown()
451 {
452     dispatch_register(0, (EventHandler)event, NULL);
453
454     grab_all_clients(FALSE);
455     clearall();
456 }