openMSX
EventDelay.cc
Go to the documentation of this file.
1 #include "EventDelay.hh"
2 #include "EventDistributor.hh"
3 #include "MSXEventDistributor.hh"
4 #include "ReverseManager.hh"
5 #include "InputEvents.hh"
6 #include "Timer.hh"
7 #include "MSXException.hh"
8 #include "checked_cast.hh"
9 #include "one_of.hh"
10 #include "ranges.hh"
11 #include "stl.hh"
12 #include <cassert>
13 
14 namespace openmsx {
15 
17  CommandController& commandController,
18  EventDistributor& eventDistributor_,
19  MSXEventDistributor& msxEventDistributor_,
20  ReverseManager& reverseManager)
21  : Schedulable(scheduler_)
22  , eventDistributor(eventDistributor_)
23  , msxEventDistributor(msxEventDistributor_)
24  , prevEmu(EmuTime::zero())
25  , prevReal(Timer::getTime())
26  , delaySetting(
27  commandController, "inputdelay",
28  "delay input to avoid key-skips", 0.0, 0.0, 10.0)
29 {
30  eventDistributor.registerEventListener(
32  eventDistributor.registerEventListener(
34 
35  eventDistributor.registerEventListener(
37  eventDistributor.registerEventListener(
39  eventDistributor.registerEventListener(
41 
42  eventDistributor.registerEventListener(
44  eventDistributor.registerEventListener(
46  eventDistributor.registerEventListener(
48  eventDistributor.registerEventListener(
50 
51  reverseManager.registerEventDelay(*this);
52 }
53 
55 {
56  eventDistributor.unregisterEventListener(
57  OPENMSX_KEY_DOWN_EVENT, *this);
58  eventDistributor.unregisterEventListener(
59  OPENMSX_KEY_UP_EVENT, *this);
60 
61  eventDistributor.unregisterEventListener(
63  eventDistributor.unregisterEventListener(
65  eventDistributor.unregisterEventListener(
67 
68  eventDistributor.unregisterEventListener(
70  eventDistributor.unregisterEventListener(
71  OPENMSX_JOY_HAT_EVENT, *this);
72  eventDistributor.unregisterEventListener(
74  eventDistributor.unregisterEventListener(
76 }
77 
78 int EventDelay::signalEvent(const EventPtr& event)
79 {
80  toBeScheduledEvents.push_back(event);
81  if (delaySetting.getDouble() == 0.0) {
83  }
84  return 0;
85 }
86 
87 void EventDelay::sync(EmuTime::param curEmu)
88 {
89  auto curRealTime = Timer::getTime();
90  auto realDuration = curRealTime - prevReal;
91  prevReal = curRealTime;
92  auto emuDuration = curEmu - prevEmu;
93  prevEmu = curEmu;
94 
95  double factor = emuDuration.toDouble() / realDuration;
96  EmuDuration extraDelay(delaySetting.getDouble());
97 
98 #if PLATFORM_ANDROID
99  // The virtual keyboard on Android sends a key press and the
100  // corresponding key release event directly after each other, without a
101  // delay. It sends both events either when the user has finished a
102  // short tap or alternatively after the user has hold the button
103  // pressed for a few seconds and then has selected the appropriate
104  // character from the multi-character-popup that the virtual keyboard
105  // displays when the user holds a button pressed for a short while.
106  // Either way, the key release event comes way too short after the
107  // press event for the MSX to process it. The two events follow each
108  // other within a few milliseconds at most. Therefore, on Android,
109  // special logic must be foreseen to delay the release event for a
110  // short while. This special logic correlates each key release event
111  // with the corresponding press event for the same key. If they are
112  // less then 2/50 second apart, the release event gets delayed until
113  // the next sync call. The 2/50 second has been chosen because it can
114  // take up to 2 vertical interrupts (2 screen refreshes) for the MSX to
115  // see the key press in the keyboard matrix, thus, 2/50 seconds is the
116  // minimum delay required for an MSX running in PAL mode.
117  std::vector<EventPtr> toBeRescheduledEvents;
118 #endif
119 
120  EmuTime time = curEmu + extraDelay;
121  for (auto& e : toBeScheduledEvents) {
122 #if PLATFORM_ANDROID
123  if (e->getType() == one_of(OPENMSX_KEY_DOWN_EVENT, OPENMSX_KEY_UP_EVENT)) {
124  auto keyEvent = checked_cast<const KeyEvent*>(e.get());
125  int maskedKeyCode = int(keyEvent->getKeyCode()) & int(Keys::K_MASK);
126  auto it = ranges::find_if(nonMatchedKeyPresses,
127  EqualTupleValue<0>(maskedKeyCode));
128  if (e->getType() == OPENMSX_KEY_DOWN_EVENT) {
129  if (it == end(nonMatchedKeyPresses)) {
130  nonMatchedKeyPresses.emplace_back(maskedKeyCode, e);
131  } else {
132  it->second = e;
133  }
134  } else {
135  if (it != end(nonMatchedKeyPresses)) {
136  auto timedPressEvent = checked_cast<const TimedEvent*>(it->second.get());
137  auto timedReleaseEvent = checked_cast<const TimedEvent*>(e.get());
138  auto pressRealTime = timedPressEvent->getRealTime();
139  auto releaseRealTime = timedReleaseEvent->getRealTime();
140  auto deltaTime = releaseRealTime - pressRealTime;
141  if (deltaTime <= 2000000 / 50) {
142  // The key release came less then 2 MSX interrupts from the key press.
143  // Reschedule it for the next sync, with the realTime updated to now, so that it seems like the
144  // key was released now and not when android released it.
145  // Otherwise, the offset calculation for the emutime further down below will go wrong on the next sync
146  EventPtr newKeyupEvent = std::make_shared<KeyUpEvent>(keyEvent->getKeyCode());
147  toBeRescheduledEvents.push_back(newKeyupEvent);
148  continue; // continue with next to be scheduled event
149  }
150  move_pop_back(nonMatchedKeyPresses, it);
151  }
152  }
153  }
154 #endif
155  scheduledEvents.push_back(e);
156  auto timedEvent = checked_cast<const TimedEvent*>(e.get());
157  auto eventRealTime = timedEvent->getRealTime();
158  assert(eventRealTime <= curRealTime);
159  auto offset = curRealTime - eventRealTime;
160  EmuDuration emuOffset(factor * offset);
161  auto schedTime = (emuOffset < extraDelay)
162  ? time - emuOffset
163  : curEmu;
164  assert(curEmu <= schedTime);
165  setSyncPoint(schedTime);
166  }
167  toBeScheduledEvents.clear();
168 
169 #if PLATFORM_ANDROID
170  append(toBeScheduledEvents, std::move(toBeRescheduledEvents));
171 #endif
172 }
173 
174 void EventDelay::executeUntil(EmuTime::param time)
175 {
176  try {
177  auto event = std::move(scheduledEvents.front());
178  scheduledEvents.pop_front();
179  msxEventDistributor.distributeEvent(std::move(event), time);
180  } catch (MSXException&) {
181  // ignore
182  }
183 }
184 
186 {
187  EmuTime time = getCurrentTime();
188 
189  for (auto& e : scheduledEvents) {
190  msxEventDistributor.distributeEvent(e, time);
191  }
192  scheduledEvents.clear();
193 
194  for (auto& e : toBeScheduledEvents) {
195  msxEventDistributor.distributeEvent(e, time);
196  }
197  toBeScheduledEvents.clear();
198 
200 }
201 
202 } // namespace openmsx
one_of.hh
openmsx::OPENMSX_JOY_AXIS_MOTION_EVENT
@ OPENMSX_JOY_AXIS_MOTION_EVENT
Definition: Event.hh:22
openmsx::EventDelay::EventDelay
EventDelay(Scheduler &scheduler, CommandController &commandController, EventDistributor &eventDistributor, MSXEventDistributor &msxEventDistributor, ReverseManager &reverseManager)
Definition: EventDelay.cc:16
openmsx::Scheduler
Definition: Scheduler.hh:33
openmsx::OPENMSX_MOUSE_MOTION_EVENT
@ OPENMSX_MOUSE_MOTION_EVENT
Definition: Event.hh:15
openmsx::CommandController
Definition: CommandController.hh:17
Timer.hh
MSXEventDistributor.hh
openmsx::MSXEventDistributor
Definition: MSXEventDistributor.hh:13
openmsx::EmuDuration
Definition: EmuDuration.hh:18
openmsx::OPENMSX_JOY_HAT_EVENT
@ OPENMSX_JOY_HAT_EVENT
Definition: Event.hh:24
openmsx::Schedulable
Every class that wants to get scheduled at some point must inherit from this class.
Definition: Schedulable.hh:33
ranges.hh
openmsx::MSXEventDistributor::distributeEvent
void distributeEvent(const EventPtr &event, EmuTime::param time)
Deliver the event to all registered listeners.
Definition: MSXEventDistributor.cc:29
MSXException.hh
openmsx::Timer::getTime
uint64_t getTime()
Get current (real) time in us.
Definition: Timer.cc:7
openmsx::OPENMSX_KEY_DOWN_EVENT
@ OPENMSX_KEY_DOWN_EVENT
Definition: Event.hh:13
openmsx::EventDistributor
Definition: EventDistributor.hh:16
EventDelay.hh
openmsx::Schedulable::setSyncPoint
void setSyncPoint(EmuTime::param timestamp)
Definition: Schedulable.cc:23
move_pop_back
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
Definition: stl.hh:177
ReverseManager.hh
openmsx::OPENMSX_MOUSE_BUTTON_UP_EVENT
@ OPENMSX_MOUSE_BUTTON_UP_EVENT
Definition: Event.hh:17
openmsx::EventDistributor::MSX
@ MSX
Definition: EventDistributor.hh:28
openmsx::Schedulable::removeSyncPoints
void removeSyncPoints()
Definition: Schedulable.cc:33
one_of
Definition: one_of.hh:7
EventDistributor.hh
openmsx::ReverseManager::registerEventDelay
void registerEventDelay(EventDelay &eventDelay_)
Definition: ReverseManager.hh:42
openmsx::FloatSetting::getDouble
double getDouble() const noexcept
Definition: FloatSetting.hh:20
InputEvents.hh
openmsx::EventDelay::sync
void sync(EmuTime::param curEmu)
Definition: EventDelay.cc:87
openmsx::InputEventFactory::EventPtr
std::shared_ptr< const Event > EventPtr
Definition: InputEventFactory.hh:15
openmsx::EventDelay::~EventDelay
~EventDelay()
Definition: EventDelay.cc:54
checked_cast.hh
openmsx::Schedulable::getCurrentTime
EmuTime::param getCurrentTime() const
Convenience method: This is the same as getScheduler().getCurrentTime().
Definition: Schedulable.cc:49
openmsx::ReverseManager
Definition: ReverseManager.hh:27
openmsx::OPENMSX_MOUSE_BUTTON_DOWN_EVENT
@ OPENMSX_MOUSE_BUTTON_DOWN_EVENT
Definition: Event.hh:18
detail::append
void append(Result &)
Definition: stl.hh:340
stl.hh
ranges::find_if
auto find_if(InputRange &&range, UnaryPredicate pred)
Definition: ranges.hh:113
openmsx::OPENMSX_KEY_UP_EVENT
@ OPENMSX_KEY_UP_EVENT
Definition: Event.hh:12
openmsx::OPENMSX_JOY_BUTTON_DOWN_EVENT
@ OPENMSX_JOY_BUTTON_DOWN_EVENT
Definition: Event.hh:27
openmsx::EventDistributor::registerEventListener
void registerEventListener(EventType type, EventListener &listener, Priority priority=OTHER)
Registers a given object to receive certain events.
Definition: EventDistributor.cc:23
openmsx::EventDistributor::unregisterEventListener
void unregisterEventListener(EventType type, EventListener &listener)
Unregisters a previously registered event listener.
Definition: EventDistributor.cc:35
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::Keys::K_MASK
@ K_MASK
Definition: Keys.hh:27
openmsx::OPENMSX_JOY_BUTTON_UP_EVENT
@ OPENMSX_JOY_BUTTON_UP_EVENT
Definition: Event.hh:26
openmsx::EventDelay::flush
void flush()
Definition: EventDelay.cc:185