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 "ranges.hh"
10 #include "stl.hh"
11 #include <cassert>
12 
13 namespace openmsx {
14 
16  CommandController& commandController,
17  EventDistributor& eventDistributor_,
18  MSXEventDistributor& msxEventDistributor_,
19  ReverseManager& reverseManager)
20  : Schedulable(scheduler_)
21  , eventDistributor(eventDistributor_)
22  , msxEventDistributor(msxEventDistributor_)
23  , prevEmu(EmuTime::zero)
24  , prevReal(Timer::getTime())
25  , delaySetting(
26  commandController, "inputdelay",
27  "delay input to avoid key-skips", 0.0, 0.0, 10.0)
28 {
29  eventDistributor.registerEventListener(
31  eventDistributor.registerEventListener(
33 
34  eventDistributor.registerEventListener(
36  eventDistributor.registerEventListener(
38  eventDistributor.registerEventListener(
40 
41  eventDistributor.registerEventListener(
43  eventDistributor.registerEventListener(
45  eventDistributor.registerEventListener(
47  eventDistributor.registerEventListener(
49 
50  reverseManager.registerEventDelay(*this);
51 }
52 
54 {
55  eventDistributor.unregisterEventListener(
56  OPENMSX_KEY_DOWN_EVENT, *this);
57  eventDistributor.unregisterEventListener(
58  OPENMSX_KEY_UP_EVENT, *this);
59 
60  eventDistributor.unregisterEventListener(
62  eventDistributor.unregisterEventListener(
64  eventDistributor.unregisterEventListener(
66 
67  eventDistributor.unregisterEventListener(
69  eventDistributor.unregisterEventListener(
70  OPENMSX_JOY_HAT_EVENT, *this);
71  eventDistributor.unregisterEventListener(
73  eventDistributor.unregisterEventListener(
75 }
76 
77 int EventDelay::signalEvent(const EventPtr& event)
78 {
79  toBeScheduledEvents.push_back(event);
80  if (delaySetting.getDouble() == 0.0) {
82  }
83  return 0;
84 }
85 
86 void EventDelay::sync(EmuTime::param curEmu)
87 {
88  auto curRealTime = Timer::getTime();
89  auto realDuration = curRealTime - prevReal;
90  prevReal = curRealTime;
91  auto emuDuration = curEmu - prevEmu;
92  prevEmu = curEmu;
93 
94  double factor = emuDuration.toDouble() / realDuration;
95  EmuDuration extraDelay(delaySetting.getDouble());
96 
97 #if PLATFORM_ANDROID
98  // The virtual keyboard on Android sends a key press and the
99  // corresponding key release event directly after each other, without a
100  // delay. It sends both events either when the user has finished a
101  // short tap or alternatively after the user has hold the button
102  // pressed for a few seconds and then has selected the appropriate
103  // character from the multi-character-popup that the virtual keyboard
104  // displays when the user holds a button pressed for a short while.
105  // Either way, the key release event comes way too short after the
106  // press event for the MSX to process it. The two events follow each
107  // other within a few milliseconds at most. Therefore, on Android,
108  // special logic must be foreseen to delay the release event for a
109  // short while. This special logic correlates each key release event
110  // with the corresponding press event for the same key. If they are
111  // less then 2/50 second apart, the release event gets delayed until
112  // the next sync call. The 2/50 second has been chosen because it can
113  // take up to 2 vertical interrupts (2 screen refreshes) for the MSX to
114  // see the key press in the keyboard matrix, thus, 2/50 seconds is the
115  // minimum delay required for an MSX running in PAL mode.
116  std::vector<EventPtr> toBeRescheduledEvents;
117 #endif
118 
119  EmuTime time = curEmu + extraDelay;
120  for (auto& e : toBeScheduledEvents) {
121 #if PLATFORM_ANDROID
122  if (e->getType() == OPENMSX_KEY_DOWN_EVENT ||
123  e->getType() == 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
void sync(EmuTime::param curEmu)
Definition: EventDelay.cc:86
void registerEventListener(EventType type, EventListener &listener, Priority priority=OTHER)
Registers a given object to receive certain events.
void registerEventDelay(EventDelay &eventDelay_)
void unregisterEventListener(EventType type, EventListener &listener)
Unregisters a previously registered event listener.
EmuTime::param getCurrentTime() const
Convenience method: This is the same as getScheduler().getCurrentTime().
Definition: Schedulable.cc:49
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
Definition: stl.hh:191
void distributeEvent(const EventPtr &event, EmuTime::param time)
Deliver the event to all registered listeners.
auto find_if(InputRange &&range, UnaryPredicate pred)
Definition: ranges.hh:113
double getDouble() const noexcept
Definition: FloatSetting.hh:20
void append(Result &)
Definition: stl.hh:354
Every class that wants to get scheduled at some point must inherit from this class.
Definition: Schedulable.hh:33
EventDelay(Scheduler &scheduler, CommandController &commandController, EventDistributor &eventDistributor, MSXEventDistributor &msxEventDistributor, ReverseManager &reverseManager)
Definition: EventDelay.cc:15
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
uint64_t getRealTime() const
Query creation time.
Definition: InputEvents.hh:17
uint64_t getTime()
Get current (real) time in us.
Definition: Timer.cc:8
void setSyncPoint(EmuTime::param timestamp)
Definition: Schedulable.cc:23
auto end(const string_view &x)
Definition: string_view.hh:152