blob: de7c1d42c017209b2a92eccc77b2f3d13d8a28bd [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
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040036#include "wayland-client.h"
Kristian Høgsbergfb590842008-11-07 14:27:23 -050037#include "wayland-glib.h"
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040038#include "window.h"
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040039
Kristian Høgsberga234e702008-10-11 22:13:51 -040040static void
41set_random_color(cairo_t *cr)
42{
43 cairo_set_source_rgba(cr,
Kristian Høgsbergd311e8a2008-10-12 22:58:40 -040044 0.5 + (random() % 50) / 49.0,
45 0.5 + (random() % 50) / 49.0,
46 0.5 + (random() % 50) / 49.0,
47 0.5 + (random() % 100) / 99.0);
Kristian Høgsberga234e702008-10-11 22:13:51 -040048}
49
50
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050051static void
52draw_stuff(cairo_surface_t *surface, int width, int height)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040053{
Kristian Høgsberga234e702008-10-11 22:13:51 -040054 const int petal_count = 3 + random() % 5;
55 const double r1 = 60 + random() % 35;
56 const double r2 = 20 + random() % 40;
57 const double u = (10 + random() % 90) / 100.0;
58 const double v = (random() % 90) / 100.0;
59
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040060 cairo_t *cr;
Kristian Høgsberga234e702008-10-11 22:13:51 -040061 int i;
62 double t, dt = 2 * M_PI / (petal_count * 2);
63 double x1, y1, x2, y2, x3, y3;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040064
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040065 cr = cairo_create(surface);
Kristian Høgsberg8ca1cc22010-08-30 08:21:44 -040066 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
67 cairo_set_source_rgba(cr, 0, 0, 0, 0);
68 cairo_paint(cr);
69
70 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberga234e702008-10-11 22:13:51 -040071 cairo_translate(cr, width / 2, height / 2);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050072 cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
Kristian Høgsberga234e702008-10-11 22:13:51 -040073 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
74 x1 = cos(t) * r1;
75 y1 = sin(t) * r1;
76 x2 = cos(t + dt) * r2;
77 y2 = sin(t + dt) * r2;
78 x3 = cos(t + 2 * dt) * r1;
79 y3 = sin(t + 2 * dt) * r1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040080
Kristian Høgsberga234e702008-10-11 22:13:51 -040081 cairo_curve_to(cr,
82 x1 - y1 * u, y1 + x1 * u,
83 x2 + y2 * v, y2 - x2 * v,
84 x2, y2);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040085
Kristian Høgsberga234e702008-10-11 22:13:51 -040086 cairo_curve_to(cr,
87 x2 - y2 * v, y2 + x2 * v,
88 x3 + y3 * u, y3 - x3 * u,
89 x3, y3);
90 }
91
92 cairo_close_path(cr);
93 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040094 cairo_fill_preserve(cr);
Kristian Høgsberga234e702008-10-11 22:13:51 -040095 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040096 cairo_stroke(cr);
97
98 cairo_destroy(cr);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040099}
100
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500101static int
102motion_handler(struct window *window,
103 struct input *input, uint32_t time,
104 int32_t x, int32_t y,
105 int32_t sx, int32_t sy, void *data)
106{
107 return POINTER_HAND1;
108}
109
110static void
111button_handler(struct window *window,
112 struct input *input, uint32_t time,
113 int button, int state, void *data)
114{
115 if (state)
116 window_move(window, input, time);
117}
118
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500119struct flower {
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400120 struct display *display;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400121 struct window *window;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500122 int width, height;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500123};
124
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400125int main(int argc, char *argv[])
126{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400127 cairo_surface_t *s;
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500128 struct flower flower;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400129 struct display *d;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500130 struct timeval tv;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400131
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400132 d = display_create(&argc, &argv, NULL);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200133 if (d == NULL) {
134 fprintf(stderr, "failed to create display: %m\n");
135 return -1;
136 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400137
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500138 gettimeofday(&tv, NULL);
139 srandom(tv.tv_usec);
140
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500141 flower.width = 200;
142 flower.height = 200;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400143 flower.display = d;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500144 flower.window = window_create(d, "flower",
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400145 flower.width, flower.height);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500146
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400147 window_set_decoration(flower.window, 0);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400148 window_draw(flower.window);
149 s = window_get_surface(flower.window);
150 if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) {
151 fprintf(stderr, "failed to create cairo drm surface\n");
152 return -1;
153 }
154
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500155 draw_stuff(s, flower.width, flower.height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400156 cairo_surface_flush(s);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400157 cairo_surface_destroy(s);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400158 window_flush(flower.window);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400159
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500160 window_set_motion_handler(flower.window, motion_handler);
161 window_set_button_handler(flower.window, button_handler);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400162 window_set_user_data(flower.window, &flower);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400163 display_run(d);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400164
165 return 0;
166}