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