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
, None
);
58 ungrab_button(b
->button
, b
->state
, win
);
62 static void grab_all_clients(gboolean grab
)
66 for (it
= client_list
; it
!= NULL
; it
= it
->next
)
67 mouse_grab_for_client(it
->data
, grab
);
70 static void clearall()
75 for(i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
) {
76 for (it
= bound_contexts
[i
]; it
!= NULL
; it
= it
->next
) {
79 ObMouseBinding
*b
= it
->data
;
80 for (j
= 0; j
< OB_MOUSE_NUM_ACTIONS
; ++j
) {
82 for (it
= b
->actions
[j
]; it
; it
= it
->next
) {
83 action_free(it
->data
);
85 g_slist_free(b
->actions
[j
]);
89 g_slist_free(bound_contexts
[i
]);
90 bound_contexts
[i
] = NULL
;
94 static gboolean
fire_button(ObMouseAction a
, ObFrameContext context
,
95 ObClient
*c
, guint state
,
96 guint button
, int x
, int y
)
101 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
103 if (b
->state
== state
&& b
->button
== button
)
106 /* if not bound, then nothing to do! */
107 if (it
== NULL
) return FALSE
;
109 for (it
= b
->actions
[a
]; it
; it
= it
->next
) {
110 ObAction
*act
= it
->data
;
111 if (act
->func
!= NULL
) {
114 g_assert(act
->func
!= action_moveresize
);
116 if (act
->func
== action_showmenu
) {
117 act
->data
.showmenu
.x
= x
;
118 act
->data
.showmenu
.y
= y
;
121 if (act
->data
.any
.interactive
) {
122 act
->data
.inter
.cancel
= FALSE
;
123 act
->data
.inter
.final
= FALSE
;
124 keyboard_interactive_grab(state
, c
, context
, act
);
127 act
->func(&act
->data
);
133 static gboolean
fire_motion(ObMouseAction a
, ObFrameContext context
,
134 ObClient
*c
, guint state
, guint button
,
135 int x_root
, int y_root
, guint32 corner
)
140 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
142 if (b
->state
== state
&& b
->button
== button
)
145 /* if not bound, then nothing to do! */
146 if (it
== NULL
) return FALSE
;
148 for (it
= b
->actions
[a
]; it
; it
= it
->next
) {
149 ObAction
*act
= it
->data
;
150 if (act
->func
!= NULL
) {
153 if (act
->func
== action_moveresize
) {
154 act
->data
.moveresize
.x
= x_root
;
155 act
->data
.moveresize
.y
= y_root
;
156 act
->data
.moveresize
.button
= button
;
157 if (!(act
->data
.moveresize
.corner
==
158 prop_atoms
.net_wm_moveresize_move
||
159 act
->data
.moveresize
.corner
==
160 prop_atoms
.net_wm_moveresize_move_keyboard
||
161 act
->data
.moveresize
.corner
==
162 prop_atoms
.net_wm_moveresize_size_keyboard
))
163 act
->data
.moveresize
.corner
= corner
;
165 g_assert_not_reached();
167 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(OB_MOUSE_ACTION_PRESS
, context
,
206 client
, e
->xbutton
.state
,
208 e
->xbutton
.x_root
, e
->xbutton
.y_root
);
210 if (CLIENT_CONTEXT(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 xerror_set_ignore(TRUE
);
224 junk1
= XGetGeometry(ob_display
, e
->xbutton
.window
,
225 &wjunk
, &junk1
, &junk2
, &w
, &h
, &b
, &ujunk
);
226 xerror_set_ignore(FALSE
);
228 if (e
->xbutton
.x
>= (signed)-b
&&
229 e
->xbutton
.y
>= (signed)-b
&&
230 e
->xbutton
.x
< (signed)(w
+b
) &&
231 e
->xbutton
.y
< (signed)(h
+b
)) {
233 /* double clicks happen if there were 2 in a row! */
234 if (lbutton
== button
&&
235 lwindow
== e
->xbutton
.window
&&
236 e
->xbutton
.time
- config_mouse_dclicktime
<=
242 lwindow
= e
->xbutton
.window
;
252 ltime
= e
->xbutton
.time
;
254 fire_button(OB_MOUSE_ACTION_RELEASE
, context
,
255 client
, e
->xbutton
.state
,
257 e
->xbutton
.x_root
, e
->xbutton
.y_root
);
259 fire_button(OB_MOUSE_ACTION_CLICK
, context
,
260 client
, e
->xbutton
.state
,
265 fire_button(OB_MOUSE_ACTION_DOUBLE_CLICK
, context
,
266 client
, e
->xbutton
.state
,
274 if (ABS(e
->xmotion
.x_root
- px
) >=
275 config_mouse_threshold
||
276 ABS(e
->xmotion
.y_root
- py
) >=
277 config_mouse_threshold
) {
280 /* You can't drag on buttons */
281 if (context
== OB_FRAME_CONTEXT_MAXIMIZE
||
282 context
== OB_FRAME_CONTEXT_ALLDESKTOPS
||
283 context
== OB_FRAME_CONTEXT_SHADE
||
284 context
== OB_FRAME_CONTEXT_ICONIFY
||
285 context
== OB_FRAME_CONTEXT_ICON
||
286 context
== OB_FRAME_CONTEXT_CLOSE
)
290 corner
= prop_atoms
.net_wm_moveresize_size_bottomright
;
293 pick_corner(e
->xmotion
.x_root
,
295 client
->frame
->area
.x
,
296 client
->frame
->area
.y
,
297 /* use the client size because the frame
298 can be differently sized (shaded
299 windows) and we want this based on the
302 client
->frame
->size
.left
+
303 client
->frame
->size
.right
,
304 client
->area
.height
+
305 client
->frame
->size
.top
+
306 client
->frame
->size
.bottom
);
307 fire_motion(OB_MOUSE_ACTION_MOTION
, context
,
308 client
, state
, button
, px
, py
, corner
);
316 g_assert_not_reached();
320 gboolean
mouse_bind(char *buttonstr
, char *contextstr
, ObMouseAction mact
,
324 ObFrameContext context
;
328 if (!translate_button(buttonstr
, &state
, &button
)) {
329 g_warning("invalid button '%s'", buttonstr
);
333 contextstr
= g_ascii_strdown(contextstr
, -1);
334 context
= frame_context_from_string(contextstr
);
336 g_warning("invalid context '%s'", contextstr
);
342 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
){
344 if (b
->state
== state
&& b
->button
== button
) {
345 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
350 grab_all_clients(FALSE
);
352 /* when there are no modifiers in the binding, then the action cannot
354 if (!state
&& action
->data
.any
.interactive
) {
355 action
->data
.any
.interactive
= FALSE
;
356 action
->data
.inter
.final
= TRUE
;
359 /* add the binding */
360 b
= g_new0(ObMouseBinding
, 1);
363 b
->actions
[mact
] = g_slist_append(NULL
, action
);
364 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
366 grab_all_clients(TRUE
);
371 void mouse_startup(gboolean reconfig
)
375 void mouse_shutdown(gboolean reconfig
)
377 grab_all_clients(FALSE
);