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;
262 static Window lwindow
= None
;
264 gboolean click
= FALSE
;
265 gboolean dclick
= FALSE
;
266 ObFrameContext context
;
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
);
281 px
= e
->data
.x
.e
->xbutton
.x_root
;
282 py
= e
->data
.x
.e
->xbutton
.y_root
;
283 button
= e
->data
.x
.e
->xbutton
.button
;
284 state
= e
->data
.x
.e
->xbutton
.state
;
286 fire_button(MouseAction_Press
, context
,
287 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
288 e
->data
.x
.e
->xbutton
.button
,
289 e
->data
.x
.e
->xbutton
.x_root
, e
->data
.x
.e
->xbutton
.y_root
);
291 if (context
== OB_FRAME_CONTEXT_CLIENT
) {
292 /* Replay the event, so it goes to the client*/
293 XAllowEvents(ob_display
, ReplayPointer
, event_lasttime
);
294 /* Fall through to the release case! */
298 case Event_X_ButtonRelease
:
299 context
= frame_context(e
->data
.x
.client
,
300 e
->data
.x
.e
->xbutton
.window
);
301 if (e
->data
.x
.e
->xbutton
.button
== button
) {
302 /* clicks are only valid if its released over the window */
305 guint ujunk
, b
, w
, h
;
306 XGetGeometry(ob_display
, e
->data
.x
.e
->xbutton
.window
,
307 &wjunk
, &junk1
, &junk2
, &w
, &h
, &b
, &ujunk
);
308 if (e
->data
.x
.e
->xbutton
.x
>= (signed)-b
&&
309 e
->data
.x
.e
->xbutton
.y
>= (signed)-b
&&
310 e
->data
.x
.e
->xbutton
.x
< (signed)(w
+b
) &&
311 e
->data
.x
.e
->xbutton
.y
< (signed)(h
+b
)) {
313 /* double clicks happen if there were 2 in a row! */
314 if (lbutton
== button
&&
315 lwindow
== e
->data
.x
.e
->xbutton
.window
&&
316 e
->data
.x
.e
->xbutton
.time
- dclicktime
<= ltime
) {
321 lwindow
= e
->data
.x
.e
->xbutton
.window
;
330 ltime
= e
->data
.x
.e
->xbutton
.time
;
332 fire_button(MouseAction_Release
, context
,
333 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
334 e
->data
.x
.e
->xbutton
.button
,
335 e
->data
.x
.e
->xbutton
.x_root
, e
->data
.x
.e
->xbutton
.y_root
);
337 fire_button(MouseAction_Click
, 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
);
343 fire_button(MouseAction_DClick
, context
,
344 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
345 e
->data
.x
.e
->xbutton
.button
,
346 e
->data
.x
.e
->xbutton
.x_root
,
347 e
->data
.x
.e
->xbutton
.y_root
);
350 case Event_X_MotionNotify
:
352 if (ABS(e
->data
.x
.e
->xmotion
.x_root
- px
) >= threshold
||
353 ABS(e
->data
.x
.e
->xmotion
.y_root
- py
) >= threshold
) {
356 context
= frame_context(e
->data
.x
.client
,
357 e
->data
.x
.e
->xmotion
.window
);
359 /* You can't drag on buttons */
360 if (context
== OB_FRAME_CONTEXT_MAXIMIZE
||
361 context
== OB_FRAME_CONTEXT_ALLDESKTOPS
||
362 context
== OB_FRAME_CONTEXT_SHADE
||
363 context
== OB_FRAME_CONTEXT_ICONIFY
||
364 context
== OB_FRAME_CONTEXT_ICON
||
365 context
== OB_FRAME_CONTEXT_CLOSE
)
368 if (!e
->data
.x
.client
)
369 corner
= prop_atoms
.net_wm_moveresize_size_bottomright
;
372 pick_corner(e
->data
.x
.e
->xmotion
.x_root
,
373 e
->data
.x
.e
->xmotion
.y_root
,
374 e
->data
.x
.client
->frame
->area
.x
,
375 e
->data
.x
.client
->frame
->area
.y
,
376 /* use the client size because the frame
377 can be differently sized (shaded
378 windows) and we want this based on the
380 e
->data
.x
.client
->area
.width
+
381 e
->data
.x
.client
->frame
->size
.left
+
382 e
->data
.x
.client
->frame
->size
.right
,
383 e
->data
.x
.client
->area
.height
+
384 e
->data
.x
.client
->frame
->size
.top
+
385 e
->data
.x
.client
->frame
->size
.bottom
);
386 fire_motion(MouseAction_Motion
, context
,
387 e
->data
.x
.client
, state
, button
,
388 e
->data
.x
.e
->xmotion
.x_root
,
389 e
->data
.x
.e
->xmotion
.y_root
, corner
);
397 g_assert_not_reached();
401 gboolean
mbind(char *buttonstr
, char *contextstr
, MouseAction mact
,
405 ObFrameContext context
;
409 if (!translate_button(buttonstr
, &state
, &button
)) {
410 g_warning("invalid button '%s'", buttonstr
);
414 contextstr
= g_ascii_strdown(contextstr
, -1);
415 context
= frame_context_from_string(contextstr
);
417 g_warning("invalid context '%s'", contextstr
);
423 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
){
425 if (b
->state
== state
&& b
->button
== button
) {
426 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
431 grab_all_clients(FALSE
);
433 /* add the binding */
434 b
= g_new0(MouseBinding
, 1);
437 b
->actions
[mact
] = g_slist_append(NULL
, action
);
438 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
440 grab_all_clients(TRUE
);
445 void plugin_startup()
447 dispatch_register(Event_Client_Mapped
| Event_Client_Destroy
|
448 Event_X_ButtonPress
| Event_X_ButtonRelease
|
449 Event_X_MotionNotify
, (EventHandler
)event
, NULL
);
452 void plugin_shutdown()
454 dispatch_register(0, (EventHandler
)event
, NULL
);
456 grab_all_clients(FALSE
);