diff --git a/src/app.rs b/src/app.rs index 520eb7a..12aeb85 100644 --- a/src/app.rs +++ b/src/app.rs @@ -7,10 +7,6 @@ pub const APP_ABOUT: &'static str = "A quick pixelflut client, that pwns pixelfl // The default thread count pub const DEFAULT_THREAD_COUNT: usize = 4; -// The default image width and height -pub const DEFAULT_IMAGE_WIDTH: u32 = 1920; -pub const DEFAULT_IMAGE_HEIGHT: u32 = 1080; - // The default frames per second rate pub const DEFAULT_IMAGE_FPS: u32 = 1; diff --git a/src/arg_handler.rs b/src/arg_handler.rs index 632d825..713e48f 100644 --- a/src/arg_handler.rs +++ b/src/arg_handler.rs @@ -38,14 +38,14 @@ impl<'a: 'b, 'b> ArgHandler<'a> { .short("w") .long("width") .value_name("PIXELS") - .help("Draw width ((def: 1920)") + .help("Draw width (def: screen width)") .display_order(2) .takes_value(true)) .arg(Arg::with_name("height") .short("h") .long("height") .value_name("PIXELS") - .help("Draw height (def: 1080)") + .help("Draw height (def: screen height)") .display_order(3) .takes_value(true)) .arg(Arg::with_name("x") @@ -108,14 +108,19 @@ impl<'a: 'b, 'b> ArgHandler<'a> { } /// Get the image size. - pub fn size(&self) -> (u32, u32) { + /// Use the given default value if not set. + pub fn size(&self, def: Option<(u32, u32)>) -> (u32, u32) { ( self.matches.value_of("width") - .unwrap_or(&format!("{}", DEFAULT_IMAGE_WIDTH)) + .unwrap_or( + &format!("{}", def.expect("No screen width set or known").0) + ) .parse::() .expect("Invalid image width"), self.matches.value_of("height") - .unwrap_or(&format!("{}", DEFAULT_IMAGE_HEIGHT)) + .unwrap_or( + &format!("{}", def.expect("No screen height set or known").1) + ) .parse::() .expect("Invalid image height"), ) diff --git a/src/main.rs b/src/main.rs index bf7bf16..e17151a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,9 +9,12 @@ mod painter; mod pix; mod rect; +use std::io::Error; + use arg_handler::ArgHandler; use image_manager::ImageManager; use pix::canvas::Canvas; +use pix::client::Client; @@ -27,22 +30,42 @@ fn main() { /// Start pixelflutting. fn start<'a>(arg_handler: &ArgHandler<'a>) { // Start - println!("Starting..."); + println!("Starting... (use CTRL+C to stop)"); + + // Gather facts about the host + let screen_size = gather_host_facts(&arg_handler) + .expect("Failed to gather facts about pixelflut server"); + + // Determine the size to use + let size = arg_handler.size(Some(screen_size)); // Create a new pixelflut canvas let mut canvas = Canvas::new( arg_handler.host(), arg_handler.count(), - arg_handler.size(), + size, arg_handler.offset(), ); // Load the image manager let mut image_manager = ImageManager::load( arg_handler.image_paths(), - &arg_handler.size(), + &size, ); // Start the work in the image manager, to walk through the frames image_manager.work(&mut canvas, arg_handler.fps()); } + +/// Gather important facts about the host. +fn gather_host_facts(arg_handler: &ArgHandler) -> Result<(u32, u32), Error> { + // Set up a client, and get the screen size + let size = Client::connect( + arg_handler.host().to_string(), + )?.read_screen_size()?; + + // Print status + println!("Gathered screen size: {}x{}", size.0, size.1); + + Ok(size) +} diff --git a/src/pix/client.rs b/src/pix/client.rs index e53e506..b515a81 100644 --- a/src/pix/client.rs +++ b/src/pix/client.rs @@ -10,6 +10,8 @@ use self::regex::Regex; use color::Color; + + // The default buffer size for reading the client stream. // - Big enough so we don't have to expand // - Small enough to not take up to much memory @@ -93,7 +95,7 @@ impl Client { /// Write the given command to the given stream, and read the output. fn write_read_command(&mut self, cmd: String) -> Result { // Write the command - self.write_command(cmd); + self.write_command(cmd)?; // Flush the pipe, ensure the command is actually sent self.stream.flush()?;