Merge pull request #8 from Johann150/master

cleanup, update clap
This commit is contained in:
Tim Visée
2023-12-29 11:54:02 +00:00
committed by GitHub
11 changed files with 302 additions and 247 deletions

213
Cargo.lock generated
View File

@@ -24,23 +24,51 @@ dependencies = [
] ]
[[package]] [[package]]
name = "ansi_term" name = "anstream"
version = "0.12.1" version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6"
dependencies = [ dependencies = [
"winapi", "anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
] ]
[[package]] [[package]]
name = "atty" name = "anstyle"
version = "0.2.14" version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
[[package]]
name = "anstyle-parse"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
dependencies = [ dependencies = [
"hermit-abi", "utf8parse",
"libc", ]
"winapi",
[[package]]
name = "anstyle-query"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys",
] ]
[[package]] [[package]]
@@ -81,25 +109,56 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "clap" name = "clap"
version = "2.34.0" version = "4.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" checksum = "bfaff671f6b22ca62406885ece523383b9b64022e341e53e009a62ebc47a45f2"
dependencies = [ dependencies = [
"ansi_term", "clap_builder",
"atty", "clap_derive",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
] ]
[[package]]
name = "clap_builder"
version = "4.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a216b506622bb1d316cd51328dce24e07bdff4a6128a47c7e7fad11878d5adbb"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
[[package]] [[package]]
name = "color_quant" name = "color_quant"
version = "1.1.0" version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]] [[package]]
name = "crc32fast" name = "crc32fast"
version = "1.3.2" version = "1.3.2"
@@ -180,6 +239,12 @@ dependencies = [
"weezl", "weezl",
] ]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.19" version = "0.1.19"
@@ -338,6 +403,24 @@ dependencies = [
"miniz_oxide 0.3.7", "miniz_oxide 0.3.7",
] ]
[[package]]
name = "proc-macro2"
version = "1.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]] [[package]]
name = "rayon" name = "rayon"
version = "1.5.3" version = "1.5.3"
@@ -393,17 +476,19 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.8.0" version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "textwrap" name = "syn"
version = "0.11.0" version = "2.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a"
dependencies = [ dependencies = [
"unicode-width", "proc-macro2",
"quote",
"unicode-ident",
] ]
[[package]] [[package]]
@@ -418,16 +503,16 @@ dependencies = [
] ]
[[package]] [[package]]
name = "unicode-width" name = "unicode-ident"
version = "0.1.9" version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]] [[package]]
name = "vec_map" name = "utf8parse"
version = "0.8.2" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]] [[package]]
name = "weezl" name = "weezl"
@@ -436,23 +521,67 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9193164d4de03a926d909d3bc7c30543cecb35400c02114792c2cae20d5e2dbb" checksum = "9193164d4de03a926d909d3bc7c30543cecb35400c02114792c2cae20d5e2dbb"
[[package]] [[package]]
name = "winapi" name = "windows-sys"
version = "0.3.9" version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [ dependencies = [
"winapi-i686-pc-windows-gnu", "windows-targets",
"winapi-x86_64-pc-windows-gnu",
] ]
[[package]] [[package]]
name = "winapi-i686-pc-windows-gnu" name = "windows-targets"
version = "0.4.0" version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "windows_aarch64_gnullvm"
version = "0.4.0" version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"

View File

