blob: b919cf5f3c6236b66bd45aec0b9afaa2f19d6b40 [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øgsberg8ca1cc22010-08-30 08:21:44 -040065 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
66 cairo_set_source_rgba(cr, 0, 0, 0, 0);
67 cairo_paint(cr);
68
69 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
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øgsberg9d69f8e2010-09-03 14:46:38 -0400101 struct display *display;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400102 struct window *window;
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øgsberg9d69f8e2010-09-03 14:46:38 -0400108frame_callback(void *data, uint32_t time)
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500109{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500110 struct flower *flower = data;
111
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400112 window_move(flower->window,
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400113 flower->x + cos((flower->offset + time) / 400.0) * 400 - flower->width / 2,
114 flower->y + sin((flower->offset + time) / 320.0) * 300 - flower->height / 2);
115 wl_display_frame_callback(display_get_display(flower->display),
116 frame_callback, flower);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400117}
118
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400119int main(int argc, char *argv[])
120{
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 struct flower flower;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400124 struct display *d;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400125
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400126 d = display_create(&argc, &argv, NULL);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200127 if (d == NULL) {
128 fprintf(stderr, "failed to create display: %m\n");
129 return -1;
130 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400131
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500132 flower.x = 512;
133 flower.y = 384;
134 flower.width = 200;
135 flower.height = 200;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400136 flower.display = d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400137 flower.window = window_create(d, "flower", flower.x, flower.y,
138 flower.width, flower.height);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500139
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500140 clock_gettime(CLOCK_MONOTONIC, &ts);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500141 srandom(ts.tv_nsec);
Kristian Høgsberg3b949982009-01-15 12:45:49 -0500142 flower.offset = random();
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500143
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400144 window_set_decoration(flower.window, 0);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400145 window_draw(flower.window);
146 s = window_get_surface(flower.window);
147 if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) {
148 fprintf(stderr, "failed to create cairo drm surface\n");
149 return -1;
150 }
151
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500152 draw_stuff(s, flower.width, flower.height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400153 cairo_surface_flush(s);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400154 cairo_surface_destroy(s);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400155 window_flush(flower.window);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400156
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400157 window_set_user_data(flower.window, &flower);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400158 wl_display_frame_callback(display_get_display(d),
159 frame_callback, &flower);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400160
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400161 display_run(d);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400162
163 return 0;
164}