]> Dogcows Code - chaz/openbox/blob - plugins/mouse/mouse.c
strdown the context name
[chaz/openbox] / plugins / mouse / mouse.c
1 #include "../../kernel/openbox.h"
2 #include "../../kernel/dispatch.h"
3 #include "../../kernel/action.h"
4 #include "../../kernel/client.h"
5 #include "../../kernel/frame.h"
6 #include "../../kernel/grab.h"
7 #include "../../kernel/engine.h"
8 #include "translate.h"
9 #include "mouse.h"
10 #include <glib.h>
11
12 void plugin_setup_config()
13 {
14 }
15
16 static int drag_threshold = 3;
17
18 /* GData of GSList*s of PointerBinding*s. */
19 static GData *bound_contexts;
20
21 struct foreach_grab_temp {
22 Client *client;
23 gboolean grab;
24 };
25
26 static void foreach_grab(GQuark key, gpointer data, gpointer user_data)
27 {
28 struct foreach_grab_temp *d = user_data;
29 GSList *it;
30 for (it = data; it != NULL; it = it->next) {
31 /* grab/ungrab the button */
32 MouseBinding *b = it->data;
33 Window win;
34 int mode;
35 unsigned int mask;
36
37 if (key == g_quark_try_string("frame")) {
38 win = d->client->frame->window;
39 mode = GrabModeAsync;
40 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
41 } else if (key == g_quark_try_string("client")) {
42 win = d->client->frame->plate;
43 mode = GrabModeSync; /* this is handled in event */
44 mask = ButtonPressMask; /* can't catch more than this with Sync
45 mode the release event is manufactured
46 in event */
47 } else return;
48
49 if (d->grab)
50 grab_button(b->button, b->state, win, mask, mode);
51 else
52 ungrab_button(b->button, b->state, win);
53 }
54 }
55
56 static void grab_for_client(Client *client, gboolean grab)
57 {
58 struct foreach_grab_temp bt;
59 bt.client = client;
60 bt.grab = grab;
61 g_datalist_foreach(&bound_contexts, foreach_grab, &bt);
62 }
63
64 static void grab_all_clients(gboolean grab)
65 {
66 GSList *it;
67
68 for (it = client_list; it != NULL; it = it->next)
69 grab_for_client(it->data, grab);
70 }
71
72 static void foreach_clear(GQuark key, gpointer data, gpointer user_data)
73 {
74 GSList *it;
75 user_data = user_data;
76 for (it = data; it != NULL; it = it->next) {
77 int i;
78
79 MouseBinding *b = it->data;
80 for (i = 0; i < NUM_MOUSEACTION; ++i)
81 if (b->action[i] != NULL)
82 action_free(b->action[i]);
83 g_free(b);
84 }
85 g_slist_free(data);
86 }
87
88 static void fire_button(MouseAction a, GQuark context, Client *c, guint state,
89 guint button)
90 {
91 GSList *it;
92 MouseBinding *b;
93
94 for (it = g_datalist_id_get_data(&bound_contexts, context);
95 it != NULL; it = it->next) {
96 b = it->data;
97 if (b->state == state && b->button == button)
98 break;
99 }
100 /* if not bound, then nothing to do! */
101 if (it == NULL) return;
102
103 if (b->action[a] != NULL && b->action[a]->func != NULL) {
104 b->action[a]->data.any.c = c;
105
106 g_assert(!(b->action[a]->func == action_move ||
107 b->action[a]->func == action_resize));
108
109 b->action[a]->func(&b->action[a]->data);
110 }
111 }
112
113 /* corner should be the opposite corner of the window in which the pointer
114 clicked, Corner_TopLeft if a good default if there is no client */
115 static void fire_motion(MouseAction a, GQuark context, Client *c, guint state,
116 guint button, int cx, int cy, int cw, int ch,
117 int dx, int dy, gboolean final, Corner corner)
118 {
119 GSList *it;
120 MouseBinding *b;
121
122 for (it = g_datalist_id_get_data(&bound_contexts, context);
123 it != NULL; it = it->next) {
124 b = it->data;
125 if (b->state == state && b->button == button)
126 break;
127 }
128 /* if not bound, then nothing to do! */
129 if (it == NULL) return;
130
131 if (b->action[a] != NULL && b->action[a]->func != NULL) {
132 b->action[a]->data.any.c = c;
133
134 if (b->action[a]->func == action_move) {
135 b->action[a]->data.move.x = cx + dx;
136 b->action[a]->data.move.y = cy + dy;
137 b->action[a]->data.move.final = final;
138 } else if (b->action[a]->func == action_resize) {
139 b->action[a]->data.resize.corner = corner;
140 switch (corner) {
141 case Corner_TopLeft:
142 b->action[a]->data.resize.x = cw + dx;
143 b->action[a]->data.resize.y = ch + dy;
144 break;
145 case Corner_TopRight:
146 b->action[a]->data.resize.x = cw - dx;
147 b->action[a]->data.resize.y = ch + dy;
148 break;
149 case Corner_BottomLeft:
150 b->action[a]->data.resize.x = cw + dx;
151 b->action[a]->data.resize.y = ch - dy;
152 break;
153 case Corner_BottomRight:
154 b->action[a]->data.resize.x = cw - dx;
155 b->action[a]->data.resize.y = ch - dy;
156 break;
157 }
158 b->action[a]->data.resize.final = final;
159 }
160 b->action[a]->func(&b->action[a]->data);
161 }
162 }
163
164 static Corner pick_corner(int x, int y, int cx, int cy, int cw, int ch)
165 {
166 if (x - cx < cw / 2) {
167 if (y - cy < ch / 2)
168 return Corner_BottomRight;
169 else
170 return Corner_TopRight;
171 } else {
172 if (y - cy < ch / 2)
173 return Corner_BottomLeft;
174 else
175 return Corner_TopLeft;
176 }
177 }
178
179 static void event(ObEvent *e, void *foo)
180 {
181 static Time ltime;
182 static int px, py, cx, cy, cw, ch, dx, dy;
183 static guint button = 0, lbutton = 0;
184 static gboolean drag = FALSE;
185 static Corner corner = Corner_TopLeft;
186 gboolean click = FALSE;
187 gboolean dclick = FALSE;
188 GQuark context;
189
190 switch (e->type) {
191 case Event_Client_Mapped:
192 grab_for_client(e->data.c.client, TRUE);
193 break;
194
195 case Event_Client_Destroy:
196 grab_for_client(e->data.c.client, FALSE);
197 break;
198
199 case Event_X_ButtonPress:
200 if (!button) {
201 if (e->data.x.client != NULL) {
202 cx = e->data.x.client->frame->area.x;
203 cy = e->data.x.client->frame->area.y;
204 cw = e->data.x.client->frame->area.width;
205 ch = e->data.x.client->frame->area.height;
206 px = e->data.x.e->xbutton.x_root;
207 py = e->data.x.e->xbutton.y_root;
208 corner = pick_corner(px, py, cx, cy, cw, ch);
209 }
210 button = e->data.x.e->xbutton.button;
211 }
212 context = engine_get_context(e->data.x.client,
213 e->data.x.e->xbutton.window);
214
215 fire_button(MouseAction_Press, context,
216 e->data.x.client, e->data.x.e->xbutton.state,
217 e->data.x.e->xbutton.button);
218
219 if (context == g_quark_try_string("client")) {
220 /* Replay the event, so it goes to the client*/
221 XAllowEvents(ob_display, ReplayPointer, CurrentTime);
222 /* Fall through to the release case! */
223 } else
224 break;
225
226 case Event_X_ButtonRelease:
227 context = engine_get_context(e->data.x.client,
228 e->data.x.e->xbutton.window);
229 if (e->data.x.e->xbutton.button == button) {
230 /* end drags */
231 if (drag) {
232 fire_motion(MouseAction_Motion, context,
233 e->data.x.client, e->data.x.e->xbutton.state,
234 e->data.x.e->xbutton.button,
235 cx, cy, cw, ch, dx, dy, TRUE, corner);
236 drag = FALSE;
237
238 lbutton = 0;
239 } else {
240 /* clicks are only valid if its released over the window */
241 if (e->data.x.e->xbutton.x >= 0 &&
242 e->data.x.e->xbutton.y >= 0) {
243 int junk;
244 Window wjunk;
245 guint ujunk, w, h;
246 XGetGeometry(ob_display, e->data.x.e->xbutton.window,
247 &wjunk, &junk, &junk, &w, &h, &ujunk, &ujunk);
248 if (e->data.x.e->xbutton.x < (signed)w &&
249 e->data.x.e->xbutton.y < (signed)h) {
250 click =TRUE;
251 /* double clicks happen if there were 2 in a row! */
252 if (lbutton == button &&
253 e->data.x.e->xbutton.time - 300 <= ltime)
254 dclick = TRUE;
255 }
256 lbutton = button;
257 } else
258 lbutton = 0;
259 }
260
261 button = 0;
262 ltime = e->data.x.e->xbutton.time;
263 }
264 fire_button(MouseAction_Press, context,
265 e->data.x.client, e->data.x.e->xbutton.state,
266 e->data.x.e->xbutton.button);
267 if (click)
268 fire_button(MouseAction_Click, context,
269 e->data.x.client, e->data.x.e->xbutton.state,
270 e->data.x.e->xbutton.button);
271 if (dclick)
272 fire_button(MouseAction_DClick, context,
273 e->data.x.client, e->data.x.e->xbutton.state,
274 e->data.x.e->xbutton.button);
275 break;
276
277 case Event_X_MotionNotify:
278 if (button) {
279 dx = e->data.x.e->xmotion.x_root - px;
280 dy = e->data.x.e->xmotion.y_root - py;
281 if (ABS(dx) >= drag_threshold || ABS(dy) >= drag_threshold)
282 drag = TRUE;
283 if (drag) {
284 context = engine_get_context(e->data.x.client,
285 e->data.x.e->xbutton.window);
286 fire_motion(MouseAction_Motion, context,
287 e->data.x.client, e->data.x.e->xmotion.state,
288 button, cx, cy, cw, ch, dx, dy, FALSE, corner);
289 }
290 }
291 break;
292
293 default:
294 g_assert_not_reached();
295 }
296 }
297
298 static gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
299 Action *action)
300 {
301 guint state, button;
302 GQuark context;
303 MouseBinding *b;
304 GSList *it;
305 guint i;
306
307 if (!translate_button(buttonstr, &state, &button)) {
308 g_warning("invalid button");
309 return FALSE;
310 }
311
312 contextstr = g_ascii_strdown(contextstr, -1);
313 context = g_quark_try_string(contextstr);
314 g_free(contextstr);
315 if (!context) {
316 g_warning("invalid context");
317 return FALSE;
318 }
319
320 for (it = g_datalist_id_get_data(&bound_contexts, context);
321 it != NULL; it = it->next){
322 b = it->data;
323 if (b->state == state && b->button == button) {
324 /* already bound */
325 if (b->action[mact] != NULL) {
326 g_warning("duplicate binding");
327 return FALSE;
328 }
329 b->action[mact] = action;
330 return TRUE;
331 }
332 }
333
334 grab_all_clients(FALSE);
335
336 /* add the binding */
337 b = g_new(MouseBinding, 1);
338 b->state = state;
339 b->button = button;
340 for (i = 0; i < NUM_MOUSEACTION; ++i)
341 b->action[i] = NULL;
342 b->action[mact] = action;
343 g_datalist_id_set_data(&bound_contexts, context,
344 g_slist_append(g_datalist_id_get_data(&bound_contexts, context), b));
345
346 grab_all_clients(TRUE);
347
348 return TRUE;
349 }
350
351 static void binddef()
352 {
353 Action *a;
354
355 /* When creating an Action struct, all of the data elements in the
356 appropriate struct need to be set, except the Client*, which will be set
357 at call-time when then action function is used.
358
359 For action_move and action_resize, the 'x', 'y', and 'final' data
360 elements should not be set, as they are set at call-time.
361 */
362
363 a = action_new(action_move);
364 mbind("1", "titlebar", MouseAction_Motion, a);
365 a = action_new(action_move);
366 mbind("1", "handle", MouseAction_Motion, a);
367 a = action_new(action_move);
368 mbind("A-1", "frame", MouseAction_Motion, a);
369
370 a = action_new(action_resize);
371 mbind("1", "blcorner", MouseAction_Motion, a);
372 a = action_new(action_resize);
373 mbind("1", "brcorner", MouseAction_Motion, a);
374 a = action_new(action_resize);
375 mbind("A-3", "frame", MouseAction_Motion, a);
376
377 a = action_new(action_focus);
378 mbind("1", "titlebar", MouseAction_Press, a);
379 a = action_new(action_focus);
380 mbind("1", "handle", MouseAction_Press, a);
381 a = action_new(action_focusraise);
382 mbind("1", "titlebar", MouseAction_Click, a);
383 a = action_new(action_focusraise);
384 mbind("1", "handle", MouseAction_Click, a);
385 a = action_new(action_lower);
386 mbind("2", "titlebar", MouseAction_Press, a);
387 a = action_new(action_lower);
388 mbind("2", "handle", MouseAction_Press, a);
389 a = action_new(action_focusraise);
390 mbind("A-1", "frame", MouseAction_Click, a);
391 a = action_new(action_lower);
392 mbind("A-3", "frame", MouseAction_Click, a);
393
394 a = action_new(action_focus);
395 mbind("1", "client", MouseAction_Press, a);
396
397 a = action_new(action_toggle_shade);
398 mbind("1", "titlebar", MouseAction_DClick, a);
399 a = action_new(action_shade);
400 mbind("4", "titlebar", MouseAction_Press, a);
401 a = action_new(action_unshade);
402 mbind("5", "titlebar", MouseAction_Press, a);
403
404 a = action_new(action_toggle_maximize_full);
405 mbind("1", "maximize", MouseAction_Click, a);
406 a = action_new(action_toggle_maximize_vert);
407 mbind("2", "maximize", MouseAction_Click, a);
408 a = action_new(action_toggle_maximize_horz);
409 mbind("3", "maximize", MouseAction_Click, a);
410 a = action_new(action_iconify);
411 mbind("1", "iconify", MouseAction_Click, a);
412 a = action_new(action_close);
413 mbind("1", "icon", MouseAction_DClick, a);
414 a = action_new(action_close);
415 mbind("1", "close", MouseAction_Click, a);
416 a = action_new(action_kill);
417 mbind("2", "close", MouseAction_Click, a);
418 a = action_new(action_toggle_omnipresent);
419 mbind("1", "alldesktops", MouseAction_Click, a);
420
421 a = action_new(action_next_desktop);
422 a->data.nextprevdesktop.wrap = TRUE;
423 mbind("4", "root", MouseAction_Click, a);
424 a = action_new(action_previous_desktop);
425 a->data.nextprevdesktop.wrap = TRUE;
426 mbind("5", "root", MouseAction_Click, a);
427 a = action_new(action_next_desktop);
428 a->data.nextprevdesktop.wrap = TRUE;
429 mbind("A-4", "root", MouseAction_Click, a);
430 a = action_new(action_previous_desktop);
431 a->data.nextprevdesktop.wrap = TRUE;
432 mbind("A-5", "root", MouseAction_Click, a);
433 a = action_new(action_next_desktop);
434 a->data.nextprevdesktop.wrap = TRUE;
435 mbind("A-4", "frame", MouseAction_Click, a);
436 a = action_new(action_previous_desktop);
437 a->data.nextprevdesktop.wrap = TRUE;
438 mbind("A-5", "frame", MouseAction_Click, a);
439 }
440
441 void plugin_startup()
442 {
443 dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
444 Event_X_ButtonPress | Event_X_ButtonRelease |
445 Event_X_MotionNotify, (EventHandler)event, NULL);
446
447 /* XXX parse a config */
448 binddef();
449 }
450
451 void plugin_shutdown()
452 {
453 dispatch_register(0, (EventHandler)event, NULL);
454
455 grab_all_clients(FALSE);
456 g_datalist_foreach(&bound_contexts, foreach_clear, NULL);
457 }
This page took 0.05651 seconds and 5 git commands to generate.