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;
18 <context name="Titlebar">
19 <mousebind button="Left" action="Press">
20 <action name="Raise"></action>
26 static void parse_xml(xmlDocPtr doc, xmlNodePtr node, void *d)
28 xmlNodePtr n, nbut, nact;
34 node = node->xmlChildrenNode;
36 if ((n = parse_find_node("dragThreshold", node)))
37 threshold = parse_int(doc, n);
38 if ((n = parse_find_node("doubleClickTime", node)))
39 dclicktime = parse_int(doc, n);
41 n = parse_find_node("context", node);
43 if (!parse_attr_string("name", n, &contextstr))
45 nbut = parse_find_node("mousebind", n->xmlChildrenNode);
47 if (!parse_attr_string("button", nbut, &buttonstr))
49 if (parse_attr_contains("press", nbut, "action"))
50 mact = MouseAction_Press;
51 else if (parse_attr_contains("release", nbut, "action"))
52 mact = MouseAction_Release;
53 else if (parse_attr_contains("click", nbut, "action"))
54 mact = MouseAction_Click;
55 else if (parse_attr_contains("doubleclick", nbut,"action"))
56 mact = MouseAction_DClick;
57 else if (parse_attr_contains("drag", nbut, "action"))
58 mact = MouseAction_Motion;
61 nact = parse_find_node("action", nbut->xmlChildrenNode);
63 if ((action = action_parse(doc, nact))) {
64 /* validate that its okay for a mouse binding*/
65 if (mact == MouseAction_Motion) {
66 if (action->func != action_moveresize ||
67 action->data.moveresize.corner ==
68 prop_atoms.net_wm_moveresize_move_keyboard ||
69 action->data.moveresize.corner ==
70 prop_atoms.net_wm_moveresize_size_keyboard) {
75 if (action->func == action_moveresize &&
76 action->data.moveresize.corner !=
77 prop_atoms.net_wm_moveresize_move_keyboard &&
78 action->data.moveresize.corner !=
79 prop_atoms.net_wm_moveresize_size_keyboard) {
85 mbind(buttonstr, contextstr, mact, action);
87 nact = parse_find_node("action", nact->next);
91 nbut = parse_find_node("mousebind", nbut->next);
95 n = parse_find_node("context", n->next);
99 void plugin_setup_config()
103 parse_register("mouse", parse_xml, NULL);
106 /* Array of GSList*s of PointerBinding*s. */
107 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
109 static void grab_for_client(ObClient *client, gboolean grab)
114 for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
115 for (it = bound_contexts[i]; it != NULL; it = it->next) {
116 /* grab/ungrab the button */
117 MouseBinding *b = it->data;
122 if (i == OB_FRAME_CONTEXT_FRAME) {
123 win = client->frame->window;
124 mode = GrabModeAsync;
125 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
126 } else if (i == OB_FRAME_CONTEXT_CLIENT) {
127 win = client->frame->plate;
128 mode = GrabModeSync; /* this is handled in event */
129 mask = ButtonPressMask; /* can't catch more than this with Sync
130 mode the release event is
131 manufactured in event() */
135 grab_button_full(b->button, b->state, win, mask, mode, None);
137 ungrab_button(b->button, b->state, win);
141 static void grab_all_clients(gboolean grab)
145 for (it = client_list; it != NULL; it = it->next)
146 grab_for_client(it->data, grab);
149 static void clearall()
154 for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
155 for (it = bound_contexts[i]; it != NULL; it = it->next) {
158 MouseBinding *b = it->data;
159 for (j = 0; j < NUM_MOUSEACTION; ++j) {
161 for (it = b->actions[j]; it; it = it->next) {
162 action_free(it->data);
164 g_slist_free(b->actions[j]);
168 g_slist_free(bound_contexts[i]);
172 static void fire_button(MouseAction a, ObFrameContext context,
173 ObClient *c, guint state,
174 guint button, int x, int y)
179 for (it = bound_contexts[context]; it != NULL; it = it->next) {
181 if (b->state == state && b->button == button)
184 /* if not bound, then nothing to do! */
185 if (it == NULL) return;
187 for (it = b->actions[a]; it; it = it->next) {
188 Action *act = it->data;
189 if (act->func != NULL) {
192 g_assert(act->func != action_moveresize);
194 if (act->func == action_showmenu) {
195 act->data.showmenu.x = x;
196 act->data.showmenu.y = y;
199 act->func(&act->data);
204 static void fire_motion(MouseAction a, ObFrameContext context, ObClient *c,
205 guint state, guint button, int x_root, int y_root,
211 for (it = bound_contexts[context]; it != NULL; it = it->next) {
213 if (b->state == state && b->button == button)
216 /* if not bound, then nothing to do! */
217 if (it == NULL) return;
219 for (it = b->actions[a]; it; it = it->next) {
220 Action *act = it->data;
221 if (act->func != NULL) {
224 if (act->func == action_moveresize) {
225 act->data.moveresize.x = x_root;
226 act->data.moveresize.y = y_root;
227 act->data.moveresize.button = button;
228 if (!(act->data.moveresize.corner ==
229 prop_atoms.net_wm_moveresize_move ||
230 act->data.moveresize.corner ==
231 prop_atoms.net_wm_moveresize_move_keyboard ||
232 act->data.moveresize.corner ==
233 prop_atoms.net_wm_moveresize_size_keyboard))
234 act->data.moveresize.corner = corner;
236 g_assert_not_reached();
238 act->func(&act->data);
243 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
245 if (x - cx < cw / 2) {
247 return prop_atoms.net_wm_moveresize_size_topleft;
249 return prop_atoms.net_wm_moveresize_size_bottomleft;
252 return prop_atoms.net_wm_moveresize_size_topright;
254 return prop_atoms.net_wm_moveresize_size_bottomright;
258 static void event(ObEvent *e, void *foo)
261 static guint button = 0, state = 0, lbutton = 0;
263 gboolean click = FALSE;
264 gboolean dclick = FALSE;
265 ObFrameContext context;
268 case Event_Client_Mapped:
269 grab_for_client(e->data.c.client, TRUE);
272 case Event_Client_Destroy:
273 grab_for_client(e->data.c.client, FALSE);
276 case Event_X_ButtonPress:
277 context = frame_context(e->data.x.client,
278 e->data.x.e->xbutton.window);
280 px = e->data.x.e->xbutton.x_root;
281 py = e->data.x.e->xbutton.y_root;
282 button = e->data.x.e->xbutton.button;
283 state = e->data.x.e->xbutton.state;
285 fire_button(MouseAction_Press, context,
286 e->data.x.client, e->data.x.e->xbutton.state,
287 e->data.x.e->xbutton.button,
288 e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
290 if (context == OB_FRAME_CONTEXT_CLIENT) {
291 /* Replay the event, so it goes to the client*/
292 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
293 /* Fall through to the release case! */
297 case Event_X_ButtonRelease:
298 context = frame_context(e->data.x.client,
299 e->data.x.e->xbutton.window);
300 if (e->data.x.e->xbutton.button == button) {
301 /* clicks are only valid if its released over the window */
304 guint ujunk, b, w, h;
305 XGetGeometry(ob_display, e->data.x.e->xbutton.window,
306 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
307 if (e->data.x.e->xbutton.x >= (signed)-b &&
308 e->data.x.e->xbutton.y >= (signed)-b &&
309 e->data.x.e->xbutton.x < (signed)(w+b) &&
310 e->data.x.e->xbutton.y < (signed)(h+b)) {
312 /* double clicks happen if there were 2 in a row! */
313 if (lbutton == button &&
314 e->data.x.e->xbutton.time - dclicktime <= ltime) {
324 ltime = e->data.x.e->xbutton.time;
326 fire_button(MouseAction_Release, context,
327 e->data.x.client, e->data.x.e->xbutton.state,
328 e->data.x.e->xbutton.button,
329 e->data.x.e->xbutton.x_root, e->data.x.e->xbutton.y_root);
331 fire_button(MouseAction_Click, context,
332 e->data.x.client, e->data.x.e->xbutton.state,
333 e->data.x.e->xbutton.button,
334 e->data.x.e->xbutton.x_root,
335 e->data.x.e->xbutton.y_root);
337 fire_button(MouseAction_DClick, context,
338 e->data.x.client, e->data.x.e->xbutton.state,
339 e->data.x.e->xbutton.button,
340 e->data.x.e->xbutton.x_root,
341 e->data.x.e->xbutton.y_root);
344 case Event_X_MotionNotify:
346 if (ABS(e->data.x.e->xmotion.x_root - px) >= threshold ||
347 ABS(e->data.x.e->xmotion.y_root - py) >= threshold) {
350 context = frame_context(e->data.x.client,
351 e->data.x.e->xmotion.window);
353 /* You can't drag on buttons */
354 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
355 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
356 context == OB_FRAME_CONTEXT_SHADE ||
357 context == OB_FRAME_CONTEXT_ICONIFY ||
358 context == OB_FRAME_CONTEXT_ICON ||
359 context == OB_FRAME_CONTEXT_CLOSE)
362 if (!e->data.x.client)
363 corner = prop_atoms.net_wm_moveresize_size_bottomright;
366 pick_corner(e->data.x.e->xmotion.x_root,
367 e->data.x.e->xmotion.y_root,
368 e->data.x.client->frame->area.x,
369 e->data.x.client->frame->area.y,
370 /* use the client size because the frame
371 can be differently sized (shaded
372 windows) and we want this based on the
374 e->data.x.client->area.width +
375 e->data.x.client->frame->size.left +
376 e->data.x.client->frame->size.right,
377 e->data.x.client->area.height +
378 e->data.x.client->frame->size.top +
379 e->data.x.client->frame->size.bottom);
380 fire_motion(MouseAction_Motion, context,
381 e->data.x.client, state, button,
382 e->data.x.e->xmotion.x_root,
383 e->data.x.e->xmotion.y_root, corner);
391 g_assert_not_reached();
395 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
399 ObFrameContext context;
403 if (!translate_button(buttonstr, &state, &button)) {
404 g_warning("invalid button '%s'", buttonstr);
408 contextstr = g_ascii_strdown(contextstr, -1);
409 context = frame_context_from_string(contextstr);
411 g_warning("invalid context '%s'", contextstr);
417 for (it = bound_contexts[context]; it != NULL; it = it->next){
419 if (b->state == state && b->button == button) {
420 b->actions[mact] = g_slist_append(b->actions[mact], action);
425 grab_all_clients(FALSE);
427 /* add the binding */
428 b = g_new0(MouseBinding, 1);
431 b->actions[mact] = g_slist_append(NULL, action);
432 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
434 grab_all_clients(TRUE);
439 void plugin_startup()
441 dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
442 Event_X_ButtonPress | Event_X_ButtonRelease |
443 Event_X_MotionNotify, (EventHandler)event, NULL);
446 void plugin_shutdown()
448 dispatch_register(0, (EventHandler)event, NULL);
450 grab_all_clients(FALSE);