client: Fix shell unstable version check
[profile/ivi/weston-ivi-shell.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 <math.h>
29 #include <cairo.h>
30 #include <sys/time.h>
31
32 #include <linux/input.h>
33 #include <wayland-client.h>
34 #include "window.h"
35
36 struct flower {
37         struct display *display;
38         struct window *window;
39         struct widget *widget;
40         int width, height;
41 };
42
43 static void
44 set_random_color(cairo_t *cr)
45 {
46         cairo_set_source_rgba(cr,
47                               0.5 + (random() % 50) / 49.0,
48                               0.5 + (random() % 50) / 49.0,
49                               0.5 + (random() % 50) / 49.0,
50                               0.5 + (random() % 100) / 99.0);
51 }
52
53
54 static void
55 draw_stuff(cairo_surface_t *surface, int width, int height)
56 {
57         const int petal_count = 3 + random() % 5;
58         const double r1 = 60 + random() % 35;
59         const double r2 = 20 + random() % 40;
60         const double u = (10 + random() % 90) / 100.0;
61         const double v = (random() % 90) / 100.0;
62
63         cairo_t *cr;
64         int i;
65         double t, dt = 2 * M_PI / (petal_count * 2);
66         double x1, y1, x2, y2, x3, y3;
67
68         cr = cairo_create(surface);
69         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
70         cairo_set_source_rgba(cr, 0, 0, 0, 0);
71         cairo_paint(cr);
72
73         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
74         cairo_translate(cr, width / 2, height / 2);
75         cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
76         for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
77                 x1 = cos(t) * r1;
78                 y1 = sin(t) * r1;
79                 x2 = cos(t + dt) * r2;
80                 y2 = sin(t + dt) * r2;
81                 x3 = cos(t + 2 * dt) * r1;
82                 y3 = sin(t + 2 * dt) * r1;
83
84                 cairo_curve_to(cr,
85                                x1 - y1 * u, y1 + x1 * u,
86                                x2 + y2 * v, y2 - x2 * v,
87                                x2, y2);                        
88
89                 cairo_curve_to(cr,
90                                x2 - y2 * v, y2 + x2 * v,
91                                x3 + y3 * u, y3 - x3 * u,
92                                x3, y3);
93         }
94
95         cairo_close_path(cr);
96         set_random_color(cr);
97         cairo_fill_preserve(cr);
98         set_random_color(cr);
99         cairo_stroke(cr);
100
101         cairo_destroy(cr);
102 }
103
104 static void
105 resize_handler(struct widget *widget,
106                int32_t width, int32_t height, void *data)
107 {
108         struct flower *flower = data;
109
110         /* Dont resize me */
111         widget_set_size(flower->widget, flower->width, flower->height);
112 }
113
114 static void
115 redraw_handler(struct widget *widget, void *data)
116 {
117         struct flower *flower = data;
118         cairo_surface_t *surface;
119
120         surface = window_get_surface(flower->window);
121         if (surface == NULL ||
122             cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
123                 fprintf(stderr, "failed to create cairo egl surface\n");
124                 return;
125         }
126
127         draw_stuff(surface, flower->width, flower->height);
128         cairo_surface_destroy(surface);
129 }
130
131 static void
132 button_handler(struct widget *widget,
133                struct input *input, uint32_t time,
134                uint32_t button, enum wl_pointer_button_state state, void *data)
135 {
136         struct flower *flower = data;
137
138         switch (button) {
139         case BTN_LEFT:
140                 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
141                         window_move(flower->window, input,
142                                     display_get_serial(flower->display));
143                 break;
144         case BTN_MIDDLE:
145                 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
146                         widget_schedule_redraw(widget);
147                 break;
148         case BTN_RIGHT:
149                 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
150                         window_show_frame_menu(flower->window, input, time);
151                 break;
152         }
153 }
154
155 static void
156 touch_down_handler(struct widget *widget, struct input *input, 
157                    uint32_t serial, uint32_t time, int32_t id, 
158                    float x, float y, void *data)
159 {
160         struct flower *flower = data;
161         window_move(flower->window, input, display_get_serial(flower->display));
162 }
163
164 int main(int argc, char *argv[])
165 {
166         struct flower flower;
167         struct display *d;
168         struct timeval tv;
169
170         d = display_create(&argc, argv);
171         if (d == NULL) {
172                 fprintf(stderr, "failed to create display: %m\n");
173                 return -1;
174         }
175
176         gettimeofday(&tv, NULL);
177         srandom(tv.tv_usec);
178
179         flower.width = 200;
180         flower.height = 200;
181         flower.display = d;
182         flower.window = window_create(d);
183         flower.widget = window_add_widget(flower.window, &flower);
184         window_set_title(flower.window, "Flower");
185
186         widget_set_resize_handler(flower.widget, resize_handler);
187         widget_set_redraw_handler(flower.widget, redraw_handler);
188         widget_set_button_handler(flower.widget, button_handler);
189         widget_set_default_cursor(flower.widget, CURSOR_HAND1);
190         widget_set_touch_down_handler(flower.widget, touch_down_handler);
191
192         window_schedule_resize(flower.window, flower.width, flower.height);
193
194         display_run(d);
195
196         return 0;
197 }