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