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
33 #include <linux/input.h>
34 #include <wayland-client.h>
35 #include <wayland-cursor.h>
39 struct display *display;
40 struct window *window;
41 struct widget *widget;
46 set_random_color(cairo_t *cr)
48 cairo_set_source_rgba(cr,
49 0.5 + (random() % 50) / 49.0,
50 0.5 + (random() % 50) / 49.0,
51 0.5 + (random() % 50) / 49.0,
52 0.5 + (random() % 100) / 99.0);
57 draw_stuff(cairo_surface_t *surface, int width, int height)
59 const int petal_count = 3 + random() % 5;
60 const double r1 = 60 + random() % 35;
61 const double r2 = 20 + random() % 40;
62 const double u = (10 + random() % 90) / 100.0;
63 const double v = (random() % 90) / 100.0;
67 double t, dt = 2 * M_PI / (petal_count * 2);
68 double x1, y1, x2, y2, x3, y3;
70 cr = cairo_create(surface);
71 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
72 cairo_set_source_rgba(cr, 0, 0, 0, 0);
75 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
76 cairo_translate(cr, width / 2, height / 2);
77 cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
78 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
81 x2 = cos(t + dt) * r2;
82 y2 = sin(t + dt) * r2;
83 x3 = cos(t + 2 * dt) * r1;
84 y3 = sin(t + 2 * dt) * r1;
87 x1 - y1 * u, y1 + x1 * u,
88 x2 + y2 * v, y2 - x2 * v,
92 x2 - y2 * v, y2 + x2 * v,
93 x3 + y3 * u, y3 - x3 * u,
99 cairo_fill_preserve(cr);
100 set_random_color(cr);
107 resize_handler(struct widget *widget,
108 int32_t width, int32_t height, void *data)
110 struct flower *flower = data;
113 widget_set_size(flower->widget, flower->width, flower->height);
117 redraw_handler(struct widget *widget, void *data)
119 struct flower *flower = data;
120 cairo_surface_t *surface;
122 surface = window_get_surface(flower->window);
123 if (surface == NULL ||
124 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
125 fprintf(stderr, "failed to create cairo egl surface\n");
129 draw_stuff(surface, flower->width, flower->height);
130 cairo_surface_destroy(surface);
134 motion_handler(struct widget *widget, struct input *input,
135 uint32_t time, float x, float y, void *data)
137 return WL_CURSOR_HAND1;
141 button_handler(struct widget *widget,
142 struct input *input, uint32_t time,
143 uint32_t button, uint32_t state, void *data)
145 struct flower *flower = data;
150 window_move(flower->window, input,
151 display_get_serial(flower->display));
155 widget_schedule_redraw(widget);
159 window_show_frame_menu(flower->window, input, time);
164 int main(int argc, char *argv[])
166 struct flower flower;
170 d = display_create(argc, argv);
172 fprintf(stderr, "failed to create display: %m\n");
176 gettimeofday(&tv, NULL);
182 flower.window = window_create(d);
183 flower.widget = window_add_widget(flower.window, &flower);
185 widget_set_resize_handler(flower.widget, resize_handler);
186 widget_set_redraw_handler(flower.widget, redraw_handler);
187 widget_set_motion_handler(flower.widget, motion_handler);
188 widget_set_button_handler(flower.widget, button_handler);
190 window_schedule_resize(flower.window, flower.width, flower.height);