WIP: next #1

Draft
vinzenz wants to merge 25 commits from next into main
9 changed files with 149 additions and 102 deletions
Showing only changes of commit 2ab80d395e - Show all commits

View file

@ -3,7 +3,7 @@
//! prefix `sp_bitmap_`
use servicepoint::{DataRef, Grid};
use std::ptr::{NonNull};
use std::ptr::NonNull;
use crate::byte_slice::SPByteSlice;

View file

@ -82,7 +82,7 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let grid = match servicepoint::ByteGrid::load(width, height, data) {
None => return std::ptr::null_mut(),
Some(grid) => grid
Some(grid) => grid,
};
if let Ok(grid) = servicepoint::BrightnessGrid::try_from(grid) {
Box::leak(Box::new(SPBrightnessGrid(grid)))

View file

@ -164,7 +164,8 @@ pub unsafe extern "C" fn sp_command_brightness(
) -> NonNull<SPCommand> {
let brightness = servicepoint::Brightness::try_from(brightness)
.expect("invalid brightness");
let result = Box::new(SPCommand(GlobalBrightnessCommand::from(brightness).into()));
let result =
Box::new(SPCommand(GlobalBrightnessCommand::from(brightness).into()));
NonNull::from(Box::leak(result))
}
@ -194,10 +195,13 @@ pub unsafe extern "C" fn sp_command_char_brightness(
) -> NonNull<SPCommand> {
assert!(!grid.is_null());
let byte_grid = unsafe { *Box::from_raw(grid) };
let result = Box::new(SPCommand(servicepoint::BrightnessGridCommand {
let result = Box::new(SPCommand(
servicepoint::BrightnessGridCommand {
origin: servicepoint::Origin::new(x, y),
grid: byte_grid.0,
}.into()));
}
.into(),
));
NonNull::from(Box::leak(result))
}
@ -232,7 +236,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear(
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
unsafe {sp_command_bitmap_linear_internal(offset, bit_vec, compression, BinaryOperation::Overwrite)}
unsafe {
sp_command_bitmap_linear_internal(
offset,
bit_vec,
compression,
BinaryOperation::Overwrite,
)
}
}
/// Set pixel data according to an and-mask starting at the offset.
@ -266,7 +277,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and(
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
unsafe {sp_command_bitmap_linear_internal(offset, bit_vec, compression, BinaryOperation::Xor)}
unsafe {
sp_command_bitmap_linear_internal(
offset,
bit_vec,
compression,
BinaryOperation::Xor,
)
}
}
/// Set pixel data according to an or-mask starting at the offset.
@ -300,7 +318,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or(
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
unsafe {sp_command_bitmap_linear_internal(offset, bit_vec, compression, BinaryOperation::Or)}
unsafe {
sp_command_bitmap_linear_internal(
offset,
bit_vec,
compression,
BinaryOperation::Or,
)
}
}
/// Set pixel data according to a xor-mask starting at the offset.
@ -334,7 +359,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
unsafe {sp_command_bitmap_linear_internal(offset, bit_vec, compression, BinaryOperation::Xor)}
unsafe {
sp_command_bitmap_linear_internal(
offset,
bit_vec,
compression,
BinaryOperation::Xor,
)
}
}
#[inline]
@ -342,7 +374,7 @@ unsafe fn sp_command_bitmap_linear_internal(
offset: usize,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
operation: BinaryOperation
operation: BinaryOperation,
) -> *mut SPCommand {
assert!(!bit_vec.is_null());
let bit_vec = unsafe { *Box::from_raw(bit_vec) };
@ -350,12 +382,15 @@ unsafe fn sp_command_bitmap_linear_internal(
Ok(compression) => compression,
Err(_) => return std::ptr::null_mut(),
};
let command = SPCommand(servicepoint::BitVecCommand {
let command = SPCommand(
servicepoint::BitVecCommand {
offset,
operation,
bitvec: bit_vec.into(),
compression,
}.into());
}
.into(),
);
Box::leak(Box::new(command))
}
@ -385,11 +420,13 @@ pub unsafe extern "C" fn sp_command_cp437_data(
) -> NonNull<SPCommand> {
assert!(!grid.is_null());
let grid = *unsafe { Box::from_raw(grid) };
let result = Box::new(SPCommand(servicepoint::Cp437GridCommand {
origin: servicepoint::Origin::new(x,
y),
let result = Box::new(SPCommand(
servicepoint::Cp437GridCommand {
origin: servicepoint::Origin::new(x, y),
grid: grid.0,
}.into()));
}
.into(),
));
NonNull::from(Box::leak(result))
}
@ -419,11 +456,13 @@ pub unsafe extern "C" fn sp_command_utf8_data(
) -> NonNull<SPCommand> {
assert!(!grid.is_null());
let grid = unsafe { *Box::from_raw(grid) };
let result = Box::new(SPCommand(servicepoint::CharGridCommand {
origin: servicepoint::Origin::new(x,
y),
let result = Box::new(SPCommand(
servicepoint::CharGridCommand {
origin: servicepoint::Origin::new(x, y),
grid: grid.0,
}.into()));
}
.into(),
));
NonNull::from(Box::leak(result))
}
@ -460,11 +499,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win(
Ok(compression) => compression,
Err(_) => return std::ptr::null_mut(),
};
let command = SPCommand(servicepoint::BitmapCommand {
let command = SPCommand(
servicepoint::BitmapCommand {
origin: servicepoint::Origin::new(x, y),
bitmap,
compression,
}.into());
}
.into(),
);
Box::leak(Box::new(command))
}

View file

@ -2,9 +2,9 @@
//!
//! prefix `sp_connection_`
use std::ffi::{c_char, CStr};
use servicepoint::Connection;
use crate::{SPCommand, SPPacket};
use servicepoint::Connection;
use std::ffi::{c_char, CStr};
/// A connection to the display.
///
@ -36,7 +36,9 @@ pub unsafe extern "C" fn sp_connection_open(
host: *const c_char,
) -> *mut SPConnection {
assert!(!host.is_null());
let host = unsafe {CStr::from_ptr(host)}.to_str().expect("Bad encoding");
let host = unsafe { CStr::from_ptr(host) }
.to_str()
.expect("Bad encoding");
let connection = match servicepoint::UdpConnection::open(host) {
Err(_) => return std::ptr::null_mut(),
Ok(value) => value,

View file

@ -72,7 +72,7 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
) -> *mut SPCp437Grid {
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 = servicepoint::Cp437Grid::load(width, height, data);
if let Some(grid) = grid {
Box::leak(Box::new(SPCp437Grid(grid)))
} else {

View file

@ -2,7 +2,7 @@
//!
//! prefix `sp_packet_`
use std::ptr::{NonNull};
use std::ptr::NonNull;
use crate::SPCommand;
@ -34,7 +34,9 @@ pub unsafe extern "C" fn sp_packet_from_command(
let command = unsafe { *Box::from_raw(command) };
if let Ok(packet) = command.0.try_into() {
Box::leak(Box::new(SPPacket(packet)))
} else { std::ptr::null_mut() }
} else {
std::ptr::null_mut()
}
}
/// Tries to load a [SPPacket] from the passed array with the specified length.
@ -105,7 +107,8 @@ pub unsafe extern "C" fn sp_packet_from_parts(
let payload = if payload.is_null() {
vec![]
} else {
let payload = unsafe {std::slice::from_raw_parts(payload, payload_len) };
let payload =
unsafe { std::slice::from_raw_parts(payload, payload_len) };
Vec::from(payload)
};