]> Dogcows Code - chaz/openbox/blob - openbox/mouse.c
themerc.xsd is not executable
[chaz/openbox] / openbox / mouse.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 mouse.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
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.
11
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.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "openbox.h"
21 #include "config.h"
22 #include "xerror.h"
23 #include "action.h"
24 #include "event.h"
25 #include "client.h"
26 #include "prop.h"
27 #include "grab.h"
28 #include "frame.h"
29 #include "translate.h"
30 #include "mouse.h"
31 #include "gettext.h"
32
33 #include <glib.h>
34
35 typedef struct {
36 guint state;
37 guint button;
38 GSList *actions[OB_NUM_MOUSE_ACTIONS]; /* lists of Action pointers */
39 } ObMouseBinding;
40
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)
46
47 /* Array of GSList*s of ObMouseBinding*s. */
48 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
49
50 ObFrameContext mouse_button_frame_context(ObFrameContext context,
51 guint button)
52 {
53 GSList *it;
54 ObFrameContext x = context;
55
56 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
57 ObMouseBinding *b = it->data;
58
59 if (b->button == button)
60 return context;
61 }
62
63 switch (context) {
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 break;
71 case OB_FRAME_CONTEXT_BOTTOM:
72 case OB_FRAME_CONTEXT_BLCORNER:
73 case OB_FRAME_CONTEXT_BRCORNER:
74 x = OB_FRAME_CONTEXT_BOTTOM;
75 break;
76 case OB_FRAME_CONTEXT_TLCORNER:
77 case OB_FRAME_CONTEXT_TRCORNER:
78 case OB_FRAME_CONTEXT_TOP:
79 case OB_FRAME_CONTEXT_MAXIMIZE:
80 case OB_FRAME_CONTEXT_ALLDESKTOPS:
81 case OB_FRAME_CONTEXT_SHADE:
82 case OB_FRAME_CONTEXT_ICONIFY:
83 case OB_FRAME_CONTEXT_ICON:
84 case OB_FRAME_CONTEXT_CLOSE:
85 x = OB_FRAME_CONTEXT_TITLEBAR;
86 break;
87 case OB_FRAME_NUM_CONTEXTS:
88 g_assert_not_reached();
89 }
90
91 return x;
92 }
93
94 void mouse_grab_for_client(ObClient *client, gboolean grab)
95 {
96 gint i;
97 GSList *it;
98
99 for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
100 for (it = bound_contexts[i]; it; it = g_slist_next(it)) {
101 /* grab/ungrab the button */
102 ObMouseBinding *b = it->data;
103 Window win;
104 gint mode;
105 guint mask;
106
107 if (FRAME_CONTEXT(i, client)) {
108 win = client->frame->window;
109 mode = GrabModeAsync;
110 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
111 } else if (CLIENT_CONTEXT(i, client)) {
112 win = client->frame->plate;
113 mode = GrabModeSync; /* this is handled in event */
114 mask = ButtonPressMask; /* can't catch more than this with Sync
115 mode the release event is
116 manufactured in event() */
117 } else continue;
118
119 if (grab)
120 grab_button_full(b->button, b->state, win, mask, mode,
121 OB_CURSOR_NONE);
122 else
123 ungrab_button(b->button, b->state, win);
124 }
125 }
126
127 static void grab_all_clients(gboolean grab)
128 {
129 GList *it;
130
131 for (it = client_list; it; it = g_list_next(it))
132 mouse_grab_for_client(it->data, grab);
133 }
134
135 void mouse_unbind_all()
136 {
137 gint i;
138 GSList *it;
139
140 for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
141 for (it = bound_contexts[i]; it; it = g_slist_next(it)) {
142 ObMouseBinding *b = it->data;
143 gint j;
144
145 for (j = 0; j < OB_NUM_MOUSE_ACTIONS; ++j) {
146 GSList *it;
147
148 for (it = b->actions[j]; it; it = g_slist_next(it))
149 action_unref(it->data);
150 g_slist_free(b->actions[j]);
151 }
152 g_free(b);
153 }
154 g_slist_free(bound_contexts[i]);
155 bound_contexts[i] = NULL;
156 }
157 }
158
159 static gboolean fire_binding(ObMouseAction a, ObFrameContext context,
160 ObClient *c, guint state,
161 guint button, gint x, gint y, Time time)
162 {
163 GSList *it;
164 ObMouseBinding *b;
165
166 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
167 b = it->data;
168 if (b->state == state && b->button == button)
169 break;
170 }
171 /* if not bound, then nothing to do! */
172 if (it == NULL) return FALSE;
173
174 action_run_mouse(b->actions[a], c, context, state, button, x, y, time);
175 return TRUE;
176 }
177
178 void mouse_event(ObClient *client, XEvent *e)
179 {
180 static Time ltime;
181 static guint button = 0, state = 0, lbutton = 0;
182 static Window lwindow = None;
183 static gint px, py, pwx = -1, pwy = -1;
184
185 ObFrameContext context;
186 gboolean click = FALSE;
187 gboolean dclick = FALSE;
188
189 switch (e->type) {
190 case ButtonPress:
191 context = frame_context(client, e->xbutton.window,
192 e->xbutton.x, e->xbutton.y);
193 context = mouse_button_frame_context(context, e->xbutton.button);
194
195 px = e->xbutton.x_root;
196 py = e->xbutton.y_root;
197 if (!button) pwx = e->xbutton.x;
198 if (!button) pwy = e->xbutton.y;
199 button = e->xbutton.button;
200 state = e->xbutton.state;
201
202 fire_binding(OB_MOUSE_ACTION_PRESS, context,
203 client, e->xbutton.state,
204 e->xbutton.button,
205 e->xbutton.x_root, e->xbutton.y_root,
206 e->xbutton.time);
207
208 if (CLIENT_CONTEXT(context, client)) {
209 /* Replay the event, so it goes to the client*/
210 XAllowEvents(ob_display, ReplayPointer, event_curtime);
211 /* Fall through to the release case! */
212 } else
213 break;
214
215 case ButtonRelease:
216 /* use where the press occured in the window */
217 context = frame_context(client, e->xbutton.window, pwx, pwy);
218 context = mouse_button_frame_context(context, e->xbutton.button);
219
220 if (e->xbutton.button == button)
221 pwx = pwy = -1;
222
223 if (e->xbutton.button == button) {
224 /* clicks are only valid if its released over the window */
225 gint junk1, junk2;
226 Window wjunk;
227 guint ujunk, b, w, h;
228 /* this can cause errors to occur when the window closes */
229 xerror_set_ignore(TRUE);
230 junk1 = XGetGeometry(ob_display, e->xbutton.window,
231 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
232 xerror_set_ignore(FALSE);
233 if (junk1) {
234 if (e->xbutton.x >= (signed)-b &&
235 e->xbutton.y >= (signed)-b &&
236 e->xbutton.x < (signed)(w+b) &&
237 e->xbutton.y < (signed)(h+b)) {
238 click = TRUE;
239 /* double clicks happen if there were 2 in a row! */
240 if (lbutton == button &&
241 lwindow == e->xbutton.window &&
242 e->xbutton.time - config_mouse_dclicktime <=
243 ltime) {
244 dclick = TRUE;
245 lbutton = 0;
246 } else {
247 lbutton = button;
248 lwindow = e->xbutton.window;
249 }
250 } else {
251 lbutton = 0;
252 lwindow = None;
253 }
254 }
255
256 button = 0;
257 state = 0;
258 ltime = e->xbutton.time;
259 }
260 fire_binding(OB_MOUSE_ACTION_RELEASE, context,
261 client, e->xbutton.state,
262 e->xbutton.button,
263 e->xbutton.x_root,
264 e->xbutton.y_root,
265 e->xbutton.time);
266 if (click)
267 fire_binding(OB_MOUSE_ACTION_CLICK, context,
268 client, e->xbutton.state,
269 e->xbutton.button,
270 e->xbutton.x_root,
271 e->xbutton.y_root,
272 e->xbutton.time);
273 if (dclick)
274 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
275 client, e->xbutton.state,
276 e->xbutton.button,
277 e->xbutton.x_root,
278 e->xbutton.y_root,
279 e->xbutton.time);
280 break;
281
282 case MotionNotify:
283 if (button) {
284 context = frame_context(client, e->xmotion.window, pwx, pwy);
285 context = mouse_button_frame_context(context, button);
286
287 if (ABS(e->xmotion.x_root - px) >= config_mouse_threshold ||
288 ABS(e->xmotion.y_root - py) >= config_mouse_threshold) {
289
290 /* You can't drag on buttons */
291 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
292 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
293 context == OB_FRAME_CONTEXT_SHADE ||
294 context == OB_FRAME_CONTEXT_ICONIFY ||
295 context == OB_FRAME_CONTEXT_ICON ||
296 context == OB_FRAME_CONTEXT_CLOSE)
297 break;
298
299 fire_binding(OB_MOUSE_ACTION_MOTION, context,
300 client, state, button, px, py, e->xmotion.time);
301 button = 0;
302 state = 0;
303 }
304 }
305 break;
306
307 default:
308 g_assert_not_reached();
309 }
310 }
311
312 gboolean mouse_bind(const gchar *buttonstr, const gchar *contextstr,
313 ObMouseAction mact, ObAction *action)
314 {
315 guint state, button;
316 ObFrameContext context;
317 ObMouseBinding *b;
318 GSList *it;
319
320 if (!translate_button(buttonstr, &state, &button)) {
321 g_message(_("Invalid button '%s' in mouse binding"), buttonstr);
322 return FALSE;
323 }
324
325 context = frame_context_from_string(contextstr);
326 if (!context) {
327 g_message(_("Invalid context '%s' in mouse binding"), contextstr);
328 return FALSE;
329 }
330
331 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
332 b = it->data;
333 if (b->state == state && b->button == button) {
334 b->actions[mact] = g_slist_append(b->actions[mact], action);
335 return TRUE;
336 }
337 }
338
339 /* when there are no modifiers in the binding, then the action cannot
340 be interactive */
341 if (!state && action->data.any.interactive) {
342 action->data.any.interactive = FALSE;
343 action->data.inter.final = TRUE;
344 }
345
346 /* add the binding */
347 b = g_new0(ObMouseBinding, 1);
348 b->state = state;
349 b->button = button;
350 b->actions[mact] = g_slist_append(NULL, action);
351 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
352
353 return TRUE;
354 }
355
356 void mouse_startup(gboolean reconfig)
357 {
358 grab_all_clients(TRUE);
359 }
360
361 void mouse_shutdown(gboolean reconfig)
362 {
363 grab_all_clients(FALSE);
364 mouse_unbind_all();
365 }
This page took 0.050437 seconds and 4 git commands to generate.