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"
12 /* GData of GSList*s of PointerBinding*s. */
13 static GData *bound_contexts;
15 struct foreach_grab_temp {
20 static void foreach_grab(GQuark key, gpointer data, gpointer user_data)
22 struct foreach_grab_temp *d = user_data;
24 for (it = data; it != NULL; it = it->next) {
25 /* grab/ungrab the button */
26 MouseBinding *b = it->data;
31 if (key == g_quark_try_string("frame")) {
32 win = d->client->frame->window;
34 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
35 } else if (key == g_quark_try_string("client")) {
36 win = d->client->frame->plate;
37 mode = GrabModeSync; /* this is handled in event */
38 mask = ButtonPressMask; /* can't catch more than this with Sync
39 mode the release event is manufactured
44 grab_button(b->button, b->state, win, mask, mode);
46 ungrab_button(b->button, b->state, win);
50 static void grab_for_client(Client *client, gboolean grab)
52 struct foreach_grab_temp bt;
55 g_datalist_foreach(&bound_contexts, foreach_grab, &bt);
58 static void grab_all_clients(gboolean grab)
62 for (it = client_list; it != NULL; it = it->next)
63 grab_for_client(it->data, grab);
66 static void foreach_clear(GQuark key, gpointer data, gpointer user_data)
69 user_data = user_data;
70 for (it = data; it != NULL; it = it->next) {
73 MouseBinding *b = it->data;
74 for (i = 0; i < NUM_MOUSEACTION; ++i)
75 if (b->action[i] != NULL)
76 action_free(b->action[i]);
82 static void fire_button(MouseAction a, GQuark context, Client *c, guint state,
88 for (it = g_datalist_id_get_data(&bound_contexts, context);
89 it != NULL; it = it->next) {
91 if (b->state == state && b->button == button)
94 /* if not bound, then nothing to do! */
95 if (it == NULL) return;
97 if (b->action[a] != NULL && b->action[a]->func != NULL) {
98 b->action[a]->data.any.c = c;
100 g_assert(!(b->action[a]->func == action_move ||
101 b->action[a]->func == action_resize));
103 b->action[a]->func(&b->action[a]->data);
107 /* corner should be the opposite corner of the window in which the pointer
108 clicked, Corner_TopLeft if a good default if there is no client */
109 static void fire_motion(MouseAction a, GQuark context, Client *c, guint state,
110 guint button, int cx, int cy, int cw, int ch,
111 int dx, int dy, gboolean final, Corner corner)
116 for (it = g_datalist_id_get_data(&bound_contexts, context);
117 it != NULL; it = it->next) {
119 if (b->state == state && b->button == button)
122 /* if not bound, then nothing to do! */
123 if (it == NULL) return;
125 if (b->action[a] != NULL && b->action[a]->func != NULL) {
126 b->action[a]->data.any.c = c;
128 if (b->action[a]->func == action_move) {
129 b->action[a]->data.move.x = cx + dx;
130 b->action[a]->data.move.y = cy + dy;
131 b->action[a]->data.move.final = final;
132 } else if (b->action[a]->func == action_resize) {
133 b->action[a]->data.resize.corner = corner;
136 b->action[a]->data.resize.x = cw + dx;
137 b->action[a]->data.resize.y = ch + dy;
139 case Corner_TopRight:
140 b->action[a]->data.resize.x = cw - dx;
141 b->action[a]->data.resize.y = ch + dy;
143 case Corner_BottomLeft:
144 b->action[a]->data.resize.x = cw + dx;
145 b->action[a]->data.resize.y = ch - dy;
147 case Corner_BottomRight:
148 b->action[a]->data.resize.x = cw - dx;
149 b->action[a]->data.resize.y = ch - dy;
152 b->action[a]->data.resize.final = final;
154 b->action[a]->func(&b->action[a]->data);
158 static Corner pick_corner(int x, int y, int cx, int cy, int cw, int ch)
160 if (x - cx < cw / 2) {
162 return Corner_BottomRight;
164 return Corner_TopRight;
167 return Corner_BottomLeft;
169 return Corner_TopLeft;
173 static void event(ObEvent *e, void *foo)
176 static int px, py, cx, cy, cw, ch, dx, dy;
177 static guint button = 0, lbutton = 0;
178 static gboolean drag = FALSE;
179 static Corner corner = Corner_TopLeft;
180 gboolean click = FALSE;
181 gboolean dclick = FALSE;
185 case Event_Client_Mapped:
186 grab_for_client(e->data.c.client, TRUE);
189 case Event_Client_Destroy:
190 grab_for_client(e->data.c.client, FALSE);
193 case Event_X_ButtonPress:
195 if (e->data.x.client != NULL) {
196 cx = e->data.x.client->frame->area.x;
197 cy = e->data.x.client->frame->area.y;
198 cw = e->data.x.client->frame->area.width;
199 ch = e->data.x.client->frame->area.height;
200 px = e->data.x.e->xbutton.x_root;
201 py = e->data.x.e->xbutton.y_root;
202 corner = pick_corner(px, py, cx, cy, cw, ch);
204 button = e->data.x.e->xbutton.button;
206 context = engine_get_context(e->data.x.client,
207 e->data.x.e->xbutton.window);
209 fire_button(MouseAction_Press, context,
210 e->data.x.client, e->data.x.e->xbutton.state,
211 e->data.x.e->xbutton.button);
213 if (context == g_quark_try_string("client")) {
214 /* Replay the event, so it goes to the client*/
215 XAllowEvents(ob_display, ReplayPointer, CurrentTime);
216 /* Fall through to the release case! */
220 case Event_X_ButtonRelease:
221 context = engine_get_context(e->data.x.client,
222 e->data.x.e->xbutton.window);
223 if (e->data.x.e->xbutton.button == button) {
226 fire_motion(MouseAction_Motion, context,
227 e->data.x.client, e->data.x.e->xbutton.state,
228 e->data.x.e->xbutton.button,
229 cx, cy, cw, ch, dx, dy, TRUE, corner);
234 /* clicks are only valid if its released over the window */
235 if (e->data.x.e->xbutton.x >= 0 &&
236 e->data.x.e->xbutton.y >= 0) {
240 XGetGeometry(ob_display, e->data.x.e->xbutton.window,
241 &wjunk, &junk, &junk, &w, &h, &ujunk, &ujunk);
242 if (e->data.x.e->xbutton.x < (signed)w &&
243 e->data.x.e->xbutton.y < (signed)h) {
245 /* double clicks happen if there were 2 in a row! */
246 if (lbutton == button &&
247 e->data.x.e->xbutton.time - 300 <= ltime)
256 ltime = e->data.x.e->xbutton.time;
258 fire_button(MouseAction_Press, context,
259 e->data.x.client, e->data.x.e->xbutton.state,
260 e->data.x.e->xbutton.button);
262 fire_button(MouseAction_Click, context,
263 e->data.x.client, e->data.x.e->xbutton.state,
264 e->data.x.e->xbutton.button);
266 fire_button(MouseAction_DClick, context,
267 e->data.x.client, e->data.x.e->xbutton.state,
268 e->data.x.e->xbutton.button);
271 case Event_X_MotionNotify:
274 dx = e->data.x.e->xmotion.x_root - px;
275 dy = e->data.x.e->xmotion.y_root - py;
276 context = engine_get_context(e->data.x.client,
277 e->data.x.e->xbutton.window);
278 fire_motion(MouseAction_Motion, context,
279 e->data.x.client, e->data.x.e->xmotion.state,
280 button, cx, cy, cw, ch, dx, dy, FALSE, corner);
285 g_assert_not_reached();
289 static gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
298 if (!translate_button(buttonstr, &state, &button)) {
299 g_warning("invalid button");
303 context = g_quark_try_string(contextstr);
305 g_warning("invalid context");
309 for (it = g_datalist_id_get_data(&bound_contexts, context);
310 it != NULL; it = it->next){
312 if (b->state == state && b->button == button) {
314 if (b->action[mact] != NULL) {
315 g_warning("duplicate binding");
318 b->action[mact] = action;
323 grab_all_clients(FALSE);
325 /* add the binding */
326 b = g_new(MouseBinding, 1);
329 for (i = 0; i < NUM_MOUSEACTION; ++i)
331 b->action[mact] = action;
332 g_datalist_id_set_data(&bound_contexts, context,
333 g_slist_append(g_datalist_id_get_data(&bound_contexts, context), b));
335 grab_all_clients(TRUE);
340 static void binddef()
344 /* When creating an Action struct, all of the data elements in the
345 appropriate struct need to be set, except the Client*, which will be set
346 at call-time when then action function is used.
348 For action_move and action_resize, the 'x', 'y', and 'final' data
349 elements should not be set, as they are set at call-time.
352 a = action_new(action_move);
353 mbind("1", "titlebar", MouseAction_Motion, a);
354 a = action_new(action_move);
355 mbind("1", "handle", MouseAction_Motion, a);
356 a = action_new(action_move);
357 mbind("A-1", "frame", MouseAction_Motion, a);
359 a = action_new(action_resize);
360 mbind("1", "blcorner", MouseAction_Motion, a);
361 a = action_new(action_resize);
362 mbind("1", "brcorner", MouseAction_Motion, a);
363 a = action_new(action_resize);
364 mbind("A-3", "frame", MouseAction_Motion, a);
366 a = action_new(action_focusraise);
367 mbind("1", "titlebar", MouseAction_Press, a);
368 a = action_new(action_focusraise);
369 mbind("1", "handle", MouseAction_Press, a);
370 a = action_new(action_lower);
371 mbind("2", "titlebar", MouseAction_Press, a);
372 a = action_new(action_lower);
373 mbind("2", "handle", MouseAction_Press, a);
374 a = action_new(action_raise);
375 mbind("A-1", "frame", MouseAction_Click, a);
376 a = action_new(action_lower);
377 mbind("A-3", "frame", MouseAction_Click, a);
379 a = action_new(action_focusraise);
380 mbind("1", "client", MouseAction_Press, a);
382 a = action_new(action_toggle_shade);
383 mbind("1", "titlebar", MouseAction_DClick, a);
384 a = action_new(action_shade);
385 mbind("4", "titlebar", MouseAction_Press, a);
386 a = action_new(action_unshade);
387 mbind("5", "titlebar", MouseAction_Click, a);
389 a = action_new(action_toggle_maximize_full);
390 mbind("1", "maximize", MouseAction_Click, a);
391 a = action_new(action_toggle_maximize_vert);
392 mbind("2", "maximize", MouseAction_Click, a);
393 a = action_new(action_toggle_maximize_horz);
394 mbind("3", "maximize", MouseAction_Click, a);
395 a = action_new(action_iconify);
396 mbind("1", "iconify", MouseAction_Click, a);
397 a = action_new(action_close);
398 mbind("1", "icon", MouseAction_DClick, a);
399 a = action_new(action_close);
400 mbind("1", "close", MouseAction_Click, a);
401 a = action_new(action_toggle_omnipresent);
402 mbind("1", "alldesktops", MouseAction_Click, a);
404 a = action_new(action_next_desktop);
405 a->data.nextprevdesktop.wrap = TRUE;
406 mbind("4", "root", MouseAction_Click, a);
407 a = action_new(action_previous_desktop);
408 a->data.nextprevdesktop.wrap = TRUE;
409 mbind("5", "root", MouseAction_Click, a);
410 a = action_new(action_next_desktop);
411 a->data.nextprevdesktop.wrap = TRUE;
412 mbind("A-4", "root", MouseAction_Click, a);
413 a = action_new(action_previous_desktop);
414 a->data.nextprevdesktop.wrap = TRUE;
415 mbind("A-5", "root", MouseAction_Click, a);
416 a = action_new(action_next_desktop);
417 a->data.nextprevdesktop.wrap = TRUE;
418 mbind("A-4", "frame", MouseAction_Click, a);
419 a = action_new(action_previous_desktop);
420 a->data.nextprevdesktop.wrap = TRUE;
421 mbind("A-5", "frame", MouseAction_Click, a);
424 void plugin_startup()
426 dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
427 Event_X_ButtonPress | Event_X_ButtonRelease |
428 Event_X_MotionNotify, (EventHandler)event, NULL);
430 /* XXX parse a config */
434 void plugin_shutdown()
436 dispatch_register(0, (EventHandler)event, NULL);
438 grab_all_clients(FALSE);
439 g_datalist_foreach(&bound_contexts, foreach_clear, NULL);