]> Dogcows Code - chaz/openbox/blob - openbox/mouse.c
update copyright step 2
[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 Ben 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 <glib.h>
32
33 typedef struct {
34 guint state;
35 guint button;
36 GSList *actions[OB_NUM_MOUSE_ACTIONS]; /* lists of Action pointers */
37 } ObMouseBinding;
38
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)
44
45 /* Array of GSList*s of ObMouseBinding*s. */
46 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
47
48 ObFrameContext mouse_button_frame_context(ObFrameContext context,
49 guint button)
50 {
51 GSList *it;
52 ObFrameContext x = context;
53
54 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
55 ObMouseBinding *b = it->data;
56
57 if (b->button == button)
58 return context;
59 }
60
61 switch (context) {
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:
69 break;
70 case OB_FRAME_CONTEXT_BLCORNER:
71 case OB_FRAME_CONTEXT_BRCORNER:
72 x = OB_FRAME_CONTEXT_HANDLE;
73 break;
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;
83 break;
84 case OB_FRAME_NUM_CONTEXTS:
85 g_assert_not_reached();
86 }
87
88 return x;
89 }
90
91 void mouse_grab_for_client(ObClient *client, gboolean grab)
92 {
93 gint i;
94 GSList *it;
95
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;
100 Window win;
101 gint mode;
102 guint mask;
103
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() */
114 } else continue;
115
116 if (grab)
117 grab_button_full(b->button, b->state, win, mask, mode,
118 OB_CURSOR_NONE);
119 else
120 ungrab_button(b->button, b->state, win);
121 }
122 }
123
124 static void grab_all_clients(gboolean grab)
125 {
126 GList *it;
127
128 for (it = client_list; it; it = g_list_next(it))
129 mouse_grab_for_client(it->data, grab);
130 }
131
132 void mouse_unbind_all()
133 {
134 gint i;
135 GSList *it;
136
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;
140 gint j;
141
142 for (j = 0; j < OB_NUM_MOUSE_ACTIONS; ++j) {
143 GSList *it;
144
145 for (it = b->actions[j]; it; it = g_slist_next(it))
146 action_unref(it->data);
147 g_slist_free(b->actions[j]);
148 }
149 g_free(b);
150 }
151 g_slist_free(bound_contexts[i]);
152 bound_contexts[i] = NULL;
153 }
154 }
155
156 static gboolean fire_binding(ObMouseAction a, ObFrameContext context,
157 ObClient *c, guint state,
158 guint button, gint x, gint y)
159 {
160 GSList *it;
161 ObMouseBinding *b;
162
163 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
164 b = it->data;
165 if (b->state == state && b->button == button)
166 break;
167 }
168 /* if not bound, then nothing to do! */
169 if (it == NULL) return FALSE;
170
171 action_run_mouse(b->actions[a], c, context, state, button, x, y);
172 return TRUE;
173 }
174
175 void mouse_event(ObClient *client, XEvent *e)
176 {
177 static Time ltime;
178 static guint button = 0, state = 0, lbutton = 0;
179 static Window lwindow = None;
180 static gint px, py;
181
182 ObFrameContext context;
183 gboolean click = FALSE;
184 gboolean dclick = FALSE;
185
186 switch (e->type) {
187 case ButtonPress:
188 context = frame_context(client, e->xany.window);
189 context = mouse_button_frame_context(context, e->xbutton.button);
190
191 px = e->xbutton.x_root;
192 py = e->xbutton.y_root;
193 button = e->xbutton.button;
194 state = e->xbutton.state;
195
196 fire_binding(OB_MOUSE_ACTION_PRESS, context,
197 client, e->xbutton.state,
198 e->xbutton.button,
199 e->xbutton.x_root, e->xbutton.y_root);
200
201 if (CLIENT_CONTEXT(context, client)) {
202 /* Replay the event, so it goes to the client*/
203 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
204 /* Fall through to the release case! */
205 } else
206 break;
207
208 case ButtonRelease:
209 context = frame_context(client, e->xany.window);
210 context = mouse_button_frame_context(context, e->xbutton.button);
211
212 if (e->xbutton.button == button) {
213 /* clicks are only valid if its released over the window */
214 gint junk1, junk2;
215 Window wjunk;
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);
222 if (junk1) {
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)) {
227 click = TRUE;
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 <=
232 ltime) {
233 dclick = TRUE;
234 lbutton = 0;
235 } else {
236 lbutton = button;
237 lwindow = e->xbutton.window;
238 }
239 } else {
240 lbutton = 0;
241 lwindow = None;
242 }
243 }
244
245 button = 0;
246 state = 0;
247 ltime = e->xbutton.time;
248 }
249 fire_binding(OB_MOUSE_ACTION_RELEASE, context,
250 client, e->xbutton.state,
251 e->xbutton.button,
252 e->xbutton.x_root, e->xbutton.y_root);
253 if (click)
254 fire_binding(OB_MOUSE_ACTION_CLICK, context,
255 client, e->xbutton.state,
256 e->xbutton.button,
257 e->xbutton.x_root,
258 e->xbutton.y_root);
259 if (dclick)
260 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
261 client, e->xbutton.state,
262 e->xbutton.button,
263 e->xbutton.x_root,
264 e->xbutton.y_root);
265 break;
266
267 case MotionNotify:
268 if (button) {
269 context = frame_context(client, e->xany.window);
270 context = mouse_button_frame_context(context, button);
271
272 if (ABS(e->xmotion.x_root - px) >=
273 config_mouse_threshold ||
274 ABS(e->xmotion.y_root - py) >=
275 config_mouse_threshold) {
276
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)
284 break;
285
286 fire_binding(OB_MOUSE_ACTION_MOTION, context,
287 client, state, button, px, py);
288 button = 0;
289 state = 0;
290 }
291 }
292 break;
293
294 default:
295 g_assert_not_reached();
296 }
297 }
298
299 gboolean mouse_bind(const gchar *buttonstr, const gchar *contextstr,
300 ObMouseAction mact, ObAction *action)
301 {
302 guint state, button;
303 ObFrameContext context;
304 ObMouseBinding *b;
305 GSList *it;
306
307 if (!translate_button(buttonstr, &state, &button)) {
308 g_warning("invalid button '%s'", buttonstr);
309 return FALSE;
310 }
311
312 context = frame_context_from_string(contextstr);
313 if (!context) {
314 g_warning("invalid context '%s'", contextstr);
315 return FALSE;
316 }
317
318 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
319 b = it->data;
320 if (b->state == state && b->button == button) {
321 b->actions[mact] = g_slist_append(b->actions[mact], action);
322 return TRUE;
323 }
324 }
325
326 /* when there are no modifiers in the binding, then the action cannot
327 be interactive */
328 if (!state && action->data.any.interactive) {
329 action->data.any.interactive = FALSE;
330 action->data.inter.final = TRUE;
331 }
332
333 /* add the binding */
334 b = g_new0(ObMouseBinding, 1);
335 b->state = state;
336 b->button = button;
337 b->actions[mact] = g_slist_append(NULL, action);
338 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
339
340 return TRUE;
341 }
342
343 void mouse_startup(gboolean reconfig)
344 {
345 grab_all_clients(TRUE);
346 }
347
348 void mouse_shutdown(gboolean reconfig)
349 {
350 grab_all_clients(FALSE);
351 mouse_unbind_all();
352 }
This page took 0.056695 seconds and 5 git commands to generate.