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