openMSX
EventDelay.cc
Go to the documentation of this file.
1#include "EventDelay.hh"
2#include "EventDistributor.hh"
4#include "ReverseManager.hh"
5#include "Event.hh"
6#include "Timer.hh"
7#include "MSXException.hh"
8#include "narrow.hh"
9#include "one_of.hh"
10#include "ranges.hh"
11#include "stl.hh"
12#include <cassert>
13
14#include <SDL.h>
15
16namespace 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(
29 commandController, "inputdelay",
30 "delay input to avoid key-skips", 0.0, 0.0, 10.0)
31{
32 for (auto type : {
42 eventDistributor.registerEventListener(type, *this, EventDistributor::MSX);
43 }
44
45 reverseManager.registerEventDelay(*this);
46}
47
63
64int EventDelay::signalEvent(const Event& event)
65{
66 toBeScheduledEvents.push_back(event);
67 if (delaySetting.getDouble() == 0.0) {
69 }
70 return 0;
71}
72
73void EventDelay::sync(EmuTime::param curEmu)
74{
75 auto curRealTime = Timer::getTime();
76 auto realDuration = curRealTime - prevReal;
77 prevReal = curRealTime;
78 auto emuDuration = curEmu - prevEmu;
79 prevEmu = curEmu;
80
81 double factor = emuDuration.toDouble() / narrow_cast<double>(realDuration);
82 EmuDuration extraDelay(delaySetting.getDouble());
83
84#if PLATFORM_ANDROID
85 // The virtual keyboard on Android sends a key press and the
86 // corresponding key release event directly after each other, without a
87 // delay. It sends both events either when the user has finished a
88 // short tap or alternatively after the user has hold the button
89 // pressed for a few seconds and then has selected the appropriate
90 // character from the multi-character-popup that the virtual keyboard
91 // displays when the user holds a button pressed for a short while.
92 // Either way, the key release event comes way too short after the
93 // press event for the MSX to process it. The two events follow each
94 // other within a few milliseconds at most. Therefore, on Android,
95 // special logic must be foreseen to delay the release event for a
96 // short while. This special logic correlates each key release event
97 // with the corresponding press event for the same key. If they are
98 // less then 2/50 second apart, the release event gets delayed until
99 // the next sync call. The 2/50 second has been chosen because it can
100 // take up to 2 vertical interrupts (2 screen refreshes) for the MSX to
101 // see the key press in the keyboard matrix, thus, 2/50 seconds is the
102 // minimum delay required for an MSX running in PAL mode.
103 std::vector<Event> toBeRescheduledEvents;
104#endif
105
106 EmuTime time = curEmu + extraDelay;
107 for (auto& e : toBeScheduledEvents) {
108#if PLATFORM_ANDROID
110 const auto& keyEvent = get_event<KeyEvent>(e);
111 int maskedKeyCode = int(keyEvent.getKeyCode()) & int(Keys::K_MASK);
112 auto it = ranges::find(nonMatchedKeyPresses, maskedKeyCode,
113 [](const auto& p) { return p.first; });
114 if (getType(e) == EventType::KEY_DOWN) {
115 if (it == end(nonMatchedKeyPresses)) {
116 nonMatchedKeyPresses.emplace_back(maskedKeyCode, e);
117 } else {
118 it->second = e;
119 }
120 } else {
121 if (it != end(nonMatchedKeyPresses)) {
122 const auto& timedPressEvent = get_event<TimedEvent>(it->second);
123 const auto& timedReleaseEvent = get_event<TimedEvent>(e);
124 auto pressRealTime = timedPressEvent.getRealTime();
125 auto releaseRealTime = timedReleaseEvent.getRealTime();
126 auto deltaTime = releaseRealTime - pressRealTime;
127 if (deltaTime <= 2000000 / 50) {
128 // The key release came less then 2 MSX interrupts from the key press.
129 // Reschedule it for the next sync, with the realTime updated to now, so that it seems like the
130 // key was released now and not when android released it.
131 // Otherwise, the offset calculation for the emuTime further down below will go wrong on the next sync
132 Event newKeyupEvent = KeyUpEvent(keyEvent.getKeyCode());
133 toBeRescheduledEvents.push_back(newKeyupEvent);
134 continue; // continue with next to be scheduled event
135 }
136 move_pop_back(nonMatchedKeyPresses, it);
137 }
138 }
139 }
140#endif
141 scheduledEvents.push_back(e);
142 const auto& sdlEvent = get_event<SdlEvent>(e);
143 uint32_t eventSdlTime = sdlEvent.getCommonSdlEvent().timestamp;
144 uint32_t sdlNow = SDL_GetTicks();
145 auto sdlOffset = int32_t(sdlNow - eventSdlTime);
146 assert(sdlOffset >= 0);
147 auto offset = 1000 * int64_t(sdlOffset); // ms -> us
148 EmuDuration emuOffset(factor * narrow_cast<double>(offset));
149 auto schedTime = (emuOffset < extraDelay)
150 ? time - emuOffset
151 : curEmu;
152 assert(curEmu <= schedTime);
153 setSyncPoint(schedTime);
154 }
155 toBeScheduledEvents.clear();
156
157#if PLATFORM_ANDROID
158 append(toBeScheduledEvents, std::move(toBeRescheduledEvents));
159#endif
160}
161
162void EventDelay::executeUntil(EmuTime::param time)
163{
164 try {
165 auto event = std::move(scheduledEvents.front());
166 scheduledEvents.pop_front();
167 msxEventDistributor.distributeEvent(event, time);
168 } catch (MSXException&) {
169 // ignore
170 }
171}
172
174{
175 EmuTime time = getCurrentTime();
176
177 for (auto& e : scheduledEvents) {
178 msxEventDistributor.distributeEvent(e, time);
179 }
180 scheduledEvents.clear();
181
182 for (auto& e : toBeScheduledEvents) {
183 msxEventDistributor.distributeEvent(e, time);
184 }
185 toBeScheduledEvents.clear();
186
188}
189
190} // namespace openmsx
void sync(EmuTime::param curEmu)
Definition EventDelay.cc:73
EventDelay(Scheduler &scheduler, CommandController &commandController, EventDistributor &eventDistributor, MSXEventDistributor &msxEventDistributor, ReverseManager &reverseManager)
Definition EventDelay.cc:18
void unregisterEventListener(EventType type, EventListener &listener)
Unregisters a previously registered event listener.
void registerEventListener(EventType type, EventListener &listener, Priority priority=OTHER)
Registers a given object to receive certain events.
double getDouble() const noexcept
void distributeEvent(const Event &event, EmuTime::param time)
Deliver the event to all registered listeners.
void registerEventDelay(EventDelay &eventDelay_)
Every class that wants to get scheduled at some point must inherit from this class.
void setSyncPoint(EmuTime::param timestamp)
EmuTime::param getCurrentTime() const
Convenience method: This is the same as getScheduler().getCurrentTime().
uint64_t getTime()
Get current (real) time in us.
Definition Timer.cc:7
This file implemented 3 utility functions:
Definition Autofire.cc:11
EventType getType(const Event &event)
Definition Event.hh:516
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:444
auto find(InputRange &&range, const T &value)
Definition ranges.hh:160
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
Definition stl.hh:134
constexpr auto end(const zstring_view &x)