wip remove newtypes

This commit is contained in:
Vinzenz Schroeter 2025-04-12 10:54:47 +02:00
parent 52f6f3f3fe
commit 1a58294f88
14 changed files with 497 additions and 430 deletions

View file

@ -1,23 +1,23 @@
//! C functions for interacting with [SPBitmap]s
//!
//! prefix `sp_bitmap_`
//!
//! A grid of pixels.
//!
//! # Examples
//!
//! ```C
//! Cp437Grid grid = sp_bitmap_new(8, 3);
//! sp_bitmap_fill(grid, true);
//! sp_bitmap_set(grid, 0, 0, false);
//! sp_bitmap_free(grid);
//! ```
use servicepoint::{DataRef, Grid};
use std::ptr::NonNull;
use crate::byte_slice::SPByteSlice;
/// A grid of pixels.
///
/// # Examples
///
/// ```C
/// Cp437Grid grid = sp_bitmap_new(8, 3);
/// sp_bitmap_fill(grid, true);
/// sp_bitmap_set(grid, 0, 0, false);
/// sp_bitmap_free(grid);
/// ```
pub struct SPBitmap(pub(crate) servicepoint::Bitmap);
/// Creates a new [SPBitmap] with the specified dimensions.
///
@ -44,9 +44,9 @@ pub struct SPBitmap(pub(crate) servicepoint::Bitmap);
pub unsafe extern "C" fn sp_bitmap_new(
width: usize,
height: usize,
) -> *mut SPBitmap {
) -> *mut servicepoint::Bitmap {
if let Some(bitmap) = servicepoint::Bitmap::new(width, height) {
Box::leak(Box::new(SPBitmap(bitmap)))
Box::leak(Box::new(bitmap))
} else {
std::ptr::null_mut()
}
@ -63,8 +63,8 @@ pub unsafe extern "C" fn sp_bitmap_new(
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling [sp_bitmap_free].
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_new_screen_sized() -> NonNull<SPBitmap> {
let result = Box::new(SPBitmap(servicepoint::Bitmap::max_sized()));
pub unsafe extern "C" fn sp_bitmap_new_screen_sized() -> NonNull<servicepoint::Bitmap> {
let result = Box::new(servicepoint::Bitmap::max_sized());
NonNull::from(Box::leak(result))
}
@ -101,11 +101,11 @@ pub unsafe extern "C" fn sp_bitmap_load(
height: usize,
data: *const u8,
data_length: usize,
) -> *mut SPBitmap {
) -> *mut servicepoint::Bitmap {
assert!(!data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, data_length) };
if let Ok(bitmap) = servicepoint::Bitmap::load(width, height, data) {
Box::leak(Box::new(SPBitmap(bitmap)))
Box::leak(Box::new(bitmap))
} else {
std::ptr::null_mut()
}
@ -129,10 +129,10 @@ pub unsafe extern "C" fn sp_bitmap_load(
/// by explicitly calling `sp_bitmap_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_clone(
bitmap: *const SPBitmap,
) -> NonNull<SPBitmap> {
bitmap: *const servicepoint::Bitmap,
) -> NonNull<servicepoint::Bitmap> {
assert!(!bitmap.is_null());
let result = Box::new(SPBitmap(unsafe { (*bitmap).0.clone() }));
let result = Box::new(unsafe { (*bitmap).clone() });
NonNull::from(Box::leak(result))
}
@ -152,7 +152,7 @@ pub unsafe extern "C" fn sp_bitmap_clone(
///
/// [SPCommand]: [crate::SPCommand]
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_free(bitmap: *mut SPBitmap) {
pub unsafe extern "C" fn sp_bitmap_free(bitmap: *mut servicepoint::Bitmap) {
assert!(!bitmap.is_null());
_ = unsafe { Box::from_raw(bitmap) };
}
@ -177,12 +177,12 @@ pub unsafe extern "C" fn sp_bitmap_free(bitmap: *mut SPBitmap) {
/// - `bitmap` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_get(
bitmap: *const SPBitmap,
bitmap: *const servicepoint::Bitmap,
x: usize,
y: usize,
) -> bool {
assert!(!bitmap.is_null());
unsafe { (*bitmap).0.get(x, y) }
unsafe { (*bitmap).get(x, y) }
}
/// Sets the value of the specified position in the [SPBitmap].
@ -208,13 +208,13 @@ pub unsafe extern "C" fn sp_bitmap_get(
/// - `bitmap` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_set(
bitmap: *mut SPBitmap,
bitmap: *mut servicepoint::Bitmap,
x: usize,
y: usize,
value: bool,
) {
assert!(!bitmap.is_null());
unsafe { (*bitmap).0.set(x, y, value) };
unsafe { (*bitmap).set(x, y, value) };
}
/// Sets the state of all pixels in the [SPBitmap].
@ -235,9 +235,9 @@ pub unsafe extern "C" fn sp_bitmap_set(
/// - `bitmap` points to a valid [SPBitmap]
/// - `bitmap` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_fill(bitmap: *mut SPBitmap, value: bool) {
pub unsafe extern "C" fn sp_bitmap_fill(bitmap: *mut servicepoint::Bitmap, value: bool) {
assert!(!bitmap.is_null());
unsafe { (*bitmap).0.fill(value) };
unsafe { (*bitmap).fill(value) };
}
/// Gets the width in pixels of the [SPBitmap] instance.
@ -256,9 +256,9 @@ pub unsafe extern "C" fn sp_bitmap_fill(bitmap: *mut SPBitmap, value: bool) {
///
/// - `bitmap` points to a valid [SPBitmap]
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_width(bitmap: *const SPBitmap) -> usize {
pub unsafe extern "C" fn sp_bitmap_width(bitmap: *const servicepoint::Bitmap) -> usize {
assert!(!bitmap.is_null());
unsafe { (*bitmap).0.width() }
unsafe { (*bitmap).width() }
}
/// Gets the height in pixels of the [SPBitmap] instance.
@ -277,9 +277,9 @@ pub unsafe extern "C" fn sp_bitmap_width(bitmap: *const SPBitmap) -> usize {
///
/// - `bitmap` points to a valid [SPBitmap]
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_height(bitmap: *const SPBitmap) -> usize {
pub unsafe extern "C" fn sp_bitmap_height(bitmap: *const servicepoint::Bitmap) -> usize {
assert!(!bitmap.is_null());
unsafe { (*bitmap).0.height() }
unsafe { (*bitmap).height() }
}
/// Gets an unsafe reference to the data of the [SPBitmap] instance.
@ -297,10 +297,10 @@ pub unsafe extern "C" fn sp_bitmap_height(bitmap: *const SPBitmap) -> usize {
/// - the returned memory range is never accessed concurrently, either via the [SPBitmap] or directly
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_unsafe_data_ref(
bitmap: *mut SPBitmap,
bitmap: *mut servicepoint::Bitmap,
) -> SPByteSlice {
assert!(!bitmap.is_null());
let data = unsafe { (*bitmap).0.data_ref_mut() };
let data = unsafe { (*bitmap).data_ref_mut() };
SPByteSlice {
start: NonNull::new(data.as_mut_ptr_range().start).unwrap(),
length: data.len(),

View file

@ -13,15 +13,15 @@ use std::ptr::NonNull;
/// sp_bitvec_set(vec, 5, true);
/// sp_bitvec_free(vec);
/// ```
pub struct SPBitVec(servicepoint::BitVec);
pub struct SPBitVec(servicepoint::BitVecU8Msb0);
impl From<servicepoint::BitVec> for SPBitVec {
fn from(actual: servicepoint::BitVec) -> Self {
impl From<servicepoint::BitVecU8Msb0> for SPBitVec {
fn from(actual: servicepoint::BitVecU8Msb0) -> Self {
Self(actual)
}
}
impl From<SPBitVec> for servicepoint::BitVec {
impl From<SPBitVec> for servicepoint::BitVecU8Msb0 {
fn from(value: SPBitVec) -> Self {
value.0
}
@ -53,7 +53,7 @@ impl Clone for SPBitVec {
/// by explicitly calling `sp_bitvec_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_new(size: usize) -> NonNull<SPBitVec> {
let result = Box::new(SPBitVec(servicepoint::BitVec::repeat(false, size)));
let result = Box::new(SPBitVec(servicepoint::BitVecU8Msb0::repeat(false, size)));
NonNull::from(Box::leak(result))
}
@ -80,7 +80,7 @@ pub unsafe extern "C" fn sp_bitvec_load(
) -> NonNull<SPBitVec> {
assert!(!data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let result = Box::new(SPBitVec(servicepoint::BitVec::from_slice(data)));
let result = Box::new(SPBitVec(servicepoint::BitVecU8Msb0::from_slice(data)));
NonNull::from(Box::leak(result))
}

View file

@ -1,9 +1,26 @@
//! C functions for interacting with [SPBrightnessGrid]s
//!
//! prefix `sp_brightness_grid_`
//!
//!
//! A grid containing brightness values.
//!
//! # Examples
//! ```C
//! SPConnection connection = sp_connection_open("127.0.0.1:2342");
//! if (connection == NULL)
//! return 1;
//!
//! SPBrightnessGrid grid = sp_brightness_grid_new(2, 2);
//! sp_brightness_grid_set(grid, 0, 0, 0);
//! sp_brightness_grid_set(grid, 1, 1, 10);
//!
//! SPCommand command = sp_command_char_brightness(grid);
//! sp_connection_free(connection);
//! ```
use crate::SPByteSlice;
use servicepoint::{DataRef, Grid};
use servicepoint::{BrightnessGrid, DataRef, Grid};
use std::convert::Into;
use std::mem::transmute;
use std::ptr::NonNull;
@ -15,23 +32,6 @@ pub const SP_BRIGHTNESS_MAX: u8 = 11;
/// Count of possible brightness values
pub const SP_BRIGHTNESS_LEVELS: u8 = 12;
/// A grid containing brightness values.
///
/// # Examples
/// ```C
/// SPConnection connection = sp_connection_open("127.0.0.1:2342");
/// if (connection == NULL)
/// return 1;
///
/// SPBrightnessGrid grid = sp_brightness_grid_new(2, 2);
/// sp_brightness_grid_set(grid, 0, 0, 0);
/// sp_brightness_grid_set(grid, 1, 1, 10);
///
/// SPCommand command = sp_command_char_brightness(grid);
/// sp_connection_free(connection);
/// ```
#[derive(Clone)]
pub struct SPBrightnessGrid(pub(crate) servicepoint::BrightnessGrid);
/// Creates a new [SPBrightnessGrid] with the specified dimensions.
///
@ -47,10 +47,10 @@ pub struct SPBrightnessGrid(pub(crate) servicepoint::BrightnessGrid);
pub unsafe extern "C" fn sp_brightness_grid_new(
width: usize,
height: usize,
) -> NonNull<SPBrightnessGrid> {
let result = Box::new(SPBrightnessGrid(servicepoint::BrightnessGrid::new(
) -> NonNull<BrightnessGrid> {
let result = Box::new(servicepoint::BrightnessGrid::new(
width, height,
)));
));
NonNull::from(Box::leak(result))
}
@ -77,7 +77,7 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
height: usize,
data: *const u8,
data_length: usize,
) -> *mut SPBrightnessGrid {
) -> *mut BrightnessGrid {
assert!(!data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let grid = match servicepoint::ByteGrid::load(width, height, data) {
@ -85,7 +85,7 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
Some(grid) => grid,
};
if let Ok(grid) = servicepoint::BrightnessGrid::try_from(grid) {
Box::leak(Box::new(SPBrightnessGrid(grid)))
Box::leak(Box::new(grid))
} else {
std::ptr::null_mut()
}
@ -113,8 +113,8 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
/// by explicitly calling `sp_brightness_grid_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_clone(
brightness_grid: *const SPBrightnessGrid,
) -> NonNull<SPBrightnessGrid> {
brightness_grid: *const BrightnessGrid,
) -> NonNull<BrightnessGrid> {
assert!(!brightness_grid.is_null());
let result = Box::new(unsafe { (*brightness_grid).clone() });
NonNull::from(Box::leak(result))
@ -141,7 +141,7 @@ pub unsafe extern "C" fn sp_brightness_grid_clone(
/// [SPCommand]: [crate::SPCommand]
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_free(
brightness_grid: *mut SPBrightnessGrid,
brightness_grid: *mut BrightnessGrid,
) {
assert!(!brightness_grid.is_null());
_ = unsafe { Box::from_raw(brightness_grid) };
@ -169,12 +169,12 @@ pub unsafe extern "C" fn sp_brightness_grid_free(
/// - `brightness_grid` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_get(
brightness_grid: *const SPBrightnessGrid,
brightness_grid: *const BrightnessGrid,
x: usize,
y: usize,
) -> u8 {
assert!(!brightness_grid.is_null());
unsafe { (*brightness_grid).0.get(x, y) }.into()
unsafe { (*brightness_grid).get(x, y) }.into()
}
/// Sets the value of the specified position in the [SPBrightnessGrid].
@ -201,7 +201,7 @@ pub unsafe extern "C" fn sp_brightness_grid_get(
/// - `brightness_grid` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_set(
brightness_grid: *mut SPBrightnessGrid,
brightness_grid: *mut BrightnessGrid,
x: usize,
y: usize,
value: u8,
@ -209,7 +209,7 @@ pub unsafe extern "C" fn sp_brightness_grid_set(
assert!(!brightness_grid.is_null());
let brightness = servicepoint::Brightness::try_from(value)
.expect("invalid brightness value");
unsafe { (*brightness_grid).0.set(x, y, brightness) };
unsafe { (*brightness_grid).set(x, y, brightness) };
}
/// Sets the value of all cells in the [SPBrightnessGrid].
@ -232,13 +232,13 @@ pub unsafe extern "C" fn sp_brightness_grid_set(
/// - `brightness_grid` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_fill(
brightness_grid: *mut SPBrightnessGrid,
brightness_grid: *mut BrightnessGrid,
value: u8,
) {
assert!(!brightness_grid.is_null());
let brightness = servicepoint::Brightness::try_from(value)
.expect("invalid brightness value");
unsafe { (*brightness_grid).0.fill(brightness) };
unsafe { (*brightness_grid).fill(brightness) };
}
/// Gets the width of the [SPBrightnessGrid] instance.
@ -260,10 +260,10 @@ pub unsafe extern "C" fn sp_brightness_grid_fill(
/// - `brightness_grid` points to a valid [SPBrightnessGrid]
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_width(
brightness_grid: *const SPBrightnessGrid,
brightness_grid: *const BrightnessGrid,
) -> usize {
assert!(!brightness_grid.is_null());
unsafe { (*brightness_grid).0.width() }
unsafe { (*brightness_grid).width() }
}
/// Gets the height of the [SPBrightnessGrid] instance.
@ -285,10 +285,10 @@ pub unsafe extern "C" fn sp_brightness_grid_width(
/// - `brightness_grid` points to a valid [SPBrightnessGrid]
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_height(
brightness_grid: *const SPBrightnessGrid,
brightness_grid: *const BrightnessGrid,
) -> usize {
assert!(!brightness_grid.is_null());
unsafe { (*brightness_grid).0.height() }
unsafe { (*brightness_grid).height() }
}
/// Gets an unsafe reference to the data of the [SPBrightnessGrid] instance.
@ -312,11 +312,11 @@ pub unsafe extern "C" fn sp_brightness_grid_height(
/// - the returned memory range is never accessed concurrently, either via the [SPBrightnessGrid] or directly
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref(
brightness_grid: *mut SPBrightnessGrid,
brightness_grid: *mut BrightnessGrid,
) -> SPByteSlice {
assert!(!brightness_grid.is_null());
assert_eq!(core::mem::size_of::<servicepoint::Brightness>(), 1);
let data = unsafe { (*brightness_grid).0.data_ref_mut() };
let data = unsafe { (*brightness_grid).data_ref_mut() };
// this assumes more about the memory layout than rust guarantees. yikes!
let data: &mut [u8] = unsafe { transmute(data) };
SPByteSlice {

View file

@ -1,33 +1,26 @@
//! C functions for interacting with [SPCharGrid]s
//!
//! prefix `sp_char_grid_`
//!
//! A C-wrapper for grid containing UTF-8 characters.
//!
//! As the rust [char] type is not FFI-safe, characters are passed in their UTF-32 form as 32bit unsigned integers.
//!
//! The encoding is enforced in most cases by the rust standard library
//! and will panic when provided with illegal characters.
//!
//! # Examples
//!
//! ```C
//! CharGrid grid = sp_char_grid_new(4, 3);
//! sp_char_grid_fill(grid, '?');
//! sp_char_grid_set(grid, 0, 0, '!');
//! sp_char_grid_free(grid);
//! ```
use servicepoint::Grid;
use servicepoint::{CharGrid, Grid};
use std::ptr::NonNull;
/// A C-wrapper for grid containing UTF-8 characters.
///
/// As the rust [char] type is not FFI-safe, characters are passed in their UTF-32 form as 32bit unsigned integers.
///
/// The encoding is enforced in most cases by the rust standard library
/// and will panic when provided with illegal characters.
///
/// # Examples
///
/// ```C
/// CharGrid grid = sp_char_grid_new(4, 3);
/// sp_char_grid_fill(grid, '?');
/// sp_char_grid_set(grid, 0, 0, '!');
/// sp_char_grid_free(grid);
/// ```
pub struct SPCharGrid(pub(crate) servicepoint::CharGrid);
impl Clone for SPCharGrid {
fn clone(&self) -> Self {
SPCharGrid(self.0.clone())
}
}
/// Creates a new [SPCharGrid] with the specified dimensions.
///
/// returns: [SPCharGrid] initialized to 0. Will never return NULL.
@ -42,9 +35,8 @@ impl Clone for SPCharGrid {
pub unsafe extern "C" fn sp_char_grid_new(
width: usize,
height: usize,
) -> NonNull<SPCharGrid> {
let result =
Box::new(SPCharGrid(servicepoint::CharGrid::new(width, height)));
) -> NonNull<CharGrid> {
let result = Box::new(CharGrid::new(width, height));
NonNull::from(Box::leak(result))
}
@ -72,13 +64,14 @@ pub unsafe extern "C" fn sp_char_grid_load(
height: usize,
data: *const u8,
data_length: usize,
) -> NonNull<SPCharGrid> {
) -> NonNull<CharGrid> {
assert!(data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let result = Box::new(SPCharGrid(
servicepoint::CharGrid::load_utf8(width, height, data.to_vec())
// TODO remove unwrap
let result = Box::new(
CharGrid::load_utf8(width, height, data.to_vec())
.unwrap(),
));
);
NonNull::from(Box::leak(result))
}
@ -100,8 +93,8 @@ pub unsafe extern "C" fn sp_char_grid_load(
/// by explicitly calling `sp_char_grid_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_clone(
char_grid: *const SPCharGrid,
) -> NonNull<SPCharGrid> {
char_grid: *const CharGrid,
) -> NonNull<CharGrid> {
assert!(!char_grid.is_null());
let result = Box::new(unsafe { (*char_grid).clone() });
NonNull::from(Box::leak(result))
@ -123,7 +116,7 @@ pub unsafe extern "C" fn sp_char_grid_clone(
///
/// [SPCommand]: [crate::SPCommand]
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut SPCharGrid) {
pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut CharGrid) {
assert!(!char_grid.is_null());
_ = unsafe { Box::from_raw(char_grid) };
}
@ -148,12 +141,12 @@ pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut SPCharGrid) {
/// - `char_grid` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_get(
char_grid: *const SPCharGrid,
char_grid: *const CharGrid,
x: usize,
y: usize,
) -> u32 {
assert!(!char_grid.is_null());
unsafe { (*char_grid).0.get(x, y) as u32 }
unsafe { (*char_grid).get(x, y) as u32 }
}
/// Sets the value of the specified position in the [SPCharGrid].
@ -181,13 +174,13 @@ pub unsafe extern "C" fn sp_char_grid_get(
/// [SPBitVec]: [crate::SPBitVec]
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_set(
char_grid: *mut SPCharGrid,
char_grid: *mut CharGrid,
x: usize,
y: usize,
value: u32,
) {
assert!(!char_grid.is_null());
unsafe { (*char_grid).0.set(x, y, char::from_u32(value).unwrap()) };
unsafe { (*char_grid).set(x, y, char::from_u32(value).unwrap()) };
}
/// Sets the value of all cells in the [SPCharGrid].
@ -209,11 +202,11 @@ pub unsafe extern "C" fn sp_char_grid_set(
/// - `char_grid` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_fill(
char_grid: *mut SPCharGrid,
char_grid: *mut CharGrid,
value: u32,
) {
assert!(!char_grid.is_null());
unsafe { (*char_grid).0.fill(char::from_u32(value).unwrap()) };
unsafe { (*char_grid).fill(char::from_u32(value).unwrap()) };
}
/// Gets the width of the [SPCharGrid] instance.
@ -233,10 +226,10 @@ pub unsafe extern "C" fn sp_char_grid_fill(
/// - `char_grid` points to a valid [SPCharGrid]
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_width(
char_grid: *const SPCharGrid,
char_grid: *const CharGrid,
) -> usize {
assert!(!char_grid.is_null());
unsafe { (*char_grid).0.width() }
unsafe { (*char_grid).width() }
}
/// Gets the height of the [SPCharGrid] instance.
@ -256,8 +249,8 @@ pub unsafe extern "C" fn sp_char_grid_width(
/// - `char_grid` points to a valid [SPCharGrid]
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_height(
char_grid: *const SPCharGrid,
char_grid: *const CharGrid,
) -> usize {
assert!(!char_grid.is_null());
unsafe { (*char_grid).0.height() }
unsafe { (*char_grid).height() }
}

View file

@ -2,11 +2,8 @@
//!
//! prefix `sp_command_`
use crate::{
SPBitVec, SPBitmap, SPBrightnessGrid, SPCharGrid, SPCompressionCode,
SPCp437Grid, SPPacket,
};
use servicepoint::{BinaryOperation, GlobalBrightnessCommand};
use crate::{SPBitVec, SPCompressionCode, SPCp437Grid};
use servicepoint::{BinaryOperation, BrightnessGrid, CharGrid, GlobalBrightnessCommand, Packet, TypedCommand};
use std::ptr::NonNull;
/// A low-level display command.
@ -23,13 +20,7 @@ use std::ptr::NonNull;
/// ```
///
/// [SPConnection]: [crate::SPConnection]
pub struct SPCommand(pub(crate) servicepoint::TypedCommand);
impl Clone for SPCommand {
fn clone(&self) -> Self {
SPCommand(self.0.clone())
}
}
/// Tries to turn a [SPPacket] into a [SPCommand].
///
@ -52,12 +43,12 @@ impl Clone for SPCommand {
/// by explicitly calling `sp_command_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_try_from_packet(
packet: *mut SPPacket,
) -> *mut SPCommand {
packet: *mut Packet,
) -> *mut TypedCommand {
let packet = *unsafe { Box::from_raw(packet) };
match servicepoint::TypedCommand::try_from(packet.0) {
match servicepoint::TypedCommand::try_from(packet) {
Err(_) => std::ptr::null_mut(),
Ok(command) => Box::into_raw(Box::new(SPCommand(command))),
Ok(command) => Box::into_raw(Box::new(command)),
}
}
@ -79,8 +70,8 @@ pub unsafe extern "C" fn sp_command_try_from_packet(
/// by explicitly calling `sp_command_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_clone(
command: *const SPCommand,
) -> NonNull<SPCommand> {
command: *const TypedCommand,
) -> NonNull<TypedCommand> {
assert!(!command.is_null());
let result = Box::new(unsafe { (*command).clone() });
NonNull::from(Box::leak(result))
@ -105,8 +96,8 @@ pub unsafe extern "C" fn sp_command_clone(
/// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_clear() -> NonNull<SPCommand> {
let result = Box::new(SPCommand(servicepoint::ClearCommand.into()));
pub unsafe extern "C" fn sp_command_clear() -> NonNull<TypedCommand> {
let result = Box::new(servicepoint::ClearCommand.into());
NonNull::from(Box::leak(result))
}
@ -123,8 +114,8 @@ pub unsafe extern "C" fn sp_command_clear() -> NonNull<SPCommand> {
/// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull<SPCommand> {
let result = Box::new(SPCommand(servicepoint::HardResetCommand.into()));
pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull<TypedCommand> {
let result = Box::new(servicepoint::HardResetCommand.into());
NonNull::from(Box::leak(result))
}
@ -139,8 +130,8 @@ pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull<SPCommand> {
/// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_fade_out() -> NonNull<SPCommand> {
let result = Box::new(SPCommand(servicepoint::FadeOutCommand.into()));
pub unsafe extern "C" fn sp_command_fade_out() -> NonNull<TypedCommand> {
let result = Box::new(servicepoint::FadeOutCommand.into());
NonNull::from(Box::leak(result))
}
@ -161,11 +152,11 @@ pub unsafe extern "C" fn sp_command_fade_out() -> NonNull<SPCommand> {
#[no_mangle]
pub unsafe extern "C" fn sp_command_brightness(
brightness: u8,
) -> NonNull<SPCommand> {
) -> NonNull<TypedCommand> {
let brightness = servicepoint::Brightness::try_from(brightness)
.expect("invalid brightness");
let result =
Box::new(SPCommand(GlobalBrightnessCommand::from(brightness).into()));
Box::new(GlobalBrightnessCommand::from(brightness).into());
NonNull::from(Box::leak(result))
}
@ -191,17 +182,17 @@ pub unsafe extern "C" fn sp_command_brightness(
pub unsafe extern "C" fn sp_command_char_brightness(
x: usize,
y: usize,
grid: *mut SPBrightnessGrid,
) -> NonNull<SPCommand> {
grid: *mut BrightnessGrid,
) -> NonNull<TypedCommand> {
assert!(!grid.is_null());
let byte_grid = unsafe { *Box::from_raw(grid) };
let result = Box::new(SPCommand(
let grid = unsafe { *Box::from_raw(grid) };
let result = Box::new(
servicepoint::BrightnessGridCommand {
origin: servicepoint::Origin::new(x, y),
grid: byte_grid.0,
grid,
}
.into(),
));
);
NonNull::from(Box::leak(result))
}
@ -235,7 +226,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear(
offset: usize,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
) -> *mut TypedCommand {
unsafe {
sp_command_bitmap_linear_internal(
offset,
@ -276,7 +267,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and(
offset: usize,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
) -> *mut TypedCommand {
unsafe {
sp_command_bitmap_linear_internal(
offset,
@ -317,7 +308,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or(
offset: usize,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
) -> *mut TypedCommand {
unsafe {
sp_command_bitmap_linear_internal(
offset,
@ -358,7 +349,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
offset: usize,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
) -> *mut TypedCommand {
unsafe {
sp_command_bitmap_linear_internal(
offset,
@ -375,22 +366,20 @@ unsafe fn sp_command_bitmap_linear_internal(
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
operation: BinaryOperation,
) -> *mut SPCommand {
) -> *mut TypedCommand {
assert!(!bit_vec.is_null());
let bit_vec = unsafe { *Box::from_raw(bit_vec) };
let compression = match compression.try_into() {
Ok(compression) => compression,
Err(_) => return std::ptr::null_mut(),
};
let command = SPCommand(
servicepoint::BitVecCommand {
let command = servicepoint::BitVecCommand {
offset,
operation,
bitvec: bit_vec.into(),
compression,
}
.into(),
);
.into();
Box::leak(Box::new(command))
}
@ -417,16 +406,16 @@ pub unsafe extern "C" fn sp_command_cp437_data(
x: usize,
y: usize,
grid: *mut SPCp437Grid,
) -> NonNull<SPCommand> {
) -> NonNull<TypedCommand> {
assert!(!grid.is_null());
let grid = *unsafe { Box::from_raw(grid) };
let result = Box::new(SPCommand(
let result = Box::new(
servicepoint::Cp437GridCommand {
origin: servicepoint::Origin::new(x, y),
grid: grid.0,
}
.into(),
));
);
NonNull::from(Box::leak(result))
}
@ -452,17 +441,17 @@ pub unsafe extern "C" fn sp_command_cp437_data(
pub unsafe extern "C" fn sp_command_utf8_data(
x: usize,
y: usize,
grid: *mut SPCharGrid,
) -> NonNull<SPCommand> {
grid: *mut CharGrid,
) -> NonNull<TypedCommand> {
assert!(!grid.is_null());
let grid = unsafe { *Box::from_raw(grid) };
let result = Box::new(SPCommand(
let result = Box::new(
servicepoint::CharGridCommand {
origin: servicepoint::Origin::new(x, y),
grid: grid.0,
grid,
}
.into(),
));
);
NonNull::from(Box::leak(result))
}
@ -490,23 +479,21 @@ pub unsafe extern "C" fn sp_command_utf8_data(
pub unsafe extern "C" fn sp_command_bitmap_linear_win(
x: usize,
y: usize,
bitmap: *mut SPBitmap,
bitmap: *mut servicepoint::Bitmap,
compression: SPCompressionCode,
) -> *mut SPCommand {
) -> *mut TypedCommand {
assert!(!bitmap.is_null());
let bitmap = unsafe { *Box::from_raw(bitmap) }.0;
let bitmap = unsafe { *Box::from_raw(bitmap) };
let compression = match compression.try_into() {
Ok(compression) => compression,
Err(_) => return std::ptr::null_mut(),
};
let command = SPCommand(
servicepoint::BitmapCommand {
let command = servicepoint::BitmapCommand {
origin: servicepoint::Origin::new(x, y),
bitmap,
compression,
}
.into(),
);
.into();
Box::leak(Box::new(command))
}
@ -531,7 +518,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win(
/// - `command` is not used concurrently or after this call
/// - `command` was not passed to another consuming function, e.g. to create a [SPPacket]
#[no_mangle]
pub unsafe extern "C" fn sp_command_free(command: *mut SPCommand) {
pub unsafe extern "C" fn sp_command_free(command: *mut TypedCommand) {
assert!(!command.is_null());
_ = unsafe { Box::from_raw(command) };
}

View file

@ -1,22 +1,20 @@
//! C functions for interacting with [SPConnection]s
//!
//! prefix `sp_connection_`
//!
//! A connection to the display.
//!
//! # Examples
//!
//! ```C
//! CConnection connection = sp_connection_open("172.23.42.29:2342");
//! if (connection != NULL)
//! sp_connection_send_command(connection, sp_command_clear());
//! ```
use crate::{SPCommand, SPPacket};
use servicepoint::Connection;
use servicepoint::{Connection, Packet, TypedCommand, UdpConnection};
use std::ffi::{c_char, CStr};
/// A connection to the display.
///
/// # Examples
///
/// ```C
/// CConnection connection = sp_connection_open("172.23.42.29:2342");
/// if (connection != NULL)
/// sp_connection_send_command(connection, sp_command_clear());
/// ```
pub struct SPConnection(pub(crate) servicepoint::UdpConnection);
/// Creates a new instance of [SPConnection].
///
/// returns: NULL if connection fails, or connected instance
@ -34,19 +32,31 @@ pub struct SPConnection(pub(crate) servicepoint::UdpConnection);
#[no_mangle]
pub unsafe extern "C" fn sp_connection_open(
host: *const c_char,
) -> *mut SPConnection {
) -> *mut UdpConnection {
assert!(!host.is_null());
let host = unsafe { CStr::from_ptr(host) }
.to_str()
.expect("Bad encoding");
let connection = match servicepoint::UdpConnection::open(host) {
let connection = match UdpConnection::open(host) {
Err(_) => return std::ptr::null_mut(),
Ok(value) => value,
};
Box::into_raw(Box::new(SPConnection(connection)))
Box::into_raw(Box::new(connection))
}
//#[no_mangle]
//pub unsafe extern "C" fn sp_connection_open_ipv4(
// host: SocketAddrV4,
//) -> *mut SPConnection {
// let connection = match servicepoint::UdpConnection::open(host) {
// Err(_) => return std::ptr::null_mut(),
// Ok(value) => value,
// };
//
// Box::into_raw(Box::new(SPConnection(connection)))
//}
// /// Creates a new instance of [SPUdpConnection] for testing that does not actually send anything.
// ///
// /// returns: a new instance. Will never return NULL.
@ -83,13 +93,13 @@ pub unsafe extern "C" fn sp_connection_open(
/// - `packet` is not used concurrently or after this call
#[no_mangle]
pub unsafe extern "C" fn sp_connection_send_packet(
connection: *const SPConnection,
packet: *mut SPPacket,
connection: *const UdpConnection,
packet: *mut Packet,
) -> bool {
assert!(!connection.is_null());
assert!(!packet.is_null());
let packet = unsafe { Box::from_raw(packet) };
unsafe { (*connection).0.send((*packet).0) }.is_ok()
unsafe { (*connection).send(*packet) }.is_ok()
}
/// Sends a [SPCommand] to the display using the [SPConnection].
@ -112,13 +122,13 @@ pub unsafe extern "C" fn sp_connection_send_packet(
/// - `command` is not used concurrently or after this call
#[no_mangle]
pub unsafe extern "C" fn sp_connection_send_command(
connection: *const SPConnection,
command: *mut SPCommand,
connection: *const UdpConnection,
command: *mut TypedCommand,
) -> bool {
assert!(!connection.is_null());
assert!(!command.is_null());
let command = (*unsafe { Box::from_raw(command) }).0;
unsafe { (*connection).0.send(command) }.is_ok()
let command = *unsafe { Box::from_raw(command) };
unsafe { (*connection).send(command) }.is_ok()
}
/// Closes and deallocates a [SPConnection].
@ -134,7 +144,7 @@ pub unsafe extern "C" fn sp_connection_send_command(
/// - `connection` points to a valid [SPConnection]
/// - `connection` is not used concurrently or after this call
#[no_mangle]
pub unsafe extern "C" fn sp_connection_free(connection: *mut SPConnection) {
pub unsafe extern "C" fn sp_connection_free(connection: *mut UdpConnection) {
assert!(!connection.is_null());
_ = unsafe { Box::from_raw(connection) };
}

View file

@ -18,6 +18,7 @@ use std::ptr::NonNull;
/// sp_cp437_grid_set(grid, 0, 0, '!');
/// sp_cp437_grid_free(grid);
/// ```
#[repr(transparent)]
pub struct SPCp437Grid(pub(crate) servicepoint::Cp437Grid);
impl Clone for SPCp437Grid {

View file

@ -1,13 +1,12 @@
//! C functions for interacting with [SPPacket]s
//!
//! prefix `sp_packet_`
//!
//!
//! The raw packet
use std::ptr::NonNull;
use crate::SPCommand;
/// The raw packet
pub struct SPPacket(pub(crate) servicepoint::Packet);
use servicepoint::{Header, Packet, TypedCommand};
/// Turns a [SPCommand] into a [SPPacket].
/// The [SPCommand] gets consumed.
@ -28,12 +27,12 @@ pub struct SPPacket(pub(crate) servicepoint::Packet);
/// by explicitly calling `sp_packet_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_packet_from_command(
command: *mut SPCommand,
) -> *mut SPPacket {
command: *mut TypedCommand,
) -> *mut Packet {
assert!(!command.is_null());
let command = unsafe { *Box::from_raw(command) };
if let Ok(packet) = command.0.try_into() {
Box::leak(Box::new(SPPacket(packet)))
if let Ok(packet) = command.try_into() {
Box::leak(Box::new(packet))
} else {
std::ptr::null_mut()
}
@ -59,12 +58,12 @@ pub unsafe extern "C" fn sp_packet_from_command(
pub unsafe extern "C" fn sp_packet_try_load(
data: *const u8,
length: usize,
) -> *mut SPPacket {
) -> *mut Packet {
assert!(!data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, length) };
match servicepoint::Packet::try_from(data) {
Err(_) => std::ptr::null_mut(),
Ok(packet) => Box::into_raw(Box::new(SPPacket(packet))),
Ok(packet) => Box::into_raw(Box::new(packet)),
}
}
@ -94,14 +93,10 @@ pub unsafe extern "C" fn sp_packet_try_load(
/// by explicitly calling [sp_packet_free].
#[no_mangle]
pub unsafe extern "C" fn sp_packet_from_parts(
command_code: u16,
a: u16,
b: u16,
c: u16,
d: u16,
header: Header,
payload: *const u8,
payload_len: usize,
) -> NonNull<SPPacket> {
) -> NonNull<Packet> {
assert_eq!(payload.is_null(), payload_len == 0);
let payload = if payload.is_null() {
@ -112,18 +107,19 @@ pub unsafe extern "C" fn sp_packet_from_parts(
Vec::from(payload)
};
let packet = servicepoint::Packet {
header: servicepoint::Header {
command_code,
a,
b,
c,
d,
},
let packet = Box::new(Packet {
header,
payload,
};
let result = Box::new(SPPacket(packet));
NonNull::from(Box::leak(result))
});
NonNull::from(Box::leak(packet))
}
#[no_mangle]
pub unsafe extern "C" fn sp_packet_get_header(
packet: *const Packet,
) -> Header {
assert!(!packet.is_null());
unsafe { (*packet).header }
}
/// Clones a [SPPacket].
@ -144,10 +140,10 @@ pub unsafe extern "C" fn sp_packet_from_parts(
/// by explicitly calling `sp_packet_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_packet_clone(
packet: *const SPPacket,
) -> NonNull<SPPacket> {
packet: *const Packet,
) -> NonNull<Packet> {
assert!(!packet.is_null());
let result = Box::new(SPPacket(unsafe { (*packet).0.clone() }));
let result = Box::new(unsafe { (*packet).clone() });
NonNull::from(Box::leak(result))
}
@ -164,7 +160,7 @@ pub unsafe extern "C" fn sp_packet_clone(
/// - `packet` points to a valid [SPPacket]
/// - `packet` is not used concurrently or after this call
#[no_mangle]
pub unsafe extern "C" fn sp_packet_free(packet: *mut SPPacket) {
pub unsafe extern "C" fn sp_packet_free(packet: *mut Packet) {
assert!(!packet.is_null());
_ = unsafe { Box::from_raw(packet) }
}