]> icculus.org git repositories - dana/openbox.git/blob - plugins/mouse/mouse.c
move the move/resize functionality into moveresize.c, for use with the netwm atoms...
[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/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                 if (b->action[j] != NULL)
101                     action_free(b->action[j]);
102             g_free(b);
103         }
104         g_slist_free(bound_contexts[i]);
105     }
106 }
107
108 static void fire_button(MouseAction a, Context context, Client *c, guint state,
109                         guint button, int x, int y)
110 {
111     GSList *it;
112     MouseBinding *b;
113
114     for (it = bound_contexts[context]; it != NULL; it = it->next) {
115         b = it->data;
116         if (b->state == state && b->button == button)
117             break;
118     }
119     /* if not bound, then nothing to do! */
120     if (it == NULL) return;
121
122     if (b->action[a] != NULL && b->action[a]->func != NULL) {
123         b->action[a]->data.any.c = c;
124
125         g_assert(b->action[a]->func != action_moveresize);
126
127         if (b->action[a]->func == action_showmenu) {
128             b->action[a]->data.showmenu.x = x;
129             b->action[a]->data.showmenu.y = y;
130         }
131
132         b->action[a]->func(&b->action[a]->data);
133     }
134 }
135
136 static void fire_motion(MouseAction a, Context context, Client *c,
137                         guint state, guint button, int x_root, int y_root,
138                         guint32 corner)
139 {
140     GSList *it;
141     MouseBinding *b;
142
143     for (it = bound_contexts[context]; it != NULL; it = it->next) {
144         b = it->data;
145         if (b->state == state && b->button == button)
146                 break;
147     }
148     /* if not bound, then nothing to do! */
149     if (it == NULL) return;
150
151     if (b->action[a] != NULL && b->action[a]->func != NULL) {
152         b->action[a]->data.any.c = c;
153
154         if (b->action[a]->func == action_moveresize) {
155             b->action[a]->data.moveresize.x = x_root;
156             b->action[a]->data.moveresize.y = y_root;
157             b->action[a]->data.moveresize.button = button;
158             if (!(b->action[a]->data.moveresize.corner ==
159                   prop_atoms.net_wm_moveresize_move ||
160                   b->action[a]->data.moveresize.corner ==
161                   prop_atoms.net_wm_moveresize_move_keyboard ||
162                   b->action[a]->data.moveresize.corner ==
163                   prop_atoms.net_wm_moveresize_size_keyboard))
164                 b->action[a]->data.moveresize.corner = corner;
165         } else
166             g_assert_not_reached();
167
168         b->action[a]->func(&b->action[a]->data);
169     }
170 }
171
172 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
173 {
174     if (x - cx < cw / 2) {
175         if (y - cy < ch / 2)
176             return prop_atoms.net_wm_moveresize_size_topleft;
177         else
178             return prop_atoms.net_wm_moveresize_size_bottomleft;
179     } else {
180         if (y - cy < ch / 2)
181             return prop_atoms.net_wm_moveresize_size_topright;
182         else
183             return prop_atoms.net_wm_moveresize_size_bottomright;
184     }
185 }
186
187 static void event(ObEvent *e, void *foo)
188 {
189     static Time ltime;
190     static guint button = 0, state = 0, lbutton = 0;
191     static int px, py;
192     gboolean click = FALSE;
193     gboolean dclick = FALSE;
194     Context context;
195     
196     switch (e->type) {
197     case Event_Client_Mapped:
198         grab_for_client(e->data.c.client, TRUE);
199         break;
200
201     case Event_Client_Destroy:
202         grab_for_client(e->data.c.client, FALSE);
203         break;
204
205     case Event_X_ButtonPress:
206         context = frame_context(e->data.x.client,
207                                 e->data.x.e->xbutton.window);
208
209         if (!button) {
210             px = e->data.x.e->xbutton.x_root;
211             py = e->data.x.e->xbutton.y_root;
212             button = e->data.x.e->xbutton.button;
213             state = e->data.x.e->xbutton.state;
214         }
215
216         fire_button(MouseAction_Press, context,
217                     e->data.x.client, e->data.x.e->xbutton.state,
218                     e->data.x.e->xbutton.button,
219                     e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
220
221         if (context == Context_Client) {
222             /* Replay the event, so it goes to the client*/
223             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
224             /* Fall through to the release case! */
225         } else
226             break;
227
228     case Event_X_ButtonRelease:
229         context = frame_context(e->data.x.client,
230                                 e->data.x.e->xbutton.window);
231         if (e->data.x.e->xbutton.button == button) {
232             /* clicks are only valid if its released over the window */
233             int junk;
234             Window wjunk;
235             guint ujunk, b, w, h;
236             XGetGeometry(ob_display, e->data.x.e->xbutton.window,
237                          &wjunk, &junk, &junk, &w, &h, &b, &ujunk);
238             if (e->data.x.e->xbutton.x >= (signed)-b &&
239                 e->data.x.e->xbutton.y >= (signed)-b &&
240                 e->data.x.e->xbutton.x < (signed)(w+b) &&
241                 e->data.x.e->xbutton.y < (signed)(h+b)) {
242                 click = TRUE;
243                 /* double clicks happen if there were 2 in a row! */
244                 if (lbutton == button &&
245                     e->data.x.e->xbutton.time - dclicktime <= ltime) {
246                     dclick = TRUE;
247                     lbutton = 0;
248                 } else
249                     lbutton = button;
250             } else
251                 lbutton = 0;
252
253             button = 0;
254             state = 0;
255             ltime = e->data.x.e->xbutton.time;
256         }
257         fire_button(MouseAction_Release, context,
258                     e->data.x.client, e->data.x.e->xbutton.state,
259                     e->data.x.e->xbutton.button,
260                     e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
261         if (click)
262             fire_button(MouseAction_Click, context,
263                         e->data.x.client, e->data.x.e->xbutton.state,
264                         e->data.x.e->xbutton.button,
265                         e->data.x.e->xbutton.x_root,
266                         e->data.x.e->xbutton.y_root);
267         if (dclick)
268             fire_button(MouseAction_DClick, context,
269                         e->data.x.client, e->data.x.e->xbutton.state,
270                         e->data.x.e->xbutton.button,
271                         e->data.x.e->xbutton.x_root,
272                         e->data.x.e->xbutton.y_root);
273         break;
274
275     case Event_X_MotionNotify:
276         if (button) {
277             if (ABS(e->data.x.e->xmotion.x_root - px) >= threshold ||
278                 ABS(e->data.x.e->xmotion.y_root - py) >= threshold) {
279                 guint32 corner = 
280                     pick_corner(e->data.x.e->xmotion.x_root,
281                                 e->data.x.e->xmotion.y_root,
282                                 e->data.x.client->frame->area.x,
283                                 e->data.x.client->frame->area.y,
284                                 /* use the client size because the frame can be
285                                    differently sized (shaded windows) and we
286                                    want this based on the clients size */
287                                 e->data.x.client->area.width +
288                                 e->data.x.client->frame->size.left +
289                                 e->data.x.client->frame->size.right,
290                                 e->data.x.client->area.height +
291                                 e->data.x.client->frame->size.top +
292                                 e->data.x.client->frame->size.bottom);
293                 context = frame_context(e->data.x.client,
294                                         e->data.x.e->xmotion.window);
295                 fire_motion(MouseAction_Motion, context,
296                             e->data.x.client, state, button,
297                             e->data.x.e->xmotion.x_root, 
298                             e->data.x.e->xmotion.y_root, corner);
299                 button = 0;
300                 state = 0;
301             }
302         }
303         break;
304
305     default:
306         g_assert_not_reached();
307     }
308 }
309
310 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
311                Action *action)
312 {
313     guint state, button;
314     Context context;
315     MouseBinding *b;
316     GSList *it;
317
318     if (!translate_button(buttonstr, &state, &button)) {
319         g_warning("invalid button '%s'", buttonstr);
320         return FALSE;
321     }
322
323     contextstr = g_ascii_strdown(contextstr, -1);
324     context = frame_context_from_string(contextstr);
325     if (!context) {
326         g_warning("invalid context '%s'", contextstr);
327         g_free(contextstr);
328         return FALSE;
329     }
330     g_free(contextstr);
331
332     for (it = bound_contexts[context]; it != NULL; it = it->next){
333         b = it->data;
334         if (b->state == state && b->button == button) {
335             /* already bound */
336             if (b->action[mact] != NULL) {
337                 g_warning("duplicate binding");
338                 return FALSE;
339             }
340             b->action[mact] = action;
341             return TRUE;
342         }
343     }
344
345     grab_all_clients(FALSE);
346
347     /* add the binding */
348     b = g_new0(MouseBinding, 1);
349     b->state = state;
350     b->button = button;
351     b->action[mact] = action;
352     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
353
354     grab_all_clients(TRUE);
355
356     return TRUE;
357 }
358
359 void plugin_startup()
360 {
361     dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
362                       Event_X_ButtonPress | Event_X_ButtonRelease |
363                       Event_X_MotionNotify, (EventHandler)event, NULL);
364 }
365
366 void plugin_shutdown()
367 {
368     dispatch_register(0, (EventHandler)event, NULL);
369
370     grab_all_clients(FALSE);
371     clearall();
372 }