@@ -14,7 +14,7 @@ categories = [
[dependencies] [dependencies]
bufstream = "0.1" bufstream = "0.1"
clap = "2.34" clap = { version = "4.4", features = [ "derive" ] }
image = "0.23" image = "0.23"
num_cpus = "1.13.1" num_cpus = "1.13.1"
regex = "1.5" regex = "1.5"

View File

@@ -1,8 +0,0 @@
// Application properties
pub const APP_NAME: &str = "pixelpwnr";
pub const APP_VERSION: &str = "0.1";
pub const APP_AUTHOR: &str = "Tim Visee <timvisee@gmail.com>";
pub const APP_ABOUT: &str = "A quick pixelflut client, that pwns pixelflut panels.";
// The default frames per second rate
pub const DEFAULT_IMAGE_FPS: u32 = 1;

View File

@@ -1,171 +1,103 @@
extern crate clap; extern crate clap;
extern crate num_cpus; extern crate num_cpus;
use clap::{App, Arg, ArgMatches}; use clap::Parser;
use app::*; #[derive(Parser)]
#[command(author, version, about, disable_help_flag = true)]
pub struct Arguments {
// manually redefine help, but without short option, because `-h`
// is already used by the height option.
/// Show this help
#[clap(long, action = clap::ArgAction::HelpLong)]
help: Option<bool>,
/// CLI argument handler. /// The host to pwn "host:port"
pub struct ArgHandler<'a> { host: String,
matches: ArgMatches<'a>,
/// Image path(s)
#[arg(short, long, value_name = "PATH", required = true, alias = "images")]
image: Vec<String>,
/// Draw width [default: screen width]
#[arg(short, long, value_name = "PIXELS")]
width: Option<u16>,
/// Draw height [default: screen height]
#[arg(short, long, value_name = "PIXELS")]
height: Option<u16>,
/// Draw X offset
#[arg(short, value_name = "PIXELS", default_value_t = 0)]
x: u16,
/// Draw Y offset
#[arg(short, value_name = "PIXELS", default_value_t = 0)]
y: u16,
/// Number of concurrent threads [default: number of CPUs]
#[arg(short, long, aliases = ["thread", "threads"])]
count: Option<usize>,
/// Frames per second with multiple images
#[arg(short = 'r', long, value_name = "RATE", default_value_t = 1)]
fps: u32,
/// Use binary mode to set pixels (`PB` protocol extension) [default: off]
#[arg(short, long, alias = "bin")]
binary: bool,
} }
impl<'a: 'b, 'b> ArgHandler<'a> { /// CLI argument handler.
/// Parse CLI arguments. pub struct ArgHandler {
pub fn parse() -> ArgHandler<'a> { data: Arguments,
// Handle/parse arguments }
let matches = App::new(APP_NAME)
.version(APP_VERSION)
.author(APP_AUTHOR)
.about(APP_ABOUT)
.arg(
Arg::with_name("HOST")
.help("The host to pwn \"host:port\"")
.required(true)
.index(1),
)
.arg(
Arg::with_name("image")
.short("i")
.long("image")
.alias("images")
.value_name("PATH")
.help("Image paths")
.required(true)
.multiple(true)
.display_order(1)
.takes_value(true),
)
.arg(
Arg::with_name("width")
.short("w")
.long("width")
.value_name("PIXELS")
.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: screen height)")
.display_order(3)
.takes_value(true),
)
.arg(
Arg::with_name("x")
.short("x")
.value_name("PIXELS")
.help("Draw X offset (def: 0)")
.display_order(4)
.takes_value(true),
)
.arg(
Arg::with_name("y")
.short("y")
.value_name("PIXELS")
.help("Draw Y offset (def: 0)")
.display_order(5)
.takes_value(true),
)
.arg(
Arg::with_name("count")
.short("c")
.long("count")
.alias("thread")
.alias("threads")
.value_name("COUNT")
.help("Number of concurrent threads (def: CPUs)")
.display_order(6)
.takes_value(true),
)
.arg(
Arg::with_name("fps")
.short("r")
.long("fps")
.value_name("RATE")
.help("Frames per second with multiple images (def: 1)")
.display_order(7)
.takes_value(true),
)
.arg(
Arg::with_name("binary")
.short("b")
.long("binary")
.help("Use binary mode to set pixels (PB)")
.display_order(7)
.takes_value(false),
)
.get_matches();
// Instantiate impl ArgHandler {
ArgHandler { matches } pub fn parse() -> ArgHandler {
ArgHandler {
data: Arguments::parse(),
}
} }
/// Get the host property. /// Get the host property.
pub fn host(&'a self) -> &'b str { pub fn host(&self) -> &str {
self.matches self.data.host.as_str()
.value_of("HOST")
.expect("Please specify a host")
} }
/// Get the thread count. /// Get the thread count.
pub fn count(&self) -> usize { pub fn count(&self) -> usize {
self.matches self.data.count.unwrap_or_else(num_cpus::get)
.value_of("count")
.map(|count| count.parse::<usize>().expect("Invalid count specified"))
.unwrap_or_else(num_cpus::get)
} }
/// Get the image paths. /// Get the image paths.
pub fn image_paths(&'a self) -> Vec<&'b str> { pub fn image_paths(&self) -> Vec<&str> {
self.matches self.data.image.iter().map(|x| x.as_str()).collect()
.values_of("image")
.expect("Please specify an image paths")
.collect()
} }
/// Get the image size. /// Get the image size.
/// Use the given default value if not set. /// Use the given default value if not set.
pub fn size(&self, def: Option<(u32, u32)>) -> (u32, u32) { pub fn size(&self, def: Option<(u16, u16)>) -> (u16, u16) {
( (
self.matches self.data
.value_of("width") .width
.map(|width| width.parse().expect("Invalid image width"))
.unwrap_or(def.expect("No screen width set or known").0), .unwrap_or(def.expect("No screen width set or known").0),
self.matches self.data
.value_of("height") .height
.map(|height| height.parse().expect("Invalid image height"))
.unwrap_or(def.expect("No screen height set or known").1), .unwrap_or(def.expect("No screen height set or known").1),
) )
} }
/// Get the image offset. /// Get the image offset.
pub fn offset(&self) -> (u32, u32) { pub fn offset(&self) -> (u16, u16) {
( (self.data.x, self.data.y)
self.matches
.value_of("x")
.map(|x| x.parse::<u32>().expect("Invalid X offset"))
.unwrap_or(0),
self.matches
.value_of("y")
.map(|y| y.parse::<u32>().expect("Invalid Y offset"))
.unwrap_or(0),
)
} }
/// Get the FPS. /// Get the FPS.
pub fn fps(&self) -> u32 { pub fn fps(&self) -> u32 {
self.matches self.data.fps
.value_of("fps")
.map(|fps| fps.parse::<u32>().expect("Invalid frames per second rate"))
.unwrap_or(DEFAULT_IMAGE_FPS)
} }
/// Whether to use binary mode. /// Whether to use binary mode.
pub fn binary(&self) -> bool { pub fn binary(&self) -> bool {
self.matches.is_present("binary") self.data.binary
} }
} }

