]> Dogcows Code - chaz/openbox/blob - openbox/mouse.c
3872b83d7e79189e4e8fc0da0ed85381b19bf071
[chaz/openbox] / openbox / mouse.c
1 #include "openbox.h"
2 #include "config.h"
3 #include "xerror.h"
4 #include "action.h"
5 #include "event.h"
6 #include "client.h"
7 #include "prop.h"
8 #include "grab.h"
9 #include "frame.h"
10 #include "translate.h"
11 #include "mouse.h"
12 #include "keyboard.h"
13 #include <glib.h>
14
15 typedef struct {
16 guint state;
17 guint button;
18 GSList *actions[OB_MOUSE_NUM_ACTIONS]; /* lists of Action pointers */
19 } ObMouseBinding;
20
21 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
22 co == OB_FRAME_CONTEXT_ROOT : \
23 co == OB_FRAME_CONTEXT_CLIENT)
24
25 /* Array of GSList*s of PointerBinding*s. */
26 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
27
28 void mouse_grab_for_client(ObClient *client, gboolean grab)
29 {
30 int i;
31 GSList *it;
32
33 for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
34 for (it = bound_contexts[i]; it != NULL; it = it->next) {
35 /* grab/ungrab the button */
36 ObMouseBinding *b = it->data;
37 Window win;
38 int mode;
39 unsigned int mask;
40
41 if (i == OB_FRAME_CONTEXT_FRAME) {
42 win = client->frame->window;
43 mode = GrabModeAsync;
44 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
45 } else if (CLIENT_CONTEXT(i, client)) {
46 win = client->frame->plate;
47 mode = GrabModeSync; /* this is handled in event */
48 mask = ButtonPressMask; /* can't catch more than this with Sync
49 mode the release event is
50 manufactured in event() */
51 } else continue;
52
53 if (grab)
54 grab_button_full(b->button, b->state, win, mask, mode, None);
55 else
56 ungrab_button(b->button, b->state, win);
57 }
58 }
59
60 static void grab_all_clients(gboolean grab)
61 {
62 GList *it;
63
64 for (it = client_list; it != NULL; it = it->next)
65 mouse_grab_for_client(it->data, grab);
66 }
67
68 static void clearall()
69 {
70 int i;
71 GSList *it;
72
73 for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
74 for (it = bound_contexts[i]; it != NULL; it = it->next) {
75 int j;
76
77 ObMouseBinding *b = it->data;
78 for (j = 0; j < OB_MOUSE_NUM_ACTIONS; ++j) {
79 GSList *it;
80 for (it = b->actions[j]; it; it = it->next) {
81 action_free(it->data);
82 }
83 g_slist_free(b->actions[j]);
84 }
85 g_free(b);
86 }
87 g_slist_free(bound_contexts[i]);
88 }
89 }
90
91 static gboolean fire_button(ObMouseAction a, ObFrameContext context,
92 ObClient *c, guint state,
93 guint button, int x, int y)
94 {
95 GSList *it;
96 ObMouseBinding *b;
97
98 for (it = bound_contexts[context]; it != NULL; it = it->next) {
99 b = it->data;
100 if (b->state == state && b->button == button)
101 break;
102 }
103 /* if not bound, then nothing to do! */
104 if (it == NULL) return FALSE;
105
106 for (it = b->actions[a]; it; it = it->next) {
107 ObAction *act = it->data;
108 if (act->func != NULL) {
109 act->data.any.c = c;
110
111 g_assert(act->func != action_moveresize);
112
113 if (act->func == action_showmenu) {
114 act->data.showmenu.x = x;
115 act->data.showmenu.y = y;
116 }
117
118 if (act->func == action_desktop_dir)
119 {
120 act->data.desktopdir.final = FALSE;
121 act->data.desktopdir.cancel = FALSE;
122 }
123 if (act->func == action_send_to_desktop_dir)
124 {
125 act->data.sendtodir.final = FALSE;
126 act->data.sendtodir.cancel = FALSE;
127 }
128
129 if (config_desktop_popup &&
130 (act->func == action_desktop_dir ||
131 act->func == action_send_to_desktop_dir))
132 {
133 keyboard_interactive_grab(state, c, context, act);
134 }
135
136 act->func(&act->data);
137 }
138 }
139 return TRUE;
140 }
141
142 static gboolean fire_motion(ObMouseAction a, ObFrameContext context,
143 ObClient *c, guint state, guint button,
144 int x_root, int y_root, guint32 corner)
145 {
146 GSList *it;
147 ObMouseBinding *b;
148
149 for (it = bound_contexts[context]; it != NULL; it = it->next) {
150 b = it->data;
151 if (b->state == state && b->button == button)
152 break;
153 }
154 /* if not bound, then nothing to do! */
155 if (it == NULL) return FALSE;
156
157 for (it = b->actions[a]; it; it = it->next) {
158 ObAction *act = it->data;
159 if (act->func != NULL) {
160 act->data.any.c = c;
161
162 if (act->func == action_moveresize) {
163 act->data.moveresize.x = x_root;
164 act->data.moveresize.y = y_root;
165 act->data.moveresize.button = button;
166 if (!(act->data.moveresize.corner ==
167 prop_atoms.net_wm_moveresize_move ||
168 act->data.moveresize.corner ==
169 prop_atoms.net_wm_moveresize_move_keyboard ||
170 act->data.moveresize.corner ==
171 prop_atoms.net_wm_moveresize_size_keyboard))
172 act->data.moveresize.corner = corner;
173 } else
174 g_assert_not_reached();
175
176 act->func(&act->data);
177 }
178 }
179 return TRUE;
180 }
181
182 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
183 {
184 if (x - cx < cw / 2) {
185 if (y - cy < ch / 2)
186 return prop_atoms.net_wm_moveresize_size_topleft;
187 else
188 return prop_atoms.net_wm_moveresize_size_bottomleft;
189 } else {
190 if (y - cy < ch / 2)
191 return prop_atoms.net_wm_moveresize_size_topright;
192 else
193 return prop_atoms.net_wm_moveresize_size_bottomright;
194 }
195 }
196
197 void mouse_event(ObClient *client, ObFrameContext context, XEvent *e)
198 {
199 static Time ltime;
200 static guint button = 0, state = 0, lbutton = 0;
201
202 static Window lwindow = None;
203 static int px, py;
204 gboolean click = FALSE;
205 gboolean dclick = FALSE;
206
207 switch (e->type) {
208 case ButtonPress:
209 px = e->xbutton.x_root;
210 py = e->xbutton.y_root;
211 button = e->xbutton.button;
212 state = e->xbutton.state;
213
214 fire_button(OB_MOUSE_ACTION_PRESS, context,
215 client, e->xbutton.state,
216 e->xbutton.button,
217 e->xbutton.x_root, e->xbutton.y_root);
218
219 if (CLIENT_CONTEXT(context, client)) {
220 /* Replay the event, so it goes to the client*/
221 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
222 /* Fall through to the release case! */
223 } else
224 break;
225
226 case ButtonRelease:
227 if (e->xbutton.button == button) {
228 /* clicks are only valid if its released over the window */
229 int junk1, junk2;
230 Window wjunk;
231 guint ujunk, b, w, h;
232 Status s;
233 xerror_set_ignore(TRUE);
234 s = XGetGeometry(ob_display, e->xbutton.window,
235 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
236 xerror_set_ignore(FALSE);
237 if (s == Success) {
238 if (e->xbutton.x >= (signed)-b &&
239 e->xbutton.y >= (signed)-b &&
240 e->xbutton.x < (signed)(w+b) &&
241 e->xbutton.y < (signed)(h+b)) {
242 click = TRUE;
243 /* double clicks happen if there were 2 in a row! */
244 if (lbutton == button &&
245 lwindow == e->xbutton.window &&
246 e->xbutton.time - config_mouse_dclicktime <=
247 ltime) {
248 dclick = TRUE;
249 lbutton = 0;
250 } else {
251 lbutton = button;
252 lwindow = e->xbutton.window;
253 }
254 } else {
255 lbutton = 0;
256 lwindow = None;
257 }
258 }
259
260 button = 0;
261 state = 0;
262 ltime = e->xbutton.time;
263 }
264 fire_button(OB_MOUSE_ACTION_RELEASE, context,
265 client, e->xbutton.state,
266 e->xbutton.button,
267 e->xbutton.x_root, e->xbutton.y_root);
268 if (click)
269 fire_button(OB_MOUSE_ACTION_CLICK, context,
270 client, e->xbutton.state,
271 e->xbutton.button,
272 e->xbutton.x_root,
273 e->xbutton.y_root);
274 if (dclick)
275 fire_button(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
276 client, e->xbutton.state,
277 e->xbutton.button,
278 e->xbutton.x_root,
279 e->xbutton.y_root);
280 break;
281
282 case MotionNotify:
283 if (button) {
284 if (ABS(e->xmotion.x_root - px) >=
285 config_mouse_threshold ||
286 ABS(e->xmotion.y_root - py) >=
287 config_mouse_threshold) {
288 guint32 corner;
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 if (!client)
300 corner = prop_atoms.net_wm_moveresize_size_bottomright;
301 else
302 corner =
303 pick_corner(e->xmotion.x_root,
304 e->xmotion.y_root,
305 client->frame->area.x,
306 client->frame->area.y,
307 /* use the client size because the frame
308 can be differently sized (shaded
309 windows) and we want this based on the
310 clients size */
311 client->area.width +
312 client->frame->size.left +
313 client->frame->size.right,
314 client->area.height +
315 client->frame->size.top +
316 client->frame->size.bottom);
317 fire_motion(OB_MOUSE_ACTION_MOTION, context,
318 client, state, button, px, py, corner);
319 button = 0;
320 state = 0;
321 }
322 }
323 break;
324
325 default:
326 g_assert_not_reached();
327 }
328 }
329
330 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
331 ObAction *action)
332 {
333 guint state, button;
334 ObFrameContext context;
335 ObMouseBinding *b;
336 GSList *it;
337
338 if (!translate_button(buttonstr, &state, &button)) {
339 g_warning("invalid button '%s'", buttonstr);
340 return FALSE;
341 }
342
343 contextstr = g_ascii_strdown(contextstr, -1);
344 context = frame_context_from_string(contextstr);
345 if (!context) {
346 g_warning("invalid context '%s'", contextstr);
347 g_free(contextstr);
348 return FALSE;
349 }
350 g_free(contextstr);
351
352 for (it = bound_contexts[context]; it != NULL; it = it->next){
353 b = it->data;
354 if (b->state == state && b->button == button) {
355 b->actions[mact] = g_slist_append(b->actions[mact], action);
356 return TRUE;
357 }
358 }
359
360 grab_all_clients(FALSE);
361
362 /* add the binding */
363 b = g_new0(ObMouseBinding, 1);
364 b->state = state;
365 b->button = button;
366 b->actions[mact] = g_slist_append(NULL, action);
367 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
368
369 grab_all_clients(TRUE);
370
371 return TRUE;
372 }
373
374 void mouse_startup()
375 {
376 }
377
378 void mouse_shutdown()
379 {
380 grab_all_clients(FALSE);
381 clearall();
382 }
This page took 0.049822 seconds and 3 git commands to generate.