blob: adbb0cb7cad1e4f4debb5bb9e8dc2d0e30ca8c28 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
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
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040023#include <stdint.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040024#include <stdio.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040025#include <stdlib.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040026#include <string.h>
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -050027#include <time.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040028#include <fcntl.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040029#include <unistd.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040030#include <math.h>
Kristian Høgsberga234e702008-10-11 22:13:51 -040031#include <time.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040032#include <cairo.h>
Kristian Høgsbergfb590842008-11-07 14:27:23 -050033#include <glib.h>
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050034#include <cairo-drm.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040035
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040036#include "wayland-client.h"
Kristian Høgsbergfb590842008-11-07 14:27:23 -050037#include "wayland-glib.h"
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040038
39static const char gem_device[] = "/dev/dri/card0";
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040040static const char socket_name[] = "\0wayland";
41
Kristian Høgsberga234e702008-10-11 22:13:51 -040042static void
43set_random_color(cairo_t *cr)
44{
45 cairo_set_source_rgba(cr,
Kristian Høgsbergd311e8a2008-10-12 22:58:40 -040046 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);
Kristian Høgsberga234e702008-10-11 22:13:51 -040050}
51
52
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050053static void
54draw_stuff(cairo_surface_t *surface, int width, int height)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040055{
Kristian Høgsberga234e702008-10-11 22:13:51 -040056 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
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040062 cairo_t *cr;
Kristian Høgsberga234e702008-10-11 22:13:51 -040063 int i;
64 double t, dt = 2 * M_PI / (petal_count * 2);
65 double x1, y1, x2, y2, x3, y3;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040066
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040067 cr = cairo_create(surface);
Kristian Høgsberga234e702008-10-11 22:13:51 -040068 cairo_translate(cr, width / 2, height / 2);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050069 cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
Kristian Høgsberga234e702008-10-11 22:13:51 -040070 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;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040077
Kristian Høgsberga234e702008-10-11 22:13:51 -040078 cairo_curve_to(cr,
79 x1 - y1 * u, y1 + x1 * u,
80 x2 + y2 * v, y2 - x2 * v,
81 x2, y2);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040082
Kristian Høgsberga234e702008-10-11 22:13:51 -040083 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);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040091 cairo_fill_preserve(cr);
Kristian Høgsberga234e702008-10-11 22:13:51 -040092 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040093 cairo_stroke(cr);
94
95 cairo_destroy(cr);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040096}
97
Kristian Høgsbergfb590842008-11-07 14:27:23 -050098struct flower {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050099 struct wl_compositor *compositor;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500100 struct wl_surface *surface;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500101 int x, y, width, height;
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500102 int offset;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500103};
104
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500105static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500106handle_acknowledge(void *data,
107 struct wl_compositor *compositor,
108 uint32_t key, uint32_t frame)
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400109{
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500110}
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400111
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500112static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500113handle_frame(void *data,
114 struct wl_compositor *compositor,
115 uint32_t frame, uint32_t timestamp)
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500116{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500117 struct flower *flower = data;
118
119 wl_surface_map(flower->surface,
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500120 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,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500122 flower->width, flower->height);
123 wl_compositor_commit(flower->compositor, 0);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400124}
125
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500126static const struct wl_compositor_listener compositor_listener = {
127 handle_acknowledge,
128 handle_frame,
129};
130
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400131int main(int argc, char *argv[])
132{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400133 struct wl_display *display;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500134 struct wl_visual *visual;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500135 int fd;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +0100136 cairo_device_t *device;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400137 cairo_surface_t *s;
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500138 struct timespec ts;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500139 GMainLoop *loop;
140 GSource *source;
141 struct flower flower;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400142
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400143 fd = open(gem_device, O_RDWR);
144 if (fd < 0) {
145 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400146 return -1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400147 }
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400148
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500149 loop = g_main_loop_new(NULL, FALSE);
150
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500151 display = wl_display_create(socket_name, sizeof socket_name);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400152 if (display == NULL) {
153 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400154 return -1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400155 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400156
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -0500157 source = wl_glib_source_new(display);
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500158 g_source_attach(source, NULL);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400159
Kristian Høgsberg57b751b2001-01-01 23:44:10 -0500160 /* Process connection events. */
161 wl_display_iterate(display, WL_DISPLAY_READABLE);
162
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500163 flower.compositor = wl_display_get_compositor(display);
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500164 flower.x = 512;
165 flower.y = 384;
166 flower.width = 200;
167 flower.height = 200;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500168 flower.surface = wl_compositor_create_surface(flower.compositor);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500169
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500170 clock_gettime(CLOCK_MONOTONIC, &ts);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500171 srandom(ts.tv_nsec);
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500172 flower.offset = random();
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500173
Kristian Høgsberg26449102009-05-28 20:23:31 -0400174 device = cairo_drm_device_get_for_fd(fd);
175 s = cairo_drm_surface_create(device,
176 CAIRO_CONTENT_COLOR_ALPHA,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500177 flower.width, flower.height);
178 draw_stuff(s, flower.width, flower.height);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400179
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500180 visual = wl_display_get_premultiplied_argb_visual(display);
181 wl_surface_attach(flower.surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500182 cairo_drm_surface_get_name(s),
183 flower.width, flower.height,
184 cairo_drm_surface_get_stride(s),
185 visual);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500186
187 wl_compositor_add_listener(flower.compositor,
188 &compositor_listener, &flower);
189
190 wl_compositor_commit(flower.compositor, 0);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400191
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500192 g_main_loop_run(loop);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400193
194 return 0;
195}