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
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
use crate::audio::{AUDIO_DEVICES, audio_device_info_loop, audio_device_labels};
use crate::midi::{monitor, normalize_controller_key};
use crate::storage::asset::project::Project;
use crate::storage::collections::Collections;
use crate::ui::action::UiAction;
use crate::ui::asset::CollectionsChangeButton;
use crate::{
    input::artnet::ARTNET_CONFIG,
    storage::asset::{
        Asset, midi_controller::MidiController, output_device::routing::OutputRouting,
    },
    ui::{
        window_common::{default_viewport_builder, gled_window_frame},
        windows::output_routings::HOVERED_OUTPUT_ROUTING,
    },
};
use chrono::Local;
use egui::{
    Button, CentralPanel, ComboBox, Context, DragValue, Id, Layout, Response, RichText, Slider, Ui,
    Vec2, ViewportId, Widget, WidgetText,
};
use egui_phosphor_icons::icons;
use midir::MidiOutput;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;
use std::thread;
use std::thread::JoinHandle;

static REFRESH_DEVICES: AtomicBool = AtomicBool::new(false);

#[derive(Default)]
pub struct ExternalDeviceSettings {
    open: bool,
    selected_submenu: String,
    handle: Option<JoinHandle<()>>,
}

#[derive(Clone)]
struct MidiControllerSelectionRow {
    port_name: String,
    input_connected: bool,
    output_connected: bool,
}

#[derive(Clone)]
struct MidiMappingAssetOptions {
    options: Vec<(
        Option<crate::storage::asset_id::AssetId<MidiController>>,
        String,
    )>,
}

impl ExternalDeviceSettings {
    #[cfg_attr(feature = "profiling", profiling::function)]
    pub fn update(
        &mut self,
        ctx: &Context,
        project: &mut Option<Project>,
        collections: &mut Collections,
        midi_diagnostics: &[monitor::MidiPortDiagnostics],
    ) {
        REFRESH_DEVICES.store(self.open && self.selected_submenu == "Audio Input", Relaxed);
        if !self.open {
            return;
        }
        let mut project = if let Some(project) = project {
            project
        } else {
            return;
        };

        ctx.show_viewport_immediate(
            ViewportId(Id::new("external devices settings window")),
            default_viewport_builder()
                .with_inner_size(Vec2::new(800.0, 500.0))
                .with_min_inner_size(Vec2::new(800.0, 500.0))
                .with_resizable(false)
                .with_minimize_button(false)
                .with_maximize_button(true),
            |ctx, _viewport_class| {
                ctx.input(|input| {
                    if input.viewport().close_requested() {
                        self.open = false;
                    }
                });

                gled_window_frame(ctx, "External Devices", |ui| {
                    let midi_mapping_rows = collect_midi_controller_rows(project, midi_diagnostics);
                    let midi_mapping_options = collect_midi_mapping_options(collections);
                    let settings_menu = SettingsMenu::new(&mut project, &mut self.selected_submenu)
                        .add_submenu("Audio Input".into(), |ui, project| {
                            audio_input_settings(ui, project)
                        })
                        .add_submenu("MIDI Mappings".into(), |ui, project| {
                            midi_mapping_settings(
                                ui,
                                project,
                                &midi_mapping_rows,
                                &midi_mapping_options,
                            )
                        })
                        .add_submenu("Artnet Bridge".into(), |ui, project| {
                            artnet_bridge_settings(ui, project, collections)
                        })
                        .add_submenu("Artnet Control".into(), |ui, project| {
                            artnet_control_input_settings(ui, project)
                        })
                        .add_submenu("Artnet Trigger".into(), |ui, project| {
                            artnet_trigger_settings(ui, project)
                        })
                        .add_submenu("OSC Input".into(), |ui, project| osc_settings(ui, project));
                    ui.add(settings_menu);
                });

                let is_refreshing = self
                    .handle
                    .as_ref()
                    .map_or_else(|| false, |x| !x.is_finished());
                if !is_refreshing && REFRESH_DEVICES.load(Relaxed) {
                    self.handle = Some(
                        thread::Builder::new()
                            .name("gled:audio:device_refresh".to_string())
                            .spawn(|| audio_device_info_loop(&REFRESH_DEVICES))
                            .expect("Could not spawn audio device refresh thread"),
                    );
                }
            },
        );
    }

