diff --git a/examples/window-rgb.rs b/examples/window-rgb.rs
index ece4268..71953c2 100644
--- a/examples/window-rgb.rs
+++ b/examples/window-rgb.rs
@@ -2,7 +2,7 @@ use std::num::NonZeroU32;
use winit::{
dpi::PhysicalSize,
- event::{Event, WindowEvent},
+ event::{DeviceEvent, ElementState, Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
@@ -15,7 +15,7 @@ fn main() {
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
- let camera = Camera::new_default_device();
+ let mut camera = Camera::new_default_device();
camera.start();
event_loop.run(move |event, _x, control_flow| {
@@ -23,9 +23,7 @@ fn main() {
match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
- let Some(frame) = camera.wait_for_frame() else {
- return
- };
+ let Some(frame) = camera.wait_for_frame() else { return };
let (w, h) = frame.size_u32();
surface.resize(NonZeroU32::new(w).unwrap(), NonZeroU32::new(h).unwrap()).unwrap();
@@ -47,6 +45,12 @@ fn main() {
Event::RedrawEventsCleared => {
window.request_redraw();
}
+ Event::DeviceEvent {
+ event: DeviceEvent::Button { button: _, state: ElementState::Released },
+ device_id: _,
+ } => {
+ camera.change_device();
+ }
_ => {}
}
});
diff --git a/src/camera.rs b/src/camera.rs
index 0527ecc..09e56e5 100644
--- a/src/camera.rs
+++ b/src/camera.rs
@@ -37,6 +37,10 @@ impl Camera {
pub fn wait_for_frame(&self) -> Option {
self.inner.wait_for_frame().map(|inner| Frame { inner })
}
+
+ pub fn change_device(&mut self) {
+ self.inner.change_device();
+ }
}
impl Frame {
@@ -66,4 +70,5 @@ pub(crate) trait InnerCamera: std::fmt::Debug {
fn start(&self);
fn stop(&self);
fn wait_for_frame(&self) -> Option;
+ fn change_device(&mut self);
}
diff --git a/src/linux_v4l2/mod.rs b/src/linux_v4l2/mod.rs
index f7d82b7..2e9d16c 100644
--- a/src/linux_v4l2/mod.rs
+++ b/src/linux_v4l2/mod.rs
@@ -1,11 +1,13 @@
use ffimage::color::Bgra;
+use v4l::context::Node;
use v4l::io::traits::CaptureStream;
use v4l::video::Capture;
use v4l::*;
use std::marker::PhantomData;
+
use std::sync::RwLock;
use crate::InnerCamera;
@@ -16,52 +18,78 @@ pub struct Camera {
stream: RwLock