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