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.
27 #include "translate.h"
30 #include "obt/display.h"
37 GSList *actions[OB_NUM_MOUSE_ACTIONS]; /* lists of Action pointers */
40 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
41 co == OB_FRAME_CONTEXT_FRAME : FALSE)
42 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
43 co == OB_FRAME_CONTEXT_DESKTOP : \
44 co == OB_FRAME_CONTEXT_CLIENT)
46 /* Array of GSList*s of ObMouseBinding*s. */
47 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
49 ObFrameContext mouse_button_frame_context(ObFrameContext context,
54 ObFrameContext x = context;
56 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
57 ObMouseBinding *b = it->data;
59 if (b->button == button && b->state == state)
64 case OB_FRAME_CONTEXT_NONE:
65 case OB_FRAME_CONTEXT_DESKTOP:
66 case OB_FRAME_CONTEXT_CLIENT:
67 case OB_FRAME_CONTEXT_TITLEBAR:
68 case OB_FRAME_CONTEXT_FRAME:
69 case OB_FRAME_CONTEXT_MOVE_RESIZE:
70 case OB_FRAME_CONTEXT_LEFT:
71 case OB_FRAME_CONTEXT_RIGHT:
73 case OB_FRAME_CONTEXT_ROOT:
74 x = OB_FRAME_CONTEXT_DESKTOP;
76 case OB_FRAME_CONTEXT_BOTTOM:
77 case OB_FRAME_CONTEXT_BLCORNER:
78 case OB_FRAME_CONTEXT_BRCORNER:
79 x = OB_FRAME_CONTEXT_BOTTOM;
81 case OB_FRAME_CONTEXT_TLCORNER:
82 case OB_FRAME_CONTEXT_TRCORNER:
83 case OB_FRAME_CONTEXT_TOP:
84 case OB_FRAME_CONTEXT_MAXIMIZE:
85 case OB_FRAME_CONTEXT_ALLDESKTOPS:
86 case OB_FRAME_CONTEXT_SHADE:
87 case OB_FRAME_CONTEXT_ICONIFY:
88 case OB_FRAME_CONTEXT_ICON:
89 case OB_FRAME_CONTEXT_CLOSE:
90 x = OB_FRAME_CONTEXT_TITLEBAR;
92 case OB_FRAME_NUM_CONTEXTS:
93 g_assert_not_reached();
96 /* allow for multiple levels of fall-through */
98 return mouse_button_frame_context(x, button, state);
103 void mouse_grab_for_client(ObClient *client, gboolean grab)
108 for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
109 for (it = bound_contexts[i]; it; it = g_slist_next(it)) {
110 /* grab/ungrab the button */
111 ObMouseBinding *b = it->data;
116 if (FRAME_CONTEXT(i, client)) {
117 win = client->frame->window;
118 mode = GrabModeAsync;
119 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
120 } else if (CLIENT_CONTEXT(i, client)) {
121 win = client->window;
122 mode = GrabModeSync; /* this is handled in event */
123 mask = ButtonPressMask; /* can't catch more than this with Sync
124 mode the release event is
125 manufactured in event() */
129 grab_button_full(b->button, b->state, win, mask, mode,
132 ungrab_button(b->button, b->state, win);
136 static void grab_all_clients(gboolean grab)
140 for (it = client_list; it; it = g_list_next(it))
141 mouse_grab_for_client(it->data, grab);
144 void mouse_unbind_all(void)
149 for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
150 for (it = bound_contexts[i]; it; it = g_slist_next(it)) {
151 ObMouseBinding *b = it->data;
154 for (j = 0; j < OB_NUM_MOUSE_ACTIONS; ++j) {
157 for (jt = b->actions[j]; jt; jt = g_slist_next(jt))
158 actions_act_unref(jt->data);
159 g_slist_free(b->actions[j]);
163 g_slist_free(bound_contexts[i]);
164 bound_contexts[i] = NULL;
168 static ObUserAction mouse_action_to_user_action(ObMouseAction a)
171 case OB_MOUSE_ACTION_PRESS: return OB_USER_ACTION_MOUSE_PRESS;
172 case OB_MOUSE_ACTION_RELEASE: return OB_USER_ACTION_MOUSE_RELEASE;
173 case OB_MOUSE_ACTION_CLICK: return OB_USER_ACTION_MOUSE_CLICK;
174 case OB_MOUSE_ACTION_DOUBLE_CLICK:
175 return OB_USER_ACTION_MOUSE_DOUBLE_CLICK;
176 case OB_MOUSE_ACTION_MOTION: return OB_USER_ACTION_MOUSE_MOTION;
178 g_assert_not_reached();
182 static gboolean fire_binding(ObMouseAction a, ObFrameContext context,
183 ObClient *c, guint state,
184 guint button, gint x, gint y)
189 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
191 if (b->state == state && b->button == button)
194 /* if not bound, then nothing to do! */
195 if (it == NULL) return FALSE;
197 actions_run_acts(b->actions[a], mouse_action_to_user_action(a),
198 state, x, y, button, context, c);
202 void mouse_event(ObClient *client, XEvent *e)
205 static guint button = 0, state = 0, lbutton = 0;
206 static Window lwindow = None;
207 static gint px, py, pwx = -1, pwy = -1;
209 ObFrameContext context;
210 gboolean click = FALSE;
211 gboolean dclick = FALSE;
215 context = frame_context(client, e->xbutton.window,
216 e->xbutton.x, e->xbutton.y);
217 context = mouse_button_frame_context(context, e->xbutton.button,
220 px = e->xbutton.x_root;
221 py = e->xbutton.y_root;
222 if (!button) pwx = e->xbutton.x;
223 if (!button) pwy = e->xbutton.y;
224 button = e->xbutton.button;
225 state = e->xbutton.state;
227 fire_binding(OB_MOUSE_ACTION_PRESS, context,
228 client, e->xbutton.state,
230 e->xbutton.x_root, e->xbutton.y_root);
232 /* if the bindings grab the pointer, there won't be a ButtonRelease
234 if (grab_on_pointer())
237 if (CLIENT_CONTEXT(context, client)) {
238 /* Replay the event, so it goes to the client*/
239 XAllowEvents(obt_display, ReplayPointer, event_curtime);
240 /* Fall through to the release case! */
245 /* use where the press occured in the window */
246 context = frame_context(client, e->xbutton.window, pwx, pwy);
247 context = mouse_button_frame_context(context, e->xbutton.button,
250 if (e->xbutton.button == button)
253 if (e->xbutton.button == button) {
254 /* clicks are only valid if its released over the window */
257 guint ujunk, b, w, h;
258 /* this can cause errors to occur when the window closes */
259 obt_display_ignore_errors(TRUE);
260 junk1 = XGetGeometry(obt_display, e->xbutton.window,
261 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
262 obt_display_ignore_errors(FALSE);
264 if (e->xbutton.x >= (signed)-b &&
265 e->xbutton.y >= (signed)-b &&
266 e->xbutton.x < (signed)(w+b) &&
267 e->xbutton.y < (signed)(h+b)) {
269 /* double clicks happen if there were 2 in a row! */
270 if (lbutton == button &&
271 lwindow == e->xbutton.window &&
272 e->xbutton.time - config_mouse_dclicktime <=
278 lwindow = e->xbutton.window;
288 ltime = e->xbutton.time;
290 fire_binding(OB_MOUSE_ACTION_RELEASE, context,
291 client, e->xbutton.state,
296 fire_binding(OB_MOUSE_ACTION_CLICK, context,
297 client, e->xbutton.state,
302 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
303 client, e->xbutton.state,
311 context = frame_context(client, e->xmotion.window, pwx, pwy);
312 context = mouse_button_frame_context(context, button, state);
314 if (ABS(e->xmotion.x_root - px) >= config_mouse_threshold ||
315 ABS(e->xmotion.y_root - py) >= config_mouse_threshold) {
317 /* You can't drag on buttons */
318 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
319 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
320 context == OB_FRAME_CONTEXT_SHADE ||
321 context == OB_FRAME_CONTEXT_ICONIFY ||
322 context == OB_FRAME_CONTEXT_ICON ||
323 context == OB_FRAME_CONTEXT_CLOSE)
326 fire_binding(OB_MOUSE_ACTION_MOTION, context,
327 client, state, button, px, py);
335 g_assert_not_reached();
339 gboolean mouse_bind(const gchar *buttonstr, const gchar *contextstr,
340 ObMouseAction mact, ObActionsAct *action)
343 ObFrameContext context;
347 if (!translate_button(buttonstr, &state, &button)) {
348 g_message(_("Invalid button '%s' in mouse binding"), buttonstr);
352 context = frame_context_from_string(contextstr);
354 g_message(_("Invalid context '%s' in mouse binding"), contextstr);
358 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
360 if (b->state == state && b->button == button) {
361 b->actions[mact] = g_slist_append(b->actions[mact], action);
366 /* add the binding */
367 b = g_new0(ObMouseBinding, 1);
370 b->actions[mact] = g_slist_append(NULL, action);
371 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
376 void mouse_startup(gboolean reconfig)
378 grab_all_clients(TRUE);
381 void mouse_shutdown(gboolean reconfig)
383 grab_all_clients(FALSE);