summaryrefslogtreecommitdiff
path: root/shapes.c
blob: 79cbf3fdfc446cdefafb9ff03a2d9b34e8b415d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/shape.h>
#include <X11/Xutil.h>

int scr_width, scr_height;

uint64_t x, y, width, height;
Window window;

#define MIN_WIDTH 10
#define MIN_HEIGHT 10
#define MAX_WIDTH (800 - MIN_WIDTH)
#define MAX_HEIGHT (800 - MIN_HEIGHT)

static uint32_t
reflect (uint64_t i, uint64_t size)
{
    i = i % (size * 2);
    if (i >= size)
	i = size * 2 - i - 1;

    return i;
}

#define OUTLINE_WIDTH 3

void
set_shape (Display *dpy, Window window, XRectangle *rect)
{
    if (rect->width > OUTLINE_WIDTH * 2 &&
	rect->height > OUTLINE_WIDTH * 2)
    {
	XRectangle xrect;
	Region inner_xregion;
	Region outer_xregion;

	inner_xregion = XCreateRegion ();
	outer_xregion = XCreateRegion ();

	xrect.x = 0;
	xrect.y = 0;
	xrect.width = rect->width;
	xrect.height = rect->height;

	XUnionRectWithRegion (&xrect, outer_xregion, outer_xregion);

	xrect.x += OUTLINE_WIDTH;
	xrect.y += OUTLINE_WIDTH;
	xrect.width -= OUTLINE_WIDTH * 2;
	xrect.height -= OUTLINE_WIDTH * 2;

	XUnionRectWithRegion (&xrect, inner_xregion, inner_xregion);

	XSubtractRegion (outer_xregion, inner_xregion, outer_xregion);

	XShapeCombineRegion (dpy, window,
			     ShapeBounding, 0, 0, outer_xregion, ShapeSet);

	XDestroyRegion (outer_xregion);
	XDestroyRegion (inner_xregion);
    }
}

static void
update_window (Display *dpy)
{
    uint32_t real_x, real_y, real_width, real_height;
    XRectangle rect;

    real_width = reflect (width, MAX_WIDTH) + MIN_WIDTH;
    real_height = reflect (height, MAX_HEIGHT) + MIN_HEIGHT;

    real_x = reflect (x, scr_width);
    real_y = reflect (y, scr_height);

    if (real_x + real_width >= scr_width)
	real_x = scr_width - real_width;
    if (real_y + real_height >= scr_height)
	real_y = scr_height - real_height;

    XMoveResizeWindow (dpy, window, real_x, real_y,
		       real_width, real_height);

    rect.x = real_x;
    rect.y = real_y;
    rect.width = real_width;
    rect.height = real_height;

    set_shape (dpy, window, &rect);
    
    
    x += rand () % 20;
    width += rand () % 20;
    y += rand () % 20;
    height += rand () % 20;

    XSync (dpy, False);
}

int
main ()
{
    Display *dpy = XOpenDisplay (NULL);
    XSetWindowAttributes attr;

    attr.override_redirect = True;
    attr.background_pixel = 0;

    x = 10;
    y = 10;
    width = 400;
    height = 400;

    scr_width = DisplayWidth (dpy, 0);
    scr_height = DisplayHeight (dpy, 0);

    window = XCreateWindow (
	dpy, DefaultRootWindow (dpy), x, y, width, height,
	0, 0,
	InputOutput, DefaultVisual (dpy, 0),
	CWOverrideRedirect | CWBackPixel, &attr);

    XMapWindow (dpy, window);

    while (1)
    {
	update_window (dpy);

	usleep (10000);
    }

    XSync (dpy, False);

    sleep (10);
}