use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove}; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, Origin, Packet, TypedCommand, }; 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, }) } #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_from_bitmap( bitmap: NonNull, ) -> NonNull { heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) } #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_into_typed( command: NonNull, ) -> NonNull { heap_move_nonnull(unsafe { heap_remove(command) }.into()) } #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_into_packet( command: NonNull, ) -> *mut Packet { heap_move_ok(unsafe { heap_remove(command) }.try_into()) } #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_clone( command: NonNull, ) -> NonNull { heap_move_nonnull(unsafe { command.as_ref().clone() }) } #[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 command. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_set( mut command: NonNull, bitmap: NonNull, ) { unsafe { command.as_mut().bitmap = heap_remove(bitmap); } } #[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; } } #[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); } } #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_set_compression( mut command: NonNull, compression: CompressionCode, ) { unsafe { command.as_mut().compression = compression; } } #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_get_compression( command: NonNull, ) -> CompressionCode { unsafe { command.as_ref().compression } }