10 #include "translate.h"
17 GSList
*actions
[OB_NUM_MOUSE_ACTIONS
]; /* lists of Action pointers */
20 #define FRAME_CONTEXT(co, cl) ((cl && cl->type != OB_CLIENT_TYPE_DESKTOP) ? \
21 co == OB_FRAME_CONTEXT_FRAME : FALSE)
22 #define CLIENT_CONTEXT(co, cl) ((cl && cl->type == OB_CLIENT_TYPE_DESKTOP) ? \
23 co == OB_FRAME_CONTEXT_DESKTOP : \
24 co == OB_FRAME_CONTEXT_CLIENT)
26 /* Array of GSList*s of ObMouseBinding*s. */
27 static GSList
*bound_contexts
[OB_FRAME_NUM_CONTEXTS
];
29 ObFrameContext
mouse_button_frame_context(ObFrameContext context
,
33 ObFrameContext x
= context
;
35 for (it
= bound_contexts
[context
]; it
; it
= g_slist_next(it
)) {
36 ObMouseBinding
*b
= it
->data
;
38 if (b
->button
== button
)
43 case OB_FRAME_CONTEXT_NONE
:
44 case OB_FRAME_CONTEXT_DESKTOP
:
45 case OB_FRAME_CONTEXT_CLIENT
:
46 case OB_FRAME_CONTEXT_TITLEBAR
:
47 case OB_FRAME_CONTEXT_HANDLE
:
48 case OB_FRAME_CONTEXT_FRAME
:
50 case OB_FRAME_CONTEXT_BLCORNER
:
51 case OB_FRAME_CONTEXT_BRCORNER
:
52 x
= OB_FRAME_CONTEXT_HANDLE
;
54 case OB_FRAME_CONTEXT_TLCORNER
:
55 case OB_FRAME_CONTEXT_TRCORNER
:
56 case OB_FRAME_CONTEXT_MAXIMIZE
:
57 case OB_FRAME_CONTEXT_ALLDESKTOPS
:
58 case OB_FRAME_CONTEXT_SHADE
:
59 case OB_FRAME_CONTEXT_ICONIFY
:
60 case OB_FRAME_CONTEXT_ICON
:
61 case OB_FRAME_CONTEXT_CLOSE
:
62 x
= OB_FRAME_CONTEXT_TITLEBAR
;
64 case OB_FRAME_NUM_CONTEXTS
:
65 g_assert_not_reached();
71 void mouse_grab_for_client(ObClient
*client
, gboolean grab
)
76 for (i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
)
77 for (it
= bound_contexts
[i
]; it
!= NULL
; it
= g_slist_next(it
)) {
78 /* grab/ungrab the button */
79 ObMouseBinding
*b
= it
->data
;
84 if (FRAME_CONTEXT(i
, client
)) {
85 win
= client
->frame
->window
;
87 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
88 } else if (CLIENT_CONTEXT(i
, client
)) {
89 win
= client
->frame
->plate
;
90 mode
= GrabModeSync
; /* this is handled in event */
91 mask
= ButtonPressMask
; /* can't catch more than this with Sync
92 mode the release event is
93 manufactured in event() */
97 grab_button_full(b
->button
, b
->state
, win
, mask
, mode
,
100 ungrab_button(b
->button
, b
->state
, win
);
104 static void grab_all_clients(gboolean grab
)
108 for (it
= client_list
; it
!= NULL
; it
= it
->next
)
109 mouse_grab_for_client(it
->data
, grab
);
112 static void clearall()
117 for(i
= 0; i
< OB_FRAME_NUM_CONTEXTS
; ++i
) {
118 for (it
= bound_contexts
[i
]; it
!= NULL
; it
= it
->next
) {
119 ObMouseBinding
*b
= it
->data
;
122 for (j
= 0; j
< OB_NUM_MOUSE_ACTIONS
; ++j
) {
125 for (it
= b
->actions
[j
]; it
; it
= it
->next
)
126 action_free(it
->data
);
127 g_slist_free(b
->actions
[j
]);
131 g_slist_free(bound_contexts
[i
]);
132 bound_contexts
[i
] = NULL
;
136 static gboolean
fire_binding(ObMouseAction a
, ObFrameContext context
,
137 ObClient
*c
, guint state
,
138 guint button
, int x
, int y
)
143 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
145 if (b
->state
== state
&& b
->button
== button
)
148 /* if not bound, then nothing to do! */
149 if (it
== NULL
) return FALSE
;
151 for (it
= b
->actions
[a
]; it
; it
= it
->next
)
152 action_run_mouse(it
->data
, c
, state
, button
, x
, y
);
156 void mouse_event(ObClient
*client
, XEvent
*e
)
159 static guint button
= 0, state
= 0, lbutton
= 0;
160 static Window lwindow
= None
;
163 ObFrameContext context
;
164 gboolean click
= FALSE
;
165 gboolean dclick
= FALSE
;
169 context
= frame_context(client
, e
->xany
.window
);
170 context
= mouse_button_frame_context(context
, e
->xbutton
.button
);
172 px
= e
->xbutton
.x_root
;
173 py
= e
->xbutton
.y_root
;
174 button
= e
->xbutton
.button
;
175 state
= e
->xbutton
.state
;
177 fire_binding(OB_MOUSE_ACTION_PRESS
, context
,
178 client
, e
->xbutton
.state
,
180 e
->xbutton
.x_root
, e
->xbutton
.y_root
);
182 if (CLIENT_CONTEXT(context
, client
)) {
183 /* Replay the event, so it goes to the client*/
184 XAllowEvents(ob_display
, ReplayPointer
, event_lasttime
);
185 /* Fall through to the release case! */
190 context
= frame_context(client
, e
->xany
.window
);
191 context
= mouse_button_frame_context(context
, e
->xbutton
.button
);
193 if (e
->xbutton
.button
== button
) {
194 /* clicks are only valid if its released over the window */
197 guint ujunk
, b
, w
, h
;
198 xerror_set_ignore(TRUE
);
199 junk1
= XGetGeometry(ob_display
, e
->xbutton
.window
,
200 &wjunk
, &junk1
, &junk2
, &w
, &h
, &b
, &ujunk
);
201 xerror_set_ignore(FALSE
);
203 if (e
->xbutton
.x
>= (signed)-b
&&
204 e
->xbutton
.y
>= (signed)-b
&&
205 e
->xbutton
.x
< (signed)(w
+b
) &&
206 e
->xbutton
.y
< (signed)(h
+b
)) {
208 /* double clicks happen if there were 2 in a row! */
209 if (lbutton
== button
&&
210 lwindow
== e
->xbutton
.window
&&
211 e
->xbutton
.time
- config_mouse_dclicktime
<=
217 lwindow
= e
->xbutton
.window
;
227 ltime
= e
->xbutton
.time
;
229 fire_binding(OB_MOUSE_ACTION_RELEASE
, context
,
230 client
, e
->xbutton
.state
,
232 e
->xbutton
.x_root
, e
->xbutton
.y_root
);
234 fire_binding(OB_MOUSE_ACTION_CLICK
, context
,
235 client
, e
->xbutton
.state
,
240 fire_binding(OB_MOUSE_ACTION_DOUBLE_CLICK
, context
,
241 client
, e
->xbutton
.state
,
249 context
= frame_context(client
, e
->xany
.window
);
250 context
= mouse_button_frame_context(context
, button
);
252 if (ABS(e
->xmotion
.x_root
- px
) >=
253 config_mouse_threshold
||
254 ABS(e
->xmotion
.y_root
- py
) >=
255 config_mouse_threshold
) {
257 /* You can't drag on buttons */
258 if (context
== OB_FRAME_CONTEXT_MAXIMIZE
||
259 context
== OB_FRAME_CONTEXT_ALLDESKTOPS
||
260 context
== OB_FRAME_CONTEXT_SHADE
||
261 context
== OB_FRAME_CONTEXT_ICONIFY
||
262 context
== OB_FRAME_CONTEXT_ICON
||
263 context
== OB_FRAME_CONTEXT_CLOSE
)
266 fire_binding(OB_MOUSE_ACTION_MOTION
, context
,
267 client
, state
, button
, px
, py
);
275 g_assert_not_reached();
279 gboolean
mouse_bind(char *buttonstr
, char *contextstr
, ObMouseAction mact
,
283 ObFrameContext context
;
287 if (!translate_button(buttonstr
, &state
, &button
)) {
288 g_warning("invalid button '%s'", buttonstr
);
292 contextstr
= g_ascii_strdown(contextstr
, -1);
293 context
= frame_context_from_string(contextstr
);
295 g_warning("invalid context '%s'", contextstr
);
301 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
){
303 if (b
->state
== state
&& b
->button
== button
) {
304 b
->actions
[mact
] = g_slist_append(b
->actions
[mact
], action
);
309 /* when there are no modifiers in the binding, then the action cannot
311 if (!state
&& action
->data
.any
.interactive
) {
312 action
->data
.any
.interactive
= FALSE
;
313 action
->data
.inter
.final
= TRUE
;
316 /* add the binding */
317 b
= g_new0(ObMouseBinding
, 1);
320 b
->actions
[mact
] = g_slist_append(NULL
, action
);
321 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
326 void mouse_startup(gboolean reconfig
)
328 grab_all_clients(TRUE
);
331 void mouse_shutdown(gboolean reconfig
)
333 grab_all_clients(FALSE
);