Skip to content

Commit

Permalink
feat: Move structs from deamon to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
DashieTM committed Nov 8, 2023
1 parent 53af159 commit a2e9daa
Show file tree
Hide file tree
Showing 10 changed files with 1,600 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ license = "GPL-3.0-only"

[dependencies]
directories-next = "2.0.0"
dbus = "0.9.7"
190 changes: 190 additions & 0 deletions src/audio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
use dbus::{
arg::{self, Append, Arg, ArgType, Get},
Signature,
};

#[derive(Debug)]
pub struct PulseError(&'static str);

pub struct Source {
index: u32,
name: String,
alias: String,
channels: u16,
volume: Vec<u32>,
muted: bool,
}

impl Append for Source {
fn append_by_ref(&self, iter: &mut arg::IterAppend) {
iter.append_struct(|i| {
i.append(&self.index);
i.append(&self.name);
i.append(&self.alias);
i.append(&self.channels);
i.append(&self.volume);
i.append(&self.muted);
});
}
}

impl<'a> Get<'a> for Source {
fn get(i: &mut arg::Iter<'a>) -> Option<Self> {
let (index, name, alias, channels, volume, muted) =
<(u32, String, String, u16, Vec<u32>, bool)>::get(i)?;
Some(Self {
index,
name,
alias,
channels,
volume,
muted,
})
}
}

impl Arg for Source {
const ARG_TYPE: arg::ArgType = ArgType::Struct;
fn signature() -> Signature<'static> {
unsafe { Signature::from_slice_unchecked("(ussqaub)\0") }
}
}

#[derive(Debug)]
pub struct Sink {
index: u32,
name: String,
alias: String,
channels: u16,
volume: Vec<u32>,
muted: bool,
}

impl Append for Sink {
fn append_by_ref(&self, iter: &mut arg::IterAppend) {
iter.append_struct(|i| {
i.append(&self.index);
i.append(&self.name);
i.append(&self.alias);
i.append(&self.channels);
i.append(&self.volume);
i.append(&self.muted);
});
}
}

impl<'a> Get<'a> for Sink {
fn get(i: &mut arg::Iter<'a>) -> Option<Self> {
let (index, name, alias, channels, volume, muted) =
<(u32, String, String, u16, Vec<u32>, bool)>::get(i)?;
Some(Self {
index,
name,
alias,
channels,
volume,
muted,
})
}
}

impl Arg for Sink {
const ARG_TYPE: arg::ArgType = ArgType::Struct;
fn signature() -> Signature<'static> {
unsafe { Signature::from_slice_unchecked("(ussqaub)\0") }
}
}

pub struct InputStream {
index: u32,
name: String,
application_name: String,
sink_index: u32,
channels: u16,
volume: Vec<u32>,
muted: bool,
}

impl Append for InputStream {
fn append_by_ref(&self, iter: &mut arg::IterAppend) {
iter.append_struct(|i| {
i.append(&self.index);
i.append(&self.name);
i.append(&self.application_name);
i.append(&self.sink_index);
i.append(&self.channels);
i.append(&self.volume);
i.append(&self.muted);
});
}
}

impl<'a> Get<'a> for InputStream {
fn get(i: &mut arg::Iter<'a>) -> Option<Self> {
let (index, name, application_name, sink_index, channels, volume, muted) =
<(u32, String, String, u32, u16, Vec<u32>, bool)>::get(i)?;
Some(Self {
index,
name,
application_name,
sink_index,
channels,
volume,
muted,
})
}
}

impl Arg for InputStream {
const ARG_TYPE: arg::ArgType = ArgType::Struct;
fn signature() -> Signature<'static> {
unsafe { Signature::from_slice_unchecked("(ussuqaub)\0") }
}
}

pub struct OutputStream {
index: u32,
name: String,
application_name: String,
source_index: u32,
channels: u16,
volume: Vec<u32>,
muted: bool,
}

impl Append for OutputStream {
fn append_by_ref(&self, iter: &mut arg::IterAppend) {
iter.append_struct(|i| {
i.append(&self.index);
i.append(&self.name);
i.append(&self.application_name);
i.append(&self.source_index);
i.append(&self.channels);
i.append(&self.volume);
i.append(&self.muted);
});
}
}

impl<'a> Get<'a> for OutputStream {
fn get(i: &mut arg::Iter<'a>) -> Option<Self> {
let (index, name, application_name, source_index, channels, volume, muted) =
<(u32, String, String, u32, u16, Vec<u32>, bool)>::get(i)?;
Some(Self {
index,
name,
application_name,
source_index,
channels,
volume,
muted,
})
}
}

impl Arg for OutputStream {
const ARG_TYPE: arg::ArgType = ArgType::Struct;
fn signature() -> Signature<'static> {
unsafe { Signature::from_slice_unchecked("(ussuqaub)\0") }
}
}
68 changes: 68 additions & 0 deletions src/bluetooth/bluetooth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use dbus::{
arg::{self, Append, Arg, ArgType, Get},
Path, Signature,
};

#[derive(Debug, Clone)]
pub struct BluetoothDevice {
path: Path<'static>,
rssi: i16,
name: String,
adapter: Path<'static>,
trusted: bool,
bonded: bool,
paired: bool,
blocked: bool,
address: String,
}

unsafe impl Send for BluetoothDevice {}
unsafe impl Sync for BluetoothDevice {}

