1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
#![allow(non_upper_case_globals)]

use libc::size_t;
use std::{
    collections::HashMap,
    ffi::{CStr, CString},
    fmt,
    marker::Send,
    os::raw::{c_char, c_int},
    sync::{Arc, Mutex},
};

use lazy_static::lazy_static;
use speech_dispatcher_sys::*;

#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum Mode {
    Single = SPDConnectionMode::SPD_MODE_SINGLE,
    Threaded = SPDConnectionMode::SPD_MODE_THREADED,
}

#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum Priority {
    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 u32,
    Male2 = SPDVoiceType::SPD_MALE2 as u32,
    Male3 = SPDVoiceType::SPD_MALE3 as u32,
    Female1 = SPDVoiceType::SPD_FEMALE1 as u32,
    Female2 = SPDVoiceType::SPD_FEMALE2 as u32,
    Female3 = SPDVoiceType::SPD_FEMALE3 as u32,
    ChildMale = SPDVoiceType::SPD_CHILD_MALE as u32,
    ChildFemale = SPDVoiceType::SPD_CHILD_FEMALE as u32,
}

#[derive(Clone, Debug, Hash, PartialEq)]
pub struct Voice {
    /// The name of this voice. Unique with regards to the output module it came from.
    pub name: String,
    /// The language of this voice. Probably a BCP 47 language tag.
    pub language: String,
    /// The variant of this language, if present. Loosely defined.
    pub variant: Option<String>,
}

impl Voice {
    /// Convert a SPDVoice to a Voice. Only fails if the fields are non-Unicode.
    /// Does not check that the pointers are non-null, caller must ensure that.
    unsafe fn try_from(v: &SPDVoice) -> Result<Self, std::str::Utf8Error> {
        // SPDVoice fields appear to all be ASCII.
        let name = CStr::from_ptr(v.name).to_str()?.to_owned();
        let language = CStr::from_ptr(v.language).to_str()?.to_owned();
        let variant = CStr::from_ptr(v.variant).to_str()?;
        let variant = if variant == "none" {
            None
        } else {
            Some(variant.to_owned())
        };
        Ok(Self {
            name,
            language,
            variant,
        })
    }
}

pub type Address = SPDConnectionAddress;

#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum DataMode {
    Text = SPDDataMode::SPD_DATA_TEXT,
    SSML = SPDDataMode::SPD_DATA_SSML,
}

#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum Notification {
    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,
    #[cfg(feature = "0_10")]
    Most = SPDPunctuation::SPD_PUNCT_MOST,
    Some = SPDPunctuation::SPD_PUNCT_SOME,
    None = SPDPunctuation::SPD_PUNCT_NONE,
}

#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum CapitalLetters {
    None = SPDCapitalLetters::SPD_CAP_NONE,
    Spell = SPDCapitalLetters::SPD_CAP_SPELL,
    Icon = SPDCapitalLetters::SPD_CAP_ICON,
}

/// Converts a `0` to a success and everything else to an error.
fn c_int_to_result(r: c_int) -> Result<(), Error> {
    match r {
        0 => Ok(()),
        _ => Err(Error::OperationFailed),
    }
}

#[derive(Default)]
struct Callbacks {
    begin: Option<Box<dyn FnMut(size_t, size_t)>>,
    end: Option<Box<dyn FnMut(size_t, size_t)>>,
    index_mark: Option<Box<dyn FnMut(size_t, size_t, String)>>,
    cancel: Option<Box<dyn FnMut(size_t, size_t)>>,
    pause: Option<Box<dyn FnMut(size_t, size_t)>>,
    resume: Option<Box<dyn FnMut(size_t, size_t)>>,
}

unsafe impl Send for Callbacks {}

unsafe impl Sync for Callbacks {}

lazy_static! {
    static ref callbacks: Mutex<HashMap<size_t, Callbacks>> = {
        let m = HashMap::new();
        Mutex::new(m)
    };
}

