10 #include "translate.h"
17 GSList *actions[OB_NUM_MOUSE_ACTIONS]; /* lists of Action pointers */
20 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
21 co == OB_FRAME_CONTEXT_FRAME : FALSE)
22 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
23 co == OB_FRAME_CONTEXT_DESKTOP : \
24 co == OB_FRAME_CONTEXT_CLIENT)
26 /* Array of GSList*s of ObMouseBinding*s. */
27 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
29 ObFrameContext mouse_button_frame_context(ObFrameContext context,
33 ObFrameContext x = context;
35 for (it = bound_contexts[context]; it; it = g_slist_next(it)) {
36 ObMouseBinding *b = it->data;
38 if (b->button == button)
43 case OB_FRAME_CONTEXT_NONE:
44 case OB_FRAME_CONTEXT_DESKTOP:
45 case OB_FRAME_CONTEXT_CLIENT:
46 case OB_FRAME_CONTEXT_TITLEBAR:
47 case OB_FRAME_CONTEXT_HANDLE:
48 case OB_FRAME_CONTEXT_FRAME:
49 case OB_FRAME_CONTEXT_MOVE_RESIZE:
51 case OB_FRAME_CONTEXT_BLCORNER:
52 case OB_FRAME_CONTEXT_BRCORNER:
53 x = OB_FRAME_CONTEXT_HANDLE;
55 case OB_FRAME_CONTEXT_TLCORNER:
56 case OB_FRAME_CONTEXT_TRCORNER:
57 case OB_FRAME_CONTEXT_MAXIMIZE:
58 case OB_FRAME_CONTEXT_ALLDESKTOPS:
59 case OB_FRAME_CONTEXT_SHADE:
60 case OB_FRAME_CONTEXT_ICONIFY:
61 case OB_FRAME_CONTEXT_ICON:
62 case OB_FRAME_CONTEXT_CLOSE:
63 x = OB_FRAME_CONTEXT_TITLEBAR;
65 case OB_FRAME_NUM_CONTEXTS:
66 g_assert_not_reached();
72 void mouse_grab_for_client(ObClient *client, gboolean grab)
77 for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
78 for (it = bound_contexts[i]; it != NULL; it = g_slist_next(it)) {
79 /* grab/ungrab the button */
80 ObMouseBinding *b = it->data;
85 if (FRAME_CONTEXT(i, client)) {
86 win = client->frame->window;
88 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
89 } else if (CLIENT_CONTEXT(i, client)) {
90 win = client->frame->plate;
91 mode = GrabModeSync; /* this is handled in event */
92 mask = ButtonPressMask; /* can't catch more than this with Sync
93 mode the release event is
94 manufactured in event() */
98 grab_button_full(b->button, b->state, win, mask, mode,
101 ungrab_button(b->button, b->state, win);
105 static void grab_all_clients(gboolean grab)
109 for (it = client_list; it != NULL; it = it->next)
110 mouse_grab_for_client(it->data, grab);
113 static void clearall()
118 for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
119 for (it = bound_contexts[i]; it != NULL; it = it->next) {
120 ObMouseBinding *b = it->data;
123 for (j = 0; j < OB_NUM_MOUSE_ACTIONS; ++j) {
126 for (it = b->actions[j]; it; it = it->next)
127 action_free(it->data);
128 g_slist_free(b->actions[j]);
132 g_slist_free(bound_contexts[i]);
133 bound_contexts[i] = NULL;
137 static gboolean fire_binding(ObMouseAction a, ObFrameContext context,
138 ObClient *c, guint state,
139 guint button, int x, int y)
144 for (it = bound_contexts[context]; it != NULL; it = it->next) {
146 if (b->state == state && b->button == button)
149 /* if not bound, then nothing to do! */
150 if (it == NULL) return FALSE;
152 for (it = b->actions[a]; it; it = it->next)
153 action_run_mouse(it->data, c, state, button, x, y);
157 void mouse_event(ObClient *client, XEvent *e)
160 static guint button = 0, state = 0, lbutton = 0;
161 static Window lwindow = None;
164 ObFrameContext context;
165 gboolean click = FALSE;
166 gboolean dclick = FALSE;
170 context = frame_context(client, e->xany.window);
171 context = mouse_button_frame_context(context, e->xbutton.button);
173 px = e->xbutton.x_root;
174 py = e->xbutton.y_root;
175 button = e->xbutton.button;
176 state = e->xbutton.state;
178 fire_binding(OB_MOUSE_ACTION_PRESS, context,
179 client, e->xbutton.state,
181 e->xbutton.x_root, e->xbutton.y_root);
183 if (CLIENT_CONTEXT(context, client)) {
184 /* Replay the event, so it goes to the client*/
185 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
186 /* Fall through to the release case! */
191 context = frame_context(client, e->xany.window);
192 context = mouse_button_frame_context(context, e->xbutton.button);
194 if (e->xbutton.button == button) {
195 /* clicks are only valid if its released over the window */
198 guint ujunk, b, w, h;
199 xerror_set_ignore(TRUE);
200 junk1 = XGetGeometry(ob_display, e->xbutton.window,
201 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
202 xerror_set_ignore(FALSE);
204 if (e->xbutton.x >= (signed)-b &&
205 e->xbutton.y >= (signed)-b &&
206 e->xbutton.x < (signed)(w+b) &&
207 e->xbutton.y < (signed)(h+b)) {
209 /* double clicks happen if there were 2 in a row! */
210 if (lbutton == button &&
211 lwindow == e->xbutton.window &&
212 e->xbutton.time - config_mouse_dclicktime <=
218 lwindow = e->xbutton.window;
228 ltime = e->xbutton.time;
230 fire_binding(OB_MOUSE_ACTION_RELEASE, context,
231 client, e->xbutton.state,
233 e->xbutton.x_root, e->xbutton.y_root);
235 fire_binding(OB_MOUSE_ACTION_CLICK, context,
236 client, e->xbutton.state,
241 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
242 client, e->xbutton.state,
250 context = frame_context(client, e->xany.window);
251 context = mouse_button_frame_context(context, button);
253 if (ABS(e->xmotion.x_root - px) >=
254 config_mouse_threshold ||
255 ABS(e->xmotion.y_root - py) >=
256 config_mouse_threshold) {
258 /* You can't drag on buttons */
259 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
260 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
261 context == OB_FRAME_CONTEXT_SHADE ||
262 context == OB_FRAME_CONTEXT_ICONIFY ||
263 context == OB_FRAME_CONTEXT_ICON ||
264 context == OB_FRAME_CONTEXT_CLOSE)
267 fire_binding(OB_MOUSE_ACTION_MOTION, context,
268 client, state, button, px, py);
276 g_assert_not_reached();
280 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
284 ObFrameContext context;
288 if (!translate_button(buttonstr, &state, &button)) {
289 g_warning("invalid button '%s'", buttonstr);
293 context = frame_context_from_string(contextstr);
295 g_warning("invalid context '%s'", contextstr);
299 for (it = bound_contexts[context]; it != NULL; it = it->next){
301 if (b->state == state && b->button == button) {
302 b->actions[mact] = g_slist_append(b->actions[mact], action);
307 /* when there are no modifiers in the binding, then the action cannot
309 if (!state && action->data.any.interactive) {
310 action->data.any.interactive = FALSE;
311 action->data.inter.final = TRUE;
314 /* add the binding */
315 b = g_new0(ObMouseBinding, 1);
318 b->actions[mact] = g_slist_append(NULL, action);
319 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
324 void mouse_startup(gboolean reconfig)
326 grab_all_clients(TRUE);
329 void mouse_shutdown(gboolean reconfig)
331 grab_all_clients(FALSE);