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.
This commit is contained in:
Nolan Darilek 2020-08-19 17:28:22 -05:00
parent 0de61beafe
commit ca2e0ffe5e
4 changed files with 326 additions and 125 deletions

View File

@ -1,11 +1,12 @@
[package] [package]
name = "speech-dispatcher-sys" name = "speech-dispatcher-sys"
version = "0.4.2" version = "0.5.0"
authors = ["Nolan Darilek <nolan@thewordnerd.info>"] authors = ["Nolan Darilek <nolan@thewordnerd.info>"]
repository = "https://gitlab.com/ndarilek/speech-dispatcher-rs" repository = "https://gitlab.com/ndarilek/speech-dispatcher-rs"
description = "speech-dispatcher system bindings" description = "speech-dispatcher system bindings"
license = "LGPL-2.1" license = "LGPL-2.1"
edition = "2018"
[build-dependencies] [build-dependencies]
bindgen = "0.53" bindgen = "0.54"
gcc = "0.3" gcc = "0.3"

View File

@ -1,10 +1,12 @@
[package] [package]
name = "speech-dispatcher" name = "speech-dispatcher"
version = "0.4.3" version = "0.5.0"
authors = ["Nolan Darilek <nolan@thewordnerd.info>"] authors = ["Nolan Darilek <nolan@thewordnerd.info>"]
repository = "https://gitlab.com/ndarilek/speech-dispatcher-rs" repository = "https://gitlab.com/ndarilek/speech-dispatcher-rs"
description = "Rusty interface to the speech-dispatcher speech synthesis library" description = "Rusty interface to the speech-dispatcher speech synthesis library"
license = "LGPL-2.1" license = "LGPL-2.1"
edition = "2018"
[dependencies] [dependencies]
speech-dispatcher-sys = { version = "0.4", path = "../speech-dispatcher-sys" } lazy_static = "1"
speech-dispatcher-sys = { version = "0.5", path = "../speech-dispatcher-sys" }

View File

