prefix/capitalize the mouse actions enum
[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[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 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 (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 }
135
136 static void fire_motion(ObMouseAction a, ObFrameContext context, ObClient *c,
137                         guint state, guint button, int x_root, int y_root,
138                         guint32 corner)
139 {
140     GSList *it;
141     ObMouseBinding *b;
142
143     for (it = bound_contexts[context]; it != NULL; it = it->next) {
144         b = it->data;
145         if (b->state == state && b->button == button)
146                 break;
147     }
148     /* if not bound, then nothing to do! */
149     if (it == NULL) return;
150
151     for (it = b->actions[a]; it; it = it->next) {
152         ObAction *act = it->data;
153         if (act->func != NULL) {
154             act->data.any.c = c;
155
156             if (act->func == action_moveresize) {
157                 act->data.moveresize.x = x_root;
158                 act->data.moveresize.y = y_root;
159                 act->data.moveresize.button = button;
160                 if (!(act->data.moveresize.corner ==
161                       prop_atoms.net_wm_moveresize_move ||
162                       act->data.moveresize.corner ==
163                       prop_atoms.net_wm_moveresize_move_keyboard ||
164                       act->data.moveresize.corner ==
165                       prop_atoms.net_wm_moveresize_size_keyboard))
166                     act->data.moveresize.corner = corner;
167             } else
168                 g_assert_not_reached();
169
170             act->func(&act->data);
171         }
172     }
173 }
174
175 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
176 {
177     if (x - cx < cw / 2) {
178         if (y - cy < ch / 2)
179             return prop_atoms.net_wm_moveresize_size_topleft;
180         else
181             return prop_atoms.net_wm_moveresize_size_bottomleft;
182     } else {
183         if (y - cy < ch / 2)
184             return prop_atoms.net_wm_moveresize_size_topright;
185         else
186             return prop_atoms.net_wm_moveresize_size_bottomright;
187     }
188 }
189
190 void mouse_event(ObClient *client, ObFrameContext context, XEvent *e)
191 {
192     static Time ltime;
193     static guint button = 0, state = 0, lbutton = 0;
194
195     static Window lwindow = None;
196     static int px, py;
197     gboolean click = FALSE;
198     gboolean dclick = FALSE;
199     
200     switch (e->type) {
201     case ButtonPress:
202         px = e->xbutton.x_root;
203         py = e->xbutton.y_root;
204         button = e->xbutton.button;
205         state = e->xbutton.state;
206
207         fire_button(OB_MOUSE_ACTION_PRESS, context,
208                     client, e->xbutton.state,
209                     e->xbutton.button,
210                     e->xbutton.x_root, e->xbutton.y_root);
211
212         if (context == OB_FRAME_CONTEXT_CLIENT) {
213             /* Replay the event, so it goes to the client*/
214             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
215             /* Fall through to the release case! */
216         } else
217             break;
218
219     case ButtonRelease:
220         if (e->xbutton.button == button) {
221             /* clicks are only valid if its released over the window */
222             int junk1, junk2;
223             Window wjunk;
224             guint ujunk, b, w, h;
225             XGetGeometry(ob_display, e->xbutton.window,
226                          &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
227             if (e->xbutton.x >= (signed)-b &&
228                 e->xbutton.y >= (signed)-b &&
229                 e->xbutton.x < (signed)(w+b) &&
230                 e->xbutton.y < (signed)(h+b)) {
231                 click = TRUE;
232                 /* double clicks happen if there were 2 in a row! */
233                 if (lbutton == button &&
234                     lwindow == e->xbutton.window &&
235                     e->xbutton.time - config_mouse_dclicktime <=
236                     ltime) {
237                     dclick = TRUE;
238                     lbutton = 0;
239                 } else {
240                     lbutton = button;
241                     lwindow = e->xbutton.window;
242                 }
243             } else {
244                 lbutton = 0;
245                 lwindow = None;
246             }
247
248             button = 0;
249             state = 0;
250             ltime = e->xbutton.time;
251         }
252         fire_button(OB_MOUSE_ACTION_RELEASE, context,
253                     client, e->xbutton.state,
254                     e->xbutton.button,
255                     e->xbutton.x_root, e->xbutton.y_root);
256         if (click)
257             fire_button(OB_MOUSE_ACTION_CLICK, context,
258                         client, e->xbutton.state,
259                         e->xbutton.button,
260                         e->xbutton.x_root,
261                         e->xbutton.y_root);
262         if (dclick)
263             fire_button(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
264                         client, e->xbutton.state,
265                         e->xbutton.button,
266                         e->xbutton.x_root,
267                         e->xbutton.y_root);
268         break;
269
270     case MotionNotify:
271         if (button) {
272             if (ABS(e->xmotion.x_root - px) >=
273                 config_mouse_threshold ||
274                 ABS(e->xmotion.y_root - py) >=
275                 config_mouse_threshold) {
276                 guint32 corner;
277
278                 /* You can't drag on buttons */
279                 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
280                     context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
281                     context == OB_FRAME_CONTEXT_SHADE ||
282                     context == OB_FRAME_CONTEXT_ICONIFY ||
283                     context == OB_FRAME_CONTEXT_ICON ||
284                     context == OB_FRAME_CONTEXT_CLOSE)
285                     break;
286
287                 if (!client)
288                     corner = prop_atoms.net_wm_moveresize_size_bottomright;
289                 else
290                     corner =
291                         pick_corner(e->xmotion.x_root,
292                                     e->xmotion.y_root,
293                                     client->frame->area.x,
294                                     client->frame->area.y,
295                                     /* use the client size because the frame
296                                        can be differently sized (shaded
297                                        windows) and we want this based on the
298                                        clients size */
299                                     client->area.width +
300                                     client->frame->size.left +
301                                     client->frame->size.right,
302                                     client->area.height +
303                                     client->frame->size.top +
304                                     client->frame->size.bottom);
305                 fire_motion(OB_MOUSE_ACTION_MOTION, context,
306                             client, state, button, px, py, corner);
307                 button = 0;
308                 state = 0;
309             }
310         }
311         break;
312
313     default:
314         g_assert_not_reached();
315     }
316 }
317
318 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
319                     ObAction *action)
320 {
321     guint state, button;
322     ObFrameContext context;
323     ObMouseBinding *b;
324     GSList *it;
325
326     if (!translate_button(buttonstr, &state, &button)) {
327         g_warning("invalid button '%s'", buttonstr);
328         return FALSE;
329     }
330
331     contextstr = g_ascii_strdown(contextstr, -1);
332     context = frame_context_from_string(contextstr);
333     if (!context) {
334         g_warning("invalid context '%s'", contextstr);
335         g_free(contextstr);
336         return FALSE;
337     }
338     g_free(contextstr);
339
340     for (it = bound_contexts[context]; it != NULL; it = it->next){
341         b = it->data;
342         if (b->state == state && b->button == button) {
343             b->actions[mact] = g_slist_append(b->actions[mact], action);
344             return TRUE;
345         }
346     }
347
348     grab_all_clients(FALSE);
349
350     /* add the binding */
351     b = g_new0(ObMouseBinding, 1);
352     b->state = state;
353     b->button = button;
354     b->actions[mact] = g_slist_append(NULL, action);
355     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
356
357     grab_all_clients(TRUE);
358
359     return TRUE;
360 }
361
362 void mouse_startup()
363 {
364 }
365
366 void mouse_shutdown()
367 {
368     grab_all_clients(FALSE);
369     clearall();
370 }