1use crate::types::*;
7use crate::wire::{BayUid, DeviceUid, EdidProfile, LinkFeature, RcAction, RcKey, RcType};
8
9macro_rules! events {
22 (
23 device {
24 $( $(#[$dmeta:meta])* $dvariant:ident => $dmethod:ident ( $($darg:ident : $dty:ty),* ); )*
25 }
26 bay {
27 $( $(#[$bmeta:meta])* $bvariant:ident => $bmethod:ident ( $($barg:ident : $bty:ty),* ); )*
28 }
29 bay_and_device {
30 $( $(#[$lmeta:meta])* $lvariant:ident => $lmethod:ident ( $($larg:ident : $lty:ty),* ); )*
31 }
32 ) => {
33 #[derive(Clone, Debug, PartialEq)]
38 #[non_exhaustive]
39 pub enum Event {
40 $(
41 $(#[$dmeta])*
42 $dvariant {
43 device: DeviceUid,
45 $( #[allow(missing_docs)] $darg: $dty, )*
46 },
47 )*
48 $(
49 $(#[$bmeta])*
50 $bvariant {
51 bay: BayUid,
53 $( #[allow(missing_docs)] $barg: $bty, )*
54 },
55 )*
56 $(
57 $(#[$lmeta])*
58 $lvariant {
59 bay: BayUid,
61 $( #[allow(missing_docs)] $larg: $lty, )*
62 },
63 )*
64 }
65
66 #[allow(unused_variables)]
74 pub trait EventHandler: Send + Sync {
75 $(
76 $(#[$dmeta])*
77 fn $dmethod(&self, device: DeviceUid $(, $darg: $dty)*) {}
78 )*
79 $(
80 $(#[$bmeta])*
81 fn $bmethod(&self, bay: BayUid $(, $barg: $bty)*) {}
82 )*
83 $(
84 $(#[$lmeta])*
85 fn $lmethod(&self, bay: BayUid $(, $larg: $lty)*) {}
86 )*
87
88 fn on_device_update(&self, device: DeviceUid) {}
90
91 fn on_bay_update(&self, bay: BayUid) {}
93 }
94
95 impl Event {
96 pub(crate) fn dispatch(self, handler: &dyn EventHandler) {
99 match self {
100 $(
101 Self::$dvariant { device $(, $darg)* } => {
102 handler.$dmethod(device $(, $darg)*);
103 handler.on_device_update(device);
104 }
105 )*
106 $(
107 Self::$bvariant { bay $(, $barg)* } => {
108 handler.$bmethod(bay $(, $barg)*);
109 handler.on_bay_update(bay);
110 }
111 )*
112 $(
113 Self::$lvariant { bay $(, $larg)* } => {
114 handler.$lmethod(bay $(, $larg)*);
115 handler.on_bay_update(bay);
116 handler.on_device_update(bay.device);
117 }
118 )*
119 }
120 }
121 }
122 };
123}
124
125impl EventHandler for () {}
132
133events! {
134 device {
135 DeviceConfigChanged => on_device_config_changed();
137 DeviceConfigComplete => on_device_config_complete();
139 DeviceOnlineChanged => on_device_online_changed(online: bool);
141 DeviceTemperatureChanged => on_device_temperature_changed(temperatures: Vec<u8>);
143 FirmwareVersionChanged => on_firmware_version_changed(version: FirmwareVersion);
145 SystemStatusChanged => on_system_status_changed(status: u16, message: String);
147 NetworkStatusChanged => on_network_status_changed(status: NetworkPortStatus);
149 V2ipStatsChanged => on_v2ip_stats_changed(stats: V2ipDeviceStats);
151 V2ipSourcesChanged => on_v2ip_sources_changed(sources: Vec<V2ipStreamSources>);
153 V2ipDetailsChanged => on_v2ip_details_changed(details: DeviceV2ipDetails);
155 V2ipSinkChanged => on_v2ip_sink_changed(sink: DeviceV2ipSink);
158 MultiviewerStatusChanged => on_multiviewer_status_changed(status: MultiviewerStatus);
160 AudioEndpointsChanged => on_audio_endpoints_changed(endpoints: AudioEndpoints);
162 MeshMasterChanged => on_mesh_master_changed(master: DeviceUid);
164 TopologyChanged => on_topology_changed(topology: Vec<TopologyEntry>);
166 AmpDolbySettingsChanged => on_amp_dolby_settings_changed(settings: AmpDolbySettings);
168 SetupStatusChanged => on_setup_status_changed(completed: bool);
170 InstallerIdChanged => on_installer_id_changed(installer_id: u16);
172 TilingChanged => on_tiling_changed(tiling: V2ipTilingConfig);
174 RcSettingsChanged => on_rc_settings_changed(settings: RcSettings);
176 V2ipLinkChanged => on_v2ip_link_changed(target: DeviceUid);
178 MultiviewerCommand => on_multiviewer_command(command: MultiviewerCommand);
180 AudioSelectInput => on_audio_select_input(change: AudioChangeSource);
182 AudioEndpointMute => on_audio_endpoint_mute(endpoint: u16, muted: bool);
184 AudioEndpointTrigger => on_audio_endpoint_trigger(endpoint: u16, active: bool);
186 AudioEndpointVolume => on_audio_endpoint_volume(endpoint: u16, volume: u32);
188 DiscoverRequest => on_discover_request();
190 SetRouteRequested => on_set_route_requested(request: SetRouteRequest);
192 EdidRequested => on_edid_requested(request: EdidRequest);
194 EdidReceived => on_edid_received(edid: EdidRecord);
196 BayNameChangeRequested => on_bay_name_change_requested(change: BayNameChange);
198 EdidProfileChangeRequested => on_edid_profile_change_requested(change: EdidProfileChange);
200 RebootRequested => on_reboot_requested(request: RebootRequest);
202 FactoryResetRequested => on_factory_reset_requested(request: FactoryResetRequest);
204 MonitoringPulse => on_monitoring_pulse();
206 UpgradeFpgaRequested => on_upgrade_fpga_requested();
208 DetectBaysRequested => on_detect_bays_requested();
210 PowerSaveRequested => on_power_save_requested(request: V2ipPowerSaveRequest);
212 KeyTransmitRequested => on_key_transmit_requested(request: KeyTransmitRequest);
214 ActionTransmitRequested => on_action_transmit_requested(request: ActionTransmitRequest);
216 IrTransmitRequested => on_ir_transmit_requested(request: IrTransmitRequest);
218 BlacklistChanged => on_blacklist_changed(change: V2ipBlacklistChange);
220 VideoWallCommand => on_video_wall_command(command: VideoWallCommand);
222 }
223 bay {
224 BayRegistered => on_bay_registered();
226 VideoSourceChanged => on_video_source_changed(source: Option<BayUid>);
228 AudioSourceChanged => on_audio_source_changed(source: Option<BayUid>);
230 VolumeChanged => on_volume_changed(volume: VolumeMuteStatus);
232 PowerChanged => on_power_changed(power: PowerStatus);
234 NameChanged => on_name_changed(name: String);
236 SignalDetectedChanged => on_signal_detected_changed(detected: bool);
238 FaultyChanged => on_faulty_changed(faulty: bool);
240 HiddenChanged => on_hidden_changed(hidden: bool);
242 PoePoweredChanged => on_poe_powered_changed(powered: bool);
244 HdbtConnectedChanged => on_hdbt_connected_changed(connected: bool);
246 SignalTypeChanged => on_signal_type_changed(signal_type: String);
248 HpdDetectedChanged => on_hpd_detected_changed(detected: bool);
250 CecDetectedChanged => on_cec_detected_changed(detected: bool);
252 ArcChanged => on_arc_changed(arc: ArcStatus);
254 EdidProfileChanged => on_edid_profile_changed(profile: EdidProfile);
256 RcTypeChanged => on_rc_type_changed(rc_type: RcType);
258 KeyPressed => on_key_pressed(key: RcKey);
260 ActionReceived => on_action_received(action: RcAction);
262 MirrorStatusChanged => on_mirror_status_changed(mirror: BayMirrorStatus);
264 AmpZoneSettingsChanged => on_amp_zone_settings_changed(settings: AmpZoneSettings);
266 VolumeStep => on_volume_step(up: bool);
268 AudioClipped => on_audio_clip(clip: AudioClip);
270 IrCaptured => on_ir_captured(capture: IrCapture);
272 FilteredDevicesChanged => on_filtered_devices_changed(filtered: Vec<DeviceUid>);
274 AudioEndpointChanged => on_audio_endpoint_changed(endpoint: u8);
276 EncoderDisabledChanged => on_encoder_disabled_changed(disabled: bool);
278 DecoderDisabledChanged => on_decoder_disabled_changed(disabled: bool);
280 }
281 bay_and_device {
282 BayLinked => on_bay_linked(linked_serial: String, bay_name: String, features: LinkFeature);
289 BayUnlinked => on_bay_unlinked(linked_serial: String, bay_name: String);
294 }
295}
296
297#[cfg(test)]
298mod tests {
299 use super::*;
300 use std::sync::Mutex;
301
302 #[derive(Default)]
303 struct Recorder {
304 calls: Mutex<Vec<String>>,
305 }
306
307 impl Recorder {
308 fn record(&self, what: &str) {
309 if let Ok(mut calls) = self.calls.lock() {
310 calls.push(what.to_owned());
311 }
312 }
313
314 fn calls(&self) -> Vec<String> {
315 self.calls.lock().map(|c| c.clone()).unwrap_or_default()
316 }
317 }
318
319 impl EventHandler for Recorder {
320 fn on_power_changed(&self, _bay: BayUid, power: PowerStatus) {
321 self.record(&format!("power={power}"));
322 }
323
324 fn on_setup_status_changed(&self, _device: DeviceUid, completed: bool) {
325 self.record(&format!("setup={completed}"));
326 }
327
328 fn on_bay_update(&self, _bay: BayUid) {
329 self.record("bay_update");
330 }
331
332 fn on_device_update(&self, _device: DeviceUid) {
333 self.record("device_update");
334 }
335 }
336
337 const DEVICE: DeviceUid = DeviceUid::from_array([9; 16]);
338
339 #[test]
340 fn a_bay_event_fires_its_own_method_then_the_generic_bay_update() {
341 let recorder = Recorder::default();
342 Event::PowerChanged {
343 bay: BayUid::new(DEVICE, 3),
344 power: PowerStatus::On,
345 }
346 .dispatch(&recorder);
347 assert_eq!(recorder.calls(), ["power=on", "bay_update"]);
348 }
349
350 #[test]
351 fn a_device_event_fires_its_own_method_then_the_generic_device_update() {
352 let recorder = Recorder::default();
353 Event::SetupStatusChanged {
354 device: DEVICE,
355 completed: true,
356 }
357 .dispatch(&recorder);
358 assert_eq!(recorder.calls(), ["setup=true", "device_update"]);
359 }
360
361 #[test]
362 fn a_link_event_fires_both_generic_updates() {
363 let recorder = Recorder::default();
364 Event::BayUnlinked {
365 bay: BayUid::new(DEVICE, 3),
366 linked_serial: "AB1234".to_owned(),
367 bay_name: "Output 1".to_owned(),
368 }
369 .dispatch(&recorder);
370 assert_eq!(recorder.calls(), ["bay_update", "device_update"]);
371 }
372
373 #[test]
374 fn an_event_a_handler_does_not_name_still_fires_the_generic_update() {
375 let recorder = Recorder::default();
376 Event::MonitoringPulse { device: DEVICE }.dispatch(&recorder);
377 assert_eq!(
378 recorder.calls(),
379 ["device_update"],
380 "the default no-op must not swallow the fan-in"
381 );
382 }
383}