From 228435b54cb30f241f7d84351cb6d50f8acf4a4f Mon Sep 17 00:00:00 2001 From: Benjamin Klum Date: Wed, 14 Feb 2024 11:36:09 +0100 Subject: [PATCH] #858 Allow MIDI scripts to access compartment Lua even when just calculating feedback address --- main/lib/helgoboss-learn | 2 +- main/src/domain/main_processor.rs | 48 ++++++++++++++++++++++--------- main/src/domain/mapping.rs | 25 ++++++++++++---- 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/main/lib/helgoboss-learn b/main/lib/helgoboss-learn index 28d741a6b..41f3f1320 160000 --- a/main/lib/helgoboss-learn +++ b/main/lib/helgoboss-learn @@ -1 +1 @@ -Subproject commit 28d741a6b1cbd7213f5ff27c0354c05229e93f91 +Subproject commit 41f3f1320953645a9795b723f04d207c72a40d6f diff --git a/main/src/domain/main_processor.rs b/main/src/domain/main_processor.rs index 66f1c6b1e..a62b08c4d 100644 --- a/main/src/domain/main_processor.rs +++ b/main/src/domain/main_processor.rs @@ -356,8 +356,10 @@ impl MainProcessor { } if let Some(mapping_with_source) = self.all_mappings().find(|m| { m.feedback_is_effectively_on() - && m.source() - .has_same_feedback_address_as_value(&released_event.feedback_value) + && m.source().has_same_feedback_address_as_value( + &released_event.feedback_value, + self.basics.source_context(m.compartment()), + ) }) { if let Some(followed_mapping) = self.follow_maybe_virtual_mapping(mapping_with_source) { if self.basics.instance_feedback_is_effectively_enabled() { @@ -1118,7 +1120,10 @@ impl MainProcessor { } if m.feedback_is_effectively_on() { // Mark source as used - if let Some(addr) = m.source().extract_feedback_address() { + if let Some(addr) = m + .source() + .extract_feedback_address(self.basics.source_context(m.compartment())) + { unused_sources.remove(&addr); } } @@ -1142,7 +1147,10 @@ impl MainProcessor { continue; } // Mark source as used - if let Some(addr) = m.source().extract_feedback_address() { + if let Some(addr) = m + .source() + .extract_feedback_address(self.basics.source_context(m.compartment())) + { unused_sources.remove(&addr); } } @@ -1194,7 +1202,10 @@ impl MainProcessor { } if m.feedback_is_effectively_on() { // Mark source as used - if let Some(addr) = m.source().extract_feedback_address() { + if let Some(addr) = m + .source() + .extract_feedback_address(self.basics.source_context(compartment)) + { unused_sources.remove(&addr); } } @@ -1400,7 +1411,10 @@ impl MainProcessor { } if m.feedback_is_effectively_on() { // Mark source as used - if let Some(addr) = m.source().extract_feedback_address() { + if let Some(addr) = m + .source() + .extract_feedback_address(self.basics.source_context(m.compartment())) + { unused_sources.remove(&addr); } } @@ -1483,7 +1497,10 @@ impl MainProcessor { ); if m.feedback_is_effectively_on() { // Mark source as used - if let Some(addr) = m.source().extract_feedback_address() { + if let Some(addr) = m + .source() + .extract_feedback_address(self.basics.source_context(m.compartment())) + { unused_sources.remove(&addr); } } @@ -2385,8 +2402,9 @@ impl MainProcessor { .filter(|m| m.feedback_is_effectively_on()) .filter_map(|m| { Some(( - m.source().extract_feedback_address()?, - m.off_feedback(self.basics.source_context(m.compartment()), NoopLogger)?, + m.source() + .extract_feedback_address(self.basics.source_context(compartment))?, + m.off_feedback(self.basics.source_context(compartment), NoopLogger)?, )) }) .collect() @@ -2396,7 +2414,9 @@ impl MainProcessor { .filter(|m| m.feedback_is_effectively_on()) .filter_map(|m| { Some(( - m.source().extract_feedback_address()?, + m.source().extract_feedback_address( + self.basics.source_context(m.compartment()), + )?, m.off_feedback(self.basics.source_context(m.compartment()), NoopLogger)?, )) }) @@ -2658,10 +2678,10 @@ impl MainProcessor { // An existing mapping is being overwritten. if previous_mapping.feedback_is_effectively_on() { // And its light is currently on. - if mapping - .source() - .has_same_feedback_address_as_source(previous_mapping.source()) - { + if mapping.source().has_same_feedback_address_as_source( + previous_mapping.source(), + self.basics.source_context(mapping.compartment()), + ) { // Source is the same. if mapping.feedback_is_effectively_on() { // Lights should still be on. diff --git a/main/src/domain/mapping.rs b/main/src/domain/mapping.rs index 9401ebb27..b681965ab 100644 --- a/main/src/domain/mapping.rs +++ b/main/src/domain/mapping.rs @@ -1622,11 +1622,14 @@ impl CompoundMappingSource { // TODO-medium There are quite some places in which we are fine with a borrowed version but // the problem is the MIDI source can't simply give us a borrowed one. Maybe we should // create one at MIDI source creation time! But for this we need to make MidiSource a struct. - pub fn extract_feedback_address(&self) -> Option { + pub fn extract_feedback_address( + &self, + source_context: RealearnSourceContext, + ) -> Option { use CompoundMappingSource::*; match self { Midi(s) => Some(CompoundMappingSourceAddress::Midi( - s.extract_feedback_address()?, + s.extract_feedback_address(source_context)?, )), Osc(s) => Some(CompoundMappingSourceAddress::Osc( s.feedback_address().clone(), @@ -1644,11 +1647,17 @@ impl CompoundMappingSource { /// Used for: /// /// - Source takeover (feedback) - pub fn has_same_feedback_address_as_value(&self, value: &FinalSourceFeedbackValue) -> bool { + pub fn has_same_feedback_address_as_value( + &self, + value: &FinalSourceFeedbackValue, + source_context: RealearnSourceContext, + ) -> bool { use CompoundMappingSource::*; match (self, value) { (Osc(s), FinalSourceFeedbackValue::Osc(v)) => s.has_same_feedback_address_as_value(v), - (Midi(s), FinalSourceFeedbackValue::Midi(v)) => s.has_same_feedback_address_as_value(v), + (Midi(s), FinalSourceFeedbackValue::Midi(v)) => { + s.has_same_feedback_address_as_value(v, source_context) + } _ => false, } } @@ -1658,11 +1667,15 @@ impl CompoundMappingSource { /// Used for: /// /// - Feedback diffing - pub fn has_same_feedback_address_as_source(&self, other: &Self) -> bool { + pub fn has_same_feedback_address_as_source( + &self, + other: &Self, + source_context: RealearnSourceContext, + ) -> bool { use CompoundMappingSource::*; match (self, other) { (Osc(s1), Osc(s2)) => s1.has_same_feedback_address_as_source(s2), - (Midi(s1), Midi(s2)) => s1.has_same_feedback_address_as_source(s2), + (Midi(s1), Midi(s2)) => s1.has_same_feedback_address_as_source(s2, source_context), (Virtual(s1), Virtual(s2)) => s1.has_same_feedback_address_as_source(s2), _ => false, }