compositor-x11: Rename the output make to "weston-X11"
[platform/upstream/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 "config.h"
24
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <math.h>
31 #include <cairo.h>
32 #include <sys/time.h>
33
34 #include <linux/input.h>
35 #include <wayland-client.h>
36 #include "window.h"
37
38 struct flower {
39         struct display *display;
40         struct window *window;
41         struct widget *widget;
42         int width, height;
43 };
44
45 static void
46 set_random_color(cairo_t *cr)
47 {
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);
53 }
54
55
56 static void
57 draw_stuff(cairo_surface_t *surface, int width, int height)
58 {
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;
64
65         cairo_t *cr;
66         int i;
67         double t, dt = 2 * M_PI / (petal_count * 2);
68         double x1, y1, x2, y2, x3, y3;
69
70         cr = cairo_create(surface);
71         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
72         cairo_set_source_rgba(cr, 0, 0, 0, 0);
73         cairo_paint(cr);
74
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) {
79                 x1 = cos(t) * r1;
80                 y1 = sin(t) * r1;
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;
85
86                 cairo_curve_to(cr,
87                                x1 - y1 * u, y1 + x1 * u,
88                                x2 + y2 * v, y2 - x2 * v,
89                                x2, y2);                        
90
91                 cairo_curve_to(cr,
92                                x2 - y2 * v, y2 + x2 * v,
93                                x3 + y3 * u, y3 - x3 * u,
94                                x3, y3);
95         }
96
97         cairo_close_path(cr);
98         set_random_color(cr);
99         cairo_fill_preserve(cr);
100         set_random_color(cr);
101         cairo_stroke(cr);
102
103         cairo_destroy(cr);
104 }
105
106 static void
107 resize_handler(struct widget *widget,
108                int32_t width, int32_t height, void *data)
109 {
110         struct flower *flower = data;
111
112         /* Dont resize me */
113         widget_set_size(flower->widget, flower->width, flower->height);
114 }
115
116 static void
117 redraw_handler(struct widget *widget, void *data)
118 {
119         struct flower *flower = data;
120         cairo_surface_t *surface;
121
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");
126                 return;
127         }
128
129         draw_stuff(surface, flower->width, flower->height);
130         cairo_surface_destroy(surface);
131 }
132
133 static void
134 button_handler(struct widget *widget,
135                struct input *input, uint32_t time,
136                uint32_t button, enum wl_pointer_button_state state, void *data)
137 {
138         struct flower *flower = data;
139
140         switch (button) {
141         case BTN_LEFT:
142                 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
143                         window_move(flower->window, input,
144                                     display_get_serial(flower->display));
145                 break;
146         case BTN_MIDDLE:
147                 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
148                         widget_schedule_redraw(widget);
149                 break;
150         case BTN_RIGHT:
151                 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
152                         window_show_frame_menu(flower->window, input, time);
153                 break;
154         }
155 }
156
157 static void
158 touch_down_handler(struct widget *widget, struct input *input, 
159                    uint32_t serial, uint32_t time, int32_t id, 
160                    float x, float y, void *data)
161 {
162         struct flower *flower = data;
163         window_move(flower->window, input, display_get_serial(flower->display));
164 }
165
166 int main(int argc, char *argv[])
167 {
168         struct flower flower;
169         struct display *d;
170         struct timeval tv;
171
172         d = display_create(&argc, argv);
173         if (d == NULL) {
174                 fprintf(stderr, "failed to create display: %m\n");
175                 return -1;
176         }
177
178         gettimeofday(&tv, NULL);
179         srandom(tv.tv_usec);
180
181         flower.width = 200;
182         flower.height = 200;
183         flower.display = d;
184         flower.window = window_create(d);
185         flower.widget = window_add_widget(flower.window, &flower);
186         window_set_title(flower.window, "Flower");
187
188         widget_set_resize_handler(flower.widget, resize_handler);
189         widget_set_redraw_handler(flower.widget, redraw_handler);
190         widget_set_button_handler(flower.widget, button_handler);
191         widget_set_default_cursor(flower.widget, CURSOR_HAND1);
192         widget_set_touch_down_handler(flower.widget, touch_down_handler);
193
194         window_schedule_resize(flower.window, flower.width, flower.height);
195
196         display_run(d);
197
198         widget_destroy(flower.widget);
199         window_destroy(flower.window);
200         display_destroy(d);
201
202         return 0;
203 }