]>
Dogcows Code - chaz/openbox/blob - plugins/mouse/mouse.c
1 #include "../../kernel/openbox.h"
2 #include "../../kernel/dispatch.h"
3 #include "../../kernel/action.h"
4 #include "../../kernel/client.h"
5 #include "../../kernel/frame.h"
6 #include "../../kernel/grab.h"
7 #include "../../kernel/engine.h"
10 #include "mouserc_parse.h"
13 void plugin_setup_config()
17 static int drag_threshold
= 3;
19 /* GData of GSList*s of PointerBinding*s. */
20 static GData
*bound_contexts
;
22 struct foreach_grab_temp
{
27 static void foreach_grab(GQuark key
, gpointer data
, gpointer user_data
)
29 struct foreach_grab_temp
*d
= user_data
;
31 for (it
= data
; it
!= NULL
; it
= it
->next
) {
32 /* grab/ungrab the button */
33 MouseBinding
*b
= it
->data
;
38 if (key
== g_quark_try_string("frame")) {
39 win
= d
->client
->frame
->window
;
41 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
42 } else if (key
== g_quark_try_string("client")) {
43 win
= d
->client
->frame
->plate
;
44 mode
= GrabModeSync
; /* this is handled in event */
45 mask
= ButtonPressMask
; /* can't catch more than this with Sync
46 mode the release event is manufactured
51 grab_button(b
->button
, b
->state
, win
, mask
, mode
);
53 ungrab_button(b
->button
, b
->state
, win
);
57 static void grab_for_client(Client
*client
, gboolean grab
)
59 struct foreach_grab_temp bt
;
62 g_datalist_foreach(&bound_contexts
, foreach_grab
, &bt
);
65 static void grab_all_clients(gboolean grab
)
69 for (it
= client_list
; it
!= NULL
; it
= it
->next
)
70 grab_for_client(it
->data
, grab
);
73 static void foreach_clear(GQuark key
, gpointer data
, gpointer user_data
)
76 user_data
= user_data
;
77 for (it
= data
; it
!= NULL
; it
= it
->next
) {
80 MouseBinding
*b
= it
->data
;
81 for (i
= 0; i
< NUM_MOUSEACTION
; ++i
)
82 if (b
->action
[i
] != NULL
)
83 action_free(b
->action
[i
]);
89 static void fire_button(MouseAction a
, GQuark context
, Client
*c
, guint state
,
95 for (it
= g_datalist_id_get_data(&bound_contexts
, context
);
96 it
!= NULL
; it
= it
->next
) {
98 if (b
->state
== state
&& b
->button
== button
)
101 /* if not bound, then nothing to do! */
102 if (it
== NULL
) return;
104 if (b
->action
[a
] != NULL
&& b
->action
[a
]->func
!= NULL
) {
105 b
->action
[a
]->data
.any
.c
= c
;
107 g_assert(!(b
->action
[a
]->func
== action_move
||
108 b
->action
[a
]->func
== action_resize
));
110 b
->action
[a
]->func(&b
->action
[a
]->data
);
114 /* corner should be the opposite corner of the window in which the pointer
115 clicked, Corner_TopLeft if a good default if there is no client */
116 static void fire_motion(MouseAction a
, GQuark context
, Client
*c
, guint state
,
117 guint button
, int cx
, int cy
, int cw
, int ch
,
118 int dx
, int dy
, gboolean final
, Corner corner
)
123 for (it
= g_datalist_id_get_data(&bound_contexts
, context
);
124 it
!= NULL
; it
= it
->next
) {
126 if (b
->state
== state
&& b
->button
== button
)
129 /* if not bound, then nothing to do! */
130 if (it
== NULL
) return;
132 if (b
->action
[a
] != NULL
&& b
->action
[a
]->func
!= NULL
) {
133 b
->action
[a
]->data
.any
.c
= c
;
135 if (b
->action
[a
]->func
== action_move
) {
136 b
->action
[a
]->data
.move
.x
= cx
+ dx
;
137 b
->action
[a
]->data
.move
.y
= cy
+ dy
;
138 b
->action
[a
]->data
.move
.final
= final
;
139 } else if (b
->action
[a
]->func
== action_resize
) {
140 b
->action
[a
]->data
.resize
.corner
= corner
;
143 b
->action
[a
]->data
.resize
.x
= cw
+ dx
;
144 b
->action
[a
]->data
.resize
.y
= ch
+ dy
;
146 case Corner_TopRight
:
147 b
->action
[a
]->data
.resize
.x
= cw
- dx
;
148 b
->action
[a
]->data
.resize
.y
= ch
+ dy
;
150 case Corner_BottomLeft
:
151 b
->action
[a
]->data
.resize
.x
= cw
+ dx
;
152 b
->action
[a
]->data
.resize
.y
= ch
- dy
;
154 case Corner_BottomRight
:
155 b
->action
[a
]->data
.resize
.x
= cw
- dx
;
156 b
->action
[a
]->data
.resize
.y
= ch
- dy
;
159 b
->action
[a
]->data
.resize
.final
= final
;
161 b
->action
[a
]->func(&b
->action
[a
]->data
);
165 static Corner
pick_corner(int x
, int y
, int cx
, int cy
, int cw
, int ch
)
167 if (x
- cx
< cw
/ 2) {
169 return Corner_BottomRight
;
171 return Corner_TopRight
;
174 return Corner_BottomLeft
;
176 return Corner_TopLeft
;
180 static void event(ObEvent
*e
, void *foo
)
183 static int px
, py
, cx
, cy
, cw
, ch
, dx
, dy
;
184 static guint button
= 0, lbutton
= 0;
185 static gboolean drag
= FALSE
;
186 static Corner corner
= Corner_TopLeft
;
187 gboolean click
= FALSE
;
188 gboolean dclick
= FALSE
;
192 case Event_Client_Mapped
:
193 grab_for_client(e
->data
.c
.client
, TRUE
);
196 case Event_Client_Destroy
:
197 grab_for_client(e
->data
.c
.client
, FALSE
);
200 case Event_X_ButtonPress
:
202 if (e
->data
.x
.client
!= NULL
) {
203 cx
= e
->data
.x
.client
->frame
->area
.x
;
204 cy
= e
->data
.x
.client
->frame
->area
.y
;
205 cw
= e
->data
.x
.client
->frame
->area
.width
;
206 ch
= e
->data
.x
.client
->frame
->area
.height
;
207 px
= e
->data
.x
.e
->xbutton
.x_root
;
208 py
= e
->data
.x
.e
->xbutton
.y_root
;
209 corner
= pick_corner(px
, py
, cx
, cy
, cw
, ch
);
211 button
= e
->data
.x
.e
->xbutton
.button
;
213 context
= engine_get_context(e
->data
.x
.client
,
214 e
->data
.x
.e
->xbutton
.window
);
216 fire_button(MouseAction_Press
, context
,
217 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
218 e
->data
.x
.e
->xbutton
.button
);
220 if (context
== g_quark_try_string("client")) {
221 /* Replay the event, so it goes to the client*/
222 XAllowEvents(ob_display
, ReplayPointer
, CurrentTime
);
223 /* Fall through to the release case! */
227 case Event_X_ButtonRelease
:
228 context
= engine_get_context(e
->data
.x
.client
,
229 e
->data
.x
.e
->xbutton
.window
);
230 if (e
->data
.x
.e
->xbutton
.button
== button
) {
233 fire_motion(MouseAction_Motion
, context
,
234 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
235 e
->data
.x
.e
->xbutton
.button
,
236 cx
, cy
, cw
, ch
, dx
, dy
, TRUE
, corner
);
241 /* clicks are only valid if its released over the window */
242 if (e
->data
.x
.e
->xbutton
.x
>= 0 &&
243 e
->data
.x
.e
->xbutton
.y
>= 0) {
247 XGetGeometry(ob_display
, e
->data
.x
.e
->xbutton
.window
,
248 &wjunk
, &junk
, &junk
, &w
, &h
, &ujunk
, &ujunk
);
249 if (e
->data
.x
.e
->xbutton
.x
< (signed)w
&&
250 e
->data
.x
.e
->xbutton
.y
< (signed)h
) {
252 /* double clicks happen if there were 2 in a row! */
253 if (lbutton
== button
&&
254 e
->data
.x
.e
->xbutton
.time
- 300 <= ltime
)
263 ltime
= e
->data
.x
.e
->xbutton
.time
;
265 fire_button(MouseAction_Press
, context
,
266 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
267 e
->data
.x
.e
->xbutton
.button
);
269 fire_button(MouseAction_Click
, context
,
270 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
271 e
->data
.x
.e
->xbutton
.button
);
273 fire_button(MouseAction_DClick
, context
,
274 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
275 e
->data
.x
.e
->xbutton
.button
);
278 case Event_X_MotionNotify
:
280 dx
= e
->data
.x
.e
->xmotion
.x_root
- px
;
281 dy
= e
->data
.x
.e
->xmotion
.y_root
- py
;
282 if (ABS(dx
) >= drag_threshold
|| ABS(dy
) >= drag_threshold
)
285 context
= engine_get_context(e
->data
.x
.client
,
286 e
->data
.x
.e
->xbutton
.window
);
287 fire_motion(MouseAction_Motion
, context
,
288 e
->data
.x
.client
, e
->data
.x
.e
->xmotion
.state
,
289 button
, cx
, cy
, cw
, ch
, dx
, dy
, FALSE
, corner
);
295 g_assert_not_reached();
299 gboolean
mbind(char *buttonstr
, char *contextstr
, MouseAction mact
,
307 if (!translate_button(buttonstr
, &state
, &button
)) {
308 g_warning("invalid button '%s'", buttonstr
);
312 contextstr
= g_ascii_strdown(contextstr
, -1);
313 context
= g_quark_try_string(contextstr
);
315 g_warning("invalid context '%s'", contextstr
);
321 for (it
= g_datalist_id_get_data(&bound_contexts
, context
);
322 it
!= NULL
; it
= it
->next
){
324 if (b
->state
== state
&& b
->button
== button
) {
326 if (b
->action
[mact
] != NULL
) {
327 g_warning("duplicate binding");
330 b
->action
[mact
] = action
;
335 grab_all_clients(FALSE
);
337 /* add the binding */
338 b
= g_new0(MouseBinding
, 1);
341 b
->action
[mact
] = action
;
342 g_datalist_id_set_data(&bound_contexts
, context
,
343 g_slist_append(g_datalist_id_get_data(&bound_contexts
, context
), b
));
345 grab_all_clients(TRUE
);
350 void plugin_startup()
352 dispatch_register(Event_Client_Mapped
| Event_Client_Destroy
|
353 Event_X_ButtonPress
| Event_X_ButtonRelease
|
354 Event_X_MotionNotify
, (EventHandler
)event
, NULL
);
359 void plugin_shutdown()
361 dispatch_register(0, (EventHandler
)event
, NULL
);
363 grab_all_clients(FALSE
);
364 g_datalist_foreach(&bound_contexts
, foreach_clear
, NULL
);
This page took 0.055246 seconds and 4 git commands to generate.