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 Ben 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"
36 GSList
*actions
[OB_NUM_MOUSE_ACTIONS
]; /* lists of Action pointers */
39 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
40 co == OB_FRAME_CONTEXT_FRAME : FALSE)
41 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
42 co == OB_FRAME_CONTEXT_DESKTOP : \
43 co == OB_FRAME_CONTEXT_CLIENT)
45 /* Array of GSList*s of ObMouseBinding*s. */
46 static GSList
*bound_contexts
[OB_FRAME_NUM_CONTEXTS
];
48 ObFrameContext
mouse_button_frame_context(ObFrameContext context
,
52 ObFrameContext x
= context
;
54 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
55 ObMouseBinding
*b
= it
->data
;
57 if (b
->button
== button
)
62 case OB_FRAME_CONTEXT_NONE
:
63 case OB_FRAME_CONTEXT_DESKTOP
:
64 case OB_FRAME_CONTEXT_CLIENT
:
65 case OB_FRAME_CONTEXT_TITLEBAR
:
66 case OB_FRAME_CONTEXT_HANDLE
:
67 case OB_FRAME_CONTEXT_FRAME
:
68 case OB_FRAME_CONTEXT_MOVE_RESIZE
:
70 case OB_FRAME_CONTEXT_BLCORNER
:
71 case OB_FRAME_CONTEXT_BRCORNER
:
72 x
= OB_FRAME_CONTEXT_HANDLE
;
74 case OB_FRAME_CONTEXT_TLCORNER
:
75 case OB_FRAME_CONTEXT_TRCORNER
:
76 case OB_FRAME_CONTEXT_MAXIMIZE
:
77 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
78 case OB_FRAME_CONTEXT_SHADE
:
79 case OB_FRAME_CONTEXT_ICONIFY
:
80 case OB_FRAME_CONTEXT_ICON
:
81 case OB_FRAME_CONTEXT_CLOSE
:
82 x
= OB_FRAME_CONTEXT_TITLEBAR
;
84 case OB_FRAME_NUM_CONTEXTS
:
85 g_assert_not_reached();
91 void mouse_grab_for_client(ObClient
*client
, gboolean grab
)
96 for (i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
)
97 for (it
= bound_contexts
[i
]; it
; it
= g_slist_next(it
)) {
98 /* grab/ungrab the button */
99 ObMouseBinding
*b
= it
->data
;
104 if (FRAME_CONTEXT(i
, client
)) {
105 win
= client
->frame
->window
;
106 mode
= GrabModeAsync
;
107 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
108 } else if (CLIENT_CONTEXT(i
, client
)) {
109 win
= client
->frame
->plate
;
110 mode
= GrabModeSync
; /* this is handled in event */
111 mask
= ButtonPressMask
; /* can't catch more than this with Sync
112 mode the release event is
113 manufactured in event() */
117 grab_button_full(b
->button
, b
->state
, win
, mask
, mode
,
120 ungrab_button(b
->button
, b
->state
, win
);
124 static void grab_all_clients(gboolean grab
)
128 for (it
= client_list
; it
; it
= g_list_next(it
))
129 mouse_grab_for_client(it
->data
, grab
);
132 void mouse_unbind_all()
137 for(i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
) {
138 for (it
= bound_contexts
[i
]; it
; it
= g_slist_next(it
)) {
139 ObMouseBinding
*b
= it
->data
;
142 for (j
= 0; j
< OB_NUM_MOUSE_ACTIONS
; ++j
) {
145 for (it
= b
->actions
[j
]; it
; it
= g_slist_next(it
))
146 action_unref(it
->data
);
147 g_slist_free(b
->actions
[j
]);
151 g_slist_free(bound_contexts
[i
]);
152 bound_contexts
[i
] = NULL
;
156 static gboolean
fire_binding(ObMouseAction a
, ObFrameContext context
,
157 ObClient
*c
, guint state
,
158 guint button
, gint x
, gint y
)
163 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
165 if (b
->state
== state
&& b
->button
== button
)
168 /* if not bound, then nothing to do! */
169 if (it
== NULL
) return FALSE
;
171 action_run_mouse(b
->actions
[a
], c
, context
, state
, button
, x
, y
);
175 void mouse_event(ObClient
*client
, XEvent
*e
)
178 static guint button
= 0, state
= 0, lbutton
= 0;
179 static Window lwindow
= None
;
182 ObFrameContext context
;
183 gboolean click
= FALSE
;
184 gboolean dclick
= FALSE
;
188 context
= frame_context(client
, e
->xany
.window
);
189 context
= mouse_button_frame_context(context
, e
->xbutton
.button
);
191 px
= e
->xbutton
.x_root
;
192 py
= e
->xbutton
.y_root
;
193 button
= e
->xbutton
.button
;
194 state
= e
->xbutton
.state
;
196 fire_binding(OB_MOUSE_ACTION_PRESS
, context
,
197 client
, e
->xbutton
.state
,
199 e
->xbutton
.x_root
, e
->xbutton
.y_root
);
201 if (CLIENT_CONTEXT(context
, client
)) {
202 /* Replay the event, so it goes to the client*/
203 XAllowEvents(ob_display
, ReplayPointer
, event_curtime
);
204 /* Fall through to the release case! */
209 context
= frame_context(client
, e
->xany
.window
);
210 context
= mouse_button_frame_context(context
, e
->xbutton
.button
);
212 if (e
->xbutton
.button
== button
) {
213 /* clicks are only valid if its released over the window */
216 guint ujunk
, b
, w
, h
;
217 /* this can cause errors to occur when the window closes */
218 xerror_set_ignore(TRUE
);
219 junk1
= XGetGeometry(ob_display
, e
->xbutton
.window
,
220 &wjunk
, &junk1
, &junk2
, &w
, &h
, &b
, &ujunk
);
221 xerror_set_ignore(FALSE
);
223 if (e
->xbutton
.x
>= (signed)-b
&&
224 e
->xbutton
.y
>= (signed)-b
&&
225 e
->xbutton
.x
< (signed)(w
+b
) &&
226 e
->xbutton
.y
< (signed)(h
+b
)) {
228 /* double clicks happen if there were 2 in a row! */
229 if (lbutton
== button
&&
230 lwindow
== e
->xbutton
.window
&&
231 e
->xbutton
.time
- config_mouse_dclicktime
<=
237 lwindow
= e
->xbutton
.window
;
247 ltime
= e
->xbutton
.time
;
249 fire_binding(OB_MOUSE_ACTION_RELEASE
, context
,
250 client
, e
->xbutton
.state
,
252 e
->xbutton
.x_root
, e
->xbutton
.y_root
);
254 fire_binding(OB_MOUSE_ACTION_CLICK
, context
,
255 client
, e
->xbutton
.state
,
260 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK
, context
,
261 client
, e
->xbutton
.state
,
269 context
= frame_context(client
, e
->xany
.window
);
270 context
= mouse_button_frame_context(context
, button
);
272 if (ABS(e
->xmotion
.x_root
- px
) >=
273 config_mouse_threshold
||
274 ABS(e
->xmotion
.y_root
- py
) >=
275 config_mouse_threshold
) {
277 /* You can't drag on buttons */
278 if (context
== OB_FRAME_CONTEXT_MAXIMIZE
||
279 context
== OB_FRAME_CONTEXT_ALLDESKTOPS
||
280 context
== OB_FRAME_CONTEXT_SHADE
||
281 context
== OB_FRAME_CONTEXT_ICONIFY
||
282 context
== OB_FRAME_CONTEXT_ICON
||
283 context
== OB_FRAME_CONTEXT_CLOSE
)
286 fire_binding(OB_MOUSE_ACTION_MOTION
, context
,
287 client
, state
, button
, px
, py
);
295 g_assert_not_reached();
299 gboolean
mouse_bind(const gchar
*buttonstr
, const gchar
*contextstr
,
300 ObMouseAction mact
, ObAction
*action
)
303 ObFrameContext context
;
307 if (!translate_button(buttonstr
, &state
, &button
)) {
308 g_warning("invalid button '%s'", buttonstr
);
312 context
= frame_context_from_string(contextstr
);
314 g_warning("invalid context '%s'", contextstr
);
318 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
320 if (b
->state
== state
&& b
->button
== button
) {
321 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
326 /* when there are no modifiers in the binding, then the action cannot
328 if (!state
&& action
->data
.any
.interactive
) {
329 action
->data
.any
.interactive
= FALSE
;
330 action
->data
.inter
.final
= TRUE
;
333 /* add the binding */
334 b
= g_new0(ObMouseBinding
, 1);
337 b
->actions
[mact
] = g_slist_append(NULL
, action
);
338 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
343 void mouse_startup(gboolean reconfig
)
345 grab_all_clients(TRUE
);
348 void mouse_shutdown(gboolean reconfig
)
350 grab_all_clients(FALSE
);