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/frame.h"
7 #include "kernel/grab.h"
8 #include "kernel/engine.h"
9 #include "kernel/parse.h"
10 #include "kernel/frame.h"
11 #include "translate.h"
13 #include "mouseparse.h"
17 static int dclicktime;
19 static void parse_assign(char *name, ParseToken *value)
21 if (!g_ascii_strcasecmp(name, "dragthreshold")) {
22 if (value->type != TOKEN_INTEGER)
23 yyerror("invalid value");
25 if (value->data.integer >= 0)
26 threshold = value->data.integer;
28 } else if (!g_ascii_strcasecmp(name, "doubleclicktime")) {
29 if (value->type != TOKEN_INTEGER)
30 yyerror("invalid value");
32 if (value->data.integer >= 0)
33 dclicktime = value->data.integer;
36 yyerror("invalid option");
37 parse_free_token(value);
40 void plugin_setup_config()
44 parse_reg_section("mouse", mouseparse, parse_assign);
47 /* Array of GSList*s of PointerBinding*s. */
48 static GSList *bound_contexts[NUM_CONTEXTS];
50 static void grab_for_client(Client *client, gboolean grab)
55 for (i = 0; i < NUM_CONTEXTS; ++i)
56 for (it = bound_contexts[i]; it != NULL; it = it->next) {
57 /* grab/ungrab the button */
58 MouseBinding *b = it->data;
63 if (i == Context_Frame) {
64 win = client->frame->window;
66 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
67 } else if (i == Context_Client) {
68 win = client->frame->plate;
69 mode = GrabModeSync; /* this is handled in event */
70 mask = ButtonPressMask; /* can't catch more than this with Sync
71 mode the release event is
72 manufactured in event() */
76 grab_button(b->button, b->state, win, mask, mode);
78 ungrab_button(b->button, b->state, win);
82 static void grab_all_clients(gboolean grab)
86 for (it = client_list; it != NULL; it = it->next)
87 grab_for_client(it->data, grab);
90 static void clearall()
95 for(i = 0; i < NUM_CONTEXTS; ++i) {
96 for (it = bound_contexts[i]; it != NULL; it = it->next) {
99 MouseBinding *b = it->data;
100 for (j = 0; j < NUM_MOUSEACTION; ++j)
101 if (b->action[j] != NULL)
102 action_free(b->action[j]);
105 g_slist_free(bound_contexts[i]);
109 static void fire_button(MouseAction a, Context context, Client *c, guint state,
115 for (it = bound_contexts[context]; it != NULL; it = it->next) {
117 if (b->state == state && b->button == button)
120 /* if not bound, then nothing to do! */
121 if (it == NULL) return;
123 if (b->action[a] != NULL && b->action[a]->func != NULL) {
124 b->action[a]->data.any.c = c;
126 g_assert(!(b->action[a]->func == action_move ||
127 b->action[a]->func == action_resize));
129 b->action[a]->func(&b->action[a]->data);
133 /* corner should be the opposite corner of the window in which the pointer
134 clicked, Corner_TopLeft if a good default if there is no client
135 Returns True or False for if a binding existed for the action or not.
137 static gboolean fire_motion(MouseAction a, Context context, Client *c,
138 guint state, guint button, int cx, int cy,
139 int cw, int ch, int dx, int dy,
140 gboolean final, Corner corner)
145 for (it = bound_contexts[context]; it != NULL; it = it->next) {
147 if (b->state == state && b->button == button)
150 /* if not bound, then nothing to do! */
151 if (it == NULL) return FALSE;
153 if (b->action[a] != NULL && b->action[a]->func != NULL) {
154 b->action[a]->data.any.c = c;
156 if (b->action[a]->func == action_move) {
157 b->action[a]->data.move.x = cx + dx;
158 b->action[a]->data.move.y = cy + dy;
159 b->action[a]->data.move.final = final;
160 } else if (b->action[a]->func == action_resize) {
161 b->action[a]->data.resize.corner = corner;
164 b->action[a]->data.resize.x = cw + dx;
165 b->action[a]->data.resize.y = ch + dy;
167 case Corner_TopRight:
168 b->action[a]->data.resize.x = cw - dx;
169 b->action[a]->data.resize.y = ch + dy;
171 case Corner_BottomLeft:
172 b->action[a]->data.resize.x = cw + dx;
173 b->action[a]->data.resize.y = ch - dy;
175 case Corner_BottomRight:
176 b->action[a]->data.resize.x = cw - dx;
177 b->action[a]->data.resize.y = ch - dy;
180 b->action[a]->data.resize.final = final;
182 b->action[a]->func(&b->action[a]->data);
188 static Corner pick_corner(int x, int y, int cx, int cy, int cw, int ch)
190 if (x - cx < cw / 2) {
192 return Corner_BottomRight;
194 return Corner_TopRight;
197 return Corner_BottomLeft;
199 return Corner_TopLeft;
203 static void event(ObEvent *e, void *foo)
206 static int px, py, cx, cy, cw, ch, dx, dy;
207 static guint button = 0, lbutton = 0;
208 static gboolean drag = FALSE, drag_used = FALSE;
209 static Corner corner = Corner_TopLeft;
210 gboolean click = FALSE;
211 gboolean dclick = FALSE;
215 case Event_Client_Mapped:
216 grab_for_client(e->data.c.client, TRUE);
219 case Event_Client_Destroy:
220 grab_for_client(e->data.c.client, FALSE);
223 case Event_X_ButtonPress:
225 if (e->data.x.client != NULL) {
226 cx = e->data.x.client->frame->area.x;
227 cy = e->data.x.client->frame->area.y;
228 /* use the client size because the frame can be differently
229 sized (shaded windows) and we want this based on the clients
231 cw = e->data.x.client->area.width +
232 e->data.x.client->frame->size.left +
233 e->data.x.client->frame->size.right;
234 ch = e->data.x.client->area.height +
235 e->data.x.client->frame->size.top +
236 e->data.x.client->frame->size.bottom;
237 px = e->data.x.e->xbutton.x_root;
238 py = e->data.x.e->xbutton.y_root;
239 corner = pick_corner(px, py, cx, cy, cw, ch);
241 button = e->data.x.e->xbutton.button;
243 context = engine_get_context(e->data.x.client,
244 e->data.x.e->xbutton.window);
246 fire_button(MouseAction_Press, context,
247 e->data.x.client, e->data.x.e->xbutton.state,
248 e->data.x.e->xbutton.button);
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! */
257 case Event_X_ButtonRelease:
258 context = engine_get_context(e->data.x.client,
259 e->data.x.e->xbutton.window);
260 if (e->data.x.e->xbutton.button == button) {
263 fire_motion(MouseAction_Motion, context,
264 e->data.x.client, e->data.x.e->xbutton.state,
265 e->data.x.e->xbutton.button,
266 cx, cy, cw, ch, dx, dy, TRUE, corner);
267 drag = drag_used = FALSE;
271 /* clicks are only valid if its released over the window */
274 guint ujunk, b, w, h;
275 XGetGeometry(ob_display, e->data.x.e->xbutton.window,
276 &wjunk, &junk, &junk, &w, &h, &b, &ujunk);
277 if (e->data.x.e->xbutton.x >= (signed)-b &&
278 e->data.x.e->xbutton.y >= (signed)-b &&
279 e->data.x.e->xbutton.x < (signed)(w+b) &&
280 e->data.x.e->xbutton.y < (signed)(h+b)) {
282 /* double clicks happen if there were 2 in a row! */
283 if (lbutton == button &&
284 e->data.x.e->xbutton.time - dclicktime <= ltime) {
294 ltime = e->data.x.e->xbutton.time;
296 fire_button(MouseAction_Release, context,
297 e->data.x.client, e->data.x.e->xbutton.state,
298 e->data.x.e->xbutton.button);
300 fire_button(MouseAction_Click, context,
301 e->data.x.client, e->data.x.e->xbutton.state,
302 e->data.x.e->xbutton.button);
304 fire_button(MouseAction_DClick, context,
305 e->data.x.client, e->data.x.e->xbutton.state,
306 e->data.x.e->xbutton.button);
309 case Event_X_MotionNotify:
311 dx = e->data.x.e->xmotion.x_root - px;
312 dy = e->data.x.e->xmotion.y_root - py;
314 (ABS(dx) >= threshold || ABS(dy) >= threshold))
317 context = engine_get_context(e->data.x.client,
318 e->data.x.e->xbutton.window);
319 drag_used = fire_motion(MouseAction_Motion, context,
321 e->data.x.e->xmotion.state,
322 button, cx, cy, cw, ch, dx, dy,
329 g_assert_not_reached();
333 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
341 if (!translate_button(buttonstr, &state, &button)) {
342 g_warning("invalid button '%s'", buttonstr);
346 contextstr = g_ascii_strdown(contextstr, -1);
347 context = frame_context_from_string(contextstr);
349 g_warning("invalid context '%s'", contextstr);
355 for (it = bound_contexts[context]; it != NULL; it = it->next){
357 if (b->state == state && b->button == button) {
359 if (b->action[mact] != NULL) {
360 g_warning("duplicate binding");
363 b->action[mact] = action;
368 grab_all_clients(FALSE);
370 /* add the binding */
371 b = g_new0(MouseBinding, 1);
374 b->action[mact] = action;
375 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
377 grab_all_clients(TRUE);
382 void plugin_startup()
384 dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
385 Event_X_ButtonPress | Event_X_ButtonRelease |
386 Event_X_MotionNotify, (EventHandler)event, NULL);
389 void plugin_shutdown()
391 dispatch_register(0, (EventHandler)event, NULL);
393 grab_all_clients(FALSE);