rodecaster-protocol 0.15.0

JUCE var / ValueTree codec and transport frame for the RODECaster Pro II / Duo wire protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
//! Inbound typed event notifications from a RØDECaster console.
//!
//! [`DeviceEvent`] represents notifications emitted by the device in response
//! to hardware changes or user actions:
//!
//! - Physical or virtual fader movements.
//! - Mute and cue/solo button toggles.
//! - Rotary encoder adjustments and color updates.
//! - Input source assignment changes.
//! - Audio processing adjustments (EQ, dynamics, HPF, pan).
//! - Sub-mix changes and routing matrix links.
//!
//! # Handling Events
//!
//! When using [`crate::ProtocolSession`], events are yielded by
//! [`crate::ProtocolSession::ingest`]:
//!
//! ```rust
//! use rodecaster_protocol::{DeviceEvent, Fader};
//!
//! fn handle_event(event: DeviceEvent) {
//!     match event {
//!         DeviceEvent::FaderLevelChanged { fader, level } => {
//!             println!("{fader:?} level is now {level}");
//!         }
//!         DeviceEvent::FaderMuteChanged { fader, muted } => {
//!             println!("{fader:?} muted: {muted}");
//!         }
//!         _ => {}
//!     }
//! }
//! ```

use crate::change_frame::{decode as decode_frame, ChangeFrame};
use crate::juce_var::Value;
use crate::layout::Layout;
use crate::names::{
    AppParam, AudioParam, BuildParam, ChannelParam, CurrentShowParam, DeviceModel, DuckerParam,
    EffectsParam, Fader, FxPresetParam, GuiParam, HeadphoneParam, InputSourceParam, MasterParam,
    MeterParam, MixMinusesParam, MixOutput, NetworkParam, OutputParam, PadParam, PadRecorderParam,
    PlayerParam, RadioParam, RadioRxParam, RadioTxParam, RcSyncMixParam, RecorderParam,
    RecordingParam, RecordingsParam, ShowControlParam, ShowParam, SipAdvancedParam,
    SipCallSlotsParam, SipCallingParam, SipRegistrationParam, Source, StorageVolumeParam,
    StreamerXMixPresetParam, StreamerXStreamMixParam, SystemParam, TestParam, ThemeParam,
    WifiScanResultParam,
};
use crate::trigger::decode_phase;
pub use crate::trigger::TriggerPhase;
use crate::valuetree::Node;

