add the MoveResize context, which is used while doing a move/resize on a window....
[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 <glib.h>
13
14 typedef struct {
15     guint state;
16     guint button;
17     GSList *actions[OB_NUM_MOUSE_ACTIONS]; /* lists of Action pointers */
18 } ObMouseBinding;
19
20 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
21                                co == OB_FRAME_CONTEXT_FRAME : FALSE)
22 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
23                                 co == OB_FRAME_CONTEXT_DESKTOP : \
24                                 co == OB_FRAME_CONTEXT_CLIENT)
25
26 /* Array of GSList*s of ObMouseBinding*s. */
27 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
28
29 ObFrameContext mouse_button_frame_context(ObFrameContext context,
30                                           guint button)
31 {
32     GSList *it;
33     ObFrameContext x = context;
34
35     for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
36         ObMouseBinding *b = it->data;
37
38         if (b->button == button)
39             return context;
40     }
41
42     switch (context) {
43     case OB_FRAME_CONTEXT_NONE:
44     case OB_FRAME_CONTEXT_DESKTOP:
45     case OB_FRAME_CONTEXT_CLIENT:
46     case OB_FRAME_CONTEXT_TITLEBAR:
47     case OB_FRAME_CONTEXT_HANDLE:
48     case OB_FRAME_CONTEXT_FRAME:
49     case OB_FRAME_CONTEXT_MOVE_RESIZE:
50         break;
51     case OB_FRAME_CONTEXT_BLCORNER:
52     case OB_FRAME_CONTEXT_BRCORNER:
53         x = OB_FRAME_CONTEXT_HANDLE;
54         break;
55     case OB_FRAME_CONTEXT_TLCORNER:
56     case OB_FRAME_CONTEXT_TRCORNER:
57     case OB_FRAME_CONTEXT_MAXIMIZE:
58     case OB_FRAME_CONTEXT_ALLDESKTOPS:
59     case OB_FRAME_CONTEXT_SHADE:
60     case OB_FRAME_CONTEXT_ICONIFY:
61     case OB_FRAME_CONTEXT_ICON:
62     case OB_FRAME_CONTEXT_CLOSE:
63         x = OB_FRAME_CONTEXT_TITLEBAR;
64         break;
65     case OB_FRAME_NUM_CONTEXTS:
66         g_assert_not_reached();
67     }
68
69     return x;
70 }
71
72 void mouse_grab_for_client(ObClient *client, gboolean grab)
73 {
74     int i;
75     GSList *it;
76
77     for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
78         for (it = bound_contexts[i]; it != NULL; it = g_slist_next(it)) {
79             /* grab/ungrab the button */
80             ObMouseBinding *b = it->data;
81             Window win;
82             int mode;
83             unsigned int mask;
84
85             if (FRAME_CONTEXT(i, client)) {
86                 win = client->frame->window;
87                 mode = GrabModeAsync;
88                 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
89             } else if (CLIENT_CONTEXT(i, client)) {
90                 win = client->frame->plate;
91                 mode = GrabModeSync; /* this is handled in event */
92                 mask = ButtonPressMask; /* can't catch more than this with Sync
93                                            mode the release event is
94                                            manufactured in event() */
95             } else continue;
96
97             if (grab)
98                 grab_button_full(b->button, b->state, win, mask, mode,
99                                  OB_CURSOR_NONE);
100             else
101                 ungrab_button(b->button, b->state, win);
102         }
103 }
104
105 static void grab_all_clients(gboolean grab)
106 {
107     GList *it;
108
109     for (it = client_list; it != NULL; it = it->next)
110         mouse_grab_for_client(it->data, grab);
111 }
112
113 static void clearall()
114 {
115     int i;
116     GSList *it;
117     
118     for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
119         for (it = bound_contexts[i]; it != NULL; it = it->next) {
120             ObMouseBinding *b = it->data;
121             int j;
122
123             for (j = 0; j < OB_NUM_MOUSE_ACTIONS; ++j) {
124                 GSList *it;
125
126                 for (it = b->actions[j]; it; it = it->next)
127                     action_free(it->data);
128                 g_slist_free(b->actions[j]);
129             }
130             g_free(b);
131         }
132         g_slist_free(bound_contexts[i]);
133         bound_contexts[i] = NULL;
134     }
135 }
136
137 static gboolean fire_binding(ObMouseAction a, ObFrameContext context,
138                              ObClient *c, guint state,
139                              guint button, int x, int y)
140 {
141     GSList *it;
142     ObMouseBinding *b;
143
144     for (it = bound_contexts[context]; it != NULL; it = it->next) {
145         b = it->data;
146         if (b->state == state && b->button == button)
147             break;
148     }
149     /* if not bound, then nothing to do! */
150     if (it == NULL) return FALSE;
151
152     for (it = b->actions[a]; it; it = it->next)
153         action_run_mouse(it->data, c, state, button, x, y);
154     return TRUE;
155 }
156
157 void mouse_event(ObClient *client, XEvent *e)
158 {
159     static Time ltime;
160     static guint button = 0, state = 0, lbutton = 0;
161     static Window lwindow = None;
162     static int px, py;
163
164     ObFrameContext context;
165     gboolean click = FALSE;
166     gboolean dclick = FALSE;
167
168     switch (e->type) {
169     case ButtonPress:
170         context = frame_context(client, e->xany.window);
171         context = mouse_button_frame_context(context, e->xbutton.button);
172
173         px = e->xbutton.x_root;
174         py = e->xbutton.y_root;
175         button = e->xbutton.button;
176         state = e->xbutton.state;
177
178         fire_binding(OB_MOUSE_ACTION_PRESS, context,
179                      client, e->xbutton.state,
180                      e->xbutton.button,
181                      e->xbutton.x_root, e->xbutton.y_root);
182
183         if (CLIENT_CONTEXT(context, client)) {
184             /* Replay the event, so it goes to the client*/
185             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
186             /* Fall through to the release case! */
187         } else
188             break;
189
190     case ButtonRelease:
191         context = frame_context(client, e->xany.window);
192         context = mouse_button_frame_context(context, e->xbutton.button);
193
194         if (e->xbutton.button == button) {
195             /* clicks are only valid if its released over the window */
196             int junk1, junk2;
197             Window wjunk;
198             guint ujunk, b, w, h;
199             xerror_set_ignore(TRUE);
200             junk1 = XGetGeometry(ob_display, e->xbutton.window,
201                                  &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
202             xerror_set_ignore(FALSE);
203             if (junk1) {
204                 if (e->xbutton.x >= (signed)-b &&
205                     e->xbutton.y >= (signed)-b &&
206                     e->xbutton.x < (signed)(w+b) &&
207                     e->xbutton.y < (signed)(h+b)) {
208                     click = TRUE;
209                     /* double clicks happen if there were 2 in a row! */
210                     if (lbutton == button &&
211                         lwindow == e->xbutton.window &&
212                         e->xbutton.time - config_mouse_dclicktime <=
213                         ltime) {
214                         dclick = TRUE;
215                         lbutton = 0;
216                     } else {
217                         lbutton = button;
218                         lwindow = e->xbutton.window;
219                     }
220                 } else {
221                     lbutton = 0;
222                     lwindow = None;
223                 }
224             }
225
226             button = 0;
227             state = 0;
228             ltime = e->xbutton.time;
229         }
230         fire_binding(OB_MOUSE_ACTION_RELEASE, context,
231                      client, e->xbutton.state,
232                      e->xbutton.button,
233                      e->xbutton.x_root, e->xbutton.y_root);
234         if (click)
235             fire_binding(OB_MOUSE_ACTION_CLICK, context,
236                          client, e->xbutton.state,
237                          e->xbutton.button,
238                          e->xbutton.x_root,
239                          e->xbutton.y_root);
240         if (dclick)
241             fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
242                          client, e->xbutton.state,
243                          e->xbutton.button,
244                          e->xbutton.x_root,
245                          e->xbutton.y_root);
246         break;
247
248     case MotionNotify:
249         if (button) {
250             context = frame_context(client, e->xany.window);
251             context = mouse_button_frame_context(context, button);
252
253             if (ABS(e->xmotion.x_root - px) >=
254                 config_mouse_threshold ||
255                 ABS(e->xmotion.y_root - py) >=
256                 config_mouse_threshold) {
257
258                 /* You can't drag on buttons */
259                 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
260                     context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
261                     context == OB_FRAME_CONTEXT_SHADE ||
262                     context == OB_FRAME_CONTEXT_ICONIFY ||
263                     context == OB_FRAME_CONTEXT_ICON ||
264                     context == OB_FRAME_CONTEXT_CLOSE)
265                     break;
266
267                 fire_binding(OB_MOUSE_ACTION_MOTION, context,
268                              client, state, button, px, py);
269                 button = 0;
270                 state = 0;
271             }
272         }
273         break;
274
275     default:
276         g_assert_not_reached();
277     }
278 }
279
280 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
281                     ObAction *action)
282 {
283     guint state, button;
284     ObFrameContext context;
285     ObMouseBinding *b;
286     GSList *it;
287
288     if (!translate_button(buttonstr, &state, &button)) {
289         g_warning("invalid button '%s'", buttonstr);
290         return FALSE;
291     }
292
293     context = frame_context_from_string(contextstr);
294     if (!context) {
295         g_warning("invalid context '%s'", contextstr);
296         return FALSE;
297     }
298
299     for (it = bound_contexts[context]; it != NULL; it = it->next){
300         b = it->data;
301         if (b->state == state && b->button == button) {
302             b->actions[mact] = g_slist_append(b->actions[mact], action);
303             return TRUE;
304         }
305     }
306
307     /* when there are no modifiers in the binding, then the action cannot
308        be interactive */
309     if (!state && action->data.any.interactive) {
310         action->data.any.interactive = FALSE;
311         action->data.inter.final = TRUE;
312     }
313
314     /* add the binding */
315     b = g_new0(ObMouseBinding, 1);
316     b->state = state;
317     b->button = button;
318     b->actions[mact] = g_slist_append(NULL, action);
319     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
320
321     return TRUE;
322 }
323
324 void mouse_startup(gboolean reconfig)
325 {
326     grab_all_clients(TRUE);
327 }
328
329 void mouse_shutdown(gboolean reconfig)
330 {
331     grab_all_clients(FALSE);
332     clearall();
333 }