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
/*
 * SPDX-License-Identifier: MIT
 */

//! PipeWire [Data Loop](https://docs.pipewire.org/group__pw__data__loop.html) bindings.
//!
use std::ptr::NonNull;
use std::rc::Rc;
use std::time::Duration;

use pipewire_wrapper_proc_macro::{RawWrapper, Wrapper};

use crate::core_api::loop_::LoopRef;
use crate::core_api::properties::Properties;
use crate::core_api::PipeWire;
use crate::spa::dict::DictRef;
use crate::spa::thread::ThreadRef;
use crate::wrapper::RawWrapper;
use crate::{i32_as_result, new_instance_raw_wrapper};

#[derive(RawWrapper, Debug)]
#[repr(transparent)]
pub struct DataLoopRef {
    #[raw]
    raw: pw_sys::pw_data_loop,
}

#[derive(Wrapper, Debug)]
pub struct DataLoop {
    #[raw_wrapper]
    ref_: NonNull<DataLoopRef>,

    pipewire: std::sync::Arc<PipeWire>,
}

impl Drop for DataLoop {
    fn drop(&mut self) {
        unsafe { pw_sys::pw_data_loop_destroy(self.as_raw_ptr()) }
    }
}

impl Default for DataLoop {
    fn default() -> Self {
        Self::new(
            &std::sync::Arc::new(PipeWire::default()),
            Properties::default().dict(),
        )
        .unwrap()
    }
}

impl DataLoop {
    pub fn new(pipewire: &std::sync::Arc<PipeWire>, properties: &DictRef) -> crate::Result<Self> {
        let ptr = unsafe { pw_sys::pw_data_loop_new(properties.as_raw_ptr()) };
        Ok(Self {
            ref_: new_instance_raw_wrapper(ptr)?,
            pipewire: pipewire.clone(),
        })
    }

    // since listener has only destroy handler, there is no big reason to implement add_listener
}

impl DataLoopRef {
    pub fn wait(&self, timeout: i32) -> crate::Result<i32> {
        let result = unsafe { pw_sys::pw_data_loop_wait(self.as_raw_ptr(), timeout) };
        i32_as_result(result, result)
    }

    pub fn exit(&self) {
        unsafe { pw_sys::pw_data_loop_exit(self.as_raw_ptr()) };
    }

    pub fn get_loop(&self) -> &LoopRef {
        unsafe { LoopRef::from_raw_ptr(pw_sys::pw_data_loop_get_loop(self.as_raw_ptr())) }
    }

    pub fn start(&self) -> crate::Result<()> {
        let result = unsafe { pw_sys::pw_data_loop_start(self.as_raw_ptr()) };
        i32_as_result(result, ())
    }

    pub fn stop(&self) -> crate::Result<()> {
        let result = unsafe { pw_sys::pw_data_loop_stop(self.as_raw_ptr()) };
        i32_as_result(result, ())
    }

    pub fn in_thread(&self) -> bool {
        unsafe { pw_sys::pw_data_loop_in_thread(self.as_raw_ptr()) }
    }

    pub fn get_thread(&self) -> &ThreadRef {
        unsafe { ThreadRef::from_raw_ptr(pw_sys::pw_data_loop_get_thread(self.as_raw_ptr())) }
    }

    //todo pw_data_loop_invoke
    //todo pw_data_loop_set_thread_utils
}