wip remove newtypes, re-export constants from base lib
This commit is contained in:
		
							parent
							
								
									5120e220ec
								
							
						
					
					
						commit
						866fc86b9f
					
				
					 8 changed files with 150 additions and 168 deletions
				
			
		| 
						 | 
				
			
			@ -2,7 +2,6 @@
 | 
			
		|||
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
#[repr(C)]
 | 
			
		||||
/// Represents a span of memory (`&mut [u8]` ) as a struct usable by C code.
 | 
			
		||||
///
 | 
			
		||||
/// You should not create an instance of this type in your C code.
 | 
			
		||||
| 
						 | 
				
			
			@ -16,6 +15,7 @@ use std::ptr::NonNull;
 | 
			
		|||
///   the function returning this type.
 | 
			
		||||
/// - an instance of this created from C is never passed to a consuming function, as the rust code
 | 
			
		||||
///   will try to free the memory of a potentially separate allocator.
 | 
			
		||||
#[repr(C)]
 | 
			
		||||
pub struct SPByteSlice {
 | 
			
		||||
    /// The start address of the memory
 | 
			
		||||
    pub start: NonNull<u8>,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,8 +2,8 @@
 | 
			
		|||
//!
 | 
			
		||||
//! prefix `sp_command_`
 | 
			
		||||
 | 
			
		||||
use crate::{SPBitVec, SPCompressionCode, SPCp437Grid};
 | 
			
		||||
use servicepoint::{BinaryOperation, BrightnessGrid, CharGrid, GlobalBrightnessCommand, Packet, TypedCommand};
 | 
			
		||||
use crate::{SPBitVec};
 | 
			
		||||
use servicepoint::{BinaryOperation, BrightnessGrid, CharGrid, CompressionCode, Cp437Grid, GlobalBrightnessCommand, Packet, TypedCommand};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
/// A low-level display command.
 | 
			