    pub fn open(&mut self) {
        self.open = true;
    }

    pub fn close(&mut self) {
        self.open = false;
    }
}

fn audio_input_settings(ui: &mut Ui, state: &mut Project) {
    let project = state;
    ui.heading("Audio Input Device");
    let mut selected = project.audio_input_device.clone();
    let old_selected = selected.clone();

    let devices = audio_device_labels(
        &AUDIO_DEVICES
            .lock()
            .expect("audio devices lock poisoned")
            .clone(),
    );
    ui.selectable_value(&mut selected, None, "None");
    for (id, label) in devices {
        ui.selectable_value(&mut selected, Some(id), label);
    }

    if selected != old_selected {
        tracing::info!("Audio input device changed to {:?}", selected);
        {
            UiAction::SetAudioDevice(selected).enqueue();
        }
    }
}

fn midi_mapping_settings(
    ui: &mut Ui,
    project: &mut Project,
    controllers: &[MidiControllerSelectionRow],
    options: &MidiMappingAssetOptions,
) {
    ui.heading("Active MIDI Mapping by Controller");
    ui.add_space(3.0);

    if controllers.is_empty() {
        ui.label("No MIDI controllers detected yet. Open MIDI Controllers window to inspect incoming ports.");
        return;
    }

    if options.options.len() <= 1 {
        ui.small("No MIDI mapping assets available. You can still remove existing assignments.");
    }

    egui::ScrollArea::vertical()
        .id_salt("external_devices_midi_mapping_list")
        .scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysVisible)
        .show(ui, |ui| {
            for controller in controllers {
                ui.separator();
                ui.label(&controller.port_name);
                ui.small(format!(
                    "Input: {} | Output: {}",
                    if controller.input_connected {
                        "connected"
                    } else {
                        "missing"
                    },
                    if controller.output_connected {
                        "connected"
                    } else {
                        "missing"
                    }
                ));

                let current = project.midi_active_mappings.get(&controller.port_name);
                let current = current.or_else(|| {
                    let normalized = normalize_controller_key(&controller.port_name);
                    let mut matches = project
                        .midi_active_mappings
                        .iter()
                        .filter(|(key, _)| normalize_controller_key(key) == normalized)
                        .map(|(_, mapping)| mapping);
                    let first = matches.next()?;
                    if matches.next().is_some() {
                        return None;
                    }
                    Some(first)
                });
                let mut selected_index = options
                    .options
                    .iter()
                    .position(|(controller_id, _)| current.is_some_and(|c| c == controller_id))
                    .unwrap_or(0);

                let original_index = selected_index;

                ComboBox::new(format!("midi_mapping_{}", controller.port_name), "")
                    .selected_text(options.options[selected_index].1.clone())
                    .show_ui(ui, |ui| {
                        for (index, (_, label)) in options.options.iter().enumerate() {
                            ui.selectable_value(&mut selected_index, index, label);
                        }
                    });

                let remove_clicked = selected_index != 0 && ui.button("Remove Mapping").clicked();
                if remove_clicked {
                    selected_index = 0;
                }

                // Only update mapping if user explicitly changed the selection or removed mapping
                let combobox_changed = selected_index != original_index;

                if combobox_changed || remove_clicked {
                    let (controller_id, _) = &options.options[selected_index];

                    // Always use exact port name as the key for specificity
                    // This ensures each port can have its own mapping
                    let exact_key = controller.port_name.clone();

                    // Now set/update the exact mapping
                    match controller_id {
                        Some(controller_id) => {
                            project
                                .midi_active_mappings
                                .insert(exact_key, Some(*controller_id));
                        }
                        None => {
                            // Removing mapping: ensure exact key is removed
                            project.midi_active_mappings.remove(&exact_key);
                        }
                    }
                }
            }
        });
}

