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
35 #include "wayland-client.h"
36 #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 window *window;
100 int x, y, width, height;
105 handle_frame(struct window *window,
106 uint32_t frame, uint32_t timestamp, void *data)
108 struct flower *flower = data;
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);
116 int main(int argc, char *argv[])
118 struct wl_display *display;
119 struct wl_visual *visual;
125 struct flower flower;
128 fd = open(gem_device, O_RDWR);
130 fprintf(stderr, "drm open failed: %m\n");
134 loop = g_main_loop_new(NULL, FALSE);
136 display = wl_display_create(socket_name, sizeof socket_name);
137 if (display == NULL) {
138 fprintf(stderr, "failed to create display: %m\n");
142 d = display_create(display, fd);
144 source = wl_glib_source_new(display);
145 g_source_attach(source, NULL);
151 flower.window = window_create(d, "flower", flower.x, flower.y,
152 flower.width, flower.height);
154 clock_gettime(CLOCK_MONOTONIC, &ts);
156 flower.offset = random();
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");
165 draw_stuff(s, flower.width, flower.height);
166 cairo_surface_flush(s);
168 window_set_frame_handler(flower.window, handle_frame, &flower);
169 window_commit(flower.window, 0);
171 g_main_loop_run(loop);