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