Skip to content

Commit

Permalink
return exit failure on configuration issue
Browse files Browse the repository at this point in the history
  • Loading branch information
russelltg committed May 18, 2024
1 parent eb85341 commit ad0dd42
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{

use anyhow::bail;
use ffmpeg::{
codec::{audio, Context, Id},
codec::{Context, Id},
decoder, encoder,
ffi::{av_find_input_format, av_get_default_channel_layout, AVChannelOrder},
filter,
Expand Down Expand Up @@ -173,7 +173,10 @@ impl AudioHandle {
.codec(&args.output, ffmpeg::media::Type::Audio);

if audio_codec_id == Id::None {
bail!("Container format {} does not support audio!", octx.format().name());
bail!(
"Container format {} does not support audio!",
octx.format().name()
);
}

let audio_codec = ffmpeg::encoder::find(audio_codec_id)
Expand Down
30 changes: 16 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
process::exit,
str::from_utf8_unchecked,
sync::{
atomic::{AtomicBool, AtomicU64, Ordering},
atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering},
Arc,
},
thread::{sleep, spawn},
Expand Down Expand Up @@ -401,7 +401,7 @@ struct State {
partial_outputs_wlr: HashMap<TypedObjectId<ZwlrOutputHeadV1>, PartialOutputInfoWlr>,
partial_outputs: HashMap<TypedObjectId<WlOutput>, PartialOutputInfo>, // key is xdg-output name (wayland object ID)
outputs: HashMap<TypedObjectId<WlOutput>, Option<OutputInfo>>, // none for disabled
quit_flag: Arc<AtomicBool>,
quit_flag: Arc<AtomicUsize>,
sigusr1_flag: Arc<AtomicBool>,
dri_device: Option<String>,
}
Expand Down Expand Up @@ -551,7 +551,7 @@ impl Dispatch<ZwlrScreencopyFrameV1, ()> for State {
Ok(enc) => enc,
Err(e) => {
eprintln!("failed to create encoder(s): {}", e);
state.quit_flag.store(true, Ordering::SeqCst);
state.quit_flag.store(1, Ordering::SeqCst);
return;
}
},
Expand Down Expand Up @@ -608,7 +608,7 @@ impl Dispatch<ZwlrScreencopyFrameV1, ()> for State {
zwlr_screencopy_frame_v1::Event::Flags { .. } => {}
zwlr_screencopy_frame_v1::Event::Failed => {
eprintln!("Failed to screencopy!");
state.quit_flag.store(true, Ordering::SeqCst)
state.quit_flag.store(1, Ordering::SeqCst)
}
_ => {}
}
Expand Down Expand Up @@ -850,7 +850,7 @@ impl State {
fn new(
conn: &Connection,
args: Args,
quit_flag: Arc<AtomicBool>,
quit_flag: Arc<AtomicUsize>,
sigusr1_flag: Arc<AtomicBool>,
) -> anyhow::Result<(Self, EventQueue<Self>)> {
let display = conn.display();
Expand Down Expand Up @@ -1092,7 +1092,7 @@ impl State {
eprintln!(
"multiple enabled displays and no --geometry or --output supplied, bailing"
);
self.quit_flag.store(true, Ordering::SeqCst);
self.quit_flag.store(1, Ordering::SeqCst);
return;
}

Expand All @@ -1105,7 +1105,7 @@ impl State {
(output, (0, 0), output.size_pixels)
} else {
eprintln!("display {} not found, bailing", disp);
self.quit_flag.store(true, Ordering::SeqCst);
self.quit_flag.store(1, Ordering::SeqCst);
return;
}
}
Expand All @@ -1130,15 +1130,15 @@ impl State {
"region {},{} {}x{} is not entirely within one output, bailing",
x, y, w, h
);
self.quit_flag.store(true, Ordering::SeqCst);
self.quit_flag.store(1, Ordering::SeqCst);
return;
}
}
(Some(_), _) => {
eprintln!(
"both --geometry and --output were passed, which is not allowed, bailing"
);
self.quit_flag.store(true, Ordering::SeqCst);
self.quit_flag.store(1, Ordering::SeqCst);
return;
}
};
Expand Down Expand Up @@ -1740,12 +1740,12 @@ fn supported_formats(codec: &ffmpeg::Codec) -> Vec<Pixel> {
}

fn main() {
let quit_flag = Arc::new(AtomicBool::new(false));
let quit_flag = Arc::new(AtomicUsize::new(usize::MAX)); // ::MAX means still running, otherwise it's an exit value
let sigusr1_flag = Arc::new(AtomicBool::new(false));

signal_hook::flag::register(SIGINT, Arc::clone(&quit_flag)).unwrap();
signal_hook::flag::register(SIGTERM, Arc::clone(&quit_flag)).unwrap();
signal_hook::flag::register(SIGHUP, Arc::clone(&quit_flag)).unwrap();
signal_hook::flag::register_usize(SIGINT, Arc::clone(&quit_flag), 0).unwrap();
signal_hook::flag::register_usize(SIGTERM, Arc::clone(&quit_flag), 1).unwrap();
signal_hook::flag::register_usize(SIGHUP, Arc::clone(&quit_flag), 0).unwrap();
signal_hook::flag::register(SIGUSR1, Arc::clone(&sigusr1_flag)).unwrap();

let args = Args::parse();
Expand Down Expand Up @@ -1796,11 +1796,13 @@ fn main() {
}
};

while !quit_flag.load(Ordering::SeqCst) {
while quit_flag.load(Ordering::SeqCst) == usize::MAX {
queue.blocking_dispatch(&mut state).unwrap();
}

if let EncConstructionStage::Complete(enc) = &mut state.enc {
enc.flush();
}

exit(quit_flag.load(Ordering::SeqCst) as i32)
}

0 comments on commit ad0dd42

Please sign in to comment.