move the keyboard and mouse plugins into the kernel for mucho sexiness.
[dana/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     g_message("%d %d %d", context, state, button);
94
95     for (it = bound_contexts[context]; it != NULL; it = it->next) {
96         b = it->data;
97         if (b->state == state && b->button == button)
98             break;
99     }
100     /* if not bound, then nothing to do! */
101     if (it == NULL) return;
102
103     for (it = b->actions[a]; it; it = it->next) {
104         ObAction *act = it->data;
105         if (act->func != NULL) {
106             act->data.any.c = c;
107
108             g_assert(act->func != action_moveresize);
109
110             if (act->func == action_showmenu) {
111                 act->data.showmenu.x = x;
112                 act->data.showmenu.y = y;
113             }
114
115             if (act->func == action_desktop_dir)
116             {
117                 act->data.desktopdir.final = FALSE;
118                 act->data.desktopdir.cancel = FALSE;
119             }
120             if (act->func == action_send_to_desktop_dir)
121             {
122                 act->data.sendtodir.final = FALSE;
123                 act->data.sendtodir.cancel = FALSE;
124             }
125
126             if ((act->func == action_desktop_dir ||
127                  act->func == action_send_to_desktop_dir)) {
128                 keyboard_interactive_grab(state, c, context, act);
129             }
130
131             g_message("acting");
132
133             act->func(&act->data);
134         }
135     }
136 }
137
138 static void fire_motion(ObMouseAction a, ObFrameContext context, ObClient *c,
139                         guint state, guint button, int x_root, int y_root,
140                         guint32 corner)
141 {
142     GSList *it;
143     ObMouseBinding *b;
144
145     for (it = bound_contexts[context]; it != NULL; it = it->next) {
146         b = it->data;
147         if (b->state == state && b->button == button)
148                 break;
149     }
150     /* if not bound, then nothing to do! */
151     if (it == NULL) return;
152
153     for (it = b->actions[a]; it; it = it->next) {
154         ObAction *act = it->data;
155         if (act->func != NULL) {
156             act->data.any.c = c;
157
158             if (act->func == action_moveresize) {
159                 act->data.moveresize.x = x_root;
160                 act->data.moveresize.y = y_root;
161                 act->data.moveresize.button = button;
162                 if (!(act->data.moveresize.corner ==
163                       prop_atoms.net_wm_moveresize_move ||
164                       act->data.moveresize.corner ==
165                       prop_atoms.net_wm_moveresize_move_keyboard ||
166                       act->data.moveresize.corner ==
167                       prop_atoms.net_wm_moveresize_size_keyboard))
168                     act->data.moveresize.corner = corner;
169             } else
170                 g_assert_not_reached();
171
172             act->func(&act->data);
173         }
174     }
175 }
176
177 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
178 {
179     if (x - cx < cw / 2) {
180         if (y - cy < ch / 2)
181             return prop_atoms.net_wm_moveresize_size_topleft;
182         else
183             return prop_atoms.net_wm_moveresize_size_bottomleft;
184     } else {
185         if (y - cy < ch / 2)
186             return prop_atoms.net_wm_moveresize_size_topright;
187         else
188             return prop_atoms.net_wm_moveresize_size_bottomright;
189     }
190 }
191
192 void mouse_event(ObClient *client, ObFrameContext context, XEvent *e)
193 {
194     static Time ltime;
195     static guint button = 0, state = 0, lbutton = 0;
196
197     static Window lwindow = None;
198     static int px, py;
199     gboolean click = FALSE;
200     gboolean dclick = FALSE;
201     
202     switch (e->type) {
203     case ButtonPress:
204         px = e->xbutton.x_root;
205         py = e->xbutton.y_root;
206         button = e->xbutton.button;
207         state = e->xbutton.state;
208
209         fire_button(MouseAction_Press, context,
210                     client, e->xbutton.state,
211                     e->xbutton.button,
212                     e->xbutton.x_root, e->xbutton.y_root);
213
214         if (context == OB_FRAME_CONTEXT_CLIENT) {
215             /* Replay the event, so it goes to the client*/
216             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
217             /* Fall through to the release case! */
218         } else
219             break;
220
221     case ButtonRelease:
222         if (e->xbutton.button == button) {
223             /* clicks are only valid if its released over the window */
224             int junk1, junk2;
225             Window wjunk;
226             guint ujunk, b, w, h;
227             XGetGeometry(ob_display, e->xbutton.window,
228                          &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
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             button = 0;
251             state = 0;
252             ltime = e->xbutton.time;
253         }
254         fire_button(MouseAction_Release, context,
255                     client, e->xbutton.state,
256                     e->xbutton.button,
257                     e->xbutton.x_root, e->xbutton.y_root);
258         if (click)
259             fire_button(MouseAction_Click, context,
260                         client, e->xbutton.state,
261                         e->xbutton.button,
262                         e->xbutton.x_root,
263                         e->xbutton.y_root);
264         if (dclick)
265             fire_button(MouseAction_DClick, context,
266                         client, e->xbutton.state,
267                         e->xbutton.button,
268                         e->xbutton.x_root,
269                         e->xbutton.y_root);
270         break;
271
272     case MotionNotify:
273         if (button) {
274             if (ABS(e->xmotion.x_root - px) >=
275                 config_mouse_threshold ||
276                 ABS(e->xmotion.y_root - py) >=
277                 config_mouse_threshold) {
278                 guint32 corner;
279
280                 /* You can't drag on buttons */
281                 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
282                     context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
283                     context == OB_FRAME_CONTEXT_SHADE ||
284                     context == OB_FRAME_CONTEXT_ICONIFY ||
285                     context == OB_FRAME_CONTEXT_ICON ||
286                     context == OB_FRAME_CONTEXT_CLOSE)
287                     break;
288
289                 if (!client)
290                     corner = prop_atoms.net_wm_moveresize_size_bottomright;
291                 else
292                     corner =
293                         pick_corner(e->xmotion.x_root,
294                                     e->xmotion.y_root,
295                                     client->frame->area.x,
296                                     client->frame->area.y,
297                                     /* use the client size because the frame
298                                        can be differently sized (shaded
299                                        windows) and we want this based on the
300                                        clients size */
301                                     client->area.width +
302                                     client->frame->size.left +
303                                     client->frame->size.right,
304                                     client->area.height +
305                                     client->frame->size.top +
306                                     client->frame->size.bottom);
307                 fire_motion(MouseAction_Motion, context,
308                             client, state, button, px, py, corner);
309                 button = 0;
310                 state = 0;
311             }
312         }
313         break;
314
315     default:
316         g_assert_not_reached();
317     }
318 }
319
320 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
321                     ObAction *action)
322 {
323     guint state, button;
324     ObFrameContext context;
325     ObMouseBinding *b;
326     GSList *it;
327
328     if (!translate_button(buttonstr, &state, &button)) {
329         g_warning("invalid button '%s'", buttonstr);
330         return FALSE;
331     }
332
333     contextstr = g_ascii_strdown(contextstr, -1);
334     context = frame_context_from_string(contextstr);
335     if (!context) {
336         g_warning("invalid context '%s'", contextstr);
337         g_free(contextstr);
338         return FALSE;
339     }
340     g_free(contextstr);
341
342     for (it = bound_contexts[context]; it != NULL; it = it->next){
343         b = it->data;
344         if (b->state == state && b->button == button) {
345             b->actions[mact] = g_slist_append(b->actions[mact], action);
346             return TRUE;
347         }
348     }
349
350     grab_all_clients(FALSE);
351
352     /* add the binding */
353     b = g_new0(ObMouseBinding, 1);
354     b->state = state;
355     b->button = button;
356     b->actions[mact] = g_slist_append(NULL, action);
357     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
358
359     grab_all_clients(TRUE);
360
361     return TRUE;
362 }
363
364 void mouse_startup()
365 {
366 }
367
368 void mouse_shutdown()
369 {
370     grab_all_clients(FALSE);
371     clearall();
372 }