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 PointerBinding*s. */
27 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
29 void mouse_grab_for_client(ObClient *client, gboolean grab)
34 for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
35 for (it = bound_contexts[i]; it != NULL; it = g_slist_next(it)) {
36 /* grab/ungrab the button */
37 ObMouseBinding *b = it->data;
42 if (FRAME_CONTEXT(i, client)) {
43 win = client->frame->window;
45 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
46 } else if (CLIENT_CONTEXT(i, client)) {
47 win = client->frame->plate;
48 mode = GrabModeSync; /* this is handled in event */
49 mask = ButtonPressMask; /* can't catch more than this with Sync
50 mode the release event is
51 manufactured in event() */
55 grab_button_full(b->button, b->state, win, mask, mode,
58 ungrab_button(b->button, b->state, win);
62 static void grab_all_clients(gboolean grab)
66 for (it = client_list; it != NULL; it = it->next)
67 mouse_grab_for_client(it->data, grab);
70 static void clearall()
75 for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
76 for (it = bound_contexts[i]; it != NULL; it = it->next) {
77 ObMouseBinding *b = it->data;
80 for (j = 0; j < OB_NUM_MOUSE_ACTIONS; ++j) {
83 for (it = b->actions[j]; it; it = it->next)
84 action_free(it->data);
85 g_slist_free(b->actions[j]);
89 g_slist_free(bound_contexts[i]);
90 bound_contexts[i] = NULL;
94 static gboolean fire_binding(ObMouseAction a, ObFrameContext context,
95 ObClient *c, guint state,
96 guint button, int x, int y)
101 for (it = bound_contexts[context]; it != NULL; it = it->next) {
103 if (b->state == state && b->button == button)
106 /* if not bound, then nothing to do! */
107 if (it == NULL) return FALSE;
109 for (it = b->actions[a]; it; it = it->next)
110 action_run_mouse(it->data, c, context, state, button, x, y);
114 void mouse_event(ObClient *client, ObFrameContext context, XEvent *e)
117 static guint button = 0, state = 0, lbutton = 0;
119 static Window lwindow = None;
121 gboolean click = FALSE;
122 gboolean dclick = FALSE;
126 px = e->xbutton.x_root;
127 py = e->xbutton.y_root;
128 button = e->xbutton.button;
129 state = e->xbutton.state;
131 fire_binding(OB_MOUSE_ACTION_PRESS, context,
132 client, e->xbutton.state,
134 e->xbutton.x_root, e->xbutton.y_root);
136 if (CLIENT_CONTEXT(context, client)) {
137 /* Replay the event, so it goes to the client*/
138 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
139 /* Fall through to the release case! */
144 if (e->xbutton.button == button) {
145 /* clicks are only valid if its released over the window */
148 guint ujunk, b, w, h;
149 xerror_set_ignore(TRUE);
150 junk1 = XGetGeometry(ob_display, e->xbutton.window,
151 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
152 xerror_set_ignore(FALSE);
154 if (e->xbutton.x >= (signed)-b &&
155 e->xbutton.y >= (signed)-b &&
156 e->xbutton.x < (signed)(w+b) &&
157 e->xbutton.y < (signed)(h+b)) {
159 /* double clicks happen if there were 2 in a row! */
160 if (lbutton == button &&
161 lwindow == e->xbutton.window &&
162 e->xbutton.time - config_mouse_dclicktime <=
168 lwindow = e->xbutton.window;
178 ltime = e->xbutton.time;
180 fire_binding(OB_MOUSE_ACTION_RELEASE, context,
181 client, e->xbutton.state,
183 e->xbutton.x_root, e->xbutton.y_root);
185 fire_binding(OB_MOUSE_ACTION_CLICK, context,
186 client, e->xbutton.state,
191 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
192 client, e->xbutton.state,
200 if (ABS(e->xmotion.x_root - px) >=
201 config_mouse_threshold ||
202 ABS(e->xmotion.y_root - py) >=
203 config_mouse_threshold) {
205 /* You can't drag on buttons */
206 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
207 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
208 context == OB_FRAME_CONTEXT_SHADE ||
209 context == OB_FRAME_CONTEXT_ICONIFY ||
210 context == OB_FRAME_CONTEXT_ICON ||
211 context == OB_FRAME_CONTEXT_CLOSE)
214 fire_binding(OB_MOUSE_ACTION_MOTION, context,
215 client, state, button, px, py);
223 g_assert_not_reached();
227 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
231 ObFrameContext context;
235 if (!translate_button(buttonstr, &state, &button)) {
236 g_warning("invalid button '%s'", buttonstr);
240 contextstr = g_ascii_strdown(contextstr, -1);
241 context = frame_context_from_string(contextstr);
243 g_warning("invalid context '%s'", contextstr);
249 for (it = bound_contexts[context]; it != NULL; it = it->next){
251 if (b->state == state && b->button == button) {
252 b->actions[mact] = g_slist_append(b->actions[mact], action);
257 /* when there are no modifiers in the binding, then the action cannot
259 if (!state && action->data.any.interactive) {
260 action->data.any.interactive = FALSE;
261 action->data.inter.final = TRUE;
264 /* add the binding */
265 b = g_new0(ObMouseBinding, 1);
268 b->actions[mact] = g_slist_append(NULL, action);
269 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
274 void mouse_startup(gboolean reconfig)
276 grab_all_clients(TRUE);
279 void mouse_shutdown(gboolean reconfig)
281 grab_all_clients(FALSE);