293 lines
10 KiB
Rust
293 lines
10 KiB
Rust
use crate::mem::{
|
|
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
|
|
};
|
|
use servicepoint::{
|
|
BitVecCommand, BitmapCommand, BrightnessGridCommand, CharGridCommand,
|
|
ClearCommand, Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand,
|
|
HardResetCommand, Packet, TypedCommand,
|
|
};
|
|
use std::ptr::{null_mut, NonNull};
|
|
|
|
/// Pointer to one of the available command structs.
|
|
#[repr(C)]
|
|
#[allow(missing_docs)]
|
|
pub union CommandUnion {
|
|
pub null: *mut u8,
|
|
pub bitmap: NonNull<BitmapCommand>,
|
|
pub bitvec: NonNull<BitVecCommand>,
|
|
pub brightness_grid: NonNull<BrightnessGridCommand>,
|
|
pub char_grid: NonNull<CharGridCommand>,
|
|
pub cp437_grid: NonNull<Cp437GridCommand>,
|
|
pub global_brightness: NonNull<GlobalBrightnessCommand>,
|
|
pub clear: NonNull<ClearCommand>,
|
|
#[allow(deprecated)]
|
|
pub bitmap_legacy: NonNull<servicepoint::BitmapLegacyCommand>,
|
|
pub hard_reset: NonNull<HardResetCommand>,
|
|
pub fade_out: NonNull<FadeOutCommand>,
|
|
}
|
|
|
|
/// Specifies the kind of command struct.
|
|
///
|
|
/// This is _not_ equivalent to the [servicepoint::CommandCode]s.
|
|
#[repr(u8)]
|
|
#[allow(missing_docs)]
|
|
pub enum CommandTag {
|
|
Invalid = 0,
|
|
Bitmap,
|
|
BitVec,
|
|
BrightnessGrid,
|
|
CharGrid,
|
|
Cp437Grid,
|
|
GlobalBrightness,
|
|
Clear,
|
|
HardReset,
|
|
FadeOut,
|
|
BitmapLegacy,
|
|
}
|
|
|
|
/// This struct represents a pointer to one of the possible command structs.
|
|
///
|
|
/// Only ever access `data` with the correct data type as specified by `tag`!
|
|
///
|
|
/// Rust equivalent: [TypedCommand].
|
|
#[repr(C)]
|
|
pub struct SPCommand {
|
|
/// Specifies which kind of command struct is contained in `data`
|
|
pub tag: CommandTag,
|
|
/// The pointer to the command struct
|
|
pub data: CommandUnion,
|
|
}
|
|
|
|
impl SPCommand {
|
|
const INVALID: SPCommand = SPCommand {
|
|
tag: CommandTag::Invalid,
|
|
data: CommandUnion { null: null_mut() },
|
|
};
|
|
}
|
|
|
|
/// Tries to turn a [Packet] into a [SPCommand].
|
|
///
|
|
/// The packet is dropped in the process.
|
|
///
|
|
/// Returns: pointer to new [SPCommand] instance or NULL if parsing failed.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_cmd_generic_try_from_packet(
|
|
packet: NonNull<Packet>,
|
|
) -> *mut SPCommand {
|
|
let packet = *unsafe { Box::from_raw(packet.as_ptr()) };
|
|
heap_move_ok(servicepoint::TypedCommand::try_from(packet).map(|value| {
|
|
match value {
|
|
TypedCommand::Clear(clear) => SPCommand {
|
|
tag: CommandTag::Clear,
|
|
data: CommandUnion {
|
|
clear: heap_move_nonnull(clear),
|
|
},
|
|
},
|
|
TypedCommand::CharGrid(char_grid) => SPCommand {
|
|
tag: CommandTag::CharGrid,
|
|
data: CommandUnion {
|
|
char_grid: heap_move_nonnull(char_grid),
|
|
},
|
|
},
|
|
TypedCommand::Cp437Grid(cp437_grid) => SPCommand {
|
|
tag: CommandTag::Cp437Grid,
|
|
data: CommandUnion {
|
|
cp437_grid: heap_move_nonnull(cp437_grid),
|
|
},
|
|
},
|
|
TypedCommand::Bitmap(bitmap) => SPCommand {
|
|
tag: CommandTag::Bitmap,
|
|
data: CommandUnion {
|
|
bitmap: heap_move_nonnull(bitmap),
|
|
},
|
|
},
|
|
TypedCommand::Brightness(global_brightness) => SPCommand {
|
|
tag: CommandTag::GlobalBrightness,
|
|
data: CommandUnion {
|
|
global_brightness: heap_move_nonnull(global_brightness),
|
|
},
|
|
},
|
|
TypedCommand::BrightnessGrid(brightness_grid) => SPCommand {
|
|
tag: CommandTag::BrightnessGrid,
|
|
data: CommandUnion {
|
|
brightness_grid: heap_move_nonnull(brightness_grid),
|
|
},
|
|
},
|
|
TypedCommand::BitVec(bitvec) => SPCommand {
|
|
tag: CommandTag::BitVec,
|
|
data: CommandUnion {
|
|
bitvec: heap_move_nonnull(bitvec),
|
|
},
|
|
},
|
|
TypedCommand::HardReset(hard_reset) => SPCommand {
|
|
tag: CommandTag::HardReset,
|
|
data: CommandUnion {
|
|
hard_reset: heap_move_nonnull(hard_reset),
|
|
},
|
|
},
|
|
TypedCommand::FadeOut(fade_out) => SPCommand {
|
|
tag: CommandTag::FadeOut,
|
|
data: CommandUnion {
|
|
fade_out: heap_move_nonnull(fade_out),
|
|
},
|
|
},
|
|
#[allow(deprecated)]
|
|
TypedCommand::BitmapLegacy(bitmap_legacy) => SPCommand {
|
|
tag: CommandTag::BitmapLegacy,
|
|
data: CommandUnion {
|
|
bitmap_legacy: heap_move_nonnull(bitmap_legacy),
|
|
},
|
|
},
|
|
}
|
|
}))
|
|
}
|
|
|
|
/// Clones an [SPCommand] instance.
|
|
///
|
|
/// returns: a new [SPCommand] instance.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_cmd_generic_clone(command: SPCommand) -> SPCommand {
|
|
unsafe {
|
|
match command.tag {
|
|
CommandTag::Clear => SPCommand {
|
|
tag: CommandTag::Clear,
|
|
data: CommandUnion {
|
|
clear: heap_clone(command.data.clear),
|
|
},
|
|
},
|
|
CommandTag::CharGrid => SPCommand {
|
|
tag: CommandTag::CharGrid,
|
|
data: CommandUnion {
|
|
char_grid: heap_clone(command.data.char_grid),
|
|
},
|
|
},
|
|
CommandTag::Cp437Grid => SPCommand {
|
|
tag: CommandTag::Cp437Grid,
|
|
data: CommandUnion {
|
|
cp437_grid: heap_clone(command.data.cp437_grid),
|
|
},
|
|
},
|
|
CommandTag::Bitmap => SPCommand {
|
|
tag: CommandTag::Bitmap,
|
|
data: CommandUnion {
|
|
bitmap: heap_clone(command.data.bitmap),
|
|
},
|
|
},
|
|
CommandTag::GlobalBrightness => SPCommand {
|
|
tag: CommandTag::GlobalBrightness,
|
|
data: CommandUnion {
|
|
global_brightness: heap_clone(
|
|
command.data.global_brightness,
|
|
),
|
|
},
|
|
},
|
|
CommandTag::BrightnessGrid => SPCommand {
|
|
tag: CommandTag::BrightnessGrid,
|
|
data: CommandUnion {
|
|
brightness_grid: heap_clone(command.data.brightness_grid),
|
|
},
|
|
},
|
|
CommandTag::BitVec => SPCommand {
|
|
tag: CommandTag::BitVec,
|
|
data: CommandUnion {
|
|
bitvec: heap_clone(command.data.bitvec),
|
|
},
|
|
},
|
|
CommandTag::HardReset => SPCommand {
|
|
tag: CommandTag::HardReset,
|
|
data: CommandUnion {
|
|
hard_reset: heap_clone(command.data.hard_reset),
|
|
},
|
|
},
|
|
CommandTag::FadeOut => SPCommand {
|
|
tag: CommandTag::FadeOut,
|
|
data: CommandUnion {
|
|
fade_out: heap_clone(command.data.fade_out),
|
|
},
|
|
},
|
|
#[allow(deprecated)]
|
|
CommandTag::BitmapLegacy => SPCommand {
|
|
tag: CommandTag::BitmapLegacy,
|
|
data: CommandUnion {
|
|
bitmap_legacy: heap_clone(command.data.bitmap_legacy),
|
|
},
|
|
},
|
|
CommandTag::Invalid => SPCommand::INVALID,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Deallocates an [SPCommand].
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```C
|
|
/// SPCommand c = sp_cmd_clear_into_generic(sp_cmd_clear_new());
|
|
/// sp_command_free(c);
|
|
/// ```
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_cmd_generic_free(command: SPCommand) {
|
|
unsafe {
|
|
match command.tag {
|
|
CommandTag::Invalid => return,
|
|
CommandTag::Bitmap => heap_drop(command.data.bitmap),
|
|
CommandTag::BitVec => heap_drop(command.data.bitvec),
|
|
CommandTag::BrightnessGrid => {
|
|
heap_drop(command.data.brightness_grid)
|
|
}
|
|
CommandTag::CharGrid => heap_drop(command.data.char_grid),
|
|
CommandTag::Cp437Grid => heap_drop(command.data.cp437_grid),
|
|
CommandTag::GlobalBrightness => {
|
|
heap_drop(command.data.global_brightness)
|
|
}
|
|
CommandTag::Clear => heap_drop(command.data.clear),
|
|
CommandTag::HardReset => heap_drop(command.data.hard_reset),
|
|
CommandTag::FadeOut => heap_drop(command.data.fade_out),
|
|
CommandTag::BitmapLegacy => heap_drop(command.data.bitmap_legacy),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Tries to turn a [SPCommand] into a [Packet].
|
|
/// The [SPCommand] gets consumed.
|
|
///
|
|
/// Returns tag [CommandTag::Invalid] in case of an error.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_cmd_generic_into_packet(
|
|
command: SPCommand,
|
|
) -> *mut Packet {
|
|
match command.tag {
|
|
CommandTag::Invalid => null_mut(),
|
|
CommandTag::Bitmap => {
|
|
heap_move_ok(unsafe { heap_remove(command.data.bitmap).try_into() })
|
|
}
|
|
CommandTag::BitVec => {
|
|
heap_move_ok(unsafe { heap_remove(command.data.bitvec).try_into() })
|
|
}
|
|
CommandTag::BrightnessGrid => heap_move_ok(unsafe {
|
|
heap_remove(command.data.brightness_grid).try_into()
|
|
}),
|
|
CommandTag::CharGrid => heap_move_ok(unsafe {
|
|
heap_remove(command.data.char_grid).try_into()
|
|
}),
|
|
CommandTag::Cp437Grid => heap_move_ok(unsafe {
|
|
heap_remove(command.data.cp437_grid).try_into()
|
|
}),
|
|
CommandTag::GlobalBrightness => heap_move_ok(unsafe {
|
|
heap_remove(command.data.global_brightness).try_into()
|
|
}),
|
|
CommandTag::Clear => {
|
|
heap_move_ok(unsafe { heap_remove(command.data.clear).try_into() })
|
|
}
|
|
CommandTag::HardReset => heap_move_ok(unsafe {
|
|
heap_remove(command.data.hard_reset).try_into()
|
|
}),
|
|
CommandTag::FadeOut => heap_move_ok(unsafe {
|
|
heap_remove(command.data.fade_out).try_into()
|
|
}),
|
|
CommandTag::BitmapLegacy => heap_move_ok(unsafe {
|
|
heap_remove(command.data.bitmap_legacy).try_into()
|
|
}),
|
|
}
|
|
}
|