pipewire/permissions.rs
1// Copyright The pipewire-rs Contributors.
2// SPDX-License-Identifier: MIT
3
4//! Permissions are used to implement [access control](https://docs.pipewire.org/page_access.html) for PipeWire clients.
5
6use bitflags::bitflags;
7use std::fmt;
8
9bitflags! {
10 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
11 pub struct PermissionFlags: u32 {
12 /// An object with this permission is visible to the client. The client will receive registry events for the object and can interact with it.
13 const R = pw_sys::PW_PERM_R;
14 /// An object with this permission can be modified by the client. This is usually done through a method that modifies the state of the object. Usually implies the [`X`](Self::X) permission.
15 const W = pw_sys::PW_PERM_W;
16 /// An object with this permission allows invoking methods on the object. Some of those methods will only query state, others will modify the object. Modifying the object through one of these methods requires the [`W`](Self:W) permission.
17 const X = pw_sys::PW_PERM_X;
18 /// An object this permission can be used as the subject in metadata.
19 const M = pw_sys::PW_PERM_M;
20 #[cfg(feature = "v0_3_77")]
21 const L = pw_sys::PW_PERM_L;
22 }
23}
24
25/// A `Permission` describes (using [`PermissionFlags`]) what the client is allowed to do with the object of id [`id`](Self::id).
26#[derive(Clone, Copy)]
27#[repr(transparent)]
28pub struct Permission(pw_sys::pw_permission);
29
30impl Permission {
31 pub fn new(id: u32, flags: PermissionFlags) -> Self {
32 Self(pw_sys::pw_permission {
33 id,
34 permissions: flags.bits(),
35 })
36 }
37
38 pub fn id(&self) -> u32 {
39 self.0.id
40 }
41
42 pub fn set_id(&mut self, id: u32) {
43 self.0.id = id;
44 }
45
46 pub fn permission_flags(&self) -> PermissionFlags {
47 PermissionFlags::from_bits_retain(self.0.permissions)
48 }
49
50 pub fn set_permission_flags(&mut self, flags: PermissionFlags) {
51 self.0.permissions = flags.bits();
52 }
53}
54
55impl fmt::Debug for Permission {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 f.debug_struct("Permission")
58 .field("id", &self.id())
59 .field("permission_flags", &self.permission_flags())
60 .finish()
61 }
62}