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,
59 ungrab_button(b->button, b->state, win);
63 static void grab_all_clients(gboolean grab)
67 for (it = client_list; it != NULL; it = it->next)
68 mouse_grab_for_client(it->data, grab);
71 static void clearall()
76 for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
77 for (it = bound_contexts[i]; it != NULL; it = it->next) {
80 ObMouseBinding *b = it->data;
81 for (j = 0; j < OB_MOUSE_NUM_ACTIONS; ++j) {
83 for (it = b->actions[j]; it; it = it->next) {
84 action_free(it->data);
86 g_slist_free(b->actions[j]);
90 g_slist_free(bound_contexts[i]);
91 bound_contexts[i] = NULL;
95 static gboolean fire_button(ObMouseAction a, ObFrameContext context,
96 ObClient *c, guint state,
97 guint button, int x, int y)
102 for (it = bound_contexts[context]; it != NULL; it = it->next) {
104 if (b->state == state && b->button == button)
107 /* if not bound, then nothing to do! */
108 if (it == NULL) return FALSE;
110 for (it = b->actions[a]; it; it = it->next) {
111 ObAction *act = it->data;
112 if (act->func != NULL) {
115 g_assert(act->func != action_moveresize);
117 if (act->func == action_showmenu) {
118 act->data.showmenu.x = x;
119 act->data.showmenu.y = y;
122 if (act->data.any.interactive) {
123 act->data.inter.cancel = FALSE;
124 act->data.inter.final = FALSE;
125 keyboard_interactive_grab(state, c, context, act);
128 act->func(&act->data);
134 static gboolean fire_motion(ObMouseAction a, ObFrameContext context,
135 ObClient *c, guint state, guint button,
136 int x_root, int y_root, guint32 corner)
141 for (it = bound_contexts[context]; it != NULL; it = it->next) {
143 if (b->state == state && b->button == button)
146 /* if not bound, then nothing to do! */
147 if (it == NULL) return FALSE;
149 for (it = b->actions[a]; it; it = it->next) {
150 ObAction *act = it->data;
151 if (act->func != NULL) {
154 if (act->func == action_moveresize) {
155 act->data.moveresize.x = x_root;
156 act->data.moveresize.y = y_root;
157 act->data.moveresize.button = button;
158 if (!(act->data.moveresize.corner ==
159 prop_atoms.net_wm_moveresize_move ||
160 act->data.moveresize.corner ==
161 prop_atoms.net_wm_moveresize_move_keyboard ||
162 act->data.moveresize.corner ==
163 prop_atoms.net_wm_moveresize_size_keyboard))
164 act->data.moveresize.corner = corner;
166 g_assert_not_reached();
168 act->func(&act->data);
174 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
176 if (x - cx < cw / 2) {
178 return prop_atoms.net_wm_moveresize_size_topleft;
180 return prop_atoms.net_wm_moveresize_size_bottomleft;
183 return prop_atoms.net_wm_moveresize_size_topright;
185 return prop_atoms.net_wm_moveresize_size_bottomright;
189 void mouse_event(ObClient *client, ObFrameContext context, XEvent *e)
192 static guint button = 0, state = 0, lbutton = 0;
194 static Window lwindow = None;
196 gboolean click = FALSE;
197 gboolean dclick = FALSE;
201 px = e->xbutton.x_root;
202 py = e->xbutton.y_root;
203 button = e->xbutton.button;
204 state = e->xbutton.state;
206 fire_button(OB_MOUSE_ACTION_PRESS, context,
207 client, e->xbutton.state,
209 e->xbutton.x_root, e->xbutton.y_root);
211 if (CLIENT_CONTEXT(context, client)) {
212 /* Replay the event, so it goes to the client*/
213 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
214 /* Fall through to the release case! */
219 if (e->xbutton.button == button) {
220 /* clicks are only valid if its released over the window */
223 guint ujunk, b, w, h;
224 xerror_set_ignore(TRUE);
225 junk1 = XGetGeometry(ob_display, e->xbutton.window,
226 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
227 xerror_set_ignore(FALSE);
229 if (e->xbutton.x >= (signed)-b &&
230 e->xbutton.y >= (signed)-b &&
231 e->xbutton.x < (signed)(w+b) &&
232 e->xbutton.y < (signed)(h+b)) {
234 /* double clicks happen if there were 2 in a row! */
235 if (lbutton == button &&
236 lwindow == e->xbutton.window &&
237 e->xbutton.time - config_mouse_dclicktime <=
243 lwindow = e->xbutton.window;
253 ltime = e->xbutton.time;
255 fire_button(OB_MOUSE_ACTION_RELEASE, context,
256 client, e->xbutton.state,
258 e->xbutton.x_root, e->xbutton.y_root);
260 fire_button(OB_MOUSE_ACTION_CLICK, context,
261 client, e->xbutton.state,
266 fire_button(OB_MOUSE_ACTION_DOUBLE_CLICK, context,
267 client, e->xbutton.state,
275 if (ABS(e->xmotion.x_root - px) >=
276 config_mouse_threshold ||
277 ABS(e->xmotion.y_root - py) >=
278 config_mouse_threshold) {
281 /* You can't drag on buttons */
282 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
283 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
284 context == OB_FRAME_CONTEXT_SHADE ||
285 context == OB_FRAME_CONTEXT_ICONIFY ||
286 context == OB_FRAME_CONTEXT_ICON ||
287 context == OB_FRAME_CONTEXT_CLOSE)
291 corner = prop_atoms.net_wm_moveresize_size_bottomright;
294 pick_corner(e->xmotion.x_root,
296 client->frame->area.x,
297 client->frame->area.y,
298 /* use the client size because the frame
299 can be differently sized (shaded
300 windows) and we want this based on the
303 client->frame->size.left +
304 client->frame->size.right,
305 client->area.height +
306 client->frame->size.top +
307 client->frame->size.bottom);
308 fire_motion(OB_MOUSE_ACTION_MOTION, context,
309 client, state, button, px, py, corner);
317 g_assert_not_reached();
321 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
325 ObFrameContext context;
329 if (!translate_button(buttonstr, &state, &button)) {
330 g_warning("invalid button '%s'", buttonstr);
334 contextstr = g_ascii_strdown(contextstr, -1);
335 context = frame_context_from_string(contextstr);
337 g_warning("invalid context '%s'", contextstr);
343 for (it = bound_contexts[context]; it != NULL; it = it->next){
345 if (b->state == state && b->button == button) {
346 b->actions[mact] = g_slist_append(b->actions[mact], action);
351 grab_all_clients(FALSE);
353 /* when there are no modifiers in the binding, then the action cannot
355 if (!state && action->data.any.interactive) {
356 action->data.any.interactive = FALSE;
357 action->data.inter.final = TRUE;
360 /* add the binding */
361 b = g_new0(ObMouseBinding, 1);
364 b->actions[mact] = g_slist_append(NULL, action);
365 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
367 grab_all_clients(TRUE);
372 void mouse_startup(gboolean reconfig)
376 void mouse_shutdown(gboolean reconfig)
378 grab_all_clients(FALSE);