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
,
55 ObFrameContext x
= context
;
57 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
58 ObMouseBinding
*b
= it
->data
;
60 if (b
->button
== button
&& b
->state
== state
)
65 case OB_FRAME_CONTEXT_NONE
:
66 case OB_FRAME_CONTEXT_DESKTOP
:
67 case OB_FRAME_CONTEXT_CLIENT
:
68 case OB_FRAME_CONTEXT_TITLEBAR
:
69 case OB_FRAME_CONTEXT_FRAME
:
70 case OB_FRAME_CONTEXT_MOVE_RESIZE
:
71 case OB_FRAME_CONTEXT_LEFT
:
72 case OB_FRAME_CONTEXT_RIGHT
:
74 case OB_FRAME_CONTEXT_ROOT
:
75 x
= OB_FRAME_CONTEXT_DESKTOP
;
77 case OB_FRAME_CONTEXT_BOTTOM
:
78 case OB_FRAME_CONTEXT_BLCORNER
:
79 case OB_FRAME_CONTEXT_BRCORNER
:
80 x
= OB_FRAME_CONTEXT_BOTTOM
;
82 case OB_FRAME_CONTEXT_TLCORNER
:
83 case OB_FRAME_CONTEXT_TRCORNER
:
84 case OB_FRAME_CONTEXT_TOP
:
85 case OB_FRAME_CONTEXT_MAXIMIZE
:
86 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
87 case OB_FRAME_CONTEXT_SHADE
:
88 case OB_FRAME_CONTEXT_ICONIFY
:
89 case OB_FRAME_CONTEXT_ICON
:
90 case OB_FRAME_CONTEXT_CLOSE
:
91 x
= OB_FRAME_CONTEXT_TITLEBAR
;
93 case OB_FRAME_NUM_CONTEXTS
:
94 g_assert_not_reached();
97 /* allow for multiple levels of fall-through */
99 return mouse_button_frame_context(x
, button
, state
);
104 void mouse_grab_for_client(ObClient
*client
, gboolean grab
)
109 for (i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
)
110 for (it
= bound_contexts
[i
]; it
; it
= g_slist_next(it
)) {
111 /* grab/ungrab the button */
112 ObMouseBinding
*b
= it
->data
;
117 if (FRAME_CONTEXT(i
, client
)) {
118 win
= client
->frame
->window
;
119 mode
= GrabModeAsync
;
120 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
121 } else if (CLIENT_CONTEXT(i
, client
)) {
122 win
= client
->window
;
123 mode
= GrabModeSync
; /* this is handled in event */
124 mask
= ButtonPressMask
; /* can't catch more than this with Sync
125 mode the release event is
126 manufactured in event() */
130 grab_button_full(b
->button
, b
->state
, win
, mask
, mode
,
133 ungrab_button(b
->button
, b
->state
, win
);
137 static void grab_all_clients(gboolean grab
)
141 for (it
= client_list
; it
; it
= g_list_next(it
))
142 mouse_grab_for_client(it
->data
, grab
);
145 void mouse_unbind_all()
150 for(i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
) {
151 for (it
= bound_contexts
[i
]; it
; it
= g_slist_next(it
)) {
152 ObMouseBinding
*b
= it
->data
;
155 for (j
= 0; j
< OB_NUM_MOUSE_ACTIONS
; ++j
) {
158 for (it
= b
->actions
[j
]; it
; it
= g_slist_next(it
))
159 action_unref(it
->data
);
160 g_slist_free(b
->actions
[j
]);
164 g_slist_free(bound_contexts
[i
]);
165 bound_contexts
[i
] = NULL
;
169 static gboolean
fire_binding(ObMouseAction a
, ObFrameContext context
,
170 ObClient
*c
, guint state
,
171 guint button
, gint x
, gint y
, Time time
)
176 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
178 if (b
->state
== state
&& b
->button
== button
)
181 /* if not bound, then nothing to do! */
182 if (it
== NULL
) return FALSE
;
184 action_run_mouse(b
->actions
[a
], c
, context
, state
, button
, x
, y
, time
);
188 void mouse_event(ObClient
*client
, XEvent
*e
)
191 static guint button
= 0, state
= 0, lbutton
= 0;
192 static Window lwindow
= None
;
193 static gint px
, py
, pwx
= -1, pwy
= -1;
195 ObFrameContext context
;
196 gboolean click
= FALSE
;
197 gboolean dclick
= FALSE
;
201 context
= frame_context(client
, e
->xbutton
.window
,
202 e
->xbutton
.x
, e
->xbutton
.y
);
203 context
= mouse_button_frame_context(context
, e
->xbutton
.button
,
206 px
= e
->xbutton
.x_root
;
207 py
= e
->xbutton
.y_root
;
208 if (!button
) pwx
= e
->xbutton
.x
;
209 if (!button
) pwy
= e
->xbutton
.y
;
210 button
= e
->xbutton
.button
;
211 state
= e
->xbutton
.state
;
213 fire_binding(OB_MOUSE_ACTION_PRESS
, context
,
214 client
, e
->xbutton
.state
,
216 e
->xbutton
.x_root
, e
->xbutton
.y_root
,
219 /* if the bindings grab the pointer, there won't be a ButtonRelease
221 if (grab_on_pointer())
224 if (CLIENT_CONTEXT(context
, client
)) {
225 /* Replay the event, so it goes to the client*/
226 XAllowEvents(ob_display
, ReplayPointer
, event_curtime
);
227 /* Fall through to the release case! */
232 /* use where the press occured in the window */
233 context
= frame_context(client
, e
->xbutton
.window
, pwx
, pwy
);
234 context
= mouse_button_frame_context(context
, e
->xbutton
.button
,
237 if (e
->xbutton
.button
== button
)
240 if (e
->xbutton
.button
== button
) {
241 /* clicks are only valid if its released over the window */
244 guint ujunk
, b
, w
, h
;
245 /* this can cause errors to occur when the window closes */
246 xerror_set_ignore(TRUE
);
247 junk1
= XGetGeometry(ob_display
, e
->xbutton
.window
,
248 &wjunk
, &junk1
, &junk2
, &w
, &h
, &b
, &ujunk
);
249 xerror_set_ignore(FALSE
);
251 if (e
->xbutton
.x
>= (signed)-b
&&
252 e
->xbutton
.y
>= (signed)-b
&&
253 e
->xbutton
.x
< (signed)(w
+b
) &&
254 e
->xbutton
.y
< (signed)(h
+b
)) {
256 /* double clicks happen if there were 2 in a row! */
257 if (lbutton
== button
&&
258 lwindow
== e
->xbutton
.window
&&
259 e
->xbutton
.time
- config_mouse_dclicktime
<=
265 lwindow
= e
->xbutton
.window
;
275 ltime
= e
->xbutton
.time
;
277 fire_binding(OB_MOUSE_ACTION_RELEASE
, context
,
278 client
, e
->xbutton
.state
,
284 fire_binding(OB_MOUSE_ACTION_CLICK
, context
,
285 client
, e
->xbutton
.state
,
291 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK
, context
,
292 client
, e
->xbutton
.state
,
301 context
= frame_context(client
, e
->xmotion
.window
, pwx
, pwy
);
302 context
= mouse_button_frame_context(context
, button
, state
);
304 if (ABS(e
->xmotion
.x_root
- px
) >= config_mouse_threshold
||
305 ABS(e
->xmotion
.y_root
- py
) >= config_mouse_threshold
) {
307 /* You can't drag on buttons */
308 if (context
== OB_FRAME_CONTEXT_MAXIMIZE
||
309 context
== OB_FRAME_CONTEXT_ALLDESKTOPS
||
310 context
== OB_FRAME_CONTEXT_SHADE
||
311 context
== OB_FRAME_CONTEXT_ICONIFY
||
312 context
== OB_FRAME_CONTEXT_ICON
||
313 context
== OB_FRAME_CONTEXT_CLOSE
)
316 fire_binding(OB_MOUSE_ACTION_MOTION
, context
,
317 client
, state
, button
, px
, py
, e
->xmotion
.time
);
325 g_assert_not_reached();
329 gboolean
mouse_bind(const gchar
*buttonstr
, const gchar
*contextstr
,
330 ObMouseAction mact
, ObAction
*action
)
333 ObFrameContext context
;
337 if (!translate_button(buttonstr
, &state
, &button
)) {
338 g_message(_("Invalid button '%s' in mouse binding"), buttonstr
);
342 context
= frame_context_from_string(contextstr
);
344 g_message(_("Invalid context '%s' in mouse binding"), contextstr
);
348 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
350 if (b
->state
== state
&& b
->button
== button
) {
351 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
356 /* when there are no modifiers in the binding, then the action cannot
358 if (!state
&& action
->data
.any
.interactive
) {
359 action
->data
.any
.interactive
= FALSE
;
360 action
->data
.inter
.final
= TRUE
;
363 /* add the binding */
364 b
= g_new0(ObMouseBinding
, 1);
367 b
->actions
[mact
] = g_slist_append(NULL
, action
);
368 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
373 void mouse_startup(gboolean reconfig
)
375 grab_all_clients(TRUE
);
378 void mouse_shutdown(gboolean reconfig
)
380 grab_all_clients(FALSE
);