fn collect_midi_mapping_options(collections: &Collections) -> MidiMappingAssetOptions {
    let mut options = vec![(None, "None".to_owned())];
    let mut controllers: Vec<_> = Asset::<MidiController>::all(collections);
    controllers.sort_by(|a, b| a.name().cmp(b.name()));

    for controller in controllers {
        options.push((Some(controller.id), controller.name().to_owned()));
    }

    MidiMappingAssetOptions { options }
}

fn collect_midi_controller_rows(
    project: &Project,
    diagnostics: &[monitor::MidiPortDiagnostics],
) -> Vec<MidiControllerSelectionRow> {
    let configured_ports = project
        .midi_active_mappings
        .keys()
        .map(|key| normalize_controller_key(key));

    let mut all_ports: BTreeSet<String> = BTreeSet::new();
    for diag in diagnostics {
        all_ports.insert(normalize_controller_key(&diag.port_name));
    }
    for output_port in list_midi_output_ports() {
        all_ports.insert(normalize_controller_key(&output_port));
    }
    for configured in configured_ports {
        all_ports.insert(configured);
    }

    all_ports
        .into_iter()
        .filter(|port_name| !is_gled_midi_port(port_name))
        .map(|port_name| {
            let input_connected = diagnostics.iter().any(|diag| {
                normalize_controller_key(&diag.port_name) == port_name && diag.input_connected
            });
            let output_connected = diagnostics.iter().any(|diag| {
                normalize_controller_key(&diag.port_name) == port_name && diag.output_connected
            });
            MidiControllerSelectionRow {
                port_name,
                input_connected,
                output_connected,
            }
        })
        .collect()
}

fn list_midi_output_ports() -> Vec<String> {
    let Ok(output) = MidiOutput::new("gled_external_devices_scan") else {
        return Vec::new();
    };

    output
        .ports()
        .into_iter()
        .filter_map(|port| output.port_name(&port).ok())
        .filter(|name| !name.trim().is_empty())
        .collect()
}

fn is_gled_midi_port(port_name: &str) -> bool {
    port_name.contains("gled_read_input") || port_name.contains("gled_write_output")
}

fn artnet_control_input_settings(ui: &mut Ui, project: &mut Project) {
    project.artnet_control_config(|config| {
        ui.heading("Artnet Control");
        ui.checkbox(&mut config.active, "Active");
        ui.add(DragValue::new(&mut config.universe).range(0..=32767));
    });
}

fn artnet_bridge_settings(ui: &mut Ui, _project: &mut Project, collections: &mut Collections) {
    let mut config = ARTNET_CONFIG.lock();
    ui.heading("Artnet Bridge");
    ui.add_space(3.0);

    if config.bridge.is_empty() {
        ui.label("No artnet data received..");
    }

    egui::ScrollArea::vertical()
        .scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysVisible)
        .id_salt("artnet_bridge_output_scroll")
        .show(ui, |ui| {
            ui.with_layout(Layout::top_down_justified(egui::Align::Min), |ui| {
                let mut hovered_output_routing = None;

                for (universe, bridge) in config.bridge.iter_mut() {
                    ui.label(RichText::new(format!("Universe: {universe}")).heading());

                    ui.horizontal(|ui| {
                        ui.label(format!("Updates: {}", bridge.updates));
                        ui.label(
                            bridge
                                .last_data_at
                                .with_timezone(&Local)
                                .format("(Last: %H:%M:%S)")
                                .to_string(),
                        );

                        bridge
                            .output_routing
                            .device
                            .collections_change_button(ui, collections);

                        if let Some(device) = bridge
                            .output_routing
                            .device
                            .and_then(|asset_id| Asset::get(asset_id, collections))
                        {
                            let device_universes = device.data.universes();
                            if !device_universes.is_empty() {
                                ComboBox::new(format!("{universe}_universe"), "")
                                    .selected_text(match bridge.output_routing.universe {
                                        None => WidgetText::from("No universe selected"),
                                        Some(universe) => WidgetText::from(universe.to_string()),
                                    })
                                    .width(150.0)
                                    .show_ui(ui, |ui| {
                                        for device_universe in device_universes {
                                            let res = ui.selectable_value(
                                                &mut bridge.output_routing.universe,
                                                Some(*device_universe),
                                                device_universe.to_string(),
                                            );
                                            if res.hovered() {
                                                hovered_output_routing = Some(OutputRouting {
                                                    device: Some(device.id),
                                                    universe: Some(*device_universe),
                                                });
                                            }
                                        }
                                    });
                            }

                            ComboBox::new(format!("{universe}_merge"), "")
                                .selected_text(match bridge.htp {
                                    true => "HTP",
                                    false => "LTP",
                                })
                                .width(50.0)
                                .show_ui(ui, |ui| {
                                    ui.selectable_value(&mut bridge.htp, true, "HTP");
                                    ui.selectable_value(&mut bridge.htp, false, "LTP");
                                });

                            if ui.add(Button::new(icons::X)).clicked() {
                                bridge.output_routing = OutputRouting::default();
                            }
                        }
                    });
                }

                *HOVERED_OUTPUT_ROUTING.lock() = hovered_output_routing;
            });
        });
}

