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