From 05c7b4cb71fd56fc9a7b5aa5fa63a501dc4e696b Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 10 May 2024 12:36:42 +0200 Subject: [PATCH] remove shared code, fix bugs --- .gitmodules | 3 -- Cargo.lock | 17 +++------ Cargo.toml | 4 +- pixel-shared-rs | 1 - src/gui.rs | 8 ++-- src/main.rs | 5 ++- src/protocol.rs | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ src/upd_loop.rs | 19 +++------- 8 files changed, 119 insertions(+), 37 deletions(-) delete mode 100644 .gitmodules delete mode 160000 pixel-shared-rs create mode 100644 src/protocol.rs diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 8c85e15..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "pixel-shared-rs"] - path = pixel-shared-rs - url = https://github.com/kaesaecracker/pixel-shared-rs.git diff --git a/Cargo.lock b/Cargo.lock index 907c4f0..f5ae887 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1281,9 +1281,9 @@ checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" [[package]] name = "num" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3135b08af27d103b0a51f2ae0f8632117b7b185ccf931445affa8df530576a41" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" dependencies = [ "num-bigint", "num-complex", @@ -1561,19 +1561,12 @@ dependencies = [ "env_logger", "image", "log", - "pixel-shared-rs", - "pixels", - "raw-window-handle 0.6.1", - "winit", -] - -[[package]] -name = "pixel-shared-rs" -version = "0.1.0" -dependencies = [ "num", "num-derive", "num-traits", + "pixels", + "raw-window-handle 0.6.1", + "winit", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 439a432..583d07c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,6 @@ winit = { version = "0.30", features = ["rwh_05"] } log = "0.4" raw-window-handle = "0.6" env_logger = "0.11" -pixel-shared-rs = { path = "pixel-shared-rs" } +num = "0.4" +num-derive = "0.4" +num-traits = "0.2" diff --git a/pixel-shared-rs b/pixel-shared-rs deleted file mode 160000 index 6e46694..0000000 --- a/pixel-shared-rs +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6e46694d877faa61647fae104afe6603c1718a06 diff --git a/src/gui.rs b/src/gui.rs index 15098e2..953bb23 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,6 +1,6 @@ +use crate::protocol::{PIXEL_HEIGHT, PIXEL_WIDTH}; use crate::DISPLAY; -use log::{debug, warn}; -use pixel_shared_rs::{PIXEL_HEIGHT, PIXEL_WIDTH}; +use log::{trace, warn}; use pixels::wgpu::TextureFormat; use pixels::{Pixels, PixelsBuilder, SurfaceTexture}; use winit::application::ApplicationHandler; @@ -40,7 +40,7 @@ impl ApplicationHandler for App { } fn window_event(&mut self, event_loop: &ActiveEventLoop, _: WindowId, event: WindowEvent) { - debug!("event {:?}", event); + trace!("event {:?}", event); match event { WindowEvent::CloseRequested => { warn!("The close button was pressed; stopping"); @@ -64,8 +64,6 @@ impl ApplicationHandler for App { i += 1; } - debug!("drawn {} pixels", i); - pixels.render().expect("could not render"); window.request_redraw(); } diff --git a/src/main.rs b/src/main.rs index 78992a5..dff120f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod gui; mod upd_loop; +mod protocol; use std::default::Default; use std::sync::mpsc; @@ -10,12 +11,12 @@ use crate::gui::App; use crate::upd_loop::start_udp_thread; use clap::Parser; use log::info; -use pixel_shared_rs::PIXEL_COUNT; +use protocol::PIXEL_COUNT; use winit::event_loop::{ControlFlow, EventLoop}; #[derive(Parser, Debug)] struct Cli { - #[arg(long = "bind", default_value = "0.0.0.0:2342")] + #[arg(long, default_value = "0.0.0.0:2342")] bind: String, } diff --git a/src/protocol.rs b/src/protocol.rs new file mode 100644 index 0000000..4cae660 --- /dev/null +++ b/src/protocol.rs @@ -0,0 +1,99 @@ +use num_derive::{FromPrimitive, ToPrimitive}; +use std::mem::size_of; + +#[derive(Debug, FromPrimitive, ToPrimitive, Default)] +pub enum DisplayCommand { + #[default] + CmdClear = 0x0002, + CmdCp437data = 0x0003, + CmdCharBrightness = 0x0005, + CmdBrightness = 0x0007, + CmdHardReset = 0x000b, + CmdFadeOut = 0x000d, + CmdBitmapLegacy = 0x0010, + CmdBitmapLinear = 0x0012, + CmdBitmapLinearWin = 0x0013, + CmdBitmapLinearAnd = 0x0014, + CmdBitmapLinearOr = 0x0015, + CmdBitmapLinearXor = 0x0016, +} + +#[repr(C)] +#[derive(Debug, Default)] +pub struct HdrWindow { + pub command: DisplayCommand, + pub x: u16, + pub y: u16, + pub w: u16, + pub h: u16, +} + +/* +#[repr(C)] +pub struct HdrBitmap { + pub command: DisplayCommand, + pub offset: u16, + pub length: u16, + pub subcommand: DisplaySubcommand, + reserved: u16, +} +*/ + +#[repr(u16)] +#[derive(Debug, FromPrimitive, ToPrimitive)] +pub enum DisplaySubcommand { + SubCmdBitmapNormal = 0x0, + SubCmdBitmapCompressZ = 0x677a, + SubCmdBitmapCompressBz = 0x627a, + SubCmdBitmapCompressLz = 0x6c7a, + SubCmdBitmapCompressZs = 0x7a73, +} + +pub const TILE_SIZE: u16 = 8; +pub const TILE_WIDTH: u16 = 56; +pub const TILE_HEIGHT: u16 = 20; +pub const PIXEL_WIDTH: u16 = TILE_WIDTH * TILE_SIZE; +pub const PIXEL_HEIGHT: u16 = TILE_HEIGHT * TILE_SIZE; +pub const PIXEL_COUNT: usize = PIXEL_WIDTH as usize * PIXEL_HEIGHT as usize; + +#[derive(Debug)] +pub enum ReadHeaderError { + BufferTooSmall, + WrongCommandEndianness(u16, DisplayCommand), + InvalidCommand(u16), +} + +pub fn read_header(buffer: &[u8]) -> Result { + if buffer.len() < size_of::() { + return Err(ReadHeaderError::BufferTooSmall); + } + + let command_u16 = read_beu16(&buffer[0..=1]); + return match num::FromPrimitive::from_u16(command_u16) { + Some(command) => Ok(HdrWindow { + command, + x: read_beu16(&buffer[2..=3]), + y: read_beu16(&buffer[4..=5]), + w: read_beu16(&buffer[6..=7]), + h: read_beu16(&buffer[8..=9]), + }), + None => { + let maybe_command: Option = + num::FromPrimitive::from_u16(u16::swap_bytes(command_u16)); + return match maybe_command { + None => Err(ReadHeaderError::InvalidCommand(command_u16)), + Some(command) => Err(ReadHeaderError::WrongCommandEndianness( + command_u16, + command, + )), + }; + } + }; +} + +fn read_beu16(buffer: &[u8]) -> u16 { + let buffer: [u8; 2] = buffer + .try_into() + .expect("cannot read u16 from buffer with size != 2"); + return u16::from_be_bytes(buffer); +} diff --git a/src/upd_loop.rs b/src/upd_loop.rs index 1f68d05..103c713 100644 --- a/src/upd_loop.rs +++ b/src/upd_loop.rs @@ -1,8 +1,8 @@ -use crate::DISPLAY; -use log::{error, info, warn}; -use pixel_shared_rs::{ +use crate::protocol::{ read_header, DisplayCommand, HdrWindow, ReadHeaderError, PIXEL_WIDTH, TILE_SIZE, }; +use crate::DISPLAY; +use log::{error, info, warn}; use std::io::ErrorKind; use std::mem::size_of; use std::net::UdpSocket; @@ -109,7 +109,7 @@ fn check_payload_size(buf: &[u8], expected: usize) -> bool { } fn print_bitmap_linear_win(header: &HdrWindow, payload: &[u8]) { - if !check_payload_size(payload, (header.w * header.h) as usize) { + if !check_payload_size(payload, header.w as usize * header.h as usize) { return; } @@ -118,19 +118,15 @@ fn print_bitmap_linear_win(header: &HdrWindow, payload: &[u8]) { header.x, header.y ); - let mut text_repr = String::new(); - for y in 0..header.h { for byte_x in 0..header.w { let byte_index = (y * header.w + byte_x) as usize; let byte = payload[byte_index]; - for pixel_x in 1u8..=8u8 { - let bit_index = 8 - pixel_x; + for pixel_x in 0u8..8u8 { + let bit_index = 7 - pixel_x; let bitmask = 1 << bit_index; let is_set = byte & bitmask != 0; - let char = if is_set { '█' } else { ' ' }; - text_repr.push(char); let x = byte_x * TILE_SIZE + pixel_x as u16; @@ -143,10 +139,7 @@ fn print_bitmap_linear_win(header: &HdrWindow, payload: &[u8]) { } } } - - text_repr.push('\n'); } - info!("{}", text_repr); } // TODO: actually convert from CP437