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"
11 #include "mouseparse.h"
15 static int dclicktime;
17 static void parse_assign(char *name, ParseToken *value)
19 if (!g_ascii_strcasecmp(name, "dragthreshold")) {
20 if (value->type != TOKEN_INTEGER)
21 yyerror("invalid value");
23 if (value->data.integer >= 0)
24 threshold = value->data.integer;
26 } else if (!g_ascii_strcasecmp(name, "doubleclicktime")) {
27 if (value->type != TOKEN_INTEGER)
28 yyerror("invalid value");
30 if (value->data.integer >= 0)
31 dclicktime = value->data.integer;
34 yyerror("invalid option");
35 parse_free_token(value);
38 void plugin_setup_config()
42 parse_reg_section("mouse", mouseparse, parse_assign);
45 /* Array of GSList*s of PointerBinding*s. */
46 static GSList *bound_contexts[NUM_CONTEXTS];
48 static void grab_for_client(Client *client, gboolean grab)
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;
61 if (i == Context_Frame) {
62 win = client->frame->window;
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() */
74 grab_button(b->button, b->state, win, mask, mode);
76 ungrab_button(b->button, b->state, win);
80 static void grab_all_clients(gboolean grab)
84 for (it = client_list; it != NULL; it = it->next)
85 grab_for_client(it->data, grab);
88 static void clearall()
93 for(i = 0; i < NUM_CONTEXTS; ++i) {
94 for (it = bound_contexts[i]; it != NULL; it = it->next) {
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]);
103 g_slist_free(bound_contexts[i]);
107 static void fire_button(MouseAction a, Context context, Client *c, guint state,
108 guint button, int x, int y)
113 for (it = bound_contexts[context]; it != NULL; it = it->next) {
115 if (b->state == state && b->button == button)
118 /* if not bound, then nothing to do! */
119 if (it == NULL) return;
121 if (b->action[a] != NULL && b->action[a]->func != NULL) {
122 b->action[a]->data.any.c = c;
124 g_assert(!(b->action[a]->func == action_move ||
125 b->action[a]->func == action_resize));
127 if (b->action[a]->func == action_showmenu) {
128 b->action[a]->data.showmenu.x = x;
129 b->action[a]->data.showmenu.y = y;
132 b->action[a]->func(&b->action[a]->data);
136 /* corner should be the opposite corner of the window in which the pointer
137 clicked, Corner_TopLeft if a good default if there is no client
138 Returns True or False for if a binding existed for the action or not.
140 static gboolean fire_motion(MouseAction a, Context context, Client *c,
141 guint state, guint button, int cx, int cy,
142 int cw, int ch, int dx, int dy,
143 gboolean final, Corner corner)
148 for (it = bound_contexts[context]; it != NULL; it = it->next) {
150 if (b->state == state && b->button == button)
153 /* if not bound, then nothing to do! */
154 if (it == NULL) return FALSE;
156 if (b->action[a] != NULL && b->action[a]->func != NULL) {
157 b->action[a]->data.any.c = c;
159 if (b->action[a]->func == action_move) {
160 b->action[a]->data.move.x = cx + dx;
161 b->action[a]->data.move.y = cy + dy;
162 b->action[a]->data.move.final = final;
163 } else if (b->action[a]->func == action_resize) {
164 b->action[a]->data.resize.corner = corner;
167 b->action[a]->data.resize.x = cw + dx;
168 b->action[a]->data.resize.y = ch + dy;
170 case Corner_TopRight:
171 b->action[a]->data.resize.x = cw - dx;
172 b->action[a]->data.resize.y = ch + dy;
174 case Corner_BottomLeft:
175 b->action[a]->data.resize.x = cw + dx;
176 b->action[a]->data.resize.y = ch - dy;
178 case Corner_BottomRight:
179 b->action[a]->data.resize.x = cw - dx;
180 b->action[a]->data.resize.y = ch - dy;
183 b->action[a]->data.resize.final = final;
185 g_assert_not_reached();
186 b->action[a]->func(&b->action[a]->data);
192 static Corner pick_corner(int x, int y, int cx, int cy, int cw, int ch)
194 if (x - cx < cw / 2) {
196 return Corner_BottomRight;
198 return Corner_TopRight;
201 return Corner_BottomLeft;
203 return Corner_TopLeft;
207 static void event(ObEvent *e, void *foo)
210 static int px, py, cx, cy, cw, ch, dx, dy;
211 static guint button = 0, state = 0, lbutton = 0;
212 static gboolean drag = FALSE, drag_used = FALSE;
213 static Corner corner = Corner_TopLeft;
214 gboolean click = FALSE;
215 gboolean dclick = FALSE;
219 case Event_Client_Mapped:
220 grab_for_client(e->data.c.client, TRUE);
223 case Event_Client_Destroy:
224 grab_for_client(e->data.c.client, FALSE);
227 case Event_X_ButtonPress:
229 if (e->data.x.client != NULL) {
230 cx = e->data.x.client->frame->area.x;
231 cy = e->data.x.client->frame->area.y;
232 /* use the client size because the frame can be differently
233 sized (shaded windows) and we want this based on the clients
235 cw = e->data.x.client->area.width +
236 e->data.x.client->frame->size.left +
237 e->data.x.client->frame->size.right;
238 ch = e->data.x.client->area.height +
239 e->data.x.client->frame->size.top +
240 e->data.x.client->frame->size.bottom;
241 px = e->data.x.e->xbutton.x_root;
242 py = e->data.x.e->xbutton.y_root;
243 corner = pick_corner(px, py, cx, cy, cw, ch);
245 button = e->data.x.e->xbutton.button;
246 state = e->data.x.e->xbutton.state;
248 context = frame_context(e->data.x.client,
249 e->data.x.e->xbutton.window);
251 fire_button(MouseAction_Press, context,
252 e->data.x.client, e->data.x.e->xbutton.state,
253 e->data.x.e->xbutton.button,
254 e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
256 if (context == Context_Client) {
257 /* Replay the event, so it goes to the client*/
258 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
259 /* Fall through to the release case! */
263 case Event_X_ButtonRelease:
264 context = frame_context(e->data.x.client,
265 e->data.x.e->xbutton.window);
266 if (e->data.x.e->xbutton.button == button) {
269 fire_motion(MouseAction_Motion, context,
270 e->data.x.client, state, button,
271 cx, cy, cw, ch, dx, dy, TRUE, corner);
272 drag = drag_used = FALSE;
276 /* clicks are only valid if its released over the window */
279 guint ujunk, b, w, h;
280 XGetGeometry(ob_display, e->data.x.e->xbutton.window,
281 &wjunk, &junk, &junk, &w, &h, &b, &ujunk);
282 if (e->data.x.e->xbutton.x >= (signed)-b &&
283 e->data.x.e->xbutton.y >= (signed)-b &&
284 e->data.x.e->xbutton.x < (signed)(w+b) &&
285 e->data.x.e->xbutton.y < (signed)(h+b)) {
287 /* double clicks happen if there were 2 in a row! */
288 if (lbutton == button &&
289 e->data.x.e->xbutton.time - dclicktime <= ltime) {
300 ltime = e->data.x.e->xbutton.time;
302 fire_button(MouseAction_Release, context,
303 e->data.x.client, e->data.x.e->xbutton.state,
304 e->data.x.e->xbutton.button,
305 e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
307 fire_button(MouseAction_Click, context,
308 e->data.x.client, e->data.x.e->xbutton.state,
309 e->data.x.e->xbutton.button,
310 e->data.x.e->xbutton.x_root,
311 e->data.x.e->xbutton.y_root);
313 fire_button(MouseAction_DClick, context,
314 e->data.x.client, e->data.x.e->xbutton.state,
315 e->data.x.e->xbutton.button,
316 e->data.x.e->xbutton.x_root,
317 e->data.x.e->xbutton.y_root);
320 case Event_X_MotionNotify:
322 dx = e->data.x.e->xmotion.x_root - px;
323 dy = e->data.x.e->xmotion.y_root - py;
325 (ABS(dx) >= threshold || ABS(dy) >= threshold))
328 context = frame_context(e->data.x.client,
329 e->data.x.e->xbutton.window);
330 drag_used = fire_motion(MouseAction_Motion, context,
332 state, button, cx, cy, cw, ch, dx, dy,
339 g_assert_not_reached();
343 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
351 if (!translate_button(buttonstr, &state, &button)) {
352 g_warning("invalid button '%s'", buttonstr);
356 contextstr = g_ascii_strdown(contextstr, -1);
357 context = frame_context_from_string(contextstr);
359 g_warning("invalid context '%s'", contextstr);
365 for (it = bound_contexts[context]; it != NULL; it = it->next){
367 if (b->state == state && b->button == button) {
369 if (b->action[mact] != NULL) {
370 g_warning("duplicate binding");
373 b->action[mact] = action;
378 grab_all_clients(FALSE);
380 /* add the binding */
381 b = g_new0(MouseBinding, 1);
384 b->action[mact] = action;
385 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
387 grab_all_clients(TRUE);
392 void plugin_startup()
394 dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
395 Event_X_ButtonPress | Event_X_ButtonRelease |
396 Event_X_MotionNotify, (EventHandler)event, NULL);
399 void plugin_shutdown()
401 dispatch_register(0, (EventHandler)event, NULL);
403 grab_all_clients(FALSE);