]>
Dogcows Code - chaz/openbox/blob - plugins/mouse/mouse.c
37c22bd80843d277cd711e8f2b1bd141060fe1a8
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/parse.h"
10 #include "kernel/frame.h"
11 #include "translate.h"
13 #include "mouseparse.h"
17 static int dclicktime
;
19 static void parse_assign(char *name
, ParseToken
*value
)
21 if (!g_ascii_strcasecmp(name
, "dragthreshold")) {
22 if (value
->type
!= TOKEN_INTEGER
)
23 yyerror("invalid value");
25 if (value
->data
.integer
>= 0)
26 threshold
= value
->data
.integer
;
28 } else if (!g_ascii_strcasecmp(name
, "doubleclicktime")) {
29 if (value
->type
!= TOKEN_INTEGER
)
30 yyerror("invalid value");
32 if (value
->data
.integer
>= 0)
33 dclicktime
= value
->data
.integer
;
36 yyerror("invalid option");
37 parse_free_token(value
);
40 void plugin_setup_config()
44 parse_reg_section("mouse", mouseparse
, parse_assign
);
47 /* Array of GSList*s of PointerBinding*s. */
48 static GSList
*bound_contexts
[NUM_CONTEXTS
];
50 static void grab_for_client(Client
*client
, gboolean grab
)
55 for (i
= 0; i
< NUM_CONTEXTS
; ++i
)
56 for (it
= bound_contexts
[i
]; it
!= NULL
; it
= it
->next
) {
57 /* grab/ungrab the button */
58 MouseBinding
*b
= it
->data
;
63 if (i
== Context_Frame
) {
64 win
= client
->frame
->window
;
66 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
67 } else if (i
== Context_Client
) {
68 win
= client
->frame
->plate
;
69 mode
= GrabModeSync
; /* this is handled in event */
70 mask
= ButtonPressMask
; /* can't catch more than this with Sync
71 mode the release event is
72 manufactured in event() */
76 grab_button(b
->button
, b
->state
, win
, mask
, mode
);
78 ungrab_button(b
->button
, b
->state
, win
);
82 static void grab_all_clients(gboolean grab
)
86 for (it
= client_list
; it
!= NULL
; it
= it
->next
)
87 grab_for_client(it
->data
, grab
);
90 static void clearall()
95 for(i
= 0; i
< NUM_CONTEXTS
; ++i
) {
96 for (it
= bound_contexts
[i
]; it
!= NULL
; it
= it
->next
) {
99 MouseBinding
*b
= it
->data
;
100 for (j
= 0; j
< NUM_MOUSEACTION
; ++j
)
101 if (b
->action
[j
] != NULL
)
102 action_free(b
->action
[j
]);
105 g_slist_free(bound_contexts
[i
]);
109 static void fire_button(MouseAction a
, Context context
, Client
*c
, guint state
,
115 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
117 if (b
->state
== state
&& b
->button
== button
)
120 /* if not bound, then nothing to do! */
121 if (it
== NULL
) return;
123 if (b
->action
[a
] != NULL
&& b
->action
[a
]->func
!= NULL
) {
124 b
->action
[a
]->data
.any
.c
= c
;
126 g_assert(!(b
->action
[a
]->func
== action_move
||
127 b
->action
[a
]->func
== action_resize
));
129 b
->action
[a
]->func(&b
->action
[a
]->data
);
133 /* corner should be the opposite corner of the window in which the pointer
134 clicked, Corner_TopLeft if a good default if there is no client
135 Returns True or False for if a binding existed for the action or not.
137 static gboolean
fire_motion(MouseAction a
, Context context
, Client
*c
,
138 guint state
, guint button
, int cx
, int cy
,
139 int cw
, int ch
, int dx
, int dy
,
140 gboolean final
, Corner corner
)
145 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
) {
147 if (b
->state
== state
&& b
->button
== button
)
150 /* if not bound, then nothing to do! */
151 if (it
== NULL
) return FALSE
;
153 if (b
->action
[a
] != NULL
&& b
->action
[a
]->func
!= NULL
) {
154 b
->action
[a
]->data
.any
.c
= c
;
156 if (b
->action
[a
]->func
== action_move
) {
157 b
->action
[a
]->data
.move
.x
= cx
+ dx
;
158 b
->action
[a
]->data
.move
.y
= cy
+ dy
;
159 b
->action
[a
]->data
.move
.final
= final
;
160 } else if (b
->action
[a
]->func
== action_resize
) {
161 b
->action
[a
]->data
.resize
.corner
= corner
;
164 b
->action
[a
]->data
.resize
.x
= cw
+ dx
;
165 b
->action
[a
]->data
.resize
.y
= ch
+ dy
;
167 case Corner_TopRight
:
168 b
->action
[a
]->data
.resize
.x
= cw
- dx
;
169 b
->action
[a
]->data
.resize
.y
= ch
+ dy
;
171 case Corner_BottomLeft
:
172 b
->action
[a
]->data
.resize
.x
= cw
+ dx
;
173 b
->action
[a
]->data
.resize
.y
= ch
- dy
;
175 case Corner_BottomRight
:
176 b
->action
[a
]->data
.resize
.x
= cw
- dx
;
177 b
->action
[a
]->data
.resize
.y
= ch
- dy
;
180 b
->action
[a
]->data
.resize
.final
= final
;
182 g_assert_not_reached();
183 b
->action
[a
]->func(&b
->action
[a
]->data
);
189 static Corner
pick_corner(int x
, int y
, int cx
, int cy
, int cw
, int ch
)
191 if (x
- cx
< cw
/ 2) {
193 return Corner_BottomRight
;
195 return Corner_TopRight
;
198 return Corner_BottomLeft
;
200 return Corner_TopLeft
;
204 static void event(ObEvent
*e
, void *foo
)
207 static int px
, py
, cx
, cy
, cw
, ch
, dx
, dy
;
208 static guint button
= 0, state
= 0, lbutton
= 0;
209 static gboolean drag
= FALSE
, drag_used
= FALSE
;
210 static Corner corner
= Corner_TopLeft
;
211 gboolean click
= FALSE
;
212 gboolean dclick
= FALSE
;
216 case Event_Client_Mapped
:
217 grab_for_client(e
->data
.c
.client
, TRUE
);
220 case Event_Client_Destroy
:
221 grab_for_client(e
->data
.c
.client
, FALSE
);
224 case Event_X_ButtonPress
:
226 if (e
->data
.x
.client
!= NULL
) {
227 cx
= e
->data
.x
.client
->frame
->area
.x
;
228 cy
= e
->data
.x
.client
->frame
->area
.y
;
229 /* use the client size because the frame can be differently
230 sized (shaded windows) and we want this based on the clients
232 cw
= e
->data
.x
.client
->area
.width
+
233 e
->data
.x
.client
->frame
->size
.left
+
234 e
->data
.x
.client
->frame
->size
.right
;
235 ch
= e
->data
.x
.client
->area
.height
+
236 e
->data
.x
.client
->frame
->size
.top
+
237 e
->data
.x
.client
->frame
->size
.bottom
;
238 px
= e
->data
.x
.e
->xbutton
.x_root
;
239 py
= e
->data
.x
.e
->xbutton
.y_root
;
240 corner
= pick_corner(px
, py
, cx
, cy
, cw
, ch
);
242 button
= e
->data
.x
.e
->xbutton
.button
;
243 state
= e
->data
.x
.e
->xbutton
.state
;
245 context
= engine_get_context(e
->data
.x
.client
,
246 e
->data
.x
.e
->xbutton
.window
);
248 fire_button(MouseAction_Press
, context
,
249 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
250 e
->data
.x
.e
->xbutton
.button
);
252 if (context
== Context_Client
) {
253 /* Replay the event, so it goes to the client*/
254 XAllowEvents(ob_display
, ReplayPointer
, event_lasttime
);
255 /* Fall through to the release case! */
259 case Event_X_ButtonRelease
:
260 context
= engine_get_context(e
->data
.x
.client
,
261 e
->data
.x
.e
->xbutton
.window
);
262 if (e
->data
.x
.e
->xbutton
.button
== button
) {
265 fire_motion(MouseAction_Motion
, context
,
266 e
->data
.x
.client
, state
, button
,
267 cx
, cy
, cw
, ch
, dx
, dy
, TRUE
, corner
);
268 drag
= drag_used
= FALSE
;
272 /* clicks are only valid if its released over the window */
275 guint ujunk
, b
, w
, h
;
276 XGetGeometry(ob_display
, e
->data
.x
.e
->xbutton
.window
,
277 &wjunk
, &junk
, &junk
, &w
, &h
, &b
, &ujunk
);
278 if (e
->data
.x
.e
->xbutton
.x
>= (signed)-b
&&
279 e
->data
.x
.e
->xbutton
.y
>= (signed)-b
&&
280 e
->data
.x
.e
->xbutton
.x
< (signed)(w
+b
) &&
281 e
->data
.x
.e
->xbutton
.y
< (signed)(h
+b
)) {
283 /* double clicks happen if there were 2 in a row! */
284 if (lbutton
== button
&&
285 e
->data
.x
.e
->xbutton
.time
- dclicktime
<= ltime
) {
296 ltime
= e
->data
.x
.e
->xbutton
.time
;
298 fire_button(MouseAction_Release
, context
,
299 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
300 e
->data
.x
.e
->xbutton
.button
);
302 fire_button(MouseAction_Click
, context
,
303 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
304 e
->data
.x
.e
->xbutton
.button
);
306 fire_button(MouseAction_DClick
, context
,
307 e
->data
.x
.client
, e
->data
.x
.e
->xbutton
.state
,
308 e
->data
.x
.e
->xbutton
.button
);
311 case Event_X_MotionNotify
:
313 dx
= e
->data
.x
.e
->xmotion
.x_root
- px
;
314 dy
= e
->data
.x
.e
->xmotion
.y_root
- py
;
316 (ABS(dx
) >= threshold
|| ABS(dy
) >= threshold
))
319 context
= engine_get_context(e
->data
.x
.client
,
320 e
->data
.x
.e
->xbutton
.window
);
321 drag_used
= fire_motion(MouseAction_Motion
, context
,
323 state
, button
, cx
, cy
, cw
, ch
, dx
, dy
,
330 g_assert_not_reached();
334 gboolean
mbind(char *buttonstr
, char *contextstr
, MouseAction mact
,
342 if (!translate_button(buttonstr
, &state
, &button
)) {
343 g_warning("invalid button '%s'", buttonstr
);
347 contextstr
= g_ascii_strdown(contextstr
, -1);
348 context
= frame_context_from_string(contextstr
);
350 g_warning("invalid context '%s'", contextstr
);
356 for (it
= bound_contexts
[context
]; it
!= NULL
; it
= it
->next
){
358 if (b
->state
== state
&& b
->button
== button
) {
360 if (b
->action
[mact
] != NULL
) {
361 g_warning("duplicate binding");
364 b
->action
[mact
] = action
;
369 grab_all_clients(FALSE
);
371 /* add the binding */
372 b
= g_new0(MouseBinding
, 1);
375 b
->action
[mact
] = action
;
376 bound_contexts
[context
] = g_slist_append(bound_contexts
[context
], b
);
378 grab_all_clients(TRUE
);
383 void plugin_startup()
385 dispatch_register(Event_Client_Mapped
| Event_Client_Destroy
|
386 Event_X_ButtonPress
| Event_X_ButtonRelease
|
387 Event_X_MotionNotify
, (EventHandler
)event
, NULL
);
390 void plugin_shutdown()
392 dispatch_register(0, (EventHandler
)event
, NULL
);
394 grab_all_clients(FALSE
);
This page took 0.051919 seconds and 4 git commands to generate.