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, Time time)
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, time);
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,
202 if (CLIENT_CONTEXT(context, client)) {
203 /* Replay the event, so it goes to the client*/
204 XAllowEvents(ob_display, ReplayPointer, event_curtime);
205 /* Fall through to the release case! */
210 context = frame_context(client, e->xany.window);
211 context = mouse_button_frame_context(context, e->xbutton.button);
213 if (e->xbutton.button == button) {
214 /* clicks are only valid if its released over the window */
217 guint ujunk, b, w, h;
218 /* this can cause errors to occur when the window closes */
219 xerror_set_ignore(TRUE);
220 junk1 = XGetGeometry(ob_display, e->xbutton.window,
221 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
222 xerror_set_ignore(FALSE);
224 if (e->xbutton.x >= (signed)-b &&
225 e->xbutton.y >= (signed)-b &&
226 e->xbutton.x < (signed)(w+b) &&
227 e->xbutton.y < (signed)(h+b)) {
229 /* double clicks happen if there were 2 in a row! */
230 if (lbutton == button &&
231 lwindow == e->xbutton.window &&
232 e->xbutton.time - config_mouse_dclicktime <=
238 lwindow = e->xbutton.window;
248 ltime = e->xbutton.time;
250 fire_binding(OB_MOUSE_ACTION_RELEASE, context,
251 client, e->xbutton.state,
253 e->xbutton.x_root, e->xbutton.y_root,
256 fire_binding(OB_MOUSE_ACTION_CLICK, context,
257 client, e->xbutton.state,
263 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
264 client, e->xbutton.state,
273 context = frame_context(client, e->xany.window);
274 context = mouse_button_frame_context(context, button);
276 if (ABS(e->xmotion.x_root - px) >=
277 config_mouse_threshold ||
278 ABS(e->xmotion.y_root - py) >=
279 config_mouse_threshold) {
281 /* You can't drag on buttons */
282 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
283 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
284 context == OB_FRAME_CONTEXT_SHADE ||
285 context == OB_FRAME_CONTEXT_ICONIFY ||
286 context == OB_FRAME_CONTEXT_ICON ||
287 context == OB_FRAME_CONTEXT_CLOSE)
290 fire_binding(OB_MOUSE_ACTION_MOTION, context,
291 client, state, button, px, py, e->xmotion.time);
299 g_assert_not_reached();
303 gboolean mouse_bind(const gchar *buttonstr, const gchar *contextstr,
304 ObMouseAction mact, ObAction *action)
307 ObFrameContext context;
311 if (!translate_button(buttonstr, &state, &button)) {
312 g_warning("invalid button '%s'", buttonstr);
316 context = frame_context_from_string(contextstr);
318 g_warning("invalid context '%s'", contextstr);
322 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
324 if (b->state == state && b->button == button) {
325 b->actions[mact] = g_slist_append(b->actions[mact], action);
330 /* when there are no modifiers in the binding, then the action cannot
332 if (!state && action->data.any.interactive) {
333 action->data.any.interactive = FALSE;
334 action->data.inter.final = TRUE;
337 /* add the binding */
338 b = g_new0(ObMouseBinding, 1);
341 b->actions[mact] = g_slist_append(NULL, action);
342 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
347 void mouse_startup(gboolean reconfig)
349 grab_all_clients(TRUE);
352 void mouse_shutdown(gboolean reconfig)
354 grab_all_clients(FALSE);