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 "stl.hh"
7
8#include <array>
9#include <condition_variable>
10#include <mutex>
11#include <vector>
12
13namespace openmsx {
14
15class Reactor;
16class EventListener;
17
19{
20public:
24 enum class Priority {
25 OTHER,
26 HOTKEY_HIGH, // above IMGUI
27 IMGUI,
28 HOTKEY_LOW, // below IMGUI
29 MSX,
30 LOWEST, // should only be used internally in EventDistributor
31 };
32 friend auto operator<=>(Priority x, Priority y) { return to_underlying(x) <=> to_underlying(y); }
33
34 explicit EventDistributor(Reactor& reactor);
35
44 Priority priority = Priority::OTHER);
45
52
57 void distributeEvent(Event&& event);
58
63 void deliverEvents();
64
71 bool sleep(unsigned us);
72
73private:
74 [[nodiscard]] bool isRegistered(EventType type, EventListener* listener) const;
75
76private:
77 Reactor& reactor;
78
79 struct Entry {
80 Priority priority;
81 EventListener* listener;
82 };
83 using PriorityMap = std::vector<Entry>; // sorted on priority
84 std::array<PriorityMap, size_t(EventType::NUM_EVENT_TYPES)> listeners;
85 using EventQueue = std::vector<Event>;
86 EventQueue scheduledEvents;
87 std::mutex mutex; // lock data structures
88 std::mutex cvMutex; // lock condition_variable
89 std::condition_variable condition;
90};
91
92} // namespace openmsx
93
94#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
constexpr auto to_underlying(E e) noexcept
Definition stl.hh:468