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
// Copyright The pipewire-rs Contributors.
// SPDX-License-Identifier: MIT

use std::ops::Deref;
use std::ptr;
use std::rc::{Rc, Weak};

use crate::{error::Error, Properties};
use crate::{AsLoop, LoopRef};
use spa::ReadableDict;

#[derive(Debug, Clone)]
pub struct MainLoop {
    inner: Rc<MainLoopInner>,
}

impl MainLoop {
    /// Initialize Pipewire and create a new `MainLoop`
    pub fn new() -> Result<Self, Error> {
        super::init();
        let inner = MainLoopInner::new::<Properties>(None)?;
        Ok(Self {
            inner: Rc::new(inner),
        })
    }

    pub fn with_properties<T: ReadableDict>(properties: &T) -> Result<Self, Error> {
        let inner = MainLoopInner::new(Some(properties))?;
        Ok(Self {
            inner: Rc::new(inner),
        })
    }

    pub fn downgrade(&self) -> WeakMainLoop {
        let weak = Rc::downgrade(&self.inner);
        WeakMainLoop { weak }
    }
}

impl AsLoop for MainLoop {
    type Target = MainLoopInner;

    fn as_loop(&self) -> &Rc<Self::Target> {
        &self.inner
    }
}

impl std::convert::AsRef<LoopRef> for MainLoop {
    fn as_ref(&self) -> &LoopRef {
        self.deref()
    }
}

impl Deref for MainLoop {
    type Target = MainLoopInner;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

pub struct WeakMainLoop {
    weak: Weak<MainLoopInner>,
}

impl WeakMainLoop {
    pub fn upgrade(&self) -> Option<MainLoop> {
        self.weak.upgrade().map(|inner| MainLoop { inner })
    }
}

#[derive(Debug)]
pub struct MainLoopInner {
    ptr: ptr::NonNull<pw_sys::pw_main_loop>,
}

impl MainLoopInner {
    fn new<T: ReadableDict>(properties: Option<&T>) -> Result<Self, Error> {
        unsafe {
            let props = properties.map_or(ptr::null(), |props| props.get_dict_ptr()) as *mut _;
            let l = pw_sys::pw_main_loop_new(props);
            let ptr = ptr::NonNull::new(l).ok_or(Error::CreationFailed)?;

            Ok(MainLoopInner { ptr })
        }
    }

    fn as_ptr(&self) -> *mut pw_sys::pw_main_loop {
        self.ptr.as_ptr()
    }

    pub fn run(&self) {
        unsafe {
            pw_sys::pw_main_loop_run(self.as_ptr());
        }
    }

    pub fn quit(&self) {
        unsafe {
            pw_sys::pw_main_loop_quit(self.as_ptr());
        }
    }
}

impl std::convert::AsRef<LoopRef> for MainLoopInner {
    fn as_ref(&self) -> &LoopRef {
        self.deref()
    }
}

impl std::ops::Deref for MainLoopInner {
    type Target = LoopRef;

    fn deref(&self) -> &Self::Target {
        unsafe { &*(pw_sys::pw_main_loop_get_loop(self.ptr.as_ptr()) as *mut LoopRef) }
    }
}

impl Drop for MainLoopInner {
    fn drop(&mut self) {
        unsafe { pw_sys::pw_main_loop_destroy(self.ptr.as_ptr()) }
    }
}