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 if ((n
= parse_find_node("dragThreshold", node
)))
35 threshold
= parse_int(doc
, n
);
36 if ((n
= parse_find_node("doubleClickTime", node
)))
37 dclicktime
= parse_int(doc
, n
);
39 n
= parse_find_node("context", node
);
41 if (!parse_attr_string("name", n
, &contextstr
))
43 nbut
= parse_find_node("mousebind", n
->xmlChildrenNode
);
45 if (!parse_attr_string("button", nbut
, &buttonstr
))
47 if (parse_attr_contains("press", nbut
, "action"))
48 mact
= MouseAction_Press
;
49 else if (parse_attr_contains("release", nbut
, "action"))
50 mact
= MouseAction_Release
;
51 else if (parse_attr_contains("click", nbut
, "action"))
52 mact
= MouseAction_Click
;
53 else if (parse_attr_contains("doubleclick", nbut
,"action"))
54 mact
= MouseAction_DClick
;
55 else if (parse_attr_contains("drag", nbut
, "action"))
56 mact
= MouseAction_Motion
;
59 nact
= parse_find_node("action", nbut
->xmlChildrenNode
);
61 if ((action
= action_parse(doc
, nact
))) {
62 /* validate that its okay for a mouse binding*/
63 if (mact
== MouseAction_Motion
) {
64 if (action
->func
!= action_moveresize
||
65 action
->data
.moveresize
.corner
==
66 prop_atoms
.net_wm_moveresize_move_keyboard
||
67 action
->data
.moveresize
.corner
==
68 prop_atoms
.net_wm_moveresize_size_keyboard
) {
73 if (action
->func
== action_moveresize
&&
74 action
->data
.moveresize
.corner
!=
75 prop_atoms
.net_wm_moveresize_move_keyboard
&&
76 action
->data
.moveresize
.corner
!=
77 prop_atoms
.net_wm_moveresize_size_keyboard
) {
83 mbind(buttonstr
, contextstr
, mact
, action
);
85 nact
= parse_find_node("action", nact
->next
);
89 nbut
= parse_find_node("mousebind", nbut
->next
);
93 n
= parse_find_node("context", n
->next
);
97 void plugin_setup_config()
101 parse_register("mouse", parse_xml
, NULL
);
104 /* Array of GSList*s of PointerBinding*s. */
105 static GSList
*bound_contexts
[NUM_CONTEXTS
];
107 static void grab_for_client(Client
*client
, gboolean grab
)
112 for (i
= 0; i
< NUM_CONTEXTS
; ++i
)
113 for (it
= bound_contexts
[i
]; it
!= NULL
; it
= it
->next
) {
114 /* grab/ungrab the button */
115 MouseBinding
*b
= it
->data
;
120 if (i
== Context_Frame
) {
121 win
= client
->frame
->window
;
122 mode
= GrabModeAsync
;
123 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
124 } else if (i
== Context_Client
) {
125 win
= client
->frame
->plate
;
126 mode
= GrabModeSync
; /* this is handled in event */
127 mask
= ButtonPressMask
; /* can't catch more than this with Sync
128 mode the release event is
129 manufactured in event() */
133 grab_button_full(b
->button
, b
->state
, win
, mask
, mode
, None
);
135 ungrab_button(b
->button
, b
->state
, win
);
139 static void grab_all_clients(gboolean grab
)
143 for (it
= client_list
; it
!= NULL
; it
= it
->next
)
144 grab_for_client(it
->data
, grab
);
147 static void clearall()
152 for(i
= 0; i
< NUM_CONTEXTS
; ++i
) {
153 for (it
= bound_contexts
[i
]; it
!= NULL
; it
= it
->next
) {
156 MouseBinding
*b
= it
->data
;
157 for (j
= 0; j
< NUM_MOUSEACTION
; ++j
) {
159 for (it
= b
->actions
[j
]; it
; it
= it
->next
) {
160 action_free(it
->data
);
162 g_slist_free(b
->actions
[j
]);
166 g_slist_free(bound_contexts
[i
]);
170 static void fire_button(MouseAction a
, Context context
, Client
*c
, guint state
,
171 guint button
, int x
, int y
)
176 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
178 if (b
->state
== state
&& b
->button
== button
)
181 /* if not bound, then nothing to do! */
182 if (it
== NULL
) return;
184 for (it
= b
->actions
[a
]; it
; it
= it
->next
) {
185 Action
*act
= it
->data
;
186 if (act
->func
!= NULL
) {
189 g_assert(act
->func
!= action_moveresize
);
191 if (act
->func
== action_showmenu
) {
192 act
->data
.showmenu
.x
= x
;
193 act
->data
.showmenu
.y
= y
;
196 act
->func(&act
->data
);
201 static void fire_motion(MouseAction a
, Context context
, Client
*c
,
202 guint state
, guint button
, int x_root
, int y_root
,
208 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
210 if (b
->state
== state
&& b
->button
== button
)
213 /* if not bound, then nothing to do! */
214 if (it
== NULL
) return;
216 for (it
= b
->actions
[a
]; it
; it
= it
->next
) {
217 Action
*act
= it
->data
;
218 if (act
->func
!= NULL
) {
221 if (act
->func
== action_moveresize
) {
222 act
->data
.moveresize
.x
= x_root
;
223 act
->data
.moveresize
.y
= y_root
;
224 act
->data
.moveresize
.button
= button
;
225 if (!(act
->data
.moveresize
.corner
==
226 prop_atoms
.net_wm_moveresize_move
||
227 act
->data
.moveresize
.corner
==
228 prop_atoms
.net_wm_moveresize_move_keyboard
||
229 act
->data
.moveresize
.corner
==
230 prop_atoms
.net_wm_moveresize_size_keyboard
))
231 act
->data
.moveresize
.corner
= corner
;
233 g_assert_not_reached();
235 act
->func(&act
->data
);
240 static guint32
pick_corner(int x
, int y
, int cx
, int cy
, int cw
, int ch
)
242 if (x
- cx
< cw
/ 2) {
244 return prop_atoms
.net_wm_moveresize_size_topleft
;
246 return prop_atoms
.net_wm_moveresize_size_bottomleft
;
249 return prop_atoms
.net_wm_moveresize_size_topright
;
251 return prop_atoms
.net_wm_moveresize_size_bottomright
;
255 static void event(ObEvent
*e
, void *foo
)
258 static guint button
= 0, state
= 0, lbutton
= 0;
260 gboolean click
= FALSE
;
261 gboolean dclick
= FALSE
;
265 case Event_Client_Mapped
:
266 grab_for_client(e
->data
.c
.client
, TRUE
);
269 case Event_Client_Destroy
:
270 grab_for_client(e
->data
.c
.client
, FALSE
);
273 case Event_X_ButtonPress
:
274 context
= frame_context(e
->data
.x
.client
,
275 e
->data
.x
.e
->xbutton
.window
);
277 px
= e
->data
.x
.e
->xbutton
.x_root
;
278 py
= e
->data
.x
.e
->xbutton
.y_root
;
279 button
= e
->data
.x
.e
->xbutton
.button
;
280 state
= e
->data
.x
.e
->xbutton
.state
;
282 fire_button(MouseAction_Press
, context
,
283 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
284 e
->data
.x
.e
->xbutton
.button
,
285 e
->data
.x
.e
->xbutton
.x_root
, e
->data
.x
.e
->xbutton
.y_root
);
287 if (context
== Context_Client
) {
288 /* Replay the event, so it goes to the client*/
289 XAllowEvents(ob_display
, ReplayPointer
, event_lasttime
);
290 /* Fall through to the release case! */
294 case Event_X_ButtonRelease
:
295 context
= frame_context(e
->data
.x
.client
,
296 e
->data
.x
.e
->xbutton
.window
);
297 if (e
->data
.x
.e
->xbutton
.button
== button
) {
298 /* clicks are only valid if its released over the window */
301 guint ujunk
, b
, w
, h
;
302 XGetGeometry(ob_display
, e
->data
.x
.e
->xbutton
.window
,
303 &wjunk
, &junk1
, &junk2
, &w
, &h
, &b
, &ujunk
);
304 if (e
->data
.x
.e
->xbutton
.x
>= (signed)-b
&&
305 e
->data
.x
.e
->xbutton
.y
>= (signed)-b
&&
306 e
->data
.x
.e
->xbutton
.x
< (signed)(w
+b
) &&
307 e
->data
.x
.e
->xbutton
.y
< (signed)(h
+b
)) {
309 /* double clicks happen if there were 2 in a row! */
310 if (lbutton
== button
&&
311 e
->data
.x
.e
->xbutton
.time
- dclicktime
<= ltime
) {
321 ltime
= e
->data
.x
.e
->xbutton
.time
;
323 fire_button(MouseAction_Release
, context
,
324 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
325 e
->data
.x
.e
->xbutton
.button
,
326 e
->data
.x
.e
->xbutton
.x_root
, e
->data
.x
.e
->xbutton
.y_root
);
328 fire_button(MouseAction_Click
, context
,
329 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
330 e
->data
.x
.e
->xbutton
.button
,
331 e
->data
.x
.e
->xbutton
.x_root
,
332 e
->data
.x
.e
->xbutton
.y_root
);
334 fire_button(MouseAction_DClick
, 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
);
341 case Event_X_MotionNotify
:
343 if (ABS(e
->data
.x
.e
->xmotion
.x_root
- px
) >= threshold
||
344 ABS(e
->data
.x
.e
->xmotion
.y_root
- py
) >= threshold
) {
347 context
= frame_context(e
->data
.x
.client
,
348 e
->data
.x
.e
->xmotion
.window
);
350 /* You can't drag on buttons */
351 if (context
== Context_Maximize
||
352 context
== Context_AllDesktops
||
353 context
== Context_Shade
||
354 context
== Context_Iconify
||
355 context
== Context_Icon
||
356 context
== Context_Close
)
359 if (!e
->data
.x
.client
)
360 corner
= prop_atoms
.net_wm_moveresize_size_bottomright
;
363 pick_corner(e
->data
.x
.e
->xmotion
.x_root
,
364 e
->data
.x
.e
->xmotion
.y_root
,
365 e
->data
.x
.client
->frame
->area
.x
,
366 e
->data
.x
.client
->frame
->area
.y
,
367 /* use the client size because the frame
368 can be differently sized (shaded
369 windows) and we want this based on the
371 e
->data
.x
.client
->area
.width
+
372 e
->data
.x
.client
->frame
->size
.left
+
373 e
->data
.x
.client
->frame
->size
.right
,
374 e
->data
.x
.client
->area
.height
+
375 e
->data
.x
.client
->frame
->size
.top
+
376 e
->data
.x
.client
->frame
->size
.bottom
);
377 fire_motion(MouseAction_Motion
, context
,
378 e
->data
.x
.client
, state
, button
,
379 e
->data
.x
.e
->xmotion
.x_root
,
380 e
->data
.x
.e
->xmotion
.y_root
, corner
);
388 g_assert_not_reached();
392 gboolean
mbind(char *buttonstr
, char *contextstr
, MouseAction mact
,
400 if (!translate_button(buttonstr
, &state
, &button
)) {
401 g_warning("invalid button '%s'", buttonstr
);
405 contextstr
= g_ascii_strdown(contextstr
, -1);
406 context
= frame_context_from_string(contextstr
);
408 g_warning("invalid context '%s'", contextstr
);
414 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
){
416 if (b
->state
== state
&& b
->button
== button
) {
417 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
422 grab_all_clients(FALSE
);
424 /* add the binding */
425 b
= g_new0(MouseBinding
, 1);
428 b
->actions
[mact
] = g_slist_append(NULL
, action
);
429 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
431 grab_all_clients(TRUE
);
436 void plugin_startup()
438 dispatch_register(Event_Client_Mapped
| Event_Client_Destroy
|
439 Event_X_ButtonPress
| Event_X_ButtonRelease
|
440 Event_X_MotionNotify
, (EventHandler
)event
, NULL
);
443 void plugin_shutdown()
445 dispatch_register(0, (EventHandler
)event
, NULL
);
447 grab_all_clients(FALSE
);