/// A typed event emitted by a connected RØDECaster console.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum DeviceEvent {
    /// Full device state received during initialization or sync.
    InitialState(Vec<DeviceEvent>),

    /// Fader mute state changed.
    FaderMuteChanged { fader: Fader, muted: bool },
    /// Fader cue/solo state changed.
    FaderCueChanged { fader: Fader, enabled: bool },
    /// Fader level changed (0..127 MIDI scale).
    FaderLevelChanged { fader: Fader, level: u8 },
    /// A fader strip was touched.
    FaderTouched { fader: Fader },
    /// Rotary encoder LED-ring colour palette index changed (`None` when cleared).
    FaderEncoderColourChanged { fader: Fader, colour: Option<i32> },
    /// Mix link or unlink request or acknowledgment trace.
    MixLinkRequested {
        source: Source,
        mix: MixOutput,
        direction: MixLinkDirection,
        origin: MixLinkRequestOrigin,
    },
    /// A fader's input-source assignment changed (`None` when unassigned).
    FaderAssignmentChanged {
        fader: Fader,
        source: Option<Source>,
    },

    /// A channel-strip parameter changed (EQ, dynamics, HPF, pan, tone, preamp).
    ChannelParamChanged {
        fader: Fader,
        param: ChannelParam,
        value: Value,
    },

    /// An input-source parameter changed (preamp gain, phantom power, mic type).
    InputSourceParamChanged {
        source: Source,
        param: InputSourceParam,
        value: Value,
    },

    /// A master-bus parameter changed (Compellor compressor, master delay).
    MasterParamChanged { param: MasterParam, value: Value },

    /// An output-bus parameter changed (monitor/Bluetooth level, mute, multi-out).
    OutputParamChanged { param: OutputParam, value: Value },

    /// Auto-ducking parameters changed.
    DuckerParamChanged { param: DuckerParam, value: Value },

    /// Recorder transport state or property changed.
    RecorderParamChanged { param: RecorderParam, value: Value },

    /// Sound player property changed (playback state, file, fade envelope).
    PlayerParamChanged { param: PlayerParam, value: Value },

    /// Per-headphone parameter changed (colour, output type).
    HeadphoneParamChanged {
        headphone: u8,
        param: HeadphoneParam,
        value: Value,
    },

    /// Per-slot channel effects parameter changed (reverb, delay, pitch, distortion).
    EffectsParamChanged {
        effects: u8,
        param: EffectsParam,
        value: Value,
    },

    /// Front-panel UI parameter changed (display/button brightness, pad bank).
    GuiParamChanged { param: GuiParam, value: Value },

    /// Sound-pad parameter changed (colour, sample, playback mode, routing).
    PadParamChanged {
        pad: u8,
        param: PadParam,
        value: Value,
    },

    /// Device-wide system parameter changed (power, clock, USB/storage status).
    SystemParamChanged { param: SystemParam, value: Value },

    /// Routing matrix level changed (`anchor` is matrix level, `value` is live fader-tracked level).
    MixLevelChanged {
        source: Source,
        mix: MixOutput,
        anchor: f32,
        value: f32,
    },
    /// Routing matrix mute state changed.
    MixMuteChanged {
        source: Source,
        mix: MixOutput,
        muted: bool,
    },
    /// Routing matrix link state changed.
    MixLinkChanged {
        source: Source,
        mix: MixOutput,
        linked: bool,
    },
    /// Routing matrix route disabled state changed.
    MixDisabledChanged {
        source: Source,
        mix: MixOutput,
        disabled: bool,
    },

    /// Device-wide networking parameter changed (WiFi, Bluetooth, IP, DNS).
    NetworkParamChanged { param: NetworkParam, value: Value },

    /// Recordings summary parameter changed (total count, total duration).
    RecordingsParamChanged {
        param: RecordingsParam,
        value: Value,
    },

    /// Specific recording metadata changed.
    RecordingParamChanged {
        recording: u8,
        param: RecordingParam,
        value: Value,
    },

    /// Storage volume parameter changed (total bytes, used bytes).
    StorageVolumeParamChanged {
        volume: u8,
        param: StorageVolumeParam,
        value: Value,
    },

    /// Rotary encoder or potentiometer position changed (0..=127).
    PotLevelChanged { pot: u8, level: u8 },

    /// Front-panel pad button press or release.
    PadButtonPressed { button: u8, pressed: bool },

    /// Front-panel mute button press or release.
    MutePressed { button: u8, pressed: bool },

    /// Device-wide audio-engine parameter changed (sample rate, buffer size, latencies).
    AudioParamChanged { param: AudioParam, value: Value },

    /// Firmware build metadata changed.
    BuildParamChanged { param: BuildParam, value: Value },

    /// Companion-app state flag changed.
    AppParamChanged { param: AppParam, value: Value },

    /// UI theme setting changed.
    ThemeParamChanged { param: ThemeParam, value: Value },

    /// Currently-loaded show identifier changed.
    CurrentShowParamChanged {
        param: CurrentShowParam,
        value: Value,
    },

    /// Show metadata changed.
    ShowParamChanged {
        show: u8,
        param: ShowParam,
        value: Value,
    },

    /// Show control or lifecycle action changed.
    ShowControlParamChanged {
        param: ShowControlParam,
        value: Value,
    },

    /// Live level meter reading changed.
    MeterParamChanged {
        meter: u8,
        param: MeterParam,
        value: Value,
    },

    /// Rotary encoder push-button press or release.
    EncoderPressed { encoder: u8, pressed: bool },

    /// Front-panel solo/listen button press or release.
    SoloPressed { button: u8, pressed: bool },

    /// Front-panel record button press or release.
    RecButtonPressed { pressed: bool },

    /// Emergency mute state changed.
    EmergencyMuteChanged { active: bool },

    /// SIP calling state parameter changed.
    SipCallingParamChanged {
        param: SipCallingParam,
        value: Value,
    },

    /// SIP account registration parameter changed.
    SipRegistrationParamChanged {
        registration: u8,
        param: SipRegistrationParam,
        value: Value,
    },

    /// SIP call slot parameter changed.
    SipCallSlotsParamChanged {
        slot: u8,
        param: SipCallSlotsParam,
        value: Value,
    },

    /// SIP advanced setting changed.
    SipAdvancedParamChanged {
        param: SipAdvancedParam,
        value: Value,
    },

    /// Streamer X mix preset parameter changed.
    StreamerXMixPresetParamChanged {
        preset: u8,
        param: StreamerXMixPresetParam,
        value: Value,
    },

    /// Streamer X stream mix parameter changed.
    StreamerXStreamMixParamChanged {
        stream: u8,
        param: StreamerXStreamMixParam,
        value: Value,
    },

    /// Effects preset parameter changed.
    FxPresetParamChanged {
        preset: u8,
        param: FxPresetParam,
        value: Value,
    },

    /// Pad recorder parameter changed.
    PadRecorderParamChanged {
        pad_recorder: u8,
        param: PadRecorderParam,
        value: Value,
    },

    /// Diagnostic or factory-test parameter changed.
    TestParamChanged { param: TestParam, value: Value },

    /// WiFi scan result entry changed.
    WifiScanResultChanged {
        slot: u8,
        param: WifiScanResultParam,
        value: Value,
    },

    /// Wireless receiver lifecycle parameter changed.
    RadioParamChanged { param: RadioParam, value: Value },

    /// Wireless transmitter parameter changed.
    RadioTxParamChanged {
        tx: u8,
        param: RadioTxParam,
        value: Value,
    },

    /// Wireless receiver parameter changed.
    RadioRxParamChanged {
        rx: u8,
        param: RadioRxParam,
        value: Value,
    },

    /// Mix-minus routing parameter changed.
    MixMinusesParamChanged {
        minuses: u8,
        param: MixMinusesParam,
        value: Value,
    },

    /// rcSync mix routing parameter changed.
    RcSyncMixParamChanged {
        mix: u8,
        param: RcSyncMixParam,
        value: Value,
    },

    /// Tree topology changed (child added, removed, or moved); layout should be rebuilt.
    LayoutInvalidated,

    /// Unrecognized property name or path under the current layout.
    Unknown {
        prop_name: String,
        path: Vec<u32>,
        value: Option<Value>,
    },
}

/// Direction of a mix-cell link request echo.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MixLinkDirection {
    /// Link action (`mixLinkRequest`).
    Link,
    /// Unlink action (`mixUnlinkRequest`).
    Unlink,
}

/// Origin of a mix-cell link request observation (alias for [`TriggerPhase`]).
pub type MixLinkRequestOrigin = TriggerPhase;

