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/frame.h"
9 #include "parser/parse.h"
10 #include "translate.h"
15 static int dclicktime;
16 gboolean mouse_lefthand;
19 <context name="Titlebar">
20 <mousebind button="Left" action="Press">
21 <action name="Raise"></action>
27 static void parse_xml(xmlDocPtr doc, xmlNodePtr node, void *d)
29 xmlNodePtr n, nbut, nact;
35 if ((n = parse_find_node("dragThreshold", node)))
36 threshold = parse_int(doc, n);
37 if ((n = parse_find_node("doubleClickTime", node)))
38 dclicktime = parse_int(doc, n);
39 if ((n = parse_find_node("leftHanded", node)))
40 mouse_lefthand = parse_bool(doc, n);
42 n = parse_find_node("context", node);
44 if (!parse_attr_string("name", n, &contextstr))
46 nbut = parse_find_node("mousebind", n->xmlChildrenNode);
48 if (!parse_attr_string("button", nbut, &buttonstr))
50 if (parse_attr_contains("press", nbut, "action"))
51 mact = MouseAction_Press;
52 else if (parse_attr_contains("release", nbut, "action"))
53 mact = MouseAction_Release;
54 else if (parse_attr_contains("click", nbut, "action"))
55 mact = MouseAction_Click;
56 else if (parse_attr_contains("doubleclick", nbut,"action"))
57 mact = MouseAction_DClick;
58 else if (parse_attr_contains("drag", nbut, "action"))
59 mact = MouseAction_Motion;
62 nact = parse_find_node("action", nbut->xmlChildrenNode);
64 if ((action = action_parse(doc, nact))) {
65 /* validate that its okay for a mouse binding*/
66 if (mact == MouseAction_Motion) {
67 if (action->func != action_moveresize ||
68 action->data.moveresize.corner ==
69 prop_atoms.net_wm_moveresize_move_keyboard ||
70 action->data.moveresize.corner ==
71 prop_atoms.net_wm_moveresize_size_keyboard) {
76 if (action->func == action_moveresize &&
77 action->data.moveresize.corner !=
78 prop_atoms.net_wm_moveresize_move_keyboard &&
79 action->data.moveresize.corner !=
80 prop_atoms.net_wm_moveresize_size_keyboard) {
86 mbind(buttonstr, contextstr, mact, action);
88 nact = parse_find_node("action", nact->next);
92 nbut = parse_find_node("mousebind", nbut->next);
96 n = parse_find_node("context", n->next);
100 void plugin_setup_config()
104 mouse_lefthand = FALSE;
105 parse_register("mouse", parse_xml, NULL);
108 /* Array of GSList*s of PointerBinding*s. */
109 static GSList *bound_contexts[NUM_CONTEXTS];
111 static void grab_for_client(Client *client, gboolean grab)
116 for (i = 0; i < NUM_CONTEXTS; ++i)
117 for (it = bound_contexts[i]; it != NULL; it = it->next) {
118 /* grab/ungrab the button */
119 MouseBinding *b = it->data;
124 if (i == Context_Frame) {
125 win = client->frame->window;
126 mode = GrabModeAsync;
127 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
128 } else if (i == Context_Client) {
129 win = client->frame->plate;
130 mode = GrabModeSync; /* this is handled in event */
131 mask = ButtonPressMask; /* can't catch more than this with Sync
132 mode the release event is
133 manufactured in event() */
137 grab_button_full(b->button, b->state, win, mask, mode, None);
139 ungrab_button(b->button, b->state, win);
143 static void grab_all_clients(gboolean grab)
147 for (it = client_list; it != NULL; it = it->next)
148 grab_for_client(it->data, grab);
151 static void clearall()
156 for(i = 0; i < NUM_CONTEXTS; ++i) {
157 for (it = bound_contexts[i]; it != NULL; it = it->next) {
160 MouseBinding *b = it->data;
161 for (j = 0; j < NUM_MOUSEACTION; ++j) {
163 for (it = b->actions[j]; it; it = it->next) {
164 action_free(it->data);
166 g_slist_free(b->actions[j]);
170 g_slist_free(bound_contexts[i]);
174 static void fire_button(MouseAction a, Context context, Client *c, guint state,
175 guint button, int x, int y)
180 for (it = bound_contexts[context]; it != NULL; it = it->next) {
182 if (b->state == state && b->button == button)
185 /* if not bound, then nothing to do! */
186 if (it == NULL) return;
188 for (it = b->actions[a]; it; it = it->next) {
189 Action *act = it->data;
190 if (act->func != NULL) {
193 g_assert(act->func != action_moveresize);
195 if (act->func == action_showmenu) {
196 act->data.showmenu.x = x;
197 act->data.showmenu.y = y;
200 act->func(&act->data);
205 static void fire_motion(MouseAction a, Context context, Client *c,
206 guint state, guint button, int x_root, int y_root,
212 for (it = bound_contexts[context]; it != NULL; it = it->next) {
214 if (b->state == state && b->button == button)
217 /* if not bound, then nothing to do! */
218 if (it == NULL) return;
220 for (it = b->actions[a]; it; it = it->next) {
221 Action *act = it->data;
222 if (act->func != NULL) {
225 if (act->func == action_moveresize) {
226 act->data.moveresize.x = x_root;
227 act->data.moveresize.y = y_root;
228 act->data.moveresize.button = button;
229 if (!(act->data.moveresize.corner ==
230 prop_atoms.net_wm_moveresize_move ||
231 act->data.moveresize.corner ==
232 prop_atoms.net_wm_moveresize_move_keyboard ||
233 act->data.moveresize.corner ==
234 prop_atoms.net_wm_moveresize_size_keyboard))
235 act->data.moveresize.corner = corner;
237 g_assert_not_reached();
239 act->func(&act->data);
244 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
246 if (x - cx < cw / 2) {
248 return prop_atoms.net_wm_moveresize_size_topleft;
250 return prop_atoms.net_wm_moveresize_size_bottomleft;
253 return prop_atoms.net_wm_moveresize_size_topright;
255 return prop_atoms.net_wm_moveresize_size_bottomright;
259 static void event(ObEvent *e, void *foo)
262 static guint button = 0, state = 0, lbutton = 0;
264 gboolean click = FALSE;
265 gboolean dclick = FALSE;
269 case Event_Client_Mapped:
270 grab_for_client(e->data.c.client, TRUE);
273 case Event_Client_Destroy:
274 grab_for_client(e->data.c.client, FALSE);
277 case Event_X_ButtonPress:
278 context = frame_context(e->data.x.client,
279 e->data.x.e->xbutton.window);
282 px = e->data.x.e->xbutton.x_root;
283 py = e->data.x.e->xbutton.y_root;
284 button = e->data.x.e->xbutton.button;
285 state = e->data.x.e->xbutton.state;
288 fire_button(MouseAction_Press, context,
289 e->data.x.client, e->data.x.e->xbutton.state,
290 e->data.x.e->xbutton.button,
291 e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
293 if (context == Context_Client) {
294 /* Replay the event, so it goes to the client*/
295 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
296 /* Fall through to the release case! */
300 case Event_X_ButtonRelease:
301 context = frame_context(e->data.x.client,
302 e->data.x.e->xbutton.window);
303 if (e->data.x.e->xbutton.button == button) {
304 /* clicks are only valid if its released over the window */
307 guint ujunk, b, w, h;
308 XGetGeometry(ob_display, e->data.x.e->xbutton.window,
309 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
310 if (e->data.x.e->xbutton.x >= (signed)-b &&
311 e->data.x.e->xbutton.y >= (signed)-b &&
312 e->data.x.e->xbutton.x < (signed)(w+b) &&
313 e->data.x.e->xbutton.y < (signed)(h+b)) {
315 /* double clicks happen if there were 2 in a row! */
316 if (lbutton == button &&
317 e->data.x.e->xbutton.time - dclicktime <= ltime) {
327 ltime = e->data.x.e->xbutton.time;
329 fire_button(MouseAction_Release, context,
330 e->data.x.client, e->data.x.e->xbutton.state,
331 e->data.x.e->xbutton.button,
332 e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
334 fire_button(MouseAction_Click, context,
335 e->data.x.client, e->data.x.e->xbutton.state,
336 e->data.x.e->xbutton.button,
337 e->data.x.e->xbutton.x_root,
338 e->data.x.e->xbutton.y_root);
340 fire_button(MouseAction_DClick, context,
341 e->data.x.client, e->data.x.e->xbutton.state,
342 e->data.x.e->xbutton.button,
343 e->data.x.e->xbutton.x_root,
344 e->data.x.e->xbutton.y_root);
347 case Event_X_MotionNotify:
349 if (ABS(e->data.x.e->xmotion.x_root - px) >= threshold ||
350 ABS(e->data.x.e->xmotion.y_root - py) >= threshold) {
353 context = frame_context(e->data.x.client,
354 e->data.x.e->xmotion.window);
356 /* You can't drag on buttons */
357 if (context == Context_Maximize ||
358 context == Context_AllDesktops ||
359 context == Context_Shade ||
360 context == Context_Iconify ||
361 context == Context_Icon ||
362 context == Context_Close)
365 if (!e->data.x.client)
366 corner = prop_atoms.net_wm_moveresize_size_bottomright;
369 pick_corner(e->data.x.e->xmotion.x_root,
370 e->data.x.e->xmotion.y_root,
371 e->data.x.client->frame->area.x,
372 e->data.x.client->frame->area.y,
373 /* use the client size because the frame
374 can be differently sized (shaded
375 windows) and we want this based on the
377 e->data.x.client->area.width +
378 e->data.x.client->frame->size.left +
379 e->data.x.client->frame->size.right,
380 e->data.x.client->area.height +
381 e->data.x.client->frame->size.top +
382 e->data.x.client->frame->size.bottom);
383 fire_motion(MouseAction_Motion, context,
384 e->data.x.client, state, button,
385 e->data.x.e->xmotion.x_root,
386 e->data.x.e->xmotion.y_root, corner);
394 g_assert_not_reached();
398 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
406 if (!translate_button(buttonstr, &state, &button)) {
407 g_warning("invalid button '%s'", buttonstr);
411 contextstr = g_ascii_strdown(contextstr, -1);
412 context = frame_context_from_string(contextstr);
414 g_warning("invalid context '%s'", contextstr);
420 for (it = bound_contexts[context]; it != NULL; it = it->next){
422 if (b->state == state && b->button == button) {
423 b->actions[mact] = g_slist_append(b->actions[mact], action);
428 grab_all_clients(FALSE);
430 /* add the binding */
431 b = g_new0(MouseBinding, 1);
434 b->actions[mact] = g_slist_append(NULL, action);
435 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
437 grab_all_clients(TRUE);
442 void plugin_startup()
444 dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
445 Event_X_ButtonPress | Event_X_ButtonRelease |
446 Event_X_MotionNotify, (EventHandler)event, NULL);
449 void plugin_shutdown()
451 dispatch_register(0, (EventHandler)event, NULL);
453 grab_all_clients(FALSE);