10 #include "translate.h"
18 GSList *actions[OB_MOUSE_NUM_ACTIONS]; /* lists of Action pointers */
21 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
22 co == OB_FRAME_CONTEXT_FRAME : FALSE)
23 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
24 co == OB_FRAME_CONTEXT_DESKTOP : \
25 co == OB_FRAME_CONTEXT_CLIENT)
27 /* Array of GSList*s of PointerBinding*s. */
28 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
30 void mouse_grab_for_client(ObClient *client, gboolean grab)
35 for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
36 for (it = bound_contexts[i]; it != NULL; it = it->next) {
37 /* grab/ungrab the button */
38 ObMouseBinding *b = it->data;
43 if (FRAME_CONTEXT(i, client)) {
44 win = client->frame->window;
46 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
47 } else if (CLIENT_CONTEXT(i, client)) {
48 win = client->frame->plate;
49 mode = GrabModeSync; /* this is handled in event */
50 mask = ButtonPressMask; /* can't catch more than this with Sync
51 mode the release event is
52 manufactured in event() */
56 grab_button_full(b->button, b->state, win, mask, mode, None);
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) {
79 ObMouseBinding *b = it->data;
80 for (j = 0; j < OB_MOUSE_NUM_ACTIONS; ++j) {
82 for (it = b->actions[j]; it; it = it->next) {
83 action_free(it->data);
85 g_slist_free(b->actions[j]);
89 g_slist_free(bound_contexts[i]);
93 static gboolean fire_button(ObMouseAction a, ObFrameContext context,
94 ObClient *c, guint state,
95 guint button, int x, int y)
100 for (it = bound_contexts[context]; it != NULL; it = it->next) {
102 if (b->state == state && b->button == button)
105 /* if not bound, then nothing to do! */
106 if (it == NULL) return FALSE;
108 for (it = b->actions[a]; it; it = it->next) {
109 ObAction *act = it->data;
110 if (act->func != NULL) {
113 g_assert(act->func != action_moveresize);
115 if (act->func == action_showmenu) {
116 act->data.showmenu.x = x;
117 act->data.showmenu.y = y;
120 if (act->data.any.interactive) {
121 act->data.inter.cancel = FALSE;
122 act->data.inter.final = FALSE;
123 keyboard_interactive_grab(state, c, context, act);
126 act->func(&act->data);
132 static gboolean fire_motion(ObMouseAction a, ObFrameContext context,
133 ObClient *c, guint state, guint button,
134 int x_root, int y_root, guint32 corner)
139 for (it = bound_contexts[context]; it != NULL; it = it->next) {
141 if (b->state == state && b->button == button)
144 /* if not bound, then nothing to do! */
145 if (it == NULL) return FALSE;
147 for (it = b->actions[a]; it; it = it->next) {
148 ObAction *act = it->data;
149 if (act->func != NULL) {
152 if (act->func == action_moveresize) {
153 act->data.moveresize.x = x_root;
154 act->data.moveresize.y = y_root;
155 act->data.moveresize.button = button;
156 if (!(act->data.moveresize.corner ==
157 prop_atoms.net_wm_moveresize_move ||
158 act->data.moveresize.corner ==
159 prop_atoms.net_wm_moveresize_move_keyboard ||
160 act->data.moveresize.corner ==
161 prop_atoms.net_wm_moveresize_size_keyboard))
162 act->data.moveresize.corner = corner;
164 g_assert_not_reached();
166 act->func(&act->data);
172 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
174 if (x - cx < cw / 2) {
176 return prop_atoms.net_wm_moveresize_size_topleft;
178 return prop_atoms.net_wm_moveresize_size_bottomleft;
181 return prop_atoms.net_wm_moveresize_size_topright;
183 return prop_atoms.net_wm_moveresize_size_bottomright;
187 void mouse_event(ObClient *client, ObFrameContext context, XEvent *e)
190 static guint button = 0, state = 0, lbutton = 0;
192 static Window lwindow = None;
194 gboolean click = FALSE;
195 gboolean dclick = FALSE;
199 px = e->xbutton.x_root;
200 py = e->xbutton.y_root;
201 button = e->xbutton.button;
202 state = e->xbutton.state;
204 fire_button(OB_MOUSE_ACTION_PRESS, context,
205 client, e->xbutton.state,
207 e->xbutton.x_root, e->xbutton.y_root);
209 if (CLIENT_CONTEXT(context, client)) {
210 /* Replay the event, so it goes to the client*/
211 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
212 /* Fall through to the release case! */
217 if (e->xbutton.button == button) {
218 /* clicks are only valid if its released over the window */
221 guint ujunk, b, w, h;
222 xerror_set_ignore(TRUE);
223 junk1 = XGetGeometry(ob_display, e->xbutton.window,
224 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
225 xerror_set_ignore(FALSE);
227 if (e->xbutton.x >= (signed)-b &&
228 e->xbutton.y >= (signed)-b &&
229 e->xbutton.x < (signed)(w+b) &&
230 e->xbutton.y < (signed)(h+b)) {
232 /* double clicks happen if there were 2 in a row! */
233 if (lbutton == button &&
234 lwindow == e->xbutton.window &&
235 e->xbutton.time - config_mouse_dclicktime <=
241 lwindow = e->xbutton.window;
251 ltime = e->xbutton.time;
253 fire_button(OB_MOUSE_ACTION_RELEASE, context,
254 client, e->xbutton.state,
256 e->xbutton.x_root, e->xbutton.y_root);
258 fire_button(OB_MOUSE_ACTION_CLICK, context,
259 client, e->xbutton.state,
264 fire_button(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
265 client, e->xbutton.state,
273 if (ABS(e->xmotion.x_root - px) >=
274 config_mouse_threshold ||
275 ABS(e->xmotion.y_root - py) >=
276 config_mouse_threshold) {
279 /* You can't drag on buttons */
280 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
281 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
282 context == OB_FRAME_CONTEXT_SHADE ||
283 context == OB_FRAME_CONTEXT_ICONIFY ||
284 context == OB_FRAME_CONTEXT_ICON ||
285 context == OB_FRAME_CONTEXT_CLOSE)
289 corner = prop_atoms.net_wm_moveresize_size_bottomright;
292 pick_corner(e->xmotion.x_root,
294 client->frame->area.x,
295 client->frame->area.y,
296 /* use the client size because the frame
297 can be differently sized (shaded
298 windows) and we want this based on the
301 client->frame->size.left +
302 client->frame->size.right,
303 client->area.height +
304 client->frame->size.top +
305 client->frame->size.bottom);
306 fire_motion(OB_MOUSE_ACTION_MOTION, context,
307 client, state, button, px, py, corner);
315 g_assert_not_reached();
319 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
323 ObFrameContext context;
327 if (!translate_button(buttonstr, &state, &button)) {
328 g_warning("invalid button '%s'", buttonstr);
332 contextstr = g_ascii_strdown(contextstr, -1);
333 context = frame_context_from_string(contextstr);
335 g_warning("invalid context '%s'", contextstr);
341 for (it = bound_contexts[context]; it != NULL; it = it->next){
343 if (b->state == state && b->button == button) {
344 b->actions[mact] = g_slist_append(b->actions[mact], action);
349 grab_all_clients(FALSE);
351 /* when there are no modifiers in the binding, then the action cannot
353 if (!state && action->data.any.interactive) {
354 action->data.any.interactive = FALSE;
355 action->data.inter.final = TRUE;
358 /* add the binding */
359 b = g_new0(ObMouseBinding, 1);
362 b->actions[mact] = g_slist_append(NULL, action);
363 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
365 grab_all_clients(TRUE);
374 void mouse_shutdown()
376 grab_all_clients(FALSE);