diff --git a/speech-dispatcher/Cargo.toml b/speech-dispatcher/Cargo.toml index d5302a7..5d6e005 100644 --- a/speech-dispatcher/Cargo.toml +++ b/speech-dispatcher/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "speech-dispatcher" -version = "0.6.0" +version = "0.7.0" authors = ["Nolan Darilek "] repository = "https://gitlab.com/ndarilek/speech-dispatcher-rs" description = "Rusty interface to the speech-dispatcher speech synthesis library" diff --git a/speech-dispatcher/examples/hello_world.rs b/speech-dispatcher/examples/hello_world.rs index e72cdd0..8882cee 100644 --- a/speech-dispatcher/examples/hello_world.rs +++ b/speech-dispatcher/examples/hello_world.rs @@ -1,5 +1,3 @@ -extern crate speech_dispatcher; - use speech_dispatcher::*; use std::io; @@ -10,12 +8,12 @@ fn main() { "hello_world", Mode::Threaded, ); - connection.on_begin(Some(|msg_id, client_id| { + connection.on_begin(Some(Box::new(|msg_id, client_id| { println!("Beginning {} from {}", msg_id, client_id) - })); - connection.on_end(Some(|msg_id, client_id| { + }))); + connection.on_end(Some(Box::new(|msg_id, client_id| { println!("Ending {} from {}", msg_id, client_id) - })); + }))); connection.say( Priority::Important, format!( diff --git a/speech-dispatcher/src/lib.rs b/speech-dispatcher/src/lib.rs index 2cf236d..26ba271 100644 --- a/speech-dispatcher/src/lib.rs +++ b/speech-dispatcher/src/lib.rs @@ -82,28 +82,19 @@ fn i32_to_bool(v: i32) -> bool { v == 1 } -#[derive(Clone, Copy)] +#[derive(Default)] struct Callbacks { - begin: Option, - end: Option, - index_mark: Option, - cancel: Option, - pause: Option, - resume: Option, + 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, - } - } -} +unsafe impl Send for Callbacks {} + +unsafe impl Sync for Callbacks {} lazy_static! { static ref callbacks: Mutex> = { @@ -121,16 +112,16 @@ unsafe extern "C" fn cb(msg_id: u64, client_id: u64, state: u32) { SPDNotificationType_SPD_EVENT_RESUME => Notification::Resume, _ => panic!("Unknown notification received in callback: {}", state), }; - if let Some(c) = callbacks.lock().unwrap().get(&client_id) { + if let Some(c) = callbacks.lock().unwrap().get_mut(&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, + Notification::Begin => &mut c.begin, + Notification::End => &mut c.end, + Notification::Cancel => &mut c.cancel, + Notification::Pause => &mut c.pause, + Notification::Resume => &mut c.resume, _ => panic!("Unknown notification type"), }; - if let Some(f) = f { + if let Some(f) = f.as_mut() { f(msg_id, client_id); } } @@ -143,12 +134,12 @@ unsafe extern "C" fn cb_im(msg_id: u64, client_id: u64, state: u32, index_mark: 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) { + if let Some(c) = callbacks.lock().unwrap().get_mut(&client_id) { let f = match state { - Notification::IndexMarks => c.index_mark, + Notification::IndexMarks => &mut c.index_mark, _ => panic!("Unknown notification type"), }; - if let Some(f) = f { + if let Some(f) = f.as_mut() { f(msg_id, client_id, index_mark); } } @@ -240,12 +231,12 @@ impl Connection { unsafe { spd_close(self.0) }; } - pub fn say>(&self, priority: Priority, text: S) -> Option { + pub fn say>(&self, priority: Priority, text: S) -> Option { let text: String = text.into(); let param = CString::new(text).unwrap(); let rv = unsafe { spd_say(self.0, priority as u32, param.as_ptr()) }; if rv != -1 { - Some(rv) + Some(rv as u64) } else { None } @@ -596,7 +587,7 @@ impl Connection { } } - pub fn on_begin(&self, f: Option) { + 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 { @@ -605,7 +596,7 @@ impl Connection { } } - pub fn on_end(&self, f: Option) { + 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 { @@ -614,7 +605,7 @@ impl Connection { } } - pub fn on_cancel(&self, f: Option) { + 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 { @@ -623,7 +614,7 @@ impl Connection { } } - pub fn on_pause(&self, f: Option) { + 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 { @@ -632,7 +623,7 @@ impl Connection { } } - pub fn on_resume(&self, f: Option) { + 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 { @@ -641,7 +632,7 @@ impl Connection { } } - pub fn on_index_mark(&self, f: Option) { + 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 {