		||||
| 
						 | 
				
			
			@ -225,7 +225,7 @@ pub unsafe extern "C" fn sp_command_char_brightness(
 | 
			
		|||
pub unsafe extern "C" fn sp_command_bitmap_linear(
 | 
			
		||||
    offset: usize,
 | 
			
		||||
    bit_vec: *mut SPBitVec,
 | 
			
		||||
    compression: SPCompressionCode,
 | 
			
		||||
    compression: CompressionCode,
 | 
			
		||||
) -> *mut TypedCommand {
 | 
			
		||||
    unsafe {
 | 
			
		||||
        sp_command_bitmap_linear_internal(
 | 
			
		||||
| 
						 | 
				
			
			@ -266,7 +266,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear(
 | 
			
		|||
pub unsafe extern "C" fn sp_command_bitmap_linear_and(
 | 
			
		||||
    offset: usize,
 | 
			
		||||
    bit_vec: *mut SPBitVec,
 | 
			
		||||
    compression: SPCompressionCode,
 | 
			
		||||
    compression: CompressionCode,
 | 
			
		||||
) -> *mut TypedCommand {
 | 
			
		||||
    unsafe {
 | 
			
		||||
        sp_command_bitmap_linear_internal(
 | 
			
		||||
| 
						 | 
				
			
			@ -307,7 +307,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and(
 | 
			
		|||
pub unsafe extern "C" fn sp_command_bitmap_linear_or(
 | 
			
		||||
    offset: usize,
 | 
			
		||||
    bit_vec: *mut SPBitVec,
 | 
			
		||||
    compression: SPCompressionCode,
 | 
			
		||||
    compression: CompressionCode,
 | 
			
		||||
) -> *mut TypedCommand {
 | 
			
		||||
    unsafe {
 | 
			
		||||
        sp_command_bitmap_linear_internal(
 | 
			
		||||
| 
						 | 
				
			
			@ -348,7 +348,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or(
 | 
			
		|||
pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
 | 
			
		||||
    offset: usize,
 | 
			
		||||
    bit_vec: *mut SPBitVec,
 | 
			
		||||
    compression: SPCompressionCode,
 | 
			
		||||
    compression: CompressionCode,
 | 
			
		||||
) -> *mut TypedCommand {
 | 
			
		||||
    unsafe {
 | 
			
		||||
        sp_command_bitmap_linear_internal(
 | 
			
		||||
| 
						 | 
				
			
			@ -364,7 +364,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
 | 
			
		|||
unsafe fn sp_command_bitmap_linear_internal(
 | 
			
		||||
    offset: usize,
 | 
			
		||||
    bit_vec: *mut SPBitVec,
 | 
			
		||||
    compression: SPCompressionCode,
 | 
			
		||||
    compression: CompressionCode,
 | 
			
		||||
    operation: BinaryOperation,
 | 
			
		||||
) -> *mut TypedCommand {
 | 
			
		||||
    assert!(!bit_vec.is_null());
 | 
			
		||||
| 
						 | 
				
			
			@ -405,14 +405,14 @@ unsafe fn sp_command_bitmap_linear_internal(
 | 
			
		|||
pub unsafe extern "C" fn sp_command_cp437_data(
 | 
			
		||||
    x: usize,
 | 
			
		||||
    y: usize,
 | 
			
		||||
    grid: *mut SPCp437Grid,
 | 
			
		||||
    grid: *mut Cp437Grid,
 | 
			
		||||
) -> NonNull<TypedCommand> {
 | 
			
		||||
    assert!(!grid.is_null());
 | 
			
		||||
    let grid = *unsafe { Box::from_raw(grid) };
 | 
			
		||||
    let result = Box::new(
 | 
			
		||||
        servicepoint::Cp437GridCommand {
 | 
			
		||||
            origin: servicepoint::Origin::new(x, y),
 | 
			
		||||
            grid: grid.0,
 | 
			
		||||
            grid,
 | 
			
		||||
        }
 | 
			
		||||
        .into(),
 | 
			
		||||
    );
 | 
			
		||||
| 
						 | 
				
			
			@ -480,7 +480,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win(
 | 
			
		|||
    x: usize,
 | 
			
		||||
    y: usize,
 | 
			
		||||
    bitmap: *mut servicepoint::Bitmap,
 | 
			
		||||
    compression: SPCompressionCode,
 | 
			
		||||
    compression: CompressionCode,
 | 
			
		||||
) -> *mut TypedCommand {
 | 
			
		||||
    assert!(!bitmap.is_null());
 | 
			
		||||
    let bitmap = unsafe { *Box::from_raw(bitmap) };
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,48 +0,0 @@
 | 
			
		|||
//! re-exported constants for use in C
 | 
			
		||||
 | 
			
		||||
use servicepoint::CompressionCode;
 | 
			
		||||
use std::time::Duration;
 | 
			
		||||
 | 
			
		||||
/// size of a single tile in one dimension
 | 
			
		||||
pub const SP_TILE_SIZE: usize = 8;
 | 
			
		||||
 | 
			
		||||
/// Display tile count in the x-direction
 | 
			
		||||
pub const SP_TILE_WIDTH: usize = 56;
 | 
			
		||||
 | 
			
		||||
/// Display tile count in the y-direction
 | 
			
		||||
pub const SP_TILE_HEIGHT: usize = 20;
 | 
			
		||||
 | 
			
		||||
/// Display width in pixels
 | 
			
		||||
pub const SP_PIXEL_WIDTH: usize = SP_TILE_WIDTH * SP_TILE_SIZE;
 | 
			
		||||
 | 
			
		||||
/// Display height in pixels
 | 
			
		||||
pub const SP_PIXEL_HEIGHT: usize = SP_TILE_HEIGHT * SP_TILE_SIZE;
 | 
			
		||||
 | 
			
		||||
/// pixel count on whole screen
 | 
			
		||||
pub const SP_PIXEL_COUNT: usize = SP_PIXEL_WIDTH * SP_PIXEL_HEIGHT;
 | 
			
		||||
 | 
			
		||||
/// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets.
 | 
			
		||||
pub const SP_FRAME_PACING_MS: u128 = Duration::from_millis(30).as_millis();
 | 
			
		||||
 | 
			
		||||
/// Specifies the kind of compression to use.
 | 
			
		||||
#[repr(u16)]
 | 
			
		||||
pub enum SPCompressionCode {
 | 
			
		||||
    /// no compression
 | 
			
		||||
    Uncompressed = 0x0,
 | 
			
		||||
    /// compress using flate2 with zlib header
 | 
			
		||||
    Zlib = 0x677a,
 | 
			
		||||
    /// compress using bzip2
 | 
			
		||||
    Bzip2 = 0x627a,
 | 
			
		||||
    /// compress using lzma
 | 
			
		||||
    Lzma = 0x6c7a,
 | 
			
		||||
    /// compress using Zstandard
 | 
			
		||||
    Zstd = 0x7a73,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl TryFrom<SPCompressionCode> for CompressionCode {
 | 
			
		||||
    type Error = ();
 | 
			
		||||
 | 
			
		||||
    fn try_from(value: SPCompressionCode) -> Result<Self, Self::Error> {
 | 
			
		||||
        CompressionCode::try_from(value as u16).map_err(|_| ())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,32 +1,25 @@
 | 
			
		|||
//! C functions for interacting with [SPCp437Grid]s
 | 
			
		||||
//!
 | 
			
		||||
//! prefix `sp_cp437_grid_`
 | 
			
		||||
//!
 | 
			
		||||
//!
 | 
			
		||||
//! A C-wrapper for grid containing codepage 437 characters.
 | 
			
		||||
//!
 | 
			
		||||
//! The encoding is currently not enforced.
 | 
			
		||||
//!
 | 
			
		||||
//! # Examples
 | 
			
		||||
//!
 | 
			
		||||
//! ```C
 | 
			
		||||
//! Cp437Grid grid = sp_cp437_grid_new(4, 3);
 | 
			
		||||
//! sp_cp437_grid_fill(grid, '?');
 | 
			
		||||
//! sp_cp437_grid_set(grid, 0, 0, '!');
 | 
			
		||||
//! sp_cp437_grid_free(grid);
 | 
			
		||||
//! ```
 | 
			
		||||
 | 
			
		||||
use crate::SPByteSlice;
 | 
			
		||||
use servicepoint::{DataRef, Grid};
 | 
			
		||||
use servicepoint::{Cp437Grid, DataRef, Grid};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
/// A C-wrapper for grid containing codepage 437 characters.
 | 
			
		||||
///
 | 
			
		||||
/// The encoding is currently not enforced.
 | 
			
		||||
///
 | 
			
		||||
/// # Examples
 | 
			
		||||
///
 | 
			
		||||
/// ```C
 | 
			
		||||
/// Cp437Grid grid = sp_cp437_grid_new(4, 3);
 | 
			
		||||
/// sp_cp437_grid_fill(grid, '?');
 | 
			
		||||
/// 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 {
 | 
			
		||||
    fn clone(&self) -> Self {
 | 
			
		||||
        SPCp437Grid(self.0.clone())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Creates a new [SPCp437Grid] with the specified dimensions.
 | 
			
		||||
///
 | 
			
		||||
/// returns: [SPCp437Grid] initialized to 0. Will never return NULL.
 | 
			
		||||
| 
						 | 
				
			
			@ -41,9 +34,8 @@ impl Clone for SPCp437Grid {
 | 
			
		|||
pub unsafe extern "C" fn sp_cp437_grid_new(
 | 
			
		||||
    width: usize,
 | 
			
		||||
    height: usize,
 | 
			
		||||
) -> NonNull<SPCp437Grid> {
 | 
			
		||||
    let result =
 | 
			
		||||
        Box::new(SPCp437Grid(servicepoint::Cp437Grid::new(width, height)));
 | 
			
		||||
) -> NonNull<Cp437Grid> {
 | 
			
		||||
    let result =        Box::new(Cp437Grid::new(width, height));
 | 
			
		||||
    NonNull::from(Box::leak(result))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -70,12 +62,12 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
 | 
			
		|||
    height: usize,
 | 
			
		||||
    data: *const u8,
 | 
			
		||||
    data_length: usize,
 | 
			
		||||
) -> *mut SPCp437Grid {
 | 
			
		||||
) -> *mut Cp437Grid {
 | 
			
		||||
    assert!(data.is_null());
 | 
			
		||||
    let data = unsafe { std::slice::from_raw_parts(data, data_length) };
 | 
			
		||||
    let grid = servicepoint::Cp437Grid::load(width, height, data);
 | 
			
		||||
    let grid = Cp437Grid::load(width, height, data);
 | 
			
		||||
    if let Some(grid) = grid {
 | 
			
		||||
        Box::leak(Box::new(SPCp437Grid(grid)))
 | 
			
		||||
        Box::leak(Box::new(grid))
 | 
			
		||||
    } else {
 | 
			
		||||
        std::ptr::null_mut()
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -99,8 +91,8 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
 | 
			
		|||
///   by explicitly calling `sp_cp437_grid_free`.
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cp437_grid_clone(
 | 
			
		||||
    cp437_grid: *const SPCp437Grid,
 | 
			
		||||
) -> NonNull<SPCp437Grid> {
 | 
			
		||||
    cp437_grid: *const Cp437Grid,
 | 
			
		||||
) -> NonNull<Cp437Grid> {
 | 
			
		||||
    assert!(!cp437_grid.is_null());
 | 
			
		||||
    let result = Box::new(unsafe { (*cp437_grid).clone() });
 | 
			
		||||
    NonNull::from(Box::leak(result))
 | 
			
		||||
| 
						 | 
				
			
			@ -122,7 +114,7 @@ pub unsafe extern "C" fn sp_cp437_grid_clone(
 | 
			
		|||
///
 | 
			
		||||
/// [SPCommand]: [crate::SPCommand]
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut SPCp437Grid) {
 | 
			
		||||
pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut Cp437Grid) {
 | 
			
		||||
    assert!(!cp437_grid.is_null());
 | 
			
		||||
    _ = unsafe { Box::from_raw(cp437_grid) };
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -147,12 +139,12 @@ pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut SPCp437Grid) {
 | 
			
		|||
/// - `cp437_grid` is not written to concurrently
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cp437_grid_get(
 | 
			
		||||
    cp437_grid: *const SPCp437Grid,
 | 
			
		||||
    cp437_grid: *const Cp437Grid,
 | 
			
		||||
    x: usize,
 | 
			
		||||
    y: usize,
 | 
			
		||||
) -> u8 {
 | 
			
		||||
    assert!(!cp437_grid.is_null());
 | 
			
		||||
    unsafe { (*cp437_grid).0.get(x, y) }
 | 
			
		||||
    unsafe { (*cp437_grid).get(x, y) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Sets the value of the specified position in the [SPCp437Grid].
 | 
			
		||||
| 
						 | 
				
			
			@ -180,13 +172,13 @@ pub unsafe extern "C" fn sp_cp437_grid_get(
 | 
			
		|||
/// [SPBitVec]: [crate::SPBitVec]
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cp437_grid_set(
 | 
			
		||||
    cp437_grid: *mut SPCp437Grid,
 | 
			
		||||
    cp437_grid: *mut Cp437Grid,
 | 
			
		||||
    x: usize,
 | 
			
		||||
    y: usize,
 | 
			
		||||
    value: u8,
 | 
			
		||||
) {
 | 
			
		||||
    assert!(!cp437_grid.is_null());
 | 
			
		||||
    unsafe { (*cp437_grid).0.set(x, y, value) };
 | 
			
		||||
    unsafe { (*cp437_grid).set(x, y, value) };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Sets the value of all cells in the [SPCp437Grid].
 | 
			
		||||
| 
						 | 
				
			
			@ -208,11 +200,11 @@ pub unsafe extern "C" fn sp_cp437_grid_set(
 | 
			
		|||
/// - `cp437_grid` is not written to or read from concurrently
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cp437_grid_fill(
 | 
			
		||||
    cp437_grid: *mut SPCp437Grid,
 | 
			
		||||
    cp437_grid: *mut Cp437Grid,
 | 
			
		||||
    value: u8,
 | 
			
		||||
) {
 | 
			
		||||
    assert!(!cp437_grid.is_null());
 | 
			
		||||
    unsafe { (*cp437_grid).0.fill(value) };
 | 
			
		||||
    unsafe { (*cp437_grid).fill(value) };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Gets the width of the [SPCp437Grid] instance.
 | 
			
		||||
| 
						 | 
				
			
			@ -232,10 +224,10 @@ pub unsafe extern "C" fn sp_cp437_grid_fill(
 | 
			
		|||
/// - `cp437_grid` points to a valid [SPCp437Grid]
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cp437_grid_width(
 | 
			
		||||
    cp437_grid: *const SPCp437Grid,
 | 
			
		||||
    cp437_grid: *const Cp437Grid,
 | 
			
		||||
) -> usize {
 | 
			
		||||
    assert!(!cp437_grid.is_null());
 | 
			
		||||
    unsafe { (*cp437_grid).0.width() }
 | 
			
		||||
    unsafe { (*cp437_grid).width() }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Gets the height of the [SPCp437Grid] instance.
 | 
			
		||||
| 
						 | 
				
			
			@ -255,10 +247,10 @@ pub unsafe extern "C" fn sp_cp437_grid_width(
 | 
			
		|||
/// - `cp437_grid` points to a valid [SPCp437Grid]
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cp437_grid_height(
 | 
			
		||||
    cp437_grid: *const SPCp437Grid,
 | 
			
		||||
    cp437_grid: *const Cp437Grid,
 | 
			
		||||
) -> usize {
 | 
			
		||||
    assert!(!cp437_grid.is_null());
 | 
			
		||||
    unsafe { (*cp437_grid).0.height() }
 | 
			
		||||
    unsafe { (*cp437_grid).height() }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Gets an unsafe reference to the data of the [SPCp437Grid] instance.
 | 
			
		||||
| 
						 | 
				
			
			@ -278,7 +270,7 @@ pub unsafe extern "C" fn sp_cp437_grid_height(
 | 
			
		|||
/// - the returned memory range is never accessed concurrently, either via the [SPCp437Grid] or directly
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref(
 | 
			
		||||
    cp437_grid: *mut SPCp437Grid,
 | 
			
		||||
    cp437_grid: *mut Cp437Grid,
 | 
			
		||||
) -> SPByteSlice {
 | 
			
		||||
    unsafe {SPByteSlice::from_slice((*cp437_grid).0.data_ref_mut()) }
 | 
			
		||||
    unsafe {SPByteSlice::from_slice((*cp437_grid).data_ref_mut()) }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -32,7 +32,6 @@ pub use crate::byte_slice::*;
 | 
			
		|||
pub use crate::char_grid::*;
 | 
			
		||||
pub use crate::command::*;
 | 
			
		||||
pub use crate::connection::*;
 | 
			
		||||
pub use crate::constants::*;
 | 
			
		||||
pub use crate::cp437_grid::*;
 | 
			
		||||
pub use crate::packet::*;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -43,6 +42,11 @@ mod byte_slice;
 | 
			
		|||
mod char_grid;
 | 
			
		||||
mod command;
 | 
			
		||||
mod connection;
 | 
			
		||||
mod constants;
 | 
			
		||||
mod cp437_grid;
 | 
			
		||||
mod packet;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
use std::time::Duration;
 | 
			
		||||
 | 
			
		||||
/// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets.
 | 
			
		||||
pub const SP_FRAME_PACING_MS: u128 = Duration::from_millis(30).as_millis();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue