17 GSList *actions[NUM_MOUSEACTION]; /* lists of Action pointers */
20 /* Array of GSList*s of PointerBinding*s. */
21 static GSList *bound_contexts[OB_FRAME_NUM_CONTEXTS];
23 void mouse_grab_for_client(ObClient *client, gboolean grab)
28 for (i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i)
29 for (it = bound_contexts[i]; it != NULL; it = it->next) {
30 /* grab/ungrab the button */
31 ObMouseBinding *b = it->data;
36 if (i == OB_FRAME_CONTEXT_FRAME) {
37 win = client->frame->window;
39 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
40 } else if (i == OB_FRAME_CONTEXT_CLIENT) {
41 win = client->frame->plate;
42 mode = GrabModeSync; /* this is handled in event */
43 mask = ButtonPressMask; /* can't catch more than this with Sync
44 mode the release event is
45 manufactured in event() */
49 grab_button_full(b->button, b->state, win, mask, mode, None);
51 ungrab_button(b->button, b->state, win);
55 static void grab_all_clients(gboolean grab)
59 for (it = client_list; it != NULL; it = it->next)
60 mouse_grab_for_client(it->data, grab);
63 static void clearall()
68 for(i = 0; i < OB_FRAME_NUM_CONTEXTS; ++i) {
69 for (it = bound_contexts[i]; it != NULL; it = it->next) {
72 ObMouseBinding *b = it->data;
73 for (j = 0; j < NUM_MOUSEACTION; ++j) {
75 for (it = b->actions[j]; it; it = it->next) {
76 action_free(it->data);
78 g_slist_free(b->actions[j]);
82 g_slist_free(bound_contexts[i]);
86 static void fire_button(ObMouseAction a, ObFrameContext context,
87 ObClient *c, guint state,
88 guint button, int x, int y)
93 for (it = bound_contexts[context]; it != NULL; it = it->next) {
95 if (b->state == state && b->button == button)
98 /* if not bound, then nothing to do! */
99 if (it == NULL) return;
101 for (it = b->actions[a]; it; it = it->next) {
102 ObAction *act = it->data;
103 if (act->func != NULL) {
106 g_assert(act->func != action_moveresize);
108 if (act->func == action_showmenu) {
109 act->data.showmenu.x = x;
110 act->data.showmenu.y = y;
113 if (act->func == action_desktop_dir)
115 act->data.desktopdir.final = FALSE;
116 act->data.desktopdir.cancel = FALSE;
118 if (act->func == action_send_to_desktop_dir)
120 act->data.sendtodir.final = FALSE;
121 act->data.sendtodir.cancel = FALSE;
124 if ((act->func == action_desktop_dir ||
125 act->func == action_send_to_desktop_dir)) {
126 keyboard_interactive_grab(state, c, context, act);
129 act->func(&act->data);
134 static void fire_motion(ObMouseAction a, ObFrameContext context, ObClient *c,
135 guint state, guint button, int x_root, int y_root,
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;
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);
173 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
175 if (x - cx < cw / 2) {
177 return prop_atoms.net_wm_moveresize_size_topleft;
179 return prop_atoms.net_wm_moveresize_size_bottomleft;
182 return prop_atoms.net_wm_moveresize_size_topright;
184 return prop_atoms.net_wm_moveresize_size_bottomright;
188 void mouse_event(ObClient *client, ObFrameContext context, XEvent *e)
191 static guint button = 0, state = 0, lbutton = 0;
193 static Window lwindow = None;
195 gboolean click = FALSE;
196 gboolean dclick = FALSE;
200 px = e->xbutton.x_root;
201 py = e->xbutton.y_root;
202 button = e->xbutton.button;
203 state = e->xbutton.state;
205 fire_button(MouseAction_Press, context,
206 client, e->xbutton.state,
208 e->xbutton.x_root, e->xbutton.y_root);
210 if (context == OB_FRAME_CONTEXT_CLIENT) {
211 /* Replay the event, so it goes to the client*/
212 XAllowEvents(ob_display, ReplayPointer, event_lasttime);
213 /* Fall through to the release case! */
218 if (e->xbutton.button == button) {
219 /* clicks are only valid if its released over the window */
222 guint ujunk, b, w, h;
223 XGetGeometry(ob_display, e->xbutton.window,
224 &wjunk, &junk1, &junk2, &w, &h, &b, &ujunk);
225 if (e->xbutton.x >= (signed)-b &&
226 e->xbutton.y >= (signed)-b &&
227 e->xbutton.x < (signed)(w+b) &&
228 e->xbutton.y < (signed)(h+b)) {
230 /* double clicks happen if there were 2 in a row! */
231 if (lbutton == button &&
232 lwindow == e->xbutton.window &&
233 e->xbutton.time - config_mouse_dclicktime <=
239 lwindow = e->xbutton.window;
248 ltime = e->xbutton.time;
250 fire_button(MouseAction_Release, context,
251 client, e->xbutton.state,
253 e->xbutton.x_root, e->xbutton.y_root);
255 fire_button(MouseAction_Click, context,
256 client, e->xbutton.state,
261 fire_button(MouseAction_DClick, context,
262 client, e->xbutton.state,
270 if (ABS(e->xmotion.x_root - px) >=
271 config_mouse_threshold ||
272 ABS(e->xmotion.y_root - py) >=
273 config_mouse_threshold) {
276 /* You can't drag on buttons */
277 if (context == OB_FRAME_CONTEXT_MAXIMIZE ||
278 context == OB_FRAME_CONTEXT_ALLDESKTOPS ||
279 context == OB_FRAME_CONTEXT_SHADE ||
280 context == OB_FRAME_CONTEXT_ICONIFY ||
281 context == OB_FRAME_CONTEXT_ICON ||
282 context == OB_FRAME_CONTEXT_CLOSE)
286 corner = prop_atoms.net_wm_moveresize_size_bottomright;
289 pick_corner(e->xmotion.x_root,
291 client->frame->area.x,
292 client->frame->area.y,
293 /* use the client size because the frame
294 can be differently sized (shaded
295 windows) and we want this based on the
298 client->frame->size.left +
299 client->frame->size.right,
300 client->area.height +
301 client->frame->size.top +
302 client->frame->size.bottom);
303 fire_motion(MouseAction_Motion, context,
304 client, state, button, px, py, corner);
312 g_assert_not_reached();
316 gboolean mouse_bind(char *buttonstr, char *contextstr, ObMouseAction mact,
320 ObFrameContext context;
324 if (!translate_button(buttonstr, &state, &button)) {
325 g_warning("invalid button '%s'", buttonstr);
329 contextstr = g_ascii_strdown(contextstr, -1);
330 context = frame_context_from_string(contextstr);
332 g_warning("invalid context '%s'", contextstr);
338 for (it = bound_contexts[context]; it != NULL; it = it->next){
340 if (b->state == state && b->button == button) {
341 b->actions[mact] = g_slist_append(b->actions[mact], action);
346 grab_all_clients(FALSE);
348 /* add the binding */
349 b = g_new0(ObMouseBinding, 1);
352 b->actions[mact] = g_slist_append(NULL, action);
353 bound_contexts[context] = g_slist_append(bound_contexts[context], b);
355 grab_all_clients(TRUE);
364 void mouse_shutdown()
366 grab_all_clients(FALSE);