summaryrefslogtreecommitdiff
path: root/clients/smoke-buffers.c
blob: 91a42d7357e03a443ad84e094b8b50c0acf3f3c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
/*
 * Copyright © 2010 Kristian Høgsberg
 * Copyright © 2013-2014 Collabora, Ltd.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <time.h>
#include <cairo.h>
#include <pthread.h>
#include "window.h"

#include <wayland-client.h>
#include "videosurface-client-protocol.h"

#define SMOKE_NB_POOL_BUFFERS 30

struct display {
	struct wl_display *display;
	struct wl_registry *registry;
	struct wl_compositor *compositor;
	struct wl_videocompositor *videocompositor;
	struct wl_shell *shell;
	struct wl_shm *shm;
	uint32_t formats;
};

struct Window {
	struct display *display;
	int width, height;
	struct wl_surface *surface;
	struct wl_shell_surface *shell_surface;
	struct wl_videosurface *video_surface;
	struct buffer buffers[SMOKE_NB_POOL_BUFFERS];
	struct wl_list free_buffers_list;
	cairo_surface_t *surfaces[SMOKE_NB_POOL_BUFFERS];
	struct wl_callback *callback;
	void *user_data;
};

struct buffer {
	struct wl_list link;
	struct wl_list *free_buffers_list;
	struct wl_buffer *buffer;
	struct smoke *smoke;
	cairo_surface_t *surface;
	void *map;
};

struct shm_pool {
	struct wl_shm_pool *pool;
	size_t size;
	size_t used;
	void *data;
};

struct smoke {
	struct display *display;
	struct Window *window;
	struct shm_pool *pool;
	uint32_t cur_time, start_time, anim;
	struct { float *d, *u, *v; } b[2];
	pthread_t smoker_thread;
	pthread_mutex_t smoke_mutex;
};

/* forward declarations */
struct smoke;
static void display_add_input(struct display *d, uint32_t id);

static void
buffer_release(void *data, struct wl_buffer *buffer)
{
	struct buffer *buf = data;

	wl_list_insert(buf->free_buffers_list, &buf->link);
}

static const struct wl_buffer_listener buffer_listener = {
	buffer_release
};

//static const struct wl_videosurface_listener video_surface_listener = {
	/* Write the event listeners here when we've got some */
//};

static void
handle_ping(void *data, struct wl_shell_surface *shell_surface,
							uint32_t serial)
{
	wl_shell_surface_pong(shell_surface, serial);
}

static void
handle_configure(void *data, struct wl_shell_surface *shell_surface,
		 uint32_t edges, int32_t width, int32_t height)
{
}

static void
handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
{
}

static const struct wl_shell_surface_listener shell_surface_listener = {
	handle_ping,
	handle_configure,
	handle_popup_done
};

static struct Window *
create_window(struct display *display, int width, int height)
{
	struct Window *window;

	window = calloc(1, sizeof *window);
	if (!window)
		return NULL;

	window->callback = NULL;
	window->display = display;
	window->width = width;
	window->height = height;
	window->surface = wl_compositor_create_surface(display->compositor);

	if (window->surface) {

		window->video_surface = wl_videocompositor_get_videosurface(
									display->videocompositor, window->surface);
		/* TODO:
		 * Someday, we'll have some events to listen to...

		if (window->video_surface)
			wl_video_surface_add_listener(window->video_surface,
										  &video_surface_listener, window);
		 */

		window->shell_surface = wl_shell_get_shell_surface(display->shell,
								   window->surface);

		if (window->shell_surface)
			wl_shell_surface_add_listener(window->shell_surface,
							  &shell_surface_listener, window);

		wl_shell_surface_set_title(window->shell_surface, "Smoky buffer queues");
		wl_shell_surface_set_toplevel(window->shell_surface);
	}

	return window;
}

