5623f5e7076a01ccc48e5dfa336dd6128e31f611
[profile/ivi/weston.git] / clients / flower.c
1 /*
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
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <math.h>
31 #include <time.h>
32 #include <cairo.h>
33 #include <sys/time.h>
34 #include <glib.h>
35
36 #include <wayland-client.h>
37 #include "window.h"
38
39 struct flower {
40         struct display *display;
41         struct window *window;
42         struct widget *widget;
43         int width, height;
44 };
45
46 static void
47 set_random_color(cairo_t *cr)
48 {
49         cairo_set_source_rgba(cr,
50                               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);
54 }
55
56
57 static void
58 draw_stuff(cairo_surface_t *surface, int width, int height)
59 {
60         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
66         cairo_t *cr;
67         int i;
68         double t, dt = 2 * M_PI / (petal_count * 2);
69         double x1, y1, x2, y2, x3, y3;
70
71         cr = cairo_create(surface);
72         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);
77         cairo_translate(cr, width / 2, height / 2);
78         cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
79         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;
86
87                 cairo_curve_to(cr,
88                                x1 - y1 * u, y1 + x1 * u,
89                                x2 + y2 * v, y2 - x2 * v,
90                                x2, y2);                        
91
92                 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);
100         cairo_fill_preserve(cr);
101         set_random_color(cr);
102         cairo_stroke(cr);
103
104         cairo_destroy(cr);
105 }
106
107 static void
108 redraw_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
124 static int
125 motion_handler(struct widget *widget, struct input *input,
126                uint32_t time, int32_t x, int32_t y, void *data)
127 {
128         return POINTER_HAND1;
129 }
130
131 static void
132 button_handler(struct widget *widget,
133                struct input *input, uint32_t time,
134                int button, int state, void *data)
135 {
136         struct flower *flower = data;
137
138         if (state)
139                 window_move(flower->window, input, time);
140 }
141
142 int main(int argc, char *argv[])
143 {
144         struct flower flower;
145         struct display *d;
146         struct timeval tv;
147
148         d = display_create(&argc, &argv, NULL);
149         if (d == NULL) {
150                 fprintf(stderr, "failed to create display: %m\n");
151                 return -1;
152         }
153
154         gettimeofday(&tv, NULL);
155         srandom(tv.tv_usec);
156
157         flower.width = 200;
158         flower.height = 200;
159         flower.display = d;
160         flower.window = window_create(d, flower.width, flower.height);
161         flower.widget = window_add_widget(flower.window, &flower);
162
163         widget_set_redraw_handler(flower.widget, redraw_handler);
164         widget_set_motion_handler(flower.widget, motion_handler);
165         widget_set_button_handler(flower.widget, button_handler);
166
167         window_schedule_redraw(flower.window);
168
169         display_run(d);
170
171         return 0;
172 }