no more interactive mouse actions, they are evil etc. actions now "feel" much more...
[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 <glib.h>
13
14 typedef struct {
15     guint state;
16     guint button;
17     GSList *actions[OB_NUM_MOUSE_ACTIONS]; /* lists of Action pointers */
18 } ObMouseBinding;
19
20 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
21                                co == OB_FRAME_CONTEXT_FRAME : FALSE)
22 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
23                                 co == OB_FRAME_CONTEXT_DESKTOP : \
24                                 co == OB_FRAME_CONTEXT_CLIENT)
25
26 /* Array of GSList*s of PointerBinding*s. */
27 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
28
29 void mouse_grab_for_client(ObClient *client, gboolean grab)
30 {
31     int i;
32     GSList *it;
33
34     for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
35         for (it = bound_contexts[i]; it != NULL; it = g_slist_next(it)) {
36             /* grab/ungrab the button */
37             ObMouseBinding *b = it->data;
38             Window win;
39             int mode;
40             unsigned int mask;
41
42             if (FRAME_CONTEXT(i, client)) {
43                 win = client->frame->window;
44                 mode = GrabModeAsync;
45                 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
46             } else if (CLIENT_CONTEXT(i, client)) {
47                 win = client->frame->plate;
48                 mode = GrabModeSync; /* this is handled in event */
49                 mask = ButtonPressMask; /* can't catch more than this with Sync
50                                            mode the release event is
51                                            manufactured in event() */
52             } else continue;
53
54             if (grab)
55                 grab_button_full(b->button, b->state, win, mask, mode,
56                                  OB_CURSOR_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             ObMouseBinding *b = it->data;
78             int j;
79
80             for (j = 0; j < OB_NUM_MOUSE_ACTIONS; ++j) {
81                 GSList *it;
82
83                 for (it = b->actions[j]; it; it = it->next)
84                     action_free(it->data);
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_binding(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         action_run_mouse(it->data, c, state, button, x, y);
111     return TRUE;
112 }
113
114 void mouse_event(ObClient *client, XEvent *e)
115 {
116     static Time ltime;
117     static guint button = 0, state = 0, lbutton = 0;
118     static Window lwindow = None;
119     static int px, py;
120
121     ObFrameContext context;
122     gboolean click = FALSE;
123     gboolean dclick = FALSE;
124
125     context = frame_context(client, e->xany.window);
126
127     switch (e->type) {
128     case ButtonPress:
129         px = e->xbutton.x_root;
130         py = e->xbutton.y_root;
131         button = e->xbutton.button;
132         state = e->xbutton.state;
133
134         fire_binding(OB_MOUSE_ACTION_PRESS, context,
135                      client, e->xbutton.state,
136                      e->xbutton.button,
137                      e->xbutton.x_root, e->xbutton.y_root);
138
139         if (CLIENT_CONTEXT(context, client)) {
140             /* Replay the event, so it goes to the client*/
141             XAllowEvents(ob_display, ReplayPointer, event_lasttime);
142             /* Fall through to the release case! */
143         } else
144             break;
145
146     case ButtonRelease:
147         if (e->xbutton.button == button) {
148             /* clicks are only valid if its released over the window */
149             int junk1, junk2;
150             Window wjunk;
151             guint ujunk, b, w, h;
152             xerror_set_ignore(TRUE);
153             junk1 = XGetGeometry(ob_display, e->xbutton.window,
154                                  &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
155             xerror_set_ignore(FALSE);
156             if (junk1) {
157                 if (e->xbutton.x >= (signed)-b &&
158                     e->xbutton.y >= (signed)-b &&
159                     e->xbutton.x < (signed)(w+b) &&
160                     e->xbutton.y < (signed)(h+b)) {
161                     click = TRUE;
162                     /* double clicks happen if there were 2 in a row! */
163                     if (lbutton == button &&
164                         lwindow == e->xbutton.window &&
165                         e->xbutton.time - config_mouse_dclicktime <=
166                         ltime) {
167                         dclick = TRUE;
168                         lbutton = 0;
169                     } else {
170                         lbutton = button;
171                         lwindow = e->xbutton.window;
172                     }
173                 } else {
174                     lbutton = 0;
175                     lwindow = None;
176                 }
177             }
178
179             button = 0;
180             state = 0;
181             ltime = e->xbutton.time;
182         }
183         fire_binding(OB_MOUSE_ACTION_RELEASE, context,
184                      client, e->xbutton.state,
185                      e->xbutton.button,
186                      e->xbutton.x_root, e->xbutton.y_root);
187         if (click)
188             fire_binding(OB_MOUSE_ACTION_CLICK, context,
189                          client, e->xbutton.state,
190                          e->xbutton.button,
191                          e->xbutton.x_root,
192                          e->xbutton.y_root);
193         if (dclick)
194             fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
195                          client, e->xbutton.state,
196                          e->xbutton.button,
197                          e->xbutton.x_root,
198                          e->xbutton.y_root);
199         break;
200
201     case MotionNotify:
202         if (button) {
203             if (ABS(e->xmotion.x_root - px) >=
204                 config_mouse_threshold ||
205                 ABS(e->xmotion.y_root - py) >=
206                 config_mouse_threshold) {
207
208                 /* You can't drag on buttons */
209                 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
210                     context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
211                     context == OB_FRAME_CONTEXT_SHADE ||
212                     context == OB_FRAME_CONTEXT_ICONIFY ||
213                     context == OB_FRAME_CONTEXT_ICON ||
214                     context == OB_FRAME_CONTEXT_CLOSE)
215                     break;
216
217                 fire_binding(OB_MOUSE_ACTION_MOTION, context,
218                              client, state, button, px, py);
219                 button = 0;
220                 state = 0;
221             }
222         }
223         break;
224
225     default:
226         g_assert_not_reached();
227     }
228 }
229
230 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
231                     ObAction *action)
232 {
233     guint state, button;
234     ObFrameContext context;
235     ObMouseBinding *b;
236     GSList *it;
237
238     if (!translate_button(buttonstr, &state, &button)) {
239         g_warning("invalid button '%s'", buttonstr);
240         return FALSE;
241     }
242
243     contextstr = g_ascii_strdown(contextstr, -1);
244     context = frame_context_from_string(contextstr);
245     if (!context) {
246         g_warning("invalid context '%s'", contextstr);
247         g_free(contextstr);
248         return FALSE;
249     }
250     g_free(contextstr);
251
252     for (it = bound_contexts[context]; it != NULL; it = it->next){
253         b = it->data;
254         if (b->state == state && b->button == button) {
255             b->actions[mact] = g_slist_append(b->actions[mact], action);
256             return TRUE;
257         }
258     }
259
260     /* when there are no modifiers in the binding, then the action cannot
261        be interactive */
262     if (!state && action->data.any.interactive) {
263         action->data.any.interactive = FALSE;
264         action->data.inter.final = TRUE;
265     }
266
267     /* add the binding */
268     b = g_new0(ObMouseBinding, 1);
269     b->state = state;
270     b->button = button;
271     b->actions[mact] = g_slist_append(NULL, action);
272     bound_contexts[context] = g_slist_append(bound_contexts[context], b);
273
274     return TRUE;
275 }
276
277 void mouse_startup(gboolean reconfig)
278 {
279     grab_all_clients(TRUE);
280 }
281
282 void mouse_shutdown(gboolean reconfig)
283 {
284     grab_all_clients(FALSE);
285     clearall();
286 }