stop using build/ dir configure shits all over itself for other people
[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/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         } else
182             g_assert_not_reached();
183         b->action[a]->func(&b->action[a]->data);
184         return TRUE;
185     }
186     return FALSE;
187 }
188
189 static Corner pick_corner(int x, int y, int cx, int cy, int cw, int ch)
190 {
191     if (x - cx < cw / 2) {
192         if (y - cy < ch / 2)
193             return Corner_BottomRight;
194         else
195             return Corner_TopRight;
196     } else {
197         if (y - cy < ch / 2)
198             return Corner_BottomLeft;
199         else
200             return Corner_TopLeft;
201     }
202 }
203
204 static void event(ObEvent *e, void *foo)
205 {
206     static Time ltime;
207     static int px, py, cx, cy, cw, ch, dx, dy;
208     static guint button = 0, state = 0, lbutton = 0;
209     static gboolean drag = FALSE, drag_used = FALSE;
210     static Corner corner = Corner_TopLeft;
211     gboolean click = FALSE;
212     gboolean dclick = FALSE;
213     Context context;
214     
215     switch (e->type) {
216     case Event_Client_Mapped:
217         grab_for_client(e->data.c.client, TRUE);
218         break;
219
220     case Event_Client_Destroy:
221         grab_for_client(e->data.c.client, FALSE);
222         break;
223
224     case Event_X_ButtonPress:
225         if (!button) {
226             if (e->data.x.client != NULL) {
227                 cx = e->data.x.client->frame->area.x;
228                 cy = e->data.x.client->frame->area.y;
229                 /* use the client size because the frame can be differently
230                    sized (shaded windows) and we want this based on the clients
231                    size */
232                 cw = e->data.x.client->area.width + 
233                     e->data.x.client->frame->size.left +
234                     e->data.x.client->frame->size.right;
235                 ch = e->data.x.client->area.height +
236                     e->data.x.client->frame->size.top +
237                     e->data.x.client->frame->size.bottom;
238                 px = e->data.x.e->xbutton.x_root;
239                 py = e->data.x.e->xbutton.y_root;
240                 corner = pick_corner(px, py, cx, cy, cw, ch);
241             }
242             button = e->data.x.e->xbutton.button;
243             state = e->data.x.e->xbutton.state;
244         }
245         context = engine_get_context(e->data.x.client,
246                                      e->data.x.e->xbutton.window);
247
248         fire_button(MouseAction_Press, context,
249                     e->data.x.client, e->data.x.e->xbutton.state,
250                     e->data.x.e->xbutton.button);
251
252         if (context == Context_Client) {
253             /* Replay the event, so it goes to the client*/
254             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
255             /* Fall through to the release case! */
256         } else
257             break;
258
259     case Event_X_ButtonRelease:
260         context = engine_get_context(e->data.x.client,
261                                      e->data.x.e->xbutton.window);
262         if (e->data.x.e->xbutton.button == button) {
263             /* end drags */
264             if (drag_used) {
265                 fire_motion(MouseAction_Motion, context,
266                             e->data.x.client, state, button,
267                             cx, cy, cw, ch, dx, dy, TRUE, corner);
268                 drag = drag_used = FALSE;
269                 
270                 lbutton = 0;
271             } else {
272                 /* clicks are only valid if its released over the window */
273                 int junk;
274                 Window wjunk;
275                 guint ujunk, b, w, h;
276                 XGetGeometry(ob_display, e->data.x.e->xbutton.window,
277                              &wjunk, &junk, &junk, &w, &h, &b, &ujunk);
278                 if (e->data.x.e->xbutton.x >= (signed)-b &&
279                     e->data.x.e->xbutton.y >= (signed)-b &&
280                     e->data.x.e->xbutton.x < (signed)(w+b) &&
281                     e->data.x.e->xbutton.y < (signed)(h+b)) {
282                     click = TRUE;
283                     /* double clicks happen if there were 2 in a row! */
284                     if (lbutton == button &&
285                         e->data.x.e->xbutton.time - dclicktime <= ltime) {
286                         dclick = TRUE;
287                         lbutton = 0;
288                     } else
289                         lbutton = button;
290                 } else
291                     lbutton = 0;
292             }
293
294             button = 0;
295             state = 0;
296             ltime = e->data.x.e->xbutton.time;
297         }
298         fire_button(MouseAction_Release, context,
299                     e->data.x.client, e->data.x.e->xbutton.state,
300                     e->data.x.e->xbutton.button);
301         if (click)
302             fire_button(MouseAction_Click, context,
303                         e->data.x.client, e->data.x.e->xbutton.state,
304                         e->data.x.e->xbutton.button);
305         if (dclick)
306             fire_button(MouseAction_DClick, context,
307                         e->data.x.client, e->data.x.e->xbutton.state,
308                         e->data.x.e->xbutton.button);
309         break;
310
311     case Event_X_MotionNotify:
312         if (button) {
313             dx = e->data.x.e->xmotion.x_root - px;
314             dy = e->data.x.e->xmotion.y_root - py;
315             if (!drag &&
316                 (ABS(dx) >= threshold || ABS(dy) >= threshold))
317                 drag = TRUE;
318             if (drag) {
319                 context = engine_get_context(e->data.x.client,
320                                              e->data.x.e->xbutton.window);
321                 drag_used = fire_motion(MouseAction_Motion, context,
322                                         e->data.x.client,
323                                         state, button, cx, cy, cw, ch, dx, dy,
324                                         FALSE, corner);
325             }
326         }
327         break;
328
329     default:
330         g_assert_not_reached();
331     }
332 }
333
334 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
335                Action *action)
336 {
337     guint state, button;
338     Context context;
339     MouseBinding *b;
340     GSList *it;
341
342     if (!translate_button(buttonstr, &state, &button)) {
343         g_warning("invalid button '%s'", buttonstr);
344         return FALSE;
345     }
346
347     contextstr = g_ascii_strdown(contextstr, -1);
348     context = frame_context_from_string(contextstr);
349     if (!context) {
350         g_warning("invalid context '%s'", contextstr);
351         g_free(contextstr);
352         return FALSE;
353     }
354     g_free(contextstr);
355
356     for (it = bound_contexts[context]; it != NULL; it = it->next){
357         b = it->data;
358         if (b->state == state && b->button == button) {
359             /* already bound */
360             if (b->action[mact] != NULL) {
361                 g_warning("duplicate binding");
362                 return FALSE;
363             }
364             b->action[mact] = action;
365             return TRUE;
366         }
367     }
368
369     grab_all_clients(FALSE);
370
371     /* add the binding */
372     b = g_new0(MouseBinding, 1);
373     b->state = state;
374     b->button = button;
375     b->action[mact] = action;
376     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
377
378     grab_all_clients(TRUE);
379
380     return TRUE;
381 }
382
383 void plugin_startup()
384 {
385     dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
386                       Event_X_ButtonPress | Event_X_ButtonRelease |
387                       Event_X_MotionNotify, (EventHandler)event, NULL);
388 }
389
390 void plugin_shutdown()
391 {
392     dispatch_register(0, (EventHandler)event, NULL);
393
394     grab_all_clients(FALSE);
395     clearall();
396 }