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