about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..1f8cd70
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,100 @@
+use std::{thread::sleep, time::Duration};
+
+use nokhwa::{
+	pixel_format::{self, RgbFormat},
+	query,
+	utils::{ApiBackend, CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType},
+	Camera,
+};
+
+fn main() {
+	nokhwa::nokhwa_initialize(callback);
+
+	let mut args = std::env::args().skip(1);
+
+	match args.next().as_deref() {
+		Some("tryids") => {
+			let start: u32 = args.next().map(|n| n.parse().unwrap()).unwrap_or(0);
+			let end: u32 = args.next().map(|n| n.parse().unwrap()).unwrap_or(255);
+
+			tryids(start, end);
+		}
+		Some("trydev") => match args.next() {
+			None => {
+				eprintln!("device path required");
+				std::process::exit(1);
+			}
+			Some(dev) => trydev(&dev),
+		},
+		Some("query") => {
+			cmdquery();
+		}
+		None | Some(_) => {
+			println!("tryids <start> <end>");
+			println!("\ttry to open video devices with IDs start to end, inclusive on both ends\n");
+			println!("trydev <path>");
+			println!("\ttry to open the video device from the full path");
+		}
+	}
+}
+
+fn callback(success: bool) {
+	println!("callback: os says: {success}");
+}
+
+fn tryids(start: u32, end: u32) {
+	for idx in start..=end {
+		let fmt = RequestedFormat::new::<RgbFormat>(RequestedFormatType::None);
+		match Camera::new(CameraIndex::Index(idx), fmt) {
+			Err(e) => {
+				eprintln!("cam [{idx}]: failed to open device: {e}");
+			}
+			Ok(cam) => {
+				let info = cam.info();
+				println!(
+					"cam [{idx}]: {} - {}\n\t{}",
+					info.index(),
+					info.human_name(),
+					info.description()
+				);
+			}
+		}
+	}
+}
+
+fn trydev(device: &str) {
+	let fmt = RequestedFormat::new::<RgbFormat>(RequestedFormatType::None);
+	match Camera::new(CameraIndex::String(device.to_owned()), fmt) {
+		Err(e) => {
+			eprintln!("cam [{device}]: failed to open device: {e}");
+		}
+		Ok(cam) => {
+			let info = cam.info();
+			println!(
+				"cam [{device}]: {} - {}\n\t{}",
+				info.index(),
+				info.human_name(),
+				info.description()
+			);
+		}
+	}
+}
+
+fn cmdquery() {
+	let query = match query(ApiBackend::Auto) {
+		Err(e) => {
+			eprintln!("failed to query devices: {e}");
+			std::process::exit(1);
+		}
+		Ok(q) => q,
+	};
+
+	for info in query {
+		println!(
+			"cam [{}]: {}\n\t{}",
+			info.index(),
+			info.human_name(),
+			info.description()
+		);
+	}
+}