]> icculus.org git repositories - dana/openbox.git/blob - plugins/mouse/mouse.c
remove that fancy new leftHanded option, it should be done via xmodmap instead
[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[NUM_CONTEXTS];
106
107 static void grab_for_client(Client *client, gboolean grab)
108 {
109     int i;
110     GSList *it;
111
112     for (i = 0; i < 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 == Context_Frame) {
121                 win = client->frame->window;
122                 mode = GrabModeAsync;
123                 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
124             } else if (i == 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 < 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, Context context, Client *c, guint state,
171                         guint button, int x, int y)
172 {
173     GSList *it;
174     MouseBinding *b;
175
176     for (it = bound_contexts[context]; it != NULL; it = it->next) {
177         b = it->data;
178         if (b->state == state && b->button == button)
179             break;
180     }
181     /* if not bound, then nothing to do! */
182     if (it == NULL) return;
183
184     for (it = b->actions[a]; it; it = it->next) {
185         Action *act = it->data;
186         if (act->func != NULL) {
187             act->data.any.c = c;
188
189             g_assert(act->func != action_moveresize);
190
191             if (act->func == action_showmenu) {
192                 act->data.showmenu.x = x;
193                 act->data.showmenu.y = y;
194             }
195
196             act->func(&act->data);
197         }
198     }
199 }
200
201 static void fire_motion(MouseAction a, Context context, Client *c,
202                         guint state, guint button, int x_root, int y_root,
203                         guint32 corner)
204 {
205     GSList *it;
206     MouseBinding *b;
207
208     for (it = bound_contexts[context]; it != NULL; it = it->next) {
209         b = it->data;
210         if (b->state == state && b->button == button)
211                 break;
212     }
213     /* if not bound, then nothing to do! */
214     if (it == NULL) return;
215
216     for (it = b->actions[a]; it; it = it->next) {
217         Action *act = it->data;
218         if (act->func != NULL) {
219             act->data.any.c = c;
220
221             if (act->func == action_moveresize) {
222                 act->data.moveresize.x = x_root;
223                 act->data.moveresize.y = y_root;
224                 act->data.moveresize.button = button;
225                 if (!(act->data.moveresize.corner ==
226                       prop_atoms.net_wm_moveresize_move ||
227                       act->data.moveresize.corner ==
228                       prop_atoms.net_wm_moveresize_move_keyboard ||
229                       act->data.moveresize.corner ==
230                       prop_atoms.net_wm_moveresize_size_keyboard))
231                     act->data.moveresize.corner = corner;
232             } else
233                 g_assert_not_reached();
234
235             act->func(&act->data);
236         }
237     }
238 }
239
240 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
241 {
242     if (x - cx < cw / 2) {
243         if (y - cy < ch / 2)
244             return prop_atoms.net_wm_moveresize_size_topleft;
245         else
246             return prop_atoms.net_wm_moveresize_size_bottomleft;
247     } else {
248         if (y - cy < ch / 2)
249             return prop_atoms.net_wm_moveresize_size_topright;
250         else
251             return prop_atoms.net_wm_moveresize_size_bottomright;
252     }
253 }
254
255 static void event(ObEvent *e, void *foo)
256 {
257     static Time ltime;
258     static guint button = 0, state = 0, lbutton = 0;
259     static int px, py;
260     gboolean click = FALSE;
261     gboolean dclick = FALSE;
262     Context context;
263     
264     switch (e->type) {
265     case Event_Client_Mapped:
266         grab_for_client(e->data.c.client, TRUE);
267         break;
268
269     case Event_Client_Destroy:
270         grab_for_client(e->data.c.client, FALSE);
271         break;
272
273     case Event_X_ButtonPress:
274         context = frame_context(e->data.x.client,
275                                 e->data.x.e->xbutton.window);
276
277         if (!button) {
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
284         fire_button(MouseAction_Press, context,
285                     e->data.x.client, e->data.x.e->xbutton.state,
286                     e->data.x.e->xbutton.button,
287                     e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
288
289         if (context == Context_Client) {
290             /* Replay the event, so it goes to the client*/
291             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
292             /* Fall through to the release case! */
293         } else
294             break;
295
296     case Event_X_ButtonRelease:
297         context = frame_context(e->data.x.client,
298                                 e->data.x.e->xbutton.window);
299         if (e->data.x.e->xbutton.button == button) {
300             /* clicks are only valid if its released over the window */
301             int junk1, junk2;
302             Window wjunk;
303             guint ujunk, b, w, h;
304             XGetGeometry(ob_display, e->data.x.e->xbutton.window,
305                          &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
306             if (e->data.x.e->xbutton.x >= (signed)-b &&
307                 e->data.x.e->xbutton.y >= (signed)-b &&
308                 e->data.x.e->xbutton.x < (signed)(w+b) &&
309                 e->data.x.e->xbutton.y < (signed)(h+b)) {
310                 click = TRUE;
311                 /* double clicks happen if there were 2 in a row! */
312                 if (lbutton == button &&
313                     e->data.x.e->xbutton.time - dclicktime <= ltime) {
314                     dclick = TRUE;
315                     lbutton = 0;
316                 } else
317                     lbutton = button;
318             } else
319                 lbutton = 0;
320
321             button = 0;
322             state = 0;
323             ltime = e->data.x.e->xbutton.time;
324         }
325         fire_button(MouseAction_Release, context,
326                     e->data.x.client, e->data.x.e->xbutton.state,
327                     e->data.x.e->xbutton.button,
328                     e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
329         if (click)
330             fire_button(MouseAction_Click, context,
331                         e->data.x.client, e->data.x.e->xbutton.state,
332                         e->data.x.e->xbutton.button,
333                         e->data.x.e->xbutton.x_root,
334                         e->data.x.e->xbutton.y_root);
335         if (dclick)
336             fire_button(MouseAction_DClick, context,
337                         e->data.x.client, e->data.x.e->xbutton.state,
338                         e->data.x.e->xbutton.button,
339                         e->data.x.e->xbutton.x_root,
340                         e->data.x.e->xbutton.y_root);
341         break;
342
343     case Event_X_MotionNotify:
344         if (button) {
345             if (ABS(e->data.x.e->xmotion.x_root - px) >= threshold ||
346                 ABS(e->data.x.e->xmotion.y_root - py) >= threshold) {
347                 guint32 corner;
348
349                 context = frame_context(e->data.x.client,
350                                         e->data.x.e->xmotion.window);
351
352                 /* You can't drag on buttons */
353                 if (context == Context_Maximize ||
354                     context == Context_AllDesktops ||
355                     context == Context_Shade ||
356                     context == Context_Iconify ||
357                     context == Context_Icon ||
358                     context == Context_Close)
359                     break;
360
361                 if (!e->data.x.client)
362                     corner = prop_atoms.net_wm_moveresize_size_bottomright;
363                 else
364                     corner =
365                         pick_corner(e->data.x.e->xmotion.x_root,
366                                     e->data.x.e->xmotion.y_root,
367                                     e->data.x.client->frame->area.x,
368                                     e->data.x.client->frame->area.y,
369                                     /* use the client size because the frame
370                                        can be differently sized (shaded
371                                        windows) and we want this based on the
372                                        clients size */
373                                     e->data.x.client->area.width +
374                                     e->data.x.client->frame->size.left +
375                                     e->data.x.client->frame->size.right,
376                                     e->data.x.client->area.height +
377                                     e->data.x.client->frame->size.top +
378                                     e->data.x.client->frame->size.bottom);
379                 fire_motion(MouseAction_Motion, context,
380                             e->data.x.client, state, button,
381                             e->data.x.e->xmotion.x_root, 
382                             e->data.x.e->xmotion.y_root, corner);
383                 button = 0;
384                 state = 0;
385             }
386         }
387         break;
388
389     default:
390         g_assert_not_reached();
391     }
392 }
393
394 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
395                Action *action)
396 {
397     guint state, button;
398     Context context;
399     MouseBinding *b;
400     GSList *it;
401
402     if (!translate_button(buttonstr, &state, &button)) {
403         g_warning("invalid button '%s'", buttonstr);
404         return FALSE;
405     }
406
407     contextstr = g_ascii_strdown(contextstr, -1);
408     context = frame_context_from_string(contextstr);
409     if (!context) {
410         g_warning("invalid context '%s'", contextstr);
411         g_free(contextstr);
412         return FALSE;
413     }
414     g_free(contextstr);
415
416     for (it = bound_contexts[context]; it != NULL; it = it->next){
417         b = it->data;
418         if (b->state == state && b->button == button) {
419             b->actions[mact] = g_slist_append(b->actions[mact], action);
420             return TRUE;
421         }
422     }
423
424     grab_all_clients(FALSE);
425
426     /* add the binding */
427     b = g_new0(MouseBinding, 1);
428     b->state = state;
429     b->button = button;
430     b->actions[mact] = g_slist_append(NULL, action);
431     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
432
433     grab_all_clients(TRUE);
434
435     return TRUE;
436 }
437
438 void plugin_startup()
439 {
440     dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
441                       Event_X_ButtonPress | Event_X_ButtonRelease |
442                       Event_X_MotionNotify, (EventHandler)event, NULL);
443 }
444
445 void plugin_shutdown()
446 {
447     dispatch_register(0, (EventHandler)event, NULL);
448
449     grab_all_clients(FALSE);
450     clearall();
451 }