static void
destroy_window(struct window *window, struct smoke *smoke)
{
	int i;

	if (window->callback)
		wl_callback_destroy(window->callback);

	for (i = 0; i < SMOKE_NB_POOL_BUFFERS; i++) {
		if (smoke->buffers[i].buffer)
			wl_buffer_destroy(smoke->buffers[i].buffer);
	}

	wl_shell_surface_destroy(window->shell_surface);
	wl_surface_destroy(window->surface);
	free(window);
}

static void
shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
{
	struct display *d = data;

	d->formats |= (1 << format);
}

struct wl_shm_listener shm_listenter = {
	shm_format
};

static void
registry_handle_global(void *data, struct wl_registry *registry,
		       uint32_t id, const char *interface, uint32_t version)
{
	struct display *d = data;

	if (strcmp(interface, "wl_compositor") == 0) {
		d->compositor =
			wl_registry_bind(registry,
					 id, &wl_compositor_interface, 1);
	} else if (strcmp(interface, "wl_shell") == 0) {
		d->shell = wl_registry_bind(registry,
					    id, &wl_shell_interface, 1);
	} else if (strcmp(interface, "wl_seat") == 0) {
		display_add_input(d, id);
	} else if (strcmp(interface, "wl_shm") == 0) {
		d->shm = wl_registry_bind(registry,
					  id, &wl_shm_interface, 1);
		wl_shm_add_listener(d->shm, &shm_listenter, d);
	} else if (strcmp(interface, "wl_videocompositor") == 0) {
		d->videocompositor =
			wl_registry_bind(registry, id,
					 &wl_videocompositor_interface, 1);
	}
}

static void
registry_handle_global_remove(void *data, struct wl_registry *registry,
			      uint32_t name)
{
}

static const struct wl_registry_listener registry_listener = {
	registry_handle_global,
	registry_handle_global_remove
};

static struct display *
create_display(void)
{
	struct display *display;

	display = malloc(sizeof *display);
	display->display = wl_display_connect(NULL);
	assert(display->display);

	display->formats = 0;
	display->registry = wl_display_get_registry(display->display);
	wl_registry_add_listener(display->registry,
				 &registry_listener, display);
	wl_display_roundtrip(display->display);
	if (display->shm == NULL) {
		fprintf(stderr, "No wl_shm global\n");
		exit(1);
	}

	wl_display_roundtrip(display->display);

	if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
		fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
		exit(1);
	}

	wl_display_get_fd(display->display);
	
	return display;
}

static void
destroy_display(struct display *display)
{
	if (display->shm)
		wl_shm_destroy(display->shm);

	if (display->shell)
		wl_shell_destroy(display->shell);

	if (display->compositor)
		wl_compositor_destroy(display->compositor);

	wl_registry_destroy(display->registry);
	wl_display_flush(display->display);
	wl_display_disconnect(display->display);
	free(display);
}

static void diffuse(struct smoke *smoke, uint32_t time,
		    float *source, float *dest)
{
	/* time is NOT used atm... but should be set to 
	 * the time in msecs since last render... that will
	 * let us "scale" the animation time (if not, it will be too fast) */
	float *s, *d;
	int x, y, k, stride;
	float t, a = 0.0002;

	stride = smoke->width;

	for (k = 0; k < 5; k++) {
		for (y = 1; y < smoke->height - 1; y++) {
			s = source + y * stride;
			d = dest + y * stride;
			for (x = 1; x < smoke->width - 1; x++) {
				t = d[x - 1] + d[x + 1] +
					d[x - stride] + d[x + stride];
				d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995;
			}
		}
	}
}

static void advect(struct smoke *smoke, uint32_t time,
		   float *uu, float *vv, float *source, float *dest)
{
	/* time is NOT used atm... but should be set to 
	 * the time in msecs since last render... that will
	 * let us "scale" the animation time (if not, it will be too fast) */
	float *s, *d;
	float *u, *v;
	int x, y, stride;
	int i, j;
	float px, py, fx, fy;

	stride = smoke->width;

	for (y = 1; y < smoke->height - 1; y++) {
		d = dest + y * stride;
		u = uu + y * stride;
		v = vv + y * stride;

		for (x = 1; x < smoke->width - 1; x++) {
			px = x - u[x];
			py = y - v[x];
			if (px < 0.5)
				px = 0.5;
			if (py < 0.5)
				py = 0.5;
			if (px > smoke->width - 0.5)
				px = smoke->width - 0.5;
			if (py > smoke->height - 0.5)
				py = smoke->height - 0.5;
			i = (int) px;
			j = (int) py;
			fx = px - i;
			fy = py - j;
			s = source + j * stride + i;
			d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) +
				(s[stride] * (1 - fx) + s[stride + 1] * fx) * fy;
		}
	}
}