unsafe extern "C" fn cb(msg_id: size_t, client_id: size_t, 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_mut(&client_id) {
        let f = match state {
            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.as_mut() {
            f(msg_id, client_id);
        }
    }
}

unsafe extern "C" fn cb_im(msg_id: size_t, client_id: size_t, state: u32, index_mark: *mut c_char) {
    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_mut(&client_id) {
        let f = match state {
            Notification::IndexMarks => &mut c.index_mark,
            _ => panic!("Unknown notification type"),
        };
        if let Some(f) = f.as_mut() {
            f(msg_id, client_id, index_mark);
        }
    }
}

#[derive(Debug)]
pub enum Error {
    /// speech-dispatcher failed to initialize. Ensure speech-dispatcher is actually working on
    /// your system; for example, does the command `spd-say hello` work?
    InitializationError,
    /// The operation failed
    OperationFailed,
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use Error::*;
        match self {
            InitializationError => write!(f, "failed to initialize"),
            OperationFailed => write!(f, "operation failed"),
        }
    }
}

#[derive(Clone, Debug)]
pub struct Connection(pub Arc<*mut SPDConnection>, size_t);

impl Connection {
    pub fn open<S: Into<String>>(
        client_name: S,
        connection_name: S,
        user_name: S,
        mode: Mode,
    ) -> Result<Self, Error> {
        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 {
            let c = spd_open(
                clientname.as_ptr(),
                connectionname.as_ptr(),
                username.as_ptr(),
                mode as u32,
            );
            if c.is_null() {
                Err(Error::InitializationError)
            } else {
                Ok(Self::setup_connection(c))
            }
        };
        let mut c = Self(Arc::new(connection?), 0);
        c.setup()?;
        Ok(c)
    }

