1 #include "kernel/openbox.h"
2 #include "kernel/dispatch.h"
3 #include "kernel/action.h"
4 #include "kernel/event.h"
5 #include "kernel/client.h"
6 #include "kernel/frame.h"
7 #include "kernel/grab.h"
8 #include "kernel/engine.h"
9 #include "kernel/config.h"
10 #include "kernel/parse.h"
11 #include "kernel/frame.h"
12 #include "translate.h"
14 #include "mouseparse.h"
17 void plugin_setup_config()
19 config_def_set(config_def_new("mouse.dragThreshold", Config_Integer
,
21 "The drag threshold in pixels before a Drag "
23 config_def_set(config_def_new("mouse.doubleClickTime", Config_Integer
,
24 "Double Click Interval",
25 "The amount of time (in milliseconds) in "
26 "which two clicks must occur to cause a "
27 "DoubleClick event."));
29 parse_reg_section("mouse", mouseparse
);
32 /* Array of GSList*s of PointerBinding*s. */
33 static GSList
*bound_contexts
[NUM_CONTEXTS
];
35 static void grab_for_client(Client
*client
, gboolean grab
)
40 for (i
= 0; i
< NUM_CONTEXTS
; ++i
)
41 for (it
= bound_contexts
[i
]; it
!= NULL
; it
= it
->next
) {
42 /* grab/ungrab the button */
43 MouseBinding
*b
= it
->data
;
48 if (i
== Context_Frame
) {
49 win
= client
->frame
->window
;
51 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
52 } else if (i
== Context_Client
) {
53 win
= client
->frame
->plate
;
54 mode
= GrabModeSync
; /* this is handled in event */
55 mask
= ButtonPressMask
; /* can't catch more than this with Sync
56 mode the release event is
57 manufactured in event() */
61 grab_button(b
->button
, b
->state
, win
, mask
, mode
);
63 ungrab_button(b
->button
, b
->state
, win
);
67 static void grab_all_clients(gboolean grab
)
71 for (it
= client_list
; it
!= NULL
; it
= it
->next
)
72 grab_for_client(it
->data
, grab
);
75 static void clearall()
80 for(i
= 0; i
< NUM_CONTEXTS
; ++i
) {
81 for (it
= bound_contexts
[i
]; it
!= NULL
; it
= it
->next
) {
84 MouseBinding
*b
= it
->data
;
85 for (j
= 0; j
< NUM_MOUSEACTION
; ++j
)
86 if (b
->action
[j
] != NULL
)
87 action_free(b
->action
[j
]);
90 g_slist_free(bound_contexts
[i
]);
94 static void fire_button(MouseAction a
, Context context
, Client
*c
, guint state
,
100 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
102 if (b
->state
== state
&& b
->button
== button
)
105 /* if not bound, then nothing to do! */
106 if (it
== NULL
) return;
108 if (b
->action
[a
] != NULL
&& b
->action
[a
]->func
!= NULL
) {
109 b
->action
[a
]->data
.any
.c
= c
;
111 g_assert(!(b
->action
[a
]->func
== action_move
||
112 b
->action
[a
]->func
== action_resize
));
114 b
->action
[a
]->func(&b
->action
[a
]->data
);
118 /* corner should be the opposite corner of the window in which the pointer
119 clicked, Corner_TopLeft if a good default if there is no client
120 Returns True or False for if a binding existed for the action or not.
122 static gboolean
fire_motion(MouseAction a
, Context context
, Client
*c
,
123 guint state
, guint button
, int cx
, int cy
,
124 int cw
, int ch
, int dx
, int dy
,
125 gboolean final
, Corner corner
)
130 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
132 if (b
->state
== state
&& b
->button
== button
)
135 /* if not bound, then nothing to do! */
136 if (it
== NULL
) return FALSE
;
138 if (b
->action
[a
] != NULL
&& b
->action
[a
]->func
!= NULL
) {
139 b
->action
[a
]->data
.any
.c
= c
;
141 if (b
->action
[a
]->func
== action_move
) {
142 b
->action
[a
]->data
.move
.x
= cx
+ dx
;
143 b
->action
[a
]->data
.move
.y
= cy
+ dy
;
144 b
->action
[a
]->data
.move
.final
= final
;
145 } else if (b
->action
[a
]->func
== action_resize
) {
146 b
->action
[a
]->data
.resize
.corner
= corner
;
149 b
->action
[a
]->data
.resize
.x
= cw
+ dx
;
150 b
->action
[a
]->data
.resize
.y
= ch
+ dy
;
152 case Corner_TopRight
:
153 b
->action
[a
]->data
.resize
.x
= cw
- dx
;
154 b
->action
[a
]->data
.resize
.y
= ch
+ dy
;
156 case Corner_BottomLeft
:
157 b
->action
[a
]->data
.resize
.x
= cw
+ dx
;
158 b
->action
[a
]->data
.resize
.y
= ch
- dy
;
160 case Corner_BottomRight
:
161 b
->action
[a
]->data
.resize
.x
= cw
- dx
;
162 b
->action
[a
]->data
.resize
.y
= ch
- dy
;
165 b
->action
[a
]->data
.resize
.final
= final
;
167 b
->action
[a
]->func(&b
->action
[a
]->data
);
173 static Corner
pick_corner(int x
, int y
, int cx
, int cy
, int cw
, int ch
)
175 if (x
- cx
< cw
/ 2) {
177 return Corner_BottomRight
;
179 return Corner_TopRight
;
182 return Corner_BottomLeft
;
184 return Corner_TopLeft
;
188 static void event(ObEvent
*e
, void *foo
)
191 static int px
, py
, cx
, cy
, cw
, ch
, dx
, dy
;
192 static guint button
= 0, lbutton
= 0;
193 static gboolean drag
= FALSE
, drag_used
= FALSE
;
194 static Corner corner
= Corner_TopLeft
;
195 ConfigValue doubleclicktime
;
196 ConfigValue dragthreshold
;
197 gboolean click
= FALSE
;
198 gboolean dclick
= FALSE
;
201 if (!config_get("mouse.dragThreshold", Config_Integer
, &dragthreshold
))
202 dragthreshold
.integer
= 3; /* default */
203 if (!config_get("mouse.doubleClickTime", Config_Integer
, &doubleclicktime
))
204 doubleclicktime
.integer
= 200; /* default */
207 case Event_Client_Mapped
:
208 grab_for_client(e
->data
.c
.client
, TRUE
);
211 case Event_Client_Destroy
:
212 grab_for_client(e
->data
.c
.client
, FALSE
);
215 case Event_X_ButtonPress
:
217 if (e
->data
.x
.client
!= NULL
) {
218 cx
= e
->data
.x
.client
->frame
->area
.x
;
219 cy
= e
->data
.x
.client
->frame
->area
.y
;
220 /* use the client size because the frame can be differently
221 sized (shaded windows) and we want this based on the clients
223 cw
= e
->data
.x
.client
->area
.width
+
224 e
->data
.x
.client
->frame
->size
.left
+
225 e
->data
.x
.client
->frame
->size
.right
;
226 ch
= e
->data
.x
.client
->area
.height
+
227 e
->data
.x
.client
->frame
->size
.top
+
228 e
->data
.x
.client
->frame
->size
.bottom
;
229 px
= e
->data
.x
.e
->xbutton
.x_root
;
230 py
= e
->data
.x
.e
->xbutton
.y_root
;
231 corner
= pick_corner(px
, py
, cx
, cy
, cw
, ch
);
233 button
= e
->data
.x
.e
->xbutton
.button
;
235 context
= engine_get_context(e
->data
.x
.client
,
236 e
->data
.x
.e
->xbutton
.window
);
238 fire_button(MouseAction_Press
, context
,
239 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
240 e
->data
.x
.e
->xbutton
.button
);
242 if (context
== Context_Client
) {
243 /* Replay the event, so it goes to the client*/
244 XAllowEvents(ob_display
, ReplayPointer
, event_lasttime
);
245 /* Fall through to the release case! */
249 case Event_X_ButtonRelease
:
250 context
= engine_get_context(e
->data
.x
.client
,
251 e
->data
.x
.e
->xbutton
.window
);
252 if (e
->data
.x
.e
->xbutton
.button
== button
) {
255 fire_motion(MouseAction_Motion
, context
,
256 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
257 e
->data
.x
.e
->xbutton
.button
,
258 cx
, cy
, cw
, ch
, dx
, dy
, TRUE
, corner
);
259 drag
= drag_used
= FALSE
;
263 /* clicks are only valid if its released over the window */
266 guint ujunk
, b
, w
, h
;
267 XGetGeometry(ob_display
, e
->data
.x
.e
->xbutton
.window
,
268 &wjunk
, &junk
, &junk
, &w
, &h
, &b
, &ujunk
);
269 if (e
->data
.x
.e
->xbutton
.x
>= (signed)-b
&&
270 e
->data
.x
.e
->xbutton
.y
>= (signed)-b
&&
271 e
->data
.x
.e
->xbutton
.x
< (signed)(w
+b
) &&
272 e
->data
.x
.e
->xbutton
.y
< (signed)(h
+b
)) {
274 /* double clicks happen if there were 2 in a row! */
275 if (lbutton
== button
&&
276 e
->data
.x
.e
->xbutton
.time
- doubleclicktime
.integer
<=
287 ltime
= e
->data
.x
.e
->xbutton
.time
;
289 fire_button(MouseAction_Release
, context
,
290 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
291 e
->data
.x
.e
->xbutton
.button
);
293 fire_button(MouseAction_Click
, context
,
294 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
295 e
->data
.x
.e
->xbutton
.button
);
297 fire_button(MouseAction_DClick
, context
,
298 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
299 e
->data
.x
.e
->xbutton
.button
);
302 case Event_X_MotionNotify
:
304 dx
= e
->data
.x
.e
->xmotion
.x_root
- px
;
305 dy
= e
->data
.x
.e
->xmotion
.y_root
- py
;
307 (ABS(dx
) >= dragthreshold
.integer
||
308 ABS(dy
) >= dragthreshold
.integer
))
311 context
= engine_get_context(e
->data
.x
.client
,
312 e
->data
.x
.e
->xbutton
.window
);
313 drag_used
= fire_motion(MouseAction_Motion
, context
,
315 e
->data
.x
.e
->xmotion
.state
,
316 button
, cx
, cy
, cw
, ch
, dx
, dy
,
323 g_assert_not_reached();
327 gboolean
mbind(char *buttonstr
, char *contextstr
, MouseAction mact
,
335 if (!translate_button(buttonstr
, &state
, &button
)) {
336 g_warning("invalid button '%s'", buttonstr
);
340 contextstr
= g_ascii_strdown(contextstr
, -1);
341 context
= frame_context_from_string(contextstr
);
343 g_warning("invalid context '%s'", contextstr
);
349 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
){
351 if (b
->state
== state
&& b
->button
== button
) {
353 if (b
->action
[mact
] != NULL
) {
354 g_warning("duplicate binding");
357 b
->action
[mact
] = action
;
362 grab_all_clients(FALSE
);
364 /* add the binding */
365 b
= g_new0(MouseBinding
, 1);
368 b
->action
[mact
] = action
;
369 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
371 grab_all_clients(TRUE
);
376 void plugin_startup()
378 dispatch_register(Event_Client_Mapped
| Event_Client_Destroy
|
379 Event_X_ButtonPress
| Event_X_ButtonRelease
|
380 Event_X_MotionNotify
, (EventHandler
)event
, NULL
);
383 void plugin_shutdown()
385 dispatch_register(0, (EventHandler
)event
, NULL
);
387 grab_all_clients(FALSE
);