toytoolkit: Don't draw shadows for maximized windows.
[profile/ivi/weston.git] / clients / smoke.c
1 /*
2  * Copyright © 2010 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
32 #include <wayland-client.h>
33 #include "window.h"
34
35 struct smoke {
36         struct display *display;
37         struct window *window;
38         struct widget *widget;
39         int width, height;
40         int offset, current;
41         uint32_t time;
42         struct { float *d, *u, *v; } b[2];
43 };
44
45 static void diffuse(struct smoke *smoke, uint32_t time,
46                     float *source, float *dest)
47 {
48         float *s, *d;
49         int x, y, k, stride;
50         float t, a = 0.0002;
51
52         stride = smoke->width;
53
54         for (k = 0; k < 5; k++) {
55                 for (y = 1; y < smoke->height - 1; y++) {
56                         s = source + y * stride;
57                         d = dest + y * stride;
58                         for (x = 1; x < smoke->width - 1; x++) {
59                                 t = d[x - 1] + d[x + 1] +
60                                         d[x - stride] + d[x + stride];
61                                 d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995;
62                         }
63                 }
64         }
65 }
66
67 static void advect(struct smoke *smoke, uint32_t time,
68                    float *uu, float *vv, float *source, float *dest)
69 {
70         float *s, *d;
71         float *u, *v;
72         int x, y, stride;
73         int i, j;
74         float px, py, fx, fy;
75
76         stride = smoke->width;
77
78         for (y = 1; y < smoke->height - 1; y++) {
79                 d = dest + y * stride;
80                 u = uu + y * stride;
81                 v = vv + y * stride;
82
83                 for (x = 1; x < smoke->width - 1; x++) {
84                         px = x - u[x];
85                         py = y - v[x];
86                         if (px < 0.5)
87                                 px = 0.5;
88                         if (py < 0.5)
89                                 py = 0.5;
90                         if (px > smoke->width - 0.5)
91                                 px = smoke->width - 0.5;
92                         if (py > smoke->height - 0.5)
93                                 py = smoke->height - 0.5;
94                         i = (int) px;
95                         j = (int) py;
96                         fx = px - i;
97                         fy = py - j;
98                         s = source + j * stride + i;
99                         d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) +
100                                 (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy;
101                 }
102         }
103 }
104
105 static void project(struct smoke *smoke, uint32_t time,
106                     float *u, float *v, float *p, float *div)
107 {
108         int x, y, k, l, s;
109         float h;
110
111         h = 1.0 / smoke->width;
112         s = smoke->width;
113         memset(p, 0, smoke->height * smoke->width);
114         for (y = 1; y < smoke->height - 1; y++) {
115                 l = y * s;
116                 for (x = 1; x < smoke->width - 1; x++) {
117                         div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] +
118                                                  v[l + x + s] - v[l + x - s]);
119                         p[l + x] = 0;
120                 }
121         }
122
123         for (k = 0; k < 5; k++) {
124                 for (y = 1; y < smoke->height - 1; y++) {
125                         l = y * s;
126                         for (x = 1; x < smoke->width - 1; x++) {
127                                 p[l + x] = (div[l + x] +
128                                             p[l + x - 1] +
129                                             p[l + x + 1] +
130                                             p[l + x - s] +
131                                             p[l + x + s]) / 4;
132                         }
133                 }
134         }
135
136         for (y = 1; y < smoke->height - 1; y++) {
137                 l = y * s;
138                 for (x = 1; x < smoke->width - 1; x++) {
139                         u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h;
140                         v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h;
141                 }
142         }
143 }
144
145 static void render(struct smoke *smoke, cairo_surface_t *surface)
146 {
147         unsigned char *dest;
148         int x, y, width, height, stride;
149         float *s;
150         uint32_t *d, c, a;
151
152         dest = cairo_image_surface_get_data(surface);
153         width = cairo_image_surface_get_width(surface);
154         height = cairo_image_surface_get_height(surface);
155         stride = cairo_image_surface_get_stride(surface);
156
157         for (y = 1; y < height - 1; y++) {
158                 s = smoke->b[smoke->current].d + y * smoke->height;
159                 d = (uint32_t *) (dest + y * stride);
160                 for (x = 1; x < width - 1; x++) {
161                         c = (int) (s[x] * 800);
162                         if (c > 255)
163                                 c = 255;
164                         a = c;
165                         if (a < 0x33)
166                                 a = 0x33;
167                         d[x] = (a << 24) | (c << 16) | (c << 8) | c;
168                 }
169         }
170 }
171
172 static void
173 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
174 {
175         struct smoke *smoke = data;
176
177         window_schedule_redraw(smoke->window);
178         smoke->time = time;
179
180         if (callback)
181                 wl_callback_destroy(callback);
182 }
183
184 static const struct wl_callback_listener listener = {
185         frame_callback,
186 };
187
188 static void
189 redraw_handler(struct widget *widget, void *data)
190 {
191         struct smoke *smoke = data;
192         uint32_t time = smoke->time;
193         struct wl_callback *callback;
194         cairo_surface_t *surface;
195
196         diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u);
197         diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v);
198         project(smoke, time / 30,
199                 smoke->b[1].u, smoke->b[1].v,
200                 smoke->b[0].u, smoke->b[0].v);
201         advect(smoke, time / 30,
202                smoke->b[1].u, smoke->b[1].v,
203                smoke->b[1].u, smoke->b[0].u);
204         advect(smoke, time / 30,
205                smoke->b[1].u, smoke->b[1].v,
206                smoke->b[1].v, smoke->b[0].v);
207         project(smoke, time / 30,
208                 smoke->b[0].u, smoke->b[0].v,
209                 smoke->b[1].u, smoke->b[1].v);
210
211         diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d);
212         advect(smoke, time / 30,
213                smoke->b[0].u, smoke->b[0].v,
214                smoke->b[1].d, smoke->b[0].d);
215
216         surface = window_get_surface(smoke->window);
217
218         render(smoke, surface);
219
220         window_damage(smoke->window, 0, 0, smoke->width, smoke->height);
221
222         cairo_surface_destroy(surface);
223
224         callback = wl_surface_frame(window_get_wl_surface(smoke->window));
225         wl_callback_add_listener(callback, &listener, smoke);
226 }
227
228 static int
229 smoke_motion_handler(struct widget *widget, struct input *input,
230                      uint32_t time, float x, float y, void *data)
231 {
232         struct smoke *smoke = data;
233         int i, i0, i1, j, j0, j1, k, d = 5;
234
235         if (x - d < 1)
236                 i0 = 1;
237         else
238                 i0 = x - d;
239         if (i0 + 2 * d > smoke->width - 1)
240                 i1 = smoke->width - 1;
241         else
242                 i1 = i0 + 2 * d;
243
244         if (y - d < 1)
245                 j0 = 1;
246         else
247                 j0 = y - d;
248         if (j0 + 2 * d > smoke->height - 1)
249                 j1 = smoke->height - 1;
250         else
251                 j1 = j0 + 2 * d;
252
253         for (i = i0; i < i1; i++)
254                 for (j = j0; j < j1; j++) {
255                         k = j * smoke->width + i;
256                         smoke->b[0].u[k] += 256 - (random() & 512);
257                         smoke->b[0].v[k] += 256 - (random() & 512);
258                         smoke->b[0].d[k] += 1;
259                 }
260
261         return CURSOR_HAND1;
262 }
263
264 static void
265 resize_handler(struct widget *widget,
266                int32_t width, int32_t height, void *data)
267 {
268         struct smoke *smoke = data;
269
270         /* Dont resize me */
271         widget_set_size(smoke->widget, smoke->width, smoke->height);
272 }
273
274 int main(int argc, char *argv[])
275 {
276         struct timespec ts;
277         struct smoke smoke;
278         struct display *d;
279         int size;
280
281         d = display_create(argc, argv);
282         if (d == NULL) {
283                 fprintf(stderr, "failed to create display: %m\n");
284                 return -1;
285         }
286
287         smoke.width = 200;
288         smoke.height = 200;
289         smoke.display = d;
290         smoke.window = window_create(d);
291         smoke.widget = window_add_widget(smoke.window, &smoke);
292         window_set_title(smoke.window, "smoke");
293
294         window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
295         clock_gettime(CLOCK_MONOTONIC, &ts);
296         srandom(ts.tv_nsec);
297         smoke.offset = random();
298
299         smoke.current = 0;
300         size = smoke.height * smoke.width;
301         smoke.b[0].d = calloc(size, sizeof(float));
302         smoke.b[0].u = calloc(size, sizeof(float));
303         smoke.b[0].v = calloc(size, sizeof(float));
304         smoke.b[1].d = calloc(size, sizeof(float));
305         smoke.b[1].u = calloc(size, sizeof(float));
306         smoke.b[1].v = calloc(size, sizeof(float));
307
308         widget_set_motion_handler(smoke.widget, smoke_motion_handler);
309         widget_set_resize_handler(smoke.widget, resize_handler);
310         widget_set_redraw_handler(smoke.widget, redraw_handler);
311
312         window_set_user_data(smoke.window, &smoke);
313
314         widget_schedule_resize(smoke.widget, smoke.width, smoke.height);
315
316         display_run(d);
317
318         return 0;
319 }