feat: 改进配置方式,重写逻辑。

This commit is contained in:
2022-11-28 00:09:18 +08:00
parent 7e1c4dd245
commit 479fdba9f6
10 changed files with 444 additions and 182 deletions

View File

@ -1,5 +1,5 @@
use scrap::Capturer;
use std::{io::ErrorKind::WouldBlock, time::Duration, thread};
pub struct Screen {
capturer: Option<Capturer>,
@ -29,15 +29,24 @@ impl Screen {
pub fn take(&mut self) -> anyhow::Result<Vec<u8>> {
match self.capturer.as_mut() {
Some(capturer) => {
let buffer = capturer
.frame()
.map_err(|error| anyhow::anyhow!("failed to frame of display. {}", error))?;
anyhow::Ok(buffer.to_vec())
}
Some(capturer) => loop {
match capturer.frame() {
Ok(buffer) => {
return anyhow::Ok(buffer.to_vec());
}
Err(error) => {
if error.kind() == WouldBlock {
thread::sleep(Duration::from_millis(16));
continue;
} else {
anyhow::bail!("failed to frame of display. {}", error);
}
}
}
},
None => anyhow::bail!("Do not initialized"),
}
}
}
unsafe impl Send for Screen {}
unsafe impl Send for Screen {}