rodecaster_protocol/
capabilities.rs1use crate::{DeviceModel, Fader, Layout, MixOutput, Node, Source, Value};
12
13#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct DeviceCapabilities {
16 model: DeviceModel,
17 firmware: Option<String>,
18 faders: Vec<Fader>,
19 sources: Vec<Source>,
20 mix_outputs: Vec<MixOutput>,
21 channel_count: u8,
22 input_source_count: u8,
23 headphone_count: u8,
24 effects_count: u8,
25 pad_count: u8,
26 sip_call_slots_count: u8,
27 sip_registration_count: u8,
28 is_in_setup: bool,
29}
30
31impl DeviceCapabilities {
32 pub(crate) fn discover(root: &Node, layout: &Layout) -> Self {
33 let model = layout.model();
34 let is_in_setup = find_bool_property(root, "disableAllPhysicalButtons").unwrap_or(false)
35 || (find_bool_property(root, "disableAllLineoutOutputs").unwrap_or(false)
36 && find_bool_property(root, "disableAllHeadphoneOutputs").unwrap_or(false));
37 Self {
38 model,
39 firmware: find_string_property(root, "systemFirmwareVersion").map(str::to_owned),
40 faders: (0..layout.fader_count())
41 .filter_map(|index| Fader::from_index(model, index))
42 .collect(),
43 sources: (0..layout.source_count())
44 .filter_map(Source::from_protocol)
45 .collect(),
46 mix_outputs: (0..layout.mix_count_per_source())
47 .filter_map(MixOutput::from_protocol)
48 .collect(),
49 channel_count: layout.channel_count(),
50 input_source_count: layout.input_source_count(),
51 headphone_count: layout.headphone_count(),
52 effects_count: layout.effects_count(),
53 pad_count: layout.pad_count(),
54 sip_call_slots_count: layout.sip_call_slots_count(),
55 sip_registration_count: layout.sip_registration_count(),
56 is_in_setup,
57 }
58 }
59
60 pub fn model(&self) -> DeviceModel {
62 self.model
63 }
64
65 pub fn firmware(&self) -> Option<&str> {
67 self.firmware.as_deref()
68 }
69
70 pub fn faders(&self) -> &[Fader] {
72 &self.faders
73 }
74
75 pub fn sources(&self) -> &[Source] {
77 &self.sources
78 }
79
80 pub fn mix_outputs(&self) -> &[MixOutput] {
82 &self.mix_outputs
83 }
84
85 pub fn supports_fader(&self, fader: Fader) -> bool {
87 self.faders.contains(&fader)
88 }
89
90 pub fn channel_count(&self) -> u8 {
91 self.channel_count
92 }
93
94 pub fn input_source_count(&self) -> u8 {
95 self.input_source_count
96 }
97
98 pub fn headphone_count(&self) -> u8 {
99 self.headphone_count
100 }
101
102 pub fn physical_combo_count(&self) -> u8 {
105 match self.model {
106 DeviceModel::Duo => 2,
107 DeviceModel::Pro2 => 4,
108 }
109 }
110
111 pub fn physical_headphone_count(&self) -> u8 {
114 match self.model {
115 DeviceModel::Duo => 2,
116 DeviceModel::Pro2 => 4,
117 }
118 }
119
120 pub fn effects_count(&self) -> u8 {
121 self.effects_count
122 }
123
124 pub fn pad_count(&self) -> u8 {
125 self.pad_count
126 }
127
128 pub fn sip_call_slots_count(&self) -> u8 {
129 self.sip_call_slots_count
130 }
131
132 pub fn sip_registration_count(&self) -> u8 {
133 self.sip_registration_count
134 }
135
136 pub fn is_in_setup(&self) -> bool {
138 self.is_in_setup
139 }
140}
141
142fn find_bool_property(node: &Node, name: &str) -> Option<bool> {
143 node.properties
144 .iter()
145 .find(|property| property.name == name)
146 .and_then(|property| match &property.value {
147 Value::Bool(value) => Some(*value),
148 _ => None,
149 })
150 .or_else(|| {
151 node.children
152 .iter()
153 .find_map(|child| find_bool_property(child, name))
154 })
155}
156
157fn find_string_property<'a>(node: &'a Node, name: &str) -> Option<&'a str> {
158 node.properties
159 .iter()
160 .find(|property| property.name == name)
161 .and_then(|property| match &property.value {
162 Value::String(value) => Some(value.as_str()),
163 _ => None,
164 })
165 .or_else(|| {
166 node.children
167 .iter()
168 .find_map(|child| find_string_property(child, name))
169 })
170}
171
172#[cfg(test)]
173mod tests {
174 use super::*;
175
176 #[test]
177 fn physical_jack_counts_match_hardware() {
178 let duo_caps = DeviceCapabilities {
179 model: DeviceModel::Duo,
180 firmware: None,
181 faders: vec![],
182 sources: vec![],
183 mix_outputs: vec![],
184 channel_count: 10,
185 input_source_count: 19,
186 headphone_count: 4,
187 effects_count: 11,
188 pad_count: 21,
189 sip_call_slots_count: 3,
190 sip_registration_count: 2,
191 is_in_setup: false,
192 };
193 assert_eq!(duo_caps.physical_combo_count(), 2);
194 assert_eq!(duo_caps.physical_headphone_count(), 2);
195
196 let pro2_caps = DeviceCapabilities {
197 model: DeviceModel::Pro2,
198 ..duo_caps
199 };
200 assert_eq!(pro2_caps.physical_combo_count(), 4);
201 assert_eq!(pro2_caps.physical_headphone_count(), 4);
202 }
203}