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
28 #include <sys/ioctl.h>
36 #include <cairo-drm.h>
38 #include "wayland-client.h"
39 #include "wayland-glib.h"
41 static const char gem_device[] = "/dev/dri/card0";
42 static const char socket_name[] = "\0wayland";
45 set_random_color(cairo_t *cr)
47 cairo_set_source_rgba(cr,
48 0.5 + (random() % 50) / 49.0,
49 0.5 + (random() % 50) / 49.0,
50 0.5 + (random() % 50) / 49.0,
51 0.5 + (random() % 100) / 99.0);
56 draw_stuff(cairo_surface_t *surface, int width, int height)
58 const int petal_count = 3 + random() % 5;
59 const double r1 = 60 + random() % 35;
60 const double r2 = 20 + random() % 40;
61 const double u = (10 + random() % 90) / 100.0;
62 const double v = (random() % 90) / 100.0;
66 double t, dt = 2 * M_PI / (petal_count * 2);
67 double x1, y1, x2, y2, x3, y3;
69 cr = cairo_create(surface);
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) {
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;
81 x1 - y1 * u, y1 + x1 * u,
82 x2 + y2 * v, y2 - x2 * v,
86 x2 - y2 * v, y2 + x2 * v,
87 x3 + y3 * u, y3 - x3 * u,
93 cairo_fill_preserve(cr);
101 struct wl_compositor *compositor;
102 struct wl_surface *surface;
103 int x, y, width, height;
108 handle_acknowledge(void *data,
109 struct wl_compositor *compositor,
110 uint32_t key, uint32_t frame)
115 handle_frame(void *data,
116 struct wl_compositor *compositor,
117 uint32_t frame, uint32_t timestamp)
119 struct flower *flower = data;
121 wl_surface_map(flower->surface,
122 flower->x + cos((flower->offset + timestamp) / 400.0) * 400 - flower->width / 2,
123 flower->y + sin((flower->offset + timestamp) / 320.0) * 300 - flower->height / 2,
124 flower->width, flower->height);
125 wl_compositor_commit(flower->compositor, 0);
128 static const struct wl_compositor_listener compositor_listener = {
133 int main(int argc, char *argv[])
135 struct wl_display *display;
136 struct wl_visual *visual;
138 cairo_drm_context_t *ctx;
143 struct flower flower;
145 fd = open(gem_device, O_RDWR);
147 fprintf(stderr, "drm open failed: %m\n");
151 loop = g_main_loop_new(NULL, FALSE);
153 display = wl_display_create(socket_name, sizeof socket_name);
154 if (display == NULL) {
155 fprintf(stderr, "failed to create display: %m\n");
159 source = wl_glib_source_new(display);
160 g_source_attach(source, NULL);
162 flower.compositor = wl_display_get_compositor(display);
167 flower.surface = wl_compositor_create_surface(flower.compositor);
169 clock_gettime(CLOCK_MONOTONIC, &ts);
171 flower.offset = random();
173 ctx = cairo_drm_context_get_for_fd(fd);
174 s = cairo_drm_surface_create(ctx, CAIRO_CONTENT_COLOR_ALPHA,
175 flower.width, flower.height);
176 draw_stuff(s, flower.width, flower.height);
178 visual = wl_display_get_premultiplied_argb_visual(display);
179 wl_surface_attach(flower.surface,
180 cairo_drm_surface_get_name(s),
181 flower.width, flower.height,
182 cairo_drm_surface_get_stride(s),
185 wl_compositor_add_listener(flower.compositor,
186 &compositor_listener, &flower);
188 wl_compositor_commit(flower.compositor, 0);
190 g_main_loop_run(loop);