View File

@@ -1,11 +1,11 @@
use rayon::prelude::*;
use std::path::Path; use std::path::Path;
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
use rayon::prelude::*;
use image; use image;
use image::DynamicImage;
use image::imageops::FilterType; use image::imageops::FilterType;
use image::DynamicImage;
use pix::canvas::Canvas; use pix::canvas::Canvas;
@@ -28,13 +28,17 @@ impl ImageManager {
} }
/// Instantiate the image manager, and load the images from the given paths. /// Instantiate the image manager, and load the images from the given paths.
pub fn load(paths: &[&str], size: (u32, u32)) -> ImageManager { pub fn load(paths: &[&str], size: (u16, u16)) -> ImageManager {
// Show a status message // Show a status message
println!("Load and process {} image(s)...", paths.len()); println!("Load and process {} image(s)...", paths.len());
// Load the images from the paths // Load the images from the paths
let image_manager = let image_manager = ImageManager::from(
ImageManager::from(paths.par_iter().map(|path| load_image(path, size)).collect()); paths
.par_iter()
.map(|path| load_image(path, size))
.collect(),
);
// TODO: process the image slices // TODO: process the image slices
@@ -84,7 +88,7 @@ impl ImageManager {
} }
/// Load the image at the given path, and size it correctly /// Load the image at the given path, and size it correctly
fn load_image(path: &str, size: (u32, u32)) -> DynamicImage { fn load_image(path: &str, size: (u16, u16)) -> DynamicImage {
// Create a path instance // Create a path instance
let path = Path::new(&path); let path = Path::new(&path);
@@ -94,8 +98,8 @@ fn load_image(path: &str, size: (u32, u32)) -> DynamicImage {
} }
// Load the image // Load the image
let image = image::open(&path).unwrap(); let image = image::open(path).unwrap();
// Resize the image to fit the screen // Resize the image to fit the screen
image.resize_exact(size.0, size.1, FilterType::Gaussian) image.resize_exact(size.0 as u32, size.1 as u32, FilterType::Gaussian)
} }

View File

