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