blob: 8b5e1bbc414ebbf544be806bb119c87c7189e6f1 [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
Kristian Høgsberga234e702008-10-11 22:13:51 -040039static void
40set_random_color(cairo_t *cr)
41{
42 cairo_set_source_rgba(cr,
Kristian Høgsbergd311e8a2008-10-12 22:58:40 -040043 0.5 + (random() % 50) / 49.0,
44 0.5 + (random() % 50) / 49.0,
45 0.5 + (random() % 50) / 49.0,
46 0.5 + (random() % 100) / 99.0);
Kristian Høgsberga234e702008-10-11 22:13:51 -040047}
48
49
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050050static void
51draw_stuff(cairo_surface_t *surface, int width, int height)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040052{
Kristian Høgsberga234e702008-10-11 22:13:51 -040053 const int petal_count = 3 + random() % 5;
54 const double r1 = 60 + random() % 35;
55 const double r2 = 20 + random() % 40;
56 const double u = (10 + random() % 90) / 100.0;
57 const double v = (random() % 90) / 100.0;
58
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040059 cairo_t *cr;
Kristian Høgsberga234e702008-10-11 22:13:51 -040060 int i;
61 double t, dt = 2 * M_PI / (petal_count * 2);
62 double x1, y1, x2, y2, x3, y3;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040063
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040064 cr = cairo_create(surface);
Kristian Høgsberga234e702008-10-11 22:13:51 -040065 cairo_translate(cr, width / 2, height / 2);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050066 cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
Kristian Høgsberga234e702008-10-11 22:13:51 -040067 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
68 x1 = cos(t) * r1;
69 y1 = sin(t) * r1;
70 x2 = cos(t + dt) * r2;
71 y2 = sin(t + dt) * r2;
72 x3 = cos(t + 2 * dt) * r1;
73 y3 = sin(t + 2 * dt) * r1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040074
Kristian Høgsberga234e702008-10-11 22:13:51 -040075 cairo_curve_to(cr,
76 x1 - y1 * u, y1 + x1 * u,
77 x2 + y2 * v, y2 - x2 * v,
78 x2, y2);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040079
Kristian Høgsberga234e702008-10-11 22:13:51 -040080 cairo_curve_to(cr,
81 x2 - y2 * v, y2 + x2 * v,
82 x3 + y3 * u, y3 - x3 * u,
83 x3, y3);
84 }
85
86 cairo_close_path(cr);
87 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040088 cairo_fill_preserve(cr);
Kristian Høgsberga234e702008-10-11 22:13:51 -040089 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040090 cairo_stroke(cr);
91
92 cairo_destroy(cr);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040093}
94
Kristian Høgsbergfb590842008-11-07 14:27:23 -050095struct flower {
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040096 struct window *window;
Kristian Høgsbergfb590842008-11-07 14:27:23 -050097 int x, y, width, height;
Kristian Høgsberg3b949982009-01-15 12:45:49 -050098 int offset;
Kristian Høgsbergfb590842008-11-07 14:27:23 -050099};
100
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500101static void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400102handle_frame(struct window *window,
103 uint32_t frame, uint32_t timestamp, void *data)
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500104{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500105 struct flower *flower = data;
106
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400107 window_move(flower->window,
108 flower->x + cos((flower->offset + timestamp) / 400.0) * 400 - flower->width / 2,
109 flower->y + sin((flower->offset + timestamp) / 320.0) * 300 - flower->height / 2);
110 window_commit(flower->window, 0);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400111}
112
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400113int main(int argc, char *argv[])
114{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400115 struct wl_display *display;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500116 struct wl_visual *visual;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500117 int fd;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400118 cairo_surface_t *s;
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500119 struct timespec ts;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500120 struct flower flower;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400121 struct display *d;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400122
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400123 d = display_create(&argc, &argv, NULL);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400124
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500125 flower.x = 512;
126 flower.y = 384;
127 flower.width = 200;
128 flower.height = 200;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400129 flower.window = window_create(d, "flower", flower.x, flower.y,
130 flower.width, flower.height);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500131
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500132 clock_gettime(CLOCK_MONOTONIC, &ts);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500133 srandom(ts.tv_nsec);
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500134 flower.offset = random();
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500135
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400136 window_set_decoration(flower.window, 0);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400137 window_draw(flower.window);
138 s = window_get_surface(flower.window);
139 if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) {
140 fprintf(stderr, "failed to create cairo drm surface\n");
141 return -1;
142 }
143
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500144 draw_stuff(s, flower.width, flower.height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400145 cairo_surface_flush(s);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400146
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400147 window_set_frame_handler(flower.window, handle_frame, &flower);
148 window_commit(flower.window, 0);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400149
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400150 display_run(d);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400151
152 return 0;
153}