use crate::commands::render_all_commands;
use crate::ffi::util::{slice_from, state_mut, state_ref};
use crate::terminal::TerminalState;
use std::ffi::c_void;
use std::os::raw::c_char;
#[no_mangle]
pub extern "C" fn ratatui_create(cols: u16, rows: u16, font_size: f32) -> *mut c_void {
let state = Box::new(TerminalState::new(cols, rows, font_size));
Box::into_raw(state) as *mut c_void
}
#[no_mangle]
pub extern "C" fn ratatui_destroy(handle: *mut c_void) {
if !handle.is_null() {
unsafe { drop(Box::from_raw(handle as *mut TerminalState)); }
}
}
#[no_mangle]
pub extern "C" fn ratatui_set_custom_font(
handle: *mut c_void,
font_data: *const u8,
font_len: u32,
) -> u8 {
if font_data.is_null() || font_len == 0 { return 0; }
let Some(state) = state_mut(handle) else { return 0; };
let bytes = slice_from(font_data, font_len as usize);
let ok = state.font.set_custom_font(bytes);
if ok {
state.resync_pixel_dimensions();
}
u8::from(ok)
}
#[no_mangle]
pub extern "C" fn ratatui_begin_frame(handle: *mut c_void) {
let Some(state) = state_mut(handle) else { return; };
state.begin_frame();
}
#[no_mangle]
pub extern "C" fn ratatui_end_frame(handle: *mut c_void) -> *const u8 {
let Some(state) = state_mut(handle) else { return std::ptr::null(); };
render_all_commands(state);
state.rasterize();
state.pixel_buffer.as_ptr()
}
#[no_mangle]
pub extern "C" fn ratatui_end_frame_hashed(handle: *mut c_void) -> *const u8 {
let Some(state) = state_mut(handle) else { return std::ptr::null(); };
render_all_commands(state);
let hash = {
let buffer = state.terminal.backend().buffer();
crate::renderer::compute_buffer_hash(buffer)
};
if state.last_buffer_hash == Some(hash) {
return std::ptr::null();
}
state.last_buffer_hash = Some(hash);
state.rasterize();
state.pixel_buffer.as_ptr()
}
#[no_mangle]
pub extern "C" fn ratatui_pixel_width(handle: *const c_void) -> u32 {
let Some(state) = state_ref(handle) else { return 0; };
state.pixel_width
}
#[no_mangle]
pub extern "C" fn ratatui_pixel_height(handle: *const c_void) -> u32 {
let Some(state) = state_ref(handle) else { return 0; };
state.pixel_height
}
#[no_mangle]
pub extern "C" fn ratatui_version() -> *const c_char {
concat!(env!("CARGO_PKG_VERSION"), "\0").as_ptr() as *const c_char
}