fn artnet_trigger_settings(ui: &mut Ui, _project: &mut Project) {
    let mut config = ARTNET_CONFIG.lock();
    ui.heading("Artnet Trigger");
    ui.add_space(3.0);

    ui.separator();
    ui.label("Universe");
    ui.add(Slider::new(&mut config.universe, 0..=32768));
}

fn osc_settings(ui: &mut Ui, project: &mut Project) {
    let osc_config = project.osc_config;
    ui.heading("OSC");
    let mut active = osc_config.active;
    if ui.checkbox(&mut active, "Active").changed() {
        UiAction::SetProjectOscActive(active).enqueue();
    }
    ui.horizontal(|ui| {
        ui.label("Port");
        let mut port = osc_config.port;
        let response = ui.add(DragValue::new(&mut port).range(1..=65535));
        if (response.lost_focus() || response.drag_stopped()) && port != osc_config.port {
            UiAction::SetProjectOscPort(port).enqueue();
        }
    });
}

struct SettingsMenu<'a, S> {
    selected_submenu: &'a mut String,
    edit_state: &'a mut S,
    submenus: BTreeMap<String, Box<dyn FnOnce(&mut Ui, &mut S) + 'a>>,
}

impl<'a, S> SettingsMenu<'a, S> {
    fn new(edit_state: &'a mut S, selected_submenu: &'a mut String) -> Self {
        Self {
            selected_submenu,
            edit_state,
            submenus: BTreeMap::default(),
        }
    }
    fn add_submenu(
        mut self,
        submenu: String,
        ui_callback: impl FnOnce(&mut Ui, &mut S) + 'a,
    ) -> Self {
        self.submenus.insert(submenu, Box::new(ui_callback));
        self
    }
}

impl<'a, S> Widget for SettingsMenu<'a, S> {
    fn ui(mut self, ui: &mut Ui) -> Response {
        debug_assert!(!self.submenus.is_empty());
        egui::Panel::left("artnet_input_config")
            .resizable(true)
            .size_range(100.0..=200.0)
            .default_size(150.0)
            .show(ui, |ui| {
                ui.take_available_space();
                self.submenus.keys().for_each(|key| {
                    ui.selectable_value(self.selected_submenu, key.clone(), key);
                });
            });
        if !self.submenus.contains_key(self.selected_submenu) {
            *self.selected_submenu = self
                .submenus
                .keys()
                .next()
                .expect("settings menu must have at least one submenu")
                .clone();
        }

        let submenu = self
            .submenus
            .remove(self.selected_submenu)
            .expect("submenu was just confirmed to be present");
        CentralPanel::default()
            .show(ui, |ui| submenu(ui, self.edit_state))
            .response
    }
}