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
->data
.any
.interactive
) {
119 act
->data
.inter
.cancel
= FALSE
;
120 act
->data
.inter
.final
= FALSE
;
121 keyboard_interactive_grab(state
, c
, context
, act
);
124 act
->func(&act
->data
);
130 static gboolean
fire_motion(ObMouseAction a
, ObFrameContext context
,
131 ObClient
*c
, guint state
, guint button
,
132 int x_root
, int y_root
, guint32 corner
)
137 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
139 if (b
->state
== state
&& b
->button
== button
)
142 /* if not bound, then nothing to do! */
143 if (it
== NULL
) return FALSE
;
145 for (it
= b
->actions
[a
]; it
; it
= it
->next
) {
146 ObAction
*act
= it
->data
;
147 if (act
->func
!= NULL
) {
150 if (act
->func
== action_moveresize
) {
151 act
->data
.moveresize
.x
= x_root
;
152 act
->data
.moveresize
.y
= y_root
;
153 act
->data
.moveresize
.button
= button
;
154 if (!(act
->data
.moveresize
.corner
==
155 prop_atoms
.net_wm_moveresize_move
||
156 act
->data
.moveresize
.corner
==
157 prop_atoms
.net_wm_moveresize_move_keyboard
||
158 act
->data
.moveresize
.corner
==
159 prop_atoms
.net_wm_moveresize_size_keyboard
))
160 act
->data
.moveresize
.corner
= corner
;
162 g_assert_not_reached();
164 act
->func(&act
->data
);
170 static guint32
pick_corner(int x
, int y
, int cx
, int cy
, int cw
, int ch
)
172 if (x
- cx
< cw
/ 2) {
174 return prop_atoms
.net_wm_moveresize_size_topleft
;
176 return prop_atoms
.net_wm_moveresize_size_bottomleft
;
179 return prop_atoms
.net_wm_moveresize_size_topright
;
181 return prop_atoms
.net_wm_moveresize_size_bottomright
;
185 void mouse_event(ObClient
*client
, ObFrameContext context
, XEvent
*e
)
188 static guint button
= 0, state
= 0, lbutton
= 0;
190 static Window lwindow
= None
;
192 gboolean click
= FALSE
;
193 gboolean dclick
= FALSE
;
197 px
= e
->xbutton
.x_root
;
198 py
= e
->xbutton
.y_root
;
199 button
= e
->xbutton
.button
;
200 state
= e
->xbutton
.state
;
202 fire_button(OB_MOUSE_ACTION_PRESS
, context
,
203 client
, e
->xbutton
.state
,
205 e
->xbutton
.x_root
, e
->xbutton
.y_root
);
207 if (CLIENT_CONTEXT(context
, client
)) {
208 /* Replay the event, so it goes to the client*/
209 XAllowEvents(ob_display
, ReplayPointer
, event_lasttime
);
210 /* Fall through to the release case! */
215 if (e
->xbutton
.button
== button
) {
216 /* clicks are only valid if its released over the window */
219 guint ujunk
, b
, w
, h
;
220 xerror_set_ignore(TRUE
);
221 junk1
= XGetGeometry(ob_display
, e
->xbutton
.window
,
222 &wjunk
, &junk1
, &junk2
, &w
, &h
, &b
, &ujunk
);
223 xerror_set_ignore(FALSE
);
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
;
249 ltime
= e
->xbutton
.time
;
251 fire_button(OB_MOUSE_ACTION_RELEASE
, context
,
252 client
, e
->xbutton
.state
,
254 e
->xbutton
.x_root
, e
->xbutton
.y_root
);
256 fire_button(OB_MOUSE_ACTION_CLICK
, context
,
257 client
, e
->xbutton
.state
,
262 fire_button(OB_MOUSE_ACTION_DOUBLE_CLICK
, context
,
263 client
, e
->xbutton
.state
,
271 if (ABS(e
->xmotion
.x_root
- px
) >=
272 config_mouse_threshold
||
273 ABS(e
->xmotion
.y_root
- py
) >=
274 config_mouse_threshold
) {
277 /* You can't drag on buttons */
278 if (context
== OB_FRAME_CONTEXT_MAXIMIZE
||
279 context
== OB_FRAME_CONTEXT_ALLDESKTOPS
||
280 context
== OB_FRAME_CONTEXT_SHADE
||
281 context
== OB_FRAME_CONTEXT_ICONIFY
||
282 context
== OB_FRAME_CONTEXT_ICON
||
283 context
== OB_FRAME_CONTEXT_CLOSE
)
287 corner
= prop_atoms
.net_wm_moveresize_size_bottomright
;
290 pick_corner(e
->xmotion
.x_root
,
292 client
->frame
->area
.x
,
293 client
->frame
->area
.y
,
294 /* use the client size because the frame
295 can be differently sized (shaded
296 windows) and we want this based on the
299 client
->frame
->size
.left
+
300 client
->frame
->size
.right
,
301 client
->area
.height
+
302 client
->frame
->size
.top
+
303 client
->frame
->size
.bottom
);
304 fire_motion(OB_MOUSE_ACTION_MOTION
, context
,
305 client
, state
, button
, px
, py
, corner
);
313 g_assert_not_reached();
317 gboolean
mouse_bind(char *buttonstr
, char *contextstr
, ObMouseAction mact
,
321 ObFrameContext context
;
325 if (!translate_button(buttonstr
, &state
, &button
)) {
326 g_warning("invalid button '%s'", buttonstr
);
330 contextstr
= g_ascii_strdown(contextstr
, -1);
331 context
= frame_context_from_string(contextstr
);
333 g_warning("invalid context '%s'", contextstr
);
339 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
){
341 if (b
->state
== state
&& b
->button
== button
) {
342 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
347 grab_all_clients(FALSE
);
349 /* when there are no modifiers in the binding, then the action cannot
351 if (!state
&& action
->data
.any
.interactive
) {
352 action
->data
.any
.interactive
= FALSE
;
353 action
->data
.inter
.final
= TRUE
;
356 /* add the binding */
357 b
= g_new0(ObMouseBinding
, 1);
360 b
->actions
[mact
] = g_slist_append(NULL
, action
);
361 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
363 grab_all_clients(TRUE
);
372 void mouse_shutdown()
374 grab_all_clients(FALSE
);