Skip to content

Commit

Permalink
Refactor params property key and fetching of values.
Browse files Browse the repository at this point in the history
- Use constants for property keys. Removes a bunch of expect calls.

- Use spa_property with type conversion.
  • Loading branch information
saivert committed Jun 26, 2024
1 parent 2bcd2e4 commit 5592af6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 43 deletions.
9 changes: 9 additions & 0 deletions src/backend/paramavailability.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use wireplumber::spa::SpaPod;

#[derive(Debug, Copy, Clone, PartialEq, Eq, glib::Enum, Default)]
#[enum_type(name = "ProfileAvailability")]
pub enum ParamAvailability {
Expand All @@ -16,3 +18,10 @@ impl From<u32> for ParamAvailability {
}
}
}


impl<'a> From<&'a SpaPod> for ParamAvailability {
fn from(value: &'a SpaPod) -> Self {
value.id().map_or(ParamAvailability::Unknown, |x| x.into())
}
}
36 changes: 11 additions & 25 deletions src/backend/pwdeviceobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::backend::PwProfileObject;
use crate::backend::{ParamAvailability, PwProfileObject};
use glib::{
self, clone,
subclass::{prelude::*, Signal},
Expand Down Expand Up @@ -170,10 +170,6 @@ impl PwDeviceObject {
None,
gtk::gio::Cancellable::NONE,
clone!(@weak self as widget => move |res| {
let keys = wp::spa::SpaIdTable::from_name("Spa:Pod:Object:Param:Profile").expect("id table");
let index_key = keys.find_value_from_short_name("index").expect("index key");
let description_key = keys.find_value_from_short_name("description").expect("decription key");
let available_key = keys.find_value_from_short_name("available").expect("available key");

if let Ok(Some(iter)) = res {
let mut profiles: Vec<PwProfileObject> = Vec::new();
Expand All @@ -184,9 +180,9 @@ impl PwDeviceObject {
continue;
}

let index = pod.find_spa_property(&index_key).expect("Index!").int().expect("Int");
let description = pod.find_spa_property(&description_key).expect("Format!").string().expect("String");
let available = pod.find_spa_property(&available_key).expect("Availability!").id().expect("Id");
let index: i32 = pod.spa_property(&wp::spa::ffi::SPA_PARAM_PROFILE_index).expect("Profile index");
let description: String = pod.spa_property(&wp::spa::ffi::SPA_PARAM_PROFILE_description).expect("Profile description");
let available = pod.find_spa_property(&wp::spa::ffi::SPA_PARAM_PROFILE_available).expect("Profile availability").id().expect("Id");

profiles.push(PwProfileObject::new(index as u32, &description, available));
}
Expand All @@ -203,19 +199,15 @@ impl PwDeviceObject {
pub(crate) fn update_current_profile_index(&self) {
let device = self.wpdevice();

let keys = wp::spa::SpaIdTable::from_name("Spa:Pod:Object:Param:Profile").expect("id table");
let index_key = keys.find_value_from_short_name("index").expect("index key");
let description_key = keys.find_value_from_short_name("description").expect("decription key");

if let Some(params) = device.enum_params_sync("Profile", None) {
for a in params {
let pod: wp::spa::SpaPod = a.get().unwrap();
if !pod.is_object() {
continue;
}

let index = pod.find_spa_property(&index_key).expect("Index!").int().expect("Int");
let description = pod.find_spa_property(&description_key).expect("Format!").string().expect("String");
let index: i32 = pod.spa_property(&wp::spa::ffi::SPA_PARAM_PROFILE_index).expect("Profile index");
let description: String = pod.spa_property(&wp::spa::ffi::SPA_PARAM_PROFILE_description).expect("Profile description");
pwvucontrol_info!("Current profile #{} {}", index, description);

self.set_profile_index(index as u32);
Expand Down Expand Up @@ -244,12 +236,6 @@ impl PwDeviceObject {
None,
gtk::gio::Cancellable::NONE,
clone!(@weak self as widget => move |res| {
let keys = wp::spa::SpaIdTable::from_name("Spa:Pod:Object:Param:Route").expect("id table");
let index_key = keys.find_value_from_short_name("index").expect("index key");
let description_key = keys.find_value_from_short_name("description").expect("decription key");
let available_key = keys.find_value_from_short_name("available").expect("available key");
let direction_key = keys.find_value_from_short_name("direction").expect("direction key");
let profiles_key = keys.find_value_from_short_name("profiles").expect("profiles key");

if let Ok(Some(iter)) = res {
let removed = widget.imp().routemodel.n_items();
Expand All @@ -262,11 +248,11 @@ impl PwDeviceObject {
continue;
}

let index = pod.find_spa_property(&index_key).expect("Index").int().expect("Int");
let description = pod.find_spa_property(&description_key).expect("Format!").string().expect("String");
let available = pod.find_spa_property(&available_key).expect("Availability!").id().expect("Id");
let direction = pod.find_spa_property(&direction_key).expect("Direction!").id().expect("Id");
let profiles = pod.find_spa_property(&profiles_key).expect("Profiles!");
let index: i32 = pod.spa_property(&wp::spa::ffi::SPA_PARAM_ROUTE_index).expect("Route index");
let description: String = pod.spa_property(&wp::spa::ffi::SPA_PARAM_ROUTE_description).expect("Route description");
let direction: RouteDirection = pod.spa_property(&wp::spa::ffi::SPA_PARAM_ROUTE_direction).expect("Route direction");
let available: ParamAvailability = pod.spa_property(&wp::spa::ffi::SPA_PARAM_ROUTE_available).expect("Route available");
let profiles = pod.find_spa_property(&wp::spa::ffi::SPA_PARAM_ROUTE_profiles).expect("Profiles!");
assert!(profiles.is_array());
let profiles_vec: Vec<u32> = profiles.array_iterator::<i32>().map(|x| x as u32).collect();

Expand Down
21 changes: 6 additions & 15 deletions src/backend/pwnodeobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,6 @@ impl PwNodeObject {

node.enum_params(Some("Format"), None, gtk::gio::Cancellable::NONE, clone!(@weak self as widget, @weak node => move |res| {
if let Ok(Some(iter)) = res {
let keys = wp::spa::SpaIdTable::from_name("Spa:Pod:Object:Param:Format").expect("id table");
let channels_key = keys.find_value_from_short_name("channels").expect("channels key");
let rate_key = keys.find_value_from_short_name("rate").expect("channels key");
let format_key = keys.find_value_from_short_name("format").expect("format key");
let position_key = keys.find_value_from_short_name("position").expect("position key");

for a in iter {
let pod: wp::spa::SpaPod = a.get().unwrap();
Expand All @@ -373,24 +368,24 @@ impl PwNodeObject {
}
}

let choice = pod.find_spa_property(&format_key).expect("Format!");
let choice = pod.find_spa_property(&wp::spa::ffi::SPA_FORMAT_AUDIO_format).expect("Format!");
let format = get_pod_maybe_choice(choice).id().expect("Format id");
if format == 0 {
pwvucontrol_warning!("Format is 0, ignoring...");
return;
}

let channels = match pod.find_spa_property(&channels_key) {
let channels = match pod.find_spa_property(&wp::spa::ffi::SPA_FORMAT_AUDIO_channels) {
Some(pod) => get_pod_maybe_choice(pod).int().expect("Channels int"),
None => 0
};

let rate = match pod.find_spa_property(&rate_key) {
let rate = match pod.find_spa_property(&wp::spa::ffi::SPA_FORMAT_AUDIO_rate) {
Some(pod) => get_pod_maybe_choice(pod).int().expect("Rate int"),
None => 0
};

let choice = pod.find_spa_property(&position_key).expect("Position!");
let choice = pod.find_spa_property(&wp::spa::ffi::SPA_FORMAT_AUDIO_position).expect("Position!");
let positionpod = get_pod_maybe_choice(choice);
let vec: Vec<u32> = positionpod.array_iterator().map(|x: i32| x as u32).collect();
let mut a = [0u32;64];
Expand Down Expand Up @@ -424,20 +419,16 @@ impl PwNodeObject {
.enum_params_sync("Props", None)
.expect("getting params");

let keys = wp::spa::SpaIdTable::from_name("Spa:Pod:Object:Param:Props").expect("id table");
let volume_key = keys.find_value_from_short_name("volume").expect("volume key");
let monitorvolumes_key = keys.find_value_from_short_name("monitorVolumes").expect("monitorVolumes key");

for a in params {
let pod: wp::spa::SpaPod = a.get().unwrap();
if pod.is_object() {
if let Some(val) = pod.find_spa_property(&volume_key) {
if let Some(val) = pod.find_spa_property(&wp::spa::ffi::SPA_PROP_volume) {
if let Some(volume) = val.float() {
self.set_mainvolume(volume);
}
}

if let Some(val) = pod.find_spa_property(&monitorvolumes_key) {
if let Some(val) = pod.find_spa_property(&wp::spa::ffi::SPA_PROP_monitorVolumes) {
if val.is_array() {
let volume = val.array_iterator::<f32>().max_by(f32::total_cmp);
self.set_monitorvolume(volume.unwrap_or_default());
Expand Down
6 changes: 3 additions & 3 deletions src/backend/pwrouteobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ glib::wrapper! {
}

impl PwRouteObject {
pub(crate) fn new(index: u32, description: &str, availability: u32, direction: u32, profiles: &[u32]) -> Self {
pub(crate) fn new(index: u32, description: &str, availability: ParamAvailability, direction: RouteDirection, profiles: &[u32]) -> Self {
let new: PwRouteObject = glib::Object::builder()
.property("index", index)
.property("description", format!("{description} ({index})"))
.property("availability", ParamAvailability::from(availability))
.property("direction", RouteDirection::from(direction))
.property("availability", availability)
.property("direction", direction)
.build();

new.set_profiles(profiles);
Expand Down
8 changes: 8 additions & 0 deletions src/backend/routedirection.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use wireplumber::spa::SpaPod;

#[derive(Debug, Copy, Clone, PartialEq, Eq, glib::Enum, Default)]
#[enum_type(name = "RouteDirection")]
pub enum RouteDirection {
Expand All @@ -17,6 +19,12 @@ impl From<u32> for RouteDirection {
}
}

impl<'a> From<&'a SpaPod> for RouteDirection {
fn from(value: &'a SpaPod) -> Self {
value.id().map_or(RouteDirection::Unknown, |x| x.into())
}
}

impl From<RouteDirection> for u32 {
fn from(value: RouteDirection) -> Self {
value as u32
Expand Down

0 comments on commit 5592af6

Please sign in to comment.