static void project(struct smoke *smoke, uint32_t time,
		    float *u, float *v, float *p, float *div)
{
	/* time is NOT used atm... but should be set to 
	 * the time in msecs since last render... that will
	 * let us "scale" the animation time (if not, it will be too fast) */
	int x, y, k, l, s;
	float h;

	h = 1.0 / smoke->width;
	s = smoke->width;
	memset(p, 0, smoke->height * smoke->width);
	for (y = 1; y < smoke->height - 1; y++) {
		l = y * s;
		for (x = 1; x < smoke->width - 1; x++) {
			div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] +
						 v[l + x + s] - v[l + x - s]);
			p[l + x] = 0;
		}
	}

	for (k = 0; k < 5; k++) {
		for (y = 1; y < smoke->height - 1; y++) {
			l = y * s;
			for (x = 1; x < smoke->width - 1; x++) {
				p[l + x] = (div[l + x] +
					    p[l + x - 1] +
					    p[l + x + 1] +
					    p[l + x - s] +
					    p[l + x + s]) / 4;
			}
		}
	}

	for (y = 1; y < smoke->height - 1; y++) {
		l = y * s;
		for (x = 1; x < smoke->width - 1; x++) {
			u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h;
			v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h;
		}
	}
}

static void render(struct smoke *smoke, cairo_surface_t *surface)
{
	unsigned char *dest;
	int x, y, width, height, stride;
	float *s;
	uint32_t *d, c, a;

	dest = cairo_image_surface_get_data(surface);
	width = cairo_image_surface_get_width(surface);
	height = cairo_image_surface_get_height(surface);
	stride = cairo_image_surface_get_stride(surface);

	for (y = 1; y < height - 1; y++) {
		s = smoke->b[0].d + y * smoke->height;
		d = (uint32_t *) (dest + y * stride);
		for (x = 1; x < width - 1; x++) {
			c = (int) (s[x] * 800);
			if (c > 255)
				c = 255;
			a = c;
			if (a < 0x33)
				a = 0x33;
			d[x] = (a << 24) | (c << 16) | (c << 8) | c;
		}
	}
}

static const struct wl_callback_listener frame_listener;

static void
window_set_user_data(struct window *window, void *data)
{
	window->user_data = data;
}

static void *
window_get_user_data(struct window *window)
{
	return window->user_data;
}

static void
frame_handle_redraw(void *data, struct wl_callback *callback, uint32_t time)
{
	/* TODO: This will mostly all go away (except for time udpate)
	 *       and be relocated inside a idle-spinning loop (another thread)
	 */
	struct window *window = data;
	//struct smoke *smoke = (struct smoke*) window_get_user_data(window);

	/* The rendering part was here formely */

	if (callback)
		wl_callback_destroy(callback);

	window->callback = wl_surface_frame(window->surface);
	wl_callback_add_listener(window->callback, &frame_listener, window);
}

static const struct wl_callback_listener frame_listener = {
	frame_handle_redraw
};

static void
seat_handle_capabilities(void *data, struct wl_seat *seat,
			 enum wl_seat_capability caps)
{
	struct input *input = data;

	/* We are only interested in POINTER events */
	if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
		input->pointer = wl_seat_get_pointer(seat);
		wl_pointer_set_user_data(input->pointer, input);
		wl_pointer_add_listener(input->pointer, &pointer_listener,
					input);
	} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
		wl_pointer_destroy(input->pointer);
		input->pointer = NULL;
	}
}

