servicepoint/crates/servicepoint_binding_c/src/brightness_grid.rs

313 lines
8.4 KiB
Rust
Raw Normal View History

2024-10-13 18:22:26 +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-10-15 22:23:52 +02:00
use std::ptr::NonNull;
2024-06-23 15:42:15 +02:00
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
/// ```
2024-10-15 21:32:37 +02:00
#[derive(Clone)]
pub struct SPBrightnessGrid(pub(crate) servicepoint::BrightnessGrid);
2024-08-29 22:21:21 +02:00
2024-10-13 18:22:26 +02:00
/// Creates a new [SPBrightnessGrid] with the specified dimensions.
2024-06-23 15:42:15 +02:00
///
2024-10-13 18:22:26 +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-10-15 22:23:52 +02:00
) -> NonNull<SPBrightnessGrid> {
let result = Box::new(SPBrightnessGrid(
servicepoint::BrightnessGrid::new(width, height),
2024-10-15 22:23:52 +02:00
));
NonNull::from(Box::leak(result))
2024-06-23 15:42:15 +02:00
}
2024-10-13 18:22:26 +02:00
/// Loads a [SPBrightnessGrid] with the specified dimensions from the provided data.
///
/// returns: new [SPBrightnessGrid] instance. Will never return NULL.
2024-06-23 15:42:15 +02:00
///
/// # Panics
///
2024-10-13 18:22:26 +02:00
/// - when `data` is NULL
/// - when the provided `data_length` does not match `height` and `width`
2024-06-23 15:42:15 +02:00
///
/// # 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-10-15 22:23:52 +02:00
) -> NonNull<SPBrightnessGrid> {
2024-10-13 18:22:26 +02:00
assert!(!data.is_null());
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");
2024-10-15 22:23:52 +02:00
let result = Box::new(SPBrightnessGrid(grid));
NonNull::from(Box::leak(result))
2024-06-23 15:42:15 +02:00
}
2024-10-13 18:22:26 +02:00
/// Clones a [SPBrightnessGrid].
2024-06-23 15:42:15 +02:00
///
/// # Arguments
///
/// - `brightness_grid`: instance to read from
///
2024-10-13 18:22:26 +02:00
/// returns: new [SPBrightnessGrid] instance. Will never return NULL.
///
/// # Panics
///
/// - when `brightness_grid` is NULL
///
2024-06-23 15:42:15 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
2024-10-13 18:22:26 +02:00
/// - `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-10-15 22:23:52 +02:00
) -> NonNull<SPBrightnessGrid> {
2024-10-13 18:22:26 +02:00
assert!(!brightness_grid.is_null());
2024-10-15 22:23:52 +02:00
let result = Box::new((*brightness_grid).clone());
NonNull::from(Box::leak(result))
2024-06-23 15:42:15 +02:00
}
2024-10-13 18:22:26 +02:00
/// Deallocates a [SPBrightnessGrid].
2024-06-23 15:42:15 +02:00
///
/// # Arguments
///
/// - `brightness_grid`: instance to read from
///
2024-10-13 18:22:26 +02:00
/// # Panics
///
/// - when `brightness_grid` is NULL
///
2024-06-23 15:42:15 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
2024-10-13 18:22:26 +02:00
/// - `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
) {
2024-10-13 18:22:26 +02:00
assert!(!brightness_grid.is_null());
_ = 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
///
2024-10-13 18:22:26 +02:00
/// returns: value at position
///
2024-06-23 15:42:15 +02:00
/// # Panics
///
2024-10-13 18:22:26 +02:00
/// - when `brightness_grid` is NULL
/// - When accessing `x` or `y` out of bounds.
2024-06-23 15:42:15 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
2024-10-13 18:22:26 +02:00
/// - `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 {
2024-10-13 18:22:26 +02:00
assert!(!brightness_grid.is_null());
(*brightness_grid).0.get(x, y).into()
2024-06-23 15:42:15 +02:00
}
2024-10-13 18:22:26 +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
///
2024-10-13 18:22:26 +02:00
/// - when `brightness_grid` is NULL
/// - 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-10-13 18:22:26 +02:00
assert!(!brightness_grid.is_null());
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-10-13 18:22:26 +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
///
2024-10-13 18:22:26 +02:00
/// - when `brightness_grid` is NULL
/// - 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:22:26 +02:00
/// - `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,
) {
2024-10-13 18:22:26 +02:00
assert!(!brightness_grid.is_null());
2024-06-23 16:01:11 +02:00
let brightness =
Brightness::try_from(value).expect("invalid brightness value");
(*brightness_grid).0.fill(brightness);
2024-06-23 15:42:15 +02:00
}
2024-10-13 18:22:26 +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
///
2024-10-13 18:22:26 +02:00
/// returns: width
///
/// # Panics
///
/// - when `brightness_grid` is NULL
///
2024-06-23 15:42:15 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
2024-10-13 18:22:26 +02:00
/// - `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 {
2024-10-13 18:22:26 +02:00
assert!(!brightness_grid.is_null());
(*brightness_grid).0.width()
2024-06-23 15:42:15 +02:00
}
2024-10-13 18:22:26 +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
///
2024-10-13 18:22:26 +02:00
/// returns: height
///
/// # Panics
///
/// - when `brightness_grid` is NULL
///
2024-06-23 15:42:15 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
2024-10-13 18:22:26 +02:00
/// - `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 {
2024-10-13 18:22:26 +02:00
assert!(!brightness_grid.is_null());
(*brightness_grid).0.height()
2024-06-23 15:42:15 +02:00
}
2024-10-13 18:22:26 +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-10-13 18:22:26 +02:00
/// returns: slice of bytes underlying the `brightness_grid`.
///
/// # Panics
///
/// - when `brightness_grid` is NULL
///
/// # Safety
2024-06-23 15:42:15 +02:00
///
/// The caller has to make sure that:
///
2024-10-13 18:22:26 +02:00
/// - `brightness_grid` points to a valid [SPBrightnessGrid]
/// - 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 {
2024-10-13 18:22:26 +02:00
assert!(!brightness_grid.is_null());
assert_eq!(core::mem::size_of::<Brightness>(), 1);
let data = (*brightness_grid).0.data_ref_mut();
2024-10-15 22:23:52 +02:00
// this assumes more about the memory layout than rust guarantees. yikes!
2024-06-23 15:42:15 +02:00
let data: &mut [u8] = transmute(data);
2024-09-05 21:15:53 +02:00
SPByteSlice {
2024-10-15 22:23:52 +02:00
start: NonNull::new(data.as_mut_ptr_range().start).unwrap(),
2024-06-23 15:42:15 +02:00
length: data.len(),
}
}