/// Decode one wire payload (the change-frame, not including transport frame)
/// into a typed event. Returns `None` if the payload isn't a recognized JUCE
/// change-frame at all.
pub fn decode_event(payload: &[u8], layout: &Layout) -> Option<DeviceEvent> {
    let frame = decode_frame(payload)?;
    Some(decode_change_frame(frame, layout))
}

/// Decode an already-parsed [`ChangeFrame`] through the discovered [`Layout`]
/// into a typed [`DeviceEvent`].
pub fn decode_event_from_frame(frame: ChangeFrame, layout: &Layout) -> DeviceEvent {
    decode_change_frame(frame, layout)
}

fn decode_change_frame(frame: ChangeFrame, layout: &Layout) -> DeviceEvent {
    match frame {
        ChangeFrame::FullSync { root } => {
            DeviceEvent::InitialState(extract_initial_state(&root, layout))
        }
        ChangeFrame::ChildAdded { .. }
        | ChangeFrame::ChildRemoved { .. }
        | ChangeFrame::ChildMoved { .. } => DeviceEvent::LayoutInvalidated,
        ChangeFrame::PropertyChanged { path, name, value } => {
            decode_property(&path, &name, Some(value), layout)
        }
        ChangeFrame::PropertyRemoved { path, name } => decode_property(&path, &name, None, layout),
    }
}

