MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
BroadcastEvents.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Core {
7class Window;
8}
9
10namespace MayaFlux::Vruta {
11class Event;
12class EventManager;
13class TaskScheduler;
14}
15
16namespace MayaFlux::Kriya {
17
18/**
19 * @brief Creates an Event coroutine that fires on every signal from a BroadcastSource.
20 *
21 * @param source Shared ownership of the BroadcastSource; lifetime is tied to the coroutine frame.
22 * @param callback Invoked with each delivered value.
23 * @return Event coroutine suitable for EventManager::add_event().
24 *
25 * @code
26 * auto src = make_persistent_shared<Vruta::BroadcastSource<float>>();
27 *
28 * get_event_manager()->add_event(std::make_shared<Vruta::Event>(
29 * Kriya::on_signal(src, [](const float& val) {
30 * // handle val
31 * })));
32 * @endcode
33 */
34template <typename T, typename Callback>
36 std::shared_ptr<Vruta::BroadcastSource<T>> source,
37 Callback callback);
38
39/**
40 * @brief Creates an Event coroutine that fires only when a predicate matches.
41 *
42 * @param source Shared ownership of the BroadcastSource.
43 * @param predicate Returns true for values that should trigger the callback.
44 * @param callback Invoked with each matching value.
45 * @return Event coroutine suitable for EventManager::add_event().
46 *
47 * @code
48 * get_event_manager()->add_event(std::make_shared<Vruta::Event>(
49 * Kriya::on_signal_matching(src,
50 * [](const float& v) { return v > 0.5f; },
51 * [](const float& v) { })));
52 * @endcode
53 */
54template <typename T, typename Predicate, typename Callback>
56 std::shared_ptr<Vruta::BroadcastSource<T>> source,
57 Predicate predicate,
58 Callback callback);
59
60/**
61 * @brief Create a BroadcastSource<bool> ticking once per audio output cycle.
62 *
63 * Registers an observer with AudioBackendService and returns the source.
64 * The returned observer id is stored inside the source's shared state via
65 * a custom deleter on the shared_ptr - unregistration is automatic when
66 * the last owner drops the source.
67 *
68 * @code
69 * auto src = Kriya::audio_output_tick();
70 * get_event_manager()->add_event(std::make_shared<Vruta::Event>(
71 * Kriya::on_signal(src, [ct](bool) {
72 * ct->process_default();
73 * })));
74 * @endcode
75 */
76[[nodiscard]] MAYAFLUX_API std::shared_ptr<Vruta::BroadcastSource<bool>> audio_output_tick();
77
79 std::shared_ptr<std::vector<uint8_t>> data;
80 uint32_t width {};
81 uint32_t height {};
82 uint32_t format {};
83};
84
85/**
86 * @brief Create a BroadcastSource<bool> ticking once per captured window frame.
87 *
88 * Registers a frame observer with DisplayService for the given window.
89 * The window must have capture enabled and the capture state must have been
90 * initialized (either via WindowAccessProcessor or set_capture_enabled before
91 * the first rendered frame).
92 *
93 * Returns nullptr if the DisplayService is unavailable, the window is null,
94 * or capture state is not yet active for the window.
95 *
96 * The observer callback is non-blocking. Signal delivery is safe across
97 * thread boundaries; the BroadcastSource handles the coroutine resume path.
98 *
99 * @code
100 * auto src = Kriya::window_frame_tick(window);
101 * get_event_manager()->add_event(std::make_shared<Vruta::Event>(
102 * Kriya::on_signal(src, [](const Kriya::WindowFrame& frame) {
103 * // frame.data, frame.width, frame.height, frame.format
104 * })));
105 * @endcode
106 */
107[[nodiscard]] MAYAFLUX_API std::shared_ptr<Vruta::BroadcastSource<WindowFrame>> window_frame_tick(
108 const std::shared_ptr<Core::Window>& window);
109
110/**
111 * @brief Call @p callback with every value @p source signals, registered on
112 * @p events.
113 *
114 * Registers on_signal(source, callback) under @p name and returns the event.
115 */
116template <typename T, typename Callback>
117std::shared_ptr<Vruta::Event> subscribe(
118 Vruta::EventManager& events,
119 std::shared_ptr<Vruta::BroadcastSource<T>> source,
120 Callback callback,
121 const std::string& name = "");
122
123/**
124 * @brief Broadcast of what @p fn returns, taken once per graphics frame.
125 *
126 * Adds a routine to @p scheduler that calls @p fn every graphics frame and
127 * signals each value it returns, registered under @p name. The routine ends
128 * once nothing holds the returned source, or when the task is cancelled.
129 *
130 * @code
131 * auto results = Kriya::frame_results<Result>(*scheduler, [state]() -> std::optional<Result> {
132 * return state->try_finish();
133 * });
134 * event_manager->add_event(std::make_shared<Vruta::Event>(
135 * Kriya::on_signal(results, [](const Result& r) { })));
136 * @endcode
137 */
138template <typename T>
139[[nodiscard]] std::shared_ptr<Vruta::BroadcastSource<T>> frame_results(
140 Vruta::TaskScheduler& scheduler,
141 std::function<std::optional<T>()> fn,
142 const std::string& name = "");
143
144/**
145 * @brief Signal what @p fn returns into @p source once per graphics frame.
146 *
147 * As the overload that creates its source, for a source that already has a
148 * consumer, so @p fn can refer to that consumer. The routine holds
149 * @p source weakly.
150 */
151template <typename T>
153 Vruta::TaskScheduler& scheduler,
154 const std::shared_ptr<Vruta::BroadcastSource<T>>& source,
155 std::function<std::optional<T>()> fn,
156 const std::string& name = "");
157
158} // namespace MayaFlux::Kriya
159
160#include "BroadcastEvents.inl"
PickCallback callback
Definition Picker.cpp:22
std::weak_ptr< Core::Window > window
Definition Picker.cpp:23
std::string name
Definition VKDevice.cpp:123
Awaitable single-value broadcast channel for cross-thread signal delivery.
Coroutine type for event-driven suspension.
Definition Event.hpp:26
Token-based multimodal task scheduling system for unified coroutine processing.
Definition Scheduler.hpp:51
Vruta::Event on_signal_matching(std::shared_ptr< Vruta::BroadcastSource< T > > source, Predicate predicate, Callback callback)
Creates an Event coroutine that fires only when a predicate matches.
std::shared_ptr< Vruta::BroadcastSource< WindowFrame > > window_frame_tick(const std::shared_ptr< Core::Window > &window)
Create a BroadcastSource<bool> ticking once per captured window frame.
std::shared_ptr< Vruta::Event > subscribe(Vruta::EventManager &events, std::shared_ptr< Vruta::BroadcastSource< T > > source, Callback callback, const std::string &name="")
Call callback with every value source signals, registered on events.
Vruta::Event on_signal(std::shared_ptr< Vruta::BroadcastSource< T > > source, Callback callback)
Creates an Event coroutine that fires on every signal from a BroadcastSource.
std::shared_ptr< Vruta::BroadcastSource< T > > frame_results(Vruta::TaskScheduler &scheduler, std::function< std::optional< T >()> fn, const std::string &name="")
Broadcast of what fn returns, taken once per graphics frame.
std::shared_ptr< Vruta::BroadcastSource< bool > > audio_output_tick()
Create a BroadcastSource<bool> ticking once per audio output cycle.
std::shared_ptr< std::vector< uint8_t > > data