1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 mouse.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
27 #include "translate.h"
30 #include "obt/display.h"
37 GSList
*actions
[OB_NUM_MOUSE_ACTIONS
]; /* lists of Action pointers */
40 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
41 co == OB_FRAME_CONTEXT_FRAME : FALSE)
42 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
43 co == OB_FRAME_CONTEXT_DESKTOP : \
44 co == OB_FRAME_CONTEXT_CLIENT)
46 /* Array of GSList*s of ObMouseBinding*s. */
47 static GSList
*bound_contexts
[OB_FRAME_NUM_CONTEXTS
];
48 /* TRUE when we have a grab on the pointer and need to reply the pointer event
49 to send it to other applications */
50 static gboolean replay_pointer_needed
;
52 ObFrameContext
mouse_button_frame_context(ObFrameContext context
,
57 ObFrameContext x
= context
;
59 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
60 ObMouseBinding
*b
= it
->data
;
62 if (b
->button
== button
&& b
->state
== state
)
67 case OB_FRAME_CONTEXT_NONE
:
68 case OB_FRAME_CONTEXT_DESKTOP
:
69 case OB_FRAME_CONTEXT_CLIENT
:
70 case OB_FRAME_CONTEXT_TITLEBAR
:
71 case OB_FRAME_CONTEXT_FRAME
:
72 case OB_FRAME_CONTEXT_MOVE_RESIZE
:
73 case OB_FRAME_CONTEXT_LEFT
:
74 case OB_FRAME_CONTEXT_RIGHT
:
76 case OB_FRAME_CONTEXT_ROOT
:
77 x
= OB_FRAME_CONTEXT_DESKTOP
;
79 case OB_FRAME_CONTEXT_BOTTOM
:
80 case OB_FRAME_CONTEXT_BLCORNER
:
81 case OB_FRAME_CONTEXT_BRCORNER
:
82 x
= OB_FRAME_CONTEXT_BOTTOM
;
84 case OB_FRAME_CONTEXT_TLCORNER
:
85 case OB_FRAME_CONTEXT_TRCORNER
:
86 case OB_FRAME_CONTEXT_TOP
:
87 case OB_FRAME_CONTEXT_MAXIMIZE
:
88 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
89 case OB_FRAME_CONTEXT_SHADE
:
90 case OB_FRAME_CONTEXT_ICONIFY
:
91 case OB_FRAME_CONTEXT_ICON
:
92 case OB_FRAME_CONTEXT_CLOSE
:
93 x
= OB_FRAME_CONTEXT_TITLEBAR
;
95 case OB_FRAME_NUM_CONTEXTS
:
96 g_assert_not_reached();
99 /* allow for multiple levels of fall-through */
101 return mouse_button_frame_context(x
, button
, state
);
106 void mouse_grab_for_client(ObClient
*client
, gboolean grab
)
111 for (i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
)
112 for (it
= bound_contexts
[i
]; it
; it
= g_slist_next(it
)) {
113 /* grab/ungrab the button */
114 ObMouseBinding
*b
= it
->data
;
119 if (FRAME_CONTEXT(i
, client
)) {
120 win
= client
->frame
->window
;
121 mode
= GrabModeAsync
;
122 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
123 } else if (CLIENT_CONTEXT(i
, client
)) {
124 win
= client
->window
;
125 mode
= GrabModeSync
; /* this is handled in event */
126 mask
= ButtonPressMask
; /* can't catch more than this with Sync
127 mode the release event is
128 manufactured in event() */
132 grab_button_full(b
->button
, b
->state
, win
, mask
, mode
,
135 ungrab_button(b
->button
, b
->state
, win
);
139 static void grab_all_clients(gboolean grab
)
143 for (it
= client_list
; it
; it
= g_list_next(it
))
144 mouse_grab_for_client(it
->data
, grab
);
147 void mouse_unbind_all(void)
152 for(i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
) {
153 for (it
= bound_contexts
[i
]; it
; it
= g_slist_next(it
)) {
154 ObMouseBinding
*b
= it
->data
;
157 for (j
= 0; j
< OB_NUM_MOUSE_ACTIONS
; ++j
) {
160 for (jt
= b
->actions
[j
]; jt
; jt
= g_slist_next(jt
))
161 actions_act_unref(jt
->data
);
162 g_slist_free(b
->actions
[j
]);
166 g_slist_free(bound_contexts
[i
]);
167 bound_contexts
[i
] = NULL
;
171 static ObUserAction
mouse_action_to_user_action(ObMouseAction a
)
174 case OB_MOUSE_ACTION_PRESS
: return OB_USER_ACTION_MOUSE_PRESS
;
175 case OB_MOUSE_ACTION_RELEASE
: return OB_USER_ACTION_MOUSE_RELEASE
;
176 case OB_MOUSE_ACTION_CLICK
: return OB_USER_ACTION_MOUSE_CLICK
;
177 case OB_MOUSE_ACTION_DOUBLE_CLICK
:
178 return OB_USER_ACTION_MOUSE_DOUBLE_CLICK
;
179 case OB_MOUSE_ACTION_MOTION
: return OB_USER_ACTION_MOUSE_MOTION
;
181 g_assert_not_reached();
185 static gboolean
fire_binding(ObMouseAction a
, ObFrameContext context
,
186 ObClient
*c
, guint state
,
187 guint button
, gint x
, gint y
)
192 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
194 if (b
->state
== state
&& b
->button
== button
)
197 /* if not bound, then nothing to do! */
198 if (it
== NULL
) return FALSE
;
200 actions_run_acts(b
->actions
[a
], mouse_action_to_user_action(a
),
201 state
, x
, y
, button
, context
, c
);
205 void mouse_replay_pointer()
207 if (replay_pointer_needed
) {
208 /* replay the pointer event before any windows move */
209 XAllowEvents(obt_display
, ReplayPointer
, event_curtime
);
210 replay_pointer_needed
= FALSE
;
214 void mouse_event(ObClient
*client
, XEvent
*e
)
217 static guint button
= 0, state
= 0, lbutton
= 0;
218 static Window lwindow
= None
;
219 static gint px
, py
, pwx
= -1, pwy
= -1;
221 ObFrameContext context
;
222 gboolean click
= FALSE
;
223 gboolean dclick
= FALSE
;
227 context
= frame_context(client
, e
->xbutton
.window
,
228 e
->xbutton
.x
, e
->xbutton
.y
);
229 context
= mouse_button_frame_context(context
, e
->xbutton
.button
,
232 px
= e
->xbutton
.x_root
;
233 py
= e
->xbutton
.y_root
;
234 if (!button
) pwx
= e
->xbutton
.x
;
235 if (!button
) pwy
= e
->xbutton
.y
;
236 button
= e
->xbutton
.button
;
237 state
= e
->xbutton
.state
;
239 /* if the binding was in a client context, then we need to call
240 XAllowEvents with ReplayPointer at some point, to send the event
241 through to the client. when this happens though depends. if
242 windows are going to be moved on screen, then the click will end
243 up going somewhere wrong, set that we need it, and if nothing
244 else causes the replay pointer to be run, then we will do it
245 after all the actions are finished.
247 (We do it after all the actions because FocusIn interrupts
248 dragging for kdesktop, so if we send the button event now, and
249 then they get a focus event after, it breaks. Instead, wait to send
250 the button press until after the actions when possible.)
252 if (CLIENT_CONTEXT(context
, client
))
253 replay_pointer_needed
= TRUE
;
255 fire_binding(OB_MOUSE_ACTION_PRESS
, context
,
256 client
, e
->xbutton
.state
,
258 e
->xbutton
.x_root
, e
->xbutton
.y_root
);
260 /* if the bindings grab the pointer, there won't be a ButtonRelease
262 if (grab_on_pointer())
265 /* replay the pointer event if it hasn't been replayed yet (i.e. no
266 windows were moved) */
267 mouse_replay_pointer();
269 /* in the client context, we won't get a button release because of the
270 way it is grabbed, so just fake one */
271 if (!CLIENT_CONTEXT(context
, client
))
275 /* use where the press occured in the window */
276 context
= frame_context(client
, e
->xbutton
.window
, pwx
, pwy
);
277 context
= mouse_button_frame_context(context
, e
->xbutton
.button
,
280 if (e
->xbutton
.button
== button
)
283 if (e
->xbutton
.button
== button
) {
284 /* clicks are only valid if its released over the window */
287 guint ujunk
, b
, w
, h
;
288 /* this can cause errors to occur when the window closes */
289 obt_display_ignore_errors(TRUE
);
290 junk1
= XGetGeometry(obt_display
, e
->xbutton
.window
,
291 &wjunk
, &junk1
, &junk2
, &w
, &h
, &b
, &ujunk
);
292 obt_display_ignore_errors(FALSE
);
294 if (e
->xbutton
.x
>= (signed)-b
&&
295 e
->xbutton
.y
>= (signed)-b
&&
296 e
->xbutton
.x
< (signed)(w
+b
) &&
297 e
->xbutton
.y
< (signed)(h
+b
)) {
299 /* double clicks happen if there were 2 in a row! */
300 if (lbutton
== button
&&
301 lwindow
== e
->xbutton
.window
&&
302 e
->xbutton
.time
- config_mouse_dclicktime
<=
308 lwindow
= e
->xbutton
.window
;
318 ltime
= e
->xbutton
.time
;
320 fire_binding(OB_MOUSE_ACTION_RELEASE
, context
,
321 client
, e
->xbutton
.state
,
326 fire_binding(OB_MOUSE_ACTION_CLICK
, context
,
327 client
, e
->xbutton
.state
,
332 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK
, context
,
333 client
, e
->xbutton
.state
,
341 context
= frame_context(client
, e
->xmotion
.window
, pwx
, pwy
);
342 context
= mouse_button_frame_context(context
, button
, state
);
344 if (ABS(e
->xmotion
.x_root
- px
) >= config_mouse_threshold
||
345 ABS(e
->xmotion
.y_root
- py
) >= config_mouse_threshold
) {
347 /* You can't drag on buttons */
348 if (context
== OB_FRAME_CONTEXT_MAXIMIZE
||
349 context
== OB_FRAME_CONTEXT_ALLDESKTOPS
||
350 context
== OB_FRAME_CONTEXT_SHADE
||
351 context
== OB_FRAME_CONTEXT_ICONIFY
||
352 context
== OB_FRAME_CONTEXT_ICON
||
353 context
== OB_FRAME_CONTEXT_CLOSE
)
356 fire_binding(OB_MOUSE_ACTION_MOTION
, context
,
357 client
, state
, button
, px
, py
);
365 g_assert_not_reached();
369 gboolean
mouse_bind(const gchar
*buttonstr
, const gchar
*contextstr
,
370 ObMouseAction mact
, ObActionsAct
*action
)
373 ObFrameContext context
;
377 if (!translate_button(buttonstr
, &state
, &button
)) {
378 g_message(_("Invalid button '%s' in mouse binding"), buttonstr
);
382 context
= frame_context_from_string(contextstr
);
384 g_message(_("Invalid context '%s' in mouse binding"), contextstr
);
388 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
390 if (b
->state
== state
&& b
->button
== button
) {
391 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
396 /* add the binding */
397 b
= g_new0(ObMouseBinding
, 1);
400 b
->actions
[mact
] = g_slist_append(NULL
, action
);
401 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
406 void mouse_startup(gboolean reconfig
)
408 grab_all_clients(TRUE
);
411 void mouse_shutdown(gboolean reconfig
)
413 grab_all_clients(FALSE
);