blob: e55433de240dcccb34cc65c4383e60e437d0299e [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øgsberg97f1ebe2008-09-30 09:46:10 -040036
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040037#include "wayland-client.h"
Kristian Høgsbergfb590842008-11-07 14:27:23 -050038#include "wayland-glib.h"
Kristian Høgsberg18fb7832008-11-25 22:53:28 -050039#include "cairo-util.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øgsberga67a71a2008-10-07 10:10:36 -040055static void *
56draw_stuff(int width, int height)
57{
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_surface_t *surface;
65 cairo_t *cr;
Kristian Høgsberga234e702008-10-11 22:13:51 -040066 int i;
67 double t, dt = 2 * M_PI / (petal_count * 2);
68 double x1, y1, x2, y2, x3, y3;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040069
70 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
71 width, height);
72
73 cr = cairo_create(surface);
Kristian Høgsberga234e702008-10-11 22:13:51 -040074 cairo_translate(cr, width / 2, height / 2);
75 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
76 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
77 x1 = cos(t) * r1;
78 y1 = sin(t) * r1;
79 x2 = cos(t + dt) * r2;
80 y2 = sin(t + dt) * r2;
81 x3 = cos(t + 2 * dt) * r1;
82 y3 = sin(t + 2 * dt) * r1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040083
Kristian Høgsberga234e702008-10-11 22:13:51 -040084 cairo_curve_to(cr,
85 x1 - y1 * u, y1 + x1 * u,
86 x2 + y2 * v, y2 - x2 * v,
87 x2, y2);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040088
Kristian Høgsberga234e702008-10-11 22:13:51 -040089 cairo_curve_to(cr,
90 x2 - y2 * v, y2 + x2 * v,
91 x3 + y3 * u, y3 - x3 * u,
92 x3, y3);
93 }
94
95 cairo_close_path(cr);
96 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040097 cairo_fill_preserve(cr);
Kristian Høgsberga234e702008-10-11 22:13:51 -040098 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040099 cairo_stroke(cr);
100
101 cairo_destroy(cr);
102
103 return surface;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400104}
105
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500106struct flower {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500107 struct wl_compositor *compositor;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500108 struct wl_surface *surface;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500109 int x, y, width, height;
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500110 int offset;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500111};
112
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500113static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500114handle_acknowledge(void *data,
115 struct wl_compositor *compositor,
116 uint32_t key, uint32_t frame)
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400117{
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500118}
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400119
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500120static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500121handle_frame(void *data,
122 struct wl_compositor *compositor,
123 uint32_t frame, uint32_t timestamp)
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500124{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500125 struct flower *flower = data;
126
127 wl_surface_map(flower->surface,
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500128 flower->x + cos((flower->offset + timestamp) / 400.0) * 400 - flower->width / 2,
129 flower->y + sin((flower->offset + timestamp) / 320.0) * 300 - flower->height / 2,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500130 flower->width, flower->height);
131 wl_compositor_commit(flower->compositor, 0);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400132}
133
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500134static const struct wl_compositor_listener compositor_listener = {
135 handle_acknowledge,
136 handle_frame,
137};
138
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400139int main(int argc, char *argv[])
140{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400141 struct wl_display *display;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500142 struct wl_visual *visual;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500143 int fd;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400144 cairo_surface_t *s;
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500145 struct timespec ts;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500146 GMainLoop *loop;
147 GSource *source;
148 struct flower flower;
Kristian Høgsberg18fb7832008-11-25 22:53:28 -0500149 struct buffer *buffer;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400150
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400151 fd = open(gem_device, O_RDWR);
152 if (fd < 0) {
153 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400154 return -1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400155 }
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400156
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500157 loop = g_main_loop_new(NULL, FALSE);
158
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500159 display = wl_display_create(socket_name, sizeof socket_name);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400160 if (display == NULL) {
161 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400162 return -1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400163 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400164
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -0500165 source = wl_glib_source_new(display);
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500166 g_source_attach(source, NULL);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400167
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500168 flower.compositor = wl_display_get_compositor(display);
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500169 flower.x = 512;
170 flower.y = 384;
171 flower.width = 200;
172 flower.height = 200;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500173 flower.surface = wl_compositor_create_surface(flower.compositor);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500174
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500175 clock_gettime(CLOCK_MONOTONIC, &ts);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500176 srandom(ts.tv_nsec);
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500177 flower.offset = random();
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500178
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500179 s = draw_stuff(flower.width, flower.height);
Kristian Høgsberg18fb7832008-11-25 22:53:28 -0500180 buffer = buffer_create_from_cairo_surface(fd, s);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400181
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500182 visual = wl_display_get_premultiplied_argb_visual(display);
183 wl_surface_attach(flower.surface,
184 buffer->name, flower.width, flower.height,
185 buffer->stride, 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}