1 #include "../kernel/dispatch.h"
2 #include "../kernel/client.h"
3 #include "../kernel/frame.h"
4 #include "../kernel/stacking.h"
5 #include "../kernel/screen.h"
8 static int resistance
= 10;
9 static gboolean window_resistance
= TRUE
; /* window-to-window */
11 static void resist(Client
*c
, int *x
, int *y
)
15 int l
, t
, r
, b
; /* requested edges */
16 int al
, at
, ar
, ab
; /* screen area edges */
17 int cl
, ct
, cr
, cb
; /* current edges */
18 int w
, h
; /* current size */
19 gboolean snapx
= FALSE
, snapy
= FALSE
;
21 w
= c
->frame
->area
.width
;
22 h
= c
->frame
->area
.height
;
29 cl
= c
->frame
->area
.x
;
30 ct
= c
->frame
->area
.y
;
31 cr
= cl
+ c
->frame
->area
.width
- 1;
32 cb
= ct
+ c
->frame
->area
.height
- 1;
34 /* snap to other clients */
35 if (window_resistance
)
36 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
38 int tl
, tt
, tr
, tb
; /* 1 past the target's edges on each side */
41 /* don't snap to self or non-visibles */
42 if (!target
->frame
->visible
|| target
== c
) continue;
44 tl
= target
->frame
->area
.x
- 1;
45 tt
= target
->frame
->area
.y
- 1;
46 tr
= tl
+ target
->frame
->area
.width
+ 1;
47 tb
= tt
+ target
->frame
->area
.height
+ 1;
49 /* snapx and snapy ensure that the window snaps to the top-most
50 window edge available, without going all the way from
51 bottom-to-top in the stacking list
53 if (!snapx
&& cl
>= tr
&& l
< tr
&& l
>= tr
- resistance
)
54 *x
= tr
, snapx
= TRUE
;
55 else if (!snapx
&& cr
<= tl
&& r
> tl
&& r
<= tl
+ resistance
)
56 *x
= tl
- w
+ 1, snapx
= TRUE
;
57 else if (!snapy
&& ct
>= tb
&& t
< tb
&& t
>= tb
- resistance
)
58 *y
= tb
, snapy
= TRUE
;
59 else if (!snapy
&& cb
<= tt
&& b
> tt
&& b
<= tt
+ resistance
)
60 *y
= tt
- h
+ 1, snapy
= TRUE
;
62 if (snapx
&& snapy
) break;
65 /* get the screen boundaries */
66 area
= screen_area(c
->desktop
);
69 ar
= al
+ area
->width
- 1;
70 ab
= at
+ area
->height
- 1;
72 /* snap to screen edges */
73 if (cl
>= al
&& l
< al
&& l
>= al
- resistance
)
75 else if (cr
<= ar
&& r
> ar
&& r
<= ar
+ resistance
)
77 if (ct
>= at
&& t
< at
&& t
>= at
- resistance
)
79 else if (cb
<= ab
&& b
> ab
&& b
< ab
+ resistance
)
83 static void event(ObEvent
*e
, void *foo
)
85 g_assert(e
->type
== Event_Client_Moving
);
87 resist(e
->data
.c
.client
, &e
->data
.c
.num
[0], &e
->data
.c
.num
[1]);
92 dispatch_register(Event_Client_Moving
, (EventHandler
)event
, NULL
);
95 void plugin_shutdown()
97 dispatch_register(0, (EventHandler
)event
, NULL
);