]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/mouse.c
add a reconfigure action, also reconfigure on SIGUSR2.
[mikachu/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 FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
22                                co == OB_FRAME_CONTEXT_FRAME : FALSE)
23 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
24                                 co == OB_FRAME_CONTEXT_DESKTOP : \
25                                 co == OB_FRAME_CONTEXT_CLIENT)
26
27 /* Array of GSList*s of PointerBinding*s. */
28 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
29
30 void mouse_grab_for_client(ObClient *client, gboolean grab)
31 {
32     int i;
33     GSList *it;
34
35     for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
36         for (it = bound_contexts[i]; it != NULL; it = it->next) {
37             /* grab/ungrab the button */
38             ObMouseBinding *b = it->data;
39             Window win;
40             int mode;
41             unsigned int mask;
42
43             if (FRAME_CONTEXT(i, client)) {
44                 win = client->frame->window;
45                 mode = GrabModeAsync;
46                 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
47             } else if (CLIENT_CONTEXT(i, client)) {
48                 win = client->frame->plate;
49                 mode = GrabModeSync; /* this is handled in event */
50                 mask = ButtonPressMask; /* can't catch more than this with Sync
51                                            mode the release event is
52                                            manufactured in event() */
53             } else continue;
54
55             if (grab)
56                 grab_button_full(b->button, b->state, win, mask, mode, None);
57             else
58                 ungrab_button(b->button, b->state, win);
59         }
60 }
61
62 static void grab_all_clients(gboolean grab)
63 {
64     GList *it;
65
66     for (it = client_list; it != NULL; it = it->next)
67         mouse_grab_for_client(it->data, grab);
68 }
69
70 static void clearall()
71 {
72     int i;
73     GSList *it;
74     
75     for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
76         for (it = bound_contexts[i]; it != NULL; it = it->next) {
77             int j;
78
79             ObMouseBinding *b = it->data;
80             for (j = 0; j < OB_MOUSE_NUM_ACTIONS; ++j) {
81                 GSList *it;
82                 for (it = b->actions[j]; it; it = it->next) {
83                     action_free(it->data);
84                 }
85                 g_slist_free(b->actions[j]);
86             }
87             g_free(b);
88         }
89         g_slist_free(bound_contexts[i]);
90         bound_contexts[i] = NULL;
91     }
92 }
93
94 static gboolean fire_button(ObMouseAction a, ObFrameContext context,
95                             ObClient *c, guint state,
96                             guint button, int x, int y)
97 {
98     GSList *it;
99     ObMouseBinding *b;
100
101     for (it = bound_contexts[context]; it != NULL; it = it->next) {
102         b = it->data;
103         if (b->state == state && b->button == button)
104             break;
105     }
106     /* if not bound, then nothing to do! */
107     if (it == NULL) return FALSE;
108
109     for (it = b->actions[a]; it; it = it->next) {
110         ObAction *act = it->data;
111         if (act->func != NULL) {
112             act->data.any.c = c;
113
114             g_assert(act->func != action_moveresize);
115
116             if (act->func == action_showmenu) {
117                 act->data.showmenu.x = x;
118                 act->data.showmenu.y = y;
119             }
120
121             if (act->data.any.interactive) {
122                 act->data.inter.cancel = FALSE;
123                 act->data.inter.final = FALSE;
124                 keyboard_interactive_grab(state, c, context, act);
125             }
126
127             act->func(&act->data);
128         }
129     }
130     return TRUE;
131 }
132
133 static gboolean fire_motion(ObMouseAction a, ObFrameContext context,
134                             ObClient *c, guint state, guint button,
135                             int x_root, int y_root, guint32 corner)
136 {
137     GSList *it;
138     ObMouseBinding *b;
139
140     for (it = bound_contexts[context]; it != NULL; it = it->next) {
141         b = it->data;
142         if (b->state == state && b->button == button)
143                 break;
144     }
145     /* if not bound, then nothing to do! */
146     if (it == NULL) return FALSE;
147
148     for (it = b->actions[a]; it; it = it->next) {
149         ObAction *act = it->data;
150         if (act->func != NULL) {
151             act->data.any.c = c;
152
153             if (act->func == action_moveresize) {
154                 act->data.moveresize.x = x_root;
155                 act->data.moveresize.y = y_root;
156                 act->data.moveresize.button = button;
157                 if (!(act->data.moveresize.corner ==
158                       prop_atoms.net_wm_moveresize_move ||
159                       act->data.moveresize.corner ==
160                       prop_atoms.net_wm_moveresize_move_keyboard ||
161                       act->data.moveresize.corner ==
162                       prop_atoms.net_wm_moveresize_size_keyboard))
163                     act->data.moveresize.corner = corner;
164             } else
165                 g_assert_not_reached();
166
167             act->func(&act->data);
168         }
169     }
170     return TRUE;
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(OB_MOUSE_ACTION_PRESS, context,
206                     client, e->xbutton.state,
207                     e->xbutton.button,
208                     e->xbutton.x_root, e->xbutton.y_root);
209
210         if (CLIENT_CONTEXT(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             xerror_set_ignore(TRUE);
224             junk1 = XGetGeometry(ob_display, e->xbutton.window,
225                                  &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
226             xerror_set_ignore(FALSE);
227             if (junk1) {
228                 if (e->xbutton.x >= (signed)-b &&
229                     e->xbutton.y >= (signed)-b &&
230                     e->xbutton.x < (signed)(w+b) &&
231                     e->xbutton.y < (signed)(h+b)) {
232                     click = TRUE;
233                     /* double clicks happen if there were 2 in a row! */
234                     if (lbutton == button &&
235                         lwindow == e->xbutton.window &&
236                         e->xbutton.time - config_mouse_dclicktime <=
237                         ltime) {
238                         dclick = TRUE;
239                         lbutton = 0;
240                     } else {
241                         lbutton = button;
242                         lwindow = e->xbutton.window;
243                     }
244                 } else {
245                     lbutton = 0;
246                     lwindow = None;
247                 }
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     /* when there are no modifiers in the binding, then the action cannot
353        be interactive */
354     if (!state && action->data.any.interactive) {
355         action->data.any.interactive = FALSE;
356         action->data.inter.final = TRUE;
357     }
358
359     /* add the binding */
360     b = g_new0(ObMouseBinding, 1);
361     b->state = state;
362     b->button = button;
363     b->actions[mact] = g_slist_append(NULL, action);
364     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
365
366     grab_all_clients(TRUE);
367
368     return TRUE;
369 }
370
371 void mouse_startup(gboolean reconfig)
372 {
373 }
374
375 void mouse_shutdown(gboolean reconfig)
376 {
377     grab_all_clients(FALSE);
378     clearall();
379 }