impl<'a> Get<'a> for BluetoothDevice {
fn get(i: &mut arg::Iter<'a>) -> Option<Self> {
let path = <Path<'static>>::get(i)?;
let rssi = <i16>::get(i)?;
let name = <String>::get(i)?;
let adapter = <Path<'static>>::get(i)?;
let trusted = <bool>::get(i)?;
let bonded = <bool>::get(i)?;
let paired = <bool>::get(i)?;
let blocked = <bool>::get(i)?;
let address = <String>::get(i)?;
Some(BluetoothDevice {
path,
rssi,
name,
adapter,
trusted,
bonded,
paired,
blocked,
address,
})
}
}

impl Append for BluetoothDevice {
fn append_by_ref(&self, iter: &mut arg::IterAppend) {
iter.append_struct(|i| {
i.append(&self.path);
i.append(&self.rssi);
i.append(&self.name);
i.append(&self.adapter);
i.append(&self.trusted);
i.append(&self.bonded);
i.append(&self.paired);
i.append(&self.blocked);
i.append(&self.address);
});
}
}

impl Arg for BluetoothDevice {
const ARG_TYPE: arg::ArgType = ArgType::Struct;
fn signature() -> Signature<'static> {
unsafe { Signature::from_slice_unchecked("(nsobbbbs)\0") }
}
}
83 changes: 83 additions & 0 deletions src/bluetooth/bluetooth_signals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// This code was autogenerated with `dbus-codegen-rust -s -d org.bluez -c blocking`, see https://github.com/diwic/dbus-rs
use dbus as dbus;
#[allow(unused_imports)]
use dbus::arg;
use dbus::blocking;

pub trait OrgFreedesktopDBusIntrospectable {
fn introspect(&self) -> Result<String, dbus::Error>;
}

impl<'a, T: blocking::BlockingSender, C: ::std::ops::Deref<Target=T>> OrgFreedesktopDBusIntrospectable for blocking::Proxy<'a, C> {

fn introspect(&self) -> Result<String, dbus::Error> {
self.method_call("org.freedesktop.DBus.Introspectable", "Introspect", ())
.and_then(|r: (String, )| Ok(r.0, ))
}
}

pub trait OrgFreedesktopDBusObjectManager {
fn get_managed_objects(&self) -> Result<::std::collections::HashMap<dbus::Path<'static>, ::std::collections::HashMap<String, arg::PropMap>>, dbus::Error>;
}

#[derive(Debug)]
pub struct InterfacesAddedSignal {
pub object: dbus::Path<'static>,
pub interfaces: ::std::collections::HashMap<String, arg::PropMap>,
}

impl arg::AppendAll for InterfacesAddedSignal {
fn append(&self, i: &mut arg::IterAppend) {
arg::RefArg::append(&self.object, i);
arg::RefArg::append(&self.interfaces, i);
}
}

impl arg::ReadAll for InterfacesAddedSignal {
fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> {
Ok(InterfacesAddedSignal {
object: i.read()?,
interfaces: i.read()?,
})
}
}

impl dbus::message::SignalArgs for InterfacesAddedSignal {
const NAME: &'static str = "InterfacesAdded";
const INTERFACE: &'static str = "org.freedesktop.DBus.ObjectManager";
}

#[derive(Debug)]
pub struct InterfaceRemovedSignal {
pub object: dbus::Path<'static>,
pub interfaces: Vec<String>,
}

impl arg::AppendAll for InterfaceRemovedSignal {
fn append(&self, i: &mut arg::IterAppend) {
arg::RefArg::append(&self.object, i);
arg::RefArg::append(&self.interfaces, i);
}
}

impl arg::ReadAll for InterfaceRemovedSignal {
fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> {
Ok(InterfaceRemovedSignal {
object: i.read()?,
interfaces: i.read()?,
})
}
}

impl dbus::message::SignalArgs for InterfaceRemovedSignal {
const NAME: &'static str = "InterfacesRemoved";
const INTERFACE: &'static str = "org.freedesktop.DBus.ObjectManager";
}

impl<'a, T: blocking::BlockingSender, C: ::std::ops::Deref<Target=T>> OrgFreedesktopDBusObjectManager for blocking::Proxy<'a, C> {

fn get_managed_objects(&self) -> Result<::std::collections::HashMap<dbus::Path<'static>, ::std::collections::HashMap<String, arg::PropMap>>, dbus::Error> {
self.method_call("org.freedesktop.DBus.ObjectManager", "GetManagedObjects", ())
.and_then(|r: (::std::collections::HashMap<dbus::Path<'static>, ::std::collections::HashMap<String, arg::PropMap>>, )| Ok(r.0, ))
}
}
2 changes: 2 additions & 0 deletions src/bluetooth/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod bluetooth;
pub mod bluetooth_signals;
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use directories_next as dirs;
use std::{fmt, fs, path::PathBuf};

mod tests;
pub mod audio;
pub mod network;
pub mod bluetooth;

#[derive(Debug, Clone)]
struct PathNotFoundError;
Expand All @@ -12,6 +15,7 @@ impl fmt::Display for PathNotFoundError {
}
}

#[allow(dead_code)]
fn create_config(project_organization: &str, project_name: &str) -> Option<PathBuf> {
let config_dir = dirs::ProjectDirs::from("com", project_organization, project_name)?;
let config_dir = config_dir.config_dir();
Expand Down
Loading

0 comments on commit a2e9daa

Please sign in to comment.