about summary refs log tree commit diff
path: root/src/main.rs
blob: 1f8cd709608f202edd122e6a6988ca443ceba588 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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()
		);
	}
}