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