separate types for c api

This commit is contained in:
Vinzenz Schroeter 2024-06-23 15:42:15 +02:00
parent c554fbd800
commit 555d917d96
11 changed files with 648 additions and 162 deletions

View file

@ -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::<Brightness>(), 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(),
}
}

View file

@ -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.

View file

@ -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<u8>;
/// 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(),

View file

@ -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;