Set window user data using separate function
[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_translate(cr, width / 2, height / 2);
66         cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
67         for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
68                 x1 = cos(t) * r1;
69                 y1 = sin(t) * r1;
70                 x2 = cos(t + dt) * r2;
71                 y2 = sin(t + dt) * r2;
72                 x3 = cos(t + 2 * dt) * r1;
73                 y3 = sin(t + 2 * dt) * r1;
74
75                 cairo_curve_to(cr,
76                                x1 - y1 * u, y1 + x1 * u,
77                                x2 + y2 * v, y2 - x2 * v,
78                                x2, y2);                        
79
80                 cairo_curve_to(cr,
81                                x2 - y2 * v, y2 + x2 * v,
82                                x3 + y3 * u, y3 - x3 * u,
83                                x3, y3);
84         }
85
86         cairo_close_path(cr);
87         set_random_color(cr);
88         cairo_fill_preserve(cr);
89         set_random_color(cr);
90         cairo_stroke(cr);
91
92         cairo_destroy(cr);
93 }
94
95 struct flower {
96         struct window *window;
97         int x, y, width, height;
98         int offset;
99 };
100
101 static void
102 handle_frame(struct window *window,
103              uint32_t frame, uint32_t timestamp, void *data)
104 {
105         struct flower *flower = data;
106
107         window_move(flower->window, 
108                     flower->x + cos((flower->offset + timestamp) / 400.0) * 400 - flower->width / 2,
109                     flower->y + sin((flower->offset + timestamp) / 320.0) * 300 - flower->height / 2);
110         window_commit(flower->window, 0);
111 }
112
113 int main(int argc, char *argv[])
114 {
115         cairo_surface_t *s;
116         struct timespec ts;
117         struct flower flower;
118         struct display *d;
119
120         d = display_create(&argc, &argv, NULL);
121
122         flower.x = 512;
123         flower.y = 384;
124         flower.width = 200;
125         flower.height = 200;
126         flower.window = window_create(d, "flower", flower.x, flower.y,
127                                       flower.width, flower.height);
128
129         clock_gettime(CLOCK_MONOTONIC, &ts);
130         srandom(ts.tv_nsec);
131         flower.offset = random();
132
133         window_set_decoration(flower.window, 0);
134         window_draw(flower.window);
135         s = window_get_surface(flower.window);
136         if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) {
137                 fprintf(stderr, "failed to create cairo drm surface\n");
138                 return -1;
139         }
140
141         draw_stuff(s, flower.width, flower.height);
142         cairo_surface_flush(s);
143
144         window_set_user_data(flower.window, &flower);
145         window_set_frame_handler(flower.window, handle_frame);
146         window_commit(flower.window, 0);
147
148         display_run(d);
149
150         return 0;
151 }