18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
28c2ecf20Sopenharmony_ci.. c:namespace:: V4L
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci.. _func-select:
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci*************
78c2ecf20Sopenharmony_ciV4L2 select()
88c2ecf20Sopenharmony_ci*************
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciName
118c2ecf20Sopenharmony_ci====
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_civ4l2-select - Synchronous I/O multiplexing
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ciSynopsis
168c2ecf20Sopenharmony_ci========
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci.. code-block:: c
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci    #include <sys/time.h>
218c2ecf20Sopenharmony_ci    #include <sys/types.h>
228c2ecf20Sopenharmony_ci    #include <unistd.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci.. c:function:: int select( int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout )
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciArguments
278c2ecf20Sopenharmony_ci=========
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci``nfds``
308c2ecf20Sopenharmony_ci  The highest-numbered file descriptor in any of the three sets, plus 1.
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci``readfds``
338c2ecf20Sopenharmony_ci  File descriptions to be watched if a read() call won't block.
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci``writefds``
368c2ecf20Sopenharmony_ci  File descriptions to be watched if a write() won't block.
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci``exceptfds``
398c2ecf20Sopenharmony_ci  File descriptions to be watched for V4L2 events.
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci``timeout``
428c2ecf20Sopenharmony_ci  Maximum time to wait.
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ciDescription
458c2ecf20Sopenharmony_ci===========
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciWith the :c:func:`select()` function applications can suspend
488c2ecf20Sopenharmony_ciexecution until the driver has captured data or is ready to accept data
498c2ecf20Sopenharmony_cifor output.
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciWhen streaming I/O has been negotiated this function waits until a
528c2ecf20Sopenharmony_cibuffer has been filled or displayed and can be dequeued with the
538c2ecf20Sopenharmony_ci:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. When buffers are already in
548c2ecf20Sopenharmony_cithe outgoing queue of the driver the function returns immediately.
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ciOn success :c:func:`select()` returns the total number of bits set in
578c2ecf20Sopenharmony_ci``fd_set``. When the function timed out it returns
588c2ecf20Sopenharmony_cia value of zero. On failure it returns -1 and the ``errno`` variable is
598c2ecf20Sopenharmony_ciset appropriately. When the application did not call
608c2ecf20Sopenharmony_ci:ref:`VIDIOC_QBUF` or
618c2ecf20Sopenharmony_ci:ref:`VIDIOC_STREAMON` yet the :c:func:`select()`
628c2ecf20Sopenharmony_cifunction succeeds, setting the bit of the file descriptor in ``readfds``
638c2ecf20Sopenharmony_cior ``writefds``, but subsequent :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`
648c2ecf20Sopenharmony_cicalls will fail. [#f1]_
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ciWhen use of the :c:func:`read()` function has been negotiated and the
678c2ecf20Sopenharmony_cidriver does not capture yet, the :c:func:`select()` function starts
688c2ecf20Sopenharmony_cicapturing. When that fails, :c:func:`select()` returns successful and
698c2ecf20Sopenharmony_cia subsequent :c:func:`read()` call, which also attempts to start
708c2ecf20Sopenharmony_cicapturing, will return an appropriate error code. When the driver
718c2ecf20Sopenharmony_cicaptures continuously (as opposed to, for example, still images) and
728c2ecf20Sopenharmony_cidata is already available the :c:func:`select()` function returns
738c2ecf20Sopenharmony_ciimmediately.
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ciWhen use of the :c:func:`write()` function has been negotiated the
768c2ecf20Sopenharmony_ci:c:func:`select()` function just waits until the driver is ready for a
778c2ecf20Sopenharmony_cinon-blocking :c:func:`write()` call.
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ciAll drivers implementing the :c:func:`read()` or :c:func:`write()`
808c2ecf20Sopenharmony_cifunction or streaming I/O must also support the :c:func:`select()`
818c2ecf20Sopenharmony_cifunction.
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciFor more details see the :c:func:`select()` manual page.
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciReturn Value
868c2ecf20Sopenharmony_ci============
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ciOn success, :c:func:`select()` returns the number of descriptors
898c2ecf20Sopenharmony_cicontained in the three returned descriptor sets, which will be zero if
908c2ecf20Sopenharmony_cithe timeout expired. On error -1 is returned, and the ``errno`` variable
918c2ecf20Sopenharmony_ciis set appropriately; the sets and ``timeout`` are undefined. Possible
928c2ecf20Sopenharmony_cierror codes are:
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciEBADF
958c2ecf20Sopenharmony_ci    One or more of the file descriptor sets specified a file descriptor
968c2ecf20Sopenharmony_ci    that is not open.
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciEBUSY
998c2ecf20Sopenharmony_ci    The driver does not support multiple read or write streams and the
1008c2ecf20Sopenharmony_ci    device is already in use.
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ciEFAULT
1038c2ecf20Sopenharmony_ci    The ``readfds``, ``writefds``, ``exceptfds`` or ``timeout`` pointer
1048c2ecf20Sopenharmony_ci    references an inaccessible memory area.
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciEINTR
1078c2ecf20Sopenharmony_ci    The call was interrupted by a signal.
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciEINVAL
1108c2ecf20Sopenharmony_ci    The ``nfds`` argument is less than zero or greater than
1118c2ecf20Sopenharmony_ci    ``FD_SETSIZE``.
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci.. [#f1]
1148c2ecf20Sopenharmony_ci   The Linux kernel implements :c:func:`select()` like the
1158c2ecf20Sopenharmony_ci   :c:func:`poll()` function, but :c:func:`select()` cannot
1168c2ecf20Sopenharmony_ci   return a ``POLLERR``.
117