blob: 5f3a85e85f0c8838899c557014c30443e5c72e3c [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øgsberg97f1ebe2008-09-30 09:46:10 -040034
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040035#include "wayland-client.h"
Kristian Høgsbergfb590842008-11-07 14:27:23 -050036#include "wayland-glib.h"
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040037#include "window.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øgsberga85fe3c2010-06-08 14:08:30 -040099 struct window *window;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500100 int x, y, width, height;
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500101 int offset;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500102};
103
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500104static void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400105handle_frame(struct window *window,
106 uint32_t frame, uint32_t timestamp, void *data)
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500107{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500108 struct flower *flower = data;
109
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400110 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);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400114}
115
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400116int main(int argc, char *argv[])
117{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400118 struct wl_display *display;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500119 struct wl_visual *visual;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500120 int fd;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400121 cairo_surface_t *s;
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500122 struct timespec ts;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500123 GMainLoop *loop;
124 GSource *source;
125 struct flower flower;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400126 struct display *d;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400127
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400128 fd = open(gem_device, O_RDWR);
129 if (fd < 0) {
130 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400131 return -1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400132 }
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400133
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500134 loop = g_main_loop_new(NULL, FALSE);
135
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500136 display = wl_display_create(socket_name, sizeof socket_name);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400137 if (display == NULL) {
138 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400139 return -1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400140 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400141
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400142 d = display_create(display, fd);
143
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -0500144 source = wl_glib_source_new(display);
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500145 g_source_attach(source, NULL);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400146
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500147 flower.x = 512;
148 flower.y = 384;
149 flower.width = 200;
150 flower.height = 200;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400151 flower.window = window_create(d, "flower", flower.x, flower.y,
152 flower.width, flower.height);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500153
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500154 clock_gettime(CLOCK_MONOTONIC, &ts);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500155 srandom(ts.tv_nsec);
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500156 flower.offset = random();
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500157
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400158 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");
162 return -1;
163 }
164
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500165 draw_stuff(s, flower.width, flower.height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400166 cairo_surface_flush(s);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400167
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400168 window_set_frame_handler(flower.window, handle_frame, &flower);
169 window_commit(flower.window, 0);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400170
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500171 g_main_loop_run(loop);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400172
173 return 0;
174}