162306a36Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
262306a36Sopenharmony_ci.. c:namespace:: V4L
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci.. _func-select:
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci*************
762306a36Sopenharmony_ciV4L2 select()
862306a36Sopenharmony_ci*************
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciName
1162306a36Sopenharmony_ci====
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_civ4l2-select - Synchronous I/O multiplexing
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciSynopsis
1662306a36Sopenharmony_ci========
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci.. code-block:: c
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci    #include <sys/time.h>
2162306a36Sopenharmony_ci    #include <sys/types.h>
2262306a36Sopenharmony_ci    #include <unistd.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci.. c:function:: int select( int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout )
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ciArguments
2762306a36Sopenharmony_ci=========
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci``nfds``
3062306a36Sopenharmony_ci  The highest-numbered file descriptor in any of the three sets, plus 1.
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci``readfds``
3362306a36Sopenharmony_ci  File descriptions to be watched if a read() call won't block.
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci``writefds``
3662306a36Sopenharmony_ci  File descriptions to be watched if a write() won't block.
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci``exceptfds``
3962306a36Sopenharmony_ci  File descriptions to be watched for V4L2 events.
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci``timeout``
4262306a36Sopenharmony_ci  Maximum time to wait.
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ciDescription
4562306a36Sopenharmony_ci===========
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciWith the :c:func:`select()` function applications can suspend
4862306a36Sopenharmony_ciexecution until the driver has captured data or is ready to accept data
4962306a36Sopenharmony_cifor output.
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ciWhen streaming I/O has been negotiated this function waits until a
5262306a36Sopenharmony_cibuffer has been filled or displayed and can be dequeued with the
5362306a36Sopenharmony_ci:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. When buffers are already in
5462306a36Sopenharmony_cithe outgoing queue of the driver the function returns immediately.
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ciOn success :c:func:`select()` returns the total number of bits set in
5762306a36Sopenharmony_ci``fd_set``. When the function timed out it returns
5862306a36Sopenharmony_cia value of zero. On failure it returns -1 and the ``errno`` variable is
5962306a36Sopenharmony_ciset appropriately. When the application did not call
6062306a36Sopenharmony_ci:ref:`VIDIOC_QBUF` or
6162306a36Sopenharmony_ci:ref:`VIDIOC_STREAMON` yet the :c:func:`select()`
6262306a36Sopenharmony_cifunction succeeds, setting the bit of the file descriptor in ``readfds``
6362306a36Sopenharmony_cior ``writefds``, but subsequent :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`
6462306a36Sopenharmony_cicalls will fail. [#f1]_
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ciWhen use of the :c:func:`read()` function has been negotiated and the
6762306a36Sopenharmony_cidriver does not capture yet, the :c:func:`select()` function starts
6862306a36Sopenharmony_cicapturing. When that fails, :c:func:`select()` returns successful and
6962306a36Sopenharmony_cia subsequent :c:func:`read()` call, which also attempts to start
7062306a36Sopenharmony_cicapturing, will return an appropriate error code. When the driver
7162306a36Sopenharmony_cicaptures continuously (as opposed to, for example, still images) and
7262306a36Sopenharmony_cidata is already available the :c:func:`select()` function returns
7362306a36Sopenharmony_ciimmediately.
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciWhen use of the :c:func:`write()` function has been negotiated the
7662306a36Sopenharmony_ci:c:func:`select()` function just waits until the driver is ready for a
7762306a36Sopenharmony_cinon-blocking :c:func:`write()` call.
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciAll drivers implementing the :c:func:`read()` or :c:func:`write()`
8062306a36Sopenharmony_cifunction or streaming I/O must also support the :c:func:`select()`
8162306a36Sopenharmony_cifunction.
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ciFor more details see the :c:func:`select()` manual page.
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ciReturn Value
8662306a36Sopenharmony_ci============
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciOn success, :c:func:`select()` returns the number of descriptors
8962306a36Sopenharmony_cicontained in the three returned descriptor sets, which will be zero if
9062306a36Sopenharmony_cithe timeout expired. On error -1 is returned, and the ``errno`` variable
9162306a36Sopenharmony_ciis set appropriately; the sets and ``timeout`` are undefined. Possible
9262306a36Sopenharmony_cierror codes are:
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ciEBADF
9562306a36Sopenharmony_ci    One or more of the file descriptor sets specified a file descriptor
9662306a36Sopenharmony_ci    that is not open.
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ciEBUSY
9962306a36Sopenharmony_ci    The driver does not support multiple read or write streams and the
10062306a36Sopenharmony_ci    device is already in use.
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciEFAULT
10362306a36Sopenharmony_ci    The ``readfds``, ``writefds``, ``exceptfds`` or ``timeout`` pointer
10462306a36Sopenharmony_ci    references an inaccessible memory area.
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciEINTR
10762306a36Sopenharmony_ci    The call was interrupted by a signal.
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ciEINVAL
11062306a36Sopenharmony_ci    The ``nfds`` argument is less than zero or greater than
11162306a36Sopenharmony_ci    ``FD_SETSIZE``.
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci.. [#f1]
11462306a36Sopenharmony_ci   The Linux kernel implements :c:func:`select()` like the
11562306a36Sopenharmony_ci   :c:func:`poll()` function, but :c:func:`select()` cannot
11662306a36Sopenharmony_ci   return a ``POLLERR``.
117