fn decode_property(path: &[u32], name: &str, value: Option<Value>, layout: &Layout) -> DeviceEvent {
    let model = layout.model();

    // CHANNEL-addressed properties (single-level path). A path that resolves to
    // a channel with no named fader (the master strip, index 9) falls through
    // to Unknown rather than matching here.
    if let Some(fader) = layout
        .channel_index_from_path(path)
        .and_then(|idx| Fader::from_index(model, idx))
    {
        match name {
            "channelOutputMute" => {
                if let Some(Value::Bool(muted)) = value {
                    return DeviceEvent::FaderMuteChanged { fader, muted };
                }
            }
            "channelCueEnable" => {
                if let Some(Value::Bool(enabled)) = value {
                    return DeviceEvent::FaderCueChanged { fader, enabled };
                }
            }
            "channelInputSource" => {
                let source = value
                    .as_ref()
                    .and_then(Value::as_int)
                    .filter(|&s| s >= 0)
                    .and_then(|s| u8::try_from(s).ok())
                    .and_then(Source::from_protocol);
                return DeviceEvent::FaderAssignmentChanged { fader, source };
            }
            _ => {}
        }
    }

    // FADER-addressed properties (two-level path through PHYSICALINTERFACE).
    if let Some(fader) = layout
        .fader_index_from_path(path)
        .and_then(|idx| Fader::from_index(model, idx))
    {
        if name == "faderLevel" {
            if let Some(v) = &value {
                if let Some(level_i64) = v.as_int() {
                    let level = level_i64.clamp(0, 127) as u8;
                    return DeviceEvent::FaderLevelChanged { fader, level };
                }
            }
        }
    }

    // MIX-cell-addressed properties (single-level path).
    if let Some((source, mix)) = layout
        .mix_cell_from_path(path)
        .and_then(|(s, m)| Some((Source::from_protocol(s)?, MixOutput::from_protocol(m)?)))
    {
        match name {
            "mixMute" => {
                if let Some(Value::Bool(muted)) = value {
                    return DeviceEvent::MixMuteChanged { source, mix, muted };
                }
            }
            "mixLink" => {
                if let Some(Value::Bool(linked)) = value {
                    return DeviceEvent::MixLinkChanged {
                        source,
                        mix,
                        linked,
                    };
                }
            }
            "mixDisabled" => {
                if let Some(Value::Bool(disabled)) = value {
                    return DeviceEvent::MixDisabledChanged {
                        source,
                        mix,
                        disabled,
                    };
                }
            }
            "mixLevelWithAnchor" => {
                if let Some(Value::String(s)) = &value {
                    if let Some((anchor, value)) = parse_mix_level(s) {
                        return DeviceEvent::MixLevelChanged {
                            source,
                            mix,
                            anchor,
                            value,
                        };
                    }
                }
            }
            // mixLinkRequest / mixUnlinkRequest at a cell's single-level path.
            // Direction = property name; origin = momentary trigger state.
            "mixLinkRequest" | "mixUnlinkRequest" => {
                if let Some(origin) = value.as_ref().and_then(decode_phase) {
                    let direction = if name == "mixLinkRequest" {
                        MixLinkDirection::Link
                    } else {
                        MixLinkDirection::Unlink
                    };
                    return DeviceEvent::MixLinkRequested {
                        source,
                        mix,
                        direction,
                        origin,
                    };
                }
            }
            _ => {}
        }
    }

    // encoderSignal (fader touch): single-level path whose value IS the fader
    // index (stride 1, no base offset). See module docs.
    if name == "encoderSignal" {
        if let Some(&raw) = path.first() {
            if raw < layout.fader_count() as u32 {
                if let Some(fader) = Fader::from_index(model, raw as u8) {
                    return DeviceEvent::FaderTouched { fader };
                }
            }
        }
    }

    // encoderColour: LED-ring colour of a fader strip's rotary encoder. Same
    // single-level addressing as encoderSignal; the value is an Int where -1
    // means "cleared / no colour" and >= 0 is the palette index. A missing
    // or non-Int value falls through to Unknown so we don't conflate the two.
    if name == "encoderColour" {
        if let Some(&raw) = path.first() {
            if raw < layout.fader_count() as u32 {
                if let Some(fader) = Fader::from_index(model, raw as u8) {
                    if let Some(i) = value.as_ref().and_then(Value::as_int) {
                        let colour = if i < 0 { None } else { Some(i as i32) };
                        return DeviceEvent::FaderEncoderColourChanged { fader, colour };
                    }
                }
            }
        }
    }

    // channelInputSource echo: resolves via stride-1 channel_index_from_path first,
    // falling back to stride-6 echo addressing when node is offset-addressed.
    if name == "channelInputSource" {
        let fader_opt = layout
            .channel_index_from_path(path)
            .and_then(|idx| Fader::from_index(model, idx))
            .or_else(|| {
                path.first()
                    .and_then(|raw| raw.checked_sub(layout.first_channel()))
                    .filter(|&offset| offset % 6 == 0)
                    .map(|offset| offset / 6)
                    .filter(|&idx| idx < layout.channel_count() as u32)
                    .and_then(|idx| Fader::from_index(model, idx as u8))
            });

        if let Some(fader) = fader_opt {
            let source = value
                .as_ref()
                .and_then(Value::as_int)
                .filter(|&s| s >= 0)
                .and_then(|s| u8::try_from(s).ok())
                .and_then(Source::from_protocol);
            return DeviceEvent::FaderAssignmentChanged { fader, source };
        }
    }

    // PHYSICALINTERFACE hardware-child input events. Path shape is
    // `[physical_interface_idx, child_index]`. Each property name here is
    // unique to its child node type, so we key off the name and hand the
    // caller the raw child index (per-model mapping is the caller's job).
    if path.len() == 2 && path[0] == layout.physical_interface_idx() {
        let child = path[1] as u8;
        match name {
            "potLevel" => {
                if let Some(Value::Int(level)) = value.as_ref() {
                    return DeviceEvent::PotLevelChanged {
                        pot: child,
                        level: (*level).clamp(0, 127) as u8,
                    };
                }
            }
            "padButtonPressed" => {
                if let Some(Value::Bool(pressed)) = value.as_ref() {
                    return DeviceEvent::PadButtonPressed {
                        button: child,
                        pressed: *pressed,
                    };
                }
            }
            "mutePressed" => {
                if let Some(Value::Bool(pressed)) = value.as_ref() {
                    return DeviceEvent::MutePressed {
                        button: child,
                        pressed: *pressed,
                    };
                }
            }
            "soloPressed" => {
                if let Some(Value::Bool(pressed)) = value.as_ref() {
                    return DeviceEvent::SoloPressed {
                        button: child,
                        pressed: *pressed,
                    };
                }
            }
            "encoderPressed" => {
                if let Some(Value::Bool(pressed)) = value.as_ref() {
                    return DeviceEvent::EncoderPressed {
                        encoder: child,
                        pressed: *pressed,
                    };
                }
            }
            "recButtonPressed" => {
                if let Some(Value::Bool(pressed)) = value.as_ref() {
                    return DeviceEvent::RecButtonPressed { pressed: *pressed };
                }
            }
            _ => {}
        }
    }

    // EMERGENCYMUTE singleton at root (path.len() == 1).
    if path.len() == 1 && name == "emergencyMuteActive" {
        if let Some(Value::Bool(active)) = value.as_ref() {
            return DeviceEvent::EmergencyMuteChanged { active: *active };
        }
    }

    // NETWORK singleton: property names are all `bt*`, `wifi*`, `cell*`, etc.
    // The wire-name set is unique to the NETWORK node, so we match on name
    // without checking path (any structurally-valid propertyChanged carrying
    // one of these names lands here).
    if let Some(param) = NetworkParam::from_known_name(name) {
        if let Some(value) = value {
            return DeviceEvent::NetworkParamChanged { param, value };
        }
    }

    // RECORDINGS singleton (path.len() == 1).
    if path.len() == 1 {
        if let Some(param) = RecordingsParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::RecordingsParamChanged { param, value };
            }
        }
    }

    // RECORDING children (path.len() == 2). path[1] is the child index within
    // the RECORDINGS container.
    if path.len() == 2 {
        if let Some(param) = RecordingParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::RecordingParamChanged {
                    recording: path[1] as u8,
                    param,
                    value,
                };
            }
        }

        // STORAGEVOLUME children (path.len() == 2). path[1] is the volume index.
        if let Some(param) = StorageVolumeParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::StorageVolumeParamChanged {
                    volume: path[1] as u8,
                    param,
                    value,
                };
            }
        }
    }

    // Path-length-1 singletons: property names are unique to each family.
    if path.len() == 1 {
        if let Some(param) = AudioParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::AudioParamChanged { param, value };
            }
        }
        if let Some(param) = BuildParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::BuildParamChanged { param, value };
            }
        }
        if let Some(param) = AppParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::AppParamChanged { param, value };
            }
        }
        if let Some(param) = ThemeParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::ThemeParamChanged { param, value };
            }
        }
        if let Some(param) = CurrentShowParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::CurrentShowParamChanged { param, value };
            }
        }
        if let Some(param) = ShowControlParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::ShowControlParamChanged { param, value };
            }
        }
    }

    // Path-length-2 children under SHOWS and METER containers.
    if path.len() == 2 {
        if let Some(param) = ShowParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::ShowParamChanged {
                    show: path[1] as u8,
                    param,
                    value,
                };
            }
        }

        if path[0] != layout.physical_interface_idx() {
            if let Some(param) = MeterParam::from_known_name(name) {
                if let Some(value) = value {
                    return DeviceEvent::MeterParamChanged {
                        meter: path[1] as u8,
                        param,
                        value,
                    };
                }
            }
        }
    }

    // SIPCALLING singleton (path.len() == 1, layout-resolved position).
    if layout.is_sip_calling_path(path) {
        if let Some(param) = SipCallingParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::SipCallingParamChanged { param, value };
            }
        }
    }

    // SIPADVANCED singleton.
    if layout.is_sip_advanced_path(path) {
        if let Some(param) = SipAdvancedParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::SipAdvancedParamChanged { param, value };
            }
        }
    }

    // SIPREGISTRATION per-instance under SIPCALLING.
    if let Some(reg) = layout.sip_registration_index_from_path(path) {
        if let Some(param) = SipRegistrationParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::SipRegistrationParamChanged {
                    registration: reg,
                    param,
                    value,
                };
            }
        }
    }

    // SIPCALLSLOTS per-slot at root.
    if let Some(slot) = layout.sip_call_slots_index_from_path(path) {
        if let Some(param) = SipCallSlotsParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::SipCallSlotsParamChanged { slot, param, value };
            }
        }
    }

    // Small-family decodes for property names that don't conflict with any
    // typed family above. Each name is unique to its family, so we key by
    // name; per-instance families take path.last() as the ordinal (no
    // layout resolution: the path itself carries enough context).

    if let Some(param) = StreamerXMixPresetParam::from_known_name(name) {
        if let Some(value) = value {
            let preset = path.last().copied().unwrap_or(0) as u8;
            return DeviceEvent::StreamerXMixPresetParamChanged {
                preset,
                param,
                value,
            };
        }
    }
    if let Some(param) = StreamerXStreamMixParam::from_known_name(name) {
        if let Some(value) = value {
            let stream = path.last().copied().unwrap_or(0) as u8;
            return DeviceEvent::StreamerXStreamMixParamChanged {
                stream,
                param,
                value,
            };
        }
    }
    if let Some(param) = FxPresetParam::from_known_name(name) {
        if let Some(value) = value {
            let preset = path.last().copied().unwrap_or(0) as u8;
            return DeviceEvent::FxPresetParamChanged {
                preset,
                param,
                value,
            };
        }
    }
    if let Some(param) = PadRecorderParam::from_known_name(name) {
        if let Some(value) = value {
            let pad_recorder = path.last().copied().unwrap_or(0) as u8;
            return DeviceEvent::PadRecorderParamChanged {
                pad_recorder,
                param,
                value,
            };
        }
    }
    if let Some(param) = TestParam::from_known_name(name) {
        if let Some(value) = value {
            return DeviceEvent::TestParamChanged { param, value };
        }
    }
    if let Some(param) = WifiScanResultParam::from_known_name(name) {
        if let Some(value) = value {
            // Path shape is [network_root_idx, scan_slot]; slot is path.last().
            let slot = path.last().copied().unwrap_or(0) as u8;
            return DeviceEvent::WifiScanResultChanged { slot, param, value };
        }
    }
    if let Some(param) = RadioParam::from_known_name(name) {
        if let Some(value) = value {
            return DeviceEvent::RadioParamChanged { param, value };
        }
    }
    if let Some(param) = RadioTxParam::from_known_name(name) {
        if let Some(value) = value {
            let tx = path.last().copied().unwrap_or(0) as u8;
            return DeviceEvent::RadioTxParamChanged { tx, param, value };
        }
    }
    if let Some(param) = RadioRxParam::from_known_name(name) {
        if let Some(value) = value {
            let rx = path.last().copied().unwrap_or(0) as u8;
            return DeviceEvent::RadioRxParamChanged { rx, param, value };
        }
    }
    if let Some(minuses) = layout.mix_minuses_index_from_path(path) {
        if let Some(param) = MixMinusesParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::MixMinusesParamChanged {
                    minuses,
                    param,
                    value,
                };
            }
        }
    }
    // RcSyncMixParam shares six wire names with the regular MIX cell family
    // (mixDisabled / mixLevelWithAnchor / mixLink / mixLinkRequest / mixMute /
    // mixUnlinkRequest). The MIX matrix decode above matches paths inside the
    // discovered `first_mix + source*13 + mix` run; anything outside that run
    // that still carries these names lands here. The seventh property
    // (`mixRcSyncLevelRequest`) is unique to RCSYNCMIX.
    if let Some(mix) = layout.rcsync_mix_index_from_path(path) {
        if let Some(param) = RcSyncMixParam::from_known_name(name) {
            if let Some(value) = value {
                return DeviceEvent::RcSyncMixParamChanged { mix, param, value };
            }
        }
    }

    // Typed param-family promotion: any *other* property on an addressable node
    // becomes the matching typed `*ParamChanged` event, carrying the
    // self-describing wire value. `resolve_param_target` decides which family the
    // path lands on (CHANNEL strip, INPUTSOURCE, the MASTERCHANNEL / OUTPUT /
    // DUCKER / RECORDER / PLAYER singletons, or a HEADPHONE jack). Those node
    // index ranges are disjoint, so the resolver order is for clarity, not
    // precedence. `value` is moved exactly once: into the matched event, else
    // into Unknown (which also catches `propertyRemoved`, where value is None).
    match (resolve_param_target(path, name, layout, model), value) {
        (Some(ParamTarget::Channel(fader)), Some(value)) => DeviceEvent::ChannelParamChanged {
            fader,
            param: ChannelParam::from_name(name),
            value,
        },
        (Some(ParamTarget::InputSource(source)), Some(value)) => {
            DeviceEvent::InputSourceParamChanged {
                source,
                param: InputSourceParam::from_name(name),
                value,
            }
        }
        (Some(ParamTarget::Master), Some(value)) => DeviceEvent::MasterParamChanged {
            param: MasterParam::from_name(name),
            value,
        },
        (Some(ParamTarget::Output), Some(value)) => DeviceEvent::OutputParamChanged {
            param: OutputParam::from_name(name),
            value,
        },
        (Some(ParamTarget::Ducker), Some(value)) => DeviceEvent::DuckerParamChanged {
            param: DuckerParam::from_name(name),
            value,
        },
        (Some(ParamTarget::Recorder), Some(value)) => DeviceEvent::RecorderParamChanged {
            param: RecorderParam::from_name(name),
            value,
        },
        (Some(ParamTarget::Player), Some(value)) => DeviceEvent::PlayerParamChanged {
            param: PlayerParam::from_name(name),
            value,
        },
        (Some(ParamTarget::Headphone(headphone)), Some(value)) => {
            DeviceEvent::HeadphoneParamChanged {
                headphone,
                param: HeadphoneParam::from_name(name),
                value,
            }
        }
        (Some(ParamTarget::Effects(effects)), Some(value)) => DeviceEvent::EffectsParamChanged {
            effects,
            param: EffectsParam::from_name(name),
            value,
        },
        (Some(ParamTarget::Gui), Some(value)) => DeviceEvent::GuiParamChanged {
            param: GuiParam::from_name(name),
            value,
        },
        (Some(ParamTarget::Pad(pad)), Some(value)) => DeviceEvent::PadParamChanged {
            pad,
            param: PadParam::from_name(name),
            value,
        },
        (Some(ParamTarget::System), Some(value)) => DeviceEvent::SystemParamChanged {
            param: SystemParam::from_name(name),
            value,
        },
        (_, value) => DeviceEvent::Unknown {
            prop_name: name.to_string(),
            path: path.to_vec(),
            value,
        },
    }
}

