1 #include "../../kernel/openbox.h"
2 #include "../../kernel/dispatch.h"
3 #include "../../kernel/action.h"
4 #include "../../kernel/event.h"
5 #include "../../kernel/client.h"
6 #include "../../kernel/frame.h"
7 #include "../../kernel/grab.h"
8 #include "../../kernel/engine.h"
11 #include "mouserc_parse.h"
14 void plugin_setup_config()
18 static int drag_threshold = 3;
20 /* GData of GSList*s of PointerBinding*s. */
21 static GData *bound_contexts;
23 struct foreach_grab_temp {
28 static void foreach_grab(GQuark key, gpointer data, gpointer user_data)
30 struct foreach_grab_temp *d = user_data;
32 for (it = data; it != NULL; it = it->next) {
33 /* grab/ungrab the button */
34 MouseBinding *b = it->data;
39 if (key == g_quark_try_string("frame")) {
40 win = d->client->frame->window;
42 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
43 } else if (key == g_quark_try_string("client")) {
44 win = d->client->frame->plate;
45 mode = GrabModeSync; /* this is handled in event */
46 mask = ButtonPressMask; /* can't catch more than this with Sync
47 mode the release event is manufactured
52 grab_button(b->button, b->state, win, mask, mode);
54 ungrab_button(b->button, b->state, win);
58 static void grab_for_client(Client *client, gboolean grab)
60 struct foreach_grab_temp bt;
63 g_datalist_foreach(&bound_contexts, foreach_grab, &bt);
66 static void grab_all_clients(gboolean grab)
70 for (it = client_list; it != NULL; it = it->next)
71 grab_for_client(it->data, grab);
74 static void foreach_clear(GQuark key, gpointer data, gpointer user_data)
77 user_data = user_data;
78 for (it = data; it != NULL; it = it->next) {
81 MouseBinding *b = it->data;
82 for (i = 0; i < NUM_MOUSEACTION; ++i)
83 if (b->action[i] != NULL)
84 action_free(b->action[i]);
90 static void fire_button(MouseAction a, GQuark context, Client *c, guint state,
96 for (it = g_datalist_id_get_data(&bound_contexts, context);
97 it != NULL; it = it->next) {
99 if (b->state == state && b->button == button)
102 /* if not bound, then nothing to do! */
103 if (it == NULL) return;
105 if (b->action[a] != NULL && b->action[a]->func != NULL) {
106 b->action[a]->data.any.c = c;
108 g_assert(!(b->action[a]->func == action_move ||
109 b->action[a]->func == action_resize));
111 b->action[a]->func(&b->action[a]->data);
115 /* corner should be the opposite corner of the window in which the pointer
116 clicked, Corner_TopLeft if a good default if there is no client */
117 static void fire_motion(MouseAction a, GQuark context, Client *c, guint state,
118 guint button, int cx, int cy, int cw, int ch,
119 int dx, int dy, gboolean final, Corner corner)
124 for (it = g_datalist_id_get_data(&bound_contexts, context);
125 it != NULL; it = it->next) {
127 if (b->state == state && b->button == button)
130 /* if not bound, then nothing to do! */
131 if (it == NULL) return;
133 if (b->action[a] != NULL && b->action[a]->func != NULL) {
134 b->action[a]->data.any.c = c;
136 if (b->action[a]->func == action_move) {
137 b->action[a]->data.move.x = cx + dx;
138 b->action[a]->data.move.y = cy + dy;
139 b->action[a]->data.move.final = final;
140 } else if (b->action[a]->func == action_resize) {
141 b->action[a]->data.resize.corner = corner;
144 b->action[a]->data.resize.x = cw + dx;
145 b->action[a]->data.resize.y = ch + dy;
147 case Corner_TopRight:
148 b->action[a]->data.resize.x = cw - dx;
149 b->action[a]->data.resize.y = ch + dy;
151 case Corner_BottomLeft:
152 b->action[a]->data.resize.x = cw + dx;
153 b->action[a]->data.resize.y = ch - dy;
155 case Corner_BottomRight:
156 b->action[a]->data.resize.x = cw - dx;
157 b->action[a]->data.resize.y = ch - dy;
160 b->action[a]->data.resize.final = final;
162 b->action[a]->func(&b->action[a]->data);
166 static Corner pick_corner(int x, int y, int cx, int cy, int cw, int ch)
168 if (x - cx < cw / 2) {
170 return Corner_BottomRight;
172 return Corner_TopRight;
175 return Corner_BottomLeft;
177 return Corner_TopLeft;
181 static void event(ObEvent *e, void *foo)
184 static int px, py, cx, cy, cw, ch, dx, dy;
185 static guint button = 0, lbutton = 0;
186 static gboolean drag = FALSE;
187 static Corner corner = Corner_TopLeft;
188 gboolean click = FALSE;
189 gboolean dclick = FALSE;
193 case Event_Client_Mapped:
194 grab_for_client(e->data.c.client, TRUE);
197 case Event_Client_Destroy:
198 grab_for_client(e->data.c.client, FALSE);
201 case Event_X_ButtonPress:
203 if (e->data.x.client != NULL) {
204 cx = e->data.x.client->frame->area.x;
205 cy = e->data.x.client->frame->area.y;
206 cw = e->data.x.client->frame->area.width;
207 ch = e->data.x.client->frame->area.height;
208 px = e->data.x.e->xbutton.x_root;
209 py = e->data.x.e->xbutton.y_root;
210 corner = pick_corner(px, py, cx, cy, cw, ch);
212 button = e->data.x.e->xbutton.button;
214 context = engine_get_context(e->data.x.client,
215 e->data.x.e->xbutton.window);
216 g_message("press %d frame %d client %d",
218 g_quark_try_string("frame"),
219 g_quark_try_string("client"));
221 fire_button(MouseAction_Press, context,
222 e->data.x.client, e->data.x.e->xbutton.state,
223 e->data.x.e->xbutton.button);
225 if (context == g_quark_try_string("client")) {
226 /* Replay the event, so it goes to the client*/
227 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
228 /* Fall through to the release case! */
232 case Event_X_ButtonRelease:
233 context = engine_get_context(e->data.x.client,
234 e->data.x.e->xbutton.window);
235 g_message("release %d frame %d client %d",
237 g_quark_try_string("frame"),
238 g_quark_try_string("client"));
239 if (e->data.x.e->xbutton.button == button) {
242 fire_motion(MouseAction_Motion, context,
243 e->data.x.client, e->data.x.e->xbutton.state,
244 e->data.x.e->xbutton.button,
245 cx, cy, cw, ch, dx, dy, TRUE, corner);
250 /* clicks are only valid if its released over the window */
253 guint ujunk, b, w, h;
254 XGetGeometry(ob_display, e->data.x.e->xbutton.window,
255 &wjunk, &junk, &junk, &w, &h, &b, &ujunk);
256 if (e->data.x.e->xbutton.x >= (signed)-b &&
257 e->data.x.e->xbutton.y >= (signed)-b &&
258 e->data.x.e->xbutton.x < (signed)(w+b) &&
259 e->data.x.e->xbutton.y < (signed)(h+b)) {
261 /* double clicks happen if there were 2 in a row! */
262 if (lbutton == button &&
263 e->data.x.e->xbutton.time - 300 <= ltime)
270 ltime = e->data.x.e->xbutton.time;
272 fire_button(MouseAction_Press, context,
273 e->data.x.client, e->data.x.e->xbutton.state,
274 e->data.x.e->xbutton.button);
276 fire_button(MouseAction_Click, context,
277 e->data.x.client, e->data.x.e->xbutton.state,
278 e->data.x.e->xbutton.button);
280 fire_button(MouseAction_DClick, context,
281 e->data.x.client, e->data.x.e->xbutton.state,
282 e->data.x.e->xbutton.button);
285 case Event_X_MotionNotify:
287 dx = e->data.x.e->xmotion.x_root - px;
288 dy = e->data.x.e->xmotion.y_root - py;
289 if (ABS(dx) >= drag_threshold || ABS(dy) >= drag_threshold)
292 context = engine_get_context(e->data.x.client,
293 e->data.x.e->xbutton.window);
294 fire_motion(MouseAction_Motion, context,
295 e->data.x.client, e->data.x.e->xmotion.state,
296 button, cx, cy, cw, ch, dx, dy, FALSE, corner);
302 g_assert_not_reached();
306 gboolean mbind(char *buttonstr, char *contextstr, MouseAction mact,
314 if (!translate_button(buttonstr, &state, &button)) {
315 g_warning("invalid button '%s'", buttonstr);
319 contextstr = g_ascii_strdown(contextstr, -1);
320 context = g_quark_try_string(contextstr);
322 g_warning("invalid context '%s'", contextstr);
328 for (it = g_datalist_id_get_data(&bound_contexts, context);
329 it != NULL; it = it->next){
331 if (b->state == state && b->button == button) {
333 if (b->action[mact] != NULL) {
334 g_warning("duplicate binding");
337 b->action[mact] = action;
342 grab_all_clients(FALSE);
344 /* add the binding */
345 b = g_new0(MouseBinding, 1);
348 b->action[mact] = action;
349 g_datalist_id_set_data(&bound_contexts, context,
350 g_slist_append(g_datalist_id_get_data(&bound_contexts, context), b));
352 grab_all_clients(TRUE);
357 void plugin_startup()
359 dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
360 Event_X_ButtonPress | Event_X_ButtonRelease |
361 Event_X_MotionNotify, (EventHandler)event, NULL);
366 void plugin_shutdown()
368 dispatch_register(0, (EventHandler)event, NULL);
370 grab_all_clients(FALSE);
371 g_datalist_foreach(&bound_contexts, foreach_clear, NULL);