From c8f9cca47a83e7524cfa7b8d05d873b2eb344317 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 19 Jan 2025 11:49:44 +0100 Subject: [PATCH 01/11] extract methods, CommandExecutor --- src/execute_command.rs | 370 +++++++++++++++++++++-------------------- src/font_renderer.rs | 54 ++++-- src/gui.rs | 65 ++++---- src/main.rs | 116 ++++++------- 4 files changed, 325 insertions(+), 280 deletions(-) diff --git a/src/execute_command.rs b/src/execute_command.rs index b0701a8..c175638 100644 --- a/src/execute_command.rs +++ b/src/execute_command.rs @@ -3,210 +3,226 @@ use servicepoint::{ Bitmap, BrightnessGrid, CharGrid, Command, Cp437Grid, Grid, Origin, Tiles, PIXEL_COUNT, PIXEL_WIDTH, TILE_SIZE, }; -use std::sync::{RwLock, RwLockWriteGuard}; +use std::sync::RwLock; use crate::font::Cp437Font; use crate::font_renderer::FontRenderer8x8; -pub(crate) fn execute_command( - command: Command, - cp436_font: &Cp437Font, - utf8_font: &FontRenderer8x8, - display_ref: &RwLock, - luma_ref: &RwLock, -) -> bool { - debug!("received {command:?}"); - match command { - Command::Clear => { - info!("clearing display"); - display_ref.write().unwrap().fill(false); - } - Command::HardReset => { - warn!("display shutting down"); - return false; - } - Command::BitmapLinearWin(Origin { x, y, .. }, pixels, _) => { - let mut display = display_ref.write().unwrap(); - print_pixel_grid(x, y, &pixels, &mut display); - } - Command::Cp437Data(origin, grid) => { - let mut display = display_ref.write().unwrap(); - print_cp437_data(origin, &grid, cp436_font, &mut display); - } - #[allow(deprecated)] - Command::BitmapLegacy => { - warn!("ignoring deprecated command {:?}", command); - } - // TODO: how to deduplicate this code in a rusty way? - Command::BitmapLinear(offset, vec, _) => { - if !check_bitmap_valid(offset as u16, vec.len()) { - return true; - } - let mut display = display_ref.write().unwrap(); - for bitmap_index in 0..vec.len() { - let (x, y) = get_coordinates_for_index(offset, bitmap_index); - display.set(x, y, vec[bitmap_index]); - } - } - Command::BitmapLinearAnd(offset, vec, _) => { - if !check_bitmap_valid(offset as u16, vec.len()) { - return true; - } - let mut display = display_ref.write().unwrap(); - for bitmap_index in 0..vec.len() { - let (x, y) = get_coordinates_for_index(offset, bitmap_index); - let old_value = display.get(x, y); - display.set(x, y, old_value && vec[bitmap_index]); - } - } - Command::BitmapLinearOr(offset, vec, _) => { - if !check_bitmap_valid(offset as u16, vec.len()) { - return true; - } - let mut display = display_ref.write().unwrap(); - for bitmap_index in 0..vec.len() { - let (x, y) = get_coordinates_for_index(offset, bitmap_index); - let old_value = display.get(x, y); - display.set(x, y, old_value || vec[bitmap_index]); - } - } - Command::BitmapLinearXor(offset, vec, _) => { - if !check_bitmap_valid(offset as u16, vec.len()) { - return true; - } - let mut display = display_ref.write().unwrap(); - for bitmap_index in 0..vec.len() { - let (x, y) = get_coordinates_for_index(offset, bitmap_index); - let old_value = display.get(x, y); - display.set(x, y, old_value ^ vec[bitmap_index]); - } - } - Command::CharBrightness(origin, grid) => { - let mut luma = luma_ref.write().unwrap(); - for inner_y in 0..grid.height() { - for inner_x in 0..grid.width() { - let brightness = grid.get(inner_x, inner_y); - luma.set( - origin.x + inner_x, - origin.y + inner_y, - brightness, - ); - } - } - } - Command::Brightness(brightness) => { - luma_ref.write().unwrap().fill(brightness); - } - Command::FadeOut => { - error!("command not implemented: {command:?}") - } - Command::Utf8Data(origin, grid) => { - let mut display = display_ref.write().unwrap(); - print_utf8_data(origin, &grid, utf8_font, &mut display); - } - }; - - true +pub struct CommandExecutor<'t> { + display: &'t RwLock, + luma: &'t RwLock, + cp437_font: Cp437Font, + utf8_font: FontRenderer8x8, } -fn check_bitmap_valid(offset: u16, payload_len: usize) -> bool { - if offset as usize + payload_len > PIXEL_COUNT { - error!("bitmap with offset {offset} is too big ({payload_len} bytes)"); - return false; +impl<'t> CommandExecutor<'t> { + pub fn new( + display: &'t RwLock, + luma: &'t RwLock, + ) -> Self { + CommandExecutor { + display, + luma, + utf8_font: FontRenderer8x8::default(), + cp437_font: Cp437Font::default(), + } } - true -} + pub(crate) fn execute(&self, command: Command) -> bool { + debug!("received {command:?}"); + match command { + Command::Clear => { + info!("clearing display"); + self.display.write().unwrap().fill(false); + } + Command::HardReset => { + warn!("display shutting down"); + return false; + } + Command::BitmapLinearWin(Origin { x, y, .. }, pixels, _) => { + self.print_pixel_grid(x, y, &pixels); + } + Command::Cp437Data(origin, grid) => { + self.print_cp437_data(origin, &grid); + } + #[allow(deprecated)] + Command::BitmapLegacy => { + warn!("ignoring deprecated command {:?}", command); + } + // TODO: how to deduplicate this code in a rusty way? + Command::BitmapLinear(offset, vec, _) => { + if !Self::check_bitmap_valid(offset as u16, vec.len()) { + return true; + } + let mut display = self.display.write().unwrap(); + for bitmap_index in 0..vec.len() { + let (x, y) = + Self::get_coordinates_for_index(offset, bitmap_index); + display.set(x, y, vec[bitmap_index]); + } + } + Command::BitmapLinearAnd(offset, vec, _) => { + if !Self::check_bitmap_valid(offset as u16, vec.len()) { + return true; + } + let mut display = self.display.write().unwrap(); + for bitmap_index in 0..vec.len() { + let (x, y) = + Self::get_coordinates_for_index(offset, bitmap_index); + let old_value = display.get(x, y); + display.set(x, y, old_value && vec[bitmap_index]); + } + } + Command::BitmapLinearOr(offset, vec, _) => { + if !Self::check_bitmap_valid(offset as u16, vec.len()) { + return true; + } + let mut display = self.display.write().unwrap(); + for bitmap_index in 0..vec.len() { + let (x, y) = + Self::get_coordinates_for_index(offset, bitmap_index); + let old_value = display.get(x, y); + display.set(x, y, old_value || vec[bitmap_index]); + } + } + Command::BitmapLinearXor(offset, vec, _) => { + if !Self::check_bitmap_valid(offset as u16, vec.len()) { + return true; + } + let mut display = self.display.write().unwrap(); + for bitmap_index in 0..vec.len() { + let (x, y) = + Self::get_coordinates_for_index(offset, bitmap_index); + let old_value = display.get(x, y); + display.set(x, y, old_value ^ vec[bitmap_index]); + } + } + Command::CharBrightness(origin, grid) => { + let mut luma = self.luma.write().unwrap(); + for inner_y in 0..grid.height() { + for inner_x in 0..grid.width() { + let brightness = grid.get(inner_x, inner_y); + luma.set( + origin.x + inner_x, + origin.y + inner_y, + brightness, + ); + } + } + } + Command::Brightness(brightness) => { + self.luma.write().unwrap().fill(brightness); + } + Command::FadeOut => { + error!("command not implemented: {command:?}") + } + Command::Utf8Data(origin, grid) => { + self.print_utf8_data(origin, &grid); + } + }; -fn print_cp437_data( - origin: Origin, - grid: &Cp437Grid, - font: &Cp437Font, - display: &mut RwLockWriteGuard, -) { - let Origin { x, y, .. } = origin; - for char_y in 0usize..grid.height() { - for char_x in 0usize..grid.width() { - let char_code = grid.get(char_x, char_y); - trace!( + true + } + + fn check_bitmap_valid(offset: u16, payload_len: usize) -> bool { + if offset as usize + payload_len > PIXEL_COUNT { + error!( + "bitmap with offset {offset} is too big ({payload_len} bytes)" + ); + return false; + } + + true + } + + fn print_cp437_data(&self, origin: Origin, grid: &Cp437Grid) { + let font = &self.cp437_font; + let Origin { x, y, .. } = origin; + for char_y in 0usize..grid.height() { + for char_x in 0usize..grid.width() { + let char_code = grid.get(char_x, char_y); + trace!( "drawing char_code {char_code:#04x} (if this was UTF-8, it would be {})", char::from(char_code) ); - let tile_x = char_x + x; - let tile_y = char_y + y; + let tile_x = char_x + x; + let tile_y = char_y + y; - let bitmap = font.get_bitmap(char_code); - if !print_pixel_grid( - tile_x * TILE_SIZE, - tile_y * TILE_SIZE, - bitmap, - display, - ) { - error!("stopping drawing text because char draw failed"); - return; + let bitmap = font.get_bitmap(char_code); + if !self.print_pixel_grid( + tile_x * TILE_SIZE, + tile_y * TILE_SIZE, + bitmap, + ) { + error!("stopping drawing text because char draw failed"); + return; + } } } } -} -fn print_utf8_data( - origin: Origin, - grid: &CharGrid, - font: &FontRenderer8x8, - display: &mut RwLockWriteGuard, -) { - let Origin { x, y, .. } = origin; - for char_y in 0usize..grid.height() { - for char_x in 0usize..grid.width() { - let char = grid.get(char_x, char_y); - trace!("drawing {char}"); + fn print_utf8_data(&self, origin: Origin, grid: &CharGrid) { + let mut display = self.display.write().unwrap(); - let tile_x = char_x + x; - let tile_y = char_y + y; + let Origin { x, y, .. } = origin; + for char_y in 0usize..grid.height() { + for char_x in 0usize..grid.width() { + let char = grid.get(char_x, char_y); + trace!("drawing {char}"); - if let Err(e) = font.render( - char, - display, - Origin::new(tile_x * TILE_SIZE, tile_y * TILE_SIZE), - ) { - error!("stopping drawing text because char draw failed: {e}"); - return; + let tile_x = char_x + x; + let tile_y = char_y + y; + + if let Err(e) = self.utf8_font.render( + char, + &mut display, + Origin::new(tile_x * TILE_SIZE, tile_y * TILE_SIZE), + ) { + error!( + "stopping drawing text because char draw failed: {e}" + ); + return; + } } } } -} -fn print_pixel_grid( - offset_x: usize, - offset_y: usize, - pixels: &Bitmap, - display: &mut RwLockWriteGuard, -) -> bool { - debug!( - "printing {}x{} grid at {offset_x} {offset_y}", - pixels.width(), - pixels.height() - ); - for inner_y in 0..pixels.height() { - for inner_x in 0..pixels.width() { - let is_set = pixels.get(inner_x, inner_y); - let x = offset_x + inner_x; - let y = offset_y + inner_y; + fn print_pixel_grid( + &self, + offset_x: usize, + offset_y: usize, + pixels: &Bitmap, + ) -> bool { + debug!( + "printing {}x{} grid at {offset_x} {offset_y}", + pixels.width(), + pixels.height() + ); + let mut display = self.display.write().unwrap(); + for inner_y in 0..pixels.height() { + for inner_x in 0..pixels.width() { + let is_set = pixels.get(inner_x, inner_y); + let x = offset_x + inner_x; + let y = offset_y + inner_y; - if x >= display.width() || y >= display.height() { - error!("stopping pixel grid draw because coordinate {x} {y} is out of bounds"); - return false; + if x >= display.width() || y >= display.height() { + error!("stopping pixel grid draw because coordinate {x} {y} is out of bounds"); + return false; + } + + display.set(x, y, is_set); } - - display.set(x, y, is_set); } + + true } - true -} - -fn get_coordinates_for_index(offset: usize, index: usize) -> (usize, usize) { - let pixel_index = offset + index; - (pixel_index % PIXEL_WIDTH, pixel_index / PIXEL_WIDTH) + fn get_coordinates_for_index( + offset: usize, + index: usize, + ) -> (usize, usize) { + let pixel_index = offset + index; + (pixel_index % PIXEL_WIDTH, pixel_index / PIXEL_WIDTH) + } } diff --git a/src/font_renderer.rs b/src/font_renderer.rs index d777d86..dbb7a91 100644 --- a/src/font_renderer.rs +++ b/src/font_renderer.rs @@ -1,18 +1,33 @@ use crate::font_renderer::RenderError::{GlyphNotFound, OutOfBounds}; -use font_kit::canvas::{Canvas, Format, RasterizationOptions}; -use font_kit::error::GlyphLoadingError; -use font_kit::family_name::FamilyName; -use font_kit::font::Font; -use font_kit::hinting::HintingOptions; -use font_kit::properties::Properties; -use font_kit::source::SystemSource; -use pathfinder_geometry::transform2d::Transform2F; -use pathfinder_geometry::vector::{vec2f, vec2i}; +use font_kit::{ + canvas::{Canvas, Format, RasterizationOptions}, + error::GlyphLoadingError, + family_name::FamilyName, + font::Font, + hinting::HintingOptions, + properties::Properties, + source::SystemSource, +}; +use pathfinder_geometry::{ + transform2d::Transform2F, + vector::{vec2f, vec2i}, +}; use servicepoint::{Bitmap, Grid, Origin, Pixels, TILE_SIZE}; use std::sync::Mutex; +struct SendFont(Font); + +// struct is only using primitives and pointers - lets try if it is only missing the declaration +unsafe impl Send for SendFont {} + +impl AsRef for SendFont { + fn as_ref(&self) -> &Font { + &self.0 + } +} + pub struct FontRenderer8x8 { - font: Font, + font: SendFont, canvas: Mutex, fallback_char: Option, } @@ -35,7 +50,7 @@ impl FontRenderer8x8 { assert_eq!(canvas.stride, TILE_SIZE); let fallback_char = fallback_char.and_then(|c| font.glyph_for_char(c)); let result = Self { - font, + font: SendFont(font), fallback_char, canvas: Mutex::new(canvas), }; @@ -49,14 +64,18 @@ impl FontRenderer8x8 { offset: Origin, ) -> Result<(), RenderError> { let mut canvas = self.canvas.lock().unwrap(); - let glyph_id = self.font.glyph_for_char(char).or(self.fallback_char); + let glyph_id = self + .font + .as_ref() + .glyph_for_char(char) + .or(self.fallback_char); let glyph_id = match glyph_id { None => return Err(GlyphNotFound(char)), Some(val) => val, }; canvas.pixels.fill(0); - self.font.rasterize_glyph( + self.font.as_ref().rasterize_glyph( &mut canvas, glyph_id, TILE_SIZE as f32, @@ -85,7 +104,14 @@ impl FontRenderer8x8 { impl Default for FontRenderer8x8 { fn default() -> Self { let utf8_font = SystemSource::new() - .select_best_match(&[FamilyName::Monospace], &Properties::new()) + .select_best_match( + &[ + FamilyName::Title("Roboto Mono".to_string()), + FamilyName::Title("Fira Mono".to_string()), + FamilyName::Monospace, + ], + &Properties::new(), + ) .unwrap() .load() .unwrap(); diff --git a/src/gui.rs b/src/gui.rs index c2b843d..a90f76a 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,3 +1,4 @@ +use std::slice::ChunksExactMut; use std::sync::mpsc::Sender; use std::sync::RwLock; @@ -62,24 +63,25 @@ impl<'t> App<'t> { fn draw(&mut self) { let window = self.window.as_ref().unwrap(); - let mut pixels = { - let window_size = window.inner_size(); - let surface_texture = SurfaceTexture::new( - window_size.width, - window_size.height, - &window, - ); - Pixels::new( - self.logical_size.width as u32, - self.logical_size.height as u32, - surface_texture, - ) - .unwrap() - }; + let window_size = window.inner_size(); + let surface_texture = + SurfaceTexture::new(window_size.width, window_size.height, &window); + let mut pixels = Pixels::new( + self.logical_size.width as u32, + self.logical_size.height as u32, + surface_texture, + ) + .unwrap(); let mut frame = pixels.frame_mut().chunks_exact_mut(4); + self.draw_frame(&mut frame); + pixels.render().expect("could not render"); + } + + fn draw_frame(&self, frame: &mut ChunksExactMut) { let display = self.display.read().unwrap(); let luma = self.luma.read().unwrap(); + for y in 0..PIXEL_HEIGHT { if self.cli.spacers && y != 0 && y % TILE_SIZE == 0 { // cannot just frame.skip(PIXEL_WIDTH as usize * SPACER_HEIGHT as usize) because of typing @@ -90,30 +92,29 @@ impl<'t> App<'t> { for x in 0..PIXEL_WIDTH { let is_set = display.get(x, y); - let brightness: u8 = - luma.get(x / TILE_SIZE, y / TILE_SIZE).into(); - let max_brightness: u8 = Brightness::MAX.into(); - let scale: f32 = (u8::MAX as f32) / (max_brightness as f32); - + let brightness = + u8::from(luma.get(x / TILE_SIZE, y / TILE_SIZE)); + let scale = + (u8::MAX as f32) / (u8::from(Brightness::MAX) as f32); let brightness = (scale * brightness as f32) as u8; - - let color = if is_set { - [ - if self.cli.red { brightness } else { 0u8 }, - if self.cli.green { brightness } else { 0u8 }, - if self.cli.blue { brightness } else { 0u8 }, - 255, - ] - } else { - [0u8, 0, 0, 255] - }; - + let color = self.get_color(is_set, brightness); let pixel = frame.next().unwrap(); pixel.copy_from_slice(&color); } } + } - pixels.render().expect("could not render"); + fn get_color(&self, is_set: bool, brightness: u8) -> [u8; 4] { + if is_set { + [ + if self.cli.red { brightness } else { 0u8 }, + if self.cli.green { brightness } else { 0u8 }, + if self.cli.blue { brightness } else { 0u8 }, + 255, + ] + } else { + [0u8, 0, 0, 255] + } } } diff --git a/src/main.rs b/src/main.rs index 8b2396f..ccc821f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,16 @@ #![deny(clippy::all)] +use crate::execute_command::CommandExecutor; +use crate::gui::{App, AppEvents}; +use clap::Parser; +use log::{info, warn, LevelFilter}; +use servicepoint::*; use std::io::ErrorKind; use std::net::UdpSocket; use std::sync::{mpsc, RwLock}; use std::time::Duration; - -use clap::Parser; -use log::{info, warn, LevelFilter}; -use servicepoint::*; use winit::event_loop::{ControlFlow, EventLoop}; -use crate::execute_command::execute_command; -use crate::font::Cp437Font; -use crate::font_renderer::FontRenderer8x8; -use crate::gui::{App, AppEvents}; - mod execute_command; mod font; mod font_renderer; @@ -34,6 +30,8 @@ struct Cli { blue: bool, } +const BUF_SIZE: usize = 8985; + fn main() { env_logger::builder() .filter_level(LevelFilter::Info) @@ -57,19 +55,8 @@ fn main() { luma.fill(Brightness::MAX); let luma = RwLock::new(luma); - run(&display, &luma, socket, Cp437Font::default(), &cli); -} - -fn run( - display_ref: &RwLock, - luma_ref: &RwLock, - socket: UdpSocket, - cp437_font: Cp437Font, - cli: &Cli, -) { let (stop_udp_tx, stop_udp_rx) = mpsc::channel(); - - let mut app = App::new(display_ref, luma_ref, stop_udp_tx, cli); + let mut app = App::new(&display, &luma, stop_udp_tx, &cli); let event_loop = EventLoop::with_user_event() .build() @@ -77,46 +64,23 @@ fn run( event_loop.set_control_flow(ControlFlow::Wait); let event_proxy = event_loop.create_proxy(); + let command_executor = CommandExecutor::new(&display, &luma); std::thread::scope(move |scope| { - let udp_thread = scope.spawn(move || { - let mut buf = [0; 8985]; - let utf8_font = FontRenderer8x8::default(); - + scope.spawn(move || { + let mut buf = [0; BUF_SIZE]; while stop_udp_rx.try_recv().is_err() { - let (amount, _) = match socket.recv_from(&mut buf) { - Err(err) if err.kind() == ErrorKind::WouldBlock => { - std::thread::sleep(Duration::from_millis(1)); - continue; - } - Ok(result) => result, - other => other.unwrap(), + let amount = match receive_into_buf(&socket, &mut buf) { + Some(value) => value, + None => continue, }; - if amount == buf.len() { - warn!( - "the received package may have been truncated to a length of {}", - amount - ); - } - - let package = match servicepoint::Packet::try_from(&buf[..amount]) { - Err(_) => { - warn!("could not load packet with length {amount} into header"); - continue; - } - Ok(package) => package, + let command = match command_from_slice(&buf[..amount]) { + Some(value) => value, + None => continue, }; - let command = match Command::try_from(package) { - Err(err) => { - warn!("could not read command for packet: {:?}", err); - continue; - } - Ok(val) => val, - }; - - if !execute_command(command, &cp437_font, &utf8_font, display_ref, luma_ref) { + if !command_executor.execute(command) { // hard reset event_proxy .send_event(AppEvents::UdpThreadClosed) @@ -129,11 +93,49 @@ fn run( .expect("could not send packet handled event"); } }); - event_loop .run_app(&mut app) .expect("could not run event loop"); - - udp_thread.join().expect("could not join udp thread"); }); } + +fn command_from_slice(slice: &[u8]) -> Option { + let package = match servicepoint::Packet::try_from(slice) { + Err(_) => { + warn!("could not load packet with length {}", slice.len()); + return None; + } + Ok(package) => package, + }; + + let command = match Command::try_from(package) { + Err(err) => { + warn!("could not read command for packet: {:?}", err); + return None; + } + Ok(val) => val, + }; + Some(command) +} + +fn receive_into_buf( + socket: &UdpSocket, + buf: &mut [u8; BUF_SIZE], +) -> Option { + let (amount, _) = match socket.recv_from(buf) { + Err(err) if err.kind() == ErrorKind::WouldBlock => { + std::thread::sleep(Duration::from_millis(1)); + return None; + } + Ok(result) => result, + other => other.unwrap(), + }; + + if amount == buf.len() { + warn!( + "the received package may have been truncated to a length of {}", + amount + ); + } + Some(amount) +} From 31a8f6a40aa4d2c5b9be72be2e04b75e5fce7b55 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 26 Jan 2025 12:02:27 +0100 Subject: [PATCH 02/11] rename App to Gui, "optimize" some stuff, ExecutionResult --- flake.nix | 2 + src/execute_command.rs | 172 +++++++++++++++++++++++------------------ src/gui.rs | 88 +++++++++++---------- src/main.rs | 83 +++++++++++++++----- 4 files changed, 205 insertions(+), 140 deletions(-) diff --git a/flake.nix b/flake.nix index d09e841..6254f4e 100644 --- a/flake.nix +++ b/flake.nix @@ -71,6 +71,8 @@ [ xe xz + + roboto ] ++ lib.optionals pkgs.stdenv.isLinux ( with pkgs; diff --git a/src/execute_command.rs b/src/execute_command.rs index c175638..a32c7b2 100644 --- a/src/execute_command.rs +++ b/src/execute_command.rs @@ -1,12 +1,13 @@ -use log::{debug, error, info, trace, warn}; -use servicepoint::{ - Bitmap, BrightnessGrid, CharGrid, Command, Cp437Grid, Grid, Origin, Tiles, - PIXEL_COUNT, PIXEL_WIDTH, TILE_SIZE, -}; -use std::sync::RwLock; - +use crate::execute_command::ExecutionResult::{Failure, Shutdown, Success}; use crate::font::Cp437Font; use crate::font_renderer::FontRenderer8x8; +use log::{debug, error, info, trace, warn}; +use servicepoint::{ + BitVec, Bitmap, BrightnessGrid, CharGrid, Command, Cp437Grid, Grid, Offset, + Origin, Tiles, PIXEL_COUNT, PIXEL_WIDTH, TILE_SIZE, +}; +use std::ops::{BitAnd, BitOr, BitXor}; +use std::sync::RwLock; pub struct CommandExecutor<'t> { display: &'t RwLock, @@ -15,6 +16,13 @@ pub struct CommandExecutor<'t> { utf8_font: FontRenderer8x8, } +#[must_use] +pub enum ExecutionResult { + Success, + Failure, + Shutdown, +} + impl<'t> CommandExecutor<'t> { pub fn new( display: &'t RwLock, @@ -28,100 +36,92 @@ impl<'t> CommandExecutor<'t> { } } - pub(crate) fn execute(&self, command: Command) -> bool { + pub(crate) fn execute(&self, command: Command) -> ExecutionResult { debug!("received {command:?}"); match command { Command::Clear => { info!("clearing display"); self.display.write().unwrap().fill(false); + Success } Command::HardReset => { warn!("display shutting down"); - return false; + Shutdown } Command::BitmapLinearWin(Origin { x, y, .. }, pixels, _) => { - self.print_pixel_grid(x, y, &pixels); + self.print_pixel_grid(x, y, &pixels) } Command::Cp437Data(origin, grid) => { - self.print_cp437_data(origin, &grid); + self.print_cp437_data(origin, &grid) } #[allow(deprecated)] Command::BitmapLegacy => { warn!("ignoring deprecated command {:?}", command); - } - // TODO: how to deduplicate this code in a rusty way? - Command::BitmapLinear(offset, vec, _) => { - if !Self::check_bitmap_valid(offset as u16, vec.len()) { - return true; - } - let mut display = self.display.write().unwrap(); - for bitmap_index in 0..vec.len() { - let (x, y) = - Self::get_coordinates_for_index(offset, bitmap_index); - display.set(x, y, vec[bitmap_index]); - } + Failure } Command::BitmapLinearAnd(offset, vec, _) => { - if !Self::check_bitmap_valid(offset as u16, vec.len()) { - return true; - } - let mut display = self.display.write().unwrap(); - for bitmap_index in 0..vec.len() { - let (x, y) = - Self::get_coordinates_for_index(offset, bitmap_index); - let old_value = display.get(x, y); - display.set(x, y, old_value && vec[bitmap_index]); - } + self.execute_bitmap_linear(offset, vec, BitAnd::bitand) } Command::BitmapLinearOr(offset, vec, _) => { - if !Self::check_bitmap_valid(offset as u16, vec.len()) { - return true; - } - let mut display = self.display.write().unwrap(); - for bitmap_index in 0..vec.len() { - let (x, y) = - Self::get_coordinates_for_index(offset, bitmap_index); - let old_value = display.get(x, y); - display.set(x, y, old_value || vec[bitmap_index]); - } + self.execute_bitmap_linear(offset, vec, BitOr::bitor) } Command::BitmapLinearXor(offset, vec, _) => { - if !Self::check_bitmap_valid(offset as u16, vec.len()) { - return true; - } - let mut display = self.display.write().unwrap(); - for bitmap_index in 0..vec.len() { - let (x, y) = - Self::get_coordinates_for_index(offset, bitmap_index); - let old_value = display.get(x, y); - display.set(x, y, old_value ^ vec[bitmap_index]); - } + self.execute_bitmap_linear(offset, vec, BitXor::bitxor) + } + Command::BitmapLinear(offset, vec, _) => { + self.execute_bitmap_linear(offset, vec, move |_, new| new) } Command::CharBrightness(origin, grid) => { - let mut luma = self.luma.write().unwrap(); - for inner_y in 0..grid.height() { - for inner_x in 0..grid.width() { - let brightness = grid.get(inner_x, inner_y); - luma.set( - origin.x + inner_x, - origin.y + inner_y, - brightness, - ); - } - } + self.execute_char_brightness(origin, grid) } Command::Brightness(brightness) => { self.luma.write().unwrap().fill(brightness); + Success } Command::FadeOut => { - error!("command not implemented: {command:?}") + error!("command not implemented: {command:?}"); + Success } Command::Utf8Data(origin, grid) => { - self.print_utf8_data(origin, &grid); + self.print_utf8_data(origin, &grid) } - }; + } + } - true + fn execute_char_brightness( + &self, + origin: Origin, + grid: BrightnessGrid, + ) -> ExecutionResult { + let mut luma = self.luma.write().unwrap(); + for inner_y in 0..grid.height() { + for inner_x in 0..grid.width() { + let brightness = grid.get(inner_x, inner_y); + luma.set(origin.x + inner_x, origin.y + inner_y, brightness); + } + } + Success + } + + fn execute_bitmap_linear( + &self, + offset: Offset, + vec: BitVec, + op: Op, + ) -> ExecutionResult + where + Op: Fn(bool, bool) -> bool, + { + if !Self::check_bitmap_valid(offset as u16, vec.len()) { + return Failure; + } + let mut display = self.display.write().unwrap(); + for bitmap_index in 0..vec.len() { + let (x, y) = Self::get_coordinates_for_index(offset, bitmap_index); + let old_value = display.get(x, y); + display.set(x, y, op(old_value, vec[bitmap_index])); + } + Success } fn check_bitmap_valid(offset: u16, payload_len: usize) -> bool { @@ -135,7 +135,11 @@ impl<'t> CommandExecutor<'t> { true } - fn print_cp437_data(&self, origin: Origin, grid: &Cp437Grid) { + fn print_cp437_data( + &self, + origin: Origin, + grid: &Cp437Grid, + ) -> ExecutionResult { let font = &self.cp437_font; let Origin { x, y, .. } = origin; for char_y in 0usize..grid.height() { @@ -150,19 +154,31 @@ impl<'t> CommandExecutor<'t> { let tile_y = char_y + y; let bitmap = font.get_bitmap(char_code); - if !self.print_pixel_grid( + match self.print_pixel_grid( tile_x * TILE_SIZE, tile_y * TILE_SIZE, bitmap, ) { - error!("stopping drawing text because char draw failed"); - return; + Success => {} + Failure => { + error!( + "stopping drawing text because char draw failed" + ); + return Failure; + } + Shutdown => return Shutdown, } } } + + Success } - fn print_utf8_data(&self, origin: Origin, grid: &CharGrid) { + fn print_utf8_data( + &self, + origin: Origin, + grid: &CharGrid, + ) -> ExecutionResult { let mut display = self.display.write().unwrap(); let Origin { x, y, .. } = origin; @@ -182,10 +198,12 @@ impl<'t> CommandExecutor<'t> { error!( "stopping drawing text because char draw failed: {e}" ); - return; + return Failure; } } } + + Success } fn print_pixel_grid( @@ -193,7 +211,7 @@ impl<'t> CommandExecutor<'t> { offset_x: usize, offset_y: usize, pixels: &Bitmap, - ) -> bool { + ) -> ExecutionResult { debug!( "printing {}x{} grid at {offset_x} {offset_y}", pixels.width(), @@ -208,14 +226,14 @@ impl<'t> CommandExecutor<'t> { if x >= display.width() || y >= display.height() { error!("stopping pixel grid draw because coordinate {x} {y} is out of bounds"); - return false; + return Failure; } display.set(x, y, is_set); } } - true + Success } fn get_coordinates_for_index( diff --git a/src/gui.rs b/src/gui.rs index a90f76a..8fceba8 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -4,10 +4,7 @@ use std::sync::RwLock; use log::{info, warn}; use pixels::{Pixels, SurfaceTexture}; -use servicepoint::{ - Bitmap, Brightness, BrightnessGrid, Grid, PIXEL_HEIGHT, PIXEL_WIDTH, - TILE_SIZE, -}; +use servicepoint::*; use winit::application::ApplicationHandler; use winit::dpi::LogicalSize; use winit::event::WindowEvent; @@ -17,7 +14,7 @@ use winit::window::{Window, WindowId}; use crate::Cli; -pub struct App<'t> { +pub struct Gui<'t> { display: &'t RwLock, luma: &'t RwLock, window: Option, @@ -27,6 +24,11 @@ pub struct App<'t> { } const SPACER_HEIGHT: usize = 4; +const NUM_SPACERS: usize = (PIXEL_HEIGHT / TILE_SIZE) - 1; +const PIXEL_HEIGHT_WITH_SPACERS: usize = + PIXEL_HEIGHT + NUM_SPACERS * SPACER_HEIGHT; + +const OFF_COLOR: [u8; 4] = [0u8, 0, 0, 255]; #[derive(Debug)] pub enum AppEvents { @@ -34,30 +36,20 @@ pub enum AppEvents { UdpThreadClosed, } -impl<'t> App<'t> { +impl<'t> Gui<'t> { pub fn new( display: &'t RwLock, luma: &'t RwLock, stop_udp_tx: Sender<()>, cli: &'t Cli, ) -> Self { - let logical_size = { - let height = if cli.spacers { - let num_spacers = (PIXEL_HEIGHT / TILE_SIZE) - 1; - PIXEL_HEIGHT + num_spacers * SPACER_HEIGHT - } else { - PIXEL_HEIGHT - }; - LogicalSize::new(PIXEL_WIDTH as u16, height as u16) - }; - - App { + Gui { display, luma, stop_udp_tx, - window: None, cli, - logical_size, + window: None, + logical_size: Self::get_logical_size(cli.spacers), } } @@ -66,6 +58,9 @@ impl<'t> App<'t> { let window_size = window.inner_size(); let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window); + + // TODO: fix pixels: creating a new instance per draw crashes after some time on macOS, + // but keeping one instance for the lifetime of the Gui SIGSEGVs on Wayland when entering a background state. let mut pixels = Pixels::new( self.logical_size.width as u32, self.logical_size.height as u32, @@ -81,44 +76,53 @@ impl<'t> App<'t> { fn draw_frame(&self, frame: &mut ChunksExactMut) { let display = self.display.read().unwrap(); let luma = self.luma.read().unwrap(); + let brightness_scale = (u8::MAX as f32) / (u8::from(Brightness::MAX) as f32); - for y in 0..PIXEL_HEIGHT { - if self.cli.spacers && y != 0 && y % TILE_SIZE == 0 { + for tile_y in 0..TILE_HEIGHT { + if self.cli.spacers && tile_y != 0 { // cannot just frame.skip(PIXEL_WIDTH as usize * SPACER_HEIGHT as usize) because of typing for _ in 0..PIXEL_WIDTH * SPACER_HEIGHT { frame.next().unwrap(); } } - for x in 0..PIXEL_WIDTH { - let is_set = display.get(x, y); - let brightness = - u8::from(luma.get(x / TILE_SIZE, y / TILE_SIZE)); - let scale = - (u8::MAX as f32) / (u8::from(Brightness::MAX) as f32); - let brightness = (scale * brightness as f32) as u8; - let color = self.get_color(is_set, brightness); - let pixel = frame.next().unwrap(); - pixel.copy_from_slice(&color); + let start_y = tile_y * TILE_SIZE; + for y in start_y..start_y + TILE_SIZE { + for tile_x in 0..TILE_WIDTH { + let brightness = u8::from(luma.get(tile_x, tile_y)); + let brightness = (brightness_scale * brightness as f32) as u8; + let on_color = self.get_on_color(brightness); + let start_x = tile_x * TILE_SIZE; + for x in start_x..start_x + TILE_SIZE { + let color = if display.get(x, y) { on_color } else { OFF_COLOR }; + let pixel = frame.next().unwrap(); + pixel.copy_from_slice(&color); + } + } } } } - fn get_color(&self, is_set: bool, brightness: u8) -> [u8; 4] { - if is_set { - [ - if self.cli.red { brightness } else { 0u8 }, - if self.cli.green { brightness } else { 0u8 }, - if self.cli.blue { brightness } else { 0u8 }, - 255, - ] + fn get_on_color(&self, brightness: u8) -> [u8; 4] { + [ + if self.cli.red { brightness } else { 0u8 }, + if self.cli.green { brightness } else { 0u8 }, + if self.cli.blue { brightness } else { 0u8 }, + 255, + ] + } + + fn get_logical_size(spacers: bool) -> LogicalSize { + let height = if spacers { + PIXEL_HEIGHT_WITH_SPACERS } else { - [0u8, 0, 0, 255] - } + PIXEL_HEIGHT + }; + LogicalSize::new(PIXEL_WIDTH as u16, height as u16) } } -impl ApplicationHandler for App<'_> { +impl ApplicationHandler for Gui<'_> { fn resumed(&mut self, event_loop: &ActiveEventLoop) { let attributes = Window::default_attributes() .with_title("servicepoint-simulator") diff --git a/src/main.rs b/src/main.rs index ccc821f..433a4c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,11 @@ #![deny(clippy::all)] -use crate::execute_command::CommandExecutor; -use crate::gui::{App, AppEvents}; +use crate::{ + execute_command::{CommandExecutor, ExecutionResult}, + gui::{AppEvents, Gui}, +}; use clap::Parser; -use log::{info, warn, LevelFilter}; +use log::{error, info, warn, LevelFilter}; use servicepoint::*; use std::io::ErrorKind; use std::net::UdpSocket; @@ -18,27 +20,62 @@ mod gui; #[derive(Parser, Debug)] struct Cli { - #[arg(long, default_value = "0.0.0.0:2342")] + #[arg( + long, + default_value = "0.0.0.0:2342", + help = "address and port to bind to" + )] bind: String, - #[arg(short, long, default_value_t = false)] + #[arg( + short, + long, + default_value_t = false, + help = "add spacers between tile rows to simulate gaps in real display" + )] spacers: bool, - #[arg(short, long, default_value_t = false)] + #[arg( + short, + long, + help = "Set default log level lower. You can also change this via the RUST_LOG environment variable." + )] + debug: bool, + #[arg( + short, + long, + default_value_t = false, + help = "Use the red color channel" + )] red: bool, - #[arg(short, long, default_value_t = false)] + #[arg( + short, + long, + default_value_t = false, + help = "Use the green color channel" + )] green: bool, - #[arg(short, long, default_value_t = false)] + #[arg( + short, + long, + default_value_t = false, + help = "Use the blue color channel" + )] blue: bool, } const BUF_SIZE: usize = 8985; fn main() { + let mut cli = Cli::parse(); + env_logger::builder() - .filter_level(LevelFilter::Info) + .filter_level(if cli.debug { + LevelFilter::Debug + } else { + LevelFilter::Info + }) .parse_default_env() .init(); - let mut cli = Cli::parse(); if !(cli.red || cli.blue || cli.green) { cli.green = true; } @@ -56,7 +93,7 @@ fn main() { let luma = RwLock::new(luma); let (stop_udp_tx, stop_udp_rx) = mpsc::channel(); - let mut app = App::new(&display, &luma, stop_udp_tx, &cli); + let mut app = Gui::new(&display, &luma, stop_udp_tx, &cli); let event_loop = EventLoop::with_user_event() .build() @@ -80,17 +117,21 @@ fn main() { None => continue, }; - if !command_executor.execute(command) { - // hard reset - event_proxy - .send_event(AppEvents::UdpThreadClosed) - .expect("could not send close event"); - break; + match command_executor.execute(command) { + ExecutionResult::Success => { + event_proxy + .send_event(AppEvents::UdpPacketHandled) + .expect("could not send packet handled event"); + } + ExecutionResult::Failure => { + error!("failed to execute command"); + } + ExecutionResult::Shutdown => { + event_proxy + .send_event(AppEvents::UdpThreadClosed) + .expect("could not send close event"); + } } - - event_proxy - .send_event(AppEvents::UdpPacketHandled) - .expect("could not send packet handled event"); } }); event_loop From 3049a8bd7a27abfe69ce55160ab1c35c50549c2b Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 26 Jan 2025 12:40:59 +0100 Subject: [PATCH 03/11] split out gui options --- src/cli.rs | 57 +++++++++++++++ src/{font.rs => cp437_font.rs} | 26 +++---- src/execute_command.rs | 9 +-- src/font_renderer.rs | 25 ++++--- src/gui.rs | 20 +++--- src/main.rs | 126 ++++++++++++--------------------- 6 files changed, 143 insertions(+), 120 deletions(-) create mode 100644 src/cli.rs rename src/{font.rs => cp437_font.rs} (95%) diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..0925980 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,57 @@ +use clap::Parser; + +#[derive(Parser, Debug)] +pub struct Cli { + #[arg( + long, + default_value = "0.0.0.0:2342", + help = "address and port to bind to" + )] + pub bind: String, + #[arg( + short, + long, + help = "Set default log level lower. You can also change this via the RUST_LOG environment variable." + )] + pub debug: bool, + #[arg( + short, + long, + help = "The name of the font family to use. This defaults to the system monospace font." + )] + pub font: Option, + #[clap(flatten)] + pub gui: GuiOptions, +} + +#[derive(Parser, Debug)] +pub struct GuiOptions { + #[arg( + short, + long, + default_value_t = false, + help = "Use the red color channel" + )] + pub red: bool, + #[arg( + short, + long, + default_value_t = false, + help = "Use the green color channel" + )] + pub green: bool, + #[arg( + short, + long, + default_value_t = false, + help = "Use the blue color channel" + )] + pub blue: bool, + #[arg( + short, + long, + default_value_t = false, + help = "add spacers between tile rows to simulate gaps in real display" + )] + pub spacers: bool, +} \ No newline at end of file diff --git a/src/font.rs b/src/cp437_font.rs similarity index 95% rename from src/font.rs rename to src/cp437_font.rs index 5f19492..1ff34b9 100644 --- a/src/font.rs +++ b/src/cp437_font.rs @@ -18,7 +18,17 @@ impl Cp437Font { impl Default for Cp437Font { fn default() -> Self { - load_static() + let mut bitmaps = + core::array::from_fn(|_| Bitmap::new(TILE_SIZE, TILE_SIZE)); + + for (char_code, bitmap) in bitmaps.iter_mut().enumerate() { + let bits = CP437_FONT_LINEAR[char_code]; + let mut bytes = bits.to_be_bytes(); + bytes.reverse(); + bitmap.data_ref_mut().copy_from_slice(bytes.as_slice()); + } + + Self::new(bitmaps) } } @@ -281,17 +291,3 @@ pub(crate) const CP437_FONT_LINEAR: [u64; 256] = [ 0x00007c7c7c7c7c00, // 0xfe 0x0000000000000000, // 0xff ]; - -fn load_static() -> Cp437Font { - let mut bitmaps = - core::array::from_fn(|_| Bitmap::new(TILE_SIZE, TILE_SIZE)); - - for (char_code, bitmap) in bitmaps.iter_mut().enumerate() { - let bits = CP437_FONT_LINEAR[char_code]; - let mut bytes = bits.to_be_bytes(); - bytes.reverse(); - bitmap.data_ref_mut().copy_from_slice(bytes.as_slice()); - } - - Cp437Font::new(bitmaps) -} diff --git a/src/execute_command.rs b/src/execute_command.rs index a32c7b2..36d4d0a 100644 --- a/src/execute_command.rs +++ b/src/execute_command.rs @@ -1,5 +1,5 @@ use crate::execute_command::ExecutionResult::{Failure, Shutdown, Success}; -use crate::font::Cp437Font; +use crate::cp437_font::Cp437Font; use crate::font_renderer::FontRenderer8x8; use log::{debug, error, info, trace, warn}; use servicepoint::{ @@ -13,7 +13,7 @@ pub struct CommandExecutor<'t> { display: &'t RwLock, luma: &'t RwLock, cp437_font: Cp437Font, - utf8_font: FontRenderer8x8, + font_renderer: FontRenderer8x8, } #[must_use] @@ -27,11 +27,12 @@ impl<'t> CommandExecutor<'t> { pub fn new( display: &'t RwLock, luma: &'t RwLock, + font_renderer: FontRenderer8x8, ) -> Self { CommandExecutor { display, luma, - utf8_font: FontRenderer8x8::default(), + font_renderer, cp437_font: Cp437Font::default(), } } @@ -190,7 +191,7 @@ impl<'t> CommandExecutor<'t> { let tile_x = char_x + x; let tile_y = char_y + y; - if let Err(e) = self.utf8_font.render( + if let Err(e) = self.font_renderer.render( char, &mut display, Origin::new(tile_x * TILE_SIZE, tile_y * TILE_SIZE), diff --git a/src/font_renderer.rs b/src/font_renderer.rs index dbb7a91..31f3537 100644 --- a/src/font_renderer.rs +++ b/src/font_renderer.rs @@ -43,12 +43,13 @@ pub enum RenderError { } impl FontRenderer8x8 { - pub fn new(font: Font, fallback_char: Option) -> Self { + const FALLBACK_CHAR: char = '?'; + pub fn new(font: Font) -> Self { let canvas = Canvas::new(vec2i(TILE_SIZE as i32, TILE_SIZE as i32), Format::A8); assert_eq!(canvas.pixels.len(), TILE_SIZE * TILE_SIZE); assert_eq!(canvas.stride, TILE_SIZE); - let fallback_char = fallback_char.and_then(|c| font.glyph_for_char(c)); + let fallback_char = font.glyph_for_char(Self::FALLBACK_CHAR); let result = Self { font: SendFont(font), fallback_char, @@ -57,6 +58,15 @@ impl FontRenderer8x8 { result } + pub fn from_name(family_name: String) -> Self { + let font = SystemSource::new() + .select_best_match(&[FamilyName::Title(family_name)], &Properties::new()) + .unwrap() + .load() + .unwrap(); + Self::new(font) + } + pub fn render( &self, char: char, @@ -104,17 +114,10 @@ impl FontRenderer8x8 { impl Default for FontRenderer8x8 { fn default() -> Self { let utf8_font = SystemSource::new() - .select_best_match( - &[ - FamilyName::Title("Roboto Mono".to_string()), - FamilyName::Title("Fira Mono".to_string()), - FamilyName::Monospace, - ], - &Properties::new(), - ) + .select_best_match(&[FamilyName::Monospace], &Properties::new()) .unwrap() .load() .unwrap(); - FontRenderer8x8::new(utf8_font, Some('?')) + FontRenderer8x8::new(utf8_font) } } diff --git a/src/gui.rs b/src/gui.rs index 8fceba8..562f7d6 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -12,14 +12,14 @@ use winit::event_loop::ActiveEventLoop; use winit::keyboard::KeyCode::KeyC; use winit::window::{Window, WindowId}; -use crate::Cli; +use crate::cli::{GuiOptions}; pub struct Gui<'t> { display: &'t RwLock, luma: &'t RwLock, window: Option, stop_udp_tx: Sender<()>, - cli: &'t Cli, + options: GuiOptions, logical_size: LogicalSize, } @@ -41,15 +41,15 @@ impl<'t> Gui<'t> { display: &'t RwLock, luma: &'t RwLock, stop_udp_tx: Sender<()>, - cli: &'t Cli, + options: GuiOptions, ) -> Self { Gui { + window: None, + logical_size: Self::get_logical_size(options.spacers), display, luma, stop_udp_tx, - cli, - window: None, - logical_size: Self::get_logical_size(cli.spacers), + options, } } @@ -79,7 +79,7 @@ impl<'t> Gui<'t> { let brightness_scale = (u8::MAX as f32) / (u8::from(Brightness::MAX) as f32); for tile_y in 0..TILE_HEIGHT { - if self.cli.spacers && tile_y != 0 { + if self.options.spacers && tile_y != 0 { // cannot just frame.skip(PIXEL_WIDTH as usize * SPACER_HEIGHT as usize) because of typing for _ in 0..PIXEL_WIDTH * SPACER_HEIGHT { frame.next().unwrap(); @@ -105,9 +105,9 @@ impl<'t> Gui<'t> { fn get_on_color(&self, brightness: u8) -> [u8; 4] { [ - if self.cli.red { brightness } else { 0u8 }, - if self.cli.green { brightness } else { 0u8 }, - if self.cli.blue { brightness } else { 0u8 }, + if self.options.red { brightness } else { 0u8 }, + if self.options.green { brightness } else { 0u8 }, + if self.options.blue { brightness } else { 0u8 }, 255, ] } diff --git a/src/main.rs b/src/main.rs index 433a4c9..e6daaa5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,89 +11,37 @@ use std::io::ErrorKind; use std::net::UdpSocket; use std::sync::{mpsc, RwLock}; use std::time::Duration; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::{ControlFlow, EventLoop, EventLoopProxy}; +use cli::Cli; +use crate::font_renderer::FontRenderer8x8; mod execute_command; -mod font; +mod cp437_font; mod font_renderer; mod gui; - -#[derive(Parser, Debug)] -struct Cli { - #[arg( - long, - default_value = "0.0.0.0:2342", - help = "address and port to bind to" - )] - bind: String, - #[arg( - short, - long, - default_value_t = false, - help = "add spacers between tile rows to simulate gaps in real display" - )] - spacers: bool, - #[arg( - short, - long, - help = "Set default log level lower. You can also change this via the RUST_LOG environment variable." - )] - debug: bool, - #[arg( - short, - long, - default_value_t = false, - help = "Use the red color channel" - )] - red: bool, - #[arg( - short, - long, - default_value_t = false, - help = "Use the green color channel" - )] - green: bool, - #[arg( - short, - long, - default_value_t = false, - help = "Use the blue color channel" - )] - blue: bool, -} +mod cli; const BUF_SIZE: usize = 8985; fn main() { let mut cli = Cli::parse(); - - env_logger::builder() - .filter_level(if cli.debug { - LevelFilter::Debug - } else { - LevelFilter::Info - }) - .parse_default_env() - .init(); - - if !(cli.red || cli.blue || cli.green) { - cli.green = true; + if !(cli.gui.red || cli.gui.blue || cli.gui.green) { + cli.gui.green = true; } + init_logging(cli.debug); info!("starting with args: {:?}", &cli); + let socket = UdpSocket::bind(&cli.bind).expect("could not bind socket"); socket .set_nonblocking(true) .expect("could not enter non blocking mode"); let display = RwLock::new(Bitmap::new(PIXEL_WIDTH, PIXEL_HEIGHT)); - - let mut luma = BrightnessGrid::new(TILE_WIDTH, TILE_HEIGHT); - luma.fill(Brightness::MAX); - let luma = RwLock::new(luma); + let luma = RwLock::new(BrightnessGrid::new(TILE_WIDTH, TILE_HEIGHT)); let (stop_udp_tx, stop_udp_rx) = mpsc::channel(); - let mut app = Gui::new(&display, &luma, stop_udp_tx, &cli); + let mut gui = Gui::new(&display, &luma, stop_udp_tx, cli.gui); let event_loop = EventLoop::with_user_event() .build() @@ -101,7 +49,9 @@ fn main() { event_loop.set_control_flow(ControlFlow::Wait); let event_proxy = event_loop.create_proxy(); - let command_executor = CommandExecutor::new(&display, &luma); + let font_renderer = cli.font.map(move |font| FontRenderer8x8::from_name(font)) + .unwrap_or_else(move || FontRenderer8x8::default()); + let command_executor = CommandExecutor::new(&display, &luma, font_renderer); std::thread::scope(move |scope| { scope.spawn(move || { @@ -117,29 +67,45 @@ fn main() { None => continue, }; - match command_executor.execute(command) { - ExecutionResult::Success => { - event_proxy - .send_event(AppEvents::UdpPacketHandled) - .expect("could not send packet handled event"); - } - ExecutionResult::Failure => { - error!("failed to execute command"); - } - ExecutionResult::Shutdown => { - event_proxy - .send_event(AppEvents::UdpThreadClosed) - .expect("could not send close event"); - } - } + handle_command(&event_proxy, &command_executor, command); } }); event_loop - .run_app(&mut app) + .run_app(&mut gui) .expect("could not run event loop"); }); } +fn handle_command(event_proxy: &EventLoopProxy, command_executor: &CommandExecutor, command: Command) { + match command_executor.execute(command) { + ExecutionResult::Success => { + event_proxy + .send_event(AppEvents::UdpPacketHandled) + .expect("could not send packet handled event"); + } + ExecutionResult::Failure => { + error!("failed to execute command"); + } + ExecutionResult::Shutdown => { + event_proxy + .send_event(AppEvents::UdpThreadClosed) + .expect("could not send close event"); + } + } +} + +fn init_logging(debug: bool) { + let filter = if debug { + LevelFilter::Debug + } else { + LevelFilter::Info + }; + env_logger::builder() + .filter_level(filter) + .parse_default_env() + .init(); +} + fn command_from_slice(slice: &[u8]) -> Option { let package = match servicepoint::Packet::try_from(slice) { Err(_) => { From 3b3e8f4c1d9729d75382907069722f7c18beec69 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 26 Jan 2025 12:51:42 +0100 Subject: [PATCH 04/11] minimal tweaks --- src/cp437_font.rs | 13 +++++++++---- src/execute_command.rs | 3 +-- src/font_renderer.rs | 10 +++------- src/main.rs | 36 +++++++++--------------------------- 4 files changed, 22 insertions(+), 40 deletions(-) diff --git a/src/cp437_font.rs b/src/cp437_font.rs index 1ff34b9..dbb99d7 100644 --- a/src/cp437_font.rs +++ b/src/cp437_font.rs @@ -1,3 +1,4 @@ +use std::ops::Index; use servicepoint::{Bitmap, DataRef, TILE_SIZE}; const CHAR_COUNT: usize = u8::MAX as usize + 1; @@ -10,10 +11,6 @@ impl Cp437Font { pub fn new(bitmaps: [Bitmap; CHAR_COUNT]) -> Self { Self { bitmaps } } - - pub fn get_bitmap(&self, char_code: u8) -> &Bitmap { - &self.bitmaps[char_code as usize] - } } impl Default for Cp437Font { @@ -32,6 +29,14 @@ impl Default for Cp437Font { } } +impl Index for Cp437Font { + type Output = Bitmap; + + fn index(&self, char_code: u8) -> &Self::Output { + &self.bitmaps[char_code as usize] + } +} + /// Font from the display firmware `cape-cccb-apd/cp437font_linear.h` pub(crate) const CP437_FONT_LINEAR: [u64; 256] = [ 0x0000000000000000, // 0x00 diff --git a/src/execute_command.rs b/src/execute_command.rs index 36d4d0a..7a2bf45 100644 --- a/src/execute_command.rs +++ b/src/execute_command.rs @@ -154,11 +154,10 @@ impl<'t> CommandExecutor<'t> { let tile_x = char_x + x; let tile_y = char_y + y; - let bitmap = font.get_bitmap(char_code); match self.print_pixel_grid( tile_x * TILE_SIZE, tile_y * TILE_SIZE, - bitmap, + &font[char_code], ) { Success => {} Failure => { diff --git a/src/font_renderer.rs b/src/font_renderer.rs index 31f3537..64e966e 100644 --- a/src/font_renderer.rs +++ b/src/font_renderer.rs @@ -78,11 +78,8 @@ impl FontRenderer8x8 { .font .as_ref() .glyph_for_char(char) - .or(self.fallback_char); - let glyph_id = match glyph_id { - None => return Err(GlyphNotFound(char)), - Some(val) => val, - }; + .or(self.fallback_char) + .ok_or_else(|| GlyphNotFound(char))?; canvas.pixels.fill(0); self.font.as_ref().rasterize_glyph( @@ -97,8 +94,7 @@ impl FontRenderer8x8 { for y in 0..TILE_SIZE { for x in 0..TILE_SIZE { - let index = x + y * TILE_SIZE; - let canvas_val = canvas.pixels[index] != 0; + let canvas_val = canvas.pixels[x + y * TILE_SIZE] != 0; let bitmap_x = (offset.x + x) as isize; let bitmap_y = (offset.y + y) as isize; if !bitmap.set_optional(bitmap_x, bitmap_y, canvas_val) { diff --git a/src/main.rs b/src/main.rs index e6daaa5..7832f9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,17 +57,9 @@ fn main() { scope.spawn(move || { let mut buf = [0; BUF_SIZE]; while stop_udp_rx.try_recv().is_err() { - let amount = match receive_into_buf(&socket, &mut buf) { - Some(value) => value, - None => continue, - }; - - let command = match command_from_slice(&buf[..amount]) { - Some(value) => value, - None => continue, - }; - - handle_command(&event_proxy, &command_executor, command); + receive_into_buf(&socket, &mut buf) + .and_then(move |amount| command_from_slice(&buf[..amount])) + .map(|cmd| handle_command(&event_proxy, &command_executor, cmd)); } }); event_loop @@ -107,22 +99,12 @@ fn init_logging(debug: bool) { } fn command_from_slice(slice: &[u8]) -> Option { - let package = match servicepoint::Packet::try_from(slice) { - Err(_) => { - warn!("could not load packet with length {}", slice.len()); - return None; - } - Ok(package) => package, - }; - - let command = match Command::try_from(package) { - Err(err) => { - warn!("could not read command for packet: {:?}", err); - return None; - } - Ok(val) => val, - }; - Some(command) + let packet = servicepoint::Packet::try_from(slice) + .inspect_err(|_| warn!("could not load packet with length {}", slice.len())) + .ok()?; + Command::try_from(packet) + .inspect_err(move |err| warn!("could not read command for packet: {:?}", err)) + .ok() } fn receive_into_buf( From 9d8e2f28c469daa3b5a09a79210ea90378aab014 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 26 Jan 2025 13:16:19 +0100 Subject: [PATCH 05/11] cargo fmt --- src/cli.rs | 2 +- src/cp437_font.rs | 2 +- src/execute_command.rs | 2 +- src/font_renderer.rs | 5 ++++- src/gui.rs | 14 ++++++++++---- src/main.rs | 30 +++++++++++++++++++++--------- 6 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 0925980..0e82f74 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -54,4 +54,4 @@ pub struct GuiOptions { help = "add spacers between tile rows to simulate gaps in real display" )] pub spacers: bool, -} \ No newline at end of file +} diff --git a/src/cp437_font.rs b/src/cp437_font.rs index dbb99d7..d259c98 100644 --- a/src/cp437_font.rs +++ b/src/cp437_font.rs @@ -1,5 +1,5 @@ -use std::ops::Index; use servicepoint::{Bitmap, DataRef, TILE_SIZE}; +use std::ops::Index; const CHAR_COUNT: usize = u8::MAX as usize + 1; diff --git a/src/execute_command.rs b/src/execute_command.rs index 7a2bf45..4b74fcf 100644 --- a/src/execute_command.rs +++ b/src/execute_command.rs @@ -1,5 +1,5 @@ -use crate::execute_command::ExecutionResult::{Failure, Shutdown, Success}; use crate::cp437_font::Cp437Font; +use crate::execute_command::ExecutionResult::{Failure, Shutdown, Success}; use crate::font_renderer::FontRenderer8x8; use log::{debug, error, info, trace, warn}; use servicepoint::{ diff --git a/src/font_renderer.rs b/src/font_renderer.rs index 64e966e..7ec20bb 100644 --- a/src/font_renderer.rs +++ b/src/font_renderer.rs @@ -60,7 +60,10 @@ impl FontRenderer8x8 { pub fn from_name(family_name: String) -> Self { let font = SystemSource::new() - .select_best_match(&[FamilyName::Title(family_name)], &Properties::new()) + .select_best_match( + &[FamilyName::Title(family_name)], + &Properties::new(), + ) .unwrap() .load() .unwrap(); diff --git a/src/gui.rs b/src/gui.rs index 562f7d6..424d128 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -12,7 +12,7 @@ use winit::event_loop::ActiveEventLoop; use winit::keyboard::KeyCode::KeyC; use winit::window::{Window, WindowId}; -use crate::cli::{GuiOptions}; +use crate::cli::GuiOptions; pub struct Gui<'t> { display: &'t RwLock, @@ -76,7 +76,8 @@ impl<'t> Gui<'t> { fn draw_frame(&self, frame: &mut ChunksExactMut) { let display = self.display.read().unwrap(); let luma = self.luma.read().unwrap(); - let brightness_scale = (u8::MAX as f32) / (u8::from(Brightness::MAX) as f32); + let brightness_scale = + (u8::MAX as f32) / (u8::from(Brightness::MAX) as f32); for tile_y in 0..TILE_HEIGHT { if self.options.spacers && tile_y != 0 { @@ -90,11 +91,16 @@ impl<'t> Gui<'t> { for y in start_y..start_y + TILE_SIZE { for tile_x in 0..TILE_WIDTH { let brightness = u8::from(luma.get(tile_x, tile_y)); - let brightness = (brightness_scale * brightness as f32) as u8; + let brightness = + (brightness_scale * brightness as f32) as u8; let on_color = self.get_on_color(brightness); let start_x = tile_x * TILE_SIZE; for x in start_x..start_x + TILE_SIZE { - let color = if display.get(x, y) { on_color } else { OFF_COLOR }; + let color = if display.get(x, y) { + on_color + } else { + OFF_COLOR + }; let pixel = frame.next().unwrap(); pixel.copy_from_slice(&color); } diff --git a/src/main.rs b/src/main.rs index 7832f9e..4ea5b7b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ #![deny(clippy::all)] +use crate::font_renderer::FontRenderer8x8; use crate::{ execute_command::{CommandExecutor, ExecutionResult}, gui::{AppEvents, Gui}, }; use clap::Parser; +use cli::Cli; use log::{error, info, warn, LevelFilter}; use servicepoint::*; use std::io::ErrorKind; @@ -12,14 +14,12 @@ use std::net::UdpSocket; use std::sync::{mpsc, RwLock}; use std::time::Duration; use winit::event_loop::{ControlFlow, EventLoop, EventLoopProxy}; -use cli::Cli; -use crate::font_renderer::FontRenderer8x8; -mod execute_command; +mod cli; mod cp437_font; +mod execute_command; mod font_renderer; mod gui; -mod cli; const BUF_SIZE: usize = 8985; @@ -49,7 +49,9 @@ fn main() { event_loop.set_control_flow(ControlFlow::Wait); let event_proxy = event_loop.create_proxy(); - let font_renderer = cli.font.map(move |font| FontRenderer8x8::from_name(font)) + let font_renderer = cli + .font + .map(move |font| FontRenderer8x8::from_name(font)) .unwrap_or_else(move || FontRenderer8x8::default()); let command_executor = CommandExecutor::new(&display, &luma, font_renderer); @@ -59,7 +61,9 @@ fn main() { while stop_udp_rx.try_recv().is_err() { receive_into_buf(&socket, &mut buf) .and_then(move |amount| command_from_slice(&buf[..amount])) - .map(|cmd| handle_command(&event_proxy, &command_executor, cmd)); + .map(|cmd| { + handle_command(&event_proxy, &command_executor, cmd) + }); } }); event_loop @@ -68,7 +72,11 @@ fn main() { }); } -fn handle_command(event_proxy: &EventLoopProxy, command_executor: &CommandExecutor, command: Command) { +fn handle_command( + event_proxy: &EventLoopProxy, + command_executor: &CommandExecutor, + command: Command, +) { match command_executor.execute(command) { ExecutionResult::Success => { event_proxy @@ -100,10 +108,14 @@ fn init_logging(debug: bool) { fn command_from_slice(slice: &[u8]) -> Option { let packet = servicepoint::Packet::try_from(slice) - .inspect_err(|_| warn!("could not load packet with length {}", slice.len())) + .inspect_err(|_| { + warn!("could not load packet with length {}", slice.len()) + }) .ok()?; Command::try_from(packet) - .inspect_err(move |err| warn!("could not read command for packet: {:?}", err)) + .inspect_err(move |err| { + warn!("could not read command for packet: {:?}", err) + }) .ok() } From 5a86fcc1b2b9f3c05b5e23dc0af7a87523c7f8c1 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 26 Jan 2025 13:27:18 +0100 Subject: [PATCH 06/11] split render function --- src/font_renderer.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/font_renderer.rs b/src/font_renderer.rs index 7ec20bb..e277c3d 100644 --- a/src/font_renderer.rs +++ b/src/font_renderer.rs @@ -13,7 +13,7 @@ use pathfinder_geometry::{ vector::{vec2f, vec2i}, }; use servicepoint::{Bitmap, Grid, Origin, Pixels, TILE_SIZE}; -use std::sync::Mutex; +use std::sync::{Mutex, MutexGuard}; struct SendFont(Font); @@ -76,14 +76,9 @@ impl FontRenderer8x8 { bitmap: &mut Bitmap, offset: Origin, ) -> Result<(), RenderError> { - let mut canvas = self.canvas.lock().unwrap(); - let glyph_id = self - .font - .as_ref() - .glyph_for_char(char) - .or(self.fallback_char) - .ok_or_else(|| GlyphNotFound(char))?; + let glyph_id = self.get_glyph(char)?; + let mut canvas = self.canvas.lock().unwrap(); canvas.pixels.fill(0); self.font.as_ref().rasterize_glyph( &mut canvas, @@ -95,6 +90,14 @@ impl FontRenderer8x8 { RasterizationOptions::Bilevel, )?; + Self::copy_to_bitmap(canvas, bitmap, offset) + } + + fn copy_to_bitmap( + canvas: MutexGuard, + bitmap: &mut Bitmap, + offset: Origin, + ) -> Result<(), RenderError> { for y in 0..TILE_SIZE { for x in 0..TILE_SIZE { let canvas_val = canvas.pixels[x + y * TILE_SIZE] != 0; @@ -105,9 +108,16 @@ impl FontRenderer8x8 { } } } - Ok(()) } + + fn get_glyph(&self, char: char) -> Result { + self.font + .as_ref() + .glyph_for_char(char) + .or(self.fallback_char) + .ok_or_else(|| GlyphNotFound(char)) + } } impl Default for FontRenderer8x8 { From a00c8c237908e001e156c0dd38812ab7aecad79a Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 26 Jan 2025 13:28:31 +0100 Subject: [PATCH 07/11] nix flake update --- flake.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flake.lock b/flake.lock index dfb3db6..cdd30be 100644 --- a/flake.lock +++ b/flake.lock @@ -37,11 +37,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1736200483, - "narHash": "sha256-JO+lFN2HsCwSLMUWXHeOad6QUxOuwe9UOAF/iSl1J4I=", + "lastModified": 1737672001, + "narHash": "sha256-YnHJJ19wqmibLQdUeq9xzE6CjrMA568KN/lFPuSVs4I=", "owner": "nixos", "repo": "nixpkgs", - "rev": "3f0a8ac25fb674611b98089ca3a5dd6480175751", + "rev": "035f8c0853c2977b24ffc4d0a42c74f00b182cd8", "type": "github" }, "original": { From 05d9ea1042ea6c62d3ccf8dabc18a356a4953ea4 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 26 Jan 2025 13:30:16 +0100 Subject: [PATCH 08/11] fix build --- .github/workflows/rust.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 7c4cffc..986f0e0 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -16,7 +16,9 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Install liblzma-dev - run: sudo apt-get install -y liblzma-dev + + - name: Install system dependencies + run: sudo apt-get install -y liblzma-dev libfontconfig1-dev + - name: Build run: cargo build --verbose From c74ae70bd8bd9e2f9575729e30de773596d4aaa1 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 26 Jan 2025 14:29:44 +0100 Subject: [PATCH 09/11] split flake into two files --- flake.nix | 137 +++++++++---------------------------- servicepoint-simulator.nix | 74 ++++++++++++++++++++ 2 files changed, 108 insertions(+), 103 deletions(-) create mode 100644 servicepoint-simulator.nix diff --git a/flake.nix b/flake.nix index 6254f4e..96cbeaa 100644 --- a/flake.nix +++ b/flake.nix @@ -25,99 +25,24 @@ "aarch64-darwin" "x86_64-darwin" ]; - forAllSystems = lib.genAttrs supported-systems; - make-rust-toolchain-core = - pkgs: - pkgs.symlinkJoin { - name = "rust-toolchain-core"; - paths = with pkgs; [ - rustc - cargo - rustPlatform.rustcSrc - ]; - }; + forAllSystems = + f: + lib.genAttrs supported-systems ( + system: + f rec { + pkgs = nixpkgs.legacyPackages.${system}; + inherit system; + } + ); in rec { packages = forAllSystems ( - system: - let - pkgs = nixpkgs.legacyPackages."${system}"; - rust-toolchain-core = make-rust-toolchain-core pkgs; - naersk' = pkgs.callPackage naersk { - cargo = rust-toolchain-core; - rustc = rust-toolchain-core; - }; - in + { pkgs, ... }: rec { - servicepoint-simulator = naersk'.buildPackage rec { - src = nix-filter.lib.filter { - root = ./.; - include = [ - ./Cargo.toml - ./Cargo.lock - ./src - ./Web437_IBM_BIOS.woff - ./README.md - ./LICENSE - ]; - }; - nativeBuildInputs = with pkgs; [ - pkg-config - makeWrapper - ]; - strictDeps = true; - buildInputs = - with pkgs; - [ - xe - xz - - roboto - ] - ++ lib.optionals pkgs.stdenv.isLinux ( - with pkgs; - [ - # gpu - libGL - vulkan-headers - vulkan-loader - vulkan-tools vulkan-tools-lunarg - vulkan-extension-layer - vulkan-validation-layers - - # keyboard - libxkbcommon - - # font loading - fontconfig - freetype - - # WINIT_UNIX_BACKEND=wayland - wayland - - # WINIT_UNIX_BACKEND=x11 - xorg.libXcursor - xorg.libXrandr - xorg.libXi - xorg.libX11 - xorg.libX11.dev - ] - ) - ++ lib.optionals pkgs.stdenv.isDarwin ( - with pkgs.darwin.apple_sdk.frameworks; - [ - Carbon - QuartzCore - AppKit - ] - ); - - postInstall = '' - wrapProgram $out/bin/servicepoint-simulator \ - --suffix LD_LIBRARY_PATH : ${lib.makeLibraryPath buildInputs} - ''; + servicepoint-simulator = import ./servicepoint-simulator.nix { + inherit nix-filter pkgs; + naersk' = pkgs.callPackage naersk { }; }; - default = servicepoint-simulator; } ); @@ -125,29 +50,35 @@ legacyPackages = packages; devShells = forAllSystems ( - system: - let - pkgs = nixpkgs.legacyPackages."${system}"; - rust-toolchain = pkgs.symlinkJoin { - name = "rust-toolchain"; - paths = with pkgs; [ - (make-rust-toolchain-core pkgs) - rustfmt - clippy - cargo-expand - ]; - }; - in + { + pkgs, + system, + }: { default = pkgs.mkShell rec { inputsFrom = [ self.packages.${system}.default ]; - packages = [ rust-toolchain pkgs.gdb ]; + packages = [ + pkgs.gdb + (pkgs.symlinkJoin { + name = "rust-toolchain"; + paths = with pkgs; [ + rustc + cargo + rustPlatform.rustcSrc + rustfmt + clippy + cargo-expand + ]; + }) + ]; LD_LIBRARY_PATH = "${pkgs.lib.makeLibraryPath (builtins.concatMap (d: d.buildInputs) inputsFrom)}"; + NIX_LD_LIBRARY_PATH = LD_LIBRARY_PATH; + NIX_LD = pkgs.stdenv.cc.bintools.dynamicLinker; RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; }; } ); - formatter = forAllSystems (system: nixpkgs.legacyPackages."${system}".nixfmt-rfc-style); + formatter = forAllSystems ({ pkgs, ... }: pkgs.nixfmt-rfc-style); }; } diff --git a/servicepoint-simulator.nix b/servicepoint-simulator.nix new file mode 100644 index 0000000..efb8fd4 --- /dev/null +++ b/servicepoint-simulator.nix @@ -0,0 +1,74 @@ +{ + naersk', + pkgs, + nix-filter, +}: +naersk'.buildPackage rec { + src = nix-filter.lib.filter { + root = ./.; + include = [ + ./Cargo.toml + ./Cargo.lock + ./src + ./Web437_IBM_BIOS.woff + ./README.md + ./LICENSE + ]; + }; + nativeBuildInputs = with pkgs; [ + pkg-config + makeWrapper + ]; + strictDeps = true; + buildInputs = + with pkgs; + [ + xe + xz + + roboto + ] + ++ lib.optionals pkgs.stdenv.isLinux ( + with pkgs; + [ + # gpu + libGL + vulkan-headers + vulkan-loader + vulkan-tools + vulkan-tools-lunarg + vulkan-extension-layer + vulkan-validation-layers + + # keyboard + libxkbcommon + + # font loading + fontconfig + freetype + + # WINIT_UNIX_BACKEND=wayland + wayland + + # WINIT_UNIX_BACKEND=x11 + xorg.libXcursor + xorg.libXrandr + xorg.libXi + xorg.libX11 + xorg.libX11.dev + ] + ) + ++ lib.optionals pkgs.stdenv.isDarwin ( + with pkgs.darwin.apple_sdk.frameworks; + [ + Carbon + QuartzCore + AppKit + ] + ); + + postInstall = '' + wrapProgram $out/bin/servicepoint-simulator \ + --suffix LD_LIBRARY_PATH : ${pkgs.lib.makeLibraryPath buildInputs} + ''; +} From 85b3954c35948a0e07b3423fef0b8cd3478a3da2 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 26 Jan 2025 14:45:46 +0100 Subject: [PATCH 10/11] Revert "nix flake update" because wayland lib cannot be found using newer nixpkgs This reverts commit a00c8c237908e001e156c0dd38812ab7aecad79a. --- flake.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flake.lock b/flake.lock index cdd30be..dfb3db6 100644 --- a/flake.lock +++ b/flake.lock @@ -37,11 +37,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1737672001, - "narHash": "sha256-YnHJJ19wqmibLQdUeq9xzE6CjrMA568KN/lFPuSVs4I=", + "lastModified": 1736200483, + "narHash": "sha256-JO+lFN2HsCwSLMUWXHeOad6QUxOuwe9UOAF/iSl1J4I=", "owner": "nixos", "repo": "nixpkgs", - "rev": "035f8c0853c2977b24ffc4d0a42c74f00b182cd8", + "rev": "3f0a8ac25fb674611b98089ca3a5dd6480175751", "type": "github" }, "original": { From 8590b38d0f1e06390f86e1a8a6f5097a0d042a10 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 26 Jan 2025 14:53:53 +0100 Subject: [PATCH 11/11] reorder CLI args, update README --- README.md | 14 +++++++++++++- src/cli.rs | 26 +++++++++++++------------- src/main.rs | 2 +- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ce48083..c6c8116 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,19 @@ Without nix: check out this repository and use `cargo run --release`. ## Command line arguments -The application binds to `0.0.0.0:2342` by default (`./servicepoint-simulator --bind host:port` to change this). +``` +Usage: servicepoint-simulator [OPTIONS] + +Options: + --bind address and port to bind to [default: 0.0.0.0:2342] + -f, --font The name of the font family to use. This defaults to the system monospace font. + -s, --spacers add spacers between tile rows to simulate gaps in real display + -r, --red Use the red color channel + -g, --green Use the green color channel + -b, --blue Use the blue color channel + -v, --verbose Set default log level lower. You can also change this via the RUST_LOG environment variable. + -h, --help Print help +``` See [env_logger](https://docs.rs/env_logger/latest/env_logger/) to configure logging. diff --git a/src/cli.rs b/src/cli.rs index 0e82f74..5d7adf0 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -8,12 +8,6 @@ pub struct Cli { help = "address and port to bind to" )] pub bind: String, - #[arg( - short, - long, - help = "Set default log level lower. You can also change this via the RUST_LOG environment variable." - )] - pub debug: bool, #[arg( short, long, @@ -22,10 +16,23 @@ pub struct Cli { pub font: Option, #[clap(flatten)] pub gui: GuiOptions, + #[arg( + short, + long, + help = "Set default log level lower. You can also change this via the RUST_LOG environment variable." + )] + pub verbose: bool, } #[derive(Parser, Debug)] pub struct GuiOptions { + #[arg( + short, + long, + default_value_t = false, + help = "add spacers between tile rows to simulate gaps in real display" + )] + pub spacers: bool, #[arg( short, long, @@ -47,11 +54,4 @@ pub struct GuiOptions { help = "Use the blue color channel" )] pub blue: bool, - #[arg( - short, - long, - default_value_t = false, - help = "add spacers between tile rows to simulate gaps in real display" - )] - pub spacers: bool, } diff --git a/src/main.rs b/src/main.rs index 4ea5b7b..fa0b127 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ fn main() { cli.gui.green = true; } - init_logging(cli.debug); + init_logging(cli.verbose); info!("starting with args: {:?}", &cli); let socket = UdpSocket::bind(&cli.bind).expect("could not bind socket");