openMSX
EventDistributor.hh
Go to the documentation of this file.
1 #ifndef EVENTDISTRIBUTOR_HH
2 #define EVENTDISTRIBUTOR_HH
3 
4 #include "Event.hh"
5 #include "Semaphore.hh"
6 #include "CondVar.hh"
7 #include "noncopyable.hh"
8 #include <map>
9 #include <memory>
10 #include <vector>
11 #include <functional>
12 
13 namespace openmsx {
14 
15 class Reactor;
16 class EventListener;
17 
19 {
20 public:
21  typedef std::shared_ptr<const Event> EventPtr;
22 
26  enum Priority {
27  OTHER = 0x08,
28  CONSOLE = 0x04,
29  HOTKEY = 0x02,
30  MSX = 0x01,
31  };
32 
33  explicit EventDistributor(Reactor& reactor);
34 
42  void registerEventListener(EventType type, EventListener& listener,
43  Priority priority = OTHER);
44 
50  void unregisterEventListener(EventType type, EventListener& listener);
51 
56  void distributeEvent(const EventPtr& event);
57 
62  void deliverEvents();
63 
70  bool sleep(unsigned us);
71 
72 private:
73  bool isRegistered(EventType type, EventListener* listener) const;
74 
75  Reactor& reactor;
76 
77  typedef std::multimap<Priority, EventListener*, std::greater<Priority>>
78  PriorityMap; // sort from big to small
79  std::map<EventType, PriorityMap> listeners;
80  typedef std::vector<EventPtr> EventQueue;
81  EventQueue scheduledEvents;
82  Semaphore sem;
83  CondVar cond;
84 };
85 
86 } // namespace openmsx
87 
88 #endif