blob: 7743b44e37eadc340813b449d439de1f629c2c8d [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øgsberg82da52b2010-12-17 09:53:12 -050033#include <sys/time.h>
Kristian Høgsbergfb590842008-11-07 14:27:23 -050034#include <glib.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040035
Pekka Paalanen50719bc2011-11-22 14:18:50 +020036#include <wayland-client.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øgsberg82da52b2010-12-17 09:53:12 -0500100static int
101motion_handler(struct window *window,
102 struct input *input, uint32_t time,
103 int32_t x, int32_t y,
104 int32_t sx, int32_t sy, void *data)
105{
106 return POINTER_HAND1;
107}
108
109static void
110button_handler(struct window *window,
111 struct input *input, uint32_t time,
112 int button, int state, void *data)
113{
114 if (state)
115 window_move(window, input, time);
116}
117
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500118struct flower {
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400119 struct display *display;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400120 struct window *window;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500121 int width, height;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500122};
123
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400124int main(int argc, char *argv[])
125{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400126 cairo_surface_t *s;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500127 struct flower flower;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400128 struct display *d;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500129 struct timeval tv;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400130
Kristian Høgsberg9de79a92011-08-24 11:09:53 -0400131 d = display_create(&argc, &argv, NULL);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200132 if (d == NULL) {
133 fprintf(stderr, "failed to create display: %m\n");
134 return -1;
135 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400136
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500137 gettimeofday(&tv, NULL);
138 srandom(tv.tv_usec);
139
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500140 flower.width = 200;
141 flower.height = 200;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400142 flower.display = d;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500143 flower.window = window_create(d, flower.width, flower.height);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500144
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500145 window_set_title(flower.window, "flower");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400146 window_set_decoration(flower.window, 0);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400147 window_draw(flower.window);
148 s = window_get_surface(flower.window);
149 if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500150 fprintf(stderr, "failed to create cairo egl surface\n");
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400151 return -1;
152 }
153
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500154 draw_stuff(s, flower.width, flower.height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400155 cairo_surface_flush(s);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400156 cairo_surface_destroy(s);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400157 window_flush(flower.window);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400158
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500159 window_set_motion_handler(flower.window, motion_handler);
160 window_set_button_handler(flower.window, button_handler);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400161 window_set_user_data(flower.window, &flower);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400162 display_run(d);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400163
164 return 0;
165}