]> icculus.org git repositories - dana/openbox.git/blob - openbox/mouse.c
dont grab frame context mouse bindings on desktop windows
[dana/openbox.git] / openbox / mouse.c
1 #include "openbox.h"
2 #include "config.h"
3 #include "xerror.h"
4 #include "action.h"
5 #include "event.h"
6 #include "client.h"
7 #include "prop.h"
8 #include "grab.h"
9 #include "frame.h"
10 #include "translate.h"
11 #include "mouse.h"
12 #include "keyboard.h"
13 #include <glib.h>
14
15 typedef struct {
16     guint state;
17     guint button;
18     GSList *actions[OB_MOUSE_NUM_ACTIONS]; /* lists of Action pointers */
19 } ObMouseBinding;
20
21 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
22                                co == OB_FRAME_CONTEXT_FRAME : FALSE)
23 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
24                                 co == OB_FRAME_CONTEXT_DESKTOP : \
25                                 co == OB_FRAME_CONTEXT_CLIENT)
26
27 /* Array of GSList*s of PointerBinding*s. */
28 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
29
30 void mouse_grab_for_client(ObClient *client, gboolean grab)
31 {
32     int i;
33     GSList *it;
34
35     for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
36         for (it = bound_contexts[i]; it != NULL; it = it->next) {
37             /* grab/ungrab the button */
38             ObMouseBinding *b = it->data;
39             Window win;
40             int mode;
41             unsigned int mask;
42
43             if (FRAME_CONTEXT(i, client)) {
44                 win = client->frame->window;
45                 mode = GrabModeAsync;
46                 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
47             } else if (CLIENT_CONTEXT(i, client)) {
48                 win = client->frame->plate;
49                 mode = GrabModeSync; /* this is handled in event */
50                 mask = ButtonPressMask; /* can't catch more than this with Sync
51                                            mode the release event is
52                                            manufactured in event() */
53             } else continue;
54
55             if (grab)
56                 grab_button_full(b->button, b->state, win, mask, mode, None);
57             else
58                 ungrab_button(b->button, b->state, win);
59         }
60 }
61
62 static void grab_all_clients(gboolean grab)
63 {
64     GList *it;
65
66     for (it = client_list; it != NULL; it = it->next)
67         mouse_grab_for_client(it->data, grab);
68 }
69
70 static void clearall()
71 {
72     int i;
73     GSList *it;
74     
75     for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
76         for (it = bound_contexts[i]; it != NULL; it = it->next) {
77             int j;
78
79             ObMouseBinding *b = it->data;
80             for (j = 0; j < OB_MOUSE_NUM_ACTIONS; ++j) {
81                 GSList *it;
82                 for (it = b->actions[j]; it; it = it->next) {
83                     action_free(it->data);
84                 }
85                 g_slist_free(b->actions[j]);
86             }
87             g_free(b);
88         }
89         g_slist_free(bound_contexts[i]);
90     }
91 }
92
93 static gboolean fire_button(ObMouseAction a, ObFrameContext context,
94                             ObClient *c, guint state,
95                             guint button, int x, int y)
96 {
97     GSList *it;
98     ObMouseBinding *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 FALSE;
107
108     for (it = b->actions[a]; it; it = it->next) {
109         ObAction *act = it->data;
110         if (act->func != NULL) {
111             act->data.any.c = c;
112
113             g_assert(act->func != action_moveresize);
114
115             if (act->func == action_showmenu) {
116                 act->data.showmenu.x = x;
117                 act->data.showmenu.y = y;
118             }
119
120             if (act->data.any.interactive) {
121                 act->data.inter.cancel = FALSE;
122                 act->data.inter.final = FALSE;
123                 keyboard_interactive_grab(state, c, context, act);
124             }
125
126             act->func(&act->data);
127         }
128     }
129     return TRUE;
130 }
131
132 static gboolean fire_motion(ObMouseAction a, ObFrameContext context,
133                             ObClient *c, guint state, guint button,
134                             int x_root, int y_root, guint32 corner)
135 {
136     GSList *it;
137     ObMouseBinding *b;
138
139     for (it = bound_contexts[context]; it != NULL; it = it->next) {
140         b = it->data;
141         if (b->state == state && b->button == button)
142                 break;
143     }
144     /* if not bound, then nothing to do! */
145     if (it == NULL) return FALSE;
146
147     for (it = b->actions[a]; it; it = it->next) {
148         ObAction *act = it->data;
149         if (act->func != NULL) {
150             act->data.any.c = c;
151
152             if (act->func == action_moveresize) {
153                 act->data.moveresize.x = x_root;
154                 act->data.moveresize.y = y_root;
155                 act->data.moveresize.button = button;
156                 if (!(act->data.moveresize.corner ==
157                       prop_atoms.net_wm_moveresize_move ||
158                       act->data.moveresize.corner ==
159                       prop_atoms.net_wm_moveresize_move_keyboard ||
160                       act->data.moveresize.corner ==
161                       prop_atoms.net_wm_moveresize_size_keyboard))
162                     act->data.moveresize.corner = corner;
163             } else
164                 g_assert_not_reached();
165
166             act->func(&act->data);
167         }
168     }
169     return TRUE;
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 void mouse_event(ObClient *client, ObFrameContext context, XEvent *e)
188 {
189     static Time ltime;
190     static guint button = 0, state = 0, lbutton = 0;
191
192     static Window lwindow = None;
193     static int px, py;
194     gboolean click = FALSE;
195     gboolean dclick = FALSE;
196
197     switch (e->type) {
198     case ButtonPress:
199         px = e->xbutton.x_root;
200         py = e->xbutton.y_root;
201         button = e->xbutton.button;
202         state = e->xbutton.state;
203
204         fire_button(OB_MOUSE_ACTION_PRESS, context,
205                     client, e->xbutton.state,
206                     e->xbutton.button,
207                     e->xbutton.x_root, e->xbutton.y_root);
208
209         if (CLIENT_CONTEXT(context, client)) {
210             /* Replay the event, so it goes to the client*/
211             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
212             /* Fall through to the release case! */
213         } else
214             break;
215
216     case ButtonRelease:
217         if (e->xbutton.button == button) {
218             /* clicks are only valid if its released over the window */
219             int junk1, junk2;
220             Window wjunk;
221             guint ujunk, b, w, h;
222             xerror_set_ignore(TRUE);
223             junk1 = XGetGeometry(ob_display, e->xbutton.window,
224                                  &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
225             xerror_set_ignore(FALSE);
226             if (junk1) {
227                 if (e->xbutton.x >= (signed)-b &&
228                     e->xbutton.y >= (signed)-b &&
229                     e->xbutton.x < (signed)(w+b) &&
230                     e->xbutton.y < (signed)(h+b)) {
231                     click = TRUE;
232                     /* double clicks happen if there were 2 in a row! */
233                     if (lbutton == button &&
234                         lwindow == e->xbutton.window &&
235                         e->xbutton.time - config_mouse_dclicktime <=
236                         ltime) {
237                         dclick = TRUE;
238                         lbutton = 0;
239                     } else {
240                         lbutton = button;
241                         lwindow = e->xbutton.window;
242                     }
243                 } else {
244                     lbutton = 0;
245                     lwindow = None;
246                 }
247             }
248
249             button = 0;
250             state = 0;
251             ltime = e->xbutton.time;
252         }
253         fire_button(OB_MOUSE_ACTION_RELEASE, context,
254                     client, e->xbutton.state,
255                     e->xbutton.button,
256                     e->xbutton.x_root, e->xbutton.y_root);
257         if (click)
258             fire_button(OB_MOUSE_ACTION_CLICK, context,
259                         client, e->xbutton.state,
260                         e->xbutton.button,
261                         e->xbutton.x_root,
262                         e->xbutton.y_root);
263         if (dclick)
264             fire_button(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
265                         client, e->xbutton.state,
266                         e->xbutton.button,
267                         e->xbutton.x_root,
268                         e->xbutton.y_root);
269         break;
270
271     case MotionNotify:
272         if (button) {
273             if (ABS(e->xmotion.x_root - px) >=
274                 config_mouse_threshold ||
275                 ABS(e->xmotion.y_root - py) >=
276                 config_mouse_threshold) {
277                 guint32 corner;
278
279                 /* You can't drag on buttons */
280                 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
281                     context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
282                     context == OB_FRAME_CONTEXT_SHADE ||
283                     context == OB_FRAME_CONTEXT_ICONIFY ||
284                     context == OB_FRAME_CONTEXT_ICON ||
285                     context == OB_FRAME_CONTEXT_CLOSE)
286                     break;
287
288                 if (!client)
289                     corner = prop_atoms.net_wm_moveresize_size_bottomright;
290                 else
291                     corner =
292                         pick_corner(e->xmotion.x_root,
293                                     e->xmotion.y_root,
294                                     client->frame->area.x,
295                                     client->frame->area.y,
296                                     /* use the client size because the frame
297                                        can be differently sized (shaded
298                                        windows) and we want this based on the
299                                        clients size */
300                                     client->area.width +
301                                     client->frame->size.left +
302                                     client->frame->size.right,
303                                     client->area.height +
304                                     client->frame->size.top +
305                                     client->frame->size.bottom);
306                 fire_motion(OB_MOUSE_ACTION_MOTION, context,
307                             client, state, button, px, py, corner);
308                 button = 0;
309                 state = 0;
310             }
311         }
312         break;
313
314     default:
315         g_assert_not_reached();
316     }
317 }
318
319 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
320                     ObAction *action)
321 {
322     guint state, button;
323     ObFrameContext context;
324     ObMouseBinding *b;
325     GSList *it;
326
327     if (!translate_button(buttonstr, &state, &button)) {
328         g_warning("invalid button '%s'", buttonstr);
329         return FALSE;
330     }
331
332     contextstr = g_ascii_strdown(contextstr, -1);
333     context = frame_context_from_string(contextstr);
334     if (!context) {
335         g_warning("invalid context '%s'", contextstr);
336         g_free(contextstr);
337         return FALSE;
338     }
339     g_free(contextstr);
340
341     for (it = bound_contexts[context]; it != NULL; it = it->next){
342         b = it->data;
343         if (b->state == state && b->button == button) {
344             b->actions[mact] = g_slist_append(b->actions[mact], action);
345             return TRUE;
346         }
347     }
348
349     grab_all_clients(FALSE);
350
351     /* when there are no modifiers in the binding, then the action cannot
352        be interactive */
353     if (!state && action->data.any.interactive) {
354         action->data.any.interactive = FALSE;
355         action->data.inter.final = TRUE;
356     }
357
358     /* add the binding */
359     b = g_new0(ObMouseBinding, 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 mouse_startup()
371 {
372 }
373
374 void mouse_shutdown()
375 {
376     grab_all_clients(FALSE);
377     clearall();
378 }