static const struct wl_seat_listener seat_listener = {
	seat_handle_capabilities,
};


static void
pointer_handle_enter(void *data, struct wl_pointer *pointer,
		     uint32_t serial, struct wl_surface *surface,
		     wl_fixed_t sx_w, wl_fixed_t sy_w)
{
}

static void
pointer_handle_leave(void *data, struct wl_pointer *pointer,
		     uint32_t serial, struct wl_surface *surface)
{
}

static void
pointer_handle_motion(void *data, struct wl_pointer *pointer,
		      uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
{
	struct input *input = data;
	struct window *window = input->pointer_focus;
	struct smoke *smoke = (struct smoke*) window_get_user_data(window);
	int i, i0, i1, j, j0, j1, k, d = 5;
	float sx = wl_fixed_to_double(sx_w);
	float sy = wl_fixed_to_double(sy_w);

	if (!window)
		return;

	/* TODO:
	 * let's TAKE OWNERSHIP of a MUTEX here on the values used by both
	 * motion_handler and render in the smoker thread. We have to prevent
	 * the two from reading/updating b[x] at the same time...
	 * so I guess just a nice pthread_mutex_lock/unlock will do the trick
	 * ...
	 * ...
	 */

	/* We clear the buffer queue */
	wl_videosurface_clear_queue(window->video_surface);

	/* FIXME: Here, add x = sx; y = sx, but in a 3-equality style */
	input->sx = sx;
	input->sy = sy;

	if (x - d < 1)
		i0 = 1;
	else
		i0 = x - d;
	if (i0 + 2 * d > smoke->width - 1)
		i1 = smoke->width - 1;
	else
		i1 = i0 + 2 * d;

	if (y - d < 1)
		j0 = 1;
	else
		j0 = y - d;
	if (j0 + 2 * d > smoke->height - 1)
		j1 = smoke->height - 1;
	else
		j1 = j0 + 2 * d;

	for (i = i0; i < i1; i++)
		for (j = j0; j < j1; j++) {
			k = j * smoke->width + i;
			smoke->b[0].u[k] += 256 - (random() & 512);
			smoke->b[0].v[k] += 256 - (random() & 512);
			smoke->b[0].d[k] += 1;
		}

	smoke->anim = 0;
}

static void
pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
		      uint32_t time, uint32_t button, uint32_t state_w)
{
}

static void
pointer_handle_axis(void *data, struct wl_pointer *pointer,
		    uint32_t time, uint32_t axis, wl_fixed_t value)
{
}

static const struct wl_pointer_listener pointer_listener = {
	pointer_handle_enter,
	pointer_handle_leave,
	pointer_handle_motion,
	pointer_handle_button,
	pointer_handle_axis,
};

static void
display_add_input(struct display *d, uint32_t id)
{
	struct input *input;

	input = malloc(sizeof *input);
	if (input == NULL)
		return;

	memset(input, 0, sizeof *input);
	input->display = d;
	input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
	input->pointer_focus = NULL;
	input->keyboard_focus = NULL;
	wl_list_insert(d->input_list.prev, &input->link);

	wl_seat_add_listener(input->seat, &seat_listener, input);
	wl_seat_set_user_data(input->seat, input);

	input->data_device =
		wl_data_device_manager_get_data_device(d->data_device_manager,
						       input->seat);
	wl_data_device_add_listener(input->data_device, &data_device_listener,
				    input);

	input->pointer_surface = wl_compositor_create_surface(d->compositor);

	input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
						TFD_CLOEXEC | TFD_NONBLOCK);
	input->repeat_task.run = keyboard_repeat_func;
	display_watch_fd(d, input->repeat_timer_fd,
			 EPOLLIN, &input->repeat_task);
}

static void
input_destroy(struct input *input)
{
	input_remove_keyboard_focus(input);
	input_remove_pointer_focus(input);

	if (input->drag_offer)
		data_offer_destroy(input->drag_offer);

	if (input->selection_offer)
		data_offer_destroy(input->selection_offer);

	wl_data_device_destroy(input->data_device);
	fini_xkb(input);

	wl_surface_destroy(input->pointer_surface);

	wl_list_remove(&input->link);
	wl_seat_destroy(input->seat);
	close(input->repeat_timer_fd);
	free(input);
}

