use crate::{DeviceModel, Fader, Layout, MixOutput, Node, Source, Value};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeviceCapabilities {
model: DeviceModel,
firmware: Option<String>,
faders: Vec<Fader>,
sources: Vec<Source>,
mix_outputs: Vec<MixOutput>,
channel_count: u8,
input_source_count: u8,
headphone_count: u8,
effects_count: u8,
pad_count: u8,
sip_call_slots_count: u8,
sip_registration_count: u8,
is_in_setup: bool,
}
impl DeviceCapabilities {
pub(crate) fn discover(root: &Node, layout: &Layout) -> Self {
let model = layout.model();
let is_in_setup = find_bool_property(root, "disableAllPhysicalButtons").unwrap_or(false)
|| (find_bool_property(root, "disableAllLineoutOutputs").unwrap_or(false)
&& find_bool_property(root, "disableAllHeadphoneOutputs").unwrap_or(false));
Self {
model,
firmware: find_string_property(root, "systemFirmwareVersion").map(str::to_owned),
faders: (0..layout.fader_count())
.filter_map(|index| Fader::from_index(model, index))
.collect(),
sources: (0..layout.source_count())
.filter_map(Source::from_protocol)
.collect(),
mix_outputs: (0..layout.mix_count_per_source())
.filter_map(MixOutput::from_protocol)
.collect(),
channel_count: layout.channel_count(),
input_source_count: layout.input_source_count(),
headphone_count: layout.headphone_count(),
effects_count: layout.effects_count(),
pad_count: layout.pad_count(),
sip_call_slots_count: layout.sip_call_slots_count(),
sip_registration_count: layout.sip_registration_count(),
is_in_setup,
}
}
pub fn model(&self) -> DeviceModel {
self.model
}
pub fn firmware(&self) -> Option<&str> {
self.firmware.as_deref()
}
pub fn faders(&self) -> &[Fader] {
&self.faders
}
pub fn sources(&self) -> &[Source] {
&self.sources
}
pub fn mix_outputs(&self) -> &[MixOutput] {
&self.mix_outputs
}
pub fn supports_fader(&self, fader: Fader) -> bool {
self.faders.contains(&fader)
}
pub fn channel_count(&self) -> u8 {
self.channel_count
}
pub fn input_source_count(&self) -> u8 {
self.input_source_count
}
pub fn headphone_count(&self) -> u8 {
self.headphone_count
}
pub fn physical_combo_count(&self) -> u8 {
match self.model {
DeviceModel::Duo => 2,
DeviceModel::Pro2 => 4,
}
}
pub fn physical_headphone_count(&self) -> u8 {
match self.model {
DeviceModel::Duo => 2,
DeviceModel::Pro2 => 4,
}
}
pub fn effects_count(&self) -> u8 {
self.effects_count
}
pub fn pad_count(&self) -> u8 {
self.pad_count
}
pub fn sip_call_slots_count(&self) -> u8 {
self.sip_call_slots_count
}
pub fn sip_registration_count(&self) -> u8 {
self.sip_registration_count
}
pub fn is_in_setup(&self) -> bool {
self.is_in_setup
}
}
fn find_bool_property(node: &Node, name: &str) -> Option<bool> {
node.properties
.iter()
.find(|property| property.name == name)
.and_then(|property| match &property.value {
Value::Bool(value) => Some(*value),
_ => None,
})
.or_else(|| {
node.children
.iter()
.find_map(|child| find_bool_property(child, name))
})
}
fn find_string_property<'a>(node: &'a Node, name: &str) -> Option<&'a str> {
node.properties
.iter()
.find(|property| property.name == name)
.and_then(|property| match &property.value {
Value::String(value) => Some(value.as_str()),
_ => None,
})
.or_else(|| {
node.children
.iter()
.find_map(|child| find_string_property(child, name))
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn physical_jack_counts_match_hardware() {
let duo_caps = DeviceCapabilities {
model: DeviceModel::Duo,
firmware: None,
faders: vec![],
sources: vec![],
mix_outputs: vec![],
channel_count: 10,
input_source_count: 19,
headphone_count: 4,
effects_count: 11,
pad_count: 21,
sip_call_slots_count: 3,
sip_registration_count: 2,
is_in_setup: false,
};
assert_eq!(duo_caps.physical_combo_count(), 2);
assert_eq!(duo_caps.physical_headphone_count(), 2);
let pro2_caps = DeviceCapabilities {
model: DeviceModel::Pro2,
..duo_caps
};
assert_eq!(pro2_caps.physical_combo_count(), 4);
assert_eq!(pro2_caps.physical_headphone_count(), 4);
}
}