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