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_ROOT : \
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
;
233 xerror_set_ignore(TRUE
);
234 s
= XGetGeometry(ob_display
, e
->xbutton
.window
,
235 &wjunk
, &junk1
, &junk2
, &w
, &h
, &b
, &ujunk
);
236 xerror_set_ignore(FALSE
);
238 if (e
->xbutton
.x
>= (signed)-b
&&
239 e
->xbutton
.y
>= (signed)-b
&&
240 e
->xbutton
.x
< (signed)(w
+b
) &&
241 e
->xbutton
.y
< (signed)(h
+b
)) {
243 /* double clicks happen if there were 2 in a row! */
244 if (lbutton
== button
&&
245 lwindow
== e
->xbutton
.window
&&
246 e
->xbutton
.time
- config_mouse_dclicktime
<=
252 lwindow
= e
->xbutton
.window
;
262 ltime
= e
->xbutton
.time
;
264 fire_button(OB_MOUSE_ACTION_RELEASE
, context
,
265 client
, e
->xbutton
.state
,
267 e
->xbutton
.x_root
, e
->xbutton
.y_root
);
269 fire_button(OB_MOUSE_ACTION_CLICK
, context
,
270 client
, e
->xbutton
.state
,
275 fire_button(OB_MOUSE_ACTION_DOUBLE_CLICK
, context
,
276 client
, e
->xbutton
.state
,
284 if (ABS(e
->xmotion
.x_root
- px
) >=
285 config_mouse_threshold
||
286 ABS(e
->xmotion
.y_root
- py
) >=
287 config_mouse_threshold
) {
290 /* You can't drag on buttons */
291 if (context
== OB_FRAME_CONTEXT_MAXIMIZE
||
292 context
== OB_FRAME_CONTEXT_ALLDESKTOPS
||
293 context
== OB_FRAME_CONTEXT_SHADE
||
294 context
== OB_FRAME_CONTEXT_ICONIFY
||
295 context
== OB_FRAME_CONTEXT_ICON
||
296 context
== OB_FRAME_CONTEXT_CLOSE
)
300 corner
= prop_atoms
.net_wm_moveresize_size_bottomright
;
303 pick_corner(e
->xmotion
.x_root
,
305 client
->frame
->area
.x
,
306 client
->frame
->area
.y
,
307 /* use the client size because the frame
308 can be differently sized (shaded
309 windows) and we want this based on the
312 client
->frame
->size
.left
+
313 client
->frame
->size
.right
,
314 client
->area
.height
+
315 client
->frame
->size
.top
+
316 client
->frame
->size
.bottom
);
317 fire_motion(OB_MOUSE_ACTION_MOTION
, context
,
318 client
, state
, button
, px
, py
, corner
);
326 g_assert_not_reached();
330 gboolean
mouse_bind(char *buttonstr
, char *contextstr
, ObMouseAction mact
,
334 ObFrameContext context
;
338 if (!translate_button(buttonstr
, &state
, &button
)) {
339 g_warning("invalid button '%s'", buttonstr
);
343 contextstr
= g_ascii_strdown(contextstr
, -1);
344 context
= frame_context_from_string(contextstr
);
346 g_warning("invalid context '%s'", contextstr
);
352 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
){
354 if (b
->state
== state
&& b
->button
== button
) {
355 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
360 grab_all_clients(FALSE
);
362 /* add the binding */
363 b
= g_new0(ObMouseBinding
, 1);
366 b
->actions
[mact
] = g_slist_append(NULL
, action
);
367 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
369 grab_all_clients(TRUE
);
378 void mouse_shutdown()
380 grab_all_clients(FALSE
);