2 * Copyright © 2008 Kristian Høgsberg
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.
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
34 #include <cairo-drm.h>
36 #include "wayland-client.h"
37 #include "wayland-glib.h"
39 static const char gem_device[] = "/dev/dri/card0";
40 static const char socket_name[] = "\0wayland";
43 set_random_color(cairo_t *cr)
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);
54 draw_stuff(cairo_surface_t *surface, int width, int height)
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;
64 double t, dt = 2 * M_PI / (petal_count * 2);
65 double x1, y1, x2, y2, x3, y3;
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) {
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;
79 x1 - y1 * u, y1 + x1 * u,
80 x2 + y2 * v, y2 - x2 * v,
84 x2 - y2 * v, y2 + x2 * v,
85 x3 + y3 * u, y3 - x3 * u,
91 cairo_fill_preserve(cr);
99 struct wl_compositor *compositor;
100 struct wl_surface *surface;
101 int x, y, width, height;
106 handle_acknowledge(void *data,
107 struct wl_compositor *compositor,
108 uint32_t key, uint32_t frame)
113 handle_frame(void *data,
114 struct wl_compositor *compositor,
115 uint32_t frame, uint32_t timestamp)
117 struct flower *flower = data;
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);
126 static const struct wl_compositor_listener compositor_listener = {
131 int main(int argc, char *argv[])
133 struct wl_display *display;
134 struct wl_visual *visual;
136 cairo_device_t *device;
141 struct flower flower;
143 fd = open(gem_device, O_RDWR);
145 fprintf(stderr, "drm open failed: %m\n");
149 loop = g_main_loop_new(NULL, FALSE);
151 display = wl_display_create(socket_name, sizeof socket_name);
152 if (display == NULL) {
153 fprintf(stderr, "failed to create display: %m\n");
157 source = wl_glib_source_new(display);
158 g_source_attach(source, NULL);
160 /* Process connection events. */
161 wl_display_iterate(display, WL_DISPLAY_READABLE);
163 flower.compositor = wl_display_get_compositor(display);
168 flower.surface = wl_compositor_create_surface(flower.compositor);
170 clock_gettime(CLOCK_MONOTONIC, &ts);
172 flower.offset = random();
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);
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),
187 wl_compositor_add_listener(flower.compositor,
188 &compositor_listener, &flower);
190 wl_compositor_commit(flower.compositor, 0);
192 g_main_loop_run(loop);