use crate::{ macros::{wrap_clone, wrap_free}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin, Packet}; use std::ptr::NonNull; /// Show text on the screen. /// /// The text is sent in the form of a 2D grid of [CP-437] encoded characters. /// /// The origin is relative to the top-left of the display. #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_new( grid: NonNull, origin_x: usize, origin_y: usize, ) -> NonNull { heap_move_nonnull(Cp437GridCommand { grid: unsafe { heap_remove(grid) }, origin: Origin::new(origin_x, origin_y), }) } /// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], /// leaving other fields as their default values. #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_from_grid( grid: NonNull, ) -> NonNull { heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } /// Tries to turn a [Cp437GridCommand] into a [Packet]. /// /// Returns: NULL or a [Packet] containing the command. #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_try_into_packet( command: NonNull, ) -> *mut Packet { heap_move_ok(unsafe { heap_remove(command) }.try_into()) } wrap_clone!(sp_cmd_cp437_grid::Cp437GridCommand); wrap_free!(sp_cmd_cp437_grid::Cp437GridCommand); /// Moves the provided bitmap into the provided command. /// /// This drops the previously contained [Cp437Grid]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_set( mut command: NonNull, grid: NonNull, ) { unsafe { command.as_mut().grid = heap_remove(grid); } } /// Show text on the screen. /// /// The text is sent in the form of a 2D grid of [CP-437] encoded characters. /// For sending UTF-8 encoded characters, see [servicepoint::CharGridCommand]. /// /// [CP-437]: https://en.wikipedia.org/wiki/Code_page_437 #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_get( mut command: NonNull, ) -> *mut Cp437Grid { unsafe { &mut command.as_mut().grid } } /// Gets the origin field of the [Cp437GridCommand]. /// /// Rust equivalent: `cp437_command.origin` #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_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; } } /// Sets the origin field of the [Cp437GridCommand]. /// /// Rust equivalent: `cp437_command.origin = Origin::new(origin_x, origin_y)` #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_set_origin( mut command: NonNull, origin_x: usize, origin_y: usize, ) { unsafe { command.as_mut().origin = Origin::new(origin_x, origin_y); } }