| 1 |
#include "Mouse.hh" |
| 2 |
|
| 3 |
#include "Event.hh" |
| 4 |
#include "MSXEventDistributor.hh" |
| 5 |
#include "StateChange.hh" |
| 6 |
#include "StateChangeDistributor.hh" |
| 7 |
|
| 8 |
#include "Clock.hh" |
| 9 |
#include "serialize.hh" |
| 10 |
#include "serialize_meta.hh" |
| 11 |
|
| 12 |
#include "Display.hh" |
| 13 |
|
| 14 |
#include "gl_vec.hh" |
| 15 |
|
| 16 |
#include "unreachable.hh" |
| 17 |
|
| 18 |
#include <SDL.h> |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <cassert> |
| 22 |
|
| 23 |
namespace openmsx { |
| 24 |
|
| 25 |
static constexpr int THRESHOLD = 2; |
| 26 |
static constexpr int SCALE = 2; |
| 27 |
static constexpr int PHASE_XHIGH1 = 0; |
| 28 |
static constexpr int PHASE_XLOW1 = 1; |
| 29 |
static constexpr int PHASE_YHIGH1 = 2; |
| 30 |
static constexpr int PHASE_YLOW1 = 3; |
| 31 |
static constexpr int PHASE_XHIGH2 = 4; |
| 32 |
static constexpr int PHASE_XLOW2 = 5; |
| 33 |
static constexpr int PHASE_YHIGH2 = 6; |
| 34 |
static constexpr int PHASE_YLOW2 = 7; |
| 35 |
static constexpr int STROBE = 0x04; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static gl::ivec2 scaleHostMotion(gl::ivec2 delta, gl::vec2 scale, gl::vec2& fraction) |
| 44 |
{ |
| 45 |
assert((scale.x > 0.0f) && (scale.y > 0.0f)); |
| 46 |
auto value = gl::vec2(delta) + fraction; |
| 47 |
auto result = floor(value / scale); |
| 48 |
fraction = value - (gl::vec2(result) * scale); |
| 49 |
return result; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
Mouse::Mouse(MSXEventDistributor& eventDistributor_, |
| 54 |
StateChangeDistributor& stateChangeDistributor_, |
| 55 |
Display& display_) |
| 56 |
: eventDistributor(eventDistributor_) |
| 57 |
, stateChangeDistributor(stateChangeDistributor_) |
| 58 |
, display(display_) |
| 59 |
, phase(PHASE_YLOW2) |
| 60 |
{ |
| 61 |
} |
| 62 |
|
| 63 |
Mouse::~Mouse() |
| 64 |
{ |
| 65 |
if (isPluggedIn()) { |
| 66 |
Mouse::unplugHelper(EmuTime::dummy()); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
zstring_view Mouse::getName() const |
| 73 |
{ |
| 74 |
return "mouse"; |
| 75 |
} |
| 76 |
|
| 77 |
zstring_view Mouse::getDescription() const |
| 78 |
{ |
| 79 |
return "MSX mouse"; |
| 80 |
} |
| 81 |
|
| 82 |
void Mouse::plugHelper(Connector& , EmuTime time) |
| 83 |
{ |
| 84 |
if (SDL_GetMouseState(nullptr, nullptr) & SDL_BUTTON(SDL_BUTTON_LEFT)) { |
| 85 |
|
| 86 |
mouseMode = false; |
| 87 |
} else { |
| 88 |
|
| 89 |
mouseMode = true; |
| 90 |
lastTime = time; |
| 91 |
} |
| 92 |
plugHelper2(); |
| 93 |
} |
| 94 |
|
| 95 |
void Mouse::plugHelper2() |
| 96 |
{ |
| 97 |
eventDistributor.registerEventListener(*this); |
| 98 |
stateChangeDistributor.registerListener(*this); |
| 99 |
} |
| 100 |
|
| 101 |
void Mouse::unplugHelper(EmuTime ) |
| 102 |
{ |
| 103 |
stateChangeDistributor.unregisterListener(*this); |
| 104 |
eventDistributor.unregisterEventListener(*this); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
uint8_t Mouse::read(EmuTime ) |
| 109 |
{ |
| 110 |
if (mouseMode) { |
| 111 |
switch (phase) { |
| 112 |
case PHASE_XHIGH1: case PHASE_XHIGH2: |
| 113 |
return ((xRel >> 4) & 0x0F) | status; |
| 114 |
case PHASE_XLOW1: case PHASE_XLOW2: |
| 115 |
return (xRel & 0x0F) | status; |
| 116 |
case PHASE_YHIGH1: case PHASE_YHIGH2: |
| 117 |
return ((yRel >> 4) & 0x0F) | status; |
| 118 |
case PHASE_YLOW1: case PHASE_YLOW2: |
| 119 |
return (yRel & 0x0F) | status; |
| 120 |
default: |
| 121 |
UNREACHABLE; |
| 122 |
} |
| 123 |
} else { |
| 124 |
emulateJoystick(); |
| 125 |
return status; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
void Mouse::emulateJoystick() |
| 130 |
{ |
| 131 |
status &= ~(JOY_UP | JOY_DOWN | JOY_LEFT | JOY_RIGHT); |
| 132 |
|
| 133 |
int deltaX = curXRel; curXRel = 0; |
| 134 |
int deltaY = curYRel; curYRel = 0; |
| 135 |
int absX = (deltaX > 0) ? deltaX : -deltaX; |
| 136 |
int absY = (deltaY > 0) ? deltaY : -deltaY; |
| 137 |
|
| 138 |
if ((absX < THRESHOLD) && (absY < THRESHOLD)) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
if (deltaX > 0) { |
| 144 |
if (deltaY > 0) { |
| 145 |
if ((12 * absX) > (5 * absY)) { |
| 146 |
status |= JOY_RIGHT; |
| 147 |
} |
| 148 |
if ((12 * absY) > (5 * absX)) { |
| 149 |
status |= JOY_DOWN; |
| 150 |
} |
| 151 |
} else { |
| 152 |
if ((12 * absX) > (5 * absY)) { |
| 153 |
status |= JOY_RIGHT; |
| 154 |
} |
| 155 |
if ((12 * absY) > (5 * absX)) { |
| 156 |
status |= JOY_UP; |
| 157 |
} |
| 158 |
} |
| 159 |
} else { |
| 160 |
if (deltaY > 0) { |
| 161 |
if ((12 * absX) > (5 * absY)) { |
| 162 |
status |= JOY_LEFT; |
| 163 |
} |
| 164 |
if ((12 * absY) > (5 * absX)) { |
| 165 |
status |= JOY_DOWN; |
| 166 |
} |
| 167 |
} else { |
| 168 |
if ((12 * absX) > (5 * absY)) { |
| 169 |
status |= JOY_LEFT; |
| 170 |
} |
| 171 |
if ((12 * absY) > (5 * absX)) { |
| 172 |
status |= JOY_UP; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
void Mouse::write(uint8_t value, EmuTime time) |
| 179 |
{ |
| 180 |
if (mouseMode) { |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
if ((time - lastTime) > EmuDuration::usec(1500)) { |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
phase = PHASE_YLOW2; |
| 201 |
} |
| 202 |
lastTime = time; |
| 203 |
|
| 204 |
switch (phase) { |
| 205 |
case PHASE_XHIGH1: case PHASE_XHIGH2: |
| 206 |
case PHASE_YHIGH1: case PHASE_YHIGH2: |
| 207 |
if ((value & STROBE) == 0) ++phase; |
| 208 |
break; |
| 209 |
case PHASE_XLOW1: case PHASE_XLOW2: |
| 210 |
if ((value & STROBE) != 0) ++phase; |
| 211 |
break; |
| 212 |
case PHASE_YLOW1: |
| 213 |
if ((value & STROBE) != 0) { |
| 214 |
phase = PHASE_XHIGH2; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
xRel = yRel = 0; |
| 219 |
} |
| 220 |
break; |
| 221 |
case PHASE_YLOW2: |
| 222 |
if ((value & STROBE) != 0) { |
| 223 |
phase = PHASE_XHIGH1; |
| 224 |
#if 0 |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
#else |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
xRel = std::clamp(curXRel, -127, 127); |
| 238 |
yRel = std::clamp(curYRel, -127, 127); |
| 239 |
curXRel -= xRel; |
| 240 |
curYRel -= yRel; |
| 241 |
#endif |
| 242 |
} |
| 243 |
break; |
| 244 |
} |
| 245 |
} else { |
| 246 |
|
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
void Mouse::signalMSXEvent(const Event& event, EmuTime time) noexcept |
| 253 |
{ |
| 254 |
visit(overloaded{ |
| 255 |
[&](const MouseMotionEvent& e) { |
| 256 |
if (e.getX() || e.getY()) { |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
auto scale = display.getMsxPixelSize().value_or(gl::vec2(SCALE)); |
| 262 |
auto delta = scaleHostMotion(gl::ivec2(e.getX(), e.getY()), |
| 263 |
scale, fractional); |
| 264 |
|
| 265 |
|
| 266 |
createMouseStateChange(time, -delta.x, -delta.y, 0, 0); |
| 267 |
} |
| 268 |
}, |
| 269 |
[&](const MouseButtonDownEvent& e) { |
| 270 |
switch (e.getButton()) { |
| 271 |
case SDL_BUTTON_LEFT: |
| 272 |
createMouseStateChange(time, 0, 0, JOY_BUTTONA, 0); |
| 273 |
break; |
| 274 |
case SDL_BUTTON_RIGHT: |
| 275 |
createMouseStateChange(time, 0, 0, JOY_BUTTONB, 0); |
| 276 |
break; |
| 277 |
default: |
| 278 |
|
| 279 |
break; |
| 280 |
} |
| 281 |
}, |
| 282 |
[&](const MouseButtonUpEvent& e) { |
| 283 |
switch (e.getButton()) { |
| 284 |
case SDL_BUTTON_LEFT: |
| 285 |
createMouseStateChange(time, 0, 0, 0, JOY_BUTTONA); |
| 286 |
break; |
| 287 |
case SDL_BUTTON_RIGHT: |
| 288 |
createMouseStateChange(time, 0, 0, 0, JOY_BUTTONB); |
| 289 |
break; |
| 290 |
default: |
| 291 |
|
| 292 |
break; |
| 293 |
} |
| 294 |
}, |
| 295 |
[](const EventBase&) { } |
| 296 |
}, event); |
| 297 |
} |
| 298 |
|
| 299 |
void Mouse::createMouseStateChange( |
| 300 |
EmuTime time, int deltaX, int deltaY, uint8_t press, uint8_t release) |
| 301 |
{ |
| 302 |
stateChangeDistributor.distributeNew<MouseState>( |
| 303 |
time, deltaX, deltaY, press, release); |
| 304 |
} |
| 305 |
|
| 306 |
void Mouse::signalStateChange(const StateChange& event) |
| 307 |
{ |
| 308 |
const auto* ms = std::get_if<MouseState>(&event); |
| 309 |
if (!ms) return; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
curXRel += ms->getDeltaX(); |
| 314 |
curYRel += ms->getDeltaY(); |
| 315 |
status = (status & ~ms->getPress()) | ms->getRelease(); |
| 316 |
} |
| 317 |
|
| 318 |
void Mouse::stopReplay(EmuTime time) noexcept |
| 319 |
{ |
| 320 |
|
| 321 |
int dx = 0 - curXRel; |
| 322 |
int dy = 0 - curYRel; |
| 323 |
uint8_t release = (JOY_BUTTONA | JOY_BUTTONB) & ~status; |
| 324 |
if ((dx != 0) || (dy != 0) || (release != 0)) { |
| 325 |
createMouseStateChange(time, dx, dy, 0, release); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
template<typename Archive> |
| 336 |
void Mouse::serialize(Archive& ar, unsigned version) |
| 337 |
{ |
| 338 |
|
| 339 |
MouseState::absHostX = MouseState::absHostY = 0; |
| 340 |
|
| 341 |
if constexpr (Archive::IS_LOADER) { |
| 342 |
if (isPluggedIn()) { |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
plugHelper2(); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
if (ar.versionBelow(version, 4)) { |
| 351 |
assert(Archive::IS_LOADER); |
| 352 |
Clock<1000> tmp(EmuTime::zero()); |
| 353 |
ar.serialize("lastTime", tmp); |
| 354 |
lastTime = tmp.getTime(); |
| 355 |
} else { |
| 356 |
ar.serialize("lastTime", lastTime); |
| 357 |
} |
| 358 |
ar.serialize("faze", phase, |
| 359 |
|
| 360 |
"xrel", xRel, |
| 361 |
"yrel", yRel, |
| 362 |
"mouseMode", mouseMode); |
| 363 |
if (ar.versionAtLeast(version, 2)) { |
| 364 |
ar.serialize("curxrel", curXRel, |
| 365 |
"curyrel", curYRel, |
| 366 |
"status", status); |
| 367 |
} |
| 368 |
if (ar.versionBelow(version, 3)) { |
| 369 |
xRel /= SCALE; |
| 370 |
yRel /= SCALE; |
| 371 |
curXRel /= SCALE; |
| 372 |
curYRel /= SCALE; |
| 373 |
|
| 374 |
} |
| 375 |
|
| 376 |
} |
| 377 |
INSTANTIATE_SERIALIZE_METHODS(Mouse); |
| 378 |
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, Mouse, "Mouse"); |
| 379 |
|
| 380 |
} |