From 8e86afb444ae5f01b1640310cc45585d97022ce2 Mon Sep 17 00:00:00 2001 From: mcb2003 Date: Sat, 12 Dec 2020 16:32:53 +0000 Subject: [PATCH] FFI: Implement tts_new(backend) This required giving the Backends enum and Features struct a C representation. --- src/ffi.rs | 16 +++++++++++++++- src/lib.rs | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ffi.rs b/src/ffi.rs index 71f14d3..16cac9e 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -5,7 +5,7 @@ use std::{ ptr, }; -use crate::TTS; +use crate::{Backends, TTS}; thread_local! { /// Stores the last reported error, so it can be retrieved at will from C @@ -38,6 +38,20 @@ pub extern "C" fn tts_clear_error() { }); } +/// Creates a new TTS object with the specified backend and returns a pointer to it. +/// If an error occured, a null pointer is returned, +/// Call `tts_get_error()` for more information about the specific error. +#[no_mangle] +pub extern "C" fn tts_new(backend: Backends) -> *mut TTS { + match TTS::new(backend) { + Ok(tts) => Box::into_raw(Box::new(tts)), + Err(e) => { + set_last_error(e.to_string()).unwrap(); + ptr::null_mut() + } + } +} + /// Creates a new TTS object with the default backend and returns a pointer to it. /// If an error occured, a null pointer is returned, /// Call `tts_get_error()` for more information about the specific error. diff --git a/src/lib.rs b/src/lib.rs index 7673420..648be45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,6 +32,7 @@ mod backends; #[cfg(feature = "ffi")] pub mod ffi; +#[repr(C)] #[derive(Clone, Copy, Debug)] pub enum Backends { #[cfg(target_os = "linux")] @@ -82,6 +83,7 @@ unsafe impl Send for UtteranceId {} unsafe impl Sync for UtteranceId {} +#[repr(C)] pub struct Features { pub stop: bool, pub rate: bool,