@@ -2,7 +2,6 @@ extern crate clap;
extern crate image; extern crate image;
extern crate rayon; extern crate rayon;
mod app;
mod arg_handler; mod arg_handler;
mod color; mod color;
mod image_manager; mod image_manager;
@@ -27,13 +26,13 @@ fn main() {
} }
/// Start pixelflutting. /// Start pixelflutting.
fn start<'a>(arg_handler: &ArgHandler<'a>) { fn start(arg_handler: &ArgHandler) {
// Start // Start
println!("Starting... (use CTRL+C to stop)"); println!("Starting... (use CTRL+C to stop)");
// Gather facts about the host // Gather facts about the host
let screen_size = let screen_size =
gather_host_facts(&arg_handler).expect("Failed to gather facts about pixelflut server"); gather_host_facts(arg_handler).expect("Failed to gather facts about pixelflut server");
// Determine the size to use // Determine the size to use
let size = arg_handler.size(Some(screen_size)); let size = arg_handler.size(Some(screen_size));
@@ -55,7 +54,7 @@ fn start<'a>(arg_handler: &ArgHandler<'a>) {
} }
/// Gather important facts about the host. /// Gather important facts about the host.
fn gather_host_facts(arg_handler: &ArgHandler) -> Result<(u32, u32), Error> { fn gather_host_facts(arg_handler: &ArgHandler) -> Result<(u16, u16), Error> {
// Set up a client, and get the screen size // Set up a client, and get the screen size
let size = Client::connect(arg_handler.host().to_string(), false)?.read_screen_size()?; let size = Client::connect(arg_handler.host().to_string(), false)?.read_screen_size()?;

View File

@@ -31,7 +31,12 @@ impl Handle {
/// Push an image update. /// Push an image update.
pub fn update_image(&self, full_image: &mut DynamicImage) { pub fn update_image(&self, full_image: &mut DynamicImage) {
// Crop the image to the area // Crop the image to the area
let image = full_image.crop(self.area.x, self.area.y, self.area.w, self.area.h); let image = full_image.crop(
self.area.x as u32,
self.area.y as u32,
self.area.w as u32,
self.area.h as u32,
);
// Push a new image to the thread // Push a new image to the thread
// TODO: return this result // TODO: return this result

View File

@@ -11,7 +11,7 @@ use rect::Rect;
pub struct Painter { pub struct Painter {
client: Option<Client>, client: Option<Client>,
area: Rect, area: Rect,
offset: (u32, u32), offset: (u16, u16),
image: Option<DynamicImage>, image: Option<DynamicImage>,
} }
@@ -20,7 +20,7 @@ impl Painter {
pub fn new( pub fn new(
client: Option<Client>, client: Option<Client>,
area: Rect, area: Rect,
offset: (u32, u32), offset: (u16, u16),
image: Option<DynamicImage>, image: Option<DynamicImage>,
) -> Painter { ) -> Painter {
Painter { Painter {
@@ -62,7 +62,7 @@ impl Painter {
} }
// Get the pixel at this location // Get the pixel at this location
let pixel = image.get_pixel(x, y); let pixel = image.get_pixel(x as u32, y as u32);
// Get the channels // Get the channels
let channels = pixel.channels(); let channels = pixel.channels();
@@ -74,7 +74,6 @@ impl Painter {
// Define the color // Define the color
let color = Color::from(channels[0], channels[1], channels[2], channels[3]); let color = Color::from(channels[0], channels[1], channels[2], channels[3]);
// Set the pixel // Set the pixel
if let Some(client) = &mut self.client { if let Some(client) = &mut self.client {
client.write_pixel( client.write_pixel(

View File

@@ -15,8 +15,8 @@ pub struct Canvas {
host: String, host: String,
painter_count: usize, painter_count: usize,
painter_handles: Vec<Handle>, painter_handles: Vec<Handle>,
size: (u32, u32), size: (u16, u16),
offset: (u32, u32), offset: (u16, u16),
} }
impl Canvas { impl Canvas {
@@ -24,8 +24,8 @@ impl Canvas {
pub fn new( pub fn new(
host: &str, host: &str,
painter_count: usize, painter_count: usize,
size: (u32, u32), size: (u16, u16),
offset: (u32, u32), offset: (u16, u16),
binary: bool, binary: bool,
) -> Canvas { ) -> Canvas {
// Initialize the object // Initialize the object
@@ -52,10 +52,10 @@ impl Canvas {
// Spawn some painters // Spawn some painters
for i in 0..self.painter_count { for i in 0..self.painter_count {
// Determine the slice width // Determine the slice width
let width = self.size.0 / (self.painter_count as u32); let width = self.size.0 / (self.painter_count as u16);
// Define the area to paint per thread // Define the area to paint per thread
let painter_area = Rect::from((i as u32) * width, 0, width, self.size.1); let painter_area = Rect::from((i as u16) * width, 0, width, self.size.1);
// Spawn the painter // Spawn the painter
self.spawn_painter(painter_area, binary); self.spawn_painter(painter_area, binary);
@@ -79,26 +79,23 @@ impl Canvas {
let mut painter = Painter::new(None, area, offset, None); let mut painter = Painter::new(None, area, offset, None);
loop { loop {
// The painting loop
'paint: loop {
// Connect // Connect
let client = match Client::connect(host.clone(), binary) { match Client::connect(host.clone(), binary) {
Ok(client) => client, Ok(client) => {
Err(e) => {
eprintln!("Painter failed to connect: {}", e);
break 'paint;
}
};
painter.set_client(Some(client)); painter.set_client(Some(client));
// Keep painting // Keep painting
loop { loop {
if let Err(e) = painter.work(&rx) { if let Err(e) = painter.work(&rx) {
println!("Painter error: {}", e); println!("Painter error: {}", e);
break 'paint; break;
} }
} }
} }
Err(e) => {
eprintln!("Painter failed to connect: {}", e);
}
};
// Sleep for half a second before restarting the painter // Sleep for half a second before restarting the painter
sleep(Duration::from_millis(500)); sleep(Duration::from_millis(500));

View File

@@ -48,23 +48,21 @@ impl Client {
} }
/// Write a pixel to the given stream. /// Write a pixel to the given stream.
pub fn write_pixel(&mut self, x: u32, y: u32, color: Color) -> Result<(), Error> { pub fn write_pixel(&mut self, x: u16, y: u16, color: Color) -> Result<(), Error> {
if self.binary { if self.binary {
self.write_command( let mut data = [
&[ b'P', b'B',
b'P', // these values will be filled in using to_le_bytes in the next step
b'B', // to account for the machines endianness
x as u8, 0, // x LSB
(x >> 8) as u8, 0, // x MSB
y as u8, 0, // y LSB
(y >> 8) as u8, 0, // y MSB
color.r, color.r, color.g, color.b, color.a,
color.g, ];
color.b, data[2..4].copy_from_slice(&x.to_le_bytes());
color.a, data[4..6].copy_from_slice(&y.to_le_bytes());
], self.write_command(&data, false)
false,
)
} else { } else {
self.write_command( self.write_command(
format!("PX {} {} {}", x, y, color.as_hex()).as_bytes(), format!("PX {} {} {}", x, y, color.as_hex()).as_bytes(),
@@ -74,7 +72,7 @@ impl Client {
} }
/// Read the size of the screen. /// Read the size of the screen.
pub fn read_screen_size(&mut self) -> Result<(u32, u32), Error> { pub fn read_screen_size(&mut self) -> Result<(u16, u16), Error> {
// Read the screen size // Read the screen size
let data = self let data = self
.write_read_command(b"SIZE") .write_read_command(b"SIZE")
@@ -87,10 +85,10 @@ impl Client {
match re.captures(&data) { match re.captures(&data) {
Some(matches) => Ok(( Some(matches) => Ok((
matches[1] matches[1]
.parse::<u32>() .parse::<u16>()
.expect("Failed to parse screen width, received malformed data"), .expect("Failed to parse screen width, received malformed data"),
matches[2] matches[2]
.parse::<u32>() .parse::<u16>()
.expect("Failed to parse screen height, received malformed data"), .expect("Failed to parse screen height, received malformed data"),
)), )),
None => Err(Error::new( None => Err(Error::new(

View File

@@ -2,14 +2,14 @@
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Rect { pub struct Rect {
// TODO: Make these properties private // TODO: Make these properties private
pub x: u32, pub x: u16,
pub y: u32, pub y: u16,
pub w: u32, pub w: u16,
pub h: u32, pub h: u16,
} }
impl Rect { impl Rect {
pub fn from(x: u32, y: u32, w: u32, h: u32) -> Rect { pub fn from(x: u16, y: u16, w: u16, h: u16) -> Rect {
Rect { x, y, w, h } Rect { x, y, w, h }
} }
} }