]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/mouse.c
adjust for changes to the parsing api.
[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[OB_MOUSE_NUM_ACTIONS]; /* 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 < OB_MOUSE_NUM_ACTIONS; ++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 gboolean 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 FALSE;
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 (config_desktop_popup &&
125                 (act->func == action_desktop_dir ||
126                  act->func == action_send_to_desktop_dir))
127             {
128                 keyboard_interactive_grab(state, c, context, act);
129             }
130
131             act->func(&act->data);
132         }
133     }
134     return TRUE;
135 }
136
137 static gboolean fire_motion(ObMouseAction a, ObFrameContext context,
138                             ObClient *c, guint state, guint button,
139                             int x_root, int y_root, guint32 corner)
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         ObAction *act = it->data;
154         if (act->func != NULL) {
155             act->data.any.c = c;
156
157             if (act->func == action_moveresize) {
158                 act->data.moveresize.x = x_root;
159                 act->data.moveresize.y = y_root;
160                 act->data.moveresize.button = button;
161                 if (!(act->data.moveresize.corner ==
162                       prop_atoms.net_wm_moveresize_move ||
163                       act->data.moveresize.corner ==
164                       prop_atoms.net_wm_moveresize_move_keyboard ||
165                       act->data.moveresize.corner ==
166                       prop_atoms.net_wm_moveresize_size_keyboard))
167                     act->data.moveresize.corner = corner;
168             } else
169                 g_assert_not_reached();
170
171             act->func(&act->data);
172         }
173     }
174     return TRUE;
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(OB_MOUSE_ACTION_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(OB_MOUSE_ACTION_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(OB_MOUSE_ACTION_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(OB_MOUSE_ACTION_DOUBLE_CLICK, 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(OB_MOUSE_ACTION_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 }