blob: 9cbc42482c11eead88d0764070486eb56f78dbd9 [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øgsberg75bc6672012-01-10 09:43:58 -050039struct flower {
40 struct display *display;
41 struct window *window;
42 struct widget *widget;
43 int width, height;
44};
45
Kristian Høgsberga234e702008-10-11 22:13:51 -040046static void
47set_random_color(cairo_t *cr)
48{
49 cairo_set_source_rgba(cr,
Kristian Høgsbergd311e8a2008-10-12 22:58:40 -040050 0.5 + (random() % 50) / 49.0,
51 0.5 + (random() % 50) / 49.0,
52 0.5 + (random() % 50) / 49.0,
53 0.5 + (random() % 100) / 99.0);
Kristian Høgsberga234e702008-10-11 22:13:51 -040054}
55
56
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050057static void
58draw_stuff(cairo_surface_t *surface, int width, int height)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040059{
Kristian Høgsberga234e702008-10-11 22:13:51 -040060 const int petal_count = 3 + random() % 5;
61 const double r1 = 60 + random() % 35;
62 const double r2 = 20 + random() % 40;
63 const double u = (10 + random() % 90) / 100.0;
64 const double v = (random() % 90) / 100.0;
65
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040066 cairo_t *cr;
Kristian Høgsberga234e702008-10-11 22:13:51 -040067 int i;
68 double t, dt = 2 * M_PI / (petal_count * 2);
69 double x1, y1, x2, y2, x3, y3;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040070
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040071 cr = cairo_create(surface);
Kristian Høgsberg8ca1cc22010-08-30 08:21:44 -040072 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
73 cairo_set_source_rgba(cr, 0, 0, 0, 0);
74 cairo_paint(cr);
75
76 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberga234e702008-10-11 22:13:51 -040077 cairo_translate(cr, width / 2, height / 2);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050078 cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
Kristian Høgsberga234e702008-10-11 22:13:51 -040079 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
80 x1 = cos(t) * r1;
81 y1 = sin(t) * r1;
82 x2 = cos(t + dt) * r2;
83 y2 = sin(t + dt) * r2;
84 x3 = cos(t + 2 * dt) * r1;
85 y3 = sin(t + 2 * dt) * r1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040086
Kristian Høgsberga234e702008-10-11 22:13:51 -040087 cairo_curve_to(cr,
88 x1 - y1 * u, y1 + x1 * u,
89 x2 + y2 * v, y2 - x2 * v,
90 x2, y2);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040091
Kristian Høgsberga234e702008-10-11 22:13:51 -040092 cairo_curve_to(cr,
93 x2 - y2 * v, y2 + x2 * v,
94 x3 + y3 * u, y3 - x3 * u,
95 x3, y3);
96 }
97
98 cairo_close_path(cr);
99 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400100 cairo_fill_preserve(cr);
Kristian Høgsberga234e702008-10-11 22:13:51 -0400101 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400102 cairo_stroke(cr);
103
104 cairo_destroy(cr);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400105}
106
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500107static void
108redraw_handler(struct widget *widget, void *data)
109{
110 struct flower *flower = data;
111 cairo_surface_t *surface;
112
113 surface = window_get_surface(flower->window);
114 if (surface == NULL ||
115 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
116 fprintf(stderr, "failed to create cairo egl surface\n");
117 return;
118 }
119
120 draw_stuff(surface, flower->width, flower->height);
121 cairo_surface_destroy(surface);
122}
123
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500124static int
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500125motion_handler(struct widget *widget, struct input *input,
126 uint32_t time, int32_t x, int32_t y, void *data)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500127{
128 return POINTER_HAND1;
129}
130
131static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500132button_handler(struct widget *widget,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500133 struct input *input, uint32_t time,
134 int button, int state, void *data)
135{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500136 struct flower *flower = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500137
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500138 if (state)
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500139 window_move(flower->window, input, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500140}
141
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400142int main(int argc, char *argv[])
143{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500144 struct flower flower;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400145 struct display *d;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500146 struct timeval tv;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400147
Kristian Høgsberg9de79a92011-08-24 11:09:53 -0400148 d = display_create(&argc, &argv, NULL);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200149 if (d == NULL) {
150 fprintf(stderr, "failed to create display: %m\n");
151 return -1;
152 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400153
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500154 gettimeofday(&tv, NULL);
155 srandom(tv.tv_usec);
156
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500157 flower.width = 200;
158 flower.height = 200;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400159 flower.display = d;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500160 flower.window = window_create(d, flower.width, flower.height);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500161 flower.widget = window_add_widget(flower.window, &flower);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500162
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400163 window_set_decoration(flower.window, 0);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400164
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500165 widget_set_redraw_handler(flower.widget, redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500166 widget_set_motion_handler(flower.widget, motion_handler);
167 widget_set_button_handler(flower.widget, button_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500168
169 window_schedule_redraw(flower.window);
170
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400171 display_run(d);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400172
173 return 0;
174}