blob: ce2cbc8b4f249cf1a2401d5728d7ec536cbeeaf8 [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øgsberga67a71a2008-10-07 10:10:36 -040027#include <i915_drm.h>
28#include <sys/ioctl.h>
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -050029#include <time.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040030#include <fcntl.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040031#include <unistd.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040032#include <math.h>
Kristian Høgsberga234e702008-10-11 22:13:51 -040033#include <time.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040034#include <cairo.h>
Kristian Høgsbergfb590842008-11-07 14:27:23 -050035#include <glib.h>
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050036#include <cairo-drm.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040037
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040038#include "wayland-client.h"
Kristian Høgsbergfb590842008-11-07 14:27:23 -050039#include "wayland-glib.h"
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040040
41static const char gem_device[] = "/dev/dri/card0";
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040042static const char socket_name[] = "\0wayland";
43
Kristian Høgsberga234e702008-10-11 22:13:51 -040044static void
45set_random_color(cairo_t *cr)
46{
47 cairo_set_source_rgba(cr,
Kristian Høgsbergd311e8a2008-10-12 22:58:40 -040048 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);
Kristian Høgsberga234e702008-10-11 22:13:51 -040052}
53
54
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050055static void
56draw_stuff(cairo_surface_t *surface, int width, int height)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040057{
Kristian Høgsberga234e702008-10-11 22:13:51 -040058 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;
63
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040064 cairo_t *cr;
Kristian Høgsberga234e702008-10-11 22:13:51 -040065 int i;
66 double t, dt = 2 * M_PI / (petal_count * 2);
67 double x1, y1, x2, y2, x3, y3;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040068
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040069 cr = cairo_create(surface);
Kristian Høgsberga234e702008-10-11 22:13:51 -040070 cairo_translate(cr, width / 2, height / 2);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050071 cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
Kristian Høgsberga234e702008-10-11 22:13:51 -040072 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
73 x1 = cos(t) * r1;
74 y1 = sin(t) * r1;
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;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040079
Kristian Høgsberga234e702008-10-11 22:13:51 -040080 cairo_curve_to(cr,
81 x1 - y1 * u, y1 + x1 * u,
82 x2 + y2 * v, y2 - x2 * v,
83 x2, y2);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040084
Kristian Høgsberga234e702008-10-11 22:13:51 -040085 cairo_curve_to(cr,
86 x2 - y2 * v, y2 + x2 * v,
87 x3 + y3 * u, y3 - x3 * u,
88 x3, y3);
89 }
90
91 cairo_close_path(cr);
92 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040093 cairo_fill_preserve(cr);
Kristian Høgsberga234e702008-10-11 22:13:51 -040094 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040095 cairo_stroke(cr);
96
97 cairo_destroy(cr);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040098}
99
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500100struct flower {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500101 struct wl_compositor *compositor;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500102 struct wl_surface *surface;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500103 int x, y, width, height;
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500104 int offset;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500105};
106
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500107static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500108handle_acknowledge(void *data,
109 struct wl_compositor *compositor,
110 uint32_t key, uint32_t frame)
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400111{
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500112}
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400113
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500114static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500115handle_frame(void *data,
116 struct wl_compositor *compositor,
117 uint32_t frame, uint32_t timestamp)
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500118{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500119 struct flower *flower = data;
120
121 wl_surface_map(flower->surface,
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500122 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,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500124 flower->width, flower->height);
125 wl_compositor_commit(flower->compositor, 0);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400126}
127
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500128static const struct wl_compositor_listener compositor_listener = {
129 handle_acknowledge,
130 handle_frame,
131};
132
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400133int main(int argc, char *argv[])
134{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400135 struct wl_display *display;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500136 struct wl_visual *visual;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500137 int fd;
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500138 cairo_drm_context_t *ctx;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400139 cairo_surface_t *s;
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500140 struct timespec ts;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500141 GMainLoop *loop;
142 GSource *source;
143 struct flower flower;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400144
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400145 fd = open(gem_device, O_RDWR);
146 if (fd < 0) {
147 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400148 return -1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400149 }
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400150
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500151 loop = g_main_loop_new(NULL, FALSE);
152
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500153 display = wl_display_create(socket_name, sizeof socket_name);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400154 if (display == NULL) {
155 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400156 return -1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400157 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400158
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -0500159 source = wl_glib_source_new(display);
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500160 g_source_attach(source, NULL);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400161
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500162 flower.compositor = wl_display_get_compositor(display);
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500163 flower.x = 512;
164 flower.y = 384;
165 flower.width = 200;
166 flower.height = 200;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500167 flower.surface = wl_compositor_create_surface(flower.compositor);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500168
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500169 clock_gettime(CLOCK_MONOTONIC, &ts);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500170 srandom(ts.tv_nsec);
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500171 flower.offset = random();
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500172
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500173 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);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400177
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500178 visual = wl_display_get_premultiplied_argb_visual(display);
179 wl_surface_attach(flower.surface,
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500180 cairo_drm_surface_get_name(s),
181 flower.width, flower.height,
182 cairo_drm_surface_get_stride(s),
183 visual);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500184
185 wl_compositor_add_listener(flower.compositor,
186 &compositor_listener, &flower);
187
188 wl_compositor_commit(flower.compositor, 0);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400189
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500190 g_main_loop_run(loop);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400191
192 return 0;
193}