fix c api, add usage example
This commit is contained in:
parent
98e8a6d639
commit
4bb505650c
12 changed files with 488 additions and 237 deletions
|
@ -106,53 +106,61 @@ impl std::fmt::Debug for BitVec {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
#[cfg(feature = "c-api")]
|
||||
pub mod c_api {
|
||||
use crate::BitVec;
|
||||
|
||||
/// Creates a new `BitVec` instance.
|
||||
/// The returned instance has to be freed with `bit_vec_dealloc`.
|
||||
pub unsafe extern "C" fn bit_vec_new(size: usize) -> *mut BitVec {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_bit_vec_new(size: usize) -> *mut BitVec {
|
||||
Box::into_raw(Box::new(BitVec::new(size)))
|
||||
}
|
||||
|
||||
/// Loads a `BitVec` from the provided data.
|
||||
/// The returned instance has to be freed with `bit_vec_dealloc`.
|
||||
pub unsafe extern "C" fn byte_grid_load(data: *const u8, data_length: usize) -> *mut BitVec {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_bit_vec_load(data: *const u8, data_length: usize) -> *mut BitVec {
|
||||
let data = std::slice::from_raw_parts(data, data_length);
|
||||
Box::into_raw(Box::new(BitVec::from(data)))
|
||||
}
|
||||
|
||||
/// Clones a `BitVec`.
|
||||
/// The returned instance has to be freed with `bit_vec_dealloc`.
|
||||
pub unsafe extern "C" fn byte_grid_clone(this: *const BitVec) -> *mut BitVec {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_bit_vec_clone(this: *const BitVec) -> *mut BitVec {
|
||||
Box::into_raw(Box::new((*this).clone()))
|
||||
}
|
||||
|
||||
/// Deallocates a `BitVec`.
|
||||
///
|
||||
/// Note: do not call this if the grid has been consumed in another way, e.g. to create a command.
|
||||
pub unsafe extern "C" fn byte_grid_dealloc(this: *mut BitVec) {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_bit_vec_dealloc(this: *mut BitVec) {
|
||||
_ = Box::from_raw(this);
|
||||
}
|
||||
|
||||
/// Gets the value of a bit from the `BitVec`.
|
||||
pub unsafe extern "C" fn byte_grid_get(this: *const BitVec, index: usize) -> bool {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_bit_vec_get(this: *const BitVec, index: usize) -> bool {
|
||||
(*this).get(index)
|
||||
}
|
||||
|
||||
/// Sets the value of a bit in the `BitVec`.
|
||||
pub unsafe extern "C" fn byte_grid_set(this: *mut BitVec, index: usize, value: bool) -> bool {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_bit_vec_set(this: *mut BitVec, index: usize, value: bool) -> bool {
|
||||
(*this).set(index, value)
|
||||
}
|
||||
|
||||
/// Sets the value of all bits in the `BitVec`.
|
||||
pub unsafe extern "C" fn byte_grid_fill(this: *mut BitVec, value: bool) {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_bit_vec_fill(this: *mut BitVec, value: bool) {
|
||||
(*this).fill(value)
|
||||
}
|
||||
|
||||
/// Gets the length of the `BitVec` in bits.
|
||||
pub unsafe extern "C" fn byte_grid_len(this: *const BitVec) -> usize{
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_bit_vec_len(this: *const BitVec) -> usize{
|
||||
(*this).len()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,59 +58,68 @@ impl Into<Vec<u8>> for ByteGrid {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
#[cfg(feature = "c-api")]
|
||||
pub mod c_api
|
||||
{
|
||||
use crate::{ByteGrid, PixelGrid};
|
||||
|
||||
/// Creates a new `ByteGrid` instance.
|
||||
/// The returned instance has to be freed with `byte_grid_dealloc`.
|
||||
pub unsafe extern "C" fn byte_grid_new(width: usize, height: usize) -> *mut ByteGrid {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_byte_grid_new(width: usize, height: usize) -> *mut ByteGrid {
|
||||
Box::into_raw(Box::new(ByteGrid::new(width, height)))
|
||||
}
|
||||
|
||||
/// Loads a `ByteGrid` with the specified dimensions from the provided data.
|
||||
/// The returned instance has to be freed with `byte_grid_dealloc`.
|
||||
pub unsafe extern "C" fn byte_grid_load(width: usize, height: usize, data: *const u8, data_length: usize) -> *mut ByteGrid {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_byte_grid_load(width: usize, height: usize, data: *const u8, data_length: usize) -> *mut ByteGrid {
|
||||
let data = std::slice::from_raw_parts(data, data_length);
|
||||
Box::into_raw(Box::new(ByteGrid::load(width, height, data)))
|
||||
}
|
||||
|
||||
/// Clones a `ByteGrid`.
|
||||
/// The returned instance has to be freed with `byte_grid_dealloc`.
|
||||
pub unsafe extern "C" fn byte_grid_clone(this: *const ByteGrid) -> *mut ByteGrid {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_byte_grid_clone(this: *const ByteGrid) -> *mut ByteGrid {
|
||||
Box::into_raw(Box::new((*this).clone()))
|
||||
}
|
||||
|
||||
/// Deallocates a `ByteGrid`.
|
||||
///
|
||||
/// Note: do not call this if the grid has been consumed in another way, e.g. to create a command.
|
||||
pub unsafe extern "C" fn byte_grid_dealloc(this: *mut ByteGrid) {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_byte_grid_dealloc(this: *mut ByteGrid) {
|
||||
_ = Box::from_raw(this);
|
||||
}
|
||||
|
||||
/// Get the current value at the specified position
|
||||
pub unsafe extern "C" fn byte_grid_get(this: *const ByteGrid, x: usize, y: usize) -> u8 {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_byte_grid_get(this: *const ByteGrid, x: usize, y: usize) -> u8 {
|
||||
(*this).get(x, y)
|
||||
}
|
||||
|
||||
/// Sets the current value at the specified position
|
||||
pub unsafe extern "C" fn byte_grid_set(this: *mut ByteGrid, x: usize, y: usize, value: u8) {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_byte_grid_set(this: *mut ByteGrid, x: usize, y: usize, value: u8) {
|
||||
(*this).set(x, y, value);
|
||||
}
|
||||
|
||||
/// Fills the whole `ByteGrid` with the specified value
|
||||
pub unsafe extern "C" fn byte_grid_fill(this: *mut ByteGrid, value: u8) {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_byte_grid_fill(this: *mut ByteGrid, value: u8) {
|
||||
(*this).fill(value);
|
||||
}
|
||||
|
||||
/// Gets the width in pixels of the `ByteGrid` instance.
|
||||
pub unsafe extern "C" fn pixel_grid_width(this: *const PixelGrid) -> usize {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_byte_grid_width(this: *const PixelGrid) -> usize {
|
||||
(*this).width
|
||||
}
|
||||
|
||||
/// Gets the height in pixels of the `ByteGrid` instance.
|
||||
pub unsafe extern "C" fn pixel_grid_height(this: *const PixelGrid) -> usize {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_byte_grid_height(this: *const PixelGrid) -> usize {
|
||||
(*this).height
|
||||
}
|
||||
}
|
|
@ -316,18 +316,18 @@ fn packet_into_linear_bitmap(
|
|||
Ok((BitVec::from(&*payload), sub))
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
#[cfg(feature = "c-api")]
|
||||
pub mod c_api
|
||||
{
|
||||
use std::ptr::null_mut;
|
||||
|
||||
use crate::{BitVec, Brightness, ByteGrid, Command, CompressionCode, Offset, Origin, Packet, PixelGrid};
|
||||
|
||||
/// Tries to load a command from the passed array with the specified length.
|
||||
/// Tries to load a `Command` from the passed array with the specified length.
|
||||
///
|
||||
/// returns: NULL in case of an error, pointer to the allocated command otherwise
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_try_load(data: *const u8, length: usize) -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_try_load(data: *const u8, length: usize) -> *mut Command {
|
||||
let data = std::slice::from_raw_parts(data, length);
|
||||
let packet = match Packet::try_from(data) {
|
||||
Err(_) => return null_mut(),
|
||||
|
@ -342,38 +342,38 @@ pub mod c_api
|
|||
|
||||
/// Clones a `Command` instance
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_clone(original: *const Command) -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_clone(original: *const Command) -> *mut Command {
|
||||
Box::into_raw(Box::new((*original).clone()))
|
||||
}
|
||||
|
||||
/// Allocates a new `Command::Clear` instance
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_clear() -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_clear() -> *mut Command {
|
||||
Box::into_raw(Box::new(Command::Clear))
|
||||
}
|
||||
|
||||
/// Allocates a new `Command::HardReset` instance
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_hard_reset() -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_hard_reset() -> *mut Command {
|
||||
Box::into_raw(Box::new(Command::HardReset))
|
||||
}
|
||||
|
||||
/// Allocates a new `Command::FadeOut` instance
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_fade_out() -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_fade_out() -> *mut Command {
|
||||
Box::into_raw(Box::new(Command::FadeOut))
|
||||
}
|
||||
|
||||
/// Allocates a new `Command::Brightness` instance
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_brightness(brightness: Brightness) -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_brightness(brightness: Brightness) -> *mut Command {
|
||||
Box::into_raw(Box::new(Command::Brightness(brightness)))
|
||||
}
|
||||
|
||||
/// Allocates a new `Command::CharBrightness` instance.
|
||||
/// The passed `ByteGrid` gets deallocated in the process.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_char_brightness(x: u16, y: u16, byte_grid: *mut ByteGrid) -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_char_brightness(x: u16, y: u16, byte_grid: *mut ByteGrid) -> *mut Command {
|
||||
let byte_grid = *Box::from_raw(byte_grid);
|
||||
Box::into_raw(Box::new(Command::CharBrightness(Origin(x, y), byte_grid)))
|
||||
}
|
||||
|
@ -381,7 +381,7 @@ pub mod c_api
|
|||
/// Allocates a new `Command::BitmapLinear` instance.
|
||||
/// The passed `BitVec` gets deallocated in the process.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_bitmap_linear(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_bitmap_linear(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
|
||||
let bit_vec = *Box::from_raw(bit_vec);
|
||||
Box::into_raw(Box::new(Command::BitmapLinear(offset, bit_vec, compression)))
|
||||
}
|
||||
|
@ -389,7 +389,7 @@ pub mod c_api
|
|||
/// Allocates a new `Command::BitmapLinearAnd` instance.
|
||||
/// The passed `BitVec` gets deallocated in the process.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_bitmap_linear_and(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_bitmap_linear_and(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
|
||||
let bit_vec = *Box::from_raw(bit_vec);
|
||||
Box::into_raw(Box::new(Command::BitmapLinearAnd(offset, bit_vec, compression)))
|
||||
}
|
||||
|
@ -397,7 +397,7 @@ pub mod c_api
|
|||
/// Allocates a new `Command::BitmapLinearOr` instance.
|
||||
/// The passed `BitVec` gets deallocated in the process.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_bitmap_linear_or(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_bitmap_linear_or(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
|
||||
let bit_vec = *Box::from_raw(bit_vec);
|
||||
Box::into_raw(Box::new(Command::BitmapLinearOr(offset, bit_vec, compression)))
|
||||
}
|
||||
|
@ -405,7 +405,7 @@ pub mod c_api
|
|||
/// Allocates a new `Command::BitmapLinearXor` instance.
|
||||
/// The passed `BitVec` gets deallocated in the process.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_bitmap_linear_xor(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_bitmap_linear_xor(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
|
||||
let bit_vec = *Box::from_raw(bit_vec);
|
||||
Box::into_raw(Box::new(Command::BitmapLinearXor(offset, bit_vec, compression)))
|
||||
}
|
||||
|
@ -413,7 +413,7 @@ pub mod c_api
|
|||
/// Allocates a new `Command::Cp437Data` instance.
|
||||
/// The passed `ByteGrid` gets deallocated in the process.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_cp437_data(x: u16, y: u16, byte_grid: *mut ByteGrid) -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_cp437_data(x: u16, y: u16, byte_grid: *mut ByteGrid) -> *mut Command {
|
||||
let byte_grid = *Box::from_raw(byte_grid);
|
||||
Box::into_raw(Box::new(Command::Cp437Data(Origin(x, y), byte_grid)))
|
||||
}
|
||||
|
@ -421,15 +421,15 @@ pub mod c_api
|
|||
/// Allocates a new `Command::BitmapLinearWin` instance.
|
||||
/// The passed `PixelGrid` gets deallocated in the process.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_bitmap_linear_win(x: u16, y: u16, byte_grid: *mut PixelGrid) -> *mut Command {
|
||||
pub unsafe extern "C" fn sp2_command_bitmap_linear_win(x: u16, y: u16, byte_grid: *mut PixelGrid) -> *mut Command {
|
||||
let byte_grid = *Box::from_raw(byte_grid);
|
||||
Box::into_raw(Box::new(Command::BitmapLinearWin(Origin(x, y), byte_grid)))
|
||||
}
|
||||
|
||||
/// Deallocates a command. Note that connection_send does this implicitly, so you only need
|
||||
/// Deallocates a `Command`. Note that connection_send does this implicitly, so you only need
|
||||
/// to do this if you use the library for parsing commands.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn command_dealloc(ptr: *mut Command) {
|
||||
pub unsafe extern "C" fn sp2_command_dealloc(ptr: *mut Command) {
|
||||
_ = Box::from_raw(ptr);
|
||||
}
|
||||
}
|
|
@ -65,6 +65,7 @@ impl Connection {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "c-api")]
|
||||
pub mod c_api
|
||||
{
|
||||
use std::ffi::{c_char, CStr};
|
||||
|
@ -79,7 +80,7 @@ pub mod c_api
|
|||
///
|
||||
/// Panics: bad string encoding
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn connection_open(host: *const c_char) -> *mut Connection {
|
||||
pub unsafe extern "C" fn sp2_connection_open(host: *const c_char) -> *mut Connection {
|
||||
let host = CStr::from_ptr(host).to_str().expect("Bad encoding");
|
||||
let connection = match Connection::open(host) {
|
||||
Err(_) => return null_mut(),
|
||||
|
@ -92,14 +93,14 @@ pub mod c_api
|
|||
|
||||
/// Sends the command instance. The instance is consumed / destroyed and cannot be used after this call.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn connection_send(connection: *const Connection, command_ptr: *mut Command) -> bool{
|
||||
pub unsafe extern "C" fn sp2_connection_send(connection: *const Connection, command_ptr: *mut Command) -> bool{
|
||||
let command = Box::from_raw(command_ptr);
|
||||
(*connection).send(*command).is_ok()
|
||||
}
|
||||
|
||||
/// Closes and deallocates a connection instance
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn connection_dealloc(ptr: *mut Connection) {
|
||||
pub unsafe extern "C" fn sp2_connection_dealloc(ptr: *mut Connection) {
|
||||
_ = Box::from_raw(ptr);
|
||||
}
|
||||
}
|
|
@ -84,59 +84,68 @@ impl Into<Vec<u8>> for PixelGrid {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
#[cfg(feature = "c-api")]
|
||||
pub mod c_api
|
||||
{
|
||||
use crate::PixelGrid;
|
||||
|
||||
/// Creates a new `PixelGrid` instance.
|
||||
/// The returned instance has to be freed with `pixel_grid_dealloc`.
|
||||
pub unsafe extern "C" fn pixel_grid_new(width: usize, height: usize) -> *mut PixelGrid {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_pixel_grid_new(width: usize, height: usize) -> *mut PixelGrid {
|
||||
Box::into_raw(Box::new(PixelGrid::new(width, height)))
|
||||
}
|
||||
|
||||
/// Loads a `PixelGrid` with the specified dimensions from the provided data.
|
||||
/// The returned instance has to be freed with `pixel_grid_dealloc`.
|
||||
pub unsafe extern "C" fn pixel_grid_load(width: usize, height: usize, data: *const u8, data_length: usize) -> *mut PixelGrid {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_pixel_grid_load(width: usize, height: usize, data: *const u8, data_length: usize) -> *mut PixelGrid {
|
||||
let data = std::slice::from_raw_parts(data, data_length);
|
||||
Box::into_raw(Box::new(PixelGrid::load(width, height, data)))
|
||||
}
|
||||
|
||||
/// Clones a `PixelGrid`.
|
||||
/// The returned instance has to be freed with `pixel_grid_dealloc`.
|
||||
pub unsafe extern "C" fn pixel_grid_clone(this: *const PixelGrid) -> *mut PixelGrid {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_pixel_grid_clone(this: *const PixelGrid) -> *mut PixelGrid {
|
||||
Box::into_raw(Box::new((*this).clone()))
|
||||
}
|
||||
|
||||
/// Deallocates a `PixelGrid`.
|
||||
///
|
||||
/// Note: do not call this if the grid has been consumed in another way, e.g. to create a command.
|
||||
pub unsafe extern "C" fn pixel_grid_dealloc(this: *mut PixelGrid) {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_pixel_grid_dealloc(this: *mut PixelGrid) {
|
||||
_ = Box::from_raw(this);
|
||||
}
|
||||
|
||||
/// Get the current value at the specified position
|
||||
pub unsafe extern "C" fn pixel_grid_get(this: *const PixelGrid, x: usize, y: usize) -> bool {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_pixel_grid_get(this: *const PixelGrid, x: usize, y: usize) -> bool {
|
||||
(*this).get(x, y)
|
||||
}
|
||||
|
||||
/// Sets the current value at the specified position
|
||||
pub unsafe extern "C" fn pixel_grid_set(this: *mut PixelGrid, x: usize, y: usize, value: bool) {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_pixel_grid_set(this: *mut PixelGrid, x: usize, y: usize, value: bool) {
|
||||
(*this).set(x, y, value);
|
||||
}
|
||||
|
||||
/// Fills the whole `PixelGrid` with the specified value
|
||||
pub unsafe extern "C" fn pixel_grid_fill(this: *mut PixelGrid, value: bool) {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_pixel_grid_fill(this: *mut PixelGrid, value: bool) {
|
||||
(*this).fill(value);
|
||||
}
|
||||
|
||||
/// Gets the width in pixels of the `PixelGrid` instance.
|
||||
pub unsafe extern "C" fn pixel_grid_width(this: *const PixelGrid) -> usize {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_pixel_grid_width(this: *const PixelGrid) -> usize {
|
||||
(*this).width
|
||||
}
|
||||
|
||||
/// Gets the height in pixels of the `PixelGrid` instance.
|
||||
pub unsafe extern "C" fn pixel_grid_height(this: *const PixelGrid) -> usize {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp2_pixel_grid_height(this: *const PixelGrid) -> usize {
|
||||
(*this).height
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue