1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 mouse.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
28 #include "translate.h"
35 GSList
*actions
[OB_NUM_MOUSE_ACTIONS
]; /* lists of Action pointers */
38 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
39 co == OB_FRAME_CONTEXT_FRAME : FALSE)
40 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
41 co == OB_FRAME_CONTEXT_DESKTOP : \
42 co == OB_FRAME_CONTEXT_CLIENT)
44 /* Array of GSList*s of ObMouseBinding*s. */
45 static GSList
*bound_contexts
[OB_FRAME_NUM_CONTEXTS
];
47 ObFrameContext
mouse_button_frame_context(ObFrameContext context
,
51 ObFrameContext x
= context
;
53 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
54 ObMouseBinding
*b
= it
->data
;
56 if (b
->button
== button
)
61 case OB_FRAME_CONTEXT_NONE
:
62 case OB_FRAME_CONTEXT_DESKTOP
:
63 case OB_FRAME_CONTEXT_CLIENT
:
64 case OB_FRAME_CONTEXT_TITLEBAR
:
65 case OB_FRAME_CONTEXT_HANDLE
:
66 case OB_FRAME_CONTEXT_FRAME
:
67 case OB_FRAME_CONTEXT_MOVE_RESIZE
:
69 case OB_FRAME_CONTEXT_BLCORNER
:
70 case OB_FRAME_CONTEXT_BRCORNER
:
71 x
= OB_FRAME_CONTEXT_HANDLE
;
73 case OB_FRAME_CONTEXT_TLCORNER
:
74 case OB_FRAME_CONTEXT_TRCORNER
:
75 case OB_FRAME_CONTEXT_MAXIMIZE
:
76 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
77 case OB_FRAME_CONTEXT_SHADE
:
78 case OB_FRAME_CONTEXT_ICONIFY
:
79 case OB_FRAME_CONTEXT_ICON
:
80 case OB_FRAME_CONTEXT_CLOSE
:
81 x
= OB_FRAME_CONTEXT_TITLEBAR
;
83 case OB_FRAME_NUM_CONTEXTS
:
84 g_assert_not_reached();
90 void mouse_grab_for_client(ObClient
*client
, gboolean grab
)
95 for (i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
)
96 for (it
= bound_contexts
[i
]; it
!= NULL
; it
= g_slist_next(it
)) {
97 /* grab/ungrab the button */
98 ObMouseBinding
*b
= it
->data
;
103 if (FRAME_CONTEXT(i
, client
)) {
104 win
= client
->frame
->window
;
105 mode
= GrabModeAsync
;
106 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
107 } else if (CLIENT_CONTEXT(i
, client
)) {
108 win
= client
->frame
->plate
;
109 mode
= GrabModeSync
; /* this is handled in event */
110 mask
= ButtonPressMask
; /* can't catch more than this with Sync
111 mode the release event is
112 manufactured in event() */
116 grab_button_full(b
->button
, b
->state
, win
, mask
, mode
,
119 ungrab_button(b
->button
, b
->state
, win
);
123 static void grab_all_clients(gboolean grab
)
127 for (it
= client_list
; it
!= NULL
; it
= it
->next
)
128 mouse_grab_for_client(it
->data
, grab
);
131 void mouse_unbind_all()
136 for(i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
) {
137 for (it
= bound_contexts
[i
]; it
!= NULL
; it
= it
->next
) {
138 ObMouseBinding
*b
= it
->data
;
141 for (j
= 0; j
< OB_NUM_MOUSE_ACTIONS
; ++j
) {
144 for (it
= b
->actions
[j
]; it
; it
= it
->next
)
145 action_unref(it
->data
);
146 g_slist_free(b
->actions
[j
]);
150 g_slist_free(bound_contexts
[i
]);
151 bound_contexts
[i
] = NULL
;
155 static gboolean
fire_binding(ObMouseAction a
, ObFrameContext context
,
156 ObClient
*c
, guint state
,
157 guint button
, gint x
, gint y
)
162 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
164 if (b
->state
== state
&& b
->button
== button
)
167 /* if not bound, then nothing to do! */
168 if (it
== NULL
) return FALSE
;
170 action_run_mouse(b
->actions
[a
], c
, context
, state
, button
, x
, y
);
174 void mouse_event(ObClient
*client
, XEvent
*e
)
177 static guint button
= 0, state
= 0, lbutton
= 0;
178 static Window lwindow
= None
;
181 ObFrameContext context
;
182 gboolean click
= FALSE
;
183 gboolean dclick
= FALSE
;
187 context
= frame_context(client
, e
->xany
.window
);
188 context
= mouse_button_frame_context(context
, e
->xbutton
.button
);
190 px
= e
->xbutton
.x_root
;
191 py
= e
->xbutton
.y_root
;
192 button
= e
->xbutton
.button
;
193 state
= e
->xbutton
.state
;
195 fire_binding(OB_MOUSE_ACTION_PRESS
, context
,
196 client
, e
->xbutton
.state
,
198 e
->xbutton
.x_root
, e
->xbutton
.y_root
);
200 if (CLIENT_CONTEXT(context
, client
)) {
201 /* Replay the event, so it goes to the client*/
202 XAllowEvents(ob_display
, ReplayPointer
, event_lasttime
);
203 /* Fall through to the release case! */
208 context
= frame_context(client
, e
->xany
.window
);
209 context
= mouse_button_frame_context(context
, e
->xbutton
.button
);
211 if (e
->xbutton
.button
== button
) {
212 /* clicks are only valid if its released over the window */
215 guint ujunk
, b
, w
, h
;
216 /* this can cause errors to occur when the window closes */
217 xerror_set_ignore(TRUE
);
218 junk1
= XGetGeometry(ob_display
, e
->xbutton
.window
,
219 &wjunk
, &junk1
, &junk2
, &w
, &h
, &b
, &ujunk
);
220 xerror_set_ignore(FALSE
);
222 if (e
->xbutton
.x
>= (signed)-b
&&
223 e
->xbutton
.y
>= (signed)-b
&&
224 e
->xbutton
.x
< (signed)(w
+b
) &&
225 e
->xbutton
.y
< (signed)(h
+b
)) {
227 /* double clicks happen if there were 2 in a row! */
228 if (lbutton
== button
&&
229 lwindow
== e
->xbutton
.window
&&
230 e
->xbutton
.time
- config_mouse_dclicktime
<=
236 lwindow
= e
->xbutton
.window
;
246 ltime
= e
->xbutton
.time
;
248 fire_binding(OB_MOUSE_ACTION_RELEASE
, context
,
249 client
, e
->xbutton
.state
,
251 e
->xbutton
.x_root
, e
->xbutton
.y_root
);
253 fire_binding(OB_MOUSE_ACTION_CLICK
, context
,
254 client
, e
->xbutton
.state
,
259 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK
, context
,
260 client
, e
->xbutton
.state
,
268 context
= frame_context(client
, e
->xany
.window
);
269 context
= mouse_button_frame_context(context
, button
);
271 if (ABS(e
->xmotion
.x_root
- px
) >=
272 config_mouse_threshold
||
273 ABS(e
->xmotion
.y_root
- py
) >=
274 config_mouse_threshold
) {
276 /* You can't drag on buttons */
277 if (context
== OB_FRAME_CONTEXT_MAXIMIZE
||
278 context
== OB_FRAME_CONTEXT_ALLDESKTOPS
||
279 context
== OB_FRAME_CONTEXT_SHADE
||
280 context
== OB_FRAME_CONTEXT_ICONIFY
||
281 context
== OB_FRAME_CONTEXT_ICON
||
282 context
== OB_FRAME_CONTEXT_CLOSE
)
285 fire_binding(OB_MOUSE_ACTION_MOTION
, context
,
286 client
, state
, button
, px
, py
);
294 g_assert_not_reached();
298 gboolean
mouse_bind(const gchar
*buttonstr
, const gchar
*contextstr
,
299 ObMouseAction mact
, ObAction
*action
)
302 ObFrameContext context
;
306 if (!translate_button(buttonstr
, &state
, &button
)) {
307 g_warning("invalid button '%s'", buttonstr
);
311 context
= frame_context_from_string(contextstr
);
313 g_warning("invalid context '%s'", contextstr
);
317 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
){
319 if (b
->state
== state
&& b
->button
== button
) {
320 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
325 /* when there are no modifiers in the binding, then the action cannot
327 if (!state
&& action
->data
.any
.interactive
) {
328 action
->data
.any
.interactive
= FALSE
;
329 action
->data
.inter
.final
= TRUE
;
332 /* add the binding */
333 b
= g_new0(ObMouseBinding
, 1);
336 b
->actions
[mact
] = g_slist_append(NULL
, action
);
337 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
342 void mouse_startup(gboolean reconfig
)
344 grab_all_clients(TRUE
);
347 void mouse_shutdown(gboolean reconfig
)
349 grab_all_clients(FALSE
);