/// Which addressable node family a property path lands on. Resolved by
/// [`resolve_param_target`] and consumed by [`decode_property`] to promote a
/// property to the matching typed `*ParamChanged` event.
enum ParamTarget {
    Channel(Fader),
    InputSource(Source),
    Master,
    Output,
    Ducker,
    Recorder,
    Player,
    Headphone(u8),
    Effects(u8),
    Gui,
    Pad(u8),
    System,
}

/// Resolve a property path to its addressable node family, if any. The node
/// index ranges are disjoint, so the first match wins and the order is for
/// readability only. Returns `None` for paths that don't land on a typed family
/// (the caller then emits [`DeviceEvent::Unknown`]).
fn resolve_param_target(
    path: &[u32],
    name: &str,
    layout: &Layout,
    model: DeviceModel,
) -> Option<ParamTarget> {
    // CHANNEL strip (the whole EQ / dynamics / HPF / aphex / pan / tone / preamp
    // strip). The three specialized props that own dedicated variants are
    // excluded so a malformed one of those falls to Unknown rather than
    // masquerading as a generic strip param.
    if !matches!(
        name,
        "channelOutputMute" | "channelCueEnable" | "channelInputSource"
    ) {
        if let Some(fader) = layout
            .channel_index_from_path(path)
            .and_then(|idx| Fader::from_index(model, idx))
        {
            return Some(ParamTarget::Channel(fader));
        }
    }
    // INPUTSOURCE: the input* preamp/source params, on a node separate from
    // CHANNEL. An ordinal past the named vocabulary (a rcSync / streamer-x
    // placeholder source) doesn't map and falls through to Unknown.
    if let Some(source) = layout
        .input_source_index_from_path(path)
        .and_then(Source::from_protocol)
    {
        return Some(ParamTarget::InputSource(source));
    }
    // Singletons: exactly one node each, key-less.
    if layout.is_master_channel_path(path) {
        return Some(ParamTarget::Master);
    }
    if layout.is_output_path(path) {
        return Some(ParamTarget::Output);
    }
    if layout.is_ducker_path(path) {
        return Some(ParamTarget::Ducker);
    }
    if layout.is_recorder_path(path) {
        return Some(ParamTarget::Recorder);
    }
    if layout.is_player_path(path) {
        return Some(ParamTarget::Player);
    }
    // HEADPHONE: multi-instance, one node per physical jack.
    if let Some(headphone) = layout.headphone_index_from_path(path) {
        return Some(ParamTarget::Headphone(headphone));
    }
    // EFFECTS_PARAMETERS: multi-instance, one node per channel-strip effects slot.
    if let Some(effects) = layout.effects_index_from_path(path) {
        return Some(ParamTarget::Effects(effects));
    }
    // GUI: the single front-panel UI-state node.
    if layout.is_gui_path(path) {
        return Some(ParamTarget::Gui);
    }
    // PAD: multi-instance, one node per sound pad inside SOUNDPADS (two-level).
    if let Some(pad) = layout.pad_index_from_path(path) {
        return Some(ParamTarget::Pad(pad));
    }
    // SYSTEM: the single device-wide state node.
    if layout.is_system_path(path) {
        return Some(ParamTarget::System);
    }
    None
}

