]> icculus.org git repositories - mikachu/openbox.git/blob - plugins/mouse/mouse.c
change how rc parsing will work. a=b will be parsed in any [section] and given to...
[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/frame.h"
7 #include "kernel/grab.h"
8 #include "kernel/engine.h"
9 #include "kernel/parse.h"
10 #include "kernel/frame.h"
11 #include "translate.h"
12 #include "mouse.h"
13 #include "mouseparse.h"
14 #include <glib.h>
15
16 static int threshold;
17 static int dclicktime;
18
19 static void parse_assign(char *name, ParseToken *value)
20 {
21     if (!g_ascii_strcasecmp(name, "dragthreshold")) {
22         if (value->type != TOKEN_INTEGER)
23             yyerror("invalid value");
24         else {
25             if (value->data.integer >= 0)
26                 threshold = value->data.integer;
27         }
28     } else if (!g_ascii_strcasecmp(name, "doubleclicktime")) {
29         if (value->type != TOKEN_INTEGER)
30             yyerror("invalid value");
31         else {
32             if (value->data.integer >= 0)
33                 dclicktime = value->data.integer;
34         }
35     } else
36         yyerror("invalid option");
37     parse_free_token(value);
38 }
39
40 void plugin_setup_config()
41 {
42     threshold = 3;
43     dclicktime = 200;
44     parse_reg_section("mouse", mouseparse, parse_assign);
45 }
46
47 /* Array of GSList*s of PointerBinding*s. */
48 static GSList *bound_contexts[NUM_CONTEXTS];
49
50 static void grab_for_client(Client *client, gboolean grab)
51 {
52     int i;
53     GSList *it;
54
55     for (i = 0; i < NUM_CONTEXTS; ++i)
56         for (it = bound_contexts[i]; it != NULL; it = it->next) {
57             /* grab/ungrab the button */
58             MouseBinding *b = it->data;
59             Window win;
60             int mode;
61             unsigned int mask;
62
63             if (i == Context_Frame) {
64                 win = client->frame->window;
65                 mode = GrabModeAsync;
66                 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
67             } else if (i == Context_Client) {
68                 win = client->frame->plate;
69                 mode = GrabModeSync; /* this is handled in event */
70                 mask = ButtonPressMask; /* can't catch more than this with Sync
71                                            mode the release event is
72                                            manufactured in event() */
73             } else continue;
74
75             if (grab)
76                 grab_button(b->button, b->state, win, mask, mode);
77             else
78                 ungrab_button(b->button, b->state, win);
79         }
80 }
81
82 static void grab_all_clients(gboolean grab)
83 {
84     GList *it;
85
86     for (it = client_list; it != NULL; it = it->next)
87         grab_for_client(it->data, grab);
88 }
89
90 static void clearall()
91 {
92     int i;
93     GSList *it;
94     
95     for(i = 0; i < NUM_CONTEXTS; ++i) {
96         for (it = bound_contexts[i]; it != NULL; it = it->next) {
97             int j;
98
99             MouseBinding *b = it->data;
100             for (j = 0; j < NUM_MOUSEACTION; ++j)
101                 if (b->action[j] != NULL)
102                     action_free(b->action[j]);
103             g_free(b);
104         }
105         g_slist_free(bound_contexts[i]);
106     }
107 }
108
109 static void fire_button(MouseAction a, Context context, Client *c, guint state,
110                         guint button)
111 {
112     GSList *it;
113     MouseBinding *b;
114
115     for (it = bound_contexts[context]; it != NULL; it = it->next) {
116         b = it->data;
117         if (b->state == state && b->button == button)
118             break;
119     }
120     /* if not bound, then nothing to do! */
121     if (it == NULL) return;
122
123     if (b->action[a] != NULL && b->action[a]->func != NULL) {
124         b->action[a]->data.any.c = c;
125
126         g_assert(!(b->action[a]->func == action_move ||
127                    b->action[a]->func == action_resize));
128
129         b->action[a]->func(&b->action[a]->data);
130     }
131 }
132
133 /* corner should be the opposite corner of the window in which the pointer
134    clicked, Corner_TopLeft if a good default if there is no client 
135    Returns True or False for if a binding existed for the action or not.
136 */
137 static gboolean fire_motion(MouseAction a, Context context, Client *c,
138                             guint state, guint button, int cx, int cy,
139                             int cw, int ch, int dx, int dy,
140                             gboolean final, Corner corner)
141 {
142     GSList *it;
143     MouseBinding *b;
144
145     for (it = bound_contexts[context]; it != NULL; it = it->next) {
146         b = it->data;
147         if (b->state == state && b->button == button)
148                 break;
149     }
150     /* if not bound, then nothing to do! */
151     if (it == NULL) return FALSE;
152
153     if (b->action[a] != NULL && b->action[a]->func != NULL) {
154         b->action[a]->data.any.c = c;
155
156         if (b->action[a]->func == action_move) {
157             b->action[a]->data.move.x = cx + dx;
158             b->action[a]->data.move.y = cy + dy;
159             b->action[a]->data.move.final = final;
160         } else if (b->action[a]->func == action_resize) {
161             b->action[a]->data.resize.corner = corner;
162             switch (corner) {
163             case Corner_TopLeft:
164                 b->action[a]->data.resize.x = cw + dx;
165                 b->action[a]->data.resize.y = ch + dy;
166                 break;
167             case Corner_TopRight:
168                 b->action[a]->data.resize.x = cw - dx;
169                 b->action[a]->data.resize.y = ch + dy;
170                 break;
171             case Corner_BottomLeft:
172                 b->action[a]->data.resize.x = cw + dx;
173                 b->action[a]->data.resize.y = ch - dy;
174                 break;
175             case Corner_BottomRight:
176                 b->action[a]->data.resize.x = cw - dx;
177                 b->action[a]->data.resize.y = ch - dy;
178                 break;
179             }
180             b->action[a]->data.resize.final = final;
181         }
182         b->action[a]->func(&b->action[a]->data);
183         return TRUE;
184     }
185     return FALSE;
186 }
187
188 static Corner pick_corner(int x, int y, int cx, int cy, int cw, int ch)
189 {
190     if (x - cx < cw / 2) {
191         if (y - cy < ch / 2)
192             return Corner_BottomRight;
193         else
194             return Corner_TopRight;
195     } else {
196         if (y - cy < ch / 2)
197             return Corner_BottomLeft;
198         else
199             return Corner_TopLeft;
200     }
201 }
202
203 static void event(ObEvent *e, void *foo)
204 {
205     static Time ltime;
206     static int px, py, cx, cy, cw, ch, dx, dy;
207     static guint button = 0, lbutton = 0;
208     static gboolean drag = FALSE, drag_used = FALSE;
209     static Corner corner = Corner_TopLeft;
210     gboolean click = FALSE;
211     gboolean dclick = FALSE;
212     Context context;
213     
214     switch (e->type) {
215     case Event_Client_Mapped:
216         grab_for_client(e->data.c.client, TRUE);
217         break;
218
219     case Event_Client_Destroy:
220         grab_for_client(e->data.c.client, FALSE);
221         break;
222
223     case Event_X_ButtonPress:
224         if (!button) {
225             if (e->data.x.client != NULL) {
226                 cx = e->data.x.client->frame->area.x;
227                 cy = e->data.x.client->frame->area.y;
228                 /* use the client size because the frame can be differently
229                    sized (shaded windows) and we want this based on the clients
230                    size */
231                 cw = e->data.x.client->area.width + 
232                     e->data.x.client->frame->size.left +
233                     e->data.x.client->frame->size.right;
234                 ch = e->data.x.client->area.height +
235                     e->data.x.client->frame->size.top +
236                     e->data.x.client->frame->size.bottom;
237                 px = e->data.x.e->xbutton.x_root;
238                 py = e->data.x.e->xbutton.y_root;
239                 corner = pick_corner(px, py, cx, cy, cw, ch);
240             }
241             button = e->data.x.e->xbutton.button;
242         }
243         context = engine_get_context(e->data.x.client,
244                                      e->data.x.e->xbutton.window);
245
246         fire_button(MouseAction_Press, context,
247                     e->data.x.client, e->data.x.e->xbutton.state,
248                     e->data.x.e->xbutton.button);
249
250         if (context == Context_Client) {
251             /* Replay the event, so it goes to the client*/
252             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
253             /* Fall through to the release case! */
254         } else
255             break;
256
257     case Event_X_ButtonRelease:
258         context = engine_get_context(e->data.x.client,
259                                      e->data.x.e->xbutton.window);
260         if (e->data.x.e->xbutton.button == button) {
261             /* end drags */
262             if (drag_used) {
263                 fire_motion(MouseAction_Motion, context,
264                             e->data.x.client, e->data.x.e->xbutton.state,
265                             e->data.x.e->xbutton.button,
266                             cx, cy, cw, ch, dx, dy, TRUE, corner);
267                 drag = drag_used = FALSE;
268                 
269                 lbutton = 0;
270             } else {
271                 /* clicks are only valid if its released over the window */
272                 int junk;
273                 Window wjunk;
274                 guint ujunk, b, w, h;
275                 XGetGeometry(ob_display, e->data.x.e->xbutton.window,
276                              &wjunk, &junk, &junk, &w, &h, &b, &ujunk);
277                 if (e->data.x.e->xbutton.x >= (signed)-b &&
278                     e->data.x.e->xbutton.y >= (signed)-b &&
279                     e->data.x.e->xbutton.x < (signed)(w+b) &&
280                     e->data.x.e->xbutton.y < (signed)(h+b)) {
281                     click = TRUE;
282                     /* double clicks happen if there were 2 in a row! */
283                     if (lbutton == button &&
284                         e->data.x.e->xbutton.time - dclicktime <= ltime) {
285                         dclick = TRUE;
286                         lbutton = 0;
287                     } else
288                         lbutton = button;
289                 } else
290                     lbutton = 0;
291             }
292
293             button = 0;
294             ltime = e->data.x.e->xbutton.time;
295         }
296         fire_button(MouseAction_Release, context,
297                     e->data.x.client, e->data.x.e->xbutton.state,
298                     e->data.x.e->xbutton.button);
299         if (click)
300             fire_button(MouseAction_Click, context,
301                         e->data.x.client, e->data.x.e->xbutton.state,
302                         e->data.x.e->xbutton.button);
303         if (dclick)
304             fire_button(MouseAction_DClick, context,
305                         e->data.x.client, e->data.x.e->xbutton.state,
306                         e->data.x.e->xbutton.button);
307         break;
308
309     case Event_X_MotionNotify:
310         if (button) {
311             dx = e->data.x.e->xmotion.x_root - px;
312             dy = e->data.x.e->xmotion.y_root - py;
313             if (!drag &&
314                 (ABS(dx) >= threshold || ABS(dy) >= threshold))
315                 drag = TRUE;
316             if (drag) {
317                 context = engine_get_context(e->data.x.client,
318                                              e->data.x.e->xbutton.window);
319                 drag_used = fire_motion(MouseAction_Motion, context,
320                                         e->data.x.client,
321                                         e->data.x.e->xmotion.state,
322                                         button, cx, cy, cw, ch, dx, dy,
323                                         FALSE, corner);
324             }
325         }
326         break;
327
328     default:
329         g_assert_not_reached();
330     }
331 }
332
333 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
334                Action *action)
335 {
336     guint state, button;
337     Context context;
338     MouseBinding *b;
339     GSList *it;
340
341     if (!translate_button(buttonstr, &state, &button)) {
342         g_warning("invalid button '%s'", buttonstr);
343         return FALSE;
344     }
345
346     contextstr = g_ascii_strdown(contextstr, -1);
347     context = frame_context_from_string(contextstr);
348     if (!context) {
349         g_warning("invalid context '%s'", contextstr);
350         g_free(contextstr);
351         return FALSE;
352     }
353     g_free(contextstr);
354
355     for (it = bound_contexts[context]; it != NULL; it = it->next){
356         b = it->data;
357         if (b->state == state && b->button == button) {
358             /* already bound */
359             if (b->action[mact] != NULL) {
360                 g_warning("duplicate binding");
361                 return FALSE;
362             }
363             b->action[mact] = action;
364             return TRUE;
365         }
366     }
367
368     grab_all_clients(FALSE);
369
370     /* add the binding */
371     b = g_new0(MouseBinding, 1);
372     b->state = state;
373     b->button = button;
374     b->action[mact] = action;
375     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
376
377     grab_all_clients(TRUE);
378
379     return TRUE;
380 }
381
382 void plugin_startup()
383 {
384     dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
385                       Event_X_ButtonPress | Event_X_ButtonRelease |
386                       Event_X_MotionNotify, (EventHandler)event, NULL);
387 }
388
389 void plugin_shutdown()
390 {
391     dispatch_register(0, (EventHandler)event, NULL);
392
393     grab_all_clients(FALSE);
394     clearall();
395 }