pipewire/main_loop/mod.rs
1// Copyright The pipewire-rs Contributors.
2// SPDX-License-Identifier: MIT
3
4//! Wrapper that runs a [loop](crate::loop_) in the current thread.
5//!
6//! This module contains wrappers for [`pw_main_loop`](pw_sys::pw_main_loop) and related items.
7
8use crate::loop_::Loop;
9
10mod box_;
11pub use box_::*;
12mod rc;
13pub use rc::*;
14
15/// Transparent wrapper around a [main loop](self).
16///
17/// This does not own the underlying object and is usually seen behind a `&` reference.
18///
19/// For owning wrappers that can construct main loops, see [`MainLoopBox`] and [`MainLoopRc`].
20///
21/// For an explanation of these, see [Smart pointers to PipeWire
22/// objects](crate#smart-pointers-to-pipewire-objects).
23#[repr(transparent)]
24pub struct MainLoop(pw_sys::pw_main_loop);
25
26impl MainLoop {
27 pub fn as_raw(&self) -> &pw_sys::pw_main_loop {
28 &self.0
29 }
30
31 pub fn as_raw_ptr(&self) -> *mut pw_sys::pw_main_loop {
32 std::ptr::addr_of!(self.0).cast_mut()
33 }
34
35 pub fn loop_(&self) -> &Loop {
36 unsafe {
37 let pw_loop = pw_sys::pw_main_loop_get_loop(self.as_raw_ptr());
38 // FIXME: Make sure pw_loop is not null
39 &*(pw_loop.cast::<Loop>())
40 }
41 }
42
43 pub fn run(&self) {
44 unsafe {
45 pw_sys::pw_main_loop_run(self.as_raw_ptr());
46 }
47 }
48
49 pub fn quit(&self) {
50 unsafe {
51 pw_sys::pw_main_loop_quit(self.as_raw_ptr());
52 }
53 }
54}
55
56impl std::convert::AsRef<Loop> for MainLoop {
57 fn as_ref(&self) -> &Loop {
58 self.loop_()
59 }
60}