Use cairo-gl in clients
[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 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 window *window;
100         int x, y, width, height;
101         int offset;
102 };
103
104 static void
105 handle_frame(struct window *window,
106              uint32_t frame, uint32_t timestamp, void *data)
107 {
108         struct flower *flower = data;
109
110         window_move(flower->window, 
111                     flower->x + cos((flower->offset + timestamp) / 400.0) * 400 - flower->width / 2,
112                     flower->y + sin((flower->offset + timestamp) / 320.0) * 300 - flower->height / 2);
113         window_commit(flower->window, 0);
114 }
115
116 int main(int argc, char *argv[])
117 {
118         struct wl_display *display;
119         struct wl_visual *visual;
120         int fd;
121         cairo_surface_t *s;
122         struct timespec ts;
123         GMainLoop *loop;
124         GSource *source;
125         struct flower flower;
126         struct display *d;
127
128         fd = open(gem_device, O_RDWR);
129         if (fd < 0) {
130                 fprintf(stderr, "drm open failed: %m\n");
131                 return -1;
132         }
133
134         loop = g_main_loop_new(NULL, FALSE);
135
136         display = wl_display_create(socket_name, sizeof socket_name);
137         if (display == NULL) {
138                 fprintf(stderr, "failed to create display: %m\n");
139                 return -1;
140         }
141
142         d = display_create(display, fd);
143
144         source = wl_glib_source_new(display);
145         g_source_attach(source, NULL);
146
147         flower.x = 512;
148         flower.y = 384;
149         flower.width = 200;
150         flower.height = 200;
151         flower.window = window_create(d, "flower", flower.x, flower.y,
152                                       flower.width, flower.height);
153
154         clock_gettime(CLOCK_MONOTONIC, &ts);
155         srandom(ts.tv_nsec);
156         flower.offset = random();
157
158         window_draw(flower.window);
159         s = window_get_surface(flower.window);
160         if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) {
161                 fprintf(stderr, "failed to create cairo drm surface\n");
162                 return -1;
163         }
164
165         draw_stuff(s, flower.width, flower.height);
166         cairo_surface_flush(s);
167
168         window_set_frame_handler(flower.window, handle_frame, &flower);
169         window_commit(flower.window, 0);
170
171         g_main_loop_run(loop);
172
173         return 0;
174 }