static struct buffer *
get_next_pool_buffer(struct smoke *smoke)
{
	if (!wl_list_empty(&smoke->free_buffers_list))
		return container_of(smoke->free_buffers_list.next, struct buffer, link);
	else
		return NULL;
}

static void *
smoker_thread_run(void *arg)
{
	struct timespec ts;
	struct buffer *buffer;
	uint32_t timestamp;
	struct smoke *smoke = (struct smoke *) arg;
	cairo_surface_t *surface;

	/* TODO:
	 * let's take MUTEX here on the b[] values
	 * ...
	 * ...
	 */

	if (anim == 0) {
		clock_gettime(CLOCK_MONOTONIC, &ts);
		smoke->start_time = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
	}	

	/* time is NOT used atm... but should be set to 
	 * the time in msecs since last render... that will
	 * let us "scale" the animation time (if not, it will be too fast) */

	diffuse(smoke, smoke->cur_time / 30, smoke->b[0].u, smoke->b[1].u);
	diffuse(smoke, smoke->cur_time / 30, smoke->b[0].v, smoke->b[1].v);
	project(smoke, smoke->cur_time / 30,
		smoke->b[1].u, smoke->b[1].v,
		smoke->b[0].u, smoke->b[0].v);
	advect(smoke, smoke->cur_time / 30,
	       smoke->b[1].u, smoke->b[1].v,
	       smoke->b[1].u, smoke->b[0].u);
	advect(smoke, smoke->cur_time / 30,
	       smoke->b[1].u, smoke->b[1].v,
	       smoke->b[1].v, smoke->b[0].v);
	project(smoke, smoke->cur_time / 30,
		smoke->b[0].u, smoke->b[0].v,
		smoke->b[1].u, smoke->b[1].v);

	diffuse(smoke, smoke->cur_time / 30, smoke->b[0].d, smoke->b[1].d);
	advect(smoke, smoke->cur_time / 30,
	       smoke->b[0].u, smoke->b[0].v,
	       smoke->b[1].d, smoke->b[0].d);

	/* Get the next available buffer */
	buffer = get_next_pool_buffer(smoke);

	render(smoke, buffer->surface);

	/* FIXME: This 1000/60 IMPLIES scanout at 60 FPS !!*/
	timestamp = smoke->start_time + (1000 * smoke->anim / 60);
	wl_videosurface_queue_buffer(&smoke->window->video_surface, buffer->buffer, );

	smoke->anim++;
}

/* The smoker thread will render smoke frames as fast as it can
 * and queue buffers in the compositor as they are ready. The output
 * part will then be overtaken by the compositor itself. No need to 
 * .attach or .commit the contents on the window's surface 
 *
 * note : the thread's scheduling policy is IDLE
 */
static int
create_smoker_thread(struct smoke *smoke)
{
	pthread_attr_t attr;
	struct sched_param schedparam;
	int s;

	s = pthread_attr_init(&attr);
	schedparam.sched_priority = 0;
	pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
	
	if (pthread_attr_setschedparam(&attr, &schedparam) != 0) {
		printf("Error! Could not set scheduler parameters\n");
		return -1;
	}

	s = pthread_create(&window->smoker_thread,
					   &attr, smoker_thread_run, (void *) smoke);
	if (s != 0) {
		printf("Error! Could not create smoker thread\n");
		return -1;
	}

	if (pthread_attr_destroy(&attr) != 0) {
		printf("Error! Could not destroy thread attributes\n");
		return -1;
	}

	return 0;
}

static struct wl_shm_pool *
make_shm_pool(struct display *display, int size, void **data)
{
	struct wl_shm_pool *pool;
	int fd;

	fd = os_create_anonymous_file(size);
	if (fd < 0) {
		fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
			size);
		return NULL;
	}

	*data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (*data == MAP_FAILED) {
		fprintf(stderr, "mmap failed: %m\n");
		close(fd);
		return NULL;
	}

	pool = wl_shm_create_pool(display->shm, fd, size);

	close(fd);

	return pool;
}

