]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/mouse.c
remove debug printings
[mikachu/openbox.git] / openbox / mouse.c
1 #include "openbox.h"
2 #include "config.h"
3 #include "action.h"
4 #include "event.h"
5 #include "client.h"
6 #include "prop.h"
7 #include "grab.h"
8 #include "frame.h"
9 #include "translate.h"
10 #include "mouse.h"
11 #include "keyboard.h"
12 #include <glib.h>
13
14 typedef struct {
15     guint state;
16     guint button;
17     GSList *actions[NUM_MOUSEACTION]; /* lists of Action pointers */
18 } ObMouseBinding;
19
20 /* Array of GSList*s of PointerBinding*s. */
21 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
22
23 void mouse_grab_for_client(ObClient *client, gboolean grab)
24 {
25     int i;
26     GSList *it;
27
28     for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
29         for (it = bound_contexts[i]; it != NULL; it = it->next) {
30             /* grab/ungrab the button */
31             ObMouseBinding *b = it->data;
32             Window win;
33             int mode;
34             unsigned int mask;
35
36             if (i == OB_FRAME_CONTEXT_FRAME) {
37                 win = client->frame->window;
38                 mode = GrabModeAsync;
39                 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
40             } else if (i == OB_FRAME_CONTEXT_CLIENT) {
41                 win = client->frame->plate;
42                 mode = GrabModeSync; /* this is handled in event */
43                 mask = ButtonPressMask; /* can't catch more than this with Sync
44                                            mode the release event is
45                                            manufactured in event() */
46             } else continue;
47
48             if (grab)
49                 grab_button_full(b->button, b->state, win, mask, mode, None);
50             else
51                 ungrab_button(b->button, b->state, win);
52         }
53 }
54
55 static void grab_all_clients(gboolean grab)
56 {
57     GList *it;
58
59     for (it = client_list; it != NULL; it = it->next)
60         mouse_grab_for_client(it->data, grab);
61 }
62
63 static void clearall()
64 {
65     int i;
66     GSList *it;
67     
68     for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
69         for (it = bound_contexts[i]; it != NULL; it = it->next) {
70             int j;
71
72             ObMouseBinding *b = it->data;
73             for (j = 0; j < NUM_MOUSEACTION; ++j) {
74                 GSList *it;
75                 for (it = b->actions[j]; it; it = it->next) {
76                     action_free(it->data);
77                 }
78                 g_slist_free(b->actions[j]);
79             }
80             g_free(b);
81         }
82         g_slist_free(bound_contexts[i]);
83     }
84 }
85
86 static void fire_button(ObMouseAction a, ObFrameContext context,
87                         ObClient *c, guint state,
88                         guint button, int x, int y)
89 {
90     GSList *it;
91     ObMouseBinding *b;
92
93     for (it = bound_contexts[context]; it != NULL; it = it->next) {
94         b = it->data;
95         if (b->state == state && b->button == button)
96             break;
97     }
98     /* if not bound, then nothing to do! */
99     if (it == NULL) return;
100
101     for (it = b->actions[a]; it; it = it->next) {
102         ObAction *act = it->data;
103         if (act->func != NULL) {
104             act->data.any.c = c;
105
106             g_assert(act->func != action_moveresize);
107
108             if (act->func == action_showmenu) {
109                 act->data.showmenu.x = x;
110                 act->data.showmenu.y = y;
111             }
112
113             if (act->func == action_desktop_dir)
114             {
115                 act->data.desktopdir.final = FALSE;
116                 act->data.desktopdir.cancel = FALSE;
117             }
118             if (act->func == action_send_to_desktop_dir)
119             {
120                 act->data.sendtodir.final = FALSE;
121                 act->data.sendtodir.cancel = FALSE;
122             }
123
124             if ((act->func == action_desktop_dir ||
125                  act->func == action_send_to_desktop_dir)) {
126                 keyboard_interactive_grab(state, c, context, act);
127             }
128
129             act->func(&act->data);
130         }
131     }
132 }
133
134 static void fire_motion(ObMouseAction a, ObFrameContext context, ObClient *c,
135                         guint state, guint button, int x_root, int y_root,
136                         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;
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 }
172
173 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
174 {
175     if (x - cx < cw / 2) {
176         if (y - cy < ch / 2)
177             return prop_atoms.net_wm_moveresize_size_topleft;
178         else
179             return prop_atoms.net_wm_moveresize_size_bottomleft;
180     } else {
181         if (y - cy < ch / 2)
182             return prop_atoms.net_wm_moveresize_size_topright;
183         else
184             return prop_atoms.net_wm_moveresize_size_bottomright;
185     }
186 }
187
188 void mouse_event(ObClient *client, ObFrameContext context, XEvent *e)
189 {
190     static Time ltime;
191     static guint button = 0, state = 0, lbutton = 0;
192
193     static Window lwindow = None;
194     static int px, py;
195     gboolean click = FALSE;
196     gboolean dclick = FALSE;
197     
198     switch (e->type) {
199     case ButtonPress:
200         px = e->xbutton.x_root;
201         py = e->xbutton.y_root;
202         button = e->xbutton.button;
203         state = e->xbutton.state;
204
205         fire_button(MouseAction_Press, context,
206                     client, e->xbutton.state,
207                     e->xbutton.button,
208                     e->xbutton.x_root, e->xbutton.y_root);
209
210         if (context == OB_FRAME_CONTEXT_CLIENT) {
211             /* Replay the event, so it goes to the client*/
212             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
213             /* Fall through to the release case! */
214         } else
215             break;
216
217     case ButtonRelease:
218         if (e->xbutton.button == button) {
219             /* clicks are only valid if its released over the window */
220             int junk1, junk2;
221             Window wjunk;
222             guint ujunk, b, w, h;
223             XGetGeometry(ob_display, e->xbutton.window,
224                          &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
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             button = 0;
247             state = 0;
248             ltime = e->xbutton.time;
249         }
250         fire_button(MouseAction_Release, context,
251                     client, e->xbutton.state,
252                     e->xbutton.button,
253                     e->xbutton.x_root, e->xbutton.y_root);
254         if (click)
255             fire_button(MouseAction_Click, context,
256                         client, e->xbutton.state,
257                         e->xbutton.button,
258                         e->xbutton.x_root,
259                         e->xbutton.y_root);
260         if (dclick)
261             fire_button(MouseAction_DClick, context,
262                         client, e->xbutton.state,
263                         e->xbutton.button,
264                         e->xbutton.x_root,
265                         e->xbutton.y_root);
266         break;
267
268     case MotionNotify:
269         if (button) {
270             if (ABS(e->xmotion.x_root - px) >=
271                 config_mouse_threshold ||
272                 ABS(e->xmotion.y_root - py) >=
273                 config_mouse_threshold) {
274                 guint32 corner;
275
276                 /* You can't drag on buttons */
277                 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
278                     context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
279                     context == OB_FRAME_CONTEXT_SHADE ||
280                     context == OB_FRAME_CONTEXT_ICONIFY ||
281                     context == OB_FRAME_CONTEXT_ICON ||
282                     context == OB_FRAME_CONTEXT_CLOSE)
283                     break;
284
285                 if (!client)
286                     corner = prop_atoms.net_wm_moveresize_size_bottomright;
287                 else
288                     corner =
289                         pick_corner(e->xmotion.x_root,
290                                     e->xmotion.y_root,
291                                     client->frame->area.x,
292                                     client->frame->area.y,
293                                     /* use the client size because the frame
294                                        can be differently sized (shaded
295                                        windows) and we want this based on the
296                                        clients size */
297                                     client->area.width +
298                                     client->frame->size.left +
299                                     client->frame->size.right,
300                                     client->area.height +
301                                     client->frame->size.top +
302                                     client->frame->size.bottom);
303                 fire_motion(MouseAction_Motion, context,
304                             client, state, button, px, py, corner);
305                 button = 0;
306                 state = 0;
307             }
308         }
309         break;
310
311     default:
312         g_assert_not_reached();
313     }
314 }
315
316 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
317                     ObAction *action)
318 {
319     guint state, button;
320     ObFrameContext context;
321     ObMouseBinding *b;
322     GSList *it;
323
324     if (!translate_button(buttonstr, &state, &button)) {
325         g_warning("invalid button '%s'", buttonstr);
326         return FALSE;
327     }
328
329     contextstr = g_ascii_strdown(contextstr, -1);
330     context = frame_context_from_string(contextstr);
331     if (!context) {
332         g_warning("invalid context '%s'", contextstr);
333         g_free(contextstr);
334         return FALSE;
335     }
336     g_free(contextstr);
337
338     for (it = bound_contexts[context]; it != NULL; it = it->next){
339         b = it->data;
340         if (b->state == state && b->button == button) {
341             b->actions[mact] = g_slist_append(b->actions[mact], action);
342             return TRUE;
343         }
344     }
345
346     grab_all_clients(FALSE);
347
348     /* add the binding */
349     b = g_new0(ObMouseBinding, 1);
350     b->state = state;
351     b->button = button;
352     b->actions[mact] = g_slist_append(NULL, action);
353     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
354
355     grab_all_clients(TRUE);
356
357     return TRUE;
358 }
359
360 void mouse_startup()
361 {
362 }
363
364 void mouse_shutdown()
365 {
366     grab_all_clients(FALSE);
367     clearall();
368 }