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