10 #include "translate.h"
18 GSList *actions[OB_MOUSE_NUM_ACTIONS]; /* lists of Action pointers */
21 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
22 co == OB_FRAME_CONTEXT_DESKTOP : \
23 co == OB_FRAME_CONTEXT_CLIENT)
25 /* Array of GSList*s of PointerBinding*s. */
26 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
28 void mouse_grab_for_client(ObClient *client, gboolean grab)
33 for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
34 for (it = bound_contexts[i]; it != NULL; it = it->next) {
35 /* grab/ungrab the button */
36 ObMouseBinding *b = it->data;
41 if (i == OB_FRAME_CONTEXT_FRAME) {
42 win = client->frame->window;
44 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
45 } else if (CLIENT_CONTEXT(i, client)) {
46 win = client->frame->plate;
47 mode = GrabModeSync; /* this is handled in event */
48 mask = ButtonPressMask; /* can't catch more than this with Sync
49 mode the release event is
50 manufactured in event() */
54 grab_button_full(b->button, b->state, win, mask, mode, None);
56 ungrab_button(b->button, b->state, win);
60 static void grab_all_clients(gboolean grab)
64 for (it = client_list; it != NULL; it = it->next)
65 mouse_grab_for_client(it->data, grab);
68 static void clearall()
73 for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
74 for (it = bound_contexts[i]; it != NULL; it = it->next) {
77 ObMouseBinding *b = it->data;
78 for (j = 0; j < OB_MOUSE_NUM_ACTIONS; ++j) {
80 for (it = b->actions[j]; it; it = it->next) {
81 action_free(it->data);
83 g_slist_free(b->actions[j]);
87 g_slist_free(bound_contexts[i]);
91 static gboolean fire_button(ObMouseAction a, ObFrameContext context,
92 ObClient *c, guint state,
93 guint button, int x, int y)
98 for (it = bound_contexts[context]; it != NULL; it = it->next) {
100 if (b->state == state && b->button == button)
103 /* if not bound, then nothing to do! */
104 if (it == NULL) return FALSE;
106 for (it = b->actions[a]; it; it = it->next) {
107 ObAction *act = it->data;
108 if (act->func != NULL) {
111 g_assert(act->func != action_moveresize);
113 if (act->func == action_showmenu) {
114 act->data.showmenu.x = x;
115 act->data.showmenu.y = y;
118 if (act->func == action_desktop_dir)
120 act->data.desktopdir.final = FALSE;
121 act->data.desktopdir.cancel = FALSE;
123 if (act->func == action_send_to_desktop_dir)
125 act->data.sendtodir.final = FALSE;
126 act->data.sendtodir.cancel = FALSE;
129 if (config_desktop_popup &&
130 (act->func == action_desktop_dir ||
131 act->func == action_send_to_desktop_dir))
133 keyboard_interactive_grab(state, c, context, act);
136 act->func(&act->data);
142 static gboolean fire_motion(ObMouseAction a, ObFrameContext context,
143 ObClient *c, guint state, guint button,
144 int x_root, int y_root, guint32 corner)
149 for (it = bound_contexts[context]; it != NULL; it = it->next) {
151 if (b->state == state && b->button == button)
154 /* if not bound, then nothing to do! */
155 if (it == NULL) return FALSE;
157 for (it = b->actions[a]; it; it = it->next) {
158 ObAction *act = it->data;
159 if (act->func != NULL) {
162 if (act->func == action_moveresize) {
163 act->data.moveresize.x = x_root;
164 act->data.moveresize.y = y_root;
165 act->data.moveresize.button = button;
166 if (!(act->data.moveresize.corner ==
167 prop_atoms.net_wm_moveresize_move ||
168 act->data.moveresize.corner ==
169 prop_atoms.net_wm_moveresize_move_keyboard ||
170 act->data.moveresize.corner ==
171 prop_atoms.net_wm_moveresize_size_keyboard))
172 act->data.moveresize.corner = corner;
174 g_assert_not_reached();
176 act->func(&act->data);
182 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
184 if (x - cx < cw / 2) {
186 return prop_atoms.net_wm_moveresize_size_topleft;
188 return prop_atoms.net_wm_moveresize_size_bottomleft;
191 return prop_atoms.net_wm_moveresize_size_topright;
193 return prop_atoms.net_wm_moveresize_size_bottomright;
197 void mouse_event(ObClient *client, ObFrameContext context, XEvent *e)
200 static guint button = 0, state = 0, lbutton = 0;
202 static Window lwindow = None;
204 gboolean click = FALSE;
205 gboolean dclick = FALSE;
209 px = e->xbutton.x_root;
210 py = e->xbutton.y_root;
211 button = e->xbutton.button;
212 state = e->xbutton.state;
214 fire_button(OB_MOUSE_ACTION_PRESS, context,
215 client, e->xbutton.state,
217 e->xbutton.x_root, e->xbutton.y_root);
219 if (CLIENT_CONTEXT(context, client)) {
220 /* Replay the event, so it goes to the client*/
221 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
222 /* Fall through to the release case! */
227 if (e->xbutton.button == button) {
228 /* clicks are only valid if its released over the window */
231 guint ujunk, b, w, h;
232 xerror_set_ignore(TRUE);
233 junk1 = XGetGeometry(ob_display, e->xbutton.window,
234 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
235 xerror_set_ignore(FALSE);
237 if (e->xbutton.x >= (signed)-b &&
238 e->xbutton.y >= (signed)-b &&
239 e->xbutton.x < (signed)(w+b) &&
240 e->xbutton.y < (signed)(h+b)) {
242 /* double clicks happen if there were 2 in a row! */
243 if (lbutton == button &&
244 lwindow == e->xbutton.window &&
245 e->xbutton.time - config_mouse_dclicktime <=
251 lwindow = e->xbutton.window;
261 ltime = e->xbutton.time;
263 fire_button(OB_MOUSE_ACTION_RELEASE, context,
264 client, e->xbutton.state,
266 e->xbutton.x_root, e->xbutton.y_root);
268 fire_button(OB_MOUSE_ACTION_CLICK, context,
269 client, e->xbutton.state,
274 fire_button(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
275 client, e->xbutton.state,
283 if (ABS(e->xmotion.x_root - px) >=
284 config_mouse_threshold ||
285 ABS(e->xmotion.y_root - py) >=
286 config_mouse_threshold) {
289 /* You can't drag on buttons */
290 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
291 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
292 context == OB_FRAME_CONTEXT_SHADE ||
293 context == OB_FRAME_CONTEXT_ICONIFY ||
294 context == OB_FRAME_CONTEXT_ICON ||
295 context == OB_FRAME_CONTEXT_CLOSE)
299 corner = prop_atoms.net_wm_moveresize_size_bottomright;
302 pick_corner(e->xmotion.x_root,
304 client->frame->area.x,
305 client->frame->area.y,
306 /* use the client size because the frame
307 can be differently sized (shaded
308 windows) and we want this based on the
311 client->frame->size.left +
312 client->frame->size.right,
313 client->area.height +
314 client->frame->size.top +
315 client->frame->size.bottom);
316 fire_motion(OB_MOUSE_ACTION_MOTION, context,
317 client, state, button, px, py, corner);
325 g_assert_not_reached();
329 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
333 ObFrameContext context;
337 if (!translate_button(buttonstr, &state, &button)) {
338 g_warning("invalid button '%s'", buttonstr);
342 contextstr = g_ascii_strdown(contextstr, -1);
343 context = frame_context_from_string(contextstr);
345 g_warning("invalid context '%s'", contextstr);
351 for (it = bound_contexts[context]; it != NULL; it = it->next){
353 if (b->state == state && b->button == button) {
354 b->actions[mact] = g_slist_append(b->actions[mact], action);
359 grab_all_clients(FALSE);
361 /* add the binding */
362 b = g_new0(ObMouseBinding, 1);
365 b->actions[mact] = g_slist_append(NULL, action);
366 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
368 grab_all_clients(TRUE);
377 void mouse_shutdown()
379 grab_all_clients(FALSE);