diff --git a/crates/servicepoint/examples/announce.rs b/crates/servicepoint/examples/announce.rs index 4f19194..75b9ba6 100644 --- a/crates/servicepoint/examples/announce.rs +++ b/crates/servicepoint/examples/announce.rs @@ -2,7 +2,7 @@ use clap::Parser; -use servicepoint::{Command, Connection, Cp473Grid, Grid, Origin}; +use servicepoint::{Command, Connection, Cp437Grid, Grid, Origin}; #[derive(Parser, Debug)] struct Cli { @@ -33,7 +33,7 @@ fn main() { let max_width = cli.text.iter().map(|t| t.len()).max().unwrap(); - let mut chars = Cp473Grid::new(max_width, cli.text.len()); + let mut chars = Cp437Grid::new(max_width, cli.text.len()); for y in 0..cli.text.len() { let row = &cli.text[y]; diff --git a/crates/servicepoint/src/command.rs b/crates/servicepoint/src/command.rs index 11b151b..3bc63b6 100644 --- a/crates/servicepoint/src/command.rs +++ b/crates/servicepoint/src/command.rs @@ -13,7 +13,7 @@ pub type Offset = usize; /// A grid containing codepage 437 characters. /// /// The encoding is currently not enforced. -pub type Cp473Grid = PrimitiveGrid; +pub type Cp437Grid = PrimitiveGrid; /// A command to send to the display. #[derive(Debug, Clone, PartialEq)] @@ -27,7 +27,7 @@ pub enum Command { /// The library does not currently convert between UTF-8 and CP-437. /// Because Rust expects UTF-8 strings, it might be necessary to only send ASCII for now. /// - Cp437Data(Origin, Cp473Grid), + Cp437Data(Origin, Cp437Grid), /// Sets a window of pixels to the specified values BitmapLinearWin(Origin, PixelGrid, CompressionCode), @@ -276,7 +276,7 @@ impl TryFrom for Command { let Packet(_, payload) = packet; Ok(Command::Cp437Data( Origin::new(a as usize, b as usize), - Cp473Grid::load(c as usize, d as usize, &payload), + Cp437Grid::load(c as usize, d as usize, &payload), )) } CommandCode::CharBrightness => { diff --git a/crates/servicepoint/src/lib.rs b/crates/servicepoint/src/lib.rs index f923257..bd9c123 100644 --- a/crates/servicepoint/src/lib.rs +++ b/crates/servicepoint/src/lib.rs @@ -6,7 +6,7 @@ pub use bitvec; use bitvec::prelude::{BitVec, Msb0}; pub use crate::brightness::{Brightness, BrightnessGrid}; -pub use crate::command::{Command, Cp473Grid, Offset}; +pub use crate::command::{Command, Cp437Grid, Offset}; pub use crate::compression_code::CompressionCode; pub use crate::connection::Connection; pub use crate::data_ref::DataRef; diff --git a/crates/servicepoint_binding_c/cbindgen.toml b/crates/servicepoint_binding_c/cbindgen.toml index 1c63d58..c65f551 100644 --- a/crates/servicepoint_binding_c/cbindgen.toml +++ b/crates/servicepoint_binding_c/cbindgen.toml @@ -33,4 +33,4 @@ include = ["servicepoint"] extra_bindings = ["servicepoint"] [parse.expand] -#all_features = true +all_features = true diff --git a/crates/servicepoint_binding_c/examples/lang_c/include/servicepoint.h b/crates/servicepoint_binding_c/examples/lang_c/include/servicepoint.h index 075f61c..270bb5c 100644 --- a/crates/servicepoint_binding_c/examples/lang_c/include/servicepoint.h +++ b/crates/servicepoint_binding_c/examples/lang_c/include/servicepoint.h @@ -70,14 +70,18 @@ typedef uint16_t sp_CompressionCode; #endif // __cplusplus /** - * A fixed-size vector of bits + * A display brightness value, checked for correct value range */ -typedef struct sp_BitVec sp_BitVec; +typedef struct sp_Brightness sp_Brightness; /** - * A 2D grid of bytes + * Opaque struct needed for correct code generation. */ -typedef struct sp_ByteGrid sp_ByteGrid; +typedef struct sp_CBitVec sp_CBitVec; + +typedef struct sp_CBrightnessGrid sp_CBrightnessGrid; + +typedef struct sp_CCp437Grid sp_CCp437Grid; /** * A command to send to the display. @@ -99,6 +103,16 @@ typedef struct sp_Packet sp_Packet; */ typedef struct sp_PixelGrid sp_PixelGrid; +/** + * A 2D grid of bytes + */ +typedef struct sp_PrimitiveGrid_Brightness sp_PrimitiveGrid_Brightness; + +/** + * A 2D grid of bytes + */ +typedef struct sp_PrimitiveGrid_u8 sp_PrimitiveGrid_u8; + /** * Represents a span of memory (`&mut [u8]` ) as a struct usable by C code. * @@ -127,9 +141,20 @@ typedef struct sp_CByteSlice { typedef size_t sp_Offset; /** - * Type alias for documenting the meaning of the u16 in enum values + * A grid containing brightness values. */ -typedef uint8_t sp_Brightness; +typedef struct sp_PrimitiveGrid_Brightness sp_BrightnessGrid; + +/** + * A grid containing codepage 437 characters. + * + * The encoding is currently not enforced. + */ +typedef struct sp_PrimitiveGrid_u8 sp_Cp437Grid; + + + + #ifdef __cplusplus extern "C" { @@ -147,7 +172,7 @@ extern "C" { * - the returned instance is freed in some way, either by using a consuming function or * by explicitly calling `sp_bit_vec_dealloc`. */ -struct sp_BitVec *sp_bit_vec_clone(const struct sp_BitVec *this_); +struct sp_CBitVec *sp_bit_vec_clone(const struct sp_CBitVec *this_); /** * Deallocates a `BitVec`. @@ -160,7 +185,7 @@ struct sp_BitVec *sp_bit_vec_clone(const struct sp_BitVec *this_); * - `this` is not used concurrently or after this call * - `this` was not passed to another consuming function, e.g. to create a `Command` */ -void sp_bit_vec_dealloc(struct sp_BitVec *this_); +void sp_bit_vec_dealloc(struct sp_CBitVec *this_); /** * Sets the value of all bits in the `BitVec`. @@ -176,7 +201,7 @@ void sp_bit_vec_dealloc(struct sp_BitVec *this_); * - `this` points to a valid `BitVec` * - `this` is not written to or read from concurrently */ -void sp_bit_vec_fill(struct sp_BitVec *this_, bool value); +void sp_bit_vec_fill(struct sp_CBitVec *this_, bool value); /** * Gets the value of a bit from the `BitVec`. @@ -199,7 +224,7 @@ void sp_bit_vec_fill(struct sp_BitVec *this_, bool value); * - `this` points to a valid `BitVec` * - `this` is not written to concurrently */ -bool sp_bit_vec_get(const struct sp_BitVec *this_, size_t index); +bool sp_bit_vec_get(const struct sp_CBitVec *this_, size_t index); /** * Returns true if length is 0. @@ -210,7 +235,7 @@ bool sp_bit_vec_get(const struct sp_BitVec *this_, size_t index); * * - `this` points to a valid `BitVec` */ -bool sp_bit_vec_is_empty(const struct sp_BitVec *this_); +bool sp_bit_vec_is_empty(const struct sp_CBitVec *this_); /** * Gets the length of the `BitVec` in bits. @@ -221,7 +246,7 @@ bool sp_bit_vec_is_empty(const struct sp_BitVec *this_); * * - `this` points to a valid `BitVec` */ -size_t sp_bit_vec_len(const struct sp_BitVec *this_); +size_t sp_bit_vec_len(const struct sp_CBitVec *this_); /** * Interpret the data as a series of bits and load then into a new `BitVec` instance. @@ -235,8 +260,8 @@ size_t sp_bit_vec_len(const struct sp_BitVec *this_); * - the returned instance is freed in some way, either by using a consuming function or * by explicitly calling `sp_bit_vec_dealloc`. */ -struct sp_BitVec *sp_bit_vec_load(const uint8_t *data, - size_t data_length); +struct sp_CBitVec *sp_bit_vec_load(const uint8_t *data, + size_t data_length); /** * Creates a new `BitVec` instance. @@ -258,7 +283,7 @@ struct sp_BitVec *sp_bit_vec_load(const uint8_t *data, * - the returned instance is freed in some way, either by using a consuming function or * by explicitly calling `sp_bit_vec_dealloc`. */ -struct sp_BitVec *sp_bit_vec_new(size_t size); +struct sp_CBitVec *sp_bit_vec_new(size_t size); /** * Sets the value of a bit in the `BitVec`. @@ -282,7 +307,7 @@ struct sp_BitVec *sp_bit_vec_new(size_t size); * - `this` points to a valid `BitVec` * - `this` is not written to or read from concurrently */ -bool sp_bit_vec_set(struct sp_BitVec *this_, size_t index, bool value); +void sp_bit_vec_set(struct sp_CBitVec *this_, size_t index, bool value); /** * Gets an unsafe reference to the data of the `BitVec` instance. @@ -295,37 +320,37 @@ bool sp_bit_vec_set(struct sp_BitVec *this_, size_t index, bool value); * - the returned memory range is never accessed after the passed `BitVec` has been freed * - the returned memory range is never accessed concurrently, either via the `BitVec` or directly */ -struct sp_CByteSlice sp_bit_vec_unsafe_data_ref(struct sp_BitVec *this_); +struct sp_CByteSlice sp_bit_vec_unsafe_data_ref(struct sp_CBitVec *this_); /** - * Clones a `ByteGrid`. + * Clones a `BrightnessGrid`. * * # Safety * * The caller has to make sure that: * - * - `this` points to a valid `ByteGrid` + * - `this` points to a valid `BrightnessGrid` * - `this` is not written to concurrently * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_byte_grid_dealloc`. + * by explicitly calling `sp_brightness_grid_dealloc`. */ -struct sp_ByteGrid *sp_byte_grid_clone(const struct sp_ByteGrid *this_); +struct sp_CBrightnessGrid *sp_brightness_grid_clone(const struct sp_CBrightnessGrid *this_); /** - * Deallocates a `ByteGrid`. + * Deallocates a `BrightnessGrid`. * * # Safety * * The caller has to make sure that: * - * - `this` points to a valid `ByteGrid` + * - `this` points to a valid `BrightnessGrid` * - `this` is not used concurrently or after this call * - `this` was not passed to another consuming function, e.g. to create a `Command` */ -void sp_byte_grid_dealloc(struct sp_ByteGrid *this_); +void sp_brightness_grid_dealloc(struct sp_CBrightnessGrid *this_); /** - * Sets the value of all cells in the `ByteGrid`. + * Sets the value of all cells in the `BrightnessGrid`. * * # Arguments * @@ -336,10 +361,10 @@ void sp_byte_grid_dealloc(struct sp_ByteGrid *this_); * * The caller has to make sure that: * - * - `this` points to a valid `ByteGrid` + * - `this` points to a valid `BrightnessGrid` * - `this` is not written to or read from concurrently */ -void sp_byte_grid_fill(struct sp_ByteGrid *this_, uint8_t value); +void sp_brightness_grid_fill(struct sp_CBrightnessGrid *this_, uint8_t value); /** * Gets the current value at the specified position. @@ -357,13 +382,15 @@ void sp_byte_grid_fill(struct sp_ByteGrid *this_, uint8_t value); * * The caller has to make sure that: * - * - `this` points to a valid `ByteGrid` + * - `this` points to a valid `BrightnessGrid` * - `this` is not written to concurrently */ -uint8_t sp_byte_grid_get(const struct sp_ByteGrid *this_, size_t x, size_t y); +uint8_t sp_brightness_grid_get(const struct sp_CBrightnessGrid *this_, + size_t x, + size_t y); /** - * Gets the height of the `ByteGrid` instance. + * Gets the height of the `BrightnessGrid` instance. * * # Arguments * @@ -373,12 +400,12 @@ uint8_t sp_byte_grid_get(const struct sp_ByteGrid *this_, size_t x, size_t y); * * The caller has to make sure that: * - * - `this` points to a valid `ByteGrid` + * - `this` points to a valid `BrightnessGrid` */ -size_t sp_byte_grid_height(const struct sp_ByteGrid *this_); +size_t sp_brightness_grid_height(const struct sp_CBrightnessGrid *this_); /** - * Loads a `ByteGrid` with the specified dimensions from the provided data. + * Loads a `BrightnessGrid` with the specified dimensions from the provided data. * * # Panics * @@ -391,30 +418,30 @@ size_t sp_byte_grid_height(const struct sp_ByteGrid *this_); * - `data` points to a valid memory location of at least `data_length` * bytes in size. * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_byte_grid_dealloc`. + * by explicitly calling `sp_brightness_grid_dealloc`. */ -struct sp_ByteGrid *sp_byte_grid_load(size_t width, - size_t height, - const uint8_t *data, - size_t data_length); +struct sp_CBrightnessGrid *sp_brightness_grid_load(size_t width, + size_t height, + const uint8_t *data, + size_t data_length); /** - * Creates a new `ByteGrid` with the specified dimensions. + * Creates a new `BrightnessGrid` with the specified dimensions. * - * returns: `ByteGrid` initialized to 0. + * returns: `BrightnessGrid` initialized to 0. * * # Safety * * The caller has to make sure that: * * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_byte_grid_dealloc`. + * by explicitly calling `sp_brightness_grid_dealloc`. */ -struct sp_ByteGrid *sp_byte_grid_new(size_t width, - size_t height); +struct sp_CBrightnessGrid *sp_brightness_grid_new(size_t width, + size_t height); /** - * Sets the value of the specified position in the `ByteGrid`. + * Sets the value of the specified position in the `BrightnessGrid`. * * # Arguments * @@ -435,26 +462,26 @@ struct sp_ByteGrid *sp_byte_grid_new(size_t width, * - `this` points to a valid `BitVec` * - `this` is not written to or read from concurrently */ -void sp_byte_grid_set(struct sp_ByteGrid *this_, - size_t x, - size_t y, - uint8_t value); +void sp_brightness_grid_set(struct sp_CBrightnessGrid *this_, + size_t x, + size_t y, + uint8_t value); /** - * Gets an unsafe reference to the data of the `ByteGrid` instance. + * Gets an unsafe reference to the data of the `BrightnessGrid` instance. * * ## Safety * * The caller has to make sure that: * - * - `this` points to a valid `ByteGrid` - * - the returned memory range is never accessed after the passed `ByteGrid` has been freed - * - the returned memory range is never accessed concurrently, either via the `ByteGrid` or directly + * - `this` points to a valid `BrightnessGrid` + * - the returned memory range is never accessed after the passed `BrightnessGrid` has been freed + * - the returned memory range is never accessed concurrently, either via the `BrightnessGrid` or directly */ -struct sp_CByteSlice sp_byte_grid_unsafe_data_ref(struct sp_ByteGrid *this_); +struct sp_CByteSlice sp_brightness_grid_unsafe_data_ref(struct sp_CBrightnessGrid *this_); /** - * Gets the width of the `ByteGrid` instance. + * Gets the width of the `BrightnessGrid` instance. * * # Arguments * @@ -464,9 +491,9 @@ struct sp_CByteSlice sp_byte_grid_unsafe_data_ref(struct sp_ByteGrid *this_); * * The caller has to make sure that: * - * - `this` points to a valid `ByteGrid` + * - `this` points to a valid `BrightnessGrid` */ -size_t sp_byte_grid_width(const struct sp_ByteGrid *this_); +size_t sp_brightness_grid_width(const struct sp_CBrightnessGrid *this_); /** * Allocates a new `Command::BitmapLinear` instance. @@ -483,7 +510,7 @@ size_t sp_byte_grid_width(const struct sp_ByteGrid *this_); * by explicitly calling `sp_command_dealloc`. */ struct sp_Command *sp_command_bitmap_linear(sp_Offset offset, - struct sp_BitVec *bit_vec, + struct sp_CBitVec *bit_vec, sp_CompressionCode compression); /** @@ -501,7 +528,7 @@ struct sp_Command *sp_command_bitmap_linear(sp_Offset offset, * by explicitly calling `sp_command_dealloc`. */ struct sp_Command *sp_command_bitmap_linear_and(sp_Offset offset, - struct sp_BitVec *bit_vec, + struct sp_CBitVec *bit_vec, sp_CompressionCode compression); /** @@ -519,7 +546,7 @@ struct sp_Command *sp_command_bitmap_linear_and(sp_Offset offset, * by explicitly calling `sp_command_dealloc`. */ struct sp_Command *sp_command_bitmap_linear_or(sp_Offset offset, - struct sp_BitVec *bit_vec, + struct sp_CBitVec *bit_vec, sp_CompressionCode compression); /** @@ -556,11 +583,16 @@ struct sp_Command *sp_command_bitmap_linear_win(size_t x, * by explicitly calling `sp_command_dealloc`. */ struct sp_Command *sp_command_bitmap_linear_xor(sp_Offset offset, - struct sp_BitVec *bit_vec, + struct sp_CBitVec *bit_vec, sp_CompressionCode compression); /** - * Allocates a new `Command::Brightness` instance. + * 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). * * # Safety * @@ -569,7 +601,7 @@ struct sp_Command *sp_command_bitmap_linear_xor(sp_Offset offset, * - the returned `Command` instance is freed in some way, either by using a consuming function or * by explicitly calling `sp_command_dealloc`. */ -struct sp_Command *sp_command_brightness(sp_Brightness brightness); +struct sp_Command *sp_command_brightness(uint8_t brightness); /** * Allocates a new `Command::CharBrightness` instance. @@ -586,7 +618,7 @@ struct sp_Command *sp_command_brightness(sp_Brightness brightness); */ struct sp_Command *sp_command_char_brightness(size_t x, size_t y, - struct sp_ByteGrid *byte_grid); + sp_BrightnessGrid *byte_grid); /** * Allocates a new `Command::Clear` instance. @@ -629,7 +661,7 @@ struct sp_Command *sp_command_clone(const struct sp_Command *original); */ struct sp_Command *sp_command_cp437_data(size_t x, size_t y, - struct sp_ByteGrid *byte_grid); + sp_Cp437Grid *byte_grid); /** * Deallocates a `Command`. @@ -732,6 +764,179 @@ struct sp_Connection *sp_connection_open(const char *host); bool sp_connection_send(const struct sp_Connection *connection, struct sp_Packet *packet); +/** + * Clones a `Cp437Grid`. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + * - `this` is not written to concurrently + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_cp437_grid_dealloc`. + */ +struct sp_CCp437Grid *sp_cp437_grid_clone(const struct sp_CCp437Grid *this_); + +/** + * Deallocates a `Cp437Grid`. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + * - `this` is not used concurrently or after this call + * - `this` was not passed to another consuming function, e.g. to create a `Command` + */ +void sp_cp437_grid_dealloc(struct sp_CCp437Grid *this_); + +/** + * Sets the value of all cells in the `Cp437Grid`. + * + * # Arguments + * + * * `this`: instance to write to + * * `value`: the value to set all cells to + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + * - `this` is not written to or read from concurrently + */ +void sp_cp437_grid_fill(struct sp_CCp437Grid *this_, uint8_t value); + +/** + * Gets the current value at the specified position. + * + * # Arguments + * + * * `this`: instance to read from + * * `x` and `y`: position of the cell to read + * + * # Panics + * + * When accessing `x` or `y` out of bounds. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + * - `this` is not written to concurrently + */ +uint8_t sp_cp437_grid_get(const struct sp_CCp437Grid *this_, + size_t x, + size_t y); + +/** + * Gets the height of the `Cp437Grid` instance. + * + * # Arguments + * + * * `this`: instance to read from + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + */ +size_t sp_cp437_grid_height(const struct sp_CCp437Grid *this_); + +/** + * Loads a `Cp437Grid` with the specified dimensions from the provided data. + * + * # Panics + * + * When the provided `data_length` is not sufficient for the `height` and `width` + * + * # Safety + * + * The caller has to make sure that: + * + * - `data` points to a valid memory location of at least `data_length` + * bytes in size. + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_cp437_grid_dealloc`. + */ +struct sp_CCp437Grid *sp_cp437_grid_load(size_t width, + size_t height, + const uint8_t *data, + size_t data_length); + +/** + * Creates a new `Cp437Grid` with the specified dimensions. + * + * returns: `Cp437Grid` initialized to 0. + * + * # Safety + * + * The caller has to make sure that: + * + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_cp437_grid_dealloc`. + */ +struct sp_CCp437Grid *sp_cp437_grid_new(size_t width, + size_t height); + +/** + * Sets the value of the specified position in the `Cp437Grid`. + * + * # Arguments + * + * * `this`: instance to write to + * * `x` and `y`: position of the cell + * * `value`: the value to write to the cell + * + * returns: old value of the cell + * + * # Panics + * + * When accessing `x` or `y` out of bounds. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BitVec` + * - `this` is not written to or read from concurrently + */ +void sp_cp437_grid_set(struct sp_CCp437Grid *this_, + size_t x, + size_t y, + uint8_t value); + +/** + * Gets an unsafe reference to the data of the `Cp437Grid` instance. + * + * ## Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + * - the returned memory range is never accessed after the passed `Cp437Grid` has been freed + * - the returned memory range is never accessed concurrently, either via the `Cp437Grid` or directly + */ +struct sp_CByteSlice sp_cp437_grid_unsafe_data_ref(struct sp_CCp437Grid *this_); + +/** + * Gets the width of the `Cp437Grid` instance. + * + * # Arguments + * + * * `this`: instance to read from + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + */ +size_t sp_cp437_grid_width(const struct sp_CCp437Grid *this_); + /** * Deallocates a `Packet`. * diff --git a/crates/servicepoint_binding_c/src/brightness_grid.rs b/crates/servicepoint_binding_c/src/brightness_grid.rs new file mode 100644 index 0000000..49203d7 --- /dev/null +++ b/crates/servicepoint_binding_c/src/brightness_grid.rs @@ -0,0 +1,222 @@ +//! C functions for interacting with `BrightnessGrid`s +//! +//! prefix `sp_brightness_grid_` + +use std::intrinsics::transmute; +use servicepoint::{BrightnessGrid, DataRef, Grid, PrimitiveGrid, Brightness}; + +use crate::c_slice::CByteSlice; + +/// C-wrapper for grid containing brightness values. +#[derive(Clone)] +pub struct CBrightnessGrid(pub(crate) BrightnessGrid); + +/// Creates a new `BrightnessGrid` with the specified dimensions. +/// +/// returns: `BrightnessGrid` initialized to 0. +/// +/// # Safety +/// +/// The caller has to make sure that: +/// +/// - the returned instance is freed in some way, either by using a consuming function or +/// by explicitly calling `sp_brightness_grid_dealloc`. +#[no_mangle] +pub unsafe extern "C" fn sp_brightness_grid_new( + width: usize, + height: usize, +) -> *mut CBrightnessGrid { + Box::into_raw(Box::new(CBrightnessGrid(BrightnessGrid::new(width, height)))) +} + +/// Loads a `BrightnessGrid` with the specified dimensions from the provided data. +/// +/// # Panics +/// +/// When the provided `data_length` is not sufficient for the `height` and `width` +/// +/// # Safety +/// +/// The caller has to make sure that: +/// +/// - `data` points to a valid memory location of at least `data_length` +/// bytes in size. +/// - the returned instance is freed in some way, either by using a consuming function or +/// by explicitly calling `sp_brightness_grid_dealloc`. +#[no_mangle] +pub unsafe extern "C" fn sp_brightness_grid_load( + width: usize, + height: usize, + data: *const u8, + data_length: usize, +) -> *mut CBrightnessGrid { + let data = std::slice::from_raw_parts(data, data_length); + let grid = PrimitiveGrid::load(width, height, data); + let grid = BrightnessGrid::try_from(grid) + .expect("invalid brightness value"); + Box::into_raw(Box::new(CBrightnessGrid(grid))) +} + +/// Clones a `BrightnessGrid`. +/// +/// # Safety +/// +/// The caller has to make sure that: +/// +/// - `this` points to a valid `BrightnessGrid` +/// - `this` is not written to concurrently +/// - the returned instance is freed in some way, either by using a consuming function or +/// by explicitly calling `sp_brightness_grid_dealloc`. +#[no_mangle] +pub unsafe extern "C" fn sp_brightness_grid_clone( + this: *const CBrightnessGrid, +) -> *mut CBrightnessGrid { + Box::into_raw(Box::new((*this).clone())) +} + +/// Deallocates a `BrightnessGrid`. +/// +/// # Safety +/// +/// The caller has to make sure that: +/// +/// - `this` points to a valid `BrightnessGrid` +/// - `this` is not used concurrently or after this call +/// - `this` was not passed to another consuming function, e.g. to create a `Command` +#[no_mangle] +pub unsafe extern "C" fn sp_brightness_grid_dealloc(this: *mut CBrightnessGrid) { + _ = Box::from_raw(this); +} + +/// Gets the current value at the specified position. +/// +/// # Arguments +/// +/// * `this`: instance to read from +/// * `x` and `y`: position of the cell to read +/// +/// # Panics +/// +/// When accessing `x` or `y` out of bounds. +/// +/// # Safety +/// +/// The caller has to make sure that: +/// +/// - `this` points to a valid `BrightnessGrid` +/// - `this` is not written to concurrently +#[no_mangle] +pub unsafe extern "C" fn sp_brightness_grid_get( + this: *const CBrightnessGrid, + x: usize, + y: usize, +) -> u8 { + (*this).0.get(x, y).into() +} + +/// Sets the value of the specified position in the `BrightnessGrid`. +/// +/// # Arguments +/// +/// * `this`: instance to write to +/// * `x` and `y`: position of the cell +/// * `value`: the value to write to the cell +/// +/// returns: old value of the cell +/// +/// # Panics +/// +/// When accessing `x` or `y` out of bounds. +/// +/// # Safety +/// +/// The caller has to make sure that: +/// +/// - `this` points to a valid `BitVec` +/// - `this` is not written to or read from concurrently +#[no_mangle] +pub unsafe extern "C" fn sp_brightness_grid_set( + this: *mut CBrightnessGrid, + x: usize, + y: usize, + value: u8, +) { + let brightness = Brightness::try_from(value) + .expect("invalid brightness value"); + (*this).0.set(x, y, brightness); +} + +/// Sets the value of all cells in the `BrightnessGrid`. +/// +/// # Arguments +/// +/// * `this`: instance to write to +/// * `value`: the value to set all cells to +/// +/// # Safety +/// +/// The caller has to make sure that: +/// +/// - `this` points to a valid `BrightnessGrid` +/// - `this` is not written to or read from concurrently +#[no_mangle] +pub unsafe extern "C" fn sp_brightness_grid_fill(this: *mut CBrightnessGrid, value: u8) { + let brightness = Brightness::try_from(value) + .expect("invalid brightness value"); + (*this).0.fill(brightness); +} + +/// Gets the width of the `BrightnessGrid` instance. +/// +/// # Arguments +/// +/// * `this`: instance to read from +/// +/// # Safety +/// +/// The caller has to make sure that: +/// +/// - `this` points to a valid `BrightnessGrid` +#[no_mangle] +pub unsafe extern "C" fn sp_brightness_grid_width(this: *const CBrightnessGrid) -> usize { + (*this).0.width() +} + +/// Gets the height of the `BrightnessGrid` instance. +/// +/// # Arguments +/// +/// * `this`: instance to read from +/// +/// # Safety +/// +/// The caller has to make sure that: +/// +/// - `this` points to a valid `BrightnessGrid` +#[no_mangle] +pub unsafe extern "C" fn sp_brightness_grid_height(this: *const CBrightnessGrid) -> usize { + (*this).0.height() +} + +/// Gets an unsafe reference to the data of the `BrightnessGrid` instance. +/// +/// ## Safety +/// +/// The caller has to make sure that: +/// +/// - `this` points to a valid `BrightnessGrid` +/// - the returned memory range is never accessed after the passed `BrightnessGrid` has been freed +/// - the returned memory range is never accessed concurrently, either via the `BrightnessGrid` or directly +#[no_mangle] +pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref( + this: *mut CBrightnessGrid, +) -> CByteSlice { + assert_eq!(std::mem::size_of::(), 1); + + let data = (*this).0.data_ref_mut(); + let data: &mut [u8] = transmute(data); + CByteSlice { + start: data.as_mut_ptr_range().start, + length: data.len(), + } +} diff --git a/crates/servicepoint_binding_c/src/command.rs b/crates/servicepoint_binding_c/src/command.rs index b799072..c3749d7 100644 --- a/crates/servicepoint_binding_c/src/command.rs +++ b/crates/servicepoint_binding_c/src/command.rs @@ -5,11 +5,13 @@ use std::ptr::null_mut; use servicepoint::{ - Brightness, BrightnessGrid, Command, CompressionCode, Cp473Grid, Offset, + Brightness, Command, CompressionCode, Offset, Origin, Packet, PixelGrid, }; use crate::bit_vec::CBitVec; +use crate::brightness_grid::CBrightnessGrid; +use crate::cp437_grid::CCp437Grid; /// Tries to turn a `Packet` into a `Command`. The packet is deallocated in the process. /// @@ -126,12 +128,12 @@ pub unsafe extern "C" fn sp_command_brightness(brightness: u8) -> *mut Command { pub unsafe extern "C" fn sp_command_char_brightness( x: usize, y: usize, - byte_grid: *mut BrightnessGrid, + byte_grid: *mut CBrightnessGrid, ) -> *mut Command { let byte_grid = *Box::from_raw(byte_grid); Box::into_raw(Box::new(Command::CharBrightness( Origin::new(x, y), - byte_grid, + byte_grid.0, ))) } @@ -254,10 +256,10 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_xor( pub unsafe extern "C" fn sp_command_cp437_data( x: usize, y: usize, - byte_grid: *mut Cp473Grid, + byte_grid: *mut CCp437Grid, ) -> *mut Command { let byte_grid = *Box::from_raw(byte_grid); - Box::into_raw(Box::new(Command::Cp437Data(Origin::new(x, y), byte_grid))) + Box::into_raw(Box::new(Command::Cp437Data(Origin::new(x, y), byte_grid.0))) } /// Allocates a new `Command::BitmapLinearWin` instance. diff --git a/crates/servicepoint_binding_c/src/byte_grid.rs b/crates/servicepoint_binding_c/src/cp437_grid.rs similarity index 54% rename from crates/servicepoint_binding_c/src/byte_grid.rs rename to crates/servicepoint_binding_c/src/cp437_grid.rs index 4c8c1cb..2c57b0d 100644 --- a/crates/servicepoint_binding_c/src/byte_grid.rs +++ b/crates/servicepoint_binding_c/src/cp437_grid.rs @@ -1,32 +1,36 @@ -//! C functions for interacting with `ByteGrid`s +//! C functions for interacting with `Cp437Grid`s //! -//! prefix `sp_byte_grid_` +//! prefix `sp_cp437_grid_` -use servicepoint::{DataRef, Grid, PrimitiveGrid}; +use servicepoint::{Cp437Grid, DataRef, Grid}; use crate::c_slice::CByteSlice; -pub type ByteGrid = PrimitiveGrid; - -/// Creates a new `ByteGrid` with the specified dimensions. +/// A C-wrapper for grid containing codepage 437 characters. /// -/// returns: `ByteGrid` initialized to 0. +/// The encoding is currently not enforced. +#[derive(Clone)] +pub struct CCp437Grid(pub(crate) Cp437Grid); + +/// Creates a new `Cp437Grid` with the specified dimensions. +/// +/// returns: `Cp437Grid` initialized to 0. /// /// # Safety /// /// The caller has to make sure that: /// /// - the returned instance is freed in some way, either by using a consuming function or -/// by explicitly calling `sp_byte_grid_dealloc`. +/// by explicitly calling `sp_cp437_grid_dealloc`. #[no_mangle] -pub unsafe extern "C" fn sp_byte_grid_new( +pub unsafe extern "C" fn sp_cp437_grid_new( width: usize, height: usize, -) -> *mut ByteGrid { - Box::into_raw(Box::new(ByteGrid::new(width, height))) +) -> *mut CCp437Grid { + Box::into_raw(Box::new(CCp437Grid(Cp437Grid::new(width, height)))) } -/// Loads a `ByteGrid` with the specified dimensions from the provided data. +/// Loads a `Cp437Grid` with the specified dimensions from the provided data. /// /// # Panics /// @@ -39,46 +43,46 @@ pub unsafe extern "C" fn sp_byte_grid_new( /// - `data` points to a valid memory location of at least `data_length` /// bytes in size. /// - the returned instance is freed in some way, either by using a consuming function or -/// by explicitly calling `sp_byte_grid_dealloc`. +/// by explicitly calling `sp_cp437_grid_dealloc`. #[no_mangle] -pub unsafe extern "C" fn sp_byte_grid_load( +pub unsafe extern "C" fn sp_cp437_grid_load( width: usize, height: usize, data: *const u8, data_length: usize, -) -> *mut ByteGrid { +) -> *mut CCp437Grid { let data = std::slice::from_raw_parts(data, data_length); - Box::into_raw(Box::new(ByteGrid::load(width, height, data))) + Box::into_raw(Box::new(CCp437Grid(Cp437Grid::load(width, height, data)))) } -/// Clones a `ByteGrid`. +/// Clones a `Cp437Grid`. /// /// # Safety /// /// The caller has to make sure that: /// -/// - `this` points to a valid `ByteGrid` +/// - `this` points to a valid `Cp437Grid` /// - `this` is not written to concurrently /// - the returned instance is freed in some way, either by using a consuming function or -/// by explicitly calling `sp_byte_grid_dealloc`. +/// by explicitly calling `sp_cp437_grid_dealloc`. #[no_mangle] -pub unsafe extern "C" fn sp_byte_grid_clone( - this: *const ByteGrid, -) -> *mut ByteGrid { +pub unsafe extern "C" fn sp_cp437_grid_clone( + this: *const CCp437Grid, +) -> *mut CCp437Grid { Box::into_raw(Box::new((*this).clone())) } -/// Deallocates a `ByteGrid`. +/// Deallocates a `Cp437Grid`. /// /// # Safety /// /// The caller has to make sure that: /// -/// - `this` points to a valid `ByteGrid` +/// - `this` points to a valid `Cp437Grid` /// - `this` is not used concurrently or after this call /// - `this` was not passed to another consuming function, e.g. to create a `Command` #[no_mangle] -pub unsafe extern "C" fn sp_byte_grid_dealloc(this: *mut ByteGrid) { +pub unsafe extern "C" fn sp_cp437_grid_dealloc(this: *mut CCp437Grid) { _ = Box::from_raw(this); } @@ -97,18 +101,18 @@ pub unsafe extern "C" fn sp_byte_grid_dealloc(this: *mut ByteGrid) { /// /// The caller has to make sure that: /// -/// - `this` points to a valid `ByteGrid` +/// - `this` points to a valid `Cp437Grid` /// - `this` is not written to concurrently #[no_mangle] -pub unsafe extern "C" fn sp_byte_grid_get( - this: *const ByteGrid, +pub unsafe extern "C" fn sp_cp437_grid_get( + this: *const CCp437Grid, x: usize, y: usize, ) -> u8 { - (*this).get(x, y) + (*this).0.get(x, y) } -/// Sets the value of the specified position in the `ByteGrid`. +/// Sets the value of the specified position in the `Cp437Grid`. /// /// # Arguments /// @@ -129,16 +133,16 @@ pub unsafe extern "C" fn sp_byte_grid_get( /// - `this` points to a valid `BitVec` /// - `this` is not written to or read from concurrently #[no_mangle] -pub unsafe extern "C" fn sp_byte_grid_set( - this: *mut ByteGrid, +pub unsafe extern "C" fn sp_cp437_grid_set( + this: *mut CCp437Grid, x: usize, y: usize, value: u8, ) { - (*this).set(x, y, value); + (*this).0.set(x, y, value); } -/// Sets the value of all cells in the `ByteGrid`. +/// Sets the value of all cells in the `Cp437Grid`. /// /// # Arguments /// @@ -149,14 +153,14 @@ pub unsafe extern "C" fn sp_byte_grid_set( /// /// The caller has to make sure that: /// -/// - `this` points to a valid `ByteGrid` +/// - `this` points to a valid `Cp437Grid` /// - `this` is not written to or read from concurrently #[no_mangle] -pub unsafe extern "C" fn sp_byte_grid_fill(this: *mut ByteGrid, value: u8) { - (*this).fill(value); +pub unsafe extern "C" fn sp_cp437_grid_fill(this: *mut CCp437Grid, value: u8) { + (*this).0.fill(value); } -/// Gets the width of the `ByteGrid` instance. +/// Gets the width of the `Cp437Grid` instance. /// /// # Arguments /// @@ -166,13 +170,13 @@ pub unsafe extern "C" fn sp_byte_grid_fill(this: *mut ByteGrid, value: u8) { /// /// The caller has to make sure that: /// -/// - `this` points to a valid `ByteGrid` +/// - `this` points to a valid `Cp437Grid` #[no_mangle] -pub unsafe extern "C" fn sp_byte_grid_width(this: *const ByteGrid) -> usize { - (*this).width() +pub unsafe extern "C" fn sp_cp437_grid_width(this: *const CCp437Grid) -> usize { + (*this).0.width() } -/// Gets the height of the `ByteGrid` instance. +/// Gets the height of the `Cp437Grid` instance. /// /// # Arguments /// @@ -182,26 +186,26 @@ pub unsafe extern "C" fn sp_byte_grid_width(this: *const ByteGrid) -> usize { /// /// The caller has to make sure that: /// -/// - `this` points to a valid `ByteGrid` +/// - `this` points to a valid `Cp437Grid` #[no_mangle] -pub unsafe extern "C" fn sp_byte_grid_height(this: *const ByteGrid) -> usize { - (*this).height() +pub unsafe extern "C" fn sp_cp437_grid_height(this: *const CCp437Grid) -> usize { + (*this).0.height() } -/// Gets an unsafe reference to the data of the `ByteGrid` instance. +/// Gets an unsafe reference to the data of the `Cp437Grid` instance. /// /// ## Safety /// /// The caller has to make sure that: /// -/// - `this` points to a valid `ByteGrid` -/// - the returned memory range is never accessed after the passed `ByteGrid` has been freed -/// - the returned memory range is never accessed concurrently, either via the `ByteGrid` or directly +/// - `this` points to a valid `Cp437Grid` +/// - the returned memory range is never accessed after the passed `Cp437Grid` has been freed +/// - the returned memory range is never accessed concurrently, either via the `Cp437Grid` or directly #[no_mangle] -pub unsafe extern "C" fn sp_byte_grid_unsafe_data_ref( - this: *mut ByteGrid, +pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref( + this: *mut CCp437Grid, ) -> CByteSlice { - let data = (*this).data_ref_mut(); + let data = (*this).0.data_ref_mut(); CByteSlice { start: data.as_mut_ptr_range().start, length: data.len(), diff --git a/crates/servicepoint_binding_c/src/lib.rs b/crates/servicepoint_binding_c/src/lib.rs index 7aaa92e..0939b66 100644 --- a/crates/servicepoint_binding_c/src/lib.rs +++ b/crates/servicepoint_binding_c/src/lib.rs @@ -9,7 +9,7 @@ pub use crate::c_slice::CByteSlice; pub mod bit_vec; -pub mod byte_grid; +pub mod brightness_grid; pub mod command; @@ -19,7 +19,9 @@ pub mod packet; pub mod pixel_grid; +pub mod c_slice; + +pub mod cp437_grid; + /// The minimum time needed for the display to refresh the screen in ms. pub const FRAME_PACING_MS: u32 = servicepoint::FRAME_PACING.as_millis() as u32; - -pub mod c_slice; diff --git a/crates/servicepoint_binding_cs/ServicePoint/BindGen/ServicePoint.g.cs b/crates/servicepoint_binding_cs/ServicePoint/BindGen/ServicePoint.g.cs index 1738b8f..2575057 100644 --- a/crates/servicepoint_binding_cs/ServicePoint/BindGen/ServicePoint.g.cs +++ b/crates/servicepoint_binding_cs/ServicePoint/BindGen/ServicePoint.g.cs @@ -61,45 +61,85 @@ namespace ServicePoint.BindGen [DllImport(__DllName, EntryPoint = "sp_bit_vec_unsafe_data_ref", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] public static extern CByteSlice sp_bit_vec_unsafe_data_ref(CBitVec* @this); - /// Creates a new `ByteGrid` with the specified dimensions. returns: `ByteGrid` initialized to 0. # Safety The caller has to make sure that: - the returned instance is freed in some way, either by using a consuming function or by explicitly calling `sp_byte_grid_dealloc`. - [DllImport(__DllName, EntryPoint = "sp_byte_grid_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern PrimitiveGrid* sp_byte_grid_new(nuint width, nuint height); + /// Creates a new `BrightnessGrid` with the specified dimensions. returns: `BrightnessGrid` initialized to 0. # Safety The caller has to make sure that: - the returned instance is freed in some way, either by using a consuming function or by explicitly calling `sp_brightness_grid_dealloc`. + [DllImport(__DllName, EntryPoint = "sp_brightness_grid_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern CBrightnessGrid* sp_brightness_grid_new(nuint width, nuint height); - /// Loads a `ByteGrid` with the specified dimensions from the provided data. # Panics When the provided `data_length` is not sufficient for the `height` and `width` # Safety The caller has to make sure that: - `data` points to a valid memory location of at least `data_length` bytes in size. - the returned instance is freed in some way, either by using a consuming function or by explicitly calling `sp_byte_grid_dealloc`. - [DllImport(__DllName, EntryPoint = "sp_byte_grid_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern PrimitiveGrid* sp_byte_grid_load(nuint width, nuint height, byte* data, nuint data_length); + /// Loads a `BrightnessGrid` with the specified dimensions from the provided data. # Panics When the provided `data_length` is not sufficient for the `height` and `width` # Safety The caller has to make sure that: - `data` points to a valid memory location of at least `data_length` bytes in size. - the returned instance is freed in some way, either by using a consuming function or by explicitly calling `sp_brightness_grid_dealloc`. + [DllImport(__DllName, EntryPoint = "sp_brightness_grid_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern CBrightnessGrid* sp_brightness_grid_load(nuint width, nuint height, byte* data, nuint data_length); - /// Clones a `ByteGrid`. # Safety The caller has to make sure that: - `this` points to a valid `ByteGrid` - `this` is not written to concurrently - the returned instance is freed in some way, either by using a consuming function or by explicitly calling `sp_byte_grid_dealloc`. - [DllImport(__DllName, EntryPoint = "sp_byte_grid_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern PrimitiveGrid* sp_byte_grid_clone(PrimitiveGrid* @this); + /// Clones a `BrightnessGrid`. # Safety The caller has to make sure that: - `this` points to a valid `BrightnessGrid` - `this` is not written to concurrently - the returned instance is freed in some way, either by using a consuming function or by explicitly calling `sp_brightness_grid_dealloc`. + [DllImport(__DllName, EntryPoint = "sp_brightness_grid_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern CBrightnessGrid* sp_brightness_grid_clone(CBrightnessGrid* @this); - /// Deallocates a `ByteGrid`. # Safety The caller has to make sure that: - `this` points to a valid `ByteGrid` - `this` is not used concurrently or after this call - `this` was not passed to another consuming function, e.g. to create a `Command` - [DllImport(__DllName, EntryPoint = "sp_byte_grid_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern void sp_byte_grid_dealloc(PrimitiveGrid* @this); + /// Deallocates a `BrightnessGrid`. # Safety The caller has to make sure that: - `this` points to a valid `BrightnessGrid` - `this` is not used concurrently or after this call - `this` was not passed to another consuming function, e.g. to create a `Command` + [DllImport(__DllName, EntryPoint = "sp_brightness_grid_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void sp_brightness_grid_dealloc(CBrightnessGrid* @this); - /// Gets the current value at the specified position. # Arguments * `this`: instance to read from * `x` and `y`: position of the cell to read # Panics When accessing `x` or `y` out of bounds. # Safety The caller has to make sure that: - `this` points to a valid `ByteGrid` - `this` is not written to concurrently - [DllImport(__DllName, EntryPoint = "sp_byte_grid_get", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern byte sp_byte_grid_get(PrimitiveGrid* @this, nuint x, nuint y); + /// Gets the current value at the specified position. # Arguments * `this`: instance to read from * `x` and `y`: position of the cell to read # Panics When accessing `x` or `y` out of bounds. # Safety The caller has to make sure that: - `this` points to a valid `BrightnessGrid` - `this` is not written to concurrently + [DllImport(__DllName, EntryPoint = "sp_brightness_grid_get", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern byte sp_brightness_grid_get(CBrightnessGrid* @this, nuint x, nuint y); - /// Sets the value of the specified position in the `ByteGrid`. # Arguments * `this`: instance to write to * `x` and `y`: position of the cell * `value`: the value to write to the cell returns: old value of the cell # Panics When accessing `x` or `y` out of bounds. # Safety The caller has to make sure that: - `this` points to a valid `BitVec` - `this` is not written to or read from concurrently - [DllImport(__DllName, EntryPoint = "sp_byte_grid_set", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern void sp_byte_grid_set(PrimitiveGrid* @this, nuint x, nuint y, byte value); + /// Sets the value of the specified position in the `BrightnessGrid`. # Arguments * `this`: instance to write to * `x` and `y`: position of the cell * `value`: the value to write to the cell returns: old value of the cell # Panics When accessing `x` or `y` out of bounds. # Safety The caller has to make sure that: - `this` points to a valid `BitVec` - `this` is not written to or read from concurrently + [DllImport(__DllName, EntryPoint = "sp_brightness_grid_set", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void sp_brightness_grid_set(CBrightnessGrid* @this, nuint x, nuint y, byte value); - /// Sets the value of all cells in the `ByteGrid`. # Arguments * `this`: instance to write to * `value`: the value to set all cells to # Safety The caller has to make sure that: - `this` points to a valid `ByteGrid` - `this` is not written to or read from concurrently - [DllImport(__DllName, EntryPoint = "sp_byte_grid_fill", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern void sp_byte_grid_fill(PrimitiveGrid* @this, byte value); + /// Sets the value of all cells in the `BrightnessGrid`. # Arguments * `this`: instance to write to * `value`: the value to set all cells to # Safety The caller has to make sure that: - `this` points to a valid `BrightnessGrid` - `this` is not written to or read from concurrently + [DllImport(__DllName, EntryPoint = "sp_brightness_grid_fill", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void sp_brightness_grid_fill(CBrightnessGrid* @this, byte value); - /// Gets the width of the `ByteGrid` instance. # Arguments * `this`: instance to read from # Safety The caller has to make sure that: - `this` points to a valid `ByteGrid` - [DllImport(__DllName, EntryPoint = "sp_byte_grid_width", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern nuint sp_byte_grid_width(PrimitiveGrid* @this); + /// Gets the width of the `BrightnessGrid` instance. # Arguments * `this`: instance to read from # Safety The caller has to make sure that: - `this` points to a valid `BrightnessGrid` + [DllImport(__DllName, EntryPoint = "sp_brightness_grid_width", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern nuint sp_brightness_grid_width(CBrightnessGrid* @this); - /// Gets the height of the `ByteGrid` instance. # Arguments * `this`: instance to read from # Safety The caller has to make sure that: - `this` points to a valid `ByteGrid` - [DllImport(__DllName, EntryPoint = "sp_byte_grid_height", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern nuint sp_byte_grid_height(PrimitiveGrid* @this); + /// Gets the height of the `BrightnessGrid` instance. # Arguments * `this`: instance to read from # Safety The caller has to make sure that: - `this` points to a valid `BrightnessGrid` + [DllImport(__DllName, EntryPoint = "sp_brightness_grid_height", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern nuint sp_brightness_grid_height(CBrightnessGrid* @this); - /// Gets an unsafe reference to the data of the `ByteGrid` instance. ## Safety The caller has to make sure that: - `this` points to a valid `ByteGrid` - the returned memory range is never accessed after the passed `ByteGrid` has been freed - the returned memory range is never accessed concurrently, either via the `ByteGrid` or directly - [DllImport(__DllName, EntryPoint = "sp_byte_grid_unsafe_data_ref", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern CByteSlice sp_byte_grid_unsafe_data_ref(PrimitiveGrid* @this); + /// Gets an unsafe reference to the data of the `BrightnessGrid` instance. ## Safety The caller has to make sure that: - `this` points to a valid `BrightnessGrid` - the returned memory range is never accessed after the passed `BrightnessGrid` has been freed - the returned memory range is never accessed concurrently, either via the `BrightnessGrid` or directly + [DllImport(__DllName, EntryPoint = "sp_brightness_grid_unsafe_data_ref", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern CByteSlice sp_brightness_grid_unsafe_data_ref(CBrightnessGrid* @this); + + /// Creates a new `Cp437Grid` with the specified dimensions. returns: `Cp437Grid` initialized to 0. # Safety The caller has to make sure that: - the returned instance is freed in some way, either by using a consuming function or by explicitly calling `sp_cp437_grid_dealloc`. + [DllImport(__DllName, EntryPoint = "sp_cp437_grid_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern CCp437Grid* sp_cp437_grid_new(nuint width, nuint height); + + /// Loads a `Cp437Grid` with the specified dimensions from the provided data. # Panics When the provided `data_length` is not sufficient for the `height` and `width` # Safety The caller has to make sure that: - `data` points to a valid memory location of at least `data_length` bytes in size. - the returned instance is freed in some way, either by using a consuming function or by explicitly calling `sp_cp437_grid_dealloc`. + [DllImport(__DllName, EntryPoint = "sp_cp437_grid_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern CCp437Grid* sp_cp437_grid_load(nuint width, nuint height, byte* data, nuint data_length); + + /// Clones a `Cp437Grid`. # Safety The caller has to make sure that: - `this` points to a valid `Cp437Grid` - `this` is not written to concurrently - the returned instance is freed in some way, either by using a consuming function or by explicitly calling `sp_cp437_grid_dealloc`. + [DllImport(__DllName, EntryPoint = "sp_cp437_grid_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern CCp437Grid* sp_cp437_grid_clone(CCp437Grid* @this); + + /// Deallocates a `Cp437Grid`. # Safety The caller has to make sure that: - `this` points to a valid `Cp437Grid` - `this` is not used concurrently or after this call - `this` was not passed to another consuming function, e.g. to create a `Command` + [DllImport(__DllName, EntryPoint = "sp_cp437_grid_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void sp_cp437_grid_dealloc(CCp437Grid* @this); + + /// Gets the current value at the specified position. # Arguments * `this`: instance to read from * `x` and `y`: position of the cell to read # Panics When accessing `x` or `y` out of bounds. # Safety The caller has to make sure that: - `this` points to a valid `Cp437Grid` - `this` is not written to concurrently + [DllImport(__DllName, EntryPoint = "sp_cp437_grid_get", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern byte sp_cp437_grid_get(CCp437Grid* @this, nuint x, nuint y); + + /// Sets the value of the specified position in the `Cp437Grid`. # Arguments * `this`: instance to write to * `x` and `y`: position of the cell * `value`: the value to write to the cell returns: old value of the cell # Panics When accessing `x` or `y` out of bounds. # Safety The caller has to make sure that: - `this` points to a valid `BitVec` - `this` is not written to or read from concurrently + [DllImport(__DllName, EntryPoint = "sp_cp437_grid_set", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void sp_cp437_grid_set(CCp437Grid* @this, nuint x, nuint y, byte value); + + /// Sets the value of all cells in the `Cp437Grid`. # Arguments * `this`: instance to write to * `value`: the value to set all cells to # Safety The caller has to make sure that: - `this` points to a valid `Cp437Grid` - `this` is not written to or read from concurrently + [DllImport(__DllName, EntryPoint = "sp_cp437_grid_fill", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void sp_cp437_grid_fill(CCp437Grid* @this, byte value); + + /// Gets the width of the `Cp437Grid` instance. # Arguments * `this`: instance to read from # Safety The caller has to make sure that: - `this` points to a valid `Cp437Grid` + [DllImport(__DllName, EntryPoint = "sp_cp437_grid_width", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern nuint sp_cp437_grid_width(CCp437Grid* @this); + + /// Gets the height of the `Cp437Grid` instance. # Arguments * `this`: instance to read from # Safety The caller has to make sure that: - `this` points to a valid `Cp437Grid` + [DllImport(__DllName, EntryPoint = "sp_cp437_grid_height", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern nuint sp_cp437_grid_height(CCp437Grid* @this); + + /// Gets an unsafe reference to the data of the `Cp437Grid` instance. ## Safety The caller has to make sure that: - `this` points to a valid `Cp437Grid` - the returned memory range is never accessed after the passed `Cp437Grid` has been freed - the returned memory range is never accessed concurrently, either via the `Cp437Grid` or directly + [DllImport(__DllName, EntryPoint = "sp_cp437_grid_unsafe_data_ref", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern CByteSlice sp_cp437_grid_unsafe_data_ref(CCp437Grid* @this); /// Tries to turn a `Packet` into a `Command`. The packet is deallocated in the process. 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`. [DllImport(__DllName, EntryPoint = "sp_command_try_from_packet", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] @@ -231,6 +271,16 @@ namespace ServicePoint.BindGen { } + [StructLayout(LayoutKind.Sequential)] + public unsafe partial struct CBrightnessGrid + { + } + + [StructLayout(LayoutKind.Sequential)] + public unsafe partial struct CCp437Grid + { + } + [StructLayout(LayoutKind.Sequential)] public unsafe partial struct CByteSlice { diff --git a/crates/servicepoint_binding_cs/build.rs b/crates/servicepoint_binding_cs/build.rs index b9b71d2..0cb37b0 100644 --- a/crates/servicepoint_binding_cs/build.rs +++ b/crates/servicepoint_binding_cs/build.rs @@ -5,7 +5,8 @@ fn main() { println!("cargo::rerun-if-changed=build.rs"); csbindgen::Builder::default() .input_extern_file("../servicepoint_binding_c/src/bit_vec.rs") - .input_extern_file("../servicepoint_binding_c/src/byte_grid.rs") + .input_extern_file("../servicepoint_binding_c/src/brightness_grid.rs") + .input_extern_file("../servicepoint_binding_c/src/cp437_grid.rs") .input_extern_file("../servicepoint_binding_c/src/command.rs") .input_extern_file("../servicepoint_binding_c/src/connection.rs") .input_extern_file("../servicepoint_binding_c/src/pixel_grid.rs")