servicepoint/crates/servicepoint_binding_c/src/brightness_grid.rs

269 lines
7.3 KiB
Rust
Raw Normal View History

2024-09-07 14:11:15 +02:00
//! C functions for interacting with `SPBrightnessGrid`s
2024-06-23 15:42:15 +02:00
//!
//! prefix `sp_brightness_grid_`
use crate::SPByteSlice;
2024-09-05 21:15:53 +02:00
use servicepoint::{Brightness, DataRef, Grid, PrimitiveGrid};
2024-06-23 15:42:15 +02:00
use std::intrinsics::transmute;
2024-09-05 21:15:53 +02:00
/// A grid containing brightness values.
///
/// # Examples
/// ```C
/// SPConnection connection = sp_connection_open("127.0.0.1:2342");
/// if (connection == NULL)
/// return 1;
///
/// SPBrightnessGrid grid = sp_brightness_grid_new(2, 2);
/// sp_brightness_grid_set(grid, 0, 0, 0);
/// sp_brightness_grid_set(grid, 1, 1, 10);
///
/// SPCommand command = sp_command_char_brightness(grid);
2024-09-07 14:11:15 +02:00
/// sp_connection_free(connection);
2024-09-05 21:15:53 +02:00
/// ```
pub struct SPBrightnessGrid(pub(crate) servicepoint::BrightnessGrid);
2024-08-29 22:21:21 +02:00
2024-09-05 21:15:53 +02:00
impl Clone for SPBrightnessGrid {
2024-08-29 22:21:21 +02:00
fn clone(&self) -> Self {
SPBrightnessGrid(self.0.clone())
2024-08-29 22:21:21 +02:00
}
}
2024-06-23 15:42:15 +02:00
2024-09-07 14:11:15 +02:00
/// Creates a new `SPBrightnessGrid` with the specified dimensions.
2024-06-23 15:42:15 +02:00
///
/// returns: `SPBrightnessGrid` initialized to 0. Will never return NULL.
2024-06-23 15:42:15 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - the returned instance is freed in some way, either by using a consuming function or
2024-09-07 14:11:15 +02:00
/// by explicitly calling `sp_brightness_grid_free`.
2024-06-23 15:42:15 +02:00
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_new(
width: usize,
height: usize,
2024-09-05 21:15:53 +02:00
) -> *mut SPBrightnessGrid {
Box::into_raw(Box::new(SPBrightnessGrid(
servicepoint::BrightnessGrid::new(width, height),
)))
2024-06-23 15:42:15 +02:00
}
2024-09-07 14:11:15 +02:00
/// Loads a `SPBrightnessGrid` with the specified dimensions from the provided data.
2024-06-23 15:42:15 +02:00
///
/// # Panics
///
/// When the provided `data_length` is not sufficient for the `height` and `width`
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `data` points to a valid memory location of at least `data_length`
/// bytes in size.
/// - the returned instance is freed in some way, either by using a consuming function or
2024-09-07 14:11:15 +02:00
/// by explicitly calling `sp_brightness_grid_free`.
2024-06-23 15:42:15 +02:00
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_load(
width: usize,
height: usize,
data: *const u8,
data_length: usize,
2024-09-05 21:15:53 +02:00
) -> *mut SPBrightnessGrid {
2024-06-23 15:42:15 +02:00
let data = std::slice::from_raw_parts(data, data_length);
let grid = PrimitiveGrid::load(width, height, data);
2024-09-05 21:15:53 +02:00
let grid = servicepoint::BrightnessGrid::try_from(grid)
.expect("invalid brightness value");
Box::into_raw(Box::new(SPBrightnessGrid(grid)))
2024-06-23 15:42:15 +02:00
}
2024-09-07 14:11:15 +02:00
/// Clones a `SPBrightnessGrid`.
2024-06-23 15:42:15 +02:00
///
/// # Arguments
///
/// - `brightness_grid`: instance to read from
///
2024-06-23 15:42:15 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
/// - `brightness_grid` points to a valid `SPBrightnessGrid`
/// - `brightness_grid` is not written to concurrently
2024-06-23 15:42:15 +02:00
/// - the returned instance is freed in some way, either by using a consuming function or
2024-09-07 14:11:15 +02:00
/// by explicitly calling `sp_brightness_grid_free`.
2024-06-23 15:42:15 +02:00
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_clone(
brightness_grid: *const SPBrightnessGrid,
2024-09-05 21:15:53 +02:00
) -> *mut SPBrightnessGrid {
Box::into_raw(Box::new((*brightness_grid).clone()))
2024-06-23 15:42:15 +02:00
}
2024-09-07 14:11:15 +02:00
/// Deallocates a `SPBrightnessGrid`.
2024-06-23 15:42:15 +02:00
///
/// # Arguments
///
/// - `brightness_grid`: instance to read from
///
2024-06-23 15:42:15 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
/// - `brightness_grid` points to a valid `SPBrightnessGrid`
/// - `brightness_grid` is not used concurrently or after this call
2024-10-13 18:12:55 +02:00
/// - `brightness_grid` was not passed to another consuming function, e.g. to create a [SPCommand]
2024-06-23 15:42:15 +02:00
#[no_mangle]
2024-09-07 14:11:15 +02:00
pub unsafe extern "C" fn sp_brightness_grid_free(
brightness_grid: *mut SPBrightnessGrid,
2024-06-23 16:01:11 +02:00
) {
_ = Box::from_raw(brightness_grid);
2024-06-23 15:42:15 +02:00
}
/// Gets the current value at the specified position.
///
/// # Arguments
///
/// - `brightness_grid`: instance to read from
/// - `x` and `y`: position of the cell to read
2024-06-23 15:42:15 +02:00
///
/// # Panics
///
/// When accessing `x` or `y` out of bounds.
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `brightness_grid` points to a valid `SPBrightnessGrid`
/// - `brightness_grid` is not written to concurrently
2024-06-23 15:42:15 +02:00
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_get(
brightness_grid: *const SPBrightnessGrid,
2024-06-23 15:42:15 +02:00
x: usize,
y: usize,
) -> u8 {
(*brightness_grid).0.get(x, y).into()
2024-06-23 15:42:15 +02:00
}
2024-09-07 14:11:15 +02:00
/// Sets the value of the specified position in the `SPBrightnessGrid`.
2024-06-23 15:42:15 +02:00
///
/// # Arguments
///
/// - `brightness_grid`: instance to write to
/// - `x` and `y`: position of the cell
/// - `value`: the value to write to the cell
2024-06-23 15:42:15 +02:00
///
/// returns: old value of the cell
///
/// # Panics
///
/// - When accessing `x` or `y` out of bounds.
/// - When providing an invalid brightness value
2024-06-23 15:42:15 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
2024-10-13 18:12:55 +02:00
/// - `brightness_grid` points to a valid [SPBitVec]
/// - `brightness_grid` is not written to or read from concurrently
2024-06-23 15:42:15 +02:00
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_set(
brightness_grid: *mut SPBrightnessGrid,
2024-06-23 15:42:15 +02:00
x: usize,
y: usize,
value: u8,
) {
2024-06-23 16:01:11 +02:00
let brightness =
Brightness::try_from(value).expect("invalid brightness value");
(*brightness_grid).0.set(x, y, brightness);
2024-06-23 15:42:15 +02:00
}
2024-09-07 14:11:15 +02:00
/// Sets the value of all cells in the `SPBrightnessGrid`.
2024-06-23 15:42:15 +02:00
///
/// # Arguments
///
/// - `brightness_grid`: instance to write to
/// - `value`: the value to set all cells to
///
/// # Panics
///
/// - When providing an invalid brightness value
2024-06-23 15:42:15 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `brightness_grid` points to a valid `SPBrightnessGrid`
/// - `brightness_grid` is not written to or read from concurrently
2024-06-23 15:42:15 +02:00
#[no_mangle]
2024-06-23 16:01:11 +02:00
pub unsafe extern "C" fn sp_brightness_grid_fill(
brightness_grid: *mut SPBrightnessGrid,
2024-06-23 16:01:11 +02:00
value: u8,
) {
let brightness =
Brightness::try_from(value).expect("invalid brightness value");
(*brightness_grid).0.fill(brightness);
2024-06-23 15:42:15 +02:00
}
2024-09-07 14:11:15 +02:00
/// Gets the width of the `SPBrightnessGrid` instance.
2024-06-23 15:42:15 +02:00
///
/// # Arguments
///
/// - `brightness_grid`: instance to read from
2024-06-23 15:42:15 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `brightness_grid` points to a valid `SPBrightnessGrid`
2024-06-23 15:42:15 +02:00
#[no_mangle]
2024-06-23 16:01:11 +02:00
pub unsafe extern "C" fn sp_brightness_grid_width(
brightness_grid: *const SPBrightnessGrid,
2024-06-23 16:01:11 +02:00
) -> usize {
(*brightness_grid).0.width()
2024-06-23 15:42:15 +02:00
}
2024-09-07 14:11:15 +02:00
/// Gets the height of the `SPBrightnessGrid` instance.
2024-06-23 15:42:15 +02:00
///
/// # Arguments
///
/// - `brightness_grid`: instance to read from
2024-06-23 15:42:15 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `brightness_grid` points to a valid `SPBrightnessGrid`
2024-06-23 15:42:15 +02:00
#[no_mangle]
2024-06-23 16:01:11 +02:00
pub unsafe extern "C" fn sp_brightness_grid_height(
brightness_grid: *const SPBrightnessGrid,
2024-06-23 16:01:11 +02:00
) -> usize {
(*brightness_grid).0.height()
2024-06-23 15:42:15 +02:00
}
2024-09-07 14:11:15 +02:00
/// Gets an unsafe reference to the data of the `SPBrightnessGrid` instance.
2024-06-23 15:42:15 +02:00
///
/// # Arguments
///
/// - `brightness_grid`: instance to read from
///
2024-06-23 15:42:15 +02:00
/// ## Safety
///
/// The caller has to make sure that:
///
/// - `brightness_grid` points to a valid `SPBrightnessGrid`
2024-09-07 14:11:15 +02:00
/// - the returned memory range is never accessed after the passed `SPBrightnessGrid` has been freed
/// - the returned memory range is never accessed concurrently, either via the `SPBrightnessGrid` or directly
2024-06-23 15:42:15 +02:00
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref(
brightness_grid: *mut SPBrightnessGrid,
2024-09-05 21:15:53 +02:00
) -> SPByteSlice {
assert_eq!(core::mem::size_of::<Brightness>(), 1);
2024-06-23 15:42:15 +02:00
let data = (*brightness_grid).0.data_ref_mut();
2024-06-23 15:42:15 +02:00
let data: &mut [u8] = transmute(data);
2024-09-05 21:15:53 +02:00
SPByteSlice {
2024-06-23 15:42:15 +02:00
start: data.as_mut_ptr_range().start,
length: data.len(),
}
}