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/prop.h"
7 #include "kernel/grab.h"
8 #include "kernel/parse.h"
9 #include "kernel/frame.h"
10 #include "translate.h"
12 #include "mouseparse.h"
16 static int dclicktime;
18 static void parse_assign(char *name, ParseToken *value)
20 if (!g_ascii_strcasecmp(name, "dragthreshold")) {
21 if (value->type != TOKEN_INTEGER)
22 yyerror("invalid value");
24 if (value->data.integer >= 0)
25 threshold = value->data.integer;
27 } else if (!g_ascii_strcasecmp(name, "doubleclicktime")) {
28 if (value->type != TOKEN_INTEGER)
29 yyerror("invalid value");
31 if (value->data.integer >= 0)
32 dclicktime = value->data.integer;
35 yyerror("invalid option");
36 parse_free_token(value);
39 void plugin_setup_config()
43 parse_reg_section("mouse", mouseparse, parse_assign);
46 /* Array of GSList*s of PointerBinding*s. */
47 static GSList *bound_contexts[NUM_CONTEXTS];
49 static void grab_for_client(Client *client, gboolean grab)
54 for (i = 0; i < NUM_CONTEXTS; ++i)
55 for (it = bound_contexts[i]; it != NULL; it = it->next) {
56 /* grab/ungrab the button */
57 MouseBinding *b = it->data;
62 if (i == Context_Frame) {
63 win = client->frame->window;
65 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
66 } else if (i == Context_Client) {
67 win = client->frame->plate;
68 mode = GrabModeSync; /* this is handled in event */
69 mask = ButtonPressMask; /* can't catch more than this with Sync
70 mode the release event is
71 manufactured in event() */
75 grab_button(b->button, b->state, win, mask, mode);
77 ungrab_button(b->button, b->state, win);
81 static void grab_all_clients(gboolean grab)
85 for (it = client_list; it != NULL; it = it->next)
86 grab_for_client(it->data, grab);
89 static void clearall()
94 for(i = 0; i < NUM_CONTEXTS; ++i) {
95 for (it = bound_contexts[i]; it != NULL; it = it->next) {
98 MouseBinding *b = it->data;
99 for (j = 0; j < NUM_MOUSEACTION; ++j)
100 if (b->action[j] != NULL)
101 action_free(b->action[j]);
104 g_slist_free(bound_contexts[i]);
108 static void fire_button(MouseAction a, Context context, Client *c, guint state,
109 guint button, int x, int y)
114 for (it = bound_contexts[context]; it != NULL; it = it->next) {
116 if (b->state == state && b->button == button)
119 /* if not bound, then nothing to do! */
120 if (it == NULL) return;
122 if (b->action[a] != NULL && b->action[a]->func != NULL) {
123 b->action[a]->data.any.c = c;
125 g_assert(b->action[a]->func != action_moveresize);
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 static void fire_motion(MouseAction a, Context context, Client *c,
137 guint state, guint button, int x_root, int y_root,
143 for (it = bound_contexts[context]; it != NULL; it = it->next) {
145 if (b->state == state && b->button == button)
148 /* if not bound, then nothing to do! */
149 if (it == NULL) return;
151 if (b->action[a] != NULL && b->action[a]->func != NULL) {
152 b->action[a]->data.any.c = c;
154 if (b->action[a]->func == action_moveresize) {
155 b->action[a]->data.moveresize.x = x_root;
156 b->action[a]->data.moveresize.y = y_root;
157 b->action[a]->data.moveresize.button = button;
158 if (!(b->action[a]->data.moveresize.corner ==
159 prop_atoms.net_wm_moveresize_move ||
160 b->action[a]->data.moveresize.corner ==
161 prop_atoms.net_wm_moveresize_move_keyboard ||
162 b->action[a]->data.moveresize.corner ==
163 prop_atoms.net_wm_moveresize_size_keyboard))
164 b->action[a]->data.moveresize.corner = corner;
166 g_assert_not_reached();
168 b->action[a]->func(&b->action[a]->data);
172 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
174 if (x - cx < cw / 2) {
176 return prop_atoms.net_wm_moveresize_size_topleft;
178 return prop_atoms.net_wm_moveresize_size_bottomleft;
181 return prop_atoms.net_wm_moveresize_size_topright;
183 return prop_atoms.net_wm_moveresize_size_bottomright;
187 static void event(ObEvent *e, void *foo)
190 static guint button = 0, state = 0, lbutton = 0;
192 gboolean click = FALSE;
193 gboolean dclick = FALSE;
197 case Event_Client_Mapped:
198 grab_for_client(e->data.c.client, TRUE);
201 case Event_Client_Destroy:
202 grab_for_client(e->data.c.client, FALSE);
205 case Event_X_ButtonPress:
206 context = frame_context(e->data.x.client,
207 e->data.x.e->xbutton.window);
210 px = e->data.x.e->xbutton.x_root;
211 py = e->data.x.e->xbutton.y_root;
212 button = e->data.x.e->xbutton.button;
213 state = e->data.x.e->xbutton.state;
216 fire_button(MouseAction_Press, context,
217 e->data.x.client, e->data.x.e->xbutton.state,
218 e->data.x.e->xbutton.button,
219 e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
221 if (context == Context_Client) {
222 /* Replay the event, so it goes to the client*/
223 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
224 /* Fall through to the release case! */
228 case Event_X_ButtonRelease:
229 context = frame_context(e->data.x.client,
230 e->data.x.e->xbutton.window);
231 if (e->data.x.e->xbutton.button == button) {
232 /* clicks are only valid if its released over the window */
235 guint ujunk, b, w, h;
236 XGetGeometry(ob_display, e->data.x.e->xbutton.window,
237 &wjunk, &junk, &junk, &w, &h, &b, &ujunk);
238 if (e->data.x.e->xbutton.x >= (signed)-b &&
239 e->data.x.e->xbutton.y >= (signed)-b &&
240 e->data.x.e->xbutton.x < (signed)(w+b) &&
241 e->data.x.e->xbutton.y < (signed)(h+b)) {
243 /* double clicks happen if there were 2 in a row! */
244 if (lbutton == button &&
245 e->data.x.e->xbutton.time - dclicktime <= ltime) {
255 ltime = e->data.x.e->xbutton.time;
257 fire_button(MouseAction_Release, context,
258 e->data.x.client, e->data.x.e->xbutton.state,
259 e->data.x.e->xbutton.button,
260 e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
262 fire_button(MouseAction_Click, context,
263 e->data.x.client, e->data.x.e->xbutton.state,
264 e->data.x.e->xbutton.button,
265 e->data.x.e->xbutton.x_root,
266 e->data.x.e->xbutton.y_root);
268 fire_button(MouseAction_DClick, context,
269 e->data.x.client, e->data.x.e->xbutton.state,
270 e->data.x.e->xbutton.button,
271 e->data.x.e->xbutton.x_root,
272 e->data.x.e->xbutton.y_root);
275 case Event_X_MotionNotify:
277 if (ABS(e->data.x.e->xmotion.x_root - px) >= threshold ||
278 ABS(e->data.x.e->xmotion.y_root - py) >= threshold) {
281 if (!e->data.x.client)
282 corner = prop_atoms.net_am_moveresize_size_bottomright;
285 pick_corner(e->data.x.e->xmotion.x_root,
286 e->data.x.e->xmotion.y_root,
287 e->data.x.client->frame->area.x,
288 e->data.x.client->frame->area.y,
289 /* use the client size because the frame
290 can be differently sized (shaded
291 windows) and we want this based on the
293 e->data.x.client->area.width +
294 e->data.x.client->frame->size.left +
295 e->data.x.client->frame->size.right,
296 e->data.x.client->area.height +
297 e->data.x.client->frame->size.top +
298 e->data.x.client->frame->size.bottom);
299 context = frame_context(e->data.x.client,
300 e->data.x.e->xmotion.window);
301 fire_motion(MouseAction_Motion, context,
302 e->data.x.client, state, button,
303 e->data.x.e->xmotion.x_root,
304 e->data.x.e->xmotion.y_root, corner);
312 g_assert_not_reached();
316 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
324 if (!translate_button(buttonstr, &state, &button)) {
325 g_warning("invalid button '%s'", buttonstr);
329 contextstr = g_ascii_strdown(contextstr, -1);
330 context = frame_context_from_string(contextstr);
332 g_warning("invalid context '%s'", contextstr);
338 for (it = bound_contexts[context]; it != NULL; it = it->next){
340 if (b->state == state && b->button == button) {
342 if (b->action[mact] != NULL) {
343 g_warning("duplicate binding");
346 b->action[mact] = action;
351 grab_all_clients(FALSE);
353 /* add the binding */
354 b = g_new0(MouseBinding, 1);
357 b->action[mact] = action;
358 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
360 grab_all_clients(TRUE);
365 void plugin_startup()
367 dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
368 Event_X_ButtonPress | Event_X_ButtonRelease |
369 Event_X_MotionNotify, (EventHandler)event, NULL);
372 void plugin_shutdown()
374 dispatch_register(0, (EventHandler)event, NULL);
376 grab_all_clients(FALSE);