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.
29 #include "translate.h"
38 GSList
*actions
[OB_NUM_MOUSE_ACTIONS
]; /* lists of Action pointers */
41 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
42 co == OB_FRAME_CONTEXT_FRAME : FALSE)
43 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
44 co == OB_FRAME_CONTEXT_DESKTOP : \
45 co == OB_FRAME_CONTEXT_CLIENT)
47 /* Array of GSList*s of ObMouseBinding*s. */
48 static GSList
*bound_contexts
[OB_FRAME_NUM_CONTEXTS
];
50 ObFrameContext
mouse_button_frame_context(ObFrameContext context
,
54 ObFrameContext x
= context
;
56 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
57 ObMouseBinding
*b
= it
->data
;
59 if (b
->button
== button
)
64 case OB_FRAME_CONTEXT_NONE
:
65 case OB_FRAME_CONTEXT_DESKTOP
:
66 case OB_FRAME_CONTEXT_CLIENT
:
67 case OB_FRAME_CONTEXT_TITLEBAR
:
68 case OB_FRAME_CONTEXT_HANDLE
:
69 case OB_FRAME_CONTEXT_FRAME
:
70 case OB_FRAME_CONTEXT_MOVE_RESIZE
:
72 case OB_FRAME_CONTEXT_BLCORNER
:
73 case OB_FRAME_CONTEXT_BRCORNER
:
74 x
= OB_FRAME_CONTEXT_HANDLE
;
76 case OB_FRAME_CONTEXT_TLCORNER
:
77 case OB_FRAME_CONTEXT_TRCORNER
:
78 case OB_FRAME_CONTEXT_MAXIMIZE
:
79 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
80 case OB_FRAME_CONTEXT_SHADE
:
81 case OB_FRAME_CONTEXT_ICONIFY
:
82 case OB_FRAME_CONTEXT_ICON
:
83 case OB_FRAME_CONTEXT_CLOSE
:
84 x
= OB_FRAME_CONTEXT_TITLEBAR
;
86 case OB_FRAME_NUM_CONTEXTS
:
87 g_assert_not_reached();
93 void mouse_grab_for_client(ObClient
*client
, gboolean grab
)
98 for (i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
)
99 for (it
= bound_contexts
[i
]; it
; it
= g_slist_next(it
)) {
100 /* grab/ungrab the button */
101 ObMouseBinding
*b
= it
->data
;
106 if (FRAME_CONTEXT(i
, client
)) {
107 win
= client
->frame
->window
;
108 mode
= GrabModeAsync
;
109 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
110 } else if (CLIENT_CONTEXT(i
, client
)) {
111 win
= client
->frame
->plate
;
112 mode
= GrabModeSync
; /* this is handled in event */
113 mask
= ButtonPressMask
; /* can't catch more than this with Sync
114 mode the release event is
115 manufactured in event() */
119 grab_button_full(b
->button
, b
->state
, win
, mask
, mode
,
122 ungrab_button(b
->button
, b
->state
, win
);
126 static void grab_all_clients(gboolean grab
)
130 for (it
= client_list
; it
; it
= g_list_next(it
))
131 mouse_grab_for_client(it
->data
, grab
);
134 void mouse_unbind_all()
139 for(i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
) {
140 for (it
= bound_contexts
[i
]; it
; it
= g_slist_next(it
)) {
141 ObMouseBinding
*b
= it
->data
;
144 for (j
= 0; j
< OB_NUM_MOUSE_ACTIONS
; ++j
) {
147 for (it
= b
->actions
[j
]; it
; it
= g_slist_next(it
))
148 action_unref(it
->data
);
149 g_slist_free(b
->actions
[j
]);
153 g_slist_free(bound_contexts
[i
]);
154 bound_contexts
[i
] = NULL
;
158 static gboolean
fire_binding(ObMouseAction a
, ObFrameContext context
,
159 ObClient
*c
, guint state
,
160 guint button
, gint x
, gint y
, Time time
)
165 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
167 if (b
->state
== state
&& b
->button
== button
)
170 /* if not bound, then nothing to do! */
171 if (it
== NULL
) return FALSE
;
173 action_run_mouse(b
->actions
[a
], c
, context
, state
, button
, x
, y
, time
);
177 void mouse_event(ObClient
*client
, XEvent
*e
)
180 static guint button
= 0, state
= 0, lbutton
= 0;
181 static Window lwindow
= None
;
182 static gint px
, py
, pwx
= -1, pwy
= -1;
184 ObFrameContext context
;
185 gboolean click
= FALSE
;
186 gboolean dclick
= FALSE
;
190 context
= frame_context(client
, e
->xbutton
.window
,
191 e
->xbutton
.x
, e
->xbutton
.y
);
192 context
= mouse_button_frame_context(context
, e
->xbutton
.button
);
194 px
= e
->xbutton
.x_root
;
195 py
= e
->xbutton
.y_root
;
196 if (!button
) pwx
= e
->xbutton
.x
;
197 if (!button
) pwy
= e
->xbutton
.y
;
198 button
= e
->xbutton
.button
;
199 state
= e
->xbutton
.state
;
201 fire_binding(OB_MOUSE_ACTION_PRESS
, context
,
202 client
, e
->xbutton
.state
,
204 e
->xbutton
.x_root
, e
->xbutton
.y_root
,
207 if (CLIENT_CONTEXT(context
, client
)) {
208 /* Replay the event, so it goes to the client*/
209 XAllowEvents(ob_display
, ReplayPointer
, event_curtime
);
210 /* Fall through to the release case! */
215 /* use where the press occured in the window */
216 context
= frame_context(client
, e
->xbutton
.window
, pwx
, pwy
);
217 context
= mouse_button_frame_context(context
, e
->xbutton
.button
);
219 if (e
->xbutton
.button
== button
)
222 if (e
->xbutton
.button
== button
) {
223 /* clicks are only valid if its released over the window */
226 guint ujunk
, b
, w
, h
;
227 /* this can cause errors to occur when the window closes */
228 xerror_set_ignore(TRUE
);
229 junk1
= XGetGeometry(ob_display
, e
->xbutton
.window
,
230 &wjunk
, &junk1
, &junk2
, &w
, &h
, &b
, &ujunk
);
231 xerror_set_ignore(FALSE
);
233 if (e
->xbutton
.x
>= (signed)-b
&&
234 e
->xbutton
.y
>= (signed)-b
&&
235 e
->xbutton
.x
< (signed)(w
+b
) &&
236 e
->xbutton
.y
< (signed)(h
+b
)) {
238 /* double clicks happen if there were 2 in a row! */
239 if (lbutton
== button
&&
240 lwindow
== e
->xbutton
.window
&&
241 e
->xbutton
.time
- config_mouse_dclicktime
<=
247 lwindow
= e
->xbutton
.window
;
257 ltime
= e
->xbutton
.time
;
259 fire_binding(OB_MOUSE_ACTION_RELEASE
, context
,
260 client
, e
->xbutton
.state
,
266 fire_binding(OB_MOUSE_ACTION_CLICK
, context
,
267 client
, e
->xbutton
.state
,
273 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK
, context
,
274 client
, e
->xbutton
.state
,
283 context
= frame_context(client
, e
->xmotion
.window
, pwx
, pwy
);
284 context
= mouse_button_frame_context(context
, button
);
286 if (ABS(e
->xmotion
.x_root
- px
) >= config_mouse_threshold
||
287 ABS(e
->xmotion
.y_root
- py
) >= config_mouse_threshold
) {
289 /* You can't drag on buttons */
290 if (context
== OB_FRAME_CONTEXT_MAXIMIZE
||
291 context
== OB_FRAME_CONTEXT_ALLDESKTOPS
||
292 context
== OB_FRAME_CONTEXT_SHADE
||
293 context
== OB_FRAME_CONTEXT_ICONIFY
||
294 context
== OB_FRAME_CONTEXT_ICON
||
295 context
== OB_FRAME_CONTEXT_CLOSE
)
298 fire_binding(OB_MOUSE_ACTION_MOTION
, context
,
299 client
, state
, button
, px
, py
, e
->xmotion
.time
);
307 g_assert_not_reached();
311 gboolean
mouse_bind(const gchar
*buttonstr
, const gchar
*contextstr
,
312 ObMouseAction mact
, ObAction
*action
)
315 ObFrameContext context
;
319 if (!translate_button(buttonstr
, &state
, &button
)) {
320 g_message(_("Invalid button '%s' in mouse binding"), buttonstr
);
324 context
= frame_context_from_string(contextstr
);
326 g_message(_("Invalid context '%s' in mouse binding"), contextstr
);
330 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
332 if (b
->state
== state
&& b
->button
== button
) {
333 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
338 /* when there are no modifiers in the binding, then the action cannot
340 if (!state
&& action
->data
.any
.interactive
) {
341 action
->data
.any
.interactive
= FALSE
;
342 action
->data
.inter
.final
= TRUE
;
345 /* add the binding */
346 b
= g_new0(ObMouseBinding
, 1);
349 b
->actions
[mact
] = g_slist_append(NULL
, action
);
350 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
355 void mouse_startup(gboolean reconfig
)
357 grab_all_clients(TRUE
);
360 void mouse_shutdown(gboolean reconfig
)
362 grab_all_clients(FALSE
);