]> icculus.org git repositories - mikachu/openbox.git/blob - plugins/mouse/mouse.c
all my changes while i was offline.
[mikachu/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(b->button, b->state, win, mask, mode);
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                 if (!e->data.x.client)
292                     corner = prop_atoms.net_wm_moveresize_size_bottomright;
293                 else
294                     corner =
295                         pick_corner(e->data.x.e->xmotion.x_root,
296                                     e->data.x.e->xmotion.y_root,
297                                     e->data.x.client->frame->area.x,
298                                     e->data.x.client->frame->area.y,
299                                     /* use the client size because the frame
300                                        can be differently sized (shaded
301                                        windows) and we want this based on the
302                                        clients size */
303                                     e->data.x.client->area.width +
304                                     e->data.x.client->frame->size.left +
305                                     e->data.x.client->frame->size.right,
306                                     e->data.x.client->area.height +
307                                     e->data.x.client->frame->size.top +
308                                     e->data.x.client->frame->size.bottom);
309                 context = frame_context(e->data.x.client,
310                                         e->data.x.e->xmotion.window);
311                 fire_motion(MouseAction_Motion, context,
312                             e->data.x.client, state, button,
313                             e->data.x.e->xmotion.x_root, 
314                             e->data.x.e->xmotion.y_root, corner);
315                 button = 0;
316                 state = 0;
317             }
318         }
319         break;
320
321     default:
322         g_assert_not_reached();
323     }
324 }
325
326 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
327                Action *action)
328 {
329     guint state, button;
330     Context context;
331     MouseBinding *b;
332     GSList *it;
333
334     if (!translate_button(buttonstr, &state, &button)) {
335         g_warning("invalid button '%s'", buttonstr);
336         return FALSE;
337     }
338
339     contextstr = g_ascii_strdown(contextstr, -1);
340     context = frame_context_from_string(contextstr);
341     if (!context) {
342         g_warning("invalid context '%s'", contextstr);
343         g_free(contextstr);
344         return FALSE;
345     }
346     g_free(contextstr);
347
348     for (it = bound_contexts[context]; it != NULL; it = it->next){
349         b = it->data;
350         if (b->state == state && b->button == button) {
351             b->actions[mact] = g_slist_append(b->actions[mact], action);
352             return TRUE;
353         }
354     }
355
356     grab_all_clients(FALSE);
357
358     /* add the binding */
359     b = g_new0(MouseBinding, 1);
360     b->state = state;
361     b->button = button;
362     b->actions[mact] = g_slist_append(NULL, action);
363     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
364
365     grab_all_clients(TRUE);
366
367     return TRUE;
368 }
369
370 void plugin_startup()
371 {
372     dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
373                       Event_X_ButtonPress | Event_X_ButtonRelease |
374                       Event_X_MotionNotify, (EventHandler)event, NULL);
375 }
376
377 void plugin_shutdown()
378 {
379     dispatch_register(0, (EventHandler)event, NULL);
380
381     grab_all_clients(FALSE);
382     clearall();
383 }