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