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