From bf348af84f87151044f2a92a273f957fcbfc3760 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 19 Aug 2020 20:59:48 -0500 Subject: [PATCH] Make client ID easier to get at. * Pass `client_id` into all callbacks. * Expose via `client_id()` method. * Bump version. --- speech-dispatcher/Cargo.toml | 2 +- speech-dispatcher/examples/hello_world.rs | 14 +++++++--- speech-dispatcher/src/lib.rs | 32 +++++++++++++---------- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/speech-dispatcher/Cargo.toml b/speech-dispatcher/Cargo.toml index 40fcfc6..d5302a7 100644 --- a/speech-dispatcher/Cargo.toml +++ b/speech-dispatcher/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "speech-dispatcher" -version = "0.5.0" +version = "0.6.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 5c8176f..e72cdd0 100644 --- a/speech-dispatcher/examples/hello_world.rs +++ b/speech-dispatcher/examples/hello_world.rs @@ -10,11 +10,19 @@ fn main() { "hello_world", Mode::Threaded, ); - connection.on_begin(Some(|id| println!("Beginning {}", id))); - connection.on_end(Some(|id| println!("Ending {}", id))); + connection.on_begin(Some(|msg_id, client_id| { + println!("Beginning {} from {}", msg_id, client_id) + })); + connection.on_end(Some(|msg_id, client_id| { + println!("Ending {} from {}", msg_id, client_id) + })); connection.say( Priority::Important, - format!("Hello, world at rate {}.", connection.get_voice_rate()), + format!( + "Hello, world at rate {} from client {}.", + connection.get_voice_rate(), + connection.client_id() + ), ); connection.set_voice_rate(100); connection.say(Priority::Important, "This is faster."); diff --git a/speech-dispatcher/src/lib.rs b/speech-dispatcher/src/lib.rs index 63f8d47..2cf236d 100644 --- a/speech-dispatcher/src/lib.rs +++ b/speech-dispatcher/src/lib.rs @@ -84,12 +84,12 @@ fn i32_to_bool(v: i32) -> bool { #[derive(Clone, Copy)] 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 { @@ -131,7 +131,7 @@ unsafe extern "C" fn cb(msg_id: u64, client_id: u64, state: u32) { _ => panic!("Unknown notification type"), }; if let Some(f) = f { - f(msg_id); + f(msg_id, client_id); } } } @@ -149,7 +149,7 @@ unsafe extern "C" fn cb_im(msg_id: u64, client_id: u64, state: u32, index_mark: _ => panic!("Unknown notification type"), }; if let Some(f) = f { - f(msg_id, index_mark); + f(msg_id, client_id, index_mark); } } } @@ -596,7 +596,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 +605,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 +614,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 +623,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 +632,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 +641,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 { @@ -649,6 +649,10 @@ impl Connection { } } } + + pub fn client_id(&self) -> u64 { + self.1 + } } unsafe impl Send for Connection {}