]> icculus.org git repositories - dana/openbox.git/blob - plugins/mouse/mouse.c
add mbinds for shading windows on the titlebar
[dana/openbox.git] / plugins / mouse / mouse.c
1 #include "../../kernel/openbox.h"
2 #include "../../kernel/dispatch.h"
3 #include "../../kernel/action.h"
4 #include "../../kernel/client.h"
5 #include "../../kernel/frame.h"
6 #include "../../kernel/engine.h"
7 #include "translate.h"
8 #include "mouse.h"
9 #include <glib.h>
10
11 /* GData of GSList*s of PointerBinding*s. */
12 static GData *bound_contexts;
13
14 struct foreach_grab_temp {
15     Client *client;
16     gboolean grab;
17 };
18
19 static void grab_button(Client *client, guint state, guint button,
20                         GQuark context, gboolean grab)
21 {
22     Window win;
23     int mode = GrabModeAsync;
24     unsigned int mask;
25
26     if (context == g_quark_try_string("frame")) {
27         win = client->frame->window;
28         mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
29     } else if (context == g_quark_try_string("client")) {
30         win = client->frame->plate;
31         mode = GrabModeSync; /* this is handled in pointer_event */
32         mask = ButtonPressMask; /* can't catch more than this with Sync mode
33                                    the release event is manufactured in
34                                    pointer_fire */
35     } else return;
36
37     if (grab)
38         /* XXX grab all lock keys */
39         XGrabButton(ob_display, button, state, win, FALSE, mask, mode,
40                     GrabModeAsync, None, None);
41     else
42         /* XXX ungrab all lock keys */
43         XUngrabButton(ob_display, button, state, win);
44 }
45
46 static void foreach_grab(GQuark key, gpointer data, gpointer user_data)
47 {
48     struct foreach_grab_temp *d = user_data;
49     GSList *it;
50     for (it = data; it != NULL; it = it->next) {
51         MouseBinding *b = it->data;
52         grab_button(d->client, b->state, b->button, key, d->grab);
53     }
54 }
55   
56 static void grab_for_client(Client *client, gboolean grab)
57 {
58     struct foreach_grab_temp bt;
59     bt.client = client;
60     bt.grab = grab;
61     g_datalist_foreach(&bound_contexts, foreach_grab, &bt);
62 }
63
64 static void grab_all_clients(gboolean grab)
65 {
66     GSList *it;
67
68     for (it = client_list; it != NULL; it = it->next)
69         grab_for_client(it->data, grab);
70 }
71
72 static void foreach_clear(GQuark key, gpointer data, gpointer user_data)
73 {
74     GSList *it;
75     user_data = user_data;
76     for (it = data; it != NULL; it = it->next) {
77         int i;
78
79         MouseBinding *b = it->data;
80         for (i = 0; i < NUM_MOUSEACTION; ++i)
81             if (b->action[i] != NULL)
82                 action_free(b->action[i]);
83         g_free(b);
84     }
85     g_slist_free(data);
86 }
87
88 static void fire_button(MouseAction a, GQuark context, Client *c, guint state,
89                         guint button)
90 {
91     GSList *it;
92     MouseBinding *b;
93
94     for (it = g_datalist_id_get_data(&bound_contexts, context);
95          it != NULL; it = it->next) {
96         b = it->data;
97         if (b->state == state && b->button == button)
98             break;
99     }
100     /* if not bound, then nothing to do! */
101     if (it == NULL) return;
102
103     if (b->action[a] != NULL && b->action[a]->func != NULL) {
104         b->action[a]->data.any.c = c;
105
106         g_assert(!(b->action[a]->func == action_move ||
107                    b->action[a]->func == action_resize));
108
109         b->action[a]->func(&b->action[a]->data);
110     }
111 }
112
113 /* corner should be the opposite corner of the window in which the pointer
114    clicked, Corner_TopLeft if a good default if there is no client */
115 static void fire_motion(MouseAction a, GQuark context, Client *c, guint state,
116                         guint button, int cx, int cy, int cw, int ch,
117                         int dx, int dy, gboolean final, Corner corner)
118 {
119     GSList *it;
120     MouseBinding *b;
121
122     for (it = g_datalist_id_get_data(&bound_contexts, context);
123          it != NULL; it = it->next) {
124         b = it->data;
125         if (b->state == state && b->button == button)
126                 break;
127     }
128     /* if not bound, then nothing to do! */
129     if (it == NULL) return;
130
131     if (b->action[a] != NULL && b->action[a]->func != NULL) {
132         b->action[a]->data.any.c = c;
133
134         if (b->action[a]->func == action_move) {
135             b->action[a]->data.move.x = cx + dx;
136             b->action[a]->data.move.y = cy + dy;
137             b->action[a]->data.move.final = final;
138         } else if (b->action[a]->func == action_resize) {
139             b->action[a]->data.resize.corner = corner;
140             switch (corner) {
141             case Corner_TopLeft:
142                 b->action[a]->data.resize.x = cw + dx;
143                 b->action[a]->data.resize.y = ch + dy;
144                 break;
145             case Corner_TopRight:
146                 b->action[a]->data.resize.x = cw - dx;
147                 b->action[a]->data.resize.y = ch + dy;
148                 break;
149             case Corner_BottomLeft:
150                 b->action[a]->data.resize.x = cw + dx;
151                 b->action[a]->data.resize.y = ch - dy;
152                 break;
153             case Corner_BottomRight:
154                 b->action[a]->data.resize.x = cw - dx;
155                 b->action[a]->data.resize.y = ch - dy;
156                 break;
157             }
158             b->action[a]->data.resize.final = final;
159         }
160         b->action[a]->func(&b->action[a]->data);
161     }
162 }
163
164 static Corner pick_corner(int x, int y, int cx, int cy, int cw, int ch)
165 {
166     if (x - cx < cw / 2) {
167         if (y - cy < ch / 2)
168             return Corner_BottomRight;
169         else
170             return Corner_TopRight;
171     } else {
172         if (y - cy < ch / 2)
173             return Corner_BottomLeft;
174         else
175             return Corner_TopLeft;
176     }
177 }
178
179 static void event(ObEvent *e, void *foo)
180 {
181     static Time ltime;
182     static int px, py, cx, cy, cw, ch, dx, dy;
183     static guint button = 0, lbutton = 0;
184     static gboolean drag = FALSE;
185     static Corner corner = Corner_TopLeft;
186     gboolean click = FALSE;
187     gboolean dclick = FALSE;
188
189     switch (e->type) {
190     case Event_Client_Mapped:
191         grab_for_client(e->data.c.client, TRUE);
192         break;
193
194     case Event_Client_Destroy:
195         grab_for_client(e->data.c.client, FALSE);
196         break;
197
198     case Event_X_ButtonPress:
199         if (!button) {
200             if (e->data.x.client == NULL)
201                 corner = Corner_TopLeft;
202             else {
203                 cx = e->data.x.client->frame->area.x;
204                 cy = e->data.x.client->frame->area.y;
205                 cw = e->data.x.client->frame->area.width;
206                 ch = e->data.x.client->frame->area.height;
207                 px = e->data.x.e->xbutton.x_root;
208                 py = e->data.x.e->xbutton.y_root;
209                 corner = pick_corner(px, py, cx, cy, cw, ch);
210                 button = e->data.x.e->xbutton.button;
211             }
212         }
213         fire_button(MouseAction_Press,
214                     engine_get_context(e->data.x.client,
215                                        e->data.x.e->xbutton.window),
216                     e->data.x.client, e->data.x.e->xbutton.state,
217                     e->data.x.e->xbutton.button);
218         break;
219
220     case Event_X_ButtonRelease:
221         if (e->data.x.e->xbutton.button == button) {
222             /* end drags */
223             if (drag) {
224                 fire_motion(MouseAction_Motion,
225                             engine_get_context(e->data.x.client,
226                                                e->data.x.e->xbutton.window),
227                             e->data.x.client, e->data.x.e->xbutton.state,
228                             e->data.x.e->xbutton.button,
229                             cx, cy, cw, ch, dx, dy, TRUE, corner);
230                 drag = FALSE;
231             }
232
233             /* clicks are only valid if its released over the window */
234             if (e->data.x.e->xbutton.x >= 0 && e->data.x.e->xbutton.y >= 0) {
235                 int junk;
236                 Window wjunk;
237                 guint ujunk, w, h;
238                 XGetGeometry(ob_display, e->data.x.e->xbutton.window,
239                              &wjunk, &junk, &junk, &w, &h, &ujunk, &ujunk);
240                 if (e->data.x.e->xbutton.x < (signed)w &&
241                     e->data.x.e->xbutton.y < (signed)h) {
242                     click =TRUE;
243                     /* double clicks happen if there were 2 in a row! */
244                     if (lbutton == button &&
245                         e->data.x.e->xbutton.time - 300 <= ltime)
246                         dclick = TRUE;
247                 }
248                 lbutton = button;
249             } else
250                 lbutton = 0;
251
252             button = 0;
253             ltime = e->data.x.e->xbutton.time;
254         }
255         fire_button(MouseAction_Press,
256                     engine_get_context(e->data.x.client,
257                                        e->data.x.e->xbutton.window),
258                     e->data.x.client, e->data.x.e->xbutton.state,
259                     e->data.x.e->xbutton.button);
260         if (click)
261             fire_button(MouseAction_Click,
262                         engine_get_context(e->data.x.client,
263                                            e->data.x.e->xbutton.window),
264                         e->data.x.client, e->data.x.e->xbutton.state,
265                         e->data.x.e->xbutton.button);
266         if (dclick)
267             fire_button(MouseAction_DClick,
268                         engine_get_context(e->data.x.client,
269                                            e->data.x.e->xbutton.window),
270                         e->data.x.client, e->data.x.e->xbutton.state,
271                         e->data.x.e->xbutton.button);
272         break;
273
274     case Event_X_MotionNotify:
275         if (button) {
276             drag = TRUE;
277             dx = e->data.x.e->xmotion.x_root - px;
278             dy = e->data.x.e->xmotion.y_root - py;
279             fire_motion(MouseAction_Motion,
280                         engine_get_context(e->data.x.client,
281                                            e->data.x.e->xbutton.window),
282                         e->data.x.client, e->data.x.e->xmotion.state,
283                         button, cx, cy, cw, ch, dx, dy, FALSE, corner);
284         }
285         break;
286
287     default:
288         g_assert_not_reached();
289     }
290 }
291
292 static gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
293                       Action *action)
294 {
295     guint state, button;
296     GQuark context;
297     MouseBinding *b;
298     GSList *it;
299     guint i;
300
301     if (!translate_button(buttonstr, &state, &button)) {
302         g_warning("invalid button");
303         return FALSE;
304     }
305
306     context = g_quark_try_string(contextstr);
307     if (!context) {
308         g_warning("invalid context");
309         return FALSE;
310     }
311
312     for (it = g_datalist_id_get_data(&bound_contexts, context);
313          it != NULL; it = it->next){
314         b = it->data;
315         if (b->state == state && b->button == button) {
316             /* already bound */
317             if (b->action[mact] != NULL) {
318                 g_warning("duplicate binding");
319                 return FALSE;
320             }
321             b->action[mact] = action;
322             return TRUE;
323         }
324     }
325
326     grab_all_clients(FALSE);
327
328     /* add the binding */
329     b = g_new(MouseBinding, 1);
330     b->state = state;
331     b->button = button;
332     for (i = 0; i < NUM_MOUSEACTION; ++i)
333         b->action[i] = NULL;
334     b->action[mact] = action;
335     g_datalist_id_set_data(&bound_contexts, context, 
336         g_slist_append(g_datalist_id_get_data(&bound_contexts, context), b));
337
338     grab_all_clients(TRUE);
339
340     return TRUE;
341 }
342
343 static void binddef()
344 {
345     Action *a;
346
347     /* When creating an Action struct, all of the data elements in the
348        appropriate struct need to be set, except the Client*, which will be set
349        at call-time when then action function is used.
350
351        For action_move and action_resize, the 'x', 'y', and 'final' data
352        elements should not be set, as they are set at call-time.       
353     */
354
355     a = action_new(action_move);
356     mbind("1", "titlebar", MouseAction_Motion, a);
357     a = action_new(action_move);
358     mbind("1", "handle", MouseAction_Motion, a);
359     a = action_new(action_move);
360     mbind("A-1", "frame", MouseAction_Motion, a);
361
362     a = action_new(action_resize);
363     mbind("1", "blcorner", MouseAction_Motion, a);
364     a = action_new(action_resize);
365     mbind("1", "brcorner", MouseAction_Motion, a);
366     a = action_new(action_resize);
367     mbind("A-3", "frame", MouseAction_Motion, a);
368
369     a = action_new(action_toggle_shade);
370     mbind("1", "titlebar", MouseAction_DClick, a);
371     a = action_new(action_shade);
372     mbind("4", "titlebar", MouseAction_Press, a);
373     a = action_new(action_unshade);
374     mbind("5", "titlebar", MouseAction_Click, a);
375 }
376
377 void plugin_startup()
378 {
379     dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
380                       Event_X_ButtonPress | Event_X_ButtonRelease |
381                       Event_X_MotionNotify, (EventHandler)event, NULL);
382
383     /* XXX parse a config */
384     binddef();
385 }
386
387 void plugin_shutdown()
388 {
389     dispatch_register(0, (EventHandler)event, NULL);
390
391     grab_all_clients(FALSE);
392     g_datalist_foreach(&bound_contexts, foreach_clear, NULL);
393 }