use crate::mem::{ heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; use std::ptr::NonNull; /// Sets a window of pixels to the specified values. /// /// The passed [Bitmap] gets consumed. /// /// Returns: a new [BitmapCommand] instance. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_new( bitmap: NonNull, origin_x: usize, origin_y: usize, compression: CompressionCode, ) -> NonNull { heap_move_nonnull(BitmapCommand { bitmap: unsafe { heap_remove(bitmap) }, origin: Origin::new(origin_x, origin_y), compression, }) } /// Move the provided [Bitmap] into a new [BitmapCommand], /// leaving other fields as their default values. /// /// Rust equivalent: `BitmapCommand::from(bitmap)` #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_from_bitmap( bitmap: NonNull, ) -> NonNull { heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) } /// Tries to turn a [BitmapCommand] into a [Packet]. /// /// Returns: NULL or a [Packet] containing the command. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_try_into_packet( command: NonNull, ) -> *mut Packet { heap_move_ok(unsafe { heap_remove(command) }.try_into()) } /// Clones an [BitmapCommand] instance. /// /// returns: a new [BitmapCommand] instance. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_clone( command: NonNull, ) -> NonNull { unsafe { heap_clone(command) } } /// Deallocates a [BitmapCommand] instance. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_free(command: NonNull) { unsafe { heap_drop(command) } } /// Returns a pointer to the provided `BitmapCommand`. /// /// # Safety /// /// - The returned bitmap inherits the lifetime of the command in which it is contained. /// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_get( mut command: NonNull, ) -> NonNull { unsafe { NonNull::from(&mut (command.as_mut().bitmap)) } } /// Moves the provided [Bitmap] to be contained in the [BitmapCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_set( mut command: NonNull, bitmap: NonNull, ) { unsafe { command.as_mut().bitmap = heap_remove(bitmap); } } /// Reads the origin field of the [BitmapCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_get_origin( command: NonNull, origin_x: NonNull, origin_y: NonNull, ) { unsafe { let origin = &command.as_ref().origin; *origin_x.as_ptr() = origin.x; *origin_y.as_ptr() = origin.y; } } /// Overwrites the origin field of the [BitmapCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_set_origin( mut command: NonNull, origin_x: usize, origin_y: usize, ) { unsafe { command.as_mut().origin = Origin::new(origin_x, origin_y); } } /// Overwrites the compression kind of the [BitmapCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_set_compression( mut command: NonNull, compression: CompressionCode, ) { unsafe { command.as_mut().compression = compression; } } /// Reads the compression kind of the [BitmapCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_get_compression( command: NonNull, ) -> CompressionCode { unsafe { command.as_ref().compression } }