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
/*
 * SPDX-License-Identifier: MIT
 */
use std::fmt::{Debug, Formatter};
use std::os::fd::RawFd;
use std::ptr::NonNull;
use std::rc::Rc;
use std::time::Duration;

use spa_sys::{spa_interface, spa_source, spa_source_io_func_t};

use pipewire_wrapper_proc_macro::{spa_interface, RawWrapper, Wrapper};

use crate::core_api::main_loop::MainLoop;
use crate::error::Error;
use crate::spa::interface::InterfaceRef;
use crate::spa_interface_call;
use crate::wrapper::{RawWrapper, SpaInterface, Wrapper};
use crate::{i32_as_result, i32_as_void_result};

pub mod utils;

#[derive(RawWrapper, Debug)]
#[spa_interface(methods=spa_sys::spa_loop_methods)]
#[repr(transparent)]
pub struct LoopRef {
    #[raw]
    raw: spa_sys::spa_loop,
}

#[derive(RawWrapper, Debug)]
#[spa_interface(methods=spa_sys::spa_loop_control_methods)]
#[repr(transparent)]
pub struct LoopControlRef {
    #[raw]
    raw: spa_sys::spa_loop_control,
}

#[derive(RawWrapper, Debug)]
#[repr(transparent)]
pub struct SourceRef {
    #[raw]
    raw: spa_sys::spa_source,
}

#[derive(Wrapper)]
pub struct IOSource<'l> {
    #[raw_wrapper]
    ref_: NonNull<SourceRef>,
    loop_: &'l dyn AsLoopRef,
    callback: Box<dyn FnMut(RawFd, u32) + 'l>,
}

#[derive(Wrapper)]
pub struct IdleSource<'l> {
    #[raw_wrapper]
    ref_: NonNull<SourceRef>,
    loop_: &'l dyn AsLoopRef,
    callback: Box<dyn FnMut() + 'l>,
}

#[derive(Wrapper)]
pub struct EventSource<'l> {
    #[raw_wrapper]
    ref_: NonNull<SourceRef>,
    loop_: &'l dyn AsLoopRef,
    callback: Box<dyn FnMut(u64) + 'l>,
}

#[derive(Wrapper)]
pub struct TimerSource<'l> {
    #[raw_wrapper]
    ref_: NonNull<SourceRef>,
    loop_: &'l dyn AsLoopRef,
    callback: Box<dyn FnMut(u64) + 'l>,
}

#[derive(Wrapper)]
pub struct SignalSource<'l> {
    #[raw_wrapper]
    ref_: NonNull<SourceRef>,
    loop_: &'l dyn AsLoopRef,
    callback: Box<dyn FnMut(i32) + 'l>,
}

pub trait AsLoopRef: Debug {
    fn loop_(&self) -> &LoopRef;
}

impl LoopRef {
    pub fn add_source(&self, source: &SourceRef) -> crate::Result<RawFd> {
        let result = spa_interface_call!(self, add_source, source.as_raw_ptr())?;
        i32_as_result(result, result)
    }

    pub fn update_source(&self, source: &SourceRef) -> crate::Result<()> {
        let result = spa_interface_call!(self, update_source, source.as_raw_ptr())?;
        i32_as_void_result(result)
    }

    pub fn remove_source(&self, source: &SourceRef) -> crate::Result<()> {
        let result = spa_interface_call!(self, remove_source, source.as_raw_ptr())?;
        i32_as_void_result(result)
    }

    //todo invoke
}

impl LoopControlRef {
    pub fn get_fd(&self) -> crate::Result<RawFd> {
        spa_interface_call!(self, get_fd)
    }

    //todo pub fn add_hook

    pub fn enter(&self) -> crate::Result<()> {
        spa_interface_call!(self, enter)
    }

    pub fn leave(&self) -> crate::Result<()> {
        spa_interface_call!(self, leave)
    }

    pub fn iterate(&self, timeout: i32) -> crate::Result<i32> {
        spa_interface_call!(self, iterate, timeout)
    }
}

impl Drop for IOSource<'_> {
    fn drop(&mut self) {
        self.loop_.loop_().remove_source(self.as_ref());
    }
}

impl Drop for IdleSource<'_> {
    fn drop(&mut self) {
        self.loop_.loop_().remove_source(self.as_ref());
    }
}

impl Drop for EventSource<'_> {
    fn drop(&mut self) {
        self.loop_.loop_().remove_source(self.as_ref());
    }
}

impl Drop for TimerSource<'_> {
    fn drop(&mut self) {
        self.loop_.loop_().remove_source(self.as_ref());
    }
}

impl Drop for SignalSource<'_> {
    fn drop(&mut self) {
        self.loop_.loop_().remove_source(self.as_ref());
    }
}

impl Debug for IOSource<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("IOSource")
            .field("ref_", &self.ref_)
            .field("loop_", &self.loop_)
            .finish()
    }
}

impl Debug for IdleSource<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("IdleSource")
            .field("ref_", &self.ref_)
            .field("loop_", &self.loop_)
            .finish()
    }
}

impl Debug for EventSource<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EventSource")
            .field("ref_", &self.ref_)
            .field("loop_", &self.loop_)
            .finish()
    }
}

impl Debug for TimerSource<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TimerSource")
            .field("ref_", &self.ref_)
            .field("loop_", &self.loop_)
            .finish()
    }
}

impl Debug for SignalSource<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SignalSource")
            .field("ref_", &self.ref_)
            .field("loop_", &self.loop_)
            .finish()
    }
}