From ca2e0ffe5e16414bd0154260f9e46483d20ff60d Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 19 Aug 2020 17:28:22 -0500 Subject: [PATCH] Upgrades and refactors. * Bump editions to 2018. * Bump dependencies and crate versions. * Implement callbacks. * Return message IDs when speaking, or `None` in case of errors. * Add callbacks to example. --- speech-dispatcher-sys/Cargo.toml | 5 +- speech-dispatcher/Cargo.toml | 6 +- speech-dispatcher/examples/hello_world.rs | 7 +- speech-dispatcher/src/lib.rs | 433 ++++++++++++++++------ 4 files changed, 326 insertions(+), 125 deletions(-) diff --git a/speech-dispatcher-sys/Cargo.toml b/speech-dispatcher-sys/Cargo.toml index 113199f..b046462 100644 --- a/speech-dispatcher-sys/Cargo.toml +++ b/speech-dispatcher-sys/Cargo.toml @@ -1,11 +1,12 @@ [package] name = "speech-dispatcher-sys" -version = "0.4.2" +version = "0.5.0" authors = ["Nolan Darilek "] repository = "https://gitlab.com/ndarilek/speech-dispatcher-rs" description = "speech-dispatcher system bindings" license = "LGPL-2.1" +edition = "2018" [build-dependencies] -bindgen = "0.53" +bindgen = "0.54" gcc = "0.3" diff --git a/speech-dispatcher/Cargo.toml b/speech-dispatcher/Cargo.toml index 65acabc..40fcfc6 100644 --- a/speech-dispatcher/Cargo.toml +++ b/speech-dispatcher/Cargo.toml @@ -1,10 +1,12 @@ [package] name = "speech-dispatcher" -version = "0.4.3" +version = "0.5.0" authors = ["Nolan Darilek "] repository = "https://gitlab.com/ndarilek/speech-dispatcher-rs" description = "Rusty interface to the speech-dispatcher speech synthesis library" license = "LGPL-2.1" +edition = "2018" [dependencies] -speech-dispatcher-sys = { version = "0.4", path = "../speech-dispatcher-sys" } +lazy_static = "1" +speech-dispatcher-sys = { version = "0.5", path = "../speech-dispatcher-sys" } diff --git a/speech-dispatcher/examples/hello_world.rs b/speech-dispatcher/examples/hello_world.rs index ea0e5a7..5c8176f 100644 --- a/speech-dispatcher/examples/hello_world.rs +++ b/speech-dispatcher/examples/hello_world.rs @@ -1,14 +1,17 @@ extern crate speech_dispatcher; use speech_dispatcher::*; +use std::io; fn main() { let connection = speech_dispatcher::Connection::open( "hello_world", "hello_world", "hello_world", - Mode::Single, + Mode::Threaded, ); + connection.on_begin(Some(|id| println!("Beginning {}", id))); + connection.on_end(Some(|id| println!("Ending {}", id))); connection.say( Priority::Important, format!("Hello, world at rate {}.", connection.get_voice_rate()), @@ -25,4 +28,6 @@ fn main() { "This statement, unlike others, has punctuation that is spoken!", ); connection.set_punctuation(Punctuation::None); + let mut _input = String::new(); + io::stdin().read_line(&mut _input).unwrap(); } diff --git a/speech-dispatcher/src/lib.rs b/speech-dispatcher/src/lib.rs index ce889c6..63f8d47 100644 --- a/speech-dispatcher/src/lib.rs +++ b/speech-dispatcher/src/lib.rs @@ -1,91 +1,181 @@ #![allow(non_upper_case_globals)] -extern crate speech_dispatcher_sys; +use std::collections::HashMap; use std::ffi::{CStr, CString}; use std::marker::Send; +use std::sync::Mutex; +use lazy_static::lazy_static; use speech_dispatcher_sys::*; +#[derive(Clone, Copy, Debug)] +#[repr(u32)] pub enum Mode { - Single = SPDConnectionMode::SPD_MODE_SINGLE as isize, - Threaded = SPDConnectionMode::SPD_MODE_THREADED as isize, + Single = SPDConnectionMode::SPD_MODE_SINGLE, + Threaded = SPDConnectionMode::SPD_MODE_THREADED, } +#[derive(Clone, Copy, Debug)] +#[repr(u32)] pub enum Priority { - Important = SPDPriority::SPD_IMPORTANT as isize, - Message = SPDPriority::SPD_MESSAGE as isize, - Text = SPDPriority::SPD_TEXT as isize, - Notification = SPDPriority::SPD_NOTIFICATION as isize, - Progress = SPDPriority::SPD_PROGRESS as isize, + Important = SPDPriority::SPD_IMPORTANT, + Message = SPDPriority::SPD_MESSAGE, + Text = SPDPriority::SPD_TEXT, + Notification = SPDPriority::SPD_NOTIFICATION, + Progress = SPDPriority::SPD_PROGRESS, } +#[derive(Clone, Copy, Debug)] +#[repr(u32)] pub enum VoiceType { - Male1 = SPDVoiceType::SPD_MALE1 as isize, - Male2 = SPDVoiceType::SPD_MALE2 as isize, - Male3 = SPDVoiceType::SPD_MALE3 as isize, - Female1 = SPDVoiceType::SPD_FEMALE1 as isize, - Female2 = SPDVoiceType::SPD_FEMALE2 as isize, - Female3 = SPDVoiceType::SPD_FEMALE3 as isize, - ChildMale = SPDVoiceType::SPD_CHILD_MALE as isize, - ChildFemale = SPDVoiceType::SPD_CHILD_FEMALE as isize, + Male1 = SPDVoiceType::SPD_MALE1, + Male2 = SPDVoiceType::SPD_MALE2, + Male3 = SPDVoiceType::SPD_MALE3, + Female1 = SPDVoiceType::SPD_FEMALE1, + Female2 = SPDVoiceType::SPD_FEMALE2, + Female3 = SPDVoiceType::SPD_FEMALE3, + ChildMale = SPDVoiceType::SPD_CHILD_MALE, + ChildFemale = SPDVoiceType::SPD_CHILD_FEMALE, } -pub struct Connection { - connection: *mut SPDConnection, -} +#[derive(Clone, Debug)] +pub struct Connection(pub *mut SPDConnection, u64); pub type Address = SPDConnectionAddress; +#[derive(Clone, Copy, Debug)] +#[repr(u32)] pub enum DataMode { - Text = SPDDataMode::SPD_DATA_TEXT as isize, - SSML = SPDDataMode::SPD_DATA_SSML as isize, + Text = SPDDataMode::SPD_DATA_TEXT, + SSML = SPDDataMode::SPD_DATA_SSML, } +#[derive(Clone, Copy, Debug)] +#[repr(u32)] pub enum Notification { - Begin = SPDNotification::SPD_BEGIN as isize, - End = SPDNotification::SPD_END as isize, - IndexMarks = SPDNotification::SPD_INDEX_MARKS as isize, - Cancel = SPDNotification::SPD_CANCEL as isize, - Pause = SPDNotification::SPD_PAUSE as isize, - Resume = SPDNotification::SPD_RESUME as isize, - All = SPDNotification::SPD_ALL as isize, + Begin = SPDNotification::SPD_BEGIN, + End = SPDNotification::SPD_END, + IndexMarks = SPDNotification::SPD_INDEX_MARKS, + Cancel = SPDNotification::SPD_CANCEL, + Pause = SPDNotification::SPD_PAUSE, + Resume = SPDNotification::SPD_RESUME, + All = SPDNotification::SPD_ALL, } +#[derive(Clone, Copy, Debug)] +#[repr(u32)] pub enum Punctuation { - All = SPDPunctuation::SPD_PUNCT_ALL as isize, - None = SPDPunctuation::SPD_PUNCT_NONE as isize, - Some = SPDPunctuation::SPD_PUNCT_SOME as isize, + All = SPDPunctuation::SPD_PUNCT_ALL, + None = SPDPunctuation::SPD_PUNCT_NONE, + Some = SPDPunctuation::SPD_PUNCT_SOME, } +#[derive(Clone, Copy, Debug)] +#[repr(u32)] pub enum CapitalLetters { - None = SPDCapitalLetters::SPD_CAP_NONE as isize, - Spell = SPDCapitalLetters::SPD_CAP_SPELL as isize, - Icon = SPDCapitalLetters::SPD_CAP_ICON as isize, + None = SPDCapitalLetters::SPD_CAP_NONE, + Spell = SPDCapitalLetters::SPD_CAP_SPELL, + Icon = SPDCapitalLetters::SPD_CAP_ICON, } fn i32_to_bool(v: i32) -> bool { v == 1 } +#[derive(Clone, Copy)] +struct Callbacks { + begin: Option, + end: Option, + index_mark: Option, + cancel: Option, + pause: Option, + resume: Option, +} + +impl Default for Callbacks { + fn default() -> Self { + Callbacks { + begin: None, + end: None, + index_mark: None, + cancel: None, + pause: None, + resume: None, + } + } +} + +lazy_static! { + static ref callbacks: Mutex> = { + let m = HashMap::new(); + Mutex::new(m) + }; +} + +unsafe extern "C" fn cb(msg_id: u64, client_id: u64, state: u32) { + let state = match state { + SPDNotificationType_SPD_EVENT_BEGIN => Notification::Begin, + SPDNotificationType_SPD_EVENT_END => Notification::End, + SPDNotificationType_SPD_EVENT_CANCEL => Notification::Cancel, + SPDNotificationType_SPD_EVENT_PAUSE => Notification::Pause, + SPDNotificationType_SPD_EVENT_RESUME => Notification::Resume, + _ => panic!("Unknown notification received in callback: {}", state), + }; + if let Some(c) = callbacks.lock().unwrap().get(&client_id) { + let f = match state { + Notification::Begin => c.begin, + Notification::End => c.end, + Notification::Cancel => c.cancel, + Notification::Pause => c.pause, + Notification::Resume => c.resume, + _ => panic!("Unknown notification type"), + }; + if let Some(f) = f { + f(msg_id); + } + } +} + +unsafe extern "C" fn cb_im(msg_id: u64, client_id: u64, state: u32, index_mark: *mut i8) { + let index_mark = CStr::from_ptr(index_mark); + let index_mark = index_mark.to_string_lossy().to_string(); + let state = match state { + SPDNotificationType_SPD_EVENT_INDEX_MARK => Notification::IndexMarks, + _ => panic!("Unknown notification received in IM callback: {}", state), + }; + if let Some(c) = callbacks.lock().unwrap().get(&client_id) { + let f = match state { + Notification::IndexMarks => c.index_mark, + _ => panic!("Unknown notification type"), + }; + if let Some(f) = f { + f(msg_id, index_mark); + } + } +} + impl Connection { pub fn open>( client_name: S, connection_name: S, user_name: S, mode: Mode, - ) -> Connection { + ) -> Self { let clientname = CString::new(client_name.into()).unwrap(); let connectionname = CString::new(connection_name.into()).unwrap(); let username = CString::new(user_name.into()).unwrap(); let connection = unsafe { - spd_open( + let c = spd_open( clientname.as_ptr(), connectionname.as_ptr(), username.as_ptr(), mode as u32, - ) + ); + Self::setup_connection(c) }; - Connection { connection } + let mut c = Self(connection, 0); + c.setup(); + c } pub unsafe fn open2>( @@ -95,148 +185,183 @@ impl Connection { mode: Mode, address: *mut Address, autospawn: bool, - ) -> Connection { + ) -> Self { let auto_spawn = if autospawn { 1 } else { 0 }; let error_result = vec![CString::new("").unwrap().into_raw()].as_mut_ptr(); let clientname = CString::new(client_name.into()).unwrap(); let connectionname = CString::new(connection_name.into()).unwrap(); let username = CString::new(user_name.into()).unwrap(); - let connection = spd_open2( - clientname.as_ptr(), - connectionname.as_ptr(), - username.as_ptr(), - mode as u32, - address, - auto_spawn, - error_result, - ); - Connection { connection } + let connection = { + let c = spd_open2( + clientname.as_ptr(), + connectionname.as_ptr(), + username.as_ptr(), + mode as u32, + address, + auto_spawn, + error_result, + ); + Self::setup_connection(c) + }; + let mut c = Self(connection, 0); + c.setup(); + c + } + + unsafe fn setup_connection(c: *mut SPDConnection) -> *mut SPDConnection { + (*c).callback_begin = Some(cb); + (*c).callback_end = Some(cb); + (*c).callback_cancel = Some(cb); + (*c).callback_pause = Some(cb); + (*c).callback_resume = Some(cb); + (*c).callback_im = Some(cb_im); + c + } + + fn setup(&mut self) { + let client_id = self.send_data("HISTORY GET CLIENT_ID\r\n", true); + if let Some(client_id) = client_id { + let client_id: Vec<&str> = client_id.split("\r\n").collect(); + let client_id = client_id.get(0); + if let Some(client_id) = client_id { + let client_id: Vec<&str> = client_id.split("-").collect(); + if let Some(client_id) = client_id.get(1) { + if let Some(client_id) = client_id.parse::().ok() { + self.1 = client_id; + } + } + } + } + callbacks.lock().unwrap().insert(self.1, Default::default()); + self.set_notification_on(Notification::All); } pub fn close(&self) { - unsafe { spd_close(self.connection) }; + unsafe { spd_close(self.0) }; } - pub fn say>(&self, priority: Priority, text: S) -> bool { + pub fn say>(&self, priority: Priority, text: S) -> Option { let text: String = text.into(); - if text.is_empty() { - return true; - } let param = CString::new(text).unwrap(); - let v = unsafe { spd_say(self.connection, priority as u32, param.as_ptr()) }; - i32_to_bool(v) + let rv = unsafe { spd_say(self.0, priority as u32, param.as_ptr()) }; + if rv != -1 { + Some(rv) + } else { + None + } } - pub fn sayf>(&self, priority: Priority, format: S) -> bool { + pub fn sayf>(&self, priority: Priority, format: S) -> Option { let format: String = format.into(); - if format.is_empty() { - return true; - } let param = CString::new(format).unwrap(); - let v = unsafe { spd_sayf(self.connection, priority as u32, param.as_ptr()) }; - i32_to_bool(v) + let rv = unsafe { spd_sayf(self.0, priority as u32, param.as_ptr()) }; + if rv != -1 { + Some(rv) + } else { + None + } } pub fn stop(&self) -> bool { - let v = unsafe { spd_stop(self.connection) }; + let v = unsafe { spd_stop(self.0) }; i32_to_bool(v) } pub fn stop_all(&self) -> bool { - let v = unsafe { spd_stop_all(self.connection) }; + let v = unsafe { spd_stop_all(self.0) }; i32_to_bool(v) } pub fn stop_uid(&self, target_uid: i32) -> bool { - let v = unsafe { spd_stop_uid(self.connection, target_uid) }; + let v = unsafe { spd_stop_uid(self.0, target_uid) }; i32_to_bool(v) } pub fn cancel(&self) -> bool { - let v = unsafe { spd_cancel(self.connection) }; + let v = unsafe { spd_cancel(self.0) }; i32_to_bool(v) } pub fn cancel_all(&self) -> bool { - let v = unsafe { spd_cancel_all(self.connection) }; + let v = unsafe { spd_cancel_all(self.0) }; i32_to_bool(v) } pub fn cancel_uid(&self, target_uid: i32) -> bool { - let v = unsafe { spd_cancel_uid(self.connection, target_uid) }; + let v = unsafe { spd_cancel_uid(self.0, target_uid) }; i32_to_bool(v) } pub fn pause(&self) -> bool { - let v = unsafe { spd_pause(self.connection) }; + let v = unsafe { spd_pause(self.0) }; i32_to_bool(v) } pub fn pause_all(&self) -> bool { - let v = unsafe { spd_pause_all(self.connection) }; + let v = unsafe { spd_pause_all(self.0) }; i32_to_bool(v) } pub fn pause_uid(&self, target_uid: i32) -> bool { - let v = unsafe { spd_pause_uid(self.connection, target_uid) }; + let v = unsafe { spd_pause_uid(self.0, target_uid) }; i32_to_bool(v) } pub fn resume(&self) -> bool { - let v = unsafe { spd_resume(self.connection) }; + let v = unsafe { spd_resume(self.0) }; i32_to_bool(v) } pub fn resume_all(&self) -> bool { - let v = unsafe { spd_resume_all(self.connection) }; + let v = unsafe { spd_resume_all(self.0) }; i32_to_bool(v) } pub fn resume_uid(&self, target_uid: i32) -> bool { - let v = unsafe { spd_resume_uid(self.connection, target_uid) }; + let v = unsafe { spd_resume_uid(self.0, target_uid) }; i32_to_bool(v) } pub fn key>(&self, priority: Priority, key_name: S) -> bool { let param = CString::new(key_name.into()).unwrap(); - let v = unsafe { spd_key(self.connection, priority as u32, param.as_ptr()) }; + let v = unsafe { spd_key(self.0, priority as u32, param.as_ptr()) }; i32_to_bool(v) } pub fn char>(&self, priority: Priority, char: S) -> bool { let param = CString::new(char.into()).unwrap(); - let v = unsafe { spd_char(self.connection, priority as u32, param.as_ptr()) }; + let v = unsafe { spd_char(self.0, priority as u32, param.as_ptr()) }; i32_to_bool(v) } pub fn wchar(&self, priority: Priority, wchar: i32) -> bool { - let v = unsafe { spd_wchar(self.connection, priority as u32, wchar) }; + let v = unsafe { spd_wchar(self.0, priority as u32, wchar) }; i32_to_bool(v) } pub fn sound_icon>(&self, priority: Priority, icon_name: S) -> bool { let param = CString::new(icon_name.into()).unwrap(); - let v = unsafe { spd_char(self.connection, priority as u32, param.as_ptr()) }; + let v = unsafe { spd_char(self.0, priority as u32, param.as_ptr()) }; i32_to_bool(v) } pub fn set_voice_type(&self, voice_type: VoiceType) -> bool { - let v = unsafe { spd_set_voice_type(self.connection, voice_type as u32) }; + let v = unsafe { spd_set_voice_type(self.0, voice_type as u32) }; i32_to_bool(v) } pub fn set_voice_type_all(&self, voice_type: VoiceType) -> bool { - let v = unsafe { spd_set_voice_type_all(self.connection, voice_type as u32) }; + let v = unsafe { spd_set_voice_type_all(self.0, voice_type as u32) }; i32_to_bool(v) } pub fn set_voice_type_uid(&self, voice_type: VoiceType, target_uid: u32) -> bool { - let v = unsafe { spd_set_voice_type_uid(self.connection, voice_type as u32, target_uid) }; + let v = unsafe { spd_set_voice_type_uid(self.0, voice_type as u32, target_uid) }; i32_to_bool(v) } pub fn get_voice_type(&self) -> VoiceType { - let v = unsafe { spd_get_voice_type(self.connection) }; + let v = unsafe { spd_get_voice_type(self.0) }; match v { SPDVoiceType::SPD_MALE1 => VoiceType::Male1, SPDVoiceType::SPD_MALE2 => VoiceType::Male2, @@ -252,123 +377,122 @@ impl Connection { pub fn set_synthesis_voice>(&self, voice_name: S) -> bool { let param = CString::new(voice_name.into()).unwrap(); - let v = unsafe { spd_set_synthesis_voice(self.connection, param.as_ptr()) }; + let v = unsafe { spd_set_synthesis_voice(self.0, param.as_ptr()) }; i32_to_bool(v) } pub fn set_synthesis_voice_all>(&self, voice_name: S) -> bool { let param = CString::new(voice_name.into()).unwrap(); - let v = unsafe { spd_set_synthesis_voice_all(self.connection, param.as_ptr()) }; + let v = unsafe { spd_set_synthesis_voice_all(self.0, param.as_ptr()) }; i32_to_bool(v) } pub fn set_synthesis_voice_uid>(&self, voice_name: S, target_uid: u32) -> bool { let param = CString::new(voice_name.into()).unwrap(); - let v = unsafe { spd_set_synthesis_voice_uid(self.connection, param.as_ptr(), target_uid) }; + let v = unsafe { spd_set_synthesis_voice_uid(self.0, param.as_ptr(), target_uid) }; i32_to_bool(v) } pub fn set_data_mode(&self, mode: DataMode) -> bool { - let v = unsafe { spd_set_data_mode(self.connection, mode as u32) }; + let v = unsafe { spd_set_data_mode(self.0, mode as u32) }; i32_to_bool(v) } pub fn set_notification_on(&self, notification: Notification) -> bool { - let v = unsafe { spd_set_notification_on(self.connection, notification as u32) }; + let v = unsafe { spd_set_notification_on(self.0, notification as u32) }; i32_to_bool(v) } pub fn set_notification_off(&self, notification: Notification) -> bool { - let v = unsafe { spd_set_notification_off(self.connection, notification as u32) }; + let v = unsafe { spd_set_notification_off(self.0, notification as u32) }; i32_to_bool(v) } pub fn set_notification>(&self, notification: Notification, state: S) -> bool { let param = CString::new(state.into()).unwrap(); - let v = - unsafe { spd_set_notification(self.connection, notification as u32, param.as_ptr()) }; + let v = unsafe { spd_set_notification(self.0, notification as u32, param.as_ptr()) }; i32_to_bool(v) } pub fn set_voice_rate(&self, rate: i32) -> bool { - let v = unsafe { spd_set_voice_rate(self.connection, rate) }; + let v = unsafe { spd_set_voice_rate(self.0, rate) }; i32_to_bool(v) } pub fn set_voice_rate_all(&self, rate: i32) -> bool { - let v = unsafe { spd_set_voice_rate_all(self.connection, rate) }; + let v = unsafe { spd_set_voice_rate_all(self.0, rate) }; i32_to_bool(v) } pub fn set_voice_rate_uid(&self, rate: i32, target_uid: u32) -> bool { - let v = unsafe { spd_set_voice_rate_uid(self.connection, rate, target_uid) }; + let v = unsafe { spd_set_voice_rate_uid(self.0, rate, target_uid) }; i32_to_bool(v) } pub fn get_voice_rate(&self) -> i32 { - unsafe { spd_get_voice_rate(self.connection) } + unsafe { spd_get_voice_rate(self.0) } } pub fn set_voice_pitch(&self, pitch: i32) -> bool { - let v = unsafe { spd_set_voice_pitch(self.connection, pitch) }; + let v = unsafe { spd_set_voice_pitch(self.0, pitch) }; i32_to_bool(v) } pub fn set_voice_pitch_all(&self, pitch: i32) -> bool { - let v = unsafe { spd_set_voice_pitch_all(self.connection, pitch) }; + let v = unsafe { spd_set_voice_pitch_all(self.0, pitch) }; i32_to_bool(v) } pub fn set_voice_pitch_uid(&self, pitch: i32, target_uid: u32) -> bool { - let v = unsafe { spd_set_voice_pitch_uid(self.connection, pitch, target_uid) }; + let v = unsafe { spd_set_voice_pitch_uid(self.0, pitch, target_uid) }; i32_to_bool(v) } pub fn get_voice_pitch(&self) -> i32 { - unsafe { spd_get_voice_pitch(self.connection) } + unsafe { spd_get_voice_pitch(self.0) } } pub fn set_volume(&self, volume: i32) -> bool { - let v = unsafe { spd_set_volume(self.connection, volume) }; + let v = unsafe { spd_set_volume(self.0, volume) }; i32_to_bool(v) } pub fn set_volume_all(&self, volume: i32) -> bool { - let v = unsafe { spd_set_volume_all(self.connection, volume) }; + let v = unsafe { spd_set_volume_all(self.0, volume) }; i32_to_bool(v) } pub fn set_volume_uid(&self, volume: i32, target_uid: u32) -> bool { - let v = unsafe { spd_set_volume_uid(self.connection, volume, target_uid) }; + let v = unsafe { spd_set_volume_uid(self.0, volume, target_uid) }; i32_to_bool(v) } pub fn get_volume(&self) -> i32 { - unsafe { spd_get_volume(self.connection) } + unsafe { spd_get_volume(self.0) } } pub fn set_punctuation(&self, punctuation: Punctuation) -> bool { - let v = unsafe { spd_set_punctuation(self.connection, punctuation as u32) }; + let v = unsafe { spd_set_punctuation(self.0, punctuation as u32) }; i32_to_bool(v) } pub fn set_punctuation_all(&self, punctuation: Punctuation) -> bool { - let v = unsafe { spd_set_punctuation_all(self.connection, punctuation as u32) }; + let v = unsafe { spd_set_punctuation_all(self.0, punctuation as u32) }; i32_to_bool(v) } pub fn set_punctuation_uid(&self, punctuation: Punctuation, target_uid: u32) -> bool { - let v = unsafe { spd_set_punctuation_uid(self.connection, punctuation as u32, target_uid) }; + let v = unsafe { spd_set_punctuation_uid(self.0, punctuation as u32, target_uid) }; i32_to_bool(v) } pub fn set_capital_letters(&self, capital_letters: CapitalLetters) -> bool { - let v = unsafe { spd_set_capital_letters(self.connection, capital_letters as u32) }; + let v = unsafe { spd_set_capital_letters(self.0, capital_letters as u32) }; i32_to_bool(v) } pub fn set_capital_letters_all(&self, capital_letters: CapitalLetters) -> bool { - let v = unsafe { spd_set_capital_letters_all(self.connection, capital_letters as u32) }; + let v = unsafe { spd_set_capital_letters_all(self.0, capital_letters as u32) }; i32_to_bool(v) } @@ -377,9 +501,7 @@ impl Connection { capital_letters: CapitalLetters, target_uid: u32, ) -> bool { - let v = unsafe { - spd_set_capital_letters_uid(self.connection, capital_letters as u32, target_uid) - }; + let v = unsafe { spd_set_capital_letters_uid(self.0, capital_letters as u32, target_uid) }; i32_to_bool(v) } @@ -389,7 +511,7 @@ impl Connection { } else { SPDSpelling::SPD_SPELL_OFF }; - let v = unsafe { spd_set_spelling(self.connection, s) }; + let v = unsafe { spd_set_spelling(self.0, s) }; i32_to_bool(v) } @@ -399,7 +521,7 @@ impl Connection { } else { SPDSpelling::SPD_SPELL_OFF }; - let v = unsafe { spd_set_spelling_all(self.connection, s) }; + let v = unsafe { spd_set_spelling_all(self.0, s) }; i32_to_bool(v) } @@ -409,42 +531,42 @@ impl Connection { } else { SPDSpelling::SPD_SPELL_OFF }; - let v = unsafe { spd_set_spelling_uid(self.connection, s, target_uid) }; + let v = unsafe { spd_set_spelling_uid(self.0, s, target_uid) }; i32_to_bool(v) } pub fn set_language>(&self, language: S) -> bool { let param = CString::new(language.into()).unwrap(); - let v = unsafe { spd_set_language(self.connection, param.as_ptr()) }; + let v = unsafe { spd_set_language(self.0, param.as_ptr()) }; i32_to_bool(v) } pub fn set_language_all>(&self, language: S) -> bool { let param = CString::new(language.into()).unwrap(); - let v = unsafe { spd_set_language_all(self.connection, param.as_ptr()) }; + let v = unsafe { spd_set_language_all(self.0, param.as_ptr()) }; i32_to_bool(v) } pub fn set_language_uid>(&self, language: S, target_uid: u32) -> bool { let param = CString::new(language.into()).unwrap(); - let v = unsafe { spd_set_language_uid(self.connection, param.as_ptr(), target_uid) }; + let v = unsafe { spd_set_language_uid(self.0, param.as_ptr(), target_uid) }; i32_to_bool(v) } pub fn get_language(&self) -> &str { - let v = unsafe { CStr::from_ptr(spd_get_language(self.connection)) }; + let v = unsafe { CStr::from_ptr(spd_get_language(self.0)) }; v.to_str().unwrap() } pub fn set_output_module>(&self, output_module: S) -> bool { let param = CString::new(output_module.into()).unwrap(); - let v = unsafe { spd_set_output_module(self.connection, param.as_ptr()) }; + let v = unsafe { spd_set_output_module(self.0, param.as_ptr()) }; i32_to_bool(v) } pub fn set_output_module_all>(&self, output_module: S) -> bool { let param = CString::new(output_module.into()).unwrap(); - let v = unsafe { spd_set_output_module_all(self.connection, param.as_ptr()) }; + let v = unsafe { spd_set_output_module_all(self.0, param.as_ptr()) }; i32_to_bool(v) } @@ -454,9 +576,79 @@ impl Connection { target_uid: u32, ) -> bool { let param = CString::new(output_module.into()).unwrap(); - let v = unsafe { spd_set_output_module_uid(self.connection, param.as_ptr(), target_uid) }; + let v = unsafe { spd_set_output_module_uid(self.0, param.as_ptr(), target_uid) }; i32_to_bool(v) } + + pub fn send_data>(&self, data: S, wait_for_reply: bool) -> Option { + let wfr: i32 = if wait_for_reply { + SPD_WAIT_REPLY as i32 + } else { + SPD_NO_REPLY as i32 + }; + let data = CString::new(data.into()).unwrap(); + let rv = unsafe { spd_send_data(self.0, data.as_ptr(), wfr) }; + if rv.is_null() { + None + } else { + let rv = unsafe { CStr::from_ptr(rv) }; + Some(rv.to_string_lossy().to_string()) + } + } + + pub fn on_begin(&self, f: Option) { + if let Ok(mut cbs) = callbacks.lock() { + let cb = cbs.get_mut(&self.1); + if let Some(cb) = cb { + cb.begin = f; + } + } + } + + pub fn on_end(&self, f: Option) { + if let Ok(mut cbs) = callbacks.lock() { + let cb = cbs.get_mut(&self.1); + if let Some(cb) = cb { + cb.end = f; + } + } + } + + pub fn on_cancel(&self, f: Option) { + if let Ok(mut cbs) = callbacks.lock() { + let cb = cbs.get_mut(&self.1); + if let Some(cb) = cb { + cb.cancel = f; + } + } + } + + pub fn on_pause(&self, f: Option) { + if let Ok(mut cbs) = callbacks.lock() { + let cb = cbs.get_mut(&self.1); + if let Some(cb) = cb { + cb.pause = f; + } + } + } + + pub fn on_resume(&self, f: Option) { + if let Ok(mut cbs) = callbacks.lock() { + let cb = cbs.get_mut(&self.1); + if let Some(cb) = cb { + cb.resume = f; + } + } + } + + pub fn on_index_mark(&self, f: Option) { + if let Ok(mut cbs) = callbacks.lock() { + let cb = cbs.get_mut(&self.1); + if let Some(cb) = cb { + cb.index_mark = f; + } + } + } } unsafe impl Send for Connection {} @@ -464,5 +656,6 @@ unsafe impl Send for Connection {} impl Drop for Connection { fn drop(&mut self) { self.close(); + callbacks.lock().unwrap().remove(&self.1); } }