@ -1,14 +1,17 @@
extern crate speech_dispatcher; extern crate speech_dispatcher;
use speech_dispatcher::*; use speech_dispatcher::*;
use std::io;
fn main() { fn main() {
let connection = speech_dispatcher::Connection::open( let connection = speech_dispatcher::Connection::open(
"hello_world", "hello_world",
"hello_world", "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( connection.say(
Priority::Important, Priority::Important,
format!("Hello, world at rate {}.", connection.get_voice_rate()), format!("Hello, world at rate {}.", connection.get_voice_rate()),
@ -25,4 +28,6 @@ fn main() {
"This statement, unlike others, has punctuation that is spoken!", "This statement, unlike others, has punctuation that is spoken!",
); );
connection.set_punctuation(Punctuation::None); connection.set_punctuation(Punctuation::None);
let mut _input = String::new();
io::stdin().read_line(&mut _input).unwrap();
} }

View File

@ -1,91 +1,181 @@
#![allow(non_upper_case_globals)] #![allow(non_upper_case_globals)]
extern crate speech_dispatcher_sys;
use std::collections::HashMap;
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::marker::Send; use std::marker::Send;
use std::sync::Mutex;
use lazy_static::lazy_static;
use speech_dispatcher_sys::*; use speech_dispatcher_sys::*;
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum Mode { pub enum Mode {
Single = SPDConnectionMode::SPD_MODE_SINGLE as isize, Single = SPDConnectionMode::SPD_MODE_SINGLE,
Threaded = SPDConnectionMode::SPD_MODE_THREADED as isize, Threaded = SPDConnectionMode::SPD_MODE_THREADED,
} }
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum Priority { pub enum Priority {
Important = SPDPriority::SPD_IMPORTANT as isize, Important = SPDPriority::SPD_IMPORTANT,
Message = SPDPriority::SPD_MESSAGE as isize, Message = SPDPriority::SPD_MESSAGE,
Text = SPDPriority::SPD_TEXT as isize, Text = SPDPriority::SPD_TEXT,
Notification = SPDPriority::SPD_NOTIFICATION as isize, Notification = SPDPriority::SPD_NOTIFICATION,
Progress = SPDPriority::SPD_PROGRESS as isize, Progress = SPDPriority::SPD_PROGRESS,
} }
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum VoiceType { pub enum VoiceType {
Male1 = SPDVoiceType::SPD_MALE1 as isize, Male1 = SPDVoiceType::SPD_MALE1,
Male2 = SPDVoiceType::SPD_MALE2 as isize, Male2 = SPDVoiceType::SPD_MALE2,
Male3 = SPDVoiceType::SPD_MALE3 as isize, Male3 = SPDVoiceType::SPD_MALE3,
Female1 = SPDVoiceType::SPD_FEMALE1 as isize, Female1 = SPDVoiceType::SPD_FEMALE1,
Female2 = SPDVoiceType::SPD_FEMALE2 as isize, Female2 = SPDVoiceType::SPD_FEMALE2,
Female3 = SPDVoiceType::SPD_FEMALE3 as isize, Female3 = SPDVoiceType::SPD_FEMALE3,
ChildMale = SPDVoiceType::SPD_CHILD_MALE as isize, ChildMale = SPDVoiceType::SPD_CHILD_MALE,
ChildFemale = SPDVoiceType::SPD_CHILD_FEMALE as isize, ChildFemale = SPDVoiceType::SPD_CHILD_FEMALE,
} }
pub struct Connection { #[derive(Clone, Debug)]
connection: *mut SPDConnection, pub struct Connection(pub *mut SPDConnection, u64);
}
pub type Address = SPDConnectionAddress; pub type Address = SPDConnectionAddress;
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum DataMode { pub enum DataMode {
Text = SPDDataMode::SPD_DATA_TEXT as isize, Text = SPDDataMode::SPD_DATA_TEXT,
SSML = SPDDataMode::SPD_DATA_SSML as isize, SSML = SPDDataMode::SPD_DATA_SSML,
} }
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum Notification { pub enum Notification {
Begin = SPDNotification::SPD_BEGIN as isize, Begin = SPDNotification::SPD_BEGIN,
End = SPDNotification::SPD_END as isize, End = SPDNotification::SPD_END,
IndexMarks = SPDNotification::SPD_INDEX_MARKS as isize, IndexMarks = SPDNotification::SPD_INDEX_MARKS,
Cancel = SPDNotification::SPD_CANCEL as isize, Cancel = SPDNotification::SPD_CANCEL,
Pause = SPDNotification::SPD_PAUSE as isize, Pause = SPDNotification::SPD_PAUSE,
Resume = SPDNotification::SPD_RESUME as isize, Resume = SPDNotification::SPD_RESUME,
All = SPDNotification::SPD_ALL as isize, All = SPDNotification::SPD_ALL,
} }
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum Punctuation { pub enum Punctuation {
All = SPDPunctuation::SPD_PUNCT_ALL as isize, All = SPDPunctuation::SPD_PUNCT_ALL,
None = SPDPunctuation::SPD_PUNCT_NONE as isize, None = SPDPunctuation::SPD_PUNCT_NONE,
Some = SPDPunctuation::SPD_PUNCT_SOME as isize, Some = SPDPunctuation::SPD_PUNCT_SOME,
} }
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum CapitalLetters { pub enum CapitalLetters {
None = SPDCapitalLetters::SPD_CAP_NONE as isize, None = SPDCapitalLetters::SPD_CAP_NONE,
Spell = SPDCapitalLetters::SPD_CAP_SPELL as isize, Spell = SPDCapitalLetters::SPD_CAP_SPELL,
Icon = SPDCapitalLetters::SPD_CAP_ICON as isize, Icon = SPDCapitalLetters::SPD_CAP_ICON,
} }
fn i32_to_bool(v: i32) -> bool { fn i32_to_bool(v: i32) -> bool {
v == 1 v == 1
} }
#[derive(Clone, Copy)]
struct Callbacks {
begin: Option<fn(u64)>,
end: Option<fn(u64)>,
index_mark: Option<fn(u64, String)>,
cancel: Option<fn(u64)>,
pause: Option<fn(u64)>,
resume: Option<fn(u64)>,
}
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<HashMap<u64, Callbacks>> = {
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 { impl Connection {
pub fn open<S: Into<String>>( pub fn open<S: Into<String>>(
client_name: S, client_name: S,
connection_name: S, connection_name: S,
user_name: S, user_name: S,
mode: Mode, mode: Mode,
) -> Connection { ) -> Self {
let clientname = CString::new(client_name.into()).unwrap(); let clientname = CString::new(client_name.into()).unwrap();
let connectionname = CString::new(connection_name.into()).unwrap(); let connectionname = CString::new(connection_name.into()).unwrap();
let username = CString::new(user_name.into()).unwrap(); let username = CString::new(user_name.into()).unwrap();
let connection = unsafe { let connection = unsafe {
spd_open( let c = spd_open(
clientname.as_ptr(), clientname.as_ptr(),
connectionname.as_ptr(), connectionname.as_ptr(),
username.as_ptr(), username.as_ptr(),
mode as u32, mode as u32,
) );
Self::setup_connection(c)
}; };
Connection { connection } let mut c = Self(connection, 0);
c.setup();
c
} }
pub unsafe fn open2<S: Into<String>>( pub unsafe fn open2<S: Into<String>>(
@ -95,148 +185,183 @@ impl Connection {
mode: Mode, mode: Mode,
address: *mut Address, address: *mut Address,
autospawn: bool, autospawn: bool,
) -> Connection { ) -> Self {
let auto_spawn = if autospawn { 1 } else { 0 }; let auto_spawn = if autospawn { 1 } else { 0 };
let error_result = vec![CString::new("").unwrap().into_raw()].as_mut_ptr(); let error_result = vec![CString::new("").unwrap().into_raw()].as_mut_ptr();
let clientname = CString::new(client_name.into()).unwrap(); let clientname = CString::new(client_name.into()).unwrap();
let connectionname = CString::new(connection_name.into()).unwrap(); let connectionname = CString::new(connection_name.into()).unwrap();
let username = CString::new(user_name.into()).unwrap(); let username = CString::new(user_name.into()).unwrap();
let connection = spd_open2( let connection = {
clientname.as_ptr(), let c = spd_open2(
connectionname.as_ptr(), clientname.as_ptr(),
username.as_ptr(), connectionname.as_ptr(),
mode as u32, username.as_ptr(),
address, mode as u32,
auto_spawn, address,
error_result, auto_spawn,
); error_result,
Connection { connection } );
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::<u64>().ok() {
self.1 = client_id;
}
}
}
}
callbacks.lock().unwrap().insert(self.1, Default::default());
self.set_notification_on(Notification::All);
} }
pub fn close(&self) { pub fn close(&self) {
unsafe { spd_close(self.connection) }; unsafe { spd_close(self.0) };
} }
pub fn say<S: Into<String>>(&self, priority: Priority, text: S) -> bool { pub fn say<S: Into<String>>(&self, priority: Priority, text: S) -> Option<i32> {
let text: String = text.into(); let text: String = text.into();
if text.is_empty() {
return true;
}
let param = CString::new(text).unwrap(); let param = CString::new(text).unwrap();
let v = unsafe { spd_say(self.connection, priority as u32, param.as_ptr()) }; let rv = unsafe { spd_say(self.0, priority as u32, param.as_ptr()) };
i32_to_bool(v) if rv != -1 {
Some(rv)
} else {
None
}
} }
pub fn sayf<S: Into<String>>(&self, priority: Priority, format: S) -> bool { pub fn sayf<S: Into<String>>(&self, priority: Priority, format: S) -> Option<i32> {
let format: String = format.into(); let format: String = format.into();
if format.is_empty() {
return true;
}
let param = CString::new(format).unwrap(); let param = CString::new(format).unwrap();
let v = unsafe { spd_sayf(self.connection, priority as u32, param.as_ptr()) }; let rv = unsafe { spd_sayf(self.0, priority as u32, param.as_ptr()) };
i32_to_bool(v) if rv != -1 {
Some(rv)
} else {
None
}
} }
pub fn stop(&self) -> bool { pub fn stop(&self) -> bool {
let v = unsafe { spd_stop(self.connection) }; let v = unsafe { spd_stop(self.0) };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn stop_all(&self) -> bool { 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) i32_to_bool(v)
} }
pub fn stop_uid(&self, target_uid: i32) -> bool { 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) i32_to_bool(v)
} }
pub fn cancel(&self) -> bool { pub fn cancel(&self) -> bool {
let v = unsafe { spd_cancel(self.connection) }; let v = unsafe { spd_cancel(self.0) };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn cancel_all(&self) -> bool { 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) i32_to_bool(v)
} }
pub fn cancel_uid(&self, target_uid: i32) -> bool { 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) i32_to_bool(v)
} }
pub fn pause(&self) -> bool { pub fn pause(&self) -> bool {
let v = unsafe { spd_pause(self.connection) }; let v = unsafe { spd_pause(self.0) };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn pause_all(&self) -> bool { 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) i32_to_bool(v)
} }
pub fn pause_uid(&self, target_uid: i32) -> bool { 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) i32_to_bool(v)
} }
pub fn resume(&self) -> bool { pub fn resume(&self) -> bool {
let v = unsafe { spd_resume(self.connection) }; let v = unsafe { spd_resume(self.0) };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn resume_all(&self) -> bool { 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) i32_to_bool(v)
} }
pub fn resume_uid(&self, target_uid: i32) -> bool { 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) i32_to_bool(v)
} }
pub fn key<S: Into<String>>(&self, priority: Priority, key_name: S) -> bool { pub fn key<S: Into<String>>(&self, priority: Priority, key_name: S) -> bool {
let param = CString::new(key_name.into()).unwrap(); 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) i32_to_bool(v)
} }
pub fn char<S: Into<String>>(&self, priority: Priority, char: S) -> bool { pub fn char<S: Into<String>>(&self, priority: Priority, char: S) -> bool {
let param = CString::new(char.into()).unwrap(); 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) i32_to_bool(v)
} }
pub fn wchar(&self, priority: Priority, wchar: i32) -> bool { 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) i32_to_bool(v)
} }
pub fn sound_icon<S: Into<String>>(&self, priority: Priority, icon_name: S) -> bool { pub fn sound_icon<S: Into<String>>(&self, priority: Priority, icon_name: S) -> bool {
let param = CString::new(icon_name.into()).unwrap(); 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) i32_to_bool(v)
} }
pub fn set_voice_type(&self, voice_type: VoiceType) -> bool { 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) i32_to_bool(v)
} }
pub fn set_voice_type_all(&self, voice_type: VoiceType) -> bool { 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) i32_to_bool(v)
} }
pub fn set_voice_type_uid(&self, voice_type: VoiceType, target_uid: u32) -> bool { 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) i32_to_bool(v)
} }
pub fn get_voice_type(&self) -> VoiceType { 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 { match v {
SPDVoiceType::SPD_MALE1 => VoiceType::Male1, SPDVoiceType::SPD_MALE1 => VoiceType::Male1,
SPDVoiceType::SPD_MALE2 => VoiceType::Male2, SPDVoiceType::SPD_MALE2 => VoiceType::Male2,
@ -252,123 +377,122 @@ impl Connection {
pub fn set_synthesis_voice<S: Into<String>>(&self, voice_name: S) -> bool { pub fn set_synthesis_voice<S: Into<String>>(&self, voice_name: S) -> bool {
let param = CString::new(voice_name.into()).unwrap(); 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) i32_to_bool(v)
} }
pub fn set_synthesis_voice_all<S: Into<String>>(&self, voice_name: S) -> bool { pub fn set_synthesis_voice_all<S: Into<String>>(&self, voice_name: S) -> bool {
let param = CString::new(voice_name.into()).unwrap(); 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) i32_to_bool(v)
} }
pub fn set_synthesis_voice_uid<S: Into<String>>(&self, voice_name: S, target_uid: u32) -> bool { pub fn set_synthesis_voice_uid<S: Into<String>>(&self, voice_name: S, target_uid: u32) -> bool {
let param = CString::new(voice_name.into()).unwrap(); 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) i32_to_bool(v)
} }
pub fn set_data_mode(&self, mode: DataMode) -> bool { 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) i32_to_bool(v)
} }
pub fn set_notification_on(&self, notification: Notification) -> bool { 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) i32_to_bool(v)
} }
pub fn set_notification_off(&self, notification: Notification) -> bool { 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) i32_to_bool(v)
} }
pub fn set_notification<S: Into<String>>(&self, notification: Notification, state: S) -> bool { pub fn set_notification<S: Into<String>>(&self, notification: Notification, state: S) -> bool {
let param = CString::new(state.into()).unwrap(); let param = CString::new(state.into()).unwrap();
let v = let v = unsafe { spd_set_notification(self.0, notification as u32, param.as_ptr()) };
unsafe { spd_set_notification(self.connection, notification as u32, param.as_ptr()) };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn set_voice_rate(&self, rate: i32) -> bool { 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) i32_to_bool(v)
} }
pub fn set_voice_rate_all(&self, rate: i32) -> bool { 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) i32_to_bool(v)
} }
pub fn set_voice_rate_uid(&self, rate: i32, target_uid: u32) -> bool { 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) i32_to_bool(v)
} }
pub fn get_voice_rate(&self) -> i32 { 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 { 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) i32_to_bool(v)
} }
pub fn set_voice_pitch_all(&self, pitch: i32) -> bool { 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) i32_to_bool(v)
} }
pub fn set_voice_pitch_uid(&self, pitch: i32, target_uid: u32) -> bool { 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) i32_to_bool(v)
} }
pub fn get_voice_pitch(&self) -> i32 { 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 { 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) i32_to_bool(v)
} }
pub fn set_volume_all(&self, volume: i32) -> bool { 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) i32_to_bool(v)
} }
pub fn set_volume_uid(&self, volume: i32, target_uid: u32) -> bool { 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) i32_to_bool(v)
} }
pub fn get_volume(&self) -> i32 { 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 { 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) i32_to_bool(v)
} }
pub fn set_punctuation_all(&self, punctuation: Punctuation) -> bool { 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) i32_to_bool(v)
} }
pub fn set_punctuation_uid(&self, punctuation: Punctuation, target_uid: u32) -> bool { 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) i32_to_bool(v)
} }
pub fn set_capital_letters(&self, capital_letters: CapitalLetters) -> bool { 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) i32_to_bool(v)
} }
pub fn set_capital_letters_all(&self, capital_letters: CapitalLetters) -> bool { 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) i32_to_bool(v)
} }
@ -377,9 +501,7 @@ impl Connection {
capital_letters: CapitalLetters, capital_letters: CapitalLetters,
target_uid: u32, target_uid: u32,
) -> bool { ) -> bool {
let v = unsafe { let v = unsafe { spd_set_capital_letters_uid(self.0, capital_letters as u32, target_uid) };
spd_set_capital_letters_uid(self.connection, capital_letters as u32, target_uid)
};
i32_to_bool(v) i32_to_bool(v)
} }
@ -389,7 +511,7 @@ impl Connection {
} else { } else {
SPDSpelling::SPD_SPELL_OFF 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) i32_to_bool(v)
} }
@ -399,7 +521,7 @@ impl Connection {
} else { } else {
SPDSpelling::SPD_SPELL_OFF 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) i32_to_bool(v)
} }
@ -409,42 +531,42 @@ impl Connection {
} else { } else {
SPDSpelling::SPD_SPELL_OFF 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) i32_to_bool(v)
} }
pub fn set_language<S: Into<String>>(&self, language: S) -> bool { pub fn set_language<S: Into<String>>(&self, language: S) -> bool {
let param = CString::new(language.into()).unwrap(); 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) i32_to_bool(v)
} }
pub fn set_language_all<S: Into<String>>(&self, language: S) -> bool { pub fn set_language_all<S: Into<String>>(&self, language: S) -> bool {
let param = CString::new(language.into()).unwrap(); 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) i32_to_bool(v)
} }
pub fn set_language_uid<S: Into<String>>(&self, language: S, target_uid: u32) -> bool { pub fn set_language_uid<S: Into<String>>(&self, language: S, target_uid: u32) -> bool {
let param = CString::new(language.into()).unwrap(); 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) i32_to_bool(v)
} }
pub fn get_language(&self) -> &str { 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() v.to_str().unwrap()
} }
pub fn set_output_module<S: Into<String>>(&self, output_module: S) -> bool { pub fn set_output_module<S: Into<String>>(&self, output_module: S) -> bool {
let param = CString::new(output_module.into()).unwrap(); 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) i32_to_bool(v)
} }
pub fn set_output_module_all<S: Into<String>>(&self, output_module: S) -> bool { pub fn set_output_module_all<S: Into<String>>(&self, output_module: S) -> bool {
let param = CString::new(output_module.into()).unwrap(); 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) i32_to_bool(v)
} }
@ -454,9 +576,79 @@ impl Connection {
target_uid: u32, target_uid: u32,
) -> bool { ) -> bool {
let param = CString::new(output_module.into()).unwrap(); 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) i32_to_bool(v)
} }
pub fn send_data<S: Into<String>>(&self, data: S, wait_for_reply: bool) -> Option<String> {
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<fn(u64)>) {
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<fn(u64)>) {
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<fn(u64)>) {
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<fn(u64)>) {
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<fn(u64)>) {
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<fn(u64, String)>) {
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 {} unsafe impl Send for Connection {}
@ -464,5 +656,6 @@ unsafe impl Send for Connection {}
impl Drop for Connection { impl Drop for Connection {
fn drop(&mut self) { fn drop(&mut self) {
self.close(); self.close();
callbacks.lock().unwrap().remove(&self.1);
} }
} }