2 * Copyright © 2008 Kristian Høgsberg
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.
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
36 #include <linux/input.h>
37 #include <wayland-client.h>
41 struct display *display;
42 struct window *window;
43 struct widget *widget;
48 set_random_color(cairo_t *cr)
50 cairo_set_source_rgba(cr,
51 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);
59 draw_stuff(cairo_surface_t *surface, int width, int height)
61 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;
69 double t, dt = 2 * M_PI / (petal_count * 2);
70 double x1, y1, x2, y2, x3, y3;
72 cr = cairo_create(surface);
73 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
74 cairo_set_source_rgba(cr, 0, 0, 0, 0);
77 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
78 cairo_translate(cr, width / 2, height / 2);
79 cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
80 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
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;
89 x1 - y1 * u, y1 + x1 * u,
90 x2 + y2 * v, y2 - x2 * v,
94 x2 - y2 * v, y2 + x2 * v,
95 x3 + y3 * u, y3 - x3 * u,
100 set_random_color(cr);
101 cairo_fill_preserve(cr);
102 set_random_color(cr);
109 resize_handler(struct widget *widget,
110 int32_t width, int32_t height, void *data)
112 struct flower *flower = data;
115 widget_set_size(flower->widget, flower->width, flower->height);
119 redraw_handler(struct widget *widget, void *data)
121 struct flower *flower = data;
122 cairo_surface_t *surface;
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");
131 draw_stuff(surface, flower->width, flower->height);
132 cairo_surface_destroy(surface);
136 motion_handler(struct widget *widget, struct input *input,
137 uint32_t time, int32_t x, int32_t y, void *data)
139 return POINTER_HAND1;
143 button_handler(struct widget *widget,
144 struct input *input, uint32_t time,
145 uint32_t button, uint32_t state, void *data)
147 struct flower *flower = data;
152 window_move(flower->window, input,
153 display_get_serial(flower->display));
157 widget_schedule_redraw(widget);
161 window_show_frame_menu(flower->window, input, time);
166 int main(int argc, char *argv[])
168 struct flower flower;
172 d = display_create(argc, argv);
174 fprintf(stderr, "failed to create display: %m\n");
178 gettimeofday(&tv, NULL);
184 flower.window = window_create(d);
185 flower.widget = window_add_widget(flower.window, &flower);
187 widget_set_resize_handler(flower.widget, resize_handler);
188 widget_set_redraw_handler(flower.widget, redraw_handler);
189 widget_set_motion_handler(flower.widget, motion_handler);
190 widget_set_button_handler(flower.widget, button_handler);
192 window_schedule_resize(flower.window, flower.width, flower.height);