/// `mixLevelWithAnchor` wire form: `anchor|value` (or a single number for
/// both). Returns `(anchor, value)`: the left/configured matrix level and the
/// right/fader-tracked level.
fn parse_mix_level(s: &str) -> Option<(f32, f32)> {
    let anchor = s.split('|').next()?.parse().ok()?;
    let value = s.split('|').next_back()?.parse().ok()?;
    Some((anchor, value))
}

/// Walk a parsed fullSync tree and extract the initial device state as a list of [`DeviceEvent`]s.
pub fn extract_initial_state(root: &Node, layout: &Layout) -> Vec<DeviceEvent> {
    let mut out = Vec::new();
    let model = layout.model();

    // 1. PHYSICALINTERFACE -> FADER initial levels (physical strips only).
    if let Some(phys) = root.children.iter().find(|n| n.name == "PHYSICALINTERFACE") {
        let mut fader_idx: u8 = 0;
        for child in &phys.children {
            if child.name != "FADER" {
                continue;
            }
            if let (Some(fader), Some(level)) = (
                Fader::from_index(model, fader_idx),
                int_prop(child, "faderLevel"),
            ) {
                out.push(DeviceEvent::FaderLevelChanged {
                    fader,
                    level: level.clamp(0, 127) as u8,
                });
            }
            fader_idx = fader_idx.saturating_add(1);
        }
    }

    // 2. CHANNEL initial mute/cue (one per strip, including virtuals). The
    // master strip (index 9) has no named fader, so its properties are skipped.
    let mut channel_idx: u8 = 0;
    for child in &root.children {
        if child.name != "CHANNEL" {
            continue;
        }
        if let Some(fader) = Fader::from_index(model, channel_idx) {
            if let Some(muted) = bool_prop(child, "channelOutputMute") {
                out.push(DeviceEvent::FaderMuteChanged { fader, muted });
            }
            if let Some(enabled) = bool_prop(child, "channelCueEnable") {
                out.push(DeviceEvent::FaderCueChanged { fader, enabled });
            }
            // In a fullSync the assignment sits on the Nth CHANNEL positionally
            // (stride 1), unlike the stride-6 incremental echo.
            if let Some(source_i) = int_prop(child, "channelInputSource") {
                out.push(DeviceEvent::FaderAssignmentChanged {
                    fader,
                    source: if source_i < 0 {
                        None
                    } else {
                        u8::try_from(source_i).ok().and_then(Source::from_protocol)
                    },
                });
            }
            // Everything else on the CHANNEL node is the channel strip (EQ,
            // dynamics, HPF, aphex, pan, tone, preamp). Emit each as a typed,
            // fader-resolved param so the initial state carries the full strip,
            // not just mute/cue/source. Skips the three handled above.
            for p in &child.properties {
                if matches!(
                    p.name.as_str(),
                    "channelOutputMute" | "channelCueEnable" | "channelInputSource"
                ) {
                    continue;
                }
                out.push(DeviceEvent::ChannelParamChanged {
                    fader,
                    param: ChannelParam::from_name(&p.name),
                    value: p.value.clone(),
                });
            }
        }
        channel_idx = channel_idx.saturating_add(1);
    }

    // 3. MIX cells initial values. Source-major: cell N has source = N/13, mix
    // = N%13. Cells whose source ordinal falls past the named vocabulary (e.g.
    // a Duo's rcSync RCV placeholder block) don't map and are skipped; the
    // counter still advances so later cells keep their source-major position.
    let mut mix_counter: u32 = 0;
    let per_source = layout.mix_count_per_source() as u32;
    for child in &root.children {
        if child.name != "MIX" {
            continue;
        }
        let source_idx = (mix_counter / per_source) as u8;
        let mix_idx = (mix_counter % per_source) as u8;
        mix_counter += 1;
        let (source, mix) = match (
            Source::from_protocol(source_idx),
            MixOutput::from_protocol(mix_idx),
        ) {
            (Some(s), Some(m)) => (s, m),
            _ => continue,
        };
        if let Some(level_s) = string_prop(child, "mixLevelWithAnchor") {
            if let Some((anchor, value)) = parse_mix_level(level_s) {
                out.push(DeviceEvent::MixLevelChanged {
                    source,
                    mix,
                    anchor,
                    value,
                });
            }
        }
        if let Some(muted) = bool_prop(child, "mixMute") {
            out.push(DeviceEvent::MixMuteChanged { source, mix, muted });
        }
        if let Some(linked) = bool_prop(child, "mixLink") {
            out.push(DeviceEvent::MixLinkChanged {
                source,
                mix,
                linked,
            });
        }
        if let Some(disabled) = bool_prop(child, "mixDisabled") {
            out.push(DeviceEvent::MixDisabledChanged {
                source,
                mix,
                disabled,
            });
        }
    }

    // 4. INPUTSOURCE initial preamp/source params (one set per addressable
    // source). The counter runs over every INPUTSOURCE node in tree order; the
    // ordinal == the device `inputId` == `Source::to_protocol`. Ordinals past
    // the named vocabulary (a Duo's rcSync / streamer-x placeholder block) don't
    // map and are skipped, the counter still advancing so later sources keep
    // their position.
    let mut input_source_idx: u32 = 0;
    for child in &root.children {
        if child.name != "INPUTSOURCE" {
            continue;
        }
        let ordinal = input_source_idx;
        input_source_idx += 1;
        let source = match u8::try_from(ordinal).ok().and_then(Source::from_protocol) {
            Some(s) => s,
            None => continue,
        };
        for p in &child.properties {
            out.push(DeviceEvent::InputSourceParamChanged {
                source,
                param: InputSourceParam::from_name(&p.name),
                value: p.value.clone(),
            });
        }
    }

    // 5. MASTERCHANNEL and OUTPUT initial params (singletons). Emit every
    // property the device carries on each node as a typed, key-less event.
    if let Some(node) = root.children.iter().find(|c| c.name == "MASTERCHANNEL") {
        for p in &node.properties {
            out.push(DeviceEvent::MasterParamChanged {
                param: MasterParam::from_name(&p.name),
                value: p.value.clone(),
            });
        }
    }
    if let Some(node) = root.children.iter().find(|c| c.name == "OUTPUT") {
        for p in &node.properties {
            out.push(DeviceEvent::OutputParamChanged {
                param: OutputParam::from_name(&p.name),
                value: p.value.clone(),
            });
        }
    }

    // 6. DUCKER / RECORDER / PLAYER initial params (singletons). Same key-less
    // shape: emit every property each node carries as its typed event.
    if let Some(node) = root.children.iter().find(|c| c.name == "DUCKER") {
        for p in &node.properties {
            out.push(DeviceEvent::DuckerParamChanged {
                param: DuckerParam::from_name(&p.name),
                value: p.value.clone(),
            });
        }
    }
    if let Some(node) = root.children.iter().find(|c| c.name == "RECORDER") {
        for p in &node.properties {
            out.push(DeviceEvent::RecorderParamChanged {
                param: RecorderParam::from_name(&p.name),
                value: p.value.clone(),
            });
        }
    }
    if let Some(node) = root.children.iter().find(|c| c.name == "PLAYER") {
        for p in &node.properties {
            out.push(DeviceEvent::PlayerParamChanged {
                param: PlayerParam::from_name(&p.name),
                value: p.value.clone(),
            });
        }
    }

    // 7. HEADPHONE initial params (multi-instance, one node per physical jack).
    // The counter runs over every HEADPHONE node in tree order; its ordinal is
    // the headphone index that events and commands address.
    let mut headphone_idx: u8 = 0;
    for child in &root.children {
        if child.name != "HEADPHONE" {
            continue;
        }
        let headphone = headphone_idx;
        headphone_idx = headphone_idx.saturating_add(1);
        for p in &child.properties {
            out.push(DeviceEvent::HeadphoneParamChanged {
                headphone,
                param: HeadphoneParam::from_name(&p.name),
                value: p.value.clone(),
            });
        }
    }

    // 8. EFFECTS_PARAMETERS initial params (multi-instance, one node per
    // channel-strip effects slot). Only the first contiguous run at root is the
    // addressable slots; the counter stops at the first non-EFFECTS_PARAMETERS
    // node so the nested PADEFFECTS set (a separate addressing context) is not
    // folded in. The ordinal is the slot index events and commands address.
    let mut effects_idx: u8 = 0;
    let mut seen_effects = false;
    for child in &root.children {
        if child.name != "EFFECTS_PARAMETERS" {
            // Stop at the end of the first run so nested-set siblings later in
            // the tree (if hoisted) never extend the addressable range.
            if seen_effects {
                break;
            }
            continue;
        }
        seen_effects = true;
        let effects = effects_idx;
        effects_idx = effects_idx.saturating_add(1);
        for p in &child.properties {
            out.push(DeviceEvent::EffectsParamChanged {
                effects,
                param: EffectsParam::from_name(&p.name),
                value: p.value.clone(),
            });
        }
    }

    // 9. GUI initial params (singleton). Same key-less shape as the other
    // singletons: emit every property the single front-panel UI-state node
    // carries as a typed GuiParamChanged.
    if let Some(node) = root.children.iter().find(|c| c.name == "GUI") {
        for p in &node.properties {
            out.push(DeviceEvent::GuiParamChanged {
                param: GuiParam::from_name(&p.name),
                value: p.value.clone(),
            });
        }
    }

    // 10. SOUNDPADS initial params (multi-instance PADs nested inside the
    // container). Only the first contiguous run of PAD nodes is the addressable
    // pads; the counter stops at the first non-PAD child so any sibling node
    // inside the container never extends the addressable range. The ordinal in
    // the run is the pad index events and commands address.
    if let Some(soundpads) = root.children.iter().find(|c| c.name == "SOUNDPADS") {
        let mut pad_idx: u8 = 0;
        let mut seen_pad = false;
        for child in &soundpads.children {
            if child.name != "PAD" {
                if seen_pad {
                    break;
                }
                continue;
            }
            seen_pad = true;
            let pad = pad_idx;
            pad_idx = pad_idx.saturating_add(1);
            for p in &child.properties {
                out.push(DeviceEvent::PadParamChanged {
                    pad,
                    param: PadParam::from_name(&p.name),
                    value: p.value.clone(),
                });
            }
        }
    }

    // 11. SYSTEM initial params (singleton). Same key-less shape as the other
    // singletons: emit every property the single device-wide state node carries
    // as a typed SystemParamChanged (identity, update lifecycle, date/time,
    // disables, USB/storage/sharing status). `boardType` surfaces here too, the
    // same value `Layout::detect_model` reads to pick the device model.
    if let Some(node) = root.children.iter().find(|c| c.name == "SYSTEM") {
        for p in &node.properties {
            out.push(DeviceEvent::SystemParamChanged {
                param: SystemParam::from_name(&p.name),
                value: p.value.clone(),
            });
        }
    }

    out
}

fn int_prop(node: &Node, name: &str) -> Option<i64> {
    node.properties
        .iter()
        .find(|p| p.name == name)
        .and_then(|p| p.value.as_int())
}

fn bool_prop(node: &Node, name: &str) -> Option<bool> {
    node.properties
        .iter()
        .find(|p| p.name == name)
        .and_then(|p| p.value.as_bool())
}

fn string_prop<'a>(node: &'a Node, name: &str) -> Option<&'a str> {
    node.properties
        .iter()
        .find(|p| p.name == name)
        .and_then(|p| match &p.value {
            Value::String(s) => Some(s.as_str()),
            _ => None,
        })
}

#[cfg(test)]
#[path = "events_tests.rs"]
mod tests;