use crate::mem::{ heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, Packet, }; use std::ptr::NonNull; /// Set pixel data starting at the pixel offset on screen. /// /// The screen will continuously overwrite more pixel data without regarding the offset, meaning /// once the starting row is full, overwriting will continue on column 0. /// /// The [`BinaryOperation`] will be applied on the display comparing old and sent bit. /// /// `new_bit = old_bit op sent_bit` /// /// For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. /// /// The contained [`DisplayBitVec`] is always uncompressed. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_new( bitvec: NonNull, offset: usize, operation: BinaryOperation, compression: CompressionCode, ) -> NonNull { heap_move_nonnull(BitVecCommand { bitvec: unsafe { heap_remove(bitvec) }, offset, operation, compression, }) } /// Tries to turn a [BitVecCommand] into a [Packet]. /// /// Returns: NULL or a [Packet] containing the command. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_try_into_packet( command: NonNull, ) -> *mut Packet { heap_move_ok(unsafe { heap_remove(command) }.try_into()) } /// Clones an [BitVecCommand] instance. /// /// returns: a new [BitVecCommand] instance. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_clone( command: NonNull, ) -> NonNull { unsafe { heap_clone(command) } } /// Deallocates a [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_free(command: NonNull) { unsafe { heap_drop(command) } } /// Returns a pointer to the [BitVec] contained in the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_get( mut command: NonNull, ) -> *mut DisplayBitVec { unsafe { &mut command.as_mut().bitvec } } /// Moves the provided [BitVec] to be contained in the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_set( mut command: NonNull, bitvec: NonNull, ) { unsafe { command.as_mut().bitvec = heap_remove(bitvec); } } /// Reads the offset field of the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_get_offset( command: NonNull, ) -> Offset { unsafe { command.as_ref().offset } } /// Overwrites the offset field of the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_set_offset( mut command: NonNull, offset: Offset, ) { unsafe { command.as_mut().offset = offset; } } /// Returns the [BinaryOperation] of the command. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_get_operation( command: NonNull, ) -> BinaryOperation { unsafe { command.as_ref().operation } } /// Overwrites the [BinaryOperation] of the command. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_set_operation( mut command: NonNull, operation: BinaryOperation, ) { unsafe { command.as_mut().operation = operation; } } /// Overwrites the compression kind of the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_set_compression( mut command: NonNull, compression: CompressionCode, ) { unsafe { command.as_mut().compression = compression; } } /// Reads the compression kind of the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_get_compression( command: NonNull, ) -> CompressionCode { unsafe { command.as_ref().compression } }