FastLED 3.9.3
Loading...
Searching...
No Matches
engine_events.cpp
1#include "engine_events.h"
2#include "namespace.h"
3
4FASTLED_NAMESPACE_BEGIN
5
6
7
8EngineEvents::Listener::Listener() {
9}
10
11EngineEvents::Listener::~Listener() {
12 #if FASTLED_HAS_ENGINE_EVENTS
13 EngineEvents* ptr = EngineEvents::getInstance();
14 const bool has_listener = ptr && ptr->_hasListener(this);
15 if (has_listener) {
16 // Warning, the listener should be removed by the subclass. If we are here
17 // then the subclass did not remove the listener and we are now in a partial
18 // state of destruction and the results may be undefined for multithreaded
19 // applications. However, for single threaded (the only option as of 2024, October)
20 // this will be ok.
21 ptr->_removeListener(this);
22 }
23 #endif
24}
25
26EngineEvents* EngineEvents::getInstance() {
27 #if FASTLED_HAS_ENGINE_EVENTS
29 #else
30 return nullptr; // strip out when engine events are disabled.
31 #endif
32}
33
34
35#if FASTLED_HAS_ENGINE_EVENTS
36void EngineEvents::_onPlatformPreLoop() {
37 for (auto& item : mListeners) {
38 auto listener = item.listener;
39 listener->onPlatformPreLoop();
40 }
41 for (auto& item : mListeners) {
42 auto listener = item.listener;
43 listener->onPlatformPreLoop2();
44 }
45}
46
47bool EngineEvents::_hasListener(Listener* listener) {
48 auto predicate = [listener](const Pair& pair) {
49 return pair.listener == listener;
50 };
51 return mListeners.find_if(predicate) != mListeners.end();
52}
53
54void EngineEvents::_addListener(Listener* listener, int priority) {
55 if (_hasListener(listener)) {
56 return;
57 }
58 for (auto it = mListeners.begin(); it != mListeners.end(); ++it) {
59 if (it->priority < priority) {
60 // this is now the highest priority in this spot.
61 EngineEvents::Pair pair = EngineEvents::Pair(listener, priority);
62 mListeners.insert(it, pair);
63 return;
64 }
65 }
66 EngineEvents::Pair pair = EngineEvents::Pair(listener, priority);
67 mListeners.push_back(pair);
68}
69
70void EngineEvents::_removeListener(Listener* listener) {
71 auto predicate = [listener](const Pair& pair) {
72 return pair.listener == listener;
73 };
74 auto it = mListeners.find_if(predicate);
75 if (it != mListeners.end()) {
76 mListeners.erase(it);
77 }
78}
79
80void EngineEvents::_onBeginFrame() {
81 // Make the copy of the listener list to avoid issues with listeners being added or removed during the loop.
82 ListenerList copy = mListeners;
83 for (auto& item : copy) {
84 auto listener = item.listener;
85 listener->onBeginFrame();
86 }
87}
88
89void EngineEvents::_onEndShowLeds() {
90 // Make the copy of the listener list to avoid issues with listeners being added or removed during the loop.
91 ListenerList copy = mListeners;
92 for (auto& item : copy) {
93 auto listener = item.listener;
94 listener->onEndShowLeds();
95 }
96}
97
98void EngineEvents::_onEndFrame() {
99 // Make the copy of the listener list to avoid issues with listeners being added or removed during the loop.
100 ListenerList copy = mListeners;
101 for (auto& item : copy) {
102 auto listener = item.listener;
103 listener->onEndFrame();
104 }
105}
106
107void EngineEvents::_onStripAdded(CLEDController* strip, uint32_t num_leds) {
108 // Make the copy of the listener list to avoid issues with listeners being added or removed during the loop.
109 ListenerList copy = mListeners;
110 for (auto& item : copy) {
111 auto listener = item.listener;
112 listener->onStripAdded(strip, num_leds);
113 }
114}
115
116
117void EngineEvents::_onCanvasUiSet(CLEDController *strip, const ScreenMap& screenmap) {
118 // Make the copy of the listener list to avoid issues with listeners being added or removed during the loop.
119 ListenerList copy = mListeners;
120 for (auto& item : copy) {
121 auto listener = item.listener;
122 listener->onCanvasUiSet(strip, screenmap);
123 }
124}
125
126
127#endif // FASTLED_HAS_ENGINE_EVENTS
128
129FASTLED_NAMESPACE_END
Base definition for an LED controller.