    pub unsafe fn open2<S: Into<String>>(
        client_name: S,
        connection_name: S,
        user_name: S,
        mode: Mode,
        address: *mut Address,
        autospawn: bool,
    ) -> Result<Self, Error> {
        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 = {
            let c = spd_open2(
                clientname.as_ptr(),
                connectionname.as_ptr(),
                username.as_ptr(),
                mode as u32,
                address,
                auto_spawn,
                error_result,
            );
            if c.is_null() {
                Err(Error::InitializationError)
            } else {
                Ok(Self::setup_connection(c))
            }
        };
        let mut c = Self(Arc::new(connection?), 0);
        c.setup()?;
        Ok(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) -> Result<(), Error> {
        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 Ok(client_id) = client_id.parse::<size_t>() {
                        self.1 = client_id;
                    }
                }
            }
        }
        callbacks.lock().unwrap().insert(self.1, Default::default());
        self.set_notification_on(Notification::All)
            .map_err(|_| Error::InitializationError)?;
        Ok(())
    }

    pub fn close(&self) {
        unsafe { spd_close(*self.0) };
    }

    pub fn say<S: Into<String>>(&self, priority: Priority, text: S) -> Option<u64> {
        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 as u64)
        } else {
            None
        }
    }

    pub fn sayf<S: Into<String>>(&self, priority: Priority, format: S) -> Option<i32> {
        let format: String = format.into();
        let param = CString::new(format).unwrap();
        let rv = unsafe { spd_sayf(*self.0, priority as u32, param.as_ptr()) };
        if rv != -1 {
            Some(rv)
        } else {
            None
        }
    }

    pub fn stop(&self) -> Result<(), Error> {
        let v = unsafe { spd_stop(*self.0) };
        c_int_to_result(v)
    }

    pub fn stop_all(&self) -> Result<(), Error> {
        let v = unsafe { spd_stop_all(*self.0) };
        c_int_to_result(v)
    }

    pub fn stop_uid(&self, target_uid: i32) -> Result<(), Error> {
        let v = unsafe { spd_stop_uid(*self.0, target_uid) };
        c_int_to_result(v)
    }

    pub fn cancel(&self) -> Result<(), Error> {
        let v = unsafe { spd_cancel(*self.0) };
        c_int_to_result(v)
    }

    pub fn cancel_all(&self) -> Result<(), Error> {
        let v = unsafe { spd_cancel_all(*self.0) };
        c_int_to_result(v)
    }

    pub fn cancel_uid(&self, target_uid: i32) -> Result<(), Error> {
        let v = unsafe { spd_cancel_uid(*self.0, target_uid) };
        c_int_to_result(v)
    }

    pub fn pause(&self) -> Result<(), Error> {
        let v = unsafe { spd_pause(*self.0) };
        c_int_to_result(v)
    }

    pub fn pause_all(&self) -> Result<(), Error> {
        let v = unsafe { spd_pause_all(*self.0) };
        c_int_to_result(v)
    }

    pub fn pause_uid(&self, target_uid: i32) -> Result<(), Error> {
        let v = unsafe { spd_pause_uid(*self.0, target_uid) };
        c_int_to_result(v)
    }

    pub fn resume(&self) -> Result<(), Error> {
        let v = unsafe { spd_resume(*self.0) };
        c_int_to_result(v)
    }

    pub fn resume_all(&self) -> Result<(), Error> {
        let v = unsafe { spd_resume_all(*self.0) };
        c_int_to_result(v)
    }

    pub fn resume_uid(&self, target_uid: i32) -> Result<(), Error> {
        let v = unsafe { spd_resume_uid(*self.0, target_uid) };
        c_int_to_result(v)
    }

    pub fn key<S: Into<String>>(&self, priority: Priority, key_name: S) -> Result<(), Error> {
        let param = CString::new(key_name.into()).unwrap();
        let v = unsafe { spd_key(*self.0, priority as u32, param.as_ptr()) };
        c_int_to_result(v)
    }

    pub fn char<S: Into<String>>(&self, priority: Priority, char: S) -> Result<(), Error> {
        let param = CString::new(char.into()).unwrap();
        let v = unsafe { spd_char(*self.0, priority as u32, param.as_ptr()) };
        c_int_to_result(v)
    }

    pub fn wchar(&self, priority: Priority, wchar: i32) -> Result<(), Error> {
        let v = unsafe { spd_wchar(*self.0, priority as u32, wchar as wchar_t) };
        c_int_to_result(v)
    }

    pub fn sound_icon<S: Into<String>>(
        &self,
        priority: Priority,
        icon_name: S,
    ) -> Result<(), Error> {
        let param = CString::new(icon_name.into()).unwrap();
        let v = unsafe { spd_char(*self.0, priority as u32, param.as_ptr()) };
        c_int_to_result(v)
    }

    pub fn set_voice_type(&self, voice_type: VoiceType) -> Result<(), Error> {
        #[cfg(all(any(feature = "0_9", feature = "0_10"), not(feature = "0_11")))]
        let v = unsafe { spd_set_voice_type(*self.0, voice_type as u32) };
        #[cfg(all(not(feature = "0_9"), any(feature = "0_11", not(feature = "0_10"))))]
        let v = unsafe { spd_set_voice_type(*self.0, voice_type as i32) };
        c_int_to_result(v)
    }

    pub fn set_voice_type_all(&self, voice_type: VoiceType) -> Result<(), Error> {
        #[cfg(all(any(feature = "0_9", feature = "0_10"), not(feature = "0_11")))]
        let v = unsafe { spd_set_voice_type_all(*self.0, voice_type as u32) };
        #[cfg(all(not(feature = "0_9"), any(feature = "0_11", not(feature = "0_10"))))]
        let v = unsafe { spd_set_voice_type_all(*self.0, voice_type as i32) };
        c_int_to_result(v)
    }

    pub fn set_voice_type_uid(&self, voice_type: VoiceType, target_uid: u32) -> Result<(), Error> {
        #[cfg(all(any(feature = "0_9", feature = "0_10"), not(feature = "0_11")))]
        let v = unsafe { spd_set_voice_type_uid(*self.0, voice_type as u32, target_uid) };
        #[cfg(all(not(feature = "0_9"), any(feature = "0_11", not(feature = "0_10"))))]
        let v = unsafe { spd_set_voice_type_uid(*self.0, voice_type as i32, target_uid) };
        c_int_to_result(v)
    }

    pub fn get_voice_type(&self) -> Result<VoiceType, Error> {
        let v = unsafe { spd_get_voice_type(*self.0) };
        Ok(match v {
            SPDVoiceType::SPD_MALE1 => VoiceType::Male1,
            SPDVoiceType::SPD_MALE2 => VoiceType::Male2,
            SPDVoiceType::SPD_MALE3 => VoiceType::Male3,
            SPDVoiceType::SPD_FEMALE1 => VoiceType::Female1,
            SPDVoiceType::SPD_FEMALE2 => VoiceType::Female2,
            SPDVoiceType::SPD_FEMALE3 => VoiceType::Female3,
            SPDVoiceType::SPD_CHILD_MALE => VoiceType::ChildMale,
            SPDVoiceType::SPD_CHILD_FEMALE => VoiceType::ChildFemale,
            _ => return Err(Error::OperationFailed), // can this happen?
        })
    }

    pub fn set_synthesis_voice(&self, voice: &Voice) -> Result<(), Error> {
        let param = CString::new(voice.name.clone()).unwrap();
        let v = unsafe { spd_set_synthesis_voice(*self.0, param.as_ptr()) };
        c_int_to_result(v)
    }

    pub fn set_synthesis_voice_all<S: Into<String>>(&self, voice_name: S) -> Result<(), Error> {
        let param = CString::new(voice_name.into()).unwrap();
        let v = unsafe { spd_set_synthesis_voice_all(*self.0, param.as_ptr()) };
        c_int_to_result(v)
    }

    pub fn set_synthesis_voice_uid<S: Into<String>>(
        &self,
        voice_name: S,
        target_uid: u32,
    ) -> Result<(), Error> {
        let param = CString::new(voice_name.into()).unwrap();
        let v = unsafe { spd_set_synthesis_voice_uid(*self.0, param.as_ptr(), target_uid) };
        c_int_to_result(v)
    }

    pub fn set_data_mode(&self, mode: DataMode) -> Result<(), Error> {
        let v = unsafe { spd_set_data_mode(*self.0, mode as u32) };
        c_int_to_result(v)
    }

    pub fn set_notification_on(&self, notification: Notification) -> Result<(), Error> {
        let v = unsafe { spd_set_notification_on(*self.0, notification as u32) };
        c_int_to_result(v)
    }

    pub fn set_notification_off(&self, notification: Notification) -> Result<(), Error> {
        let v = unsafe { spd_set_notification_off(*self.0, notification as u32) };
        c_int_to_result(v)
    }

    pub fn set_notification<S: Into<String>>(
        &self,
        notification: Notification,
        state: S,
    ) -> Result<(), Error> {
        let param = CString::new(state.into()).unwrap();
        let v = unsafe { spd_set_notification(*self.0, notification as u32, param.as_ptr()) };
        c_int_to_result(v)
    }

    pub fn set_voice_rate(&self, rate: i32) -> Result<(), Error> {
        let v = unsafe { spd_set_voice_rate(*self.0, rate) };
        c_int_to_result(v)
    }

    pub fn set_voice_rate_all(&self, rate: i32) -> Result<(), Error> {
        let v = unsafe { spd_set_voice_rate_all(*self.0, rate) };
        c_int_to_result(v)
    }

    pub fn set_voice_rate_uid(&self, rate: i32, target_uid: u32) -> Result<(), Error> {
        let v = unsafe { spd_set_voice_rate_uid(*self.0, rate, target_uid) };
        c_int_to_result(v)
    }

    pub fn get_voice_rate(&self) -> i32 {
        unsafe { spd_get_voice_rate(*self.0) }
    }

    pub fn set_voice_pitch(&self, pitch: i32) -> Result<(), Error> {
        let v = unsafe { spd_set_voice_pitch(*self.0, pitch) };
        c_int_to_result(v)
    }

    pub fn set_voice_pitch_all(&self, pitch: i32) -> Result<(), Error> {
        let v = unsafe { spd_set_voice_pitch_all(*self.0, pitch) };
        c_int_to_result(v)
    }

    pub fn set_voice_pitch_uid(&self, pitch: i32, target_uid: u32) -> Result<(), Error> {
        let v = unsafe { spd_set_voice_pitch_uid(*self.0, pitch, target_uid) };
        c_int_to_result(v)
    }

    pub fn get_voice_pitch(&self) -> i32 {
        unsafe { spd_get_voice_pitch(*self.0) }
    }

    pub fn set_volume(&self, volume: i32) -> Result<(), Error> {
        let v = unsafe { spd_set_volume(*self.0, volume) };
        c_int_to_result(v)
    }

    pub fn set_volume_all(&self, volume: i32) -> Result<(), Error> {
        let v = unsafe { spd_set_volume_all(*self.0, volume) };
        c_int_to_result(v)
    }

    pub fn set_volume_uid(&self, volume: i32, target_uid: u32) -> Result<(), Error> {
        let v = unsafe { spd_set_volume_uid(*self.0, volume, target_uid) };
        c_int_to_result(v)
    }

    pub fn get_volume(&self) -> i32 {
        unsafe { spd_get_volume(*self.0) }
    }

    pub fn set_punctuation(&self, punctuation: Punctuation) -> Result<(), Error> {
        let v = unsafe { spd_set_punctuation(*self.0, punctuation as u32) };
        c_int_to_result(v)
    }

    pub fn set_punctuation_all(&self, punctuation: Punctuation) -> Result<(), Error> {
        let v = unsafe { spd_set_punctuation_all(*self.0, punctuation as u32) };
        c_int_to_result(v)
    }

    pub fn set_punctuation_uid(
        &self,
        punctuation: Punctuation,
        target_uid: u32,
    ) -> Result<(), Error> {
        let v = unsafe { spd_set_punctuation_uid(*self.0, punctuation as u32, target_uid) };
        c_int_to_result(v)
    }

    pub fn set_capital_letters(&self, capital_letters: CapitalLetters) -> Result<(), Error> {
        let v = unsafe { spd_set_capital_letters(*self.0, capital_letters as u32) };
        c_int_to_result(v)
    }

    pub fn set_capital_letters_all(&self, capital_letters: CapitalLetters) -> Result<(), Error> {
        let v = unsafe { spd_set_capital_letters_all(*self.0, capital_letters as u32) };
        c_int_to_result(v)
    }

    pub fn set_capital_letters_uid(
        &self,
        capital_letters: CapitalLetters,
        target_uid: u32,
    ) -> Result<(), Error> {
        let v = unsafe { spd_set_capital_letters_uid(*self.0, capital_letters as u32, target_uid) };
        c_int_to_result(v)
    }

    pub fn set_spelling(&self, spelling: bool) -> Result<(), Error> {
        let s = if spelling {
            SPDSpelling::SPD_SPELL_ON
        } else {
            SPDSpelling::SPD_SPELL_OFF
        };
        let v = unsafe { spd_set_spelling(*self.0, s) };
        c_int_to_result(v)
    }

    pub fn set_spelling_all(&self, spelling: bool) -> Result<(), Error> {
        let s = if spelling {
            SPDSpelling::SPD_SPELL_ON
        } else {
            SPDSpelling::SPD_SPELL_OFF
        };
        let v = unsafe { spd_set_spelling_all(*self.0, s) };
        c_int_to_result(v)
    }

    pub fn set_spelling_uid(&self, spelling: bool, target_uid: u32) -> Result<(), Error> {
        let s = if spelling {
            SPDSpelling::SPD_SPELL_ON
        } else {
            SPDSpelling::SPD_SPELL_OFF
        };
        let v = unsafe { spd_set_spelling_uid(*self.0, s, target_uid) };
        c_int_to_result(v)
    }

    pub fn set_language<S: Into<String>>(&self, language: S) -> Result<(), Error> {
        let param = CString::new(language.into()).unwrap();
        let v = unsafe { spd_set_language(*self.0, param.as_ptr()) };
        c_int_to_result(v)
    }

    pub fn set_language_all<S: Into<String>>(&self, language: S) -> Result<(), Error> {
        let param = CString::new(language.into()).unwrap();
        let v = unsafe { spd_set_language_all(*self.0, param.as_ptr()) };
        c_int_to_result(v)
    }

    pub fn set_language_uid<S: Into<String>>(
        &self,
        language: S,
        target_uid: u32,
    ) -> Result<(), Error> {
        let param = CString::new(language.into()).unwrap();
        let v = unsafe { spd_set_language_uid(*self.0, param.as_ptr(), target_uid) };
        c_int_to_result(v)
    }

    pub fn get_language(&self) -> Result<&str, Error> {
        let language = unsafe { spd_get_language(*self.0) };
        if language.is_null() {
            Err(Error::OperationFailed)
        } else {
            let language = unsafe { CStr::from_ptr(language) };
            language.to_str().map_err(|_| Error::OperationFailed)
        }
    }

    pub fn set_output_module<S: Into<String>>(&self, output_module: S) -> Result<(), Error> {
        let param = CString::new(output_module.into()).unwrap();
        let v = unsafe { spd_set_output_module(*self.0, param.as_ptr()) };
        c_int_to_result(v)
    }

    pub fn set_output_module_all<S: Into<String>>(&self, output_module: S) -> Result<(), Error> {
        let param = CString::new(output_module.into()).unwrap();
        let v = unsafe { spd_set_output_module_all(*self.0, param.as_ptr()) };
        c_int_to_result(v)
    }

    pub fn set_output_module_uid<S: Into<String>>(
        &self,
        output_module: S,
        target_uid: u32,
    ) -> Result<(), Error> {
        let param = CString::new(output_module.into()).unwrap();
        let v = unsafe { spd_set_output_module_uid(*self.0, param.as_ptr(), target_uid) };
        c_int_to_result(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<Box<dyn FnMut(size_t, size_t)>>) {
        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<Box<dyn FnMut(size_t, size_t)>>) {
        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<Box<dyn FnMut(size_t, size_t)>>) {
        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<Box<dyn FnMut(size_t, size_t)>>) {
        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<Box<dyn FnMut(size_t, size_t)>>) {
        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<Box<dyn FnMut(size_t, size_t, String)>>) {
        if let Ok(mut cbs) = callbacks.lock() {
            let cb = cbs.get_mut(&self.1);
            if let Some(cb) = cb {
                cb.index_mark = f;
            }
        }
    }

    pub fn list_synthesis_voices(&self) -> Result<Vec<Voice>, Error> {
        let start = unsafe { spd_list_synthesis_voices(*self.0) };
        let slice = unsafe { null_term_array_ptr_to_slice(start) }.ok_or(Error::OperationFailed)?;
        let voices = unsafe {
            slice
                .iter()
                .map(|v| v.read())
                .flat_map(|v| {
                    if v.name.is_null() || v.language.is_null() || v.variant.is_null() {
                        None
                    } else {
                        Voice::try_from(&v).ok()
                    }
                })
                .collect()
        };
        unsafe {
            free_spd_voices(start);
        }
        Ok(voices)
    }

    pub fn list_output_modules(&self) -> Result<Vec<String>, Error> {
        let start = unsafe { spd_list_modules(*self.0) };
        let slice = unsafe { null_term_array_ptr_to_slice(start) }.ok_or(Error::OperationFailed)?;
        let modules = unsafe {
            slice
                .iter()
                .flat_map(|v| CStr::from_ptr(*v).to_str().ok().map(String::from))
                .collect()
        };
        unsafe {
            free_spd_modules(start);
        }
        Ok(modules)
    }

    pub fn client_id(&self) -> size_t {
        self.1
    }
}

/// Interpret a null-terminated array of pointers as a slice of pointers.
/// None of the pointers will be null if this returns Some.
unsafe fn null_term_array_ptr_to_slice<'a, T>(start: *mut *mut T) -> Option<&'a [*mut T]> {
    if start.is_null() {
        return None;
    }
    let mut current = start;
    let mut len = 0;

    while !current.read().is_null() {
        len += 1;
        current = current.add(1);
    }

    Some(std::slice::from_raw_parts(start, len))
}

unsafe impl Send for Connection {}

impl Drop for Connection {
    fn drop(&mut self) {
        if Arc::strong_count(&self.0) <= 1 {
            self.close();
            callbacks.lock().unwrap().remove(&self.1);
        }
    }
}