Skip to content

Commit 30f6501

Browse files
committed
Add new non-blocking open function
fixup fixup
1 parent 75c42c4 commit 30f6501

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/lib.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod shared;
1414
pub mod stream;
1515
pub mod uvc;
1616

17-
use nix::errno::Errno;
17+
use nix::{errno::Errno, libc};
1818
use pixel_format::PixelFormat;
1919
use std::{
2020
fmt,
@@ -86,11 +86,24 @@ impl Device {
8686
///
8787
/// If the path does not refer to a V4L2 device node, an error will be returned.
8888
pub fn open<A: AsRef<Path>>(path: A) -> io::Result<Self> {
89-
Self::open_impl(path.as_ref())
89+
Self::open_impl(path.as_ref(), true)
9090
}
9191

92-
fn open_impl(path: &Path) -> io::Result<Self> {
93-
let file = OpenOptions::new().read(true).write(true).open(path)?;
92+
/// Opens a V4L2 device file from the given path.
93+
///
94+
/// If the path does not refer to a V4L2 device node, an error will be returned.
95+
pub fn open_non_blocking<A: AsRef<Path>>(path: A) -> io::Result<Self> {
96+
Self::open_impl(path.as_ref(), false)
97+
}
98+
99+
fn open_impl(path: &Path, blocking: bool) -> io::Result<Self> {
100+
let mut open_options = OpenOptions::new();
101+
open_options.read(true).write(true);
102+
if !blocking {
103+
open_options.custom_flags(libc::O_NONBLOCK);
104+
}
105+
let file = open_options.open(path)?;
106+
94107
let mut this = Self {
95108
file,
96109
available_capabilities: CapabilityFlags::empty(),

0 commit comments

Comments
 (0)