gled 2.28.5

gled is an application for creating animations and effects on artnet or dmx installations
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
use crate::{
    midi::state::MidiState,
    output_state::ProjectState,
    storage::{
        asset::{
            Asset,
            midi_controller::{MidiController, MidiControllerMapping},
        },
        collections::Collections,
    },
};
use egui::mutex::Mutex;
use kanal::{Receiver, Sender, unbounded};
use midir::MidiOutputConnection;
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex as StdMutex, OnceLock};
use std::thread::spawn;

mod input_handling;
mod output_eval;
mod scene_state;

/// UI -> Runtime test command for MIDI output testing
#[derive(Debug, Clone)]
pub enum TestCommand {
    /// Send a MIDI test output: (status, data1, value)
    SendOutput { status: u8, data1: u8, value: u8 },
    /// Clear a MIDI test output by (status, data1)
    ClearOutput { status: u8, data1: u8 },
    /// Set the test device port name
    SetTestDevice { port_name: String },
    /// Unset test device (stop all test sending)
    UnsetTestDevice,
}

fn create_test_command_channel() -> (Sender<TestCommand>, Arc<Mutex<Receiver<TestCommand>>>) {
    let (sender, receiver) = unbounded::<TestCommand>();
    (sender, Arc::new(Mutex::new(receiver)))
}

#[allow(dead_code)]
#[derive(Default, Clone)]
pub struct RuntimeTestState {
    pub selected_output_port: Option<String>,
    pub scene_color_value_overrides: HashMap<String, u8>,
    pub value_preview_binding_indices: HashSet<usize>,
    pub preview_mapping: Option<MidiControllerMapping>,
}

/// Runtime execution state for test device output.
/// Maintained by test command processing in output_eval loop.
#[derive(Debug, Clone, Default)]
pub(super) struct RuntimeTestExecutionState {
    /// Selected test device port name
    pub selected_test_device: Option<String>,
    /// Active test outputs: (status, data1) -> value
    pub active_outputs: HashMap<(u8, u8), u8>,
}

static RUNTIME_TEST_STATES: OnceLock<
    StdMutex<HashMap<crate::storage::asset_id::AssetId<MidiController>, RuntimeTestState>>,
> = OnceLock::new();

static RUNTIME_TEST_EXECUTION_STATE: OnceLock<StdMutex<RuntimeTestExecutionState>> =
    OnceLock::new();

pub(super) fn runtime_test_states()
-> &'static StdMutex<HashMap<crate::storage::asset_id::AssetId<MidiController>, RuntimeTestState>> {
    RUNTIME_TEST_STATES.get_or_init(|| StdMutex::new(HashMap::new()))
}

pub(super) fn runtime_test_execution_state() -> &'static StdMutex<RuntimeTestExecutionState> {
    RUNTIME_TEST_EXECUTION_STATE.get_or_init(|| StdMutex::new(RuntimeTestExecutionState::default()))
}

#[allow(dead_code)]
pub fn set_controller_test_state(
    controller_id: crate::storage::asset_id::AssetId<MidiController>,
    state: Option<RuntimeTestState>,
) {
    let mut states = runtime_test_states()
        .lock()
        .expect("runtime test states lock poisoned");
    if let Some(state) = state {
        states.insert(controller_id, state);
    } else {
        states.remove(&controller_id);
    }
}

pub fn clear_all_test_states() {
    runtime_test_states()
        .lock()
        .expect("runtime test states lock poisoned")
        .clear();
}

#[derive(Clone)]
pub struct RuntimeBus {
    subscribers: Arc<Mutex<Vec<Sender<MidiRuntimeSnapshot>>>>,
    test_command_receiver: Arc<Mutex<Receiver<TestCommand>>>,
}

pub struct Runtime {
    receiver: Receiver<MidiRuntimeSnapshot>,
    snapshot: MidiRuntimeSnapshot,
    test_command_receiver: Arc<Mutex<Receiver<TestCommand>>>,
    last_sent_outputs: HashMap<(u8, u8), u8>,
    last_input_values: HashMap<(u8, u8), u8>,
}

