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