openMSX
EventDistributor.hh
Go to the documentation of this file.
1#ifndef EVENTDISTRIBUTOR_HH
2#define EVENTDISTRIBUTOR_HH
3
4#include "Event.hh"
5
6#include <array>
7#include <condition_variable>
8#include <mutex>
9#include <vector>
10#include <utility>
11
12namespace openmsx {
13
14class Reactor;
15class EventListener;
16
18{
19public:
23 enum class Priority {
24 OTHER,
25 HOTKEY_HIGH, // above IMGUI
26 IMGUI,
27 HOTKEY_LOW, // below IMGUI
28 MSX,
29 LOWEST, // should only be used internally in EventDistributor
30 };
31 friend auto operator<=>(Priority x, Priority y) { return std::to_underlying(x) <=> std::to_underlying(y); }
32
33 explicit EventDistributor(Reactor& reactor);
34
43 Priority priority = Priority::OTHER);
44
51
56 void distributeEvent(Event&& event);
57
62 void deliverEvents();
63
70 bool sleep(unsigned us);
71
72private:
73 [[nodiscard]] bool isRegistered(EventType type, EventListener* listener) const;
74
75private:
76 Reactor& reactor;
77
78 struct Entry {
79 Priority priority;
80 EventListener* listener;
81 };
82 using PriorityMap = std::vector<Entry>; // sorted on priority
83 std::array<PriorityMap, size_t(EventType::NUM_EVENT_TYPES)> listeners;
84 using EventQueue = std::vector<Event>;
85 EventQueue scheduledEvents;
86 std::mutex mutex; // lock data structures
87 std::mutex cvMutex; // lock condition_variable
88 std::condition_variable condition;
89};
90
91} // namespace openmsx
92
93#endif
void unregisterEventListener(EventType type, EventListener &listener)
Unregisters a previously registered event listener.
void distributeEvent(Event &&event)
Schedule the given event for delivery.
friend auto operator<=>(Priority x, Priority y)
bool sleep(unsigned us)
Sleep for the specified amount of time, but return early when (another thread) called the distributeE...
void deliverEvents()
This actually delivers the events.
void registerEventListener(EventType type, EventListener &listener, Priority priority=Priority::OTHER)
Registers a given object to receive certain events.
Priority
Priorities from high to low, higher priority listeners can block events for lower priority listeners.
Contains the main loop of openMSX.
Definition Reactor.hh:75
This file implemented 3 utility functions:
Definition Autofire.cc:11
EventType
Definition Event.hh:454
std::variant< KeyUpEvent, KeyDownEvent, MouseMotionEvent, MouseButtonUpEvent, MouseButtonDownEvent, MouseWheelEvent, JoystickAxisMotionEvent, JoystickHatEvent, JoystickButtonUpEvent, JoystickButtonDownEvent, OsdControlReleaseEvent, OsdControlPressEvent, WindowEvent, TextEvent, FileDropEvent, QuitEvent, FinishFrameEvent, CliCommandEvent, GroupEvent, BootEvent, FrameDrawnEvent, BreakEvent, SwitchRendererEvent, TakeReverseSnapshotEvent, AfterTimedEvent, MachineLoadedEvent, MachineActivatedEvent, MachineDeactivatedEvent, MidiInReaderEvent, MidiInWindowsEvent, MidiInCoreMidiEvent, MidiInCoreMidiVirtualEvent, MidiInALSAEvent, Rs232TesterEvent, Rs232NetEvent, ImGuiDelayedActionEvent, ImGuiActiveEvent > Event
Definition Event.hh:445