Using Box for child

This commit is contained in:
Benjamin Bädorf 2020-11-19 19:47:41 +01:00
parent da4dcf9968
commit a947442044
No known key found for this signature in database
GPG key ID: 4406E80E13CD656C

View file

@ -110,8 +110,8 @@ fn help() {
std::process::exit(0); std::process::exit(0);
} }
fn stream_black(config: &mut Config) -> Result<Vec<Child>, Error> { fn stream_black(config: &mut Config) -> Result<Vec<Box<Child>>, Error> {
let mut cmd = Command::new("ffmpeg") let cmd = Command::new("ffmpeg")
.args(&[ .args(&[
"-i", "-i",
format!( format!(
@ -142,10 +142,10 @@ fn stream_black(config: &mut Config) -> Result<Vec<Child>, Error> {
config.current_output = "".to_string(); config.current_output = "".to_string();
return Ok(vec![cmd]); return Ok(vec![Box::new(cmd)]);
} }
fn record_screen(config: &mut Config, output: SwayOutput) -> Result<Vec<Child>, Error> { fn record_screen(config: &mut Config, output: SwayOutput) -> Result<Vec<Box<Child>>, Error> {
let resolution = Resolution { let resolution = Resolution {
height: output.current_mode.height, height: output.current_mode.height,
width: output.current_mode.width, width: output.current_mode.width,
@ -191,7 +191,7 @@ fn record_screen(config: &mut Config, output: SwayOutput) -> Result<Vec<Child>,
config.current_output = output.name.as_str().to_string(); config.current_output = output.name.as_str().to_string();
let mut processes = vec![recorder]; let mut processes = vec![Box::new(recorder)];
if device_number != config.devices_from { if device_number != config.devices_from {
if config.verbose { if config.verbose {
@ -227,7 +227,7 @@ fn record_screen(config: &mut Config, output: SwayOutput) -> Result<Vec<Child>,
}) })
.spawn()?; .spawn()?;
processes.push(upscaler); processes.push(Box::new(upscaler));
} }
format!("/dev/video{}", device_number).as_str(); format!("/dev/video{}", device_number).as_str();
@ -411,6 +411,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.outputs .outputs
.insert(config.resolutions[0], config.devices_from); .insert(config.resolutions[0], config.devices_from);
config.current_device_index += 1; config.current_device_index += 1;
let valid_screens = get_valid_screens_for_recording(&config);
let mut recorders: Vec<Box<Child>> = if valid_screens.len() == 0 {
stream_black(&mut config)?
} else {
let output = get_output(&mut config, valid_screens[0].output.as_str());
record_screen(&mut config, output)?
};
let stdout = match Command::new("sh") let stdout = match Command::new("sh")
.args(&["-c", "swaymsg -t subscribe -m \"['window']\""]) .args(&["-c", "swaymsg -t subscribe -m \"['window']\""])
@ -425,32 +432,30 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}; };
let reader = BufReader::new(stdout); let reader = BufReader::new(stdout);
reader reader.lines().filter_map(|line| line.ok()).for_each(|_| {
.lines()
.filter_map(|line| line.ok())
.for_each(|_| -> Result<(), Error> {
println!("Switched focus"); println!("Switched focus");
let valid_screens = get_valid_screens_for_recording(&config); let valid_screens = get_valid_screens_for_recording(&config);
if valid_screens.len() > 0 && valid_screens[0].output == config.current_output { if valid_screens.len() > 0 && valid_screens[0].output == config.current_output {
return Ok(()); return;
}
for recorder in recorders.iter_mut() {
if config.verbose {
println!("Killing child");
} }
let output = get_output(&mut config, valid_screens[0].output.as_str());
for recorder in recorders.iter() {
match recorder.kill() { match recorder.kill() {
Ok(_) => {} Ok(_) => {}
Err(err) => panic!("{:?}", err), Err(err) => panic!("{:?}", err),
}; };
} }
let mut recorders: Vec<Child> = if valid_screens.len() != 0 { recorders = if valid_screens.len() == 0 {
stream_black(&mut config)? stream_black(&mut config).unwrap()
} else { } else {
let output = get_output(&mut config, valid_screens[0].output.as_str()); let output = get_output(&mut config, valid_screens[0].output.as_str());
record_screen(&mut config, output)? record_screen(&mut config, output).unwrap()
}; };
println!("Recording {}", config.current_output);
Ok(()) println!("Recording {}", config.current_output);
}); });
Ok(()) Ok(())