blob: db79788c197d512d1c78bd482975ab88d8fe5646 [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øgsberg44f36e32008-11-26 12:57:31 -0500107 struct wl_display *display;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500108 struct wl_surface *surface;
109 int i;
110 int x, y, width, height;
111};
112
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500113static void
114move_flower(struct flower *flower)
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400115{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500116 wl_surface_map(flower->surface,
117 flower->x + cos(flower->i / 31.0) * 400 - flower->width / 2,
118 flower->y + sin(flower->i / 27.0) * 300 - flower->height / 2,
119 flower->width, flower->height);
120 flower->i++;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500121 wl_display_commit(flower->display, 0);
122}
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400123
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500124static void
125event_handler(struct wl_display *display,
126 uint32_t object, uint32_t opcode,
127 uint32_t size, uint32_t *p, void *data)
128{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500129 if (object == 1 && opcode == 4)
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500130 move_flower(data);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400131}
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øgsbergfb590842008-11-07 14:27:23 -0500136 int fd;
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øgsberg18fb7832008-11-25 22:53:28 -0500142 struct buffer *buffer;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400143
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400144 fd = open(gem_device, O_RDWR);
145 if (fd < 0) {
146 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400147 return -1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400148 }
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400149
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500150 loop = g_main_loop_new(NULL, FALSE);
151
152 display = wl_display_create(socket_name);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400153 if (display == NULL) {
154 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400155 return -1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400156 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400157
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500158 source = wayland_source_new(display);
159 g_source_attach(source, NULL);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400160
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500161 flower.display = display;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500162 flower.x = 512;
163 flower.y = 384;
164 flower.width = 200;
165 flower.height = 200;
166 flower.surface = wl_display_create_surface(display);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500167
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500168 clock_gettime(CLOCK_MONOTONIC, &ts);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500169 srandom(ts.tv_nsec);
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500170 flower.i = ts.tv_nsec;
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500171
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500172 s = draw_stuff(flower.width, flower.height);
Kristian Høgsberg18fb7832008-11-25 22:53:28 -0500173 buffer = buffer_create_from_cairo_surface(fd, s);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400174
Kristian Høgsberg18fb7832008-11-25 22:53:28 -0500175 wl_surface_attach(flower.surface, buffer->name, flower.width, flower.height,
176 buffer->stride);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500177 wl_display_set_event_handler(display, event_handler, &flower);
178 move_flower(&flower);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400179
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500180 g_main_loop_run(loop);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400181
182 return 0;
183}