]> icculus.org git repositories - dana/openbox.git/blob - plugins/mouse/mouse.c
* Change xml parsing to pass the parent node, rather than the first
[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 int px, py;
263     gboolean click = FALSE;
264     gboolean dclick = FALSE;
265     ObFrameContext context;
266     
267     switch (e->type) {
268     case Event_Client_Mapped:
269         grab_for_client(e->data.c.client, TRUE);
270         break;
271
272     case Event_Client_Destroy:
273         grab_for_client(e->data.c.client, FALSE);
274         break;
275
276     case Event_X_ButtonPress:
277         context = frame_context(e->data.x.client,
278                                 e->data.x.e->xbutton.window);
279
280         px = e->data.x.e->xbutton.x_root;
281         py = e->data.x.e->xbutton.y_root;
282         button = e->data.x.e->xbutton.button;
283         state = e->data.x.e->xbutton.state;
284
285         fire_button(MouseAction_Press, context,
286                     e->data.x.client, e->data.x.e->xbutton.state,
287                     e->data.x.e->xbutton.button,
288                     e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
289
290         if (context == OB_FRAME_CONTEXT_CLIENT) {
291             /* Replay the event, so it goes to the client*/
292             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
293             /* Fall through to the release case! */
294         } else
295             break;
296
297     case Event_X_ButtonRelease:
298         context = frame_context(e->data.x.client,
299                                 e->data.x.e->xbutton.window);
300         if (e->data.x.e->xbutton.button == button) {
301             /* clicks are only valid if its released over the window */
302             int junk1, junk2;
303             Window wjunk;
304             guint ujunk, b, w, h;
305             XGetGeometry(ob_display, e->data.x.e->xbutton.window,
306                          &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
307             if (e->data.x.e->xbutton.x >= (signed)-b &&
308                 e->data.x.e->xbutton.y >= (signed)-b &&
309                 e->data.x.e->xbutton.x < (signed)(w+b) &&
310                 e->data.x.e->xbutton.y < (signed)(h+b)) {
311                 click = TRUE;
312                 /* double clicks happen if there were 2 in a row! */
313                 if (lbutton == button &&
314                     e->data.x.e->xbutton.time - dclicktime <= ltime) {
315                     dclick = TRUE;
316                     lbutton = 0;
317                 } else
318                     lbutton = button;
319             } else
320                 lbutton = 0;
321
322             button = 0;
323             state = 0;
324             ltime = e->data.x.e->xbutton.time;
325         }
326         fire_button(MouseAction_Release, context,
327                     e->data.x.client, e->data.x.e->xbutton.state,
328                     e->data.x.e->xbutton.button,
329                     e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
330         if (click)
331             fire_button(MouseAction_Click, context,
332                         e->data.x.client, e->data.x.e->xbutton.state,
333                         e->data.x.e->xbutton.button,
334                         e->data.x.e->xbutton.x_root,
335                         e->data.x.e->xbutton.y_root);
336         if (dclick)
337             fire_button(MouseAction_DClick, 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         break;
343
344     case Event_X_MotionNotify:
345         if (button) {
346             if (ABS(e->data.x.e->xmotion.x_root - px) >= threshold ||
347                 ABS(e->data.x.e->xmotion.y_root - py) >= threshold) {
348                 guint32 corner;
349
350                 context = frame_context(e->data.x.client,
351                                         e->data.x.e->xmotion.window);
352
353                 /* You can't drag on buttons */
354                 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
355                     context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
356                     context == OB_FRAME_CONTEXT_SHADE ||
357                     context == OB_FRAME_CONTEXT_ICONIFY ||
358                     context == OB_FRAME_CONTEXT_ICON ||
359                     context == OB_FRAME_CONTEXT_CLOSE)
360                     break;
361
362                 if (!e->data.x.client)
363                     corner = prop_atoms.net_wm_moveresize_size_bottomright;
364                 else
365                     corner =
366                         pick_corner(e->data.x.e->xmotion.x_root,
367                                     e->data.x.e->xmotion.y_root,
368                                     e->data.x.client->frame->area.x,
369                                     e->data.x.client->frame->area.y,
370                                     /* use the client size because the frame
371                                        can be differently sized (shaded
372                                        windows) and we want this based on the
373                                        clients size */
374                                     e->data.x.client->area.width +
375                                     e->data.x.client->frame->size.left +
376                                     e->data.x.client->frame->size.right,
377                                     e->data.x.client->area.height +
378                                     e->data.x.client->frame->size.top +
379                                     e->data.x.client->frame->size.bottom);
380                 fire_motion(MouseAction_Motion, context,
381                             e->data.x.client, state, button,
382                             e->data.x.e->xmotion.x_root, 
383                             e->data.x.e->xmotion.y_root, corner);
384                 button = 0;
385                 state = 0;
386             }
387         }
388         break;
389
390     default:
391         g_assert_not_reached();
392     }
393 }
394
395 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
396                Action *action)
397 {
398     guint state, button;
399     ObFrameContext context;
400     MouseBinding *b;
401     GSList *it;
402
403     if (!translate_button(buttonstr, &state, &button)) {
404         g_warning("invalid button '%s'", buttonstr);
405         return FALSE;
406     }
407
408     contextstr = g_ascii_strdown(contextstr, -1);
409     context = frame_context_from_string(contextstr);
410     if (!context) {
411         g_warning("invalid context '%s'", contextstr);
412         g_free(contextstr);
413         return FALSE;
414     }
415     g_free(contextstr);
416
417     for (it = bound_contexts[context]; it != NULL; it = it->next){
418         b = it->data;
419         if (b->state == state && b->button == button) {
420             b->actions[mact] = g_slist_append(b->actions[mact], action);
421             return TRUE;
422         }
423     }
424
425     grab_all_clients(FALSE);
426
427     /* add the binding */
428     b = g_new0(MouseBinding, 1);
429     b->state = state;
430     b->button = button;
431     b->actions[mact] = g_slist_append(NULL, action);
432     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
433
434     grab_all_clients(TRUE);
435
436     return TRUE;
437 }
438
439 void plugin_startup()
440 {
441     dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
442                       Event_X_ButtonPress | Event_X_ButtonRelease |
443                       Event_X_MotionNotify, (EventHandler)event, NULL);
444 }
445
446 void plugin_shutdown()
447 {
448     dispatch_register(0, (EventHandler)event, NULL);
449
450     grab_all_clients(FALSE);
451     clearall();
452 }