static struct shm_pool *
shm_pool_create(struct display *display, size_t size)
{
	struct shm_pool *pool = malloc(sizeof *pool);

	if (!pool)
		return NULL;

	pool->pool = make_shm_pool(display, size, &pool->data);
	if (!pool->pool) {
		free(pool);
		return NULL;
	}

	pool->size = size;
	pool->used = 0;

	return pool;
}

static int
data_length_for_shm_surface(struct rectangle *rect)
{
	int stride;

	stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
						rect->width);
	return stride * rect->height;
}

static int
create_shm_pool(struct smoke *smoke)
{
	int i, ret = 0;
	struct rect;
	struct shm_pool *pool;
	cairo_surface_t *surface;
	uint32_t offset;
	int stride;

	rect.width  = smoke->width;
	rect.height = smoke->height;

	stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, rect.width);
	pool = shm_pool_create(smoke->display, SMOKE_NB_POOL_BUFFERS * stride * rect.height);
	if (!pool)
		ret = 1;
	else {
		for (i = 0; i < SMOKE_NB_POOL_BUFFERS; i++) {
			offset = i * rect.width * rect.height;
			smoke->buffers[i].smoke = smoke;
			smoke->buffers[i].buffer = wl_shm_pool_create_buffer(pool->pool,
										offset,	rect.width,
										rect.height, stride, WL_SHM_FORMAT_XRGB8888);
			smoke->buffers[i].map  = (void *) pool->data + offset;
			smoke->buffers[i].free_buffers_list = &smoke->free_buffers_list;
			wl_list_insert(&smoke->free_buffers_list, &smoke->buffers[i].link);
			wl_buffer_add_listener(smoke->buffers[i].buffer, &buffer_listener, buffer);

			surface = cairo_image_surface_create_for_data (smoke->buffers[i].map,
							CAIRO_FORMAT_ARGB32, rect->width, rect->height,
							stride);

			smoke->buffers[i].surface = surface;
		}
	}

	return ret;
}

int main(int argc, char *argv[])
{
	struct timespec ts;
	struct smoke smoke;
	struct display *display = NULL;
    struct Window *window = NULL;
	struct shm_pool *pool;
	int size;

	display = create_display();
	if (display == NULL) {
		fprintf(stderr, "failed to create display: %m\n");
		return 1;
	}
	smoke.display = display;

	window = create_window(display, 200, 200);
	if (window == NULL) {
		fprintf(stderr, "failed to create window: %m\n");
		return 1;
	}
	smoke.window = window;

	wl_list_init(&smoke.free_buffers_list);
	if (create_pool_and_buffers(smoke) != 0)
		return 1;

	clock_gettime(CLOCK_MONOTONIC, &ts);
	srandom(ts.tv_nsec);
	smoke->cur_time = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;

	size = smoke.window->height * smoke.window->width;
	smoke.b[0].d = calloc(size, sizeof(float));
	smoke.b[0].u = calloc(size, sizeof(float));
	smoke.b[0].v = calloc(size, sizeof(float));
	smoke.b[1].d = calloc(size, sizeof(float));
	smoke.b[1].u = calloc(size, sizeof(float));
	smoke.b[1].v = calloc(size, sizeof(float));

	window_set_user_data(smoke.window, &smoke);

	/* Initialise damage to full surface, so the padding gets painted */
	wl_surface_damage(window->surface, 0, 0, window->width, window->height);

	/* FIXME: do we put 0 here for time, or ts ? */
	frame_handle_redraw(window, NULL, 0);

	/* Create our smoke-rendering thread */
	if (create_smoker_thread(&smoke) != 0)
		return 1;

	while (running && ret != -1)
		ret = wl_display_dispatch(display->display);

	fprintf(stderr, "simple-shm exiting\n");
	destroy_window(window, &smoke);
	destroy_display(display);

	return 0;
}