Move clients to subdirectory
[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 #include <cairo-drm.h>
35
36 #include "wayland-client.h"
37 #include "wayland-glib.h"
38
39 static const char gem_device[] = "/dev/dri/card0";
40 static const char socket_name[] = "\0wayland";
41
42 static void
43 set_random_color(cairo_t *cr)
44 {
45         cairo_set_source_rgba(cr,
46                               0.5 + (random() % 50) / 49.0,
47                               0.5 + (random() % 50) / 49.0,
48                               0.5 + (random() % 50) / 49.0,
49                               0.5 + (random() % 100) / 99.0);
50 }
51
52
53 static void
54 draw_stuff(cairo_surface_t *surface, int width, int height)
55 {
56         const int petal_count = 3 + random() % 5;
57         const double r1 = 60 + random() % 35;
58         const double r2 = 20 + random() % 40;
59         const double u = (10 + random() % 90) / 100.0;
60         const double v = (random() % 90) / 100.0;
61
62         cairo_t *cr;
63         int i;
64         double t, dt = 2 * M_PI / (petal_count * 2);
65         double x1, y1, x2, y2, x3, y3;
66
67         cr = cairo_create(surface);
68         cairo_translate(cr, width / 2, height / 2);
69         cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
70         for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
71                 x1 = cos(t) * r1;
72                 y1 = sin(t) * r1;
73                 x2 = cos(t + dt) * r2;
74                 y2 = sin(t + dt) * r2;
75                 x3 = cos(t + 2 * dt) * r1;
76                 y3 = sin(t + 2 * dt) * r1;
77
78                 cairo_curve_to(cr,
79                                x1 - y1 * u, y1 + x1 * u,
80                                x2 + y2 * v, y2 - x2 * v,
81                                x2, y2);                        
82
83                 cairo_curve_to(cr,
84                                x2 - y2 * v, y2 + x2 * v,
85                                x3 + y3 * u, y3 - x3 * u,
86                                x3, y3);
87         }
88
89         cairo_close_path(cr);
90         set_random_color(cr);
91         cairo_fill_preserve(cr);
92         set_random_color(cr);
93         cairo_stroke(cr);
94
95         cairo_destroy(cr);
96 }
97
98 struct flower {
99         struct wl_compositor *compositor;
100         struct wl_surface *surface;
101         int x, y, width, height;
102         int offset;
103 };
104
105 static void
106 handle_acknowledge(void *data,
107                           struct wl_compositor *compositor,
108                           uint32_t key, uint32_t frame)
109 {
110 }
111
112 static void
113 handle_frame(void *data,
114                     struct wl_compositor *compositor,
115                     uint32_t frame, uint32_t timestamp)
116 {
117         struct flower *flower = data;
118
119         wl_surface_map(flower->surface, 
120                        flower->x + cos((flower->offset + timestamp) / 400.0) * 400 - flower->width / 2,
121                        flower->y + sin((flower->offset + timestamp) / 320.0) * 300 - flower->height / 2,
122                        flower->width, flower->height);
123         wl_compositor_commit(flower->compositor, 0);
124 }
125
126 static const struct wl_compositor_listener compositor_listener = {
127         handle_acknowledge,
128         handle_frame,
129 };
130
131 int main(int argc, char *argv[])
132 {
133         struct wl_display *display;
134         struct wl_visual *visual;
135         int fd;
136         cairo_device_t *device;
137         cairo_surface_t *s;
138         struct timespec ts;
139         GMainLoop *loop;
140         GSource *source;
141         struct flower flower;
142
143         fd = open(gem_device, O_RDWR);
144         if (fd < 0) {
145                 fprintf(stderr, "drm open failed: %m\n");
146                 return -1;
147         }
148
149         loop = g_main_loop_new(NULL, FALSE);
150
151         display = wl_display_create(socket_name, sizeof socket_name);
152         if (display == NULL) {
153                 fprintf(stderr, "failed to create display: %m\n");
154                 return -1;
155         }
156
157         source = wl_glib_source_new(display);
158         g_source_attach(source, NULL);
159
160         /* Process connection events. */
161         wl_display_iterate(display, WL_DISPLAY_READABLE);
162
163         flower.compositor = wl_display_get_compositor(display);
164         flower.x = 512;
165         flower.y = 384;
166         flower.width = 200;
167         flower.height = 200;
168         flower.surface = wl_compositor_create_surface(flower.compositor);
169
170         clock_gettime(CLOCK_MONOTONIC, &ts);
171         srandom(ts.tv_nsec);
172         flower.offset = random();
173
174         device = cairo_drm_device_get_for_fd(fd);
175         s = cairo_drm_surface_create(device,
176                                      CAIRO_CONTENT_COLOR_ALPHA,
177                                      flower.width, flower.height);
178         draw_stuff(s, flower.width, flower.height);
179
180         visual = wl_display_get_premultiplied_argb_visual(display);
181         wl_surface_attach(flower.surface,
182                           cairo_drm_surface_get_name(s),
183                           flower.width, flower.height,
184                           cairo_drm_surface_get_stride(s),
185                           visual);
186
187         wl_compositor_add_listener(flower.compositor,
188                                    &compositor_listener, &flower);
189
190         wl_compositor_commit(flower.compositor, 0);
191
192         g_main_loop_run(loop);
193
194         return 0;
195 }