flower: Clear surface
[profile/ivi/weston.git] / clients / flower.c
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <math.h>
31 #include <time.h>
32 #include <cairo.h>
33 #include <glib.h>
34
35 #include "wayland-client.h"
36 #include "wayland-glib.h"
37 #include "window.h"
38
39 static void
40 set_random_color(cairo_t *cr)
41 {
42         cairo_set_source_rgba(cr,
43                               0.5 + (random() % 50) / 49.0,
44                               0.5 + (random() % 50) / 49.0,
45                               0.5 + (random() % 50) / 49.0,
46                               0.5 + (random() % 100) / 99.0);
47 }
48
49
50 static void
51 draw_stuff(cairo_surface_t *surface, int width, int height)
52 {
53         const int petal_count = 3 + random() % 5;
54         const double r1 = 60 + random() % 35;
55         const double r2 = 20 + random() % 40;
56         const double u = (10 + random() % 90) / 100.0;
57         const double v = (random() % 90) / 100.0;
58
59         cairo_t *cr;
60         int i;
61         double t, dt = 2 * M_PI / (petal_count * 2);
62         double x1, y1, x2, y2, x3, y3;
63
64         cr = cairo_create(surface);
65         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
66         cairo_set_source_rgba(cr, 0, 0, 0, 0);
67         cairo_paint(cr);
68
69         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
70         cairo_translate(cr, width / 2, height / 2);
71         cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
72         for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
73                 x1 = cos(t) * r1;
74                 y1 = sin(t) * r1;
75                 x2 = cos(t + dt) * r2;
76                 y2 = sin(t + dt) * r2;
77                 x3 = cos(t + 2 * dt) * r1;
78                 y3 = sin(t + 2 * dt) * r1;
79
80                 cairo_curve_to(cr,
81                                x1 - y1 * u, y1 + x1 * u,
82                                x2 + y2 * v, y2 - x2 * v,
83                                x2, y2);                        
84
85                 cairo_curve_to(cr,
86                                x2 - y2 * v, y2 + x2 * v,
87                                x3 + y3 * u, y3 - x3 * u,
88                                x3, y3);
89         }
90
91         cairo_close_path(cr);
92         set_random_color(cr);
93         cairo_fill_preserve(cr);
94         set_random_color(cr);
95         cairo_stroke(cr);
96
97         cairo_destroy(cr);
98 }
99
100 struct flower {
101         struct window *window;
102         int x, y, width, height;
103         int offset;
104 };
105
106 static void
107 handle_frame(struct window *window,
108              uint32_t frame, uint32_t timestamp, void *data)
109 {
110         struct flower *flower = data;
111
112         window_move(flower->window, 
113                     flower->x + cos((flower->offset + timestamp) / 400.0) * 400 - flower->width / 2,
114                     flower->y + sin((flower->offset + timestamp) / 320.0) * 300 - flower->height / 2);
115         window_commit(flower->window, 0);
116 }
117
118 int main(int argc, char *argv[])
119 {
120         cairo_surface_t *s;
121         struct timespec ts;
122         struct flower flower;
123         struct display *d;
124
125         d = display_create(&argc, &argv, NULL);
126
127         flower.x = 512;
128         flower.y = 384;
129         flower.width = 200;
130         flower.height = 200;
131         flower.window = window_create(d, "flower", flower.x, flower.y,
132                                       flower.width, flower.height);
133
134         clock_gettime(CLOCK_MONOTONIC, &ts);
135         srandom(ts.tv_nsec);
136         flower.offset = random();
137
138         window_set_decoration(flower.window, 0);
139         window_draw(flower.window);
140         s = window_get_surface(flower.window);
141         if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) {
142                 fprintf(stderr, "failed to create cairo drm surface\n");
143                 return -1;
144         }
145
146         draw_stuff(s, flower.width, flower.height);
147         cairo_surface_flush(s);
148
149         window_set_user_data(flower.window, &flower);
150         window_set_frame_handler(flower.window, handle_frame);
151         window_commit(flower.window, 0);
152
153         display_run(d);
154
155         return 0;
156 }