2024-05-28 22:23:21 +02:00
|
|
|
//! C functions for interacting with `Command`s
|
|
|
|
//!
|
|
|
|
//! prefix `sp_command_`
|
|
|
|
|
2024-05-26 13:15:11 +02:00
|
|
|
use std::ptr::null_mut;
|
|
|
|
|
|
|
|
use servicepoint::{
|
2024-06-27 19:38:07 +02:00
|
|
|
Brightness, Command, CompressionCode, Offset, Origin, Packet, PixelGrid,
|
2024-05-26 13:15:11 +02:00
|
|
|
};
|
|
|
|
|
2024-06-03 21:08:26 +02:00
|
|
|
use crate::bit_vec::CBitVec;
|
2024-06-23 15:42:15 +02:00
|
|
|
use crate::brightness_grid::CBrightnessGrid;
|
|
|
|
use crate::cp437_grid::CCp437Grid;
|
2024-06-03 21:08:26 +02:00
|
|
|
|
2024-08-29 22:02:53 +02:00
|
|
|
/// A low-level display command.
|
|
|
|
///
|
|
|
|
/// This struct and associated functions implement the UDP protocol for the display.
|
|
|
|
///
|
|
|
|
/// To send a `CCommand`, use a `Connection`.
|
2024-08-29 20:39:42 +02:00
|
|
|
pub struct CCommand(pub(crate) Command);
|
|
|
|
|
|
|
|
impl Clone for CCommand {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
CCommand(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Tries to turn a `Packet` into a `Command`. The packet is deallocated in the process.
|
2024-05-26 13:15:11 +02:00
|
|
|
///
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Returns: pointer to new `Command` instance or NULL
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `packet` points to a valid instance of `Packet`
|
|
|
|
/// - `packet` is not used concurrently or after this call
|
|
|
|
/// - the result is checked for NULL
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_command_try_from_packet(
|
|
|
|
packet: *mut Packet,
|
2024-08-29 20:39:42 +02:00
|
|
|
) -> *mut CCommand {
|
2024-05-26 13:15:11 +02:00
|
|
|
let packet = *Box::from_raw(packet);
|
|
|
|
match Command::try_from(packet) {
|
|
|
|
Err(_) => null_mut(),
|
2024-08-29 20:39:42 +02:00
|
|
|
Ok(command) => Box::into_raw(Box::new(CCommand(command))),
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Clones a `Command` instance.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid instance of `Command`
|
|
|
|
/// - `this` is not written to concurrently
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_command_clone(
|
2024-08-29 20:39:42 +02:00
|
|
|
original: *const CCommand,
|
|
|
|
) -> *mut CCommand {
|
2024-05-26 13:15:11 +02:00
|
|
|
Box::into_raw(Box::new((*original).clone()))
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Allocates a new `Command::Clear` instance.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-08-29 20:39:42 +02:00
|
|
|
pub unsafe extern "C" fn sp_command_clear() -> *mut CCommand {
|
|
|
|
Box::into_raw(Box::new(CCommand(Command::Clear)))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Allocates a new `Command::HardReset` instance.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-08-29 20:39:42 +02:00
|
|
|
pub unsafe extern "C" fn sp_command_hard_reset() -> *mut CCommand {
|
|
|
|
Box::into_raw(Box::new(CCommand(Command::HardReset)))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Allocates a new `Command::FadeOut` instance.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-08-29 20:39:42 +02:00
|
|
|
pub unsafe extern "C" fn sp_command_fade_out() -> *mut CCommand {
|
|
|
|
Box::into_raw(Box::new(CCommand(Command::FadeOut)))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-06-23 13:00:13 +02:00
|
|
|
/// Allocates a new `Command::Brightness` instance for setting the brightness of all tiles to the
|
|
|
|
/// same value.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// - When the provided brightness value is out of range (0-11).
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-08-29 20:39:42 +02:00
|
|
|
pub unsafe extern "C" fn sp_command_brightness(
|
|
|
|
brightness: u8,
|
|
|
|
) -> *mut CCommand {
|
2024-06-23 13:38:46 +02:00
|
|
|
let brightness =
|
|
|
|
Brightness::try_from(brightness).expect("invalid brightness");
|
2024-08-29 20:39:42 +02:00
|
|
|
Box::into_raw(Box::new(CCommand(Command::Brightness(brightness))))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocates a new `Command::CharBrightness` instance.
|
2024-05-28 21:25:59 +02:00
|
|
|
/// The passed `ByteGrid` gets consumed.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `byte_grid` points to a valid instance of `ByteGrid`
|
|
|
|
/// - `byte_grid` is not used concurrently or after this call
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_command_char_brightness(
|
|
|
|
x: usize,
|
|
|
|
y: usize,
|
2024-06-23 15:42:15 +02:00
|
|
|
byte_grid: *mut CBrightnessGrid,
|
2024-08-29 20:39:42 +02:00
|
|
|
) -> *mut CCommand {
|
2024-05-26 13:15:11 +02:00
|
|
|
let byte_grid = *Box::from_raw(byte_grid);
|
2024-08-29 20:39:42 +02:00
|
|
|
Box::into_raw(Box::new(CCommand(Command::CharBrightness(
|
2024-06-23 13:38:46 +02:00
|
|
|
Origin::new(x, y),
|
2024-08-29 22:21:21 +02:00
|
|
|
byte_grid.actual,
|
2024-08-29 20:39:42 +02:00
|
|
|
))))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocates a new `Command::BitmapLinear` instance.
|
2024-05-28 21:25:59 +02:00
|
|
|
/// The passed `BitVec` gets consumed.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `bit_vec` points to a valid instance of `BitVec`
|
|
|
|
/// - `bit_vec` is not used concurrently or after this call
|
|
|
|
/// - `compression` matches one of the allowed enum values
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_command_bitmap_linear(
|
|
|
|
offset: Offset,
|
2024-06-03 21:08:26 +02:00
|
|
|
bit_vec: *mut CBitVec,
|
2024-05-26 13:15:11 +02:00
|
|
|
compression: CompressionCode,
|
2024-08-29 20:39:42 +02:00
|
|
|
) -> *mut CCommand {
|
2024-05-26 13:15:11 +02:00
|
|
|
let bit_vec = *Box::from_raw(bit_vec);
|
2024-08-29 20:39:42 +02:00
|
|
|
Box::into_raw(Box::new(CCommand(Command::BitmapLinear(
|
2024-05-26 13:15:11 +02:00
|
|
|
offset,
|
2024-06-03 21:08:26 +02:00
|
|
|
bit_vec.into(),
|
2024-05-26 13:15:11 +02:00
|
|
|
compression,
|
2024-08-29 20:39:42 +02:00
|
|
|
))))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocates a new `Command::BitmapLinearAnd` instance.
|
2024-05-28 21:25:59 +02:00
|
|
|
/// The passed `BitVec` gets consumed.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `bit_vec` points to a valid instance of `BitVec`
|
|
|
|
/// - `bit_vec` is not used concurrently or after this call
|
|
|
|
/// - `compression` matches one of the allowed enum values
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_command_bitmap_linear_and(
|
|
|
|
offset: Offset,
|
2024-06-03 21:08:26 +02:00
|
|
|
bit_vec: *mut CBitVec,
|
2024-05-26 13:15:11 +02:00
|
|
|
compression: CompressionCode,
|
2024-08-29 20:39:42 +02:00
|
|
|
) -> *mut CCommand {
|
2024-05-26 13:15:11 +02:00
|
|
|
let bit_vec = *Box::from_raw(bit_vec);
|
2024-08-29 20:39:42 +02:00
|
|
|
Box::into_raw(Box::new(CCommand(Command::BitmapLinearAnd(
|
2024-05-26 13:15:11 +02:00
|
|
|
offset,
|
2024-06-03 21:08:26 +02:00
|
|
|
bit_vec.into(),
|
2024-05-26 13:15:11 +02:00
|
|
|
compression,
|
2024-08-29 20:39:42 +02:00
|
|
|
))))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocates a new `Command::BitmapLinearOr` instance.
|
2024-05-28 21:25:59 +02:00
|
|
|
/// The passed `BitVec` gets consumed.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `bit_vec` points to a valid instance of `BitVec`
|
|
|
|
/// - `bit_vec` is not used concurrently or after this call
|
|
|
|
/// - `compression` matches one of the allowed enum values
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_command_bitmap_linear_or(
|
|
|
|
offset: Offset,
|
2024-06-03 21:08:26 +02:00
|
|
|
bit_vec: *mut CBitVec,
|
2024-05-26 13:15:11 +02:00
|
|
|
compression: CompressionCode,
|
2024-08-29 20:39:42 +02:00
|
|
|
) -> *mut CCommand {
|
2024-05-26 13:15:11 +02:00
|
|
|
let bit_vec = *Box::from_raw(bit_vec);
|
2024-08-29 20:39:42 +02:00
|
|
|
Box::into_raw(Box::new(CCommand(Command::BitmapLinearOr(
|
2024-05-26 13:15:11 +02:00
|
|
|
offset,
|
2024-06-03 21:08:26 +02:00
|
|
|
bit_vec.into(),
|
2024-05-26 13:15:11 +02:00
|
|
|
compression,
|
2024-08-29 20:39:42 +02:00
|
|
|
))))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocates a new `Command::BitmapLinearXor` instance.
|
2024-05-28 21:25:59 +02:00
|
|
|
/// The passed `BitVec` gets consumed.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `bit_vec` points to a valid instance of `BitVec`
|
|
|
|
/// - `bit_vec` is not used concurrently or after this call
|
|
|
|
/// - `compression` matches one of the allowed enum values
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
|
|
|
|
offset: Offset,
|
2024-06-03 21:08:26 +02:00
|
|
|
bit_vec: *mut CBitVec,
|
2024-05-26 13:15:11 +02:00
|
|
|
compression: CompressionCode,
|
2024-08-29 20:39:42 +02:00
|
|
|
) -> *mut CCommand {
|
2024-05-26 13:15:11 +02:00
|
|
|
let bit_vec = *Box::from_raw(bit_vec);
|
2024-08-29 20:39:42 +02:00
|
|
|
Box::into_raw(Box::new(CCommand(Command::BitmapLinearXor(
|
2024-05-26 13:15:11 +02:00
|
|
|
offset,
|
2024-06-03 21:08:26 +02:00
|
|
|
bit_vec.into(),
|
2024-05-26 13:15:11 +02:00
|
|
|
compression,
|
2024-08-29 20:39:42 +02:00
|
|
|
))))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocates a new `Command::Cp437Data` instance.
|
2024-05-28 21:25:59 +02:00
|
|
|
/// The passed `ByteGrid` gets consumed.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `byte_grid` points to a valid instance of `ByteGrid`
|
|
|
|
/// - `byte_grid` is not used concurrently or after this call
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_command_cp437_data(
|
|
|
|
x: usize,
|
|
|
|
y: usize,
|
2024-06-23 15:42:15 +02:00
|
|
|
byte_grid: *mut CCp437Grid,
|
2024-08-29 20:39:42 +02:00
|
|
|
) -> *mut CCommand {
|
2024-05-26 13:15:11 +02:00
|
|
|
let byte_grid = *Box::from_raw(byte_grid);
|
2024-08-29 20:39:42 +02:00
|
|
|
Box::into_raw(Box::new(CCommand(Command::Cp437Data(
|
|
|
|
Origin::new(x, y),
|
2024-08-29 22:21:21 +02:00
|
|
|
byte_grid.actual,
|
2024-08-29 20:39:42 +02:00
|
|
|
))))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocates a new `Command::BitmapLinearWin` instance.
|
2024-05-28 21:25:59 +02:00
|
|
|
/// The passed `PixelGrid` gets consumed.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `pixel_grid` points to a valid instance of `PixelGrid`
|
|
|
|
/// - `pixel_grid` is not used concurrently or after this call
|
|
|
|
/// - `compression` matches one of the allowed enum values
|
|
|
|
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_command_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_command_bitmap_linear_win(
|
|
|
|
x: usize,
|
|
|
|
y: usize,
|
2024-05-28 21:25:59 +02:00
|
|
|
pixel_grid: *mut PixelGrid,
|
2024-05-26 13:15:11 +02:00
|
|
|
compression_code: CompressionCode,
|
2024-08-29 20:39:42 +02:00
|
|
|
) -> *mut CCommand {
|
2024-05-28 21:25:59 +02:00
|
|
|
let byte_grid = *Box::from_raw(pixel_grid);
|
2024-08-29 20:39:42 +02:00
|
|
|
Box::into_raw(Box::new(CCommand(Command::BitmapLinearWin(
|
2024-06-23 13:38:46 +02:00
|
|
|
Origin::new(x, y),
|
2024-05-26 13:15:11 +02:00
|
|
|
byte_grid,
|
|
|
|
compression_code,
|
2024-08-29 20:39:42 +02:00
|
|
|
))))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Deallocates a `Command`.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `Command`
|
|
|
|
/// - `this` is not used concurrently or after this call
|
|
|
|
/// - `this` was not passed to another consuming function, e.g. to create a `Packet`
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-08-29 20:39:42 +02:00
|
|
|
pub unsafe extern "C" fn sp_command_dealloc(ptr: *mut CCommand) {
|
2024-05-26 13:15:11 +02:00
|
|
|
_ = Box::from_raw(ptr);
|
|
|
|
}
|