blob: c92a3eb7ad805a0268114c5e1f2f6a1930f70f51 [file] [log] [blame]
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +00001/*
2 * Copyright © 2015 Collabora Ltd.
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
Bryce Harringtonb4dae9b2016-06-15 18:13:07 -070023#include "config.h"
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +000024
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030025#include <stdint.h>
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +000026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdbool.h>
30#include <assert.h>
31#include <unistd.h>
32#include <sys/mman.h>
33#include <signal.h>
34#include <fcntl.h>
35
36#include <drm_fourcc.h>
37
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +000038#include <sys/mman.h>
39#include <sys/stat.h>
40#include <fcntl.h>
41#include <errno.h>
42#include <linux/videodev2.h>
43#include <linux/input.h>
44
45#include <wayland-client.h>
Bryce Harrington0d1a6222016-02-11 16:42:49 -080046#include "shared/zalloc.h"
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +000047#include "xdg-shell-unstable-v5-client-protocol.h"
48#include "fullscreen-shell-unstable-v1-client-protocol.h"
49#include "linux-dmabuf-unstable-v1-client-protocol.h"
50
51#define CLEAR(x) memset(&(x), 0, sizeof(x))
52
53static int
54xioctl(int fh, int request, void *arg)
55{
56 int r;
57
58 do {
59 r = ioctl(fh, request, arg);
60 } while (r == -1 && errno == EINTR);
61
62 return r;
63}
64
65static uint32_t
66parse_format(const char fmt[4])
67{
68 return fourcc_code(fmt[0], fmt[1], fmt[2], fmt[3]);
69}
70
71static inline const char *
72dump_format(uint32_t format, char out[4])
73{
74#if BYTE_ORDER == BIG_ENDIAN
75 format = __builtin_bswap32(format);
76#endif
77 memcpy(out, &format, 4);
78 return out;
79}
80
81struct buffer_format {
82 int width;
83 int height;
84 enum v4l2_buf_type type;
85 uint32_t format;
86
87 unsigned num_planes;
88 unsigned strides[VIDEO_MAX_PLANES];
89};
90
91struct display {
92 struct wl_display *display;
93 struct wl_registry *registry;
94 struct wl_compositor *compositor;
95 struct wl_seat *seat;
96 struct wl_keyboard *keyboard;
97 struct xdg_shell *shell;
98 struct zwp_fullscreen_shell_v1 *fshell;
99 struct zwp_linux_dmabuf_v1 *dmabuf;
100 bool requested_format_found;
101
102 int v4l_fd;
103 struct buffer_format format;
104 uint32_t drm_format;
105};
106
107struct buffer {
108 struct wl_buffer *buffer;
109 struct display *display;
110 int busy;
111 int index;
112
113 int dmabuf_fds[VIDEO_MAX_PLANES];
114 int data_offsets[VIDEO_MAX_PLANES];
115};
116
117#define NUM_BUFFERS 3
118
119struct window {
120 struct display *display;
121 struct wl_surface *surface;
122 struct xdg_surface *xdg_surface;
123 struct buffer buffers[NUM_BUFFERS];
124 struct wl_callback *callback;
125};
126
127static bool running = true;
128
129static int
130queue(struct display *display, struct buffer *buffer)
131{
132 struct v4l2_buffer buf;
133 struct v4l2_plane planes[VIDEO_MAX_PLANES];
134 unsigned i;
135
136 CLEAR(buf);
137 buf.type = display->format.type;
138 buf.memory = V4L2_MEMORY_MMAP;
139 buf.index = buffer->index;
140
141 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
142 CLEAR(planes);
143 buf.length = VIDEO_MAX_PLANES;
144 buf.m.planes = planes;
145 }
146
147 if (xioctl(display->v4l_fd, VIDIOC_QUERYBUF, &buf) == -1) {
148 perror("VIDIOC_QUERYBUF");
149 return 0;
150 }
151
152 if (xioctl(display->v4l_fd, VIDIOC_QBUF, &buf) == -1) {
153 perror("VIDIOC_QBUF");
154 return 0;
155 }
156
157 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
158 if (display->format.num_planes != buf.length) {
159 fprintf(stderr, "Wrong number of planes returned by "
160 "QUERYBUF\n");
161 return 0;
162 }
163
164 for (i = 0; i < buf.length; ++i)
165 buffer->data_offsets[i] = buf.m.planes[i].data_offset;
166 }
167
168 return 1;
169}
170
171static void
172buffer_release(void *data, struct wl_buffer *buffer)
173{
174 struct buffer *mybuf = data;
175
176 mybuf->busy = 0;
177
178 if (!queue(mybuf->display, mybuf))
179 running = false;
180}
181
182static const struct wl_buffer_listener buffer_listener = {
183 buffer_release
184};
185
186static unsigned int
187set_format(struct display *display, uint32_t format)
188{
189 struct v4l2_format fmt;
190 char buf[4];
191
192 CLEAR(fmt);
193
194 fmt.type = display->format.type;
195
196 if (xioctl(display->v4l_fd, VIDIOC_G_FMT, &fmt) == -1) {
197 perror("VIDIOC_G_FMT");
198 return 0;
199 }
200
201 /* No need to set the format if it already is the one we want */
202 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
203 fmt.fmt.pix.pixelformat == format)
204 return 1;
205 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
206 fmt.fmt.pix_mp.pixelformat == format)
207 return fmt.fmt.pix_mp.num_planes;
208
209 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
210 fmt.fmt.pix.pixelformat = format;
211 else
212 fmt.fmt.pix_mp.pixelformat = format;
213
214 if (xioctl(display->v4l_fd, VIDIOC_S_FMT, &fmt) == -1) {
215 perror("VIDIOC_S_FMT");
216 return 0;
217 }
218
219 if ((display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
220 fmt.fmt.pix.pixelformat != format) ||
221 (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
222 fmt.fmt.pix_mp.pixelformat != format)) {
223 fprintf(stderr, "Failed to set format to %.4s\n",
224 dump_format(format, buf));
225 return 0;
226 }
227
228 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
229 return fmt.fmt.pix_mp.num_planes;
230
231 return 1;
232}
233
234static int
235v4l_connect(struct display *display, const char *dev_name)
236{
237 struct v4l2_capability cap;
238 struct v4l2_requestbuffers req;
239 unsigned int num_planes;
240
241 display->v4l_fd = open(dev_name, O_RDWR);
242 if (display->v4l_fd < 0) {
243 perror(dev_name);
244 return 0;
245 }
246
247 if (xioctl(display->v4l_fd, VIDIOC_QUERYCAP, &cap) == -1) {
248 if (errno == EINVAL) {
249 fprintf(stderr, "%s is no V4L2 device\n", dev_name);
250 } else {
251 perror("VIDIOC_QUERYCAP");
252 }
253 return 0;
254 }
255
256 if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)
257 display->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
258 else if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
259 display->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
260 else {
261 fprintf(stderr, "%s is no video capture device\n", dev_name);
262 return 0;
263 }
264
265 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
266 fprintf(stderr, "%s does not support dmabuf i/o\n", dev_name);
267 return 0;
268 }
269
270 /* Select video input, video standard and tune here */
271
272 num_planes = set_format(display, display->format.format);
273 if (num_planes < 1)
274 return 0;
275
276 CLEAR(req);
277
278 req.type = display->format.type;
279 req.memory = V4L2_MEMORY_MMAP;
280 req.count = NUM_BUFFERS * num_planes;
281
282 if (xioctl(display->v4l_fd, VIDIOC_REQBUFS, &req) == -1) {
283 if (errno == EINVAL) {
284 fprintf(stderr, "%s does not support dmabuf\n",
285 dev_name);
286 } else {
287 perror("VIDIOC_REQBUFS");
288 }
289 return 0;
290 }
291
292 if (req.count < NUM_BUFFERS * num_planes) {
293 fprintf(stderr, "Insufficient buffer memory on %s\n", dev_name);
294 return 0;
295 }
296
297 printf("Created %d buffers\n", req.count);
298
299 return 1;
300}
301
302static void
303v4l_shutdown(struct display *display)
304{
305 close(display->v4l_fd);
306}
307
308static void
309create_succeeded(void *data,
310 struct zwp_linux_buffer_params_v1 *params,
311 struct wl_buffer *new_buffer)
312{
313 struct buffer *buffer = data;
314 unsigned i;
315
316 buffer->buffer = new_buffer;
317 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
318
319 zwp_linux_buffer_params_v1_destroy(params);
320
321 for (i = 0; i < buffer->display->format.num_planes; ++i)
322 close(buffer->dmabuf_fds[i]);
323}
324
325static void
326create_failed(void *data, struct zwp_linux_buffer_params_v1 *params)
327{
328 struct buffer *buffer = data;
329 unsigned i;
330
331 buffer->buffer = NULL;
332
333 zwp_linux_buffer_params_v1_destroy(params);
334
335 for (i = 0; i < buffer->display->format.num_planes; ++i)
336 close(buffer->dmabuf_fds[i]);
337
338 running = false;
339
340 fprintf(stderr, "Error: zwp_linux_buffer_params.create failed.\n");
341}
342
343static const struct zwp_linux_buffer_params_v1_listener params_listener = {
344 create_succeeded,
345 create_failed
346};
347
348static void
349create_dmabuf_buffer(struct display *display, struct buffer *buffer)
350{
351 struct zwp_linux_buffer_params_v1 *params;
352 uint64_t modifier;
353 uint32_t flags;
354 unsigned i;
355
356 modifier = 0;
357 flags = ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT;
358
359 params = zwp_linux_dmabuf_v1_create_params(display->dmabuf);
360 for (i = 0; i < display->format.num_planes; ++i)
361 zwp_linux_buffer_params_v1_add(params,
362 buffer->dmabuf_fds[i],
363 i, /* plane_idx */
364 buffer->data_offsets[i], /* offset */
365 display->format.strides[i],
366 modifier >> 32,
367 modifier & 0xffffffff);
368 zwp_linux_buffer_params_v1_add_listener(params, &params_listener,
369 buffer);
370 zwp_linux_buffer_params_v1_create(params,
371 display->format.width,
372 display->format.height,
373 display->drm_format,
374 flags);
375}
376
377static int
378buffer_export(struct display *display, int index, int dmafd[])
379{
380 struct v4l2_exportbuffer expbuf;
381 unsigned i;
382
383 CLEAR(expbuf);
384
385 for (i = 0; i < display->format.num_planes; ++i) {
386 expbuf.type = display->format.type;
387 expbuf.index = index;
388 expbuf.plane = i;
389 if (xioctl(display->v4l_fd, VIDIOC_EXPBUF, &expbuf) == -1) {
390 perror("VIDIOC_EXPBUF");
391 while (i)
392 close(dmafd[--i]);
393 return 0;
394 }
395 dmafd[i] = expbuf.fd;
396 }
397
398 return 1;
399}
400
401static int
402queue_initial_buffers(struct display *display,
403 struct buffer buffers[NUM_BUFFERS])
404{
405 struct buffer *buffer;
406 int index;
407
408 for (index = 0; index < NUM_BUFFERS; ++index) {
409 buffer = &buffers[index];
410 buffer->display = display;
411 buffer->index = index;
412
413 if (!queue(display, buffer)) {
414 fprintf(stderr, "Failed to queue buffer\n");
415 return 0;
416 }
417
418 assert(!buffer->buffer);
419 if (!buffer_export(display, index, buffer->dmabuf_fds))
420 return 0;
421
422 create_dmabuf_buffer(display, buffer);
423 }
424
425 return 1;
426}
427
428static int
429dequeue(struct display *display)
430{
431 struct v4l2_buffer buf;
432 struct v4l2_plane planes[VIDEO_MAX_PLANES];
433
434 CLEAR(buf);
435 buf.type = display->format.type;
436 buf.memory = V4L2_MEMORY_MMAP;
437 buf.length = VIDEO_MAX_PLANES;
438 buf.m.planes = planes;
439
440 /* This ioctl is blocking until a buffer is ready to be displayed */
441 if (xioctl(display->v4l_fd, VIDIOC_DQBUF, &buf) == -1) {
442 perror("VIDIOC_DQBUF");
443 return -1;
444 }
445
446 assert(buf.flags & V4L2_BUF_FLAG_DONE);
447
448 return buf.index;
449}
450
451static int
452fill_buffer_format(struct display *display)
453{
454 struct v4l2_format fmt;
455 struct v4l2_pix_format *pix;
456 struct v4l2_pix_format_mplane *pix_mp;
457 int i;
458 char buf[4];
459
460 CLEAR(fmt);
461 fmt.type = display->format.type;
462
463 /* Preserve original settings as set by v4l2-ctl for example */
464 if (xioctl(display->v4l_fd, VIDIOC_G_FMT, &fmt) == -1) {
465 perror("VIDIOC_G_FMT");
466 return 0;
467 }
468
469 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
470 pix = &fmt.fmt.pix;
471
472 printf("%d×%d, %.4s\n", pix->width, pix->height,
473 dump_format(pix->pixelformat, buf));
474
475 display->format.num_planes = 1;
476 display->format.width = pix->width;
477 display->format.height = pix->height;
478 display->format.strides[0] = pix->bytesperline;
479 } else {
480 pix_mp = &fmt.fmt.pix_mp;
481
482 display->format.num_planes = pix_mp->num_planes;
483 display->format.width = pix_mp->width;
484 display->format.height = pix_mp->height;
485
486 for (i = 0; i < pix_mp->num_planes; ++i)
487 display->format.strides[i] = pix_mp->plane_fmt[i].bytesperline;
488
489 printf("%d×%d, %.4s, %d planes\n",
490 pix_mp->width, pix_mp->height,
491 dump_format(pix_mp->pixelformat, buf),
492 pix_mp->num_planes);
493 }
494
495 return 1;
496}
497
498static int
499v4l_init(struct display *display, struct buffer buffers[NUM_BUFFERS]) {
500 if (!fill_buffer_format(display)) {
501 fprintf(stderr, "Failed to fill buffer format\n");
502 return 0;
503 }
504
505 if (!queue_initial_buffers(display, buffers)) {
506 fprintf(stderr, "Failed to queue initial buffers\n");
507 return 0;
508 }
509
510 return 1;
511}
512
513static int
514start_capture(struct display *display)
515{
516 int type = display->format.type;
517
518 if (xioctl(display->v4l_fd, VIDIOC_STREAMON, &type) == -1) {
519 perror("VIDIOC_STREAMON");
520 return 0;
521 }
522
523 return 1;
524}
525
526static void
527handle_configure(void *data, struct xdg_surface *surface,
528 int32_t width, int32_t height,
529 struct wl_array *states, uint32_t serial)
530{
531}
532
533static void
534handle_delete(void *data, struct xdg_surface *xdg_surface)
535{
536 running = false;
537}
538
539static const struct xdg_surface_listener xdg_surface_listener = {
540 handle_configure,
541 handle_delete,
542};
543
544static struct window *
545create_window(struct display *display)
546{
547 struct window *window;
548
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800549 window = zalloc(sizeof *window);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000550 if (!window)
551 return NULL;
552
553 window->callback = NULL;
554 window->display = display;
555 window->surface = wl_compositor_create_surface(display->compositor);
556
557 if (display->shell) {
558 window->xdg_surface =
559 xdg_shell_get_xdg_surface(display->shell,
560 window->surface);
561
562 assert(window->xdg_surface);
563
564 xdg_surface_add_listener(window->xdg_surface,
565 &xdg_surface_listener, window);
566
567 xdg_surface_set_title(window->xdg_surface, "simple-dmabuf-v4l");
568 } else if (display->fshell) {
569 zwp_fullscreen_shell_v1_present_surface(display->fshell,
570 window->surface,
571 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
572 NULL);
573 } else {
574 assert(0);
575 }
576
577 return window;
578}
579
580static void
581destroy_window(struct window *window)
582{
583 int i;
584 unsigned j;
585
586 if (window->callback)
587 wl_callback_destroy(window->callback);
588
589 if (window->xdg_surface)
590 xdg_surface_destroy(window->xdg_surface);
591 wl_surface_destroy(window->surface);
592
593 for (i = 0; i < NUM_BUFFERS; i++) {
594 if (!window->buffers[i].buffer)
595 continue;
596
597 wl_buffer_destroy(window->buffers[i].buffer);
598 for (j = 0; j < window->display->format.num_planes; ++j)
599 close(window->buffers[i].dmabuf_fds[j]);
600 }
601
602 v4l_shutdown(window->display);
603
604 free(window);
605}
606
607static const struct wl_callback_listener frame_listener;
608
609static void
610redraw(void *data, struct wl_callback *callback, uint32_t time)
611{
612 struct window *window = data;
613 struct buffer *buffer;
614 int index, num_busy = 0;
615
616 /* Check for a deadlock situation where we would block forever trying
617 * to dequeue a buffer while all of them are locked by the compositor.
618 */
619 for (index = 0; index < NUM_BUFFERS; ++index)
620 if (window->buffers[index].busy)
621 ++num_busy;
622
623 /* A robust application would just postpone redraw until it has queued
624 * a buffer.
625 */
626 assert(num_busy < NUM_BUFFERS);
627
628 index = dequeue(window->display);
629 if (index < 0) {
630 /* We couldn’t get any buffer out of the camera, exiting. */
631 running = false;
632 return;
633 }
634
635 buffer = &window->buffers[index];
636 assert(!buffer->busy);
637
638 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
639 wl_surface_damage(window->surface, 0, 0,
640 window->display->format.width,
641 window->display->format.height);
642
643 if (callback)
644 wl_callback_destroy(callback);
645
646 window->callback = wl_surface_frame(window->surface);
647 wl_callback_add_listener(window->callback, &frame_listener, window);
648 wl_surface_commit(window->surface);
649 buffer->busy = 1;
650}
651
652static const struct wl_callback_listener frame_listener = {
653 redraw
654};
655
656static void
657dmabuf_format(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
658 uint32_t format)
659{
660 struct display *d = data;
661
662 if (format == d->drm_format)
663 d->requested_format_found = true;
664}
665
666static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
667 dmabuf_format
668};
669
670static void
671keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
672 uint32_t format, int fd, uint32_t size)
673{
674 /* Just so we don’t leak the keymap fd */
675 close(fd);
676}
677
678static void
679keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
680 uint32_t serial, struct wl_surface *surface,
681 struct wl_array *keys)
682{
683}
684
685static void
686keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
687 uint32_t serial, struct wl_surface *surface)
688{
689}
690
691static void
692keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
693 uint32_t serial, uint32_t time, uint32_t key,
694 uint32_t state)
695{
696 struct display *d = data;
697
698 if (!d->shell)
699 return;
700
701 if (key == KEY_ESC && state)
702 running = false;
703}
704
705static void
706keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
707 uint32_t serial, uint32_t mods_depressed,
708 uint32_t mods_latched, uint32_t mods_locked,
709 uint32_t group)
710{
711}
712
713static const struct wl_keyboard_listener keyboard_listener = {
714 keyboard_handle_keymap,
715 keyboard_handle_enter,
716 keyboard_handle_leave,
717 keyboard_handle_key,
718 keyboard_handle_modifiers,
719};
720
721static void
722seat_handle_capabilities(void *data, struct wl_seat *seat,
723 enum wl_seat_capability caps)
724{
725 struct display *d = data;
726
727 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
728 d->keyboard = wl_seat_get_keyboard(seat);
729 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
730 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
731 wl_keyboard_destroy(d->keyboard);
732 d->keyboard = NULL;
733 }
734}
735
736static const struct wl_seat_listener seat_listener = {
737 seat_handle_capabilities,
738};
739
740static void
741xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
742{
743 xdg_shell_pong(shell, serial);
744}
745
746static const struct xdg_shell_listener xdg_shell_listener = {
747 xdg_shell_ping,
748};
749
750#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
751#ifdef static_assert
752static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
753 "Interface version doesn't match implementation version");
754#endif
755
756static void
757registry_handle_global(void *data, struct wl_registry *registry,
758 uint32_t id, const char *interface, uint32_t version)
759{
760 struct display *d = data;
761
762 if (strcmp(interface, "wl_compositor") == 0) {
763 d->compositor =
764 wl_registry_bind(registry,
765 id, &wl_compositor_interface, 1);
766 } else if (strcmp(interface, "wl_seat") == 0) {
767 d->seat = wl_registry_bind(registry,
768 id, &wl_seat_interface, 1);
769 wl_seat_add_listener(d->seat, &seat_listener, d);
770 } else if (strcmp(interface, "xdg_shell") == 0) {
771 d->shell = wl_registry_bind(registry,
772 id, &xdg_shell_interface, 1);
773 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
774 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
775 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
776 d->fshell = wl_registry_bind(registry,
777 id, &zwp_fullscreen_shell_v1_interface,
778 1);
779 } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0) {
780 d->dmabuf = wl_registry_bind(registry,
781 id, &zwp_linux_dmabuf_v1_interface,
782 1);
783 zwp_linux_dmabuf_v1_add_listener(d->dmabuf, &dmabuf_listener,
784 d);
785 }
786}
787
788static void
789registry_handle_global_remove(void *data, struct wl_registry *registry,
790 uint32_t name)
791{
792}
793
794static const struct wl_registry_listener registry_listener = {
795 registry_handle_global,
796 registry_handle_global_remove
797};
798
799static struct display *
800create_display(uint32_t requested_format)
801{
802 struct display *display;
803
804 display = malloc(sizeof *display);
805 if (display == NULL) {
806 fprintf(stderr, "out of memory\n");
807 exit(1);
808 }
809 display->display = wl_display_connect(NULL);
810 assert(display->display);
811
812 display->drm_format = requested_format;
813
814 display->registry = wl_display_get_registry(display->display);
815 wl_registry_add_listener(display->registry,
816 &registry_listener, display);
817 wl_display_roundtrip(display->display);
818 if (display->dmabuf == NULL) {
819 fprintf(stderr, "No zwp_linux_dmabuf global\n");
820 exit(1);
821 }
822
823 wl_display_roundtrip(display->display);
824
825 /* XXX: fake, because the compositor does not yet advertise anything */
826 display->requested_format_found = true;
827
828 if (!display->requested_format_found) {
829 fprintf(stderr, "DRM_FORMAT_YUYV not available\n");
830 exit(1);
831 }
832
833 return display;
834}
835
836static void
837destroy_display(struct display *display)
838{
839 if (display->dmabuf)
840 zwp_linux_dmabuf_v1_destroy(display->dmabuf);
841
842 if (display->shell)
843 xdg_shell_destroy(display->shell);
844
845 if (display->fshell)
846 zwp_fullscreen_shell_v1_release(display->fshell);
847
848 if (display->compositor)
849 wl_compositor_destroy(display->compositor);
850
851 wl_registry_destroy(display->registry);
852 wl_display_flush(display->display);
853 wl_display_disconnect(display->display);
854 free(display);
855}
856
857static void
858usage(const char *argv0)
859{
860 printf("Usage: %s [V4L2 device] [V4L2 format] [DRM format]\n"
861 "\n"
862 "The default V4L2 device is /dev/video0\n"
863 "\n"
864 "Both formats are FOURCC values (see http://fourcc.org/)\n"
865 "V4L2 formats are defined in <linux/videodev2.h>\n"
866 "DRM formats are defined in <libdrm/drm_fourcc.h>\n"
867 "The default for both formats is YUYV.\n"
868 "If the V4L2 and DRM formats differ, the data is simply "
869 "reinterpreted rather than converted.\n", argv0);
870 exit(0);
871}
872
873static void
874signal_int(int signum)
875{
876 running = false;
877}
878
879int
880main(int argc, char **argv)
881{
882 struct sigaction sigint;
883 struct display *display;
884 struct window *window;
885 const char *v4l_device;
886 uint32_t v4l_format, drm_format;
887 int ret = 0;
888
889 if (argc < 2) {
890 v4l_device = "/dev/video0";
891 } else if (!strcmp(argv[1], "--help")) {
892 usage(argv[0]);
893 } else {
894 v4l_device = argv[1];
895 }
896
897 if (argc < 3)
898 v4l_format = parse_format("YUYV");
899 else
900 v4l_format = parse_format(argv[2]);
901
902 if (argc < 4)
903 drm_format = v4l_format;
904 else
905 drm_format = parse_format(argv[3]);
906
907 display = create_display(drm_format);
908 display->format.format = v4l_format;
909
910 window = create_window(display);
911 if (!window)
912 return 1;
913
914 if (!v4l_connect(display, v4l_device))
915 return 1;
916
917 if (!v4l_init(display, window->buffers))
918 return 1;
919
920 sigint.sa_handler = signal_int;
921 sigemptyset(&sigint.sa_mask);
922 sigint.sa_flags = SA_RESETHAND;
923 sigaction(SIGINT, &sigint, NULL);
924
925 /* Here we retrieve the linux-dmabuf objects, or error */
926 wl_display_roundtrip(display->display);
927
928 /* In case of error, running will be 0 */
929 if (!running)
930 return 1;
931
932 /* We got all of our buffers, we can start the capture! */
933 if (!start_capture(display))
934 return 1;
935
936 redraw(window, NULL, 0);
937
938 while (running && ret != -1)
939 ret = wl_display_dispatch(display->display);
940
941 fprintf(stderr, "simple-dmabuf-v4l exiting\n");
942 destroy_window(window);
943 destroy_display(display);
944
945 return 0;
946}