servicepoint/servicepoint2/src/command.rs

449 lines
17 KiB
Rust
Raw Normal View History

use crate::{BitVec, ByteGrid, CompressionCode, Header, Packet, PixelGrid, TILE_SIZE};
use crate::command_code::CommandCode;
2024-05-12 17:15:30 +02:00
use crate::compression::{into_compressed, into_decompressed};
2024-05-10 00:53:12 +02:00
2024-05-12 01:30:55 +02:00
/// An origin marks the top left position of a window sent to the display.
2024-05-10 12:24:07 +02:00
#[derive(Debug, Clone, Copy)]
2024-05-10 00:53:12 +02:00
pub struct Origin(pub u16, pub u16);
impl Origin {
pub fn top_left() -> Self {
Self(0, 0)
}
}
2024-05-10 00:53:12 +02:00
/// Size defines the width and height of a window
2024-05-10 12:24:07 +02:00
#[derive(Debug, Clone, Copy)]
2024-05-10 00:53:12 +02:00
pub struct Size(pub u16, pub u16);
/// Type alias for documenting the meaning of the u16 in enum values
2024-05-12 17:15:30 +02:00
pub type Offset = u16;
2024-05-10 00:53:12 +02:00
/// Type alias for documenting the meaning of the u16 in enum values
2024-05-12 17:15:30 +02:00
pub type Brightness = u8;
2024-05-10 00:53:12 +02:00
2024-05-12 13:11:42 +02:00
/// A command to send to the display.
2024-05-12 17:15:30 +02:00
#[derive(Debug, Clone)]
2024-05-10 00:53:12 +02:00
pub enum Command {
2024-05-12 01:30:55 +02:00
/// Set all pixels to the off state
2024-05-10 00:53:12 +02:00
Clear,
2024-05-12 01:30:55 +02:00
/// Kills the udp daemon, usually results in a reboot of the display.
2024-05-10 00:53:12 +02:00
HardReset,
/// Slowly decrease brightness until off? Untested.
2024-05-10 00:53:12 +02:00
FadeOut,
2024-05-12 01:30:55 +02:00
/// Set the brightness of tiles
2024-05-11 12:43:17 +02:00
CharBrightness(Origin, ByteGrid),
2024-05-12 01:30:55 +02:00
/// Set the brightness of all tiles
2024-05-10 00:53:12 +02:00
Brightness(Brightness),
2024-05-10 21:27:34 +02:00
#[deprecated]
/// Legacy command code, gets ignored by the real display.
2024-05-10 21:27:34 +02:00
BitmapLegacy,
2024-05-12 01:30:55 +02:00
/// Set pixel data starting at the offset.
/// The contained BitVec is always uncompressed.
2024-05-11 21:14:20 +02:00
BitmapLinear(Offset, BitVec, CompressionCode),
2024-05-12 01:30:55 +02:00
/// Set pixel data according to an and-mask starting at the offset.
/// The contained BitVec is always uncompressed.
2024-05-11 21:14:20 +02:00
BitmapLinearAnd(Offset, BitVec, CompressionCode),
2024-05-12 01:30:55 +02:00
/// Set pixel data according to an or-mask starting at the offset.
/// The contained BitVec is always uncompressed.
2024-05-11 21:14:20 +02:00
BitmapLinearOr(Offset, BitVec, CompressionCode),
2024-05-12 01:30:55 +02:00
/// Set pixel data according to an xor-mask starting at the offset.
/// The contained BitVec is always uncompressed.
2024-05-11 21:14:20 +02:00
BitmapLinearXor(Offset, BitVec, CompressionCode),
2024-05-12 01:30:55 +02:00
/// Show text on the screen. Note that the byte data has to be CP437 encoded.
2024-05-11 12:43:17 +02:00
Cp437Data(Origin, ByteGrid),
2024-05-12 01:30:55 +02:00
/// Sets a window of pixels to the specified values
BitmapLinearWin(Origin, PixelGrid, CompressionCode),
2024-05-10 00:53:12 +02:00
}
impl From<Command> for Packet {
/// Move the `Command` into a `Packet` instance for sending.
fn from(value: Command) -> Self {
match value {
2024-05-10 19:55:18 +02:00
Command::Clear => command_code_only(CommandCode::Clear),
Command::FadeOut => command_code_only(CommandCode::FadeOut),
Command::HardReset => command_code_only(CommandCode::HardReset),
2024-05-10 21:27:34 +02:00
#[allow(deprecated)]
2024-05-11 23:28:08 +02:00
Command::BitmapLegacy => {
command_code_only(CommandCode::BitmapLegacy)
2024-05-10 18:33:51 +02:00
}
Command::CharBrightness(Origin(x, y), grid) => Packet(
Header(CommandCode::CharBrightness.into(),
x, y, grid.width as u16, grid.height as u16),
2024-05-11 23:28:08 +02:00
grid.into(),
),
Command::Brightness(brightness) => Packet(
Header(
2024-05-12 13:11:42 +02:00
CommandCode::Brightness.into(),
2024-05-11 23:28:08 +02:00
0x00000,
0x0000,
0x0000,
0x0000,
),
vec![brightness],
),
Command::BitmapLinearWin(Origin(pixel_x, pixel_y), pixels, compression) => {
2024-05-10 12:24:07 +02:00
debug_assert_eq!(pixel_x % 8, 0);
debug_assert_eq!(pixels.width % 8, 0);
let tile_x = pixel_x / TILE_SIZE;
let tile_w = pixels.width as u16 / TILE_SIZE;
let pixel_h = pixels.height as u16;
let payload = into_compressed(compression, pixels.into());
let command = match compression {
CompressionCode::Uncompressed => CommandCode::BitmapLinearWinUncompressed,
CompressionCode::Zlib => CommandCode::BitmapLinearWinZlib,
CompressionCode::Bzip2 => CommandCode::BitmapLinearWinBzip2,
CompressionCode::Lzma => CommandCode::BitmapLinearWinLzma,
CompressionCode::Zstd => CommandCode::BitmapLinearWinZstd,
};
2024-05-10 19:55:18 +02:00
Packet(
Header(command.into(), tile_x, pixel_y, tile_w, pixel_h),
payload,
2024-05-11 23:28:08 +02:00
)
2024-05-10 18:33:51 +02:00
}
2024-05-11 21:14:20 +02:00
Command::BitmapLinear(offset, bits, compression) => {
2024-05-11 23:28:08 +02:00
bitmap_linear_into_packet(
CommandCode::BitmapLinear,
offset,
compression,
bits.into(),
)
2024-05-10 18:33:51 +02:00
}
2024-05-11 21:14:20 +02:00
Command::BitmapLinearAnd(offset, bits, compression) => {
2024-05-11 23:28:08 +02:00
bitmap_linear_into_packet(
CommandCode::BitmapLinearAnd,
offset,
compression,
bits.into(),
)
2024-05-10 18:33:51 +02:00
}
2024-05-11 21:14:20 +02:00
Command::BitmapLinearOr(offset, bits, compression) => {
2024-05-11 23:28:08 +02:00
bitmap_linear_into_packet(
CommandCode::BitmapLinearOr,
offset,
compression,
bits.into(),
)
2024-05-11 21:14:20 +02:00
}
Command::BitmapLinearXor(offset, bits, compression) => {
2024-05-11 23:28:08 +02:00
bitmap_linear_into_packet(
CommandCode::BitmapLinearXor,
offset,
compression,
bits.into(),
)
2024-05-10 12:24:07 +02:00
}
Command::Cp437Data(Origin(x, y), grid) => Packet(
Header(CommandCode::Cp437Data.into(), x, y, grid.width as u16, grid.height as u16),
2024-05-11 23:28:08 +02:00
grid.into(),
),
2024-05-10 00:53:12 +02:00
}
}
}
2024-05-10 19:55:18 +02:00
#[derive(Debug)]
/// Err values for `Command::try_from`.
2024-05-10 19:55:18 +02:00
pub enum TryFromPacketError {
2024-05-12 01:30:55 +02:00
/// the contained command code does not correspond to a known command
2024-05-10 19:55:18 +02:00
InvalidCommand(u16),
2024-05-12 01:30:55 +02:00
/// the expected payload size was n, but size m was found
2024-05-10 21:45:33 +02:00
UnexpectedPayloadSize(usize, usize),
2024-05-12 01:30:55 +02:00
/// Header fields not needed for the command have been used.
///
/// Note that these commands would usually still work on the actual display.
2024-05-10 19:55:18 +02:00
ExtraneousHeaderValues,
2024-05-12 01:30:55 +02:00
/// The contained compression code is not known. This could be of disabled features.
2024-05-11 21:14:20 +02:00
InvalidCompressionCode(u16),
2024-05-12 01:30:55 +02:00
/// Decompression of the payload failed. This can be caused by corrupted packets.
2024-05-11 22:21:27 +02:00
DecompressionFailed,
2024-05-10 21:45:33 +02:00
}
2024-05-10 19:55:18 +02:00
impl TryFrom<Packet> for Command {
type Error = TryFromPacketError;
/// Try to interpret the `Packet` as one containing a `Command`
fn try_from(packet: Packet) -> Result<Self, Self::Error> {
let Packet(Header(command_u16, a, b, c, d), _) = packet;
2024-05-12 13:11:42 +02:00
let command_code = match CommandCode::try_from(command_u16) {
Err(_) => {
2024-05-12 01:30:55 +02:00
return Err(TryFromPacketError::InvalidCommand(command_u16));
2024-05-11 23:28:08 +02:00
}
2024-05-12 13:11:42 +02:00
Ok(value) => value,
2024-05-10 19:55:18 +02:00
};
match command_code {
CommandCode::Clear => match check_command_only(packet) {
2024-05-11 23:28:08 +02:00
Some(err) => Err(err),
None => Ok(Command::Clear),
},
2024-05-10 19:55:18 +02:00
CommandCode::Brightness => {
let Packet(header, payload) = packet;
2024-05-11 21:14:20 +02:00
if payload.len() != 1 {
2024-05-11 23:28:08 +02:00
return Err(TryFromPacketError::UnexpectedPayloadSize(
1,
payload.len(),
));
2024-05-11 21:14:20 +02:00
}
let Header(_, a, b, c, d) = header;
if a != 0 || b != 0 || c != 0 || d != 0 {
Err(TryFromPacketError::ExtraneousHeaderValues)
} else {
Ok(Command::Brightness(payload[0]))
2024-05-10 19:55:18 +02:00
}
}
CommandCode::HardReset => match check_command_only(packet) {
2024-05-11 23:28:08 +02:00
Some(err) => Err(err),
None => Ok(Command::HardReset),
},
CommandCode::FadeOut => match check_command_only(packet) {
2024-05-11 23:28:08 +02:00
Some(err) => Err(err),
None => Ok(Command::FadeOut),
},
2024-05-10 19:55:18 +02:00
CommandCode::Cp437Data => {
let Packet(_, payload) = packet;
2024-05-10 19:55:18 +02:00
Ok(Command::Cp437Data(
2024-05-11 21:14:20 +02:00
Origin(a, b),
ByteGrid::load(c as usize, d as usize, &payload),
2024-05-10 19:55:18 +02:00
))
}
CommandCode::CharBrightness => {
let Packet(_, payload) = packet;
2024-05-10 19:55:18 +02:00
Ok(Command::CharBrightness(
2024-05-11 21:14:20 +02:00
Origin(a, b),
ByteGrid::load(c as usize, d as usize, &payload),
2024-05-10 19:55:18 +02:00
))
}
2024-05-10 21:27:34 +02:00
#[allow(deprecated)]
2024-05-11 23:28:08 +02:00
CommandCode::BitmapLegacy => Ok(Command::BitmapLegacy),
2024-05-10 21:45:33 +02:00
CommandCode::BitmapLinear => {
let (vec, compression) = packet_into_linear_bitmap(packet)?;
2024-05-11 21:14:20 +02:00
Ok(Command::BitmapLinear(a, vec, compression))
2024-05-10 21:45:33 +02:00
}
CommandCode::BitmapLinearAnd => {
let (vec, compression) = packet_into_linear_bitmap(packet)?;
2024-05-11 21:14:20 +02:00
Ok(Command::BitmapLinearAnd(a, vec, compression))
2024-05-10 21:45:33 +02:00
}
CommandCode::BitmapLinearOr => {
let (vec, compression) = packet_into_linear_bitmap(packet)?;
2024-05-11 21:14:20 +02:00
Ok(Command::BitmapLinearOr(a, vec, compression))
2024-05-10 21:45:33 +02:00
}
CommandCode::BitmapLinearXor => {
let (vec, compression) = packet_into_linear_bitmap(packet)?;
2024-05-11 21:14:20 +02:00
Ok(Command::BitmapLinearXor(a, vec, compression))
}
CommandCode::BitmapLinearWinUncompressed => {
packet_into_bitmap_win(packet, CompressionCode::Uncompressed)
}
CommandCode::BitmapLinearWinZlib => {
packet_into_bitmap_win(packet, CompressionCode::Zlib)
}
CommandCode::BitmapLinearWinBzip2 => {
packet_into_bitmap_win(packet, CompressionCode::Bzip2)
}
CommandCode::BitmapLinearWinLzma => {
packet_into_bitmap_win(packet, CompressionCode::Lzma)
}
CommandCode::BitmapLinearWinZstd => {
packet_into_bitmap_win(packet, CompressionCode::Zstd)
}
2024-05-11 21:14:20 +02:00
}
}
}
fn packet_into_bitmap_win(packet: Packet, compression: CompressionCode) -> Result<Command, TryFromPacketError> {
let Packet(Header(_, tiles_x, pixels_y, tile_w, pixel_h), payload) = packet;
let payload = match into_decompressed(compression, payload) {
None => return Err(TryFromPacketError::DecompressionFailed),
Some(decompressed) => decompressed,
};
Ok(Command::BitmapLinearWin(
Origin(tiles_x * TILE_SIZE, pixels_y),
PixelGrid::load(
tile_w as usize * TILE_SIZE as usize,
pixel_h as usize,
&payload,
),
CompressionCode::Uncompressed,
))
}
/// Helper method for BitMapLinear*-Commands into Packet
2024-05-11 23:28:08 +02:00
fn bitmap_linear_into_packet(
command: CommandCode,
offset: Offset,
compression: CompressionCode,
payload: Vec<u8>,
) -> Packet {
2024-05-16 21:32:33 +02:00
let length = payload.len() as u16;
2024-05-11 22:21:27 +02:00
let payload = into_compressed(compression, payload);
2024-05-11 23:28:08 +02:00
Packet(
Header(
2024-05-12 13:11:42 +02:00
command.into(),
2024-05-11 23:28:08 +02:00
offset,
2024-05-16 21:32:33 +02:00
length,
2024-05-12 13:11:42 +02:00
compression.into(),
2024-05-11 23:28:08 +02:00
0,
),
payload,
)
2024-05-11 21:14:20 +02:00
}
/// Helper method for creating empty packets only containing the command code
2024-05-11 21:14:20 +02:00
fn command_code_only(code: CommandCode) -> Packet {
2024-05-12 13:14:33 +02:00
Packet(Header(code.into(), 0x0000, 0x0000, 0x0000, 0x0000), vec![])
2024-05-11 21:14:20 +02:00
}
/// Helper method for checking that a packet is empty and only contains a command code
2024-05-11 21:14:20 +02:00
fn check_command_only(packet: Packet) -> Option<TryFromPacketError> {
let Packet(Header(_, a, b, c, d), payload) = packet;
2024-05-15 23:08:54 +02:00
if !payload.is_empty() {
2024-05-11 21:14:20 +02:00
Some(TryFromPacketError::UnexpectedPayloadSize(0, payload.len()))
} else if a != 0 || b != 0 || c != 0 || d != 0 {
Some(TryFromPacketError::ExtraneousHeaderValues)
} else {
None
}
}
/// Helper method for Packets into BitMapLinear*-Commands
2024-05-11 23:28:08 +02:00
fn packet_into_linear_bitmap(
packet: Packet,
) -> Result<(BitVec, CompressionCode), TryFromPacketError> {
2024-05-11 21:14:20 +02:00
let Packet(Header(_, _, length, sub, reserved), payload) = packet;
if reserved != 0 {
return Err(TryFromPacketError::ExtraneousHeaderValues);
}
2024-05-12 13:11:42 +02:00
let sub = match CompressionCode::try_from(sub) {
Err(_) => return Err(TryFromPacketError::InvalidCompressionCode(sub)),
Ok(value) => value,
2024-05-11 21:14:20 +02:00
};
2024-05-11 22:21:27 +02:00
let payload = match into_decompressed(sub, payload) {
None => return Err(TryFromPacketError::DecompressionFailed),
2024-05-11 23:28:08 +02:00
Some(value) => value,
2024-05-11 22:21:27 +02:00
};
2024-05-16 21:32:33 +02:00
if payload.len() != length as usize {
return Err(TryFromPacketError::UnexpectedPayloadSize(
length as usize,
payload.len(),
));
}
2024-05-12 01:30:55 +02:00
Ok((BitVec::from(&*payload), sub))
2024-05-11 22:21:27 +02:00
}
2024-05-12 17:15:30 +02:00
2024-05-12 18:28:53 +02:00
#[cfg(feature = "c-api")]
2024-05-12 17:15:30 +02:00
pub mod c_api
{
use std::ptr::null_mut;
use crate::{BitVec, Brightness, ByteGrid, Command, CompressionCode, Offset, Origin, Packet, PixelGrid};
/// Tries to turn a `Packet` into a `Command`. The packet is gets deallocated in the process.
2024-05-12 17:15:30 +02:00
///
/// Returns: pointer to command or NULL
2024-05-12 17:15:30 +02:00
#[no_mangle]
pub unsafe extern "C" fn sp2_command_try_from_packet(packet: *mut Packet) -> *mut Command {
let packet = *Box::from_raw(packet);
match Command::try_from(packet) {
2024-05-15 23:08:54 +02:00
Err(_) => null_mut(),
Ok(command) => Box::into_raw(Box::new(command)),
}
2024-05-12 17:15:30 +02:00
}
/// Clones a `Command` instance
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_clone(original: *const Command) -> *mut Command {
2024-05-12 17:15:30 +02:00
Box::into_raw(Box::new((*original).clone()))
}
/// Allocates a new `Command::Clear` instance
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_clear() -> *mut Command {
2024-05-12 17:15:30 +02:00
Box::into_raw(Box::new(Command::Clear))
}
/// Allocates a new `Command::HardReset` instance
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_hard_reset() -> *mut Command {
2024-05-12 17:15:30 +02:00
Box::into_raw(Box::new(Command::HardReset))
}
/// Allocates a new `Command::FadeOut` instance
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_fade_out() -> *mut Command {
2024-05-12 17:15:30 +02:00
Box::into_raw(Box::new(Command::FadeOut))
}
/// Allocates a new `Command::Brightness` instance
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_brightness(brightness: Brightness) -> *mut Command {
2024-05-12 17:15:30 +02:00
Box::into_raw(Box::new(Command::Brightness(brightness)))
}
/// Allocates a new `Command::CharBrightness` instance.
/// The passed `ByteGrid` gets deallocated in the process.
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_char_brightness(x: u16, y: u16, byte_grid: *mut ByteGrid) -> *mut Command {
2024-05-12 17:15:30 +02:00
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(Command::CharBrightness(Origin(x, y), byte_grid)))
}
/// Allocates a new `Command::BitmapLinear` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_bitmap_linear(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
2024-05-12 17:15:30 +02:00
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinear(offset, bit_vec, compression)))
}
/// Allocates a new `Command::BitmapLinearAnd` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_bitmap_linear_and(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
2024-05-12 17:15:30 +02:00
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearAnd(offset, bit_vec, compression)))
}
/// Allocates a new `Command::BitmapLinearOr` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_bitmap_linear_or(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
2024-05-12 17:15:30 +02:00
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearOr(offset, bit_vec, compression)))
}
/// Allocates a new `Command::BitmapLinearXor` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_bitmap_linear_xor(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
2024-05-12 17:15:30 +02:00
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearXor(offset, bit_vec, compression)))
}
/// Allocates a new `Command::Cp437Data` instance.
/// The passed `ByteGrid` gets deallocated in the process.
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_cp437_data(x: u16, y: u16, byte_grid: *mut ByteGrid) -> *mut Command {
2024-05-12 17:15:30 +02:00
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(Command::Cp437Data(Origin(x, y), byte_grid)))
}
/// Allocates a new `Command::BitmapLinearWin` instance.
/// The passed `PixelGrid` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn sp2_command_bitmap_linear_win(x: u16, y: u16, byte_grid: *mut PixelGrid, compression_code: CompressionCode) -> *mut Command {
2024-05-12 17:15:30 +02:00
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(Command::BitmapLinearWin(Origin(x, y), byte_grid, compression_code)))
2024-05-12 17:15:30 +02:00
}
2024-05-12 18:28:53 +02:00
/// Deallocates a `Command`. Note that connection_send does this implicitly, so you only need
2024-05-12 17:15:30 +02:00
/// to do this if you use the library for parsing commands.
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_dealloc(ptr: *mut Command) {
2024-05-12 17:15:30 +02:00
_ = Box::from_raw(ptr);
}
}