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_BOTTOM
:
75 case OB_FRAME_CONTEXT_BLCORNER
:
76 case OB_FRAME_CONTEXT_BRCORNER
:
77 x
= OB_FRAME_CONTEXT_BOTTOM
;
79 case OB_FRAME_CONTEXT_TLCORNER
:
80 case OB_FRAME_CONTEXT_TRCORNER
:
81 case OB_FRAME_CONTEXT_TOP
:
82 case OB_FRAME_CONTEXT_MAXIMIZE
:
83 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
84 case OB_FRAME_CONTEXT_SHADE
:
85 case OB_FRAME_CONTEXT_ICONIFY
:
86 case OB_FRAME_CONTEXT_ICON
:
87 case OB_FRAME_CONTEXT_CLOSE
:
88 x
= OB_FRAME_CONTEXT_TITLEBAR
;
90 case OB_FRAME_NUM_CONTEXTS
:
91 g_assert_not_reached();
94 /* allow for multiple levels of fall-through */
96 return mouse_button_frame_context(x
, button
, state
);
101 void mouse_grab_for_client(ObClient
*client
, gboolean grab
)
106 for (i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
)
107 for (it
= bound_contexts
[i
]; it
; it
= g_slist_next(it
)) {
108 /* grab/ungrab the button */
109 ObMouseBinding
*b
= it
->data
;
114 if (FRAME_CONTEXT(i
, client
)) {
115 win
= client
->frame
->window
;
116 mode
= GrabModeAsync
;
117 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
118 } else if (CLIENT_CONTEXT(i
, client
)) {
119 win
= client
->frame
->plate
;
120 mode
= GrabModeSync
; /* this is handled in event */
121 mask
= ButtonPressMask
; /* can't catch more than this with Sync
122 mode the release event is
123 manufactured in event() */
127 grab_button_full(b
->button
, b
->state
, win
, mask
, mode
,
130 ungrab_button(b
->button
, b
->state
, win
);
134 static void grab_all_clients(gboolean grab
)
138 for (it
= client_list
; it
; it
= g_list_next(it
))
139 mouse_grab_for_client(it
->data
, grab
);
142 void mouse_unbind_all()
147 for(i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
) {
148 for (it
= bound_contexts
[i
]; it
; it
= g_slist_next(it
)) {
149 ObMouseBinding
*b
= it
->data
;
152 for (j
= 0; j
< OB_NUM_MOUSE_ACTIONS
; ++j
) {
155 for (it
= b
->actions
[j
]; it
; it
= g_slist_next(it
))
156 action_unref(it
->data
);
157 g_slist_free(b
->actions
[j
]);
161 g_slist_free(bound_contexts
[i
]);
162 bound_contexts
[i
] = NULL
;
166 static gboolean
fire_binding(ObMouseAction a
, ObFrameContext context
,
167 ObClient
*c
, guint state
,
168 guint button
, gint x
, gint y
, Time time
)
173 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
175 if (b
->state
== state
&& b
->button
== button
)
178 /* if not bound, then nothing to do! */
179 if (it
== NULL
) return FALSE
;
181 action_run_mouse(b
->actions
[a
], c
, context
, state
, button
, x
, y
, time
);
185 void mouse_event(ObClient
*client
, XEvent
*e
)
188 static guint button
= 0, state
= 0, lbutton
= 0;
189 static Window lwindow
= None
;
190 static gint px
, py
, pwx
= -1, pwy
= -1;
192 ObFrameContext context
;
193 gboolean click
= FALSE
;
194 gboolean dclick
= FALSE
;
198 context
= frame_context(client
, e
->xbutton
.window
,
199 e
->xbutton
.x
, e
->xbutton
.y
);
200 context
= mouse_button_frame_context(context
, e
->xbutton
.button
,
203 px
= e
->xbutton
.x_root
;
204 py
= e
->xbutton
.y_root
;
205 if (!button
) pwx
= e
->xbutton
.x
;
206 if (!button
) pwy
= e
->xbutton
.y
;
207 button
= e
->xbutton
.button
;
208 state
= e
->xbutton
.state
;
210 fire_binding(OB_MOUSE_ACTION_PRESS
, context
,
211 client
, e
->xbutton
.state
,
213 e
->xbutton
.x_root
, e
->xbutton
.y_root
,
216 /* if the bindings grab the pointer, there won't be a ButtonRelease
218 if (grab_on_pointer())
221 if (CLIENT_CONTEXT(context
, client
)) {
222 /* Replay the event, so it goes to the client*/
223 XAllowEvents(ob_display
, ReplayPointer
, event_curtime
);
224 /* Fall through to the release case! */
229 /* use where the press occured in the window */
230 context
= frame_context(client
, e
->xbutton
.window
, pwx
, pwy
);
231 context
= mouse_button_frame_context(context
, e
->xbutton
.button
,
234 if (e
->xbutton
.button
== button
)
237 if (e
->xbutton
.button
== button
) {
238 /* clicks are only valid if its released over the window */
241 guint ujunk
, b
, w
, h
;
242 /* this can cause errors to occur when the window closes */
243 xerror_set_ignore(TRUE
);
244 junk1
= XGetGeometry(ob_display
, e
->xbutton
.window
,
245 &wjunk
, &junk1
, &junk2
, &w
, &h
, &b
, &ujunk
);
246 xerror_set_ignore(FALSE
);
248 if (e
->xbutton
.x
>= (signed)-b
&&
249 e
->xbutton
.y
>= (signed)-b
&&
250 e
->xbutton
.x
< (signed)(w
+b
) &&
251 e
->xbutton
.y
< (signed)(h
+b
)) {
253 /* double clicks happen if there were 2 in a row! */
254 if (lbutton
== button
&&
255 lwindow
== e
->xbutton
.window
&&
256 e
->xbutton
.time
- config_mouse_dclicktime
<=
262 lwindow
= e
->xbutton
.window
;
272 ltime
= e
->xbutton
.time
;
274 fire_binding(OB_MOUSE_ACTION_RELEASE
, context
,
275 client
, e
->xbutton
.state
,
281 fire_binding(OB_MOUSE_ACTION_CLICK
, context
,
282 client
, e
->xbutton
.state
,
288 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK
, context
,
289 client
, e
->xbutton
.state
,
298 context
= frame_context(client
, e
->xmotion
.window
, pwx
, pwy
);
299 context
= mouse_button_frame_context(context
, button
, state
);
301 if (ABS(e
->xmotion
.x_root
- px
) >= config_mouse_threshold
||
302 ABS(e
->xmotion
.y_root
- py
) >= config_mouse_threshold
) {
304 /* You can't drag on buttons */
305 if (context
== OB_FRAME_CONTEXT_MAXIMIZE
||
306 context
== OB_FRAME_CONTEXT_ALLDESKTOPS
||
307 context
== OB_FRAME_CONTEXT_SHADE
||
308 context
== OB_FRAME_CONTEXT_ICONIFY
||
309 context
== OB_FRAME_CONTEXT_ICON
||
310 context
== OB_FRAME_CONTEXT_CLOSE
)
313 fire_binding(OB_MOUSE_ACTION_MOTION
, context
,
314 client
, state
, button
, px
, py
, e
->xmotion
.time
);
322 g_assert_not_reached();
326 gboolean
mouse_bind(const gchar
*buttonstr
, const gchar
*contextstr
,
327 ObMouseAction mact
, ObAction
*action
)
330 ObFrameContext context
;
334 if (!translate_button(buttonstr
, &state
, &button
)) {
335 g_message(_("Invalid button '%s' in mouse binding"), buttonstr
);
339 context
= frame_context_from_string(contextstr
);
341 g_message(_("Invalid context '%s' in mouse binding"), contextstr
);
345 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
347 if (b
->state
== state
&& b
->button
== button
) {
348 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
353 /* when there are no modifiers in the binding, then the action cannot
355 if (!state
&& action
->data
.any
.interactive
) {
356 action
->data
.any
.interactive
= FALSE
;
357 action
->data
.inter
.final
= TRUE
;
360 /* add the binding */
361 b
= g_new0(ObMouseBinding
, 1);
364 b
->actions
[mact
] = g_slist_append(NULL
, action
);
365 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
370 void mouse_startup(gboolean reconfig
)
372 grab_all_clients(TRUE
);
375 void mouse_shutdown(gboolean reconfig
)
377 grab_all_clients(FALSE
);