#[derive(Default, Clone)]
pub(super) struct MidiRuntimeSnapshot {
    pub(super) mappings_by_controller: HashMap<RuntimeControllerKey, ActiveRuntimeMapping>,
    pub(super) scene_locations_row_major: Vec<crate::storage::asset::scene::grid::GridLocation>,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(super) enum RuntimeControllerKey {
    PortName(String),
    TestOnly(crate::storage::asset_id::AssetId<MidiController>),
}

#[derive(Clone)]
pub(super) struct ActiveRuntimeMapping {
    pub(super) _controller_id: crate::storage::asset_id::AssetId<MidiController>,
    pub(super) mapping: MidiControllerMapping,
}

impl Runtime {
    fn refresh_snapshot(&mut self) {
        while let Ok(Some(snapshot)) = self.receiver.try_recv() {
            self.snapshot = snapshot;
        }
    }

    fn process_test_commands(&mut self) {
        let mut exec_state = runtime_test_execution_state()
            .lock()
            .expect("test execution state lock poisoned");

        let test_receiver = self.test_command_receiver.lock();
        while let Ok(Some(cmd)) = test_receiver.try_recv() {
            match cmd {
                TestCommand::SendOutput {
                    status,
                    data1,
                    value,
                } => {
                    let old = exec_state.active_outputs.insert((status, data1), value);
                    if old != Some(value)
                        && let Some(port) = exec_state.selected_test_device.as_deref()
                    {
                        crate::midi::monitor::push_event(
                            port,
                            "test output",
                            &[status, data1, value],
                        );
                    }
                }
                TestCommand::ClearOutput { status, data1 } => {
                    if exec_state.active_outputs.remove(&(status, data1)).is_some()
                        && let Some(port) = exec_state.selected_test_device.as_deref()
                    {
                        crate::midi::monitor::push_event(port, "test clear", &[status, data1]);
                    }
                }
                TestCommand::SetTestDevice { port_name } => {
                    exec_state.selected_test_device = Some(port_name);
                }
                TestCommand::UnsetTestDevice => {
                    exec_state.selected_test_device = None;
                    exec_state.active_outputs.clear();
                }
            }
        }
    }

    pub fn handle_input(&mut self, port_name: &str, message: &[u8]) -> bool {
        self.refresh_snapshot();
        let handled =
            input_handling::handle_input_from_snapshot(&self.snapshot, port_name, message);
        if handled && message.len() == 3 {
            self.last_input_values
                .insert((message[0], message[1]), message[2]);
        }
        handled
    }

    pub fn send_output(
        &mut self,
        port_name: &str,
        state: &MidiState,
        connection: &mut MidiOutputConnection,
    ) -> bool {
        self.refresh_snapshot();
        self.process_test_commands();
        output_eval::send_output_from_snapshot(
            &self.snapshot,
            port_name,
            state,
            connection,
            &mut self.last_sent_outputs,
            &self.last_input_values,
        )
    }
}

impl RuntimeBus {
    pub fn new() -> (Self, Sender<TestCommand>) {
        let (test_command_sender, test_command_receiver) = create_test_command_channel();
        let subscribers: Arc<Mutex<Vec<Sender<MidiRuntimeSnapshot>>>> =
            Arc::new(Mutex::new(Vec::new()));
        let subscribers_for_thread = Arc::clone(&subscribers);

        spawn(move || {
            #[cfg(feature = "profiling")]
            profiling::register_thread!("midi:runtime");

            let state_receiver = crate::output_state::new_receiver();
            let mut collections = Collections::default();

            loop {
                let Ok(state) = state_receiver.recv() else {
                    continue;
                };
                collections.update();
                let snapshot = snapshot_from_project_state(&state, &collections);
                subscribers_for_thread
                    .lock()
                    .retain(|subscriber| subscriber.send(snapshot.clone()).is_ok());
            }
        });

        (
            Self {
                subscribers,
                test_command_receiver,
            },
            test_command_sender,
        )
    }

    pub fn runtime(&self) -> Runtime {
        let (sender, receiver) = unbounded();
        self.subscribers.lock().push(sender);

        Runtime {
            receiver,
            snapshot: MidiRuntimeSnapshot::default(),
            test_command_receiver: Arc::clone(&self.test_command_receiver),
            last_sent_outputs: HashMap::new(),
            last_input_values: HashMap::new(),
        }
    }
}

impl Default for RuntimeBus {
    fn default() -> Self {
        let (runtime_bus, _test_command_sender) = Self::new();
        runtime_bus
    }
}

fn snapshot_from_project_state(
    state: &ProjectState,
    collections: &Collections,
) -> MidiRuntimeSnapshot {
    let mut mappings_by_controller = HashMap::new();
    let mut included_controller_ids = HashSet::new();
    let mut scene_locations_row_major = Vec::new();

    if let Some(project) = state.project.as_ref() {
        scene_locations_row_major = project.scenes_instances_grid.keys().copied().collect();
        scene_locations_row_major.sort_by_key(|location| (location.row, location.col));
    }

    if let Some(project) = state.project.as_ref() {
        for (controller_key, selection) in &project.midi_active_mappings {
            let Some(controller_id) = selection else {
                continue;
            };
            let Some(controller) = Asset::<MidiController>::get(*controller_id, collections) else {
                continue;
            };

            mappings_by_controller.insert(
                RuntimeControllerKey::PortName(controller_key.clone()),
                ActiveRuntimeMapping {
                    _controller_id: *controller_id,
                    mapping: controller.data.mapping.clone(),
                },
            );
            included_controller_ids.insert(*controller_id);
        }
    }

    // Controllers not routed via External Devices still participate in Test Device mode.
    for controller in Asset::<MidiController>::all(collections) {
        if included_controller_ids.contains(&controller.id) {
            continue;
        }

        mappings_by_controller.insert(
            RuntimeControllerKey::TestOnly(controller.id),
            ActiveRuntimeMapping {
                _controller_id: controller.id,
                mapping: controller.data.mapping.clone(),
            },
        );
    }

    MidiRuntimeSnapshot {
        mappings_by_controller,
        scene_locations_row_major,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_command_enum_creation() {
        let cmd_send = TestCommand::SendOutput {
            status: 144,
            data1: 60,
            value: 100,
        };
        assert!(matches!(
            cmd_send,
            TestCommand::SendOutput {
                status: 144,
                data1: 60,
                value: 100
            }
        ));

        let cmd_clear = TestCommand::ClearOutput {
            status: 144,
            data1: 60,
        };
        assert!(matches!(
            cmd_clear,
            TestCommand::ClearOutput {
                status: 144,
                data1: 60
            }
        ));

        let cmd_set = TestCommand::SetTestDevice {
            port_name: "Device1".to_string(),
        };
        assert!(matches!(cmd_set, TestCommand::SetTestDevice { .. }));

        let cmd_unset = TestCommand::UnsetTestDevice;
        assert!(matches!(cmd_unset, TestCommand::UnsetTestDevice));
    }

    #[test]
    fn test_runtime_test_execution_state_active_outputs() {
        let state = runtime_test_execution_state();
        let mut exec_state = state.lock().expect("test execution state lock poisoned");

        // Initially empty
        assert!(exec_state.active_outputs.is_empty());
        assert!(exec_state.selected_test_device.is_none());

        // Add outputs
        exec_state.active_outputs.insert((144, 60), 100);
        exec_state.active_outputs.insert((176, 7), 64);
        assert_eq!(exec_state.active_outputs.len(), 2);

        // Update value
        exec_state.active_outputs.insert((144, 60), 127);
        assert_eq!(exec_state.active_outputs.get(&(144, 60)), Some(&127));

        // Remove output
        exec_state.active_outputs.remove(&(144, 60));
        assert_eq!(exec_state.active_outputs.len(), 1);

        // Clear all
        exec_state.active_outputs.clear();
        assert!(exec_state.active_outputs.is_empty());
    }

    #[test]
    fn test_runtime_test_execution_state_device_management() {
        let state = runtime_test_execution_state();
        let mut exec_state = state.lock().expect("test execution state lock poisoned");

        // Clear from any previous test
        exec_state.selected_test_device = None;
        exec_state.active_outputs.clear();

        // Set device
        exec_state.selected_test_device = Some("MidiOut1".to_string());
        assert_eq!(
            exec_state.selected_test_device,
            Some("MidiOut1".to_string())
        );

        // Add outputs while device is set
        exec_state.active_outputs.insert((144, 60), 100);
        assert_eq!(exec_state.active_outputs.len(), 1);

        // Clear device and outputs
        exec_state.selected_test_device = None;
        exec_state.active_outputs.clear();
        assert!(exec_state.selected_test_device.is_none());
        assert!(exec_state.active_outputs.is_empty());
    }
}