1059b1c5bSMauro Carvalho Chehab.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2407e84cdSMauro Carvalho Chehab.. c:namespace:: V4L
354f38fcaSMauro Carvalho Chehab
454f38fcaSMauro Carvalho Chehab.. _func-select:
554f38fcaSMauro Carvalho Chehab
654f38fcaSMauro Carvalho Chehab*************
754f38fcaSMauro Carvalho ChehabV4L2 select()
854f38fcaSMauro Carvalho Chehab*************
954f38fcaSMauro Carvalho Chehab
1054f38fcaSMauro Carvalho ChehabName
1154f38fcaSMauro Carvalho Chehab====
1254f38fcaSMauro Carvalho Chehab
1354f38fcaSMauro Carvalho Chehabv4l2-select - Synchronous I/O multiplexing
1454f38fcaSMauro Carvalho Chehab
1554f38fcaSMauro Carvalho ChehabSynopsis
1654f38fcaSMauro Carvalho Chehab========
1754f38fcaSMauro Carvalho Chehab
1854f38fcaSMauro Carvalho Chehab.. code-block:: c
1954f38fcaSMauro Carvalho Chehab
2054f38fcaSMauro Carvalho Chehab    #include <sys/time.h>
2154f38fcaSMauro Carvalho Chehab    #include <sys/types.h>
2254f38fcaSMauro Carvalho Chehab    #include <unistd.h>
2354f38fcaSMauro Carvalho Chehab
2454f38fcaSMauro Carvalho Chehab.. c:function:: int select( int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout )
2554f38fcaSMauro Carvalho Chehab
2654f38fcaSMauro Carvalho ChehabArguments
2754f38fcaSMauro Carvalho Chehab=========
2854f38fcaSMauro Carvalho Chehab
2954f38fcaSMauro Carvalho Chehab``nfds``
3054f38fcaSMauro Carvalho Chehab  The highest-numbered file descriptor in any of the three sets, plus 1.
3154f38fcaSMauro Carvalho Chehab
3254f38fcaSMauro Carvalho Chehab``readfds``
3354f38fcaSMauro Carvalho Chehab  File descriptions to be watched if a read() call won't block.
3454f38fcaSMauro Carvalho Chehab
3554f38fcaSMauro Carvalho Chehab``writefds``
3654f38fcaSMauro Carvalho Chehab  File descriptions to be watched if a write() won't block.
3754f38fcaSMauro Carvalho Chehab
3854f38fcaSMauro Carvalho Chehab``exceptfds``
3954f38fcaSMauro Carvalho Chehab  File descriptions to be watched for V4L2 events.
4054f38fcaSMauro Carvalho Chehab
4154f38fcaSMauro Carvalho Chehab``timeout``
4254f38fcaSMauro Carvalho Chehab  Maximum time to wait.
4354f38fcaSMauro Carvalho Chehab
4454f38fcaSMauro Carvalho ChehabDescription
4554f38fcaSMauro Carvalho Chehab===========
4654f38fcaSMauro Carvalho Chehab
47407e84cdSMauro Carvalho ChehabWith the :c:func:`select()` function applications can suspend
4854f38fcaSMauro Carvalho Chehabexecution until the driver has captured data or is ready to accept data
4954f38fcaSMauro Carvalho Chehabfor output.
5054f38fcaSMauro Carvalho Chehab
5154f38fcaSMauro Carvalho ChehabWhen streaming I/O has been negotiated this function waits until a
5254f38fcaSMauro Carvalho Chehabbuffer has been filled or displayed and can be dequeued with the
5354f38fcaSMauro Carvalho Chehab:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. When buffers are already in
5454f38fcaSMauro Carvalho Chehabthe outgoing queue of the driver the function returns immediately.
5554f38fcaSMauro Carvalho Chehab
56407e84cdSMauro Carvalho ChehabOn success :c:func:`select()` returns the total number of bits set in
57407e84cdSMauro Carvalho Chehab``fd_set``. When the function timed out it returns
5854f38fcaSMauro Carvalho Chehaba value of zero. On failure it returns -1 and the ``errno`` variable is
5954f38fcaSMauro Carvalho Chehabset appropriately. When the application did not call
6054f38fcaSMauro Carvalho Chehab:ref:`VIDIOC_QBUF` or
61407e84cdSMauro Carvalho Chehab:ref:`VIDIOC_STREAMON` yet the :c:func:`select()`
6254f38fcaSMauro Carvalho Chehabfunction succeeds, setting the bit of the file descriptor in ``readfds``
6354f38fcaSMauro Carvalho Chehabor ``writefds``, but subsequent :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`
6454f38fcaSMauro Carvalho Chehabcalls will fail. [#f1]_
6554f38fcaSMauro Carvalho Chehab
66407e84cdSMauro Carvalho ChehabWhen use of the :c:func:`read()` function has been negotiated and the
67407e84cdSMauro Carvalho Chehabdriver does not capture yet, the :c:func:`select()` function starts
68407e84cdSMauro Carvalho Chehabcapturing. When that fails, :c:func:`select()` returns successful and
69407e84cdSMauro Carvalho Chehaba subsequent :c:func:`read()` call, which also attempts to start
7054f38fcaSMauro Carvalho Chehabcapturing, will return an appropriate error code. When the driver
7154f38fcaSMauro Carvalho Chehabcaptures continuously (as opposed to, for example, still images) and
72407e84cdSMauro Carvalho Chehabdata is already available the :c:func:`select()` function returns
7354f38fcaSMauro Carvalho Chehabimmediately.
7454f38fcaSMauro Carvalho Chehab
75407e84cdSMauro Carvalho ChehabWhen use of the :c:func:`write()` function has been negotiated the
76407e84cdSMauro Carvalho Chehab:c:func:`select()` function just waits until the driver is ready for a
77407e84cdSMauro Carvalho Chehabnon-blocking :c:func:`write()` call.
7854f38fcaSMauro Carvalho Chehab
79407e84cdSMauro Carvalho ChehabAll drivers implementing the :c:func:`read()` or :c:func:`write()`
80407e84cdSMauro Carvalho Chehabfunction or streaming I/O must also support the :c:func:`select()`
8154f38fcaSMauro Carvalho Chehabfunction.
8254f38fcaSMauro Carvalho Chehab
83407e84cdSMauro Carvalho ChehabFor more details see the :c:func:`select()` manual page.
8454f38fcaSMauro Carvalho Chehab
8554f38fcaSMauro Carvalho ChehabReturn Value
8654f38fcaSMauro Carvalho Chehab============
8754f38fcaSMauro Carvalho Chehab
88407e84cdSMauro Carvalho ChehabOn success, :c:func:`select()` returns the number of descriptors
8954f38fcaSMauro Carvalho Chehabcontained in the three returned descriptor sets, which will be zero if
9054f38fcaSMauro Carvalho Chehabthe timeout expired. On error -1 is returned, and the ``errno`` variable
9154f38fcaSMauro Carvalho Chehabis set appropriately; the sets and ``timeout`` are undefined. Possible
9254f38fcaSMauro Carvalho Chehaberror codes are:
9354f38fcaSMauro Carvalho Chehab
9454f38fcaSMauro Carvalho ChehabEBADF
9554f38fcaSMauro Carvalho Chehab    One or more of the file descriptor sets specified a file descriptor
9654f38fcaSMauro Carvalho Chehab    that is not open.
9754f38fcaSMauro Carvalho Chehab
9854f38fcaSMauro Carvalho ChehabEBUSY
9954f38fcaSMauro Carvalho Chehab    The driver does not support multiple read or write streams and the
10054f38fcaSMauro Carvalho Chehab    device is already in use.
10154f38fcaSMauro Carvalho Chehab
10254f38fcaSMauro Carvalho ChehabEFAULT
10354f38fcaSMauro Carvalho Chehab    The ``readfds``, ``writefds``, ``exceptfds`` or ``timeout`` pointer
10454f38fcaSMauro Carvalho Chehab    references an inaccessible memory area.
10554f38fcaSMauro Carvalho Chehab
10654f38fcaSMauro Carvalho ChehabEINTR
10754f38fcaSMauro Carvalho Chehab    The call was interrupted by a signal.
10854f38fcaSMauro Carvalho Chehab
10954f38fcaSMauro Carvalho ChehabEINVAL
11054f38fcaSMauro Carvalho Chehab    The ``nfds`` argument is less than zero or greater than
11154f38fcaSMauro Carvalho Chehab    ``FD_SETSIZE``.
11254f38fcaSMauro Carvalho Chehab
11354f38fcaSMauro Carvalho Chehab.. [#f1]
114407e84cdSMauro Carvalho Chehab   The Linux kernel implements :c:func:`select()` like the
115407e84cdSMauro Carvalho Chehab   :c:func:`poll()` function, but :c:func:`select()` cannot
11654f38fcaSMauro Carvalho Chehab   return a ``POLLERR``.
117