File: protocol_control.c

package info (click to toggle)
waypipe 0.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,276 kB
  • sloc: ansic: 15,809; xml: 9,436; python: 1,726; sh: 101; makefile: 35
file content (969 lines) | stat: -rw-r--r-- 29,613 bytes parent folder | download | duplicates (3)
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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/*
 * Copyright © 2020 Manuel Stoeckl
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "common.h"
#include "main.h"
#include "parsing.h"
#include "util.h"

#include "protocol_functions.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>

struct msgtransfer {
	struct test_state *src;
	struct test_state *dst;
};

/* Override the libc clock_gettime, so we can test presentation-time
 * protocol. Note: the video drivers sometimes call this function. */
int clock_gettime(clockid_t clock_id, struct timespec *tp)
{
	/* Assume every call costs 1ns */
	time_value += 1;

	if (clock_id == CLOCK_REALTIME) {
		tp->tv_sec = (int64_t)(time_value / 1000000000uLL);
		tp->tv_nsec = (int64_t)(time_value % 1000000000uLL);
	} else {
		tp->tv_sec = (int64_t)((time_value + local_time_offset) /
				       1000000000uLL);
		tp->tv_nsec = (int64_t)((time_value + local_time_offset) %
					1000000000uLL);
	}
	return 0;
}

static void print_pass(bool pass)
{
	fprintf(stdout, "%s\n", pass ? "PASS" : "FAIL");
}

static char *make_filled_pattern(size_t size, uint32_t contents)
{
	uint32_t *mem = calloc(1, size);
	for (size_t i = 0; i < size / 4; i++) {
		mem[i] = contents;
	}
	return (char *)mem;
}

static int make_filled_file(size_t size, const char *contents)
{
	int fd = create_anon_file();
	ftruncate(fd, (off_t)size);

	uint32_t *mem = (uint32_t *)mmap(
			NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	memcpy(mem, contents, size);
	munmap(mem, size);
	return fd;
}

static bool check_file_contents(int fd, size_t size, const char *contents)
{
	if (fd == -1) {
		return false;
	}

	off_t fsize = lseek(fd, 0, SEEK_END);
	if (fsize != (off_t)size) {
		wp_error("fd size mismatch: %zu %zu\n", fsize, size);
		return -1;
	}

	uint32_t *mem = (uint32_t *)mmap(
			NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
	if (mem == MAP_FAILED) {
		wp_error("Failed to map file");
		return -1;
	}
	bool match = memcmp(mem, contents, size) == 0;
	munmap(mem, size);
	return match;
}
static int get_only_fd_from_msg(const struct test_state *s)
{

	if (s->rcvd && s->rcvd[s->nrcvd - 1].nfds == 1) {
		return s->rcvd[s->nrcvd - 1].fds[0];
	} else {
		return -1;
	}
}
static int get_fd_from_nth_to_last_msg(const struct test_state *s, int nth)
{
	if (!s->rcvd || s->nrcvd < nth) {
		return -1;
	}
	const struct msg *m = &s->rcvd[s->nrcvd - nth];
	if (m->nfds != 1) {
		return -1;
	}
	return m->fds[0];
}

static void msg_send_handler(struct transfer_states *ts, struct test_state *src,
		struct test_state *dst)
{
	struct msg m;
	m.data = ts->msg_space;
	m.fds = ts->fd_space;
	m.len = (int)ts->msg_size;
	m.nfds = (int)ts->fd_size;
	for (int i = 0; i < m.nfds; i++) {
		m.fds[i] = dup(m.fds[i]);
		if (m.fds[i] == -1) {
			wp_error("Invalid fd provided");
		}
	}
	send_protocol_msg(src, dst, m);
	memset(ts->msg_space, 0, sizeof(ts->msg_space));
	memset(ts->fd_space, 0, sizeof(ts->fd_space));
}
static int setup_tstate(struct transfer_states *ts)
{
	memset(ts, 0, sizeof(*ts));
	ts->send = msg_send_handler;
	ts->comp = calloc(1, sizeof(struct test_state));
	ts->app = calloc(1, sizeof(struct test_state));
	if (!ts->comp || !ts->app) {
		goto fail_alloc;
	}
	if (setup_state(ts->comp, true, true) == -1) {
		goto fail_comp_setup;
	}
	if (setup_state(ts->app, false, true) == -1) {
		goto fail_app_setup;
	}
	return 0;

fail_app_setup:
	cleanup_state(ts->app);
fail_comp_setup:
	cleanup_state(ts->comp);
fail_alloc:
	free(ts->comp);
	free(ts->app);
	return -1;
}
static void cleanup_tstate(struct transfer_states *ts)
{
	cleanup_state(ts->comp);
	cleanup_state(ts->app);
	free(ts->comp);
	free(ts->app);
}

static bool test_fixed_shm_buffer_copy(void)
{
	fprintf(stdout, "\n  shm_pool+buffer test\n");

	struct transfer_states T;
	if (setup_tstate(&T) == -1) {
		wp_error("Test setup failed");
		return true;
	}
	bool pass = true;

	char *testpat = make_filled_pattern(16384, 0xFEDCBA98);
	int fd = make_filled_file(16384, testpat);
	int ret_fd = -1;

	struct wp_objid display = {0x1}, registry = {0x2}, shm = {0x3},
			compositor = {0x4}, pool = {0x5}, buffer = {0x6},
			surface = {0x7};

	send_wl_display_req_get_registry(&T, display, registry);
	send_wl_registry_evt_global(&T, registry, 1, "wl_shm", 1);
	send_wl_registry_evt_global(&T, registry, 2, "wl_compositor", 1);
	send_wl_registry_req_bind(&T, registry, 1, "wl_shm", 1, shm);
	send_wl_registry_req_bind(
			&T, registry, 2, "wl_compositor", 1, compositor);
	send_wl_shm_req_create_pool(&T, shm, pool, fd, 16384);
	ret_fd = get_only_fd_from_msg(T.comp);
	send_wl_shm_pool_req_create_buffer(
			&T, pool, buffer, 0, 64, 64, 256, 0x30334258);
	send_wl_compositor_req_create_surface(&T, compositor, surface);
	send_wl_surface_req_attach(&T, surface, buffer, 0, 0);
	send_wl_surface_req_damage(&T, surface, 0, 0, 64, 64);
	send_wl_surface_req_commit(&T, surface);

	/* confirm receipt of fd with the correct contents; if not,
	 * reject */
	if (ret_fd == -1) {
		wp_error("Fd not passed through");
		pass = false;
		goto end;
	}
	pass = check_file_contents(ret_fd, 16384, testpat);
	if (!pass) {
		wp_error("Failed to transfer file");
	}
end:
	free(testpat);
	checked_close(fd);
	cleanup_tstate(&T);

	print_pass(pass);
	return pass;
}

static bool test_fixed_shm_screencopy_copy(void)
{
	fprintf(stdout, "\n screencopy test\n");

	struct transfer_states T;
	if (setup_tstate(&T) == -1) {
		wp_error("Test setup failed");
		return true;
	}
	bool pass = true;

	char *testpat_orig = make_filled_pattern(16384, 0xFEDCBA98);
	char *testpat_screen = make_filled_pattern(16384, 0x77557755);
	int fd = make_filled_file(16384, testpat_orig);
	int ret_fd = -1;

	struct wp_objid display = {0x1}, registry = {0x2}, shm = {0x3},
			output = {0x4}, pool = {0x5}, buffer = {0x6},
			frame = {0x7}, screencopy = {0x8};

	send_wl_display_req_get_registry(&T, display, registry);
	send_wl_registry_evt_global(&T, registry, 1, "wl_shm", 1);
	send_wl_registry_evt_global(&T, registry, 2, "wl_output", 1);
	send_wl_registry_evt_global(
			&T, registry, 3, "zwlr_screencopy_manager_v1", 1);
	send_wl_registry_req_bind(&T, registry, 1, "wl_shm", 1, shm);
	send_wl_registry_req_bind(&T, registry, 2, "wl_output", 1, output);
	send_wl_registry_req_bind(&T, registry, 3, "zwlr_screencopy_manager_v1",
			1, screencopy);
	send_wl_shm_req_create_pool(&T, shm, pool, fd, 16384);
	ret_fd = get_only_fd_from_msg(T.comp);
	if (ret_fd == -1) {
		wp_error("Fd not passed through");
		pass = false;
		goto end;
	}
	send_zwlr_screencopy_manager_v1_req_capture_output(
			&T, screencopy, frame, 0, output);
	send_zwlr_screencopy_frame_v1_evt_buffer(&T, frame, 0, 64, 64, 16384);
	send_wl_shm_pool_req_create_buffer(
			&T, pool, buffer, 0, 64, 64, 256, 0x30334258);
	send_zwlr_screencopy_frame_v1_req_copy(&T, frame, buffer);

	uint32_t *mem = (uint32_t *)mmap(NULL, 16384, PROT_READ | PROT_WRITE,
			MAP_SHARED, ret_fd, 0);
	memcpy(mem, testpat_screen, 16384);
	munmap(mem, 16384);

	send_zwlr_screencopy_frame_v1_evt_flags(&T, frame, 0);
	send_zwlr_screencopy_frame_v1_evt_ready(&T, frame, 0, 12345, 600000000);

	/* confirm receipt of fd with the correct contents; if not,
	 * reject */
	if (ret_fd == -1) {
		wp_error("Fd not passed through");
		pass = false;
		goto end;
	}
	pass = check_file_contents(fd, 16384, testpat_screen);
	if (!pass) {
		wp_error("Failed to transfer file");
	}
end:
	free(testpat_screen);
	free(testpat_orig);
	checked_close(fd);
	cleanup_tstate(&T);

	print_pass(pass);
	return pass;
}

static bool test_fixed_keymap_copy(void)
{
	fprintf(stdout, "\n  Keymap test\n");
	struct transfer_states T;
	if (setup_tstate(&T) == -1) {
		wp_error("Test setup failed");
		return true;
	}
	bool pass = true;

	char *testpat = make_filled_pattern(16384, 0xFEDCBA98);
	int fd = make_filled_file(16384, testpat);
	int ret_fd = -1;

	struct wp_objid display = {0x1}, registry = {0x2}, seat = {0x3},
			keyboard = {0x4};

	send_wl_display_req_get_registry(&T, display, registry);
	send_wl_registry_evt_global(&T, registry, 1, "wl_seat", 7);
	send_wl_registry_req_bind(&T, registry, 1, "wl_seat", 7, seat);
	send_wl_seat_evt_capabilities(&T, seat, 3);
	send_wl_seat_req_get_keyboard(&T, seat, keyboard);
	send_wl_keyboard_evt_keymap(&T, keyboard, 1, fd, 16384);
	ret_fd = get_only_fd_from_msg(T.app);

	/* confirm receipt of fd with the correct contents; if not,
	 * reject */
	if (ret_fd == -1) {
		wp_error("Fd not passed through");
		pass = false;
		goto end;
	}
	pass = check_file_contents(ret_fd, 16384, testpat);
	if (!pass) {
		wp_error("Failed to transfer file");
	}

end:
	free(testpat);
	checked_close(fd);
	cleanup_tstate(&T);

	print_pass(pass);
	return pass;
}

#define DMABUF_FORMAT 875713112

static int create_dmabuf(void)
{
	struct render_data rd;
	memset(&rd, 0, sizeof(rd));
	rd.drm_fd = -1;
	rd.av_disabled = true;

	const size_t test_width = 256;
	const size_t test_height = 384;
	const size_t test_cpp = 4;
	const size_t test_size = test_width * test_height * test_cpp;
	const struct dmabuf_slice_data slice_data = {
			.width = (uint32_t)test_width,
			.height = (uint32_t)test_height,
			.format = DMABUF_FORMAT,
			.num_planes = 1,
			.modifier = 0,
			.offsets = {0, 0, 0, 0},
			.strides = {(uint32_t)(test_width * test_cpp), 0, 0, 0},
			.using_planes = {true, false, false, false},
	};

	int dmafd = -1;
	if (init_render_data(&rd) == -1) {
		return -1;
	}
	struct gbm_bo *bo = make_dmabuf(&rd, &slice_data);
	if (!bo) {
		goto end;
	}

	void *map_handle = NULL;
	uint32_t stride;
	void *data = map_dmabuf(bo, true, &map_handle, &stride);
	if (!data) {
		destroy_dmabuf(bo);
		goto end;
	}
	/* TODO: the best test pattern is a colored gradient, so we can
	 * check whether the copy flips things or not */
	memset(data, 0x80, test_size);
	unmap_dmabuf(bo, map_handle);

	dmafd = export_dmabuf(bo);
	if (dmafd == -1) {
		goto end;
	}

end:
	destroy_dmabuf(bo);
	cleanup_render_data(&rd);

	return dmafd;
}

enum dmabuf_copy_type {
	COPY_LINUX_DMABUF,
	COPY_LINUX_DMABUF_INDIR,
	COPY_DRM_PRIME,
	COPY_WLR_EXPORT,
};

static bool test_fixed_dmabuf_copy(enum dmabuf_copy_type type)
{
	const char *const types[] = {"linux-dmabuf", "linux-dmabuf-indir",
			"drm-prime", "wlr-export"};
	fprintf(stdout, "\n  DMABUF test, %s\n", types[(int)type]);

	int dmabufd = create_dmabuf();
	const int width = 256, height = 384;
	if (dmabufd == -1) {
		return true;
	}
	struct transfer_states T;
	if (setup_tstate(&T) == -1) {
		wp_error("Test setup failed");
		return true;
	}
	bool pass = true;
	int ret_fd = -1;

	switch (type) {
	case COPY_LINUX_DMABUF: {
		struct wp_objid display = {0x1}, registry = {0x2},
				linux_dmabuf = {0x3}, compositor = {0x4},
				params = {0x5}, buffer = {0x6}, surface = {0x7};

		send_wl_display_req_get_registry(&T, display, registry);
		send_wl_registry_evt_global(
				&T, registry, 1, "zwp_linux_dmabuf_v1", 1);
		send_wl_registry_evt_global(
				&T, registry, 2, "wl_compositor", 1);
		send_wl_registry_req_bind(&T, registry, 1,
				"zwp_linux_dmabuf_v1", 1, linux_dmabuf);
		send_wl_registry_req_bind(&T, registry, 12, "wl_compositor", 1,
				compositor);
		send_zwp_linux_dmabuf_v1_evt_modifier(
				&T, linux_dmabuf, DMABUF_FORMAT, 0, 0);
		send_zwp_linux_dmabuf_v1_req_create_params(
				&T, linux_dmabuf, params);
		send_zwp_linux_buffer_params_v1_req_add(
				&T, params, dmabufd, 0, 0, 256 * 4, 0, 0);
		send_zwp_linux_buffer_params_v1_req_create_immed(
				&T, params, buffer, 256, 384, DMABUF_FORMAT, 0);
		/* this message + previous, after reordering, are treated as one
		 * bundle; if that is fixed, this will break, and 1 should
		 * become 2 */
		ret_fd = get_fd_from_nth_to_last_msg(T.comp, 1);
		send_zwp_linux_buffer_params_v1_req_destroy(&T, params);
		send_wl_compositor_req_create_surface(&T, compositor, surface);
		send_wl_surface_req_attach(&T, surface, buffer, 0, 0);
		send_wl_surface_req_damage(&T, surface, 0, 0, 64, 64);
		send_wl_surface_req_commit(&T, surface);
	} break;
	case COPY_LINUX_DMABUF_INDIR: {
		struct wp_objid display = {0x1}, registry = {0x2},
				linux_dmabuf = {0x3}, compositor = {0x4},
				params = {0x5}, buffer = {0x6}, surface = {0x7};

		send_wl_display_req_get_registry(&T, display, registry);
		send_wl_registry_evt_global(
				&T, registry, 1, "zwp_linux_dmabuf_v1", 1);
		send_wl_registry_evt_global(
				&T, registry, 2, "wl_compositor", 1);
		send_wl_registry_req_bind(&T, registry, 1,
				"zwp_linux_dmabuf_v1", 1, linux_dmabuf);
		send_wl_registry_req_bind(&T, registry, 12, "wl_compositor", 1,
				compositor);
		send_zwp_linux_dmabuf_v1_evt_modifier(
				&T, linux_dmabuf, DMABUF_FORMAT, 0, 0);
		send_zwp_linux_dmabuf_v1_req_create_params(
				&T, linux_dmabuf, params);
		send_zwp_linux_buffer_params_v1_req_add(
				&T, params, dmabufd, 0, 0, 256 * 4, 0, 0);
		send_zwp_linux_buffer_params_v1_req_create(
				&T, params, 256, 384, DMABUF_FORMAT, 0);
		/* this message + previous, after reordering, are treated as one
		 * bundle; if that is fixed, this will break, and 1 should
		 * become 2 */
		ret_fd = get_fd_from_nth_to_last_msg(T.comp, 1);
		send_zwp_linux_buffer_params_v1_evt_created(&T, params, buffer);
		send_zwp_linux_buffer_params_v1_req_destroy(&T, params);
		send_wl_compositor_req_create_surface(&T, compositor, surface);
		send_wl_surface_req_attach(&T, surface, buffer, 0, 0);
		send_wl_surface_req_damage(&T, surface, 0, 0, 64, 64);
		send_wl_surface_req_commit(&T, surface);
	} break;
	case COPY_DRM_PRIME: {
		struct wp_objid display = {0x1}, registry = {0x2},
				wl_drm = {0x3}, compositor = {0x4},
				buffer = {0x5}, surface = {0x6};

		send_wl_display_req_get_registry(&T, display, registry);
		send_wl_registry_evt_global(&T, registry, 1, "wl_drm", 1);
		send_wl_registry_evt_global(
				&T, registry, 2, "wl_compositor", 1);
		send_wl_registry_req_bind(&T, registry, 1, "wl_drm", 1, wl_drm);
		send_wl_registry_req_bind(&T, registry, 12, "wl_compositor", 1,
				compositor);

		send_wl_drm_evt_device(&T, wl_drm, "/dev/dri/renderD128");
		send_wl_drm_evt_format(&T, wl_drm, DMABUF_FORMAT);
		send_wl_drm_evt_capabilities(&T, wl_drm, 1);
		send_wl_drm_req_create_prime_buffer(&T, wl_drm, buffer, dmabufd,
				width, height, DMABUF_FORMAT, 0, width * 4, 0,
				0, 0, 0);

		ret_fd = get_fd_from_nth_to_last_msg(T.comp, 1);
		send_wl_compositor_req_create_surface(&T, compositor, surface);
		send_wl_surface_req_attach(&T, surface, buffer, 0, 0);
		send_wl_surface_req_damage(&T, surface, 0, 0, 64, 64);
		send_wl_surface_req_commit(&T, surface);
	} break;

	case COPY_WLR_EXPORT: {
		/* note: here the compositor creates and sends fd to client */

		struct wp_objid display = {0x1}, registry = {0x2},
				export_manager = {0x3}, output = {0x4},
				dmabuf_frame = {0x5};

		send_wl_display_req_get_registry(&T, display, registry);
		send_wl_registry_evt_global(&T, registry, 1,
				"zwlr_export_dmabuf_manager_v1", 1);
		send_wl_registry_evt_global(&T, registry, 2, "wl_output", 1);
		send_wl_registry_req_bind(&T, registry, 1,
				"zwlr_export_dmabuf_manager_v1", 1,
				export_manager);
		send_wl_registry_req_bind(
				&T, registry, 12, "wl_output", 1, output);

		send_zwlr_export_dmabuf_manager_v1_req_capture_output(
				&T, export_manager, dmabuf_frame, 1, output);
		send_zwlr_export_dmabuf_frame_v1_evt_frame(&T, dmabuf_frame,
				width, height, 0, 0, 0, 1, DMABUF_FORMAT, 0, 0,
				1);
		send_zwlr_export_dmabuf_frame_v1_evt_object(&T, dmabuf_frame, 0,
				dmabufd, width * height * 4, 0, width * 4, 0);
		ret_fd = get_only_fd_from_msg(T.app);
		send_zwlr_export_dmabuf_frame_v1_evt_ready(
				&T, dmabuf_frame, 555555, 555555555, 333333333);
	} break;
	}

	if (ret_fd == -1) {
		wp_error("Fd not passed through");
		pass = false;
		goto end;
	}
	// TODO: verify that the FD contents are correct

end:
	checked_close(dmabufd);
	/* todo: the drm_fd may be dup'd by libgbm but not freed */
	cleanup_tstate(&T);

	print_pass(pass);
	return pass;
}

enum data_device_type {
	DDT_WAYLAND,
	DDT_GTK_PRIMARY,
	DDT_PRIMARY,
	DDT_WLR,
};
static const char *const data_device_type_strs[] = {"wayland main",
		"gtk primary selection", "primary selection",
		"wlroots data control"};

/* Confirm that wl_data_offer.receive creates a pipe matching the input */
static bool test_data_offer(enum data_device_type type)
{
	fprintf(stdout, "\n  Data offer test: %s\n",
			data_device_type_strs[type]);
	struct transfer_states T;
	if (setup_tstate(&T) == -1) {
		wp_error("Test setup failed");
		return true;
	}
	bool pass = true;

	int src_pipe[2];
	pipe(src_pipe);
	int ret_fd = -1;

	struct wp_objid display = {0x1}, registry = {0x2}, ddman = {0x3},
			seat = {0x4}, ddev = {0x5}, offer = {0xff000001};

	send_wl_display_req_get_registry(&T, display, registry);
	send_wl_registry_evt_global(&T, registry, 1, "wl_seat", 7);
	send_wl_registry_req_bind(&T, registry, 1, "wl_seat", 7, seat);
	switch (type) {
	case DDT_WAYLAND:
		send_wl_registry_evt_global(
				&T, registry, 2, "wl_data_device_manager", 3);
		send_wl_registry_req_bind(&T, registry, 2,
				"wl_data_device_manager", 3, ddman);
		send_wl_data_device_manager_req_get_data_device(
				&T, ddman, ddev, seat);
		send_wl_data_device_evt_data_offer(&T, ddev, offer);
		send_wl_data_offer_evt_offer(
				&T, offer, "text/plain;charset=utf-8");
		send_wl_data_device_evt_selection(&T, ddev, offer);
		send_wl_data_offer_req_receive(&T, offer,
				"text/plain;charset=utf-8", src_pipe[1]);
		break;
	case DDT_GTK_PRIMARY:
		send_wl_registry_evt_global(&T, registry, 2,
				"gtk_primary_selection_device_manager", 1);
		send_wl_registry_req_bind(&T, registry, 2,
				"gtk_primary_selection_device_manager", 1,
				ddman);
		send_gtk_primary_selection_device_manager_req_get_device(
				&T, ddman, ddev, seat);
		send_gtk_primary_selection_device_evt_data_offer(
				&T, ddev, offer);
		send_gtk_primary_selection_offer_evt_offer(
				&T, offer, "text/plain;charset=utf-8");
		send_gtk_primary_selection_device_evt_selection(
				&T, ddev, offer);
		send_gtk_primary_selection_offer_req_receive(&T, offer,
				"text/plain;charset=utf-8", src_pipe[1]);
		break;
	case DDT_PRIMARY:
		send_wl_registry_evt_global(&T, registry, 2,
				"zwp_primary_selection_device_manager_v1", 1);
		send_wl_registry_req_bind(&T, registry, 2,
				"zwp_primary_selection_device_manager_v1", 1,
				ddman);
		send_zwp_primary_selection_device_manager_v1_req_get_device(
				&T, ddman, ddev, seat);
		send_zwp_primary_selection_device_v1_evt_data_offer(
				&T, ddev, offer);
		send_zwp_primary_selection_offer_v1_evt_offer(
				&T, offer, "text/plain;charset=utf-8");
		send_zwp_primary_selection_device_v1_evt_selection(
				&T, ddev, offer);
		send_zwp_primary_selection_offer_v1_req_receive(&T, offer,
				"text/plain;charset=utf-8", src_pipe[1]);
		break;
	case DDT_WLR:
		send_wl_registry_evt_global(&T, registry, 2,
				"zwlr_data_control_manager_v1", 1);
		send_wl_registry_req_bind(&T, registry, 2,
				"zwlr_data_control_manager_v1", 1, ddman);
		send_zwlr_data_control_manager_v1_req_get_data_device(
				&T, ddman, ddev, seat);
		send_zwlr_data_control_device_v1_evt_data_offer(
				&T, ddev, offer);
		send_zwlr_data_control_offer_v1_evt_offer(
				&T, offer, "text/plain;charset=utf-8");
		send_zwlr_data_control_device_v1_evt_selection(&T, ddev, offer);
		send_zwlr_data_control_offer_v1_req_receive(&T, offer,
				"text/plain;charset=utf-8", src_pipe[1]);
		break;
	}
	ret_fd = get_only_fd_from_msg(T.comp);

	/* confirm receipt of fd with the correct contents; if not,
	 * reject */
	if (ret_fd == -1) {
		wp_error("Fd not passed through");
		pass = false;
		goto end;
	}
	uint8_t tmp = 0xab;
	if (write(ret_fd, &tmp, 1) != 1) {
		wp_error("Fd not writable");
		pass = false;
		goto end;
	}
end:

	checked_close(src_pipe[0]);
	checked_close(src_pipe[1]);
	cleanup_tstate(&T);

	print_pass(pass);
	return pass;
}

/* Confirm that wl_data_source.data_offer creates a pipe matching the input */
static bool test_data_source(enum data_device_type type)
{
	fprintf(stdout, "\n  Data source test: %s\n",
			data_device_type_strs[type]);
	struct transfer_states T;
	if (setup_tstate(&T) == -1) {
		wp_error("Test setup failed");
		return true;
	}
	bool pass = true;

	int dst_pipe[2];
	pipe(dst_pipe);
	int ret_fd = -1;

	struct wp_objid display = {0x1}, registry = {0x2}, ddman = {0x3},
			seat = {0x4}, ddev = {0x5}, dsource = {0x6};

	send_wl_display_req_get_registry(&T, display, registry);
	send_wl_registry_evt_global(&T, registry, 1, "wl_seat", 7);
	send_wl_registry_req_bind(&T, registry, 1, "wl_seat", 7, seat);
	switch (type) {
	case DDT_WAYLAND:
		send_wl_registry_evt_global(
				&T, registry, 2, "wl_data_device_manager", 1);
		send_wl_registry_req_bind(&T, registry, 2,
				"wl_data_device_manager", 1, ddman);
		send_wl_data_device_manager_req_get_data_device(
				&T, ddman, ddev, seat);
		send_wl_data_device_manager_req_create_data_source(
				&T, ddman, dsource);
		send_wl_data_source_req_offer(
				&T, dsource, "text/plain;charset=utf-8");
		send_wl_data_device_req_set_selection(&T, ddev, dsource, 9999);
		send_wl_data_source_evt_send(&T, dsource,
				"text/plain;charset=utf-8", dst_pipe[0]);
		break;
	case DDT_GTK_PRIMARY:
		send_wl_registry_evt_global(&T, registry, 2,
				"gtk_primary_selection_device_manager", 1);
		send_wl_registry_req_bind(&T, registry, 2,
				"gtk_primary_selection_device_manager", 1,
				ddman);
		send_gtk_primary_selection_device_manager_req_get_device(
				&T, ddman, ddev, seat);
		send_gtk_primary_selection_device_manager_req_create_source(
				&T, ddman, dsource);
		send_gtk_primary_selection_source_req_offer(
				&T, dsource, "text/plain;charset=utf-8");
		send_gtk_primary_selection_device_req_set_selection(
				&T, ddev, dsource, 9999);
		send_gtk_primary_selection_source_evt_send(&T, dsource,
				"text/plain;charset=utf-8", dst_pipe[0]);
		break;
	case DDT_PRIMARY:
		send_wl_registry_evt_global(&T, registry, 2,
				"zwp_primary_selection_device_manager_v1", 1);
		send_wl_registry_req_bind(&T, registry, 2,
				"zwp_primary_selection_device_manager_v1", 1,
				ddman);
		send_zwp_primary_selection_device_manager_v1_req_get_device(
				&T, ddman, ddev, seat);
		send_zwp_primary_selection_device_manager_v1_req_create_source(
				&T, ddman, dsource);
		send_zwp_primary_selection_source_v1_req_offer(
				&T, dsource, "text/plain;charset=utf-8");
		send_zwp_primary_selection_device_v1_req_set_selection(
				&T, ddev, dsource, 9999);
		send_zwp_primary_selection_source_v1_evt_send(&T, dsource,
				"text/plain;charset=utf-8", dst_pipe[0]);
		break;
	case DDT_WLR:
		send_wl_registry_evt_global(&T, registry, 2,
				"zwlr_data_control_manager_v1", 1);
		send_wl_registry_req_bind(&T, registry, 2,
				"zwlr_data_control_manager_v1", 1, ddman);
		send_zwlr_data_control_manager_v1_req_get_data_device(
				&T, ddman, ddev, seat);
		send_zwlr_data_control_manager_v1_req_create_data_source(
				&T, ddman, dsource);
		send_zwlr_data_control_source_v1_req_offer(
				&T, dsource, "text/plain;charset=utf-8");
		send_zwlr_data_control_device_v1_req_set_selection(
				&T, ddev, dsource);
		send_zwlr_data_control_source_v1_evt_send(&T, dsource,
				"text/plain;charset=utf-8", dst_pipe[0]);
		break;
	}
	ret_fd = get_only_fd_from_msg(T.app);

	/* confirm receipt of fd with the correct contents; if not,
	 * reject */
	if (ret_fd == -1) {
		wp_error("Fd not passed through");
		pass = false;
		goto end;
	}
	/* todo: check readable */
end:

	checked_close(dst_pipe[0]);
	checked_close(dst_pipe[1]);
	cleanup_tstate(&T);

	print_pass(pass);
	return pass;
}

/* Check that gamma_control copies the input file */
static bool test_gamma_control(void)
{
	fprintf(stdout, "\n  Gamma control test\n");
	struct transfer_states T;
	if (setup_tstate(&T) == -1) {
		wp_error("Test setup failed");
		return true;
	}
	bool pass = true;

	int ret_fd = -1;

	char *testpat = make_filled_pattern(1024, 0x12345678);
	int fd = make_filled_file(1024, testpat);

	struct wp_objid display = {0x1}, registry = {0x2},
			gamma_manager = {0x3}, output = {0x4},
			gamma_control = {0x5};

	send_wl_display_req_get_registry(&T, display, registry);
	send_wl_registry_evt_global(
			&T, registry, 1, "zwlr_gamma_control_manager_v1", 1);
	send_wl_registry_req_bind(&T, registry, 1,
			"zwlr_gamma_control_manager_v1", 1, gamma_manager);
	send_wl_registry_evt_global(&T, registry, 1, "wl_output", 3);
	send_wl_registry_req_bind(&T, registry, 1, "wl_output", 3, output);
	send_zwlr_gamma_control_manager_v1_req_get_gamma_control(
			&T, gamma_manager, gamma_control, output);
	send_zwlr_gamma_control_v1_evt_gamma_size(&T, gamma_control, 1024);
	send_zwlr_gamma_control_v1_req_set_gamma(&T, gamma_control, fd);

	ret_fd = get_only_fd_from_msg(T.comp);

	/* confirm receipt of fd with the correct contents; if not,
	 * reject */
	if (ret_fd == -1) {
		wp_error("Fd not passed through");
		pass = false;
		goto end;
	}
	pass = check_file_contents(ret_fd, 1024, testpat);
	if (!pass) {
		wp_error("Failed to transfer file");
	}
end:

	free(testpat);
	checked_close(fd);
	cleanup_tstate(&T);

	print_pass(pass);
	return pass;
}

/* Check that gamma_control copies the input file */
static bool test_presentation_time(void)
{
	fprintf(stdout, "\n  Presentation time test\n");
	struct transfer_states T;
	if (setup_tstate(&T) == -1) {
		wp_error("Test setup failed");
		return true;
	}
	bool pass = true;

	struct wp_objid display = {0x1}, registry = {0x2}, presentation = {0x3},
			compositor = {0x4}, surface = {0x5}, feedback = {0x6};
	T.app->local_time_offset = 500;
	T.comp->local_time_offset = 600;

	send_wl_display_req_get_registry(&T, display, registry);
	send_wl_registry_evt_global(&T, registry, 1, "wp_presentation", 1);
	send_wl_registry_evt_global(&T, registry, 2, "wl_compositor", 1);
	send_wl_registry_req_bind(
			&T, registry, 1, "wp_presentation", 1, presentation);
	/* todo: run another branch with CLOCK_REALTIME */
	send_wp_presentation_evt_clock_id(&T, presentation, CLOCK_MONOTONIC);
	send_wl_registry_req_bind(
			&T, registry, 12, "wl_compositor", 1, compositor);
	send_wl_compositor_req_create_surface(&T, compositor, surface);

	send_wl_surface_req_damage(&T, surface, 0, 0, 64, 64);

	send_wp_presentation_req_feedback(&T, presentation, surface, feedback);
	send_wl_surface_req_commit(&T, surface);
	send_wp_presentation_feedback_evt_presented(
			&T, feedback, 0, 30, 120000, 16666666, 0, 0, 7);
	const struct msg *const last_msg = &T.app->rcvd[T.app->nrcvd - 1];
	uint32_t tv_sec_hi = last_msg->data[2], tv_sec_lo = last_msg->data[3],
		 tv_nsec = last_msg->data[4];
	if (tv_nsec != 120000 + T.app->local_time_offset -
					T.comp->local_time_offset) {
		wp_error("Time translation failed %d %d %d", tv_sec_hi,
				tv_sec_lo, tv_nsec);
		pass = false;
		goto end;
	}

	/* look at timestamp */
	if (!pass) {
		goto end;
	}
end:
	cleanup_tstate(&T);

	print_pass(pass);
	return pass;
}

/* Check whether the video encoding feature can replicate a uniform
 * color image */
static bool test_fixed_video_color_copy(enum video_coding_fmt fmt, bool hw)
{
	(void)fmt;
	(void)hw;
	/* todo: back out if no dmabuf support or no video support */
	return true;
}

log_handler_func_t log_funcs[2] = {test_log_handler, test_log_handler};
int main(int argc, char **argv)
{
	(void)argc;
	(void)argv;

	set_initial_fds();

	int ntest = 21;
	int nsuccess = 0;
	nsuccess += test_fixed_shm_buffer_copy();
	nsuccess += test_fixed_shm_screencopy_copy();
	nsuccess += test_fixed_keymap_copy();
	nsuccess += test_fixed_dmabuf_copy(COPY_LINUX_DMABUF);
	nsuccess += test_fixed_dmabuf_copy(COPY_LINUX_DMABUF_INDIR);
	nsuccess += test_fixed_dmabuf_copy(COPY_DRM_PRIME);
	nsuccess += test_fixed_dmabuf_copy(COPY_WLR_EXPORT);
	nsuccess += test_data_offer(DDT_WAYLAND);
	nsuccess += test_data_offer(DDT_PRIMARY);
	nsuccess += test_data_offer(DDT_GTK_PRIMARY);
	nsuccess += test_data_offer(DDT_WLR);
	nsuccess += test_data_source(DDT_WAYLAND);
	nsuccess += test_data_source(DDT_PRIMARY);
	nsuccess += test_data_source(DDT_GTK_PRIMARY);
	nsuccess += test_data_source(DDT_WLR);
	nsuccess += test_gamma_control();
	nsuccess += test_presentation_time();
	nsuccess += test_fixed_video_color_copy(VIDEO_H264, false);
	nsuccess += test_fixed_video_color_copy(VIDEO_H264, true);
	nsuccess += test_fixed_video_color_copy(VIDEO_VP9, false);
	nsuccess += test_fixed_video_color_copy(VIDEO_AV1, false);
	// TODO: add tests for handling of common errors, e.g. invalid fd,
	// or type confusion

	fprintf(stdout, "\n%d of %d cases passed\n", nsuccess, ntest);

	check_unclosed_fds();

	return (nsuccess == ntest) ? EXIT_SUCCESS : EXIT_FAILURE;
}