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_full(b
->button
, b
->state
, win
, mask
, mode
, None
);
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
) {
101 for (it
= b
->actions
[j
]; it
; it
= it
->next
) {
102 action_free(it
->data
);
104 g_slist_free(b
->actions
[j
]);
108 g_slist_free(bound_contexts
[i
]);
112 static void fire_button(MouseAction a
, Context context
, Client
*c
, guint state
,
113 guint button
, int x
, int y
)
118 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
120 if (b
->state
== state
&& b
->button
== button
)
123 /* if not bound, then nothing to do! */
124 if (it
== NULL
) return;
126 for (it
= b
->actions
[a
]; it
; it
= it
->next
) {
127 Action
*act
= it
->data
;
128 if (act
->func
!= NULL
) {
131 g_assert(act
->func
!= action_moveresize
);
133 if (act
->func
== action_showmenu
) {
134 act
->data
.showmenu
.x
= x
;
135 act
->data
.showmenu
.y
= y
;
138 act
->func(&act
->data
);
143 static void fire_motion(MouseAction a
, Context context
, Client
*c
,
144 guint state
, guint button
, int x_root
, int y_root
,
150 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
152 if (b
->state
== state
&& b
->button
== button
)
155 /* if not bound, then nothing to do! */
156 if (it
== NULL
) return;
158 for (it
= b
->actions
[a
]; it
; it
= it
->next
) {
159 Action
*act
= it
->data
;
160 if (act
->func
!= NULL
) {
163 if (act
->func
== action_moveresize
) {
164 act
->data
.moveresize
.x
= x_root
;
165 act
->data
.moveresize
.y
= y_root
;
166 act
->data
.moveresize
.button
= button
;
167 if (!(act
->data
.moveresize
.corner
==
168 prop_atoms
.net_wm_moveresize_move
||
169 act
->data
.moveresize
.corner
==
170 prop_atoms
.net_wm_moveresize_move_keyboard
||
171 act
->data
.moveresize
.corner
==
172 prop_atoms
.net_wm_moveresize_size_keyboard
))
173 act
->data
.moveresize
.corner
= corner
;
175 g_assert_not_reached();
177 act
->func(&act
->data
);
182 static guint32
pick_corner(int x
, int y
, int cx
, int cy
, int cw
, int ch
)
184 if (x
- cx
< cw
/ 2) {
186 return prop_atoms
.net_wm_moveresize_size_topleft
;
188 return prop_atoms
.net_wm_moveresize_size_bottomleft
;
191 return prop_atoms
.net_wm_moveresize_size_topright
;
193 return prop_atoms
.net_wm_moveresize_size_bottomright
;
197 static void event(ObEvent
*e
, void *foo
)
200 static guint button
= 0, state
= 0, lbutton
= 0;
202 gboolean click
= FALSE
;
203 gboolean dclick
= FALSE
;
207 case Event_Client_Mapped
:
208 grab_for_client(e
->data
.c
.client
, TRUE
);
211 case Event_Client_Destroy
:
212 grab_for_client(e
->data
.c
.client
, FALSE
);
215 case Event_X_ButtonPress
:
216 context
= frame_context(e
->data
.x
.client
,
217 e
->data
.x
.e
->xbutton
.window
);
220 px
= e
->data
.x
.e
->xbutton
.x_root
;
221 py
= e
->data
.x
.e
->xbutton
.y_root
;
222 button
= e
->data
.x
.e
->xbutton
.button
;
223 state
= e
->data
.x
.e
->xbutton
.state
;
226 fire_button(MouseAction_Press
, context
,
227 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
228 e
->data
.x
.e
->xbutton
.button
,
229 e
->data
.x
.e
->xbutton
.x_root
, e
->data
.x
.e
->xbutton
.y_root
);
231 if (context
== Context_Client
) {
232 /* Replay the event, so it goes to the client*/
233 XAllowEvents(ob_display
, ReplayPointer
, event_lasttime
);
234 /* Fall through to the release case! */
238 case Event_X_ButtonRelease
:
239 context
= frame_context(e
->data
.x
.client
,
240 e
->data
.x
.e
->xbutton
.window
);
241 if (e
->data
.x
.e
->xbutton
.button
== button
) {
242 /* clicks are only valid if its released over the window */
245 guint ujunk
, b
, w
, h
;
246 XGetGeometry(ob_display
, e
->data
.x
.e
->xbutton
.window
,
247 &wjunk
, &junk
, &junk
, &w
, &h
, &b
, &ujunk
);
248 if (e
->data
.x
.e
->xbutton
.x
>= (signed)-b
&&
249 e
->data
.x
.e
->xbutton
.y
>= (signed)-b
&&
250 e
->data
.x
.e
->xbutton
.x
< (signed)(w
+b
) &&
251 e
->data
.x
.e
->xbutton
.y
< (signed)(h
+b
)) {
253 /* double clicks happen if there were 2 in a row! */
254 if (lbutton
== button
&&
255 e
->data
.x
.e
->xbutton
.time
- dclicktime
<= ltime
) {
265 ltime
= e
->data
.x
.e
->xbutton
.time
;
267 fire_button(MouseAction_Release
, context
,
268 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
269 e
->data
.x
.e
->xbutton
.button
,
270 e
->data
.x
.e
->xbutton
.x_root
, e
->data
.x
.e
->xbutton
.y_root
);
272 fire_button(MouseAction_Click
, context
,
273 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
274 e
->data
.x
.e
->xbutton
.button
,
275 e
->data
.x
.e
->xbutton
.x_root
,
276 e
->data
.x
.e
->xbutton
.y_root
);
278 fire_button(MouseAction_DClick
, context
,
279 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
280 e
->data
.x
.e
->xbutton
.button
,
281 e
->data
.x
.e
->xbutton
.x_root
,
282 e
->data
.x
.e
->xbutton
.y_root
);
285 case Event_X_MotionNotify
:
287 if (ABS(e
->data
.x
.e
->xmotion
.x_root
- px
) >= threshold
||
288 ABS(e
->data
.x
.e
->xmotion
.y_root
- py
) >= threshold
) {
291 context
= frame_context(e
->data
.x
.client
,
292 e
->data
.x
.e
->xmotion
.window
);
294 /* You can't drag on buttons */
295 if (context
== Context_Maximize
||
296 context
== Context_AllDesktops
||
297 context
== Context_Shade
||
298 context
== Context_Iconify
||
299 context
== Context_Icon
||
300 context
== Context_Close
)
303 if (!e
->data
.x
.client
)
304 corner
= prop_atoms
.net_wm_moveresize_size_bottomright
;
307 pick_corner(e
->data
.x
.e
->xmotion
.x_root
,
308 e
->data
.x
.e
->xmotion
.y_root
,
309 e
->data
.x
.client
->frame
->area
.x
,
310 e
->data
.x
.client
->frame
->area
.y
,
311 /* use the client size because the frame
312 can be differently sized (shaded
313 windows) and we want this based on the
315 e
->data
.x
.client
->area
.width
+
316 e
->data
.x
.client
->frame
->size
.left
+
317 e
->data
.x
.client
->frame
->size
.right
,
318 e
->data
.x
.client
->area
.height
+
319 e
->data
.x
.client
->frame
->size
.top
+
320 e
->data
.x
.client
->frame
->size
.bottom
);
321 fire_motion(MouseAction_Motion
, context
,
322 e
->data
.x
.client
, state
, button
,
323 e
->data
.x
.e
->xmotion
.x_root
,
324 e
->data
.x
.e
->xmotion
.y_root
, corner
);
332 g_assert_not_reached();
336 gboolean
mbind(char *buttonstr
, char *contextstr
, MouseAction mact
,
344 if (!translate_button(buttonstr
, &state
, &button
)) {
345 g_warning("invalid button '%s'", buttonstr
);
349 contextstr
= g_ascii_strdown(contextstr
, -1);
350 context
= frame_context_from_string(contextstr
);
352 g_warning("invalid context '%s'", contextstr
);
358 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
){
360 if (b
->state
== state
&& b
->button
== button
) {
361 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
366 grab_all_clients(FALSE
);
368 /* add the binding */
369 b
= g_new0(MouseBinding
, 1);
372 b
->actions
[mact
] = g_slist_append(NULL
, action
);
373 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
375 grab_all_clients(TRUE
);
380 void plugin_startup()
382 dispatch_register(Event_Client_Mapped
| Event_Client_Destroy
|
383 Event_X_ButtonPress
| Event_X_ButtonRelease
|
384 Event_X_MotionNotify
, (EventHandler
)event
, NULL
);
387 void plugin_shutdown()
389 dispatch_register(0, (EventHandler
)event
, NULL
);
391 grab_all_clients(FALSE
);