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 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 eventDistributor.registerEventListener(
52
53 reverseManager.registerEventDelay(*this);
54}
55
57{
58 eventDistributor.unregisterEventListener(
59 EventType::KEY_DOWN, *this);
60 eventDistributor.unregisterEventListener(
61 EventType::KEY_UP, *this);
62
63 eventDistributor.unregisterEventListener(
65 eventDistributor.unregisterEventListener(
67 eventDistributor.unregisterEventListener(
69
70 eventDistributor.unregisterEventListener(
72 eventDistributor.unregisterEventListener(
73 EventType::JOY_HAT, *this);
74 eventDistributor.unregisterEventListener(
76 eventDistributor.unregisterEventListener(
78}
79
80int EventDelay::signalEvent(const Event& event)
81{
82 toBeScheduledEvents.push_back(event);
83 if (delaySetting.getDouble() == 0.0) {
85 }
86 return 0;
87}
88
89void EventDelay::sync(EmuTime::param curEmu)
90{
91 auto curRealTime = Timer::getTime();
92 auto realDuration = curRealTime - prevReal;
93 prevReal = curRealTime;
94 auto emuDuration = curEmu - prevEmu;
95 prevEmu = curEmu;
96
97 double factor = emuDuration.toDouble() / narrow_cast<double>(realDuration);
98 EmuDuration extraDelay(delaySetting.getDouble());
99
100#if PLATFORM_ANDROID
101 // The virtual keyboard on Android sends a key press and the
102 // corresponding key release event directly after each other, without a
103 // delay. It sends both events either when the user has finished a
104 // short tap or alternatively after the user has hold the button
105 // pressed for a few seconds and then has selected the appropriate
106 // character from the multi-character-popup that the virtual keyboard
107 // displays when the user holds a button pressed for a short while.
108 // Either way, the key release event comes way too short after the
109 // press event for the MSX to process it. The two events follow each
110 // other within a few milliseconds at most. Therefore, on Android,
111 // special logic must be foreseen to delay the release event for a
112 // short while. This special logic correlates each key release event
113 // with the corresponding press event for the same key. If they are
114 // less then 2/50 second apart, the release event gets delayed until
115 // the next sync call. The 2/50 second has been chosen because it can
116 // take up to 2 vertical interrupts (2 screen refreshes) for the MSX to
117 // see the key press in the keyboard matrix, thus, 2/50 seconds is the
118 // minimum delay required for an MSX running in PAL mode.
119 std::vector<Event> toBeRescheduledEvents;
120#endif
121
122 EmuTime time = curEmu + extraDelay;
123 for (auto& e : toBeScheduledEvents) {
124#if PLATFORM_ANDROID
126 const auto& keyEvent = get_event<KeyEvent>(e);
127 int maskedKeyCode = int(keyEvent.getKeyCode()) & int(Keys::K_MASK);
128 auto it = ranges::find(nonMatchedKeyPresses, maskedKeyCode,
129 [](const auto& p) { return p.first; });
130 if (getType(e) == EventType::KEY_DOWN) {
131 if (it == end(nonMatchedKeyPresses)) {
132 nonMatchedKeyPresses.emplace_back(maskedKeyCode, e);
133 } else {
134 it->second = e;
135 }
136 } else {
137 if (it != end(nonMatchedKeyPresses)) {
138 const auto& timedPressEvent = get_event<TimedEvent>(it->second);
139 const auto& timedReleaseEvent = get_event<TimedEvent>(e);
140 auto pressRealTime = timedPressEvent.getRealTime();
141 auto releaseRealTime = timedReleaseEvent.getRealTime();
142 auto deltaTime = releaseRealTime - pressRealTime;
143 if (deltaTime <= 2000000 / 50) {
144 // The key release came less then 2 MSX interrupts from the key press.
145 // Reschedule it for the next sync, with the realTime updated to now, so that it seems like the
146 // key was released now and not when android released it.
147 // Otherwise, the offset calculation for the emuTime further down below will go wrong on the next sync
148 Event newKeyupEvent = KeyUpEvent(keyEvent.getKeyCode());
149 toBeRescheduledEvents.push_back(newKeyupEvent);
150 continue; // continue with next to be scheduled event
151 }
152 move_pop_back(nonMatchedKeyPresses, it);
153 }
154 }
155 }
156#endif
157 scheduledEvents.push_back(e);
158 const auto& sdlEvent = get_event<SdlEvent>(e);
159 uint32_t eventSdlTime = sdlEvent.getCommonSdlEvent().timestamp;
160 uint32_t sdlNow = SDL_GetTicks();
161 auto sdlOffset = int32_t(sdlNow - eventSdlTime);
162 assert(sdlOffset >= 0);
163 auto offset = 1000 * int64_t(sdlOffset); // ms -> us
164 EmuDuration emuOffset(factor * narrow_cast<double>(offset));
165 auto schedTime = (emuOffset < extraDelay)
166 ? time - emuOffset
167 : curEmu;
168 assert(curEmu <= schedTime);
169 setSyncPoint(schedTime);
170 }
171 toBeScheduledEvents.clear();
172
173#if PLATFORM_ANDROID
174 append(toBeScheduledEvents, std::move(toBeRescheduledEvents));
175#endif
176}
177
178void EventDelay::executeUntil(EmuTime::param time)
179{
180 try {
181 auto event = std::move(scheduledEvents.front());
182 scheduledEvents.pop_front();
183 msxEventDistributor.distributeEvent(event, time);
184 } catch (MSXException&) {
185 // ignore
186 }
187}
188
190{
191 EmuTime time = getCurrentTime();
192
193 for (auto& e : scheduledEvents) {
194 msxEventDistributor.distributeEvent(e, time);
195 }
196 scheduledEvents.clear();
197
198 for (auto& e : toBeScheduledEvents) {
199 msxEventDistributor.distributeEvent(e, time);
200 }
201 toBeScheduledEvents.clear();
202
204}
205
206} // namespace openmsx
Definition: one_of.hh:7
void sync(EmuTime::param curEmu)
Definition: EventDelay.cc:89
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
Definition: FloatSetting.hh:20
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.
Definition: Schedulable.hh:34
void setSyncPoint(EmuTime::param timestamp)
Definition: Schedulable.cc:23
EmuTime::param getCurrentTime() const
Convenience method: This is the same as getScheduler().getCurrentTime().
Definition: Schedulable.cc:49
constexpr double e
Definition: Math.hh:21
uint64_t getTime()
Get current (real) time in us.
Definition: Timer.cc:7
This file implemented 3 utility functions:
Definition: Autofire.cc:9
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, ImGuiDelayedActionEvent, ImGuiActiveEvent > Event
Definition: Event.hh:450
EventType getType(const Event &event)
Definition: Event.hh:521
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)