blob: ae70d58e5975ee247245458517557f636a8ec229 [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øgsbergc25a1d72012-01-31 09:54:04 -050036#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020037#include <wayland-client.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040038#include "window.h"
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040039
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050040struct flower {
41 struct display *display;
42 struct window *window;
43 struct widget *widget;
44 int width, height;
45};
46
Kristian Høgsberga234e702008-10-11 22:13:51 -040047static void
48set_random_color(cairo_t *cr)
49{
50 cairo_set_source_rgba(cr,
Kristian Høgsbergd311e8a2008-10-12 22:58:40 -040051 0.5 + (random() % 50) / 49.0,
52 0.5 + (random() % 50) / 49.0,
53 0.5 + (random() % 50) / 49.0,
54 0.5 + (random() % 100) / 99.0);
Kristian Høgsberga234e702008-10-11 22:13:51 -040055}
56
57
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050058static void
59draw_stuff(cairo_surface_t *surface, int width, int height)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040060{
Kristian Høgsberga234e702008-10-11 22:13:51 -040061 const int petal_count = 3 + random() % 5;
62 const double r1 = 60 + random() % 35;
63 const double r2 = 20 + random() % 40;
64 const double u = (10 + random() % 90) / 100.0;
65 const double v = (random() % 90) / 100.0;
66
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040067 cairo_t *cr;
Kristian Høgsberga234e702008-10-11 22:13:51 -040068 int i;
69 double t, dt = 2 * M_PI / (petal_count * 2);
70 double x1, y1, x2, y2, x3, y3;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040071
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040072 cr = cairo_create(surface);
Kristian Høgsberg8ca1cc22010-08-30 08:21:44 -040073 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
74 cairo_set_source_rgba(cr, 0, 0, 0, 0);
75 cairo_paint(cr);
76
77 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberga234e702008-10-11 22:13:51 -040078 cairo_translate(cr, width / 2, height / 2);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050079 cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
Kristian Høgsberga234e702008-10-11 22:13:51 -040080 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
81 x1 = cos(t) * r1;
82 y1 = sin(t) * r1;
83 x2 = cos(t + dt) * r2;
84 y2 = sin(t + dt) * r2;
85 x3 = cos(t + 2 * dt) * r1;
86 y3 = sin(t + 2 * dt) * r1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040087
Kristian Høgsberga234e702008-10-11 22:13:51 -040088 cairo_curve_to(cr,
89 x1 - y1 * u, y1 + x1 * u,
90 x2 + y2 * v, y2 - x2 * v,
91 x2, y2);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040092
Kristian Høgsberga234e702008-10-11 22:13:51 -040093 cairo_curve_to(cr,
94 x2 - y2 * v, y2 + x2 * v,
95 x3 + y3 * u, y3 - x3 * u,
96 x3, y3);
97 }
98
99 cairo_close_path(cr);
100 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400101 cairo_fill_preserve(cr);
Kristian Høgsberga234e702008-10-11 22:13:51 -0400102 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400103 cairo_stroke(cr);
104
105 cairo_destroy(cr);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400106}
107
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500108static void
Kristian Høgsberg8e054f72012-01-31 11:53:20 -0500109resize_handler(struct widget *widget,
110 int32_t width, int32_t height, void *data)
111{
112 struct flower *flower = data;
113
114 /* Dont resize me */
115 widget_set_size(flower->widget, flower->width, flower->height);
116}
117
118static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500119redraw_handler(struct widget *widget, void *data)
120{
121 struct flower *flower = data;
122 cairo_surface_t *surface;
123
124 surface = window_get_surface(flower->window);
125 if (surface == NULL ||
126 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
127 fprintf(stderr, "failed to create cairo egl surface\n");
128 return;
129 }
130
131 draw_stuff(surface, flower->width, flower->height);
132 cairo_surface_destroy(surface);
133}
134
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500135static int
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500136motion_handler(struct widget *widget, struct input *input,
137 uint32_t time, int32_t x, int32_t y, void *data)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500138{
139 return POINTER_HAND1;
140}
141
142static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500143button_handler(struct widget *widget,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500144 struct input *input, uint32_t time,
145 int button, int state, void *data)
146{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500147 struct flower *flower = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500148
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500149 switch (button) {
150 case BTN_LEFT:
151 if (state)
152 window_move(flower->window, input, time);
153 break;
154 case BTN_MIDDLE:
155 if (state)
156 widget_schedule_redraw(widget);
157 break;
158 case BTN_RIGHT:
159 if (state)
160 window_show_frame_menu(flower->window, input, time);
161 break;
162 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500163}
164
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400165int main(int argc, char *argv[])
166{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500167 struct flower flower;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400168 struct display *d;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500169 struct timeval tv;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400170
Kristian Høgsberg9de79a92011-08-24 11:09:53 -0400171 d = display_create(&argc, &argv, NULL);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200172 if (d == NULL) {
173 fprintf(stderr, "failed to create display: %m\n");
174 return -1;
175 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400176
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500177 gettimeofday(&tv, NULL);
178 srandom(tv.tv_usec);
179
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500180 flower.width = 200;
181 flower.height = 200;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400182 flower.display = d;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500183 flower.window = window_create(d, flower.width, flower.height);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500184 flower.widget = window_add_widget(flower.window, &flower);
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500185
Kristian Høgsberg8e054f72012-01-31 11:53:20 -0500186 widget_set_resize_handler(flower.widget, resize_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500187 widget_set_redraw_handler(flower.widget, redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500188 widget_set_motion_handler(flower.widget, motion_handler);
189 widget_set_button_handler(flower.widget, button_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500190
191 window_schedule_redraw(flower.window);
192
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400193 display_run(d);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400194
195 return 0;
196}