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/engine.h"
11 /* GData of GSList*s of PointerBinding*s. */
12 static GData *bound_contexts;
14 struct foreach_grab_temp {
19 static void grab_button(Client *client, guint state, guint button,
20 GQuark context, gboolean grab)
23 int mode = GrabModeAsync;
26 if (context == g_quark_try_string("frame")) {
27 win = client->frame->window;
28 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
29 } else if (context == g_quark_try_string("client")) {
30 win = client->frame->plate;
31 mode = GrabModeSync; /* this is handled in pointer_event */
32 mask = ButtonPressMask; /* can't catch more than this with Sync mode
33 the release event is manufactured in
38 /* XXX grab all lock keys */
39 XGrabButton(ob_display, button, state, win, FALSE, mask, mode,
40 GrabModeAsync, None, None);
42 /* XXX ungrab all lock keys */
43 XUngrabButton(ob_display, button, state, win);
46 static void foreach_grab(GQuark key, gpointer data, gpointer user_data)
48 struct foreach_grab_temp *d = user_data;
50 for (it = data; it != NULL; it = it->next) {
51 MouseBinding *b = it->data;
52 grab_button(d->client, b->state, b->button, key, d->grab);
56 static void grab_for_client(Client *client, gboolean grab)
58 struct foreach_grab_temp bt;
61 g_datalist_foreach(&bound_contexts, foreach_grab, &bt);
64 static void grab_all_clients(gboolean grab)
68 for (it = client_list; it != NULL; it = it->next)
69 grab_for_client(it->data, grab);
72 static void foreach_clear(GQuark key, gpointer data, gpointer user_data)
75 user_data = user_data;
76 for (it = data; it != NULL; it = it->next) {
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]);
88 static void fire_button(MouseAction a, GQuark context, Client *c, guint state,
94 for (it = g_datalist_id_get_data(&bound_contexts, context);
95 it != NULL; it = it->next) {
97 if (b->state == state && b->button == button)
100 /* if not bound, then nothing to do! */
101 if (it == NULL) return;
103 if (b->action[a] != NULL && b->action[a]->func != NULL) {
104 b->action[a]->data.any.c = c;
106 g_assert(!(b->action[a]->func == action_move ||
107 b->action[a]->func == action_resize));
109 b->action[a]->func(&b->action[a]->data);
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)
122 for (it = g_datalist_id_get_data(&bound_contexts, context);
123 it != NULL; it = it->next) {
125 if (b->state == state && b->button == button)
128 /* if not bound, then nothing to do! */
129 if (it == NULL) return;
131 if (b->action[a] != NULL && b->action[a]->func != NULL) {
132 b->action[a]->data.any.c = c;
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;
142 b->action[a]->data.resize.x = cw + dx;
143 b->action[a]->data.resize.y = ch + dy;
145 case Corner_TopRight:
146 b->action[a]->data.resize.x = cw - dx;
147 b->action[a]->data.resize.y = ch + dy;
149 case Corner_BottomLeft:
150 b->action[a]->data.resize.x = cw + dx;
151 b->action[a]->data.resize.y = ch - dy;
153 case Corner_BottomRight:
154 b->action[a]->data.resize.x = cw - dx;
155 b->action[a]->data.resize.y = ch - dy;
158 b->action[a]->data.resize.final = final;
160 b->action[a]->func(&b->action[a]->data);
164 static Corner pick_corner(int x, int y, int cx, int cy, int cw, int ch)
166 if (x - cx < cw / 2) {
168 return Corner_BottomRight;
170 return Corner_TopRight;
173 return Corner_BottomLeft;
175 return Corner_TopLeft;
179 static void event(ObEvent *e, void *foo)
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;
191 case Event_Client_Mapped:
192 grab_for_client(e->data.c.client, TRUE);
195 case Event_Client_Destroy:
196 grab_for_client(e->data.c.client, FALSE);
199 case Event_X_ButtonPress:
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);
210 button = e->data.x.e->xbutton.button;
212 context = engine_get_context(e->data.x.client,
213 e->data.x.e->xbutton.window);
215 fire_button(MouseAction_Press, context,
216 e->data.x.client, e->data.x.e->xbutton.state,
217 e->data.x.e->xbutton.button);
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! */
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) {
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);
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) {
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) {
251 /* double clicks happen if there were 2 in a row! */
252 if (lbutton == button &&
253 e->data.x.e->xbutton.time - 300 <= ltime)
262 ltime = e->data.x.e->xbutton.time;
264 fire_button(MouseAction_Press, context,
265 e->data.x.client, e->data.x.e->xbutton.state,
266 e->data.x.e->xbutton.button);
268 fire_button(MouseAction_Click, context,
269 e->data.x.client, e->data.x.e->xbutton.state,
270 e->data.x.e->xbutton.button);
272 fire_button(MouseAction_DClick, context,
273 e->data.x.client, e->data.x.e->xbutton.state,
274 e->data.x.e->xbutton.button);
277 case Event_X_MotionNotify:
280 dx = e->data.x.e->xmotion.x_root - px;
281 dy = e->data.x.e->xmotion.y_root - py;
282 context = engine_get_context(e->data.x.client,
283 e->data.x.e->xbutton.window);
284 fire_motion(MouseAction_Motion, context,
285 e->data.x.client, e->data.x.e->xmotion.state,
286 button, cx, cy, cw, ch, dx, dy, FALSE, corner);
291 g_assert_not_reached();
295 static gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
304 if (!translate_button(buttonstr, &state, &button)) {
305 g_warning("invalid button");
309 context = g_quark_try_string(contextstr);
311 g_warning("invalid context");
315 for (it = g_datalist_id_get_data(&bound_contexts, context);
316 it != NULL; it = it->next){
318 if (b->state == state && b->button == button) {
320 if (b->action[mact] != NULL) {
321 g_warning("duplicate binding");
324 b->action[mact] = action;
329 grab_all_clients(FALSE);
331 /* add the binding */
332 b = g_new(MouseBinding, 1);
335 for (i = 0; i < NUM_MOUSEACTION; ++i)
337 b->action[mact] = action;
338 g_datalist_id_set_data(&bound_contexts, context,
339 g_slist_append(g_datalist_id_get_data(&bound_contexts, context), b));
341 grab_all_clients(TRUE);
346 static void binddef()
350 /* When creating an Action struct, all of the data elements in the
351 appropriate struct need to be set, except the Client*, which will be set
352 at call-time when then action function is used.
354 For action_move and action_resize, the 'x', 'y', and 'final' data
355 elements should not be set, as they are set at call-time.
358 a = action_new(action_move);
359 mbind("1", "titlebar", MouseAction_Motion, a);
360 a = action_new(action_move);
361 mbind("1", "handle", MouseAction_Motion, a);
362 a = action_new(action_move);
363 mbind("A-1", "frame", MouseAction_Motion, a);
365 a = action_new(action_resize);
366 mbind("1", "blcorner", MouseAction_Motion, a);
367 a = action_new(action_resize);
368 mbind("1", "brcorner", MouseAction_Motion, a);
369 a = action_new(action_resize);
370 mbind("A-3", "frame", MouseAction_Motion, a);
372 a = action_new(action_focusraise);
373 mbind("1", "titlebar", MouseAction_Press, a);
374 a = action_new(action_focusraise);
375 mbind("1", "handle", MouseAction_Press, a);
376 a = action_new(action_lower);
377 mbind("2", "titlebar", MouseAction_Press, a);
378 a = action_new(action_lower);
379 mbind("2", "handle", MouseAction_Press, a);
380 a = action_new(action_raise);
381 mbind("A-1", "frame", MouseAction_Click, a);
382 a = action_new(action_lower);
383 mbind("A-3", "frame", MouseAction_Click, a);
385 a = action_new(action_focusraise);
386 mbind("1", "client", MouseAction_Press, a);
388 a = action_new(action_toggle_shade);
389 mbind("1", "titlebar", MouseAction_DClick, a);
390 a = action_new(action_shade);
391 mbind("4", "titlebar", MouseAction_Press, a);
392 a = action_new(action_unshade);
393 mbind("5", "titlebar", MouseAction_Click, a);
395 a = action_new(action_toggle_maximize_full);
396 mbind("1", "maximize", MouseAction_Click, a);
397 a = action_new(action_toggle_maximize_vert);
398 mbind("2", "maximize", MouseAction_Click, a);
399 a = action_new(action_toggle_maximize_horz);
400 mbind("3", "maximize", MouseAction_Click, a);
401 a = action_new(action_iconify);
402 mbind("1", "iconify", MouseAction_Click, a);
403 a = action_new(action_close);
404 mbind("1", "icon", MouseAction_DClick, a);
405 a = action_new(action_close);
406 mbind("1", "close", MouseAction_Click, a);
407 a = action_new(action_toggle_omnipresent);
408 mbind("1", "alldesktops", MouseAction_Click, a);
410 a = action_new(action_next_desktop);
411 a->data.nextprevdesktop.wrap = TRUE;
412 mbind("4", "root", MouseAction_Click, a);
413 a = action_new(action_previous_desktop);
414 a->data.nextprevdesktop.wrap = TRUE;
415 mbind("5", "root", MouseAction_Click, a);
416 a = action_new(action_next_desktop);
417 a->data.nextprevdesktop.wrap = TRUE;
418 mbind("A-4", "root", MouseAction_Click, a);
419 a = action_new(action_previous_desktop);
420 a->data.nextprevdesktop.wrap = TRUE;
421 mbind("A-5", "root", MouseAction_Click, a);
422 a = action_new(action_next_desktop);
423 a->data.nextprevdesktop.wrap = TRUE;
424 mbind("A-4", "frame", MouseAction_Click, a);
425 a = action_new(action_previous_desktop);
426 a->data.nextprevdesktop.wrap = TRUE;
427 mbind("A-5", "frame", MouseAction_Click, a);
430 void plugin_startup()
432 dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
433 Event_X_ButtonPress | Event_X_ButtonRelease |
434 Event_X_MotionNotify, (EventHandler)event, NULL);
436 /* XXX parse a config */
440 void plugin_shutdown()
442 dispatch_register(0, (EventHandler)event, NULL);
444 grab_all_clients(FALSE);
445 g_datalist_foreach(&bound_contexts, foreach_clear, NULL);