| e8555c89 Andy McCall |
1 |
#include "InputEventGenerator.hh" |
| 2 |
|
| 3 |
#include "Event.hh" |
| 4 |
#include "EventDistributor.hh" |
| 5 |
#include "FileOperations.hh" |
| 6 |
#include "GlobalSettings.hh" |
| 7 |
#include "IntegerSetting.hh" |
| 8 |
#include "SDLKey.hh" |
| 9 |
|
| 10 |
#include "one_of.hh" |
| 11 |
#include "outer.hh" |
| 12 |
#include "unreachable.hh" |
| 13 |
#include "utf8_unchecked.hh" |
| 14 |
|
| 15 |
#include <utility> |
| 16 |
|
| 17 |
namespace openmsx { |
| 18 |
|
| 19 |
InputEventGenerator::InputEventGenerator(CommandController& commandController, |
| 20 |
EventDistributor& eventDistributor_) |
| 21 |
: eventDistributor(eventDistributor_) |
| 22 |
, joystickManager(eventDistributor.getReactor()) |
| 23 |
, grabInput( |
| 24 |
commandController, "grabinput", |
| 25 |
"This setting controls if openMSX takes over mouse and keyboard input", |
| 26 |
false, Setting::Save::NO) |
| 27 |
, escapeGrabCmd(commandController) |
| 28 |
{ |
| 29 |
eventDistributor.registerEventListener(EventType::WINDOW, *this); |
| 30 |
} |
| 31 |
|
| 32 |
InputEventGenerator::~InputEventGenerator() |
| 33 |
{ |
| 34 |
eventDistributor.unregisterEventListener(EventType::WINDOW, *this); |
| 35 |
} |
| 36 |
|
| 37 |
void InputEventGenerator::wait() |
| 38 |
{ |
| 39 |
|
| 40 |
if (!SDL_WasInit(SDL_INIT_VIDEO)) { |
| 41 |
SDL_Delay(100); |
| 42 |
} |
| 43 |
|
| 44 |
if (SDL_WaitEvent(nullptr)) { |
| 45 |
poll(); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
void InputEventGenerator::poll(std::optional<int> timeoutMs) |
| 50 |
{ |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
SDL_Event event1, event2; |
| 81 |
auto* prev = &event1; |
| 82 |
auto* curr = &event2; |
| 83 |
bool pending = false; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
auto getNextEvent = [&] { |
| 89 |
return timeoutMs |
| 90 |
? SDL_WaitEventTimeout(curr, *std::exchange(timeoutMs, {})) |
| 91 |
: SDL_PollEvent(curr); |
| 92 |
}; |
| 93 |
|
| 94 |
while (getNextEvent()) { |
| 95 |
|
| 96 |
|
| 97 |
if (curr->type == SDL_USEREVENT) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
if (pending) { |
| 101 |
pending = false; |
| 102 |
if ((prev->type == SDL_KEYDOWN) && (curr->type == SDL_TEXTINPUT)) { |
| 103 |
const char* utf8 = curr->text.text; |
| 104 |
auto unicode = utf8::unchecked::next(utf8); |
| 105 |
handleKeyDown(prev->key, unicode); |
| 106 |
if (unicode) { |
| 107 |
splitText(curr->text.timestamp, utf8); |
| 108 |
} |
| 109 |
eventDistributor.distributeEvent(TextEvent(*curr)); |
| 110 |
continue; |
| 111 |
} else { |
| 112 |
handle(*prev); |
| 113 |
} |
| 114 |
} |
| 115 |
if (curr->type == SDL_KEYDOWN) { |
| 116 |
pending = true; |
| 117 |
std::swap(curr, prev); |
| 118 |
} else { |
| 119 |
handle(*curr); |
| 120 |
} |
| 121 |
} |
| 122 |
if (pending) { |
| 123 |
handle(*prev); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
void InputEventGenerator::setNewOsdControlButtonState(unsigned newState) |
| 128 |
{ |
| 129 |
unsigned deltaState = osdControlButtonsState ^ newState; |
| 130 |
using enum OsdControlEvent::Button; |
| 131 |
for (auto b : {LEFT, RIGHT, UP, DOWN, A, B}) { |
| 132 |
if (deltaState & (1 << std::to_underlying(b))) { |
| 133 |
if (newState & (1 << std::to_underlying(b))) { |
| 134 |
eventDistributor.distributeEvent(OsdControlReleaseEvent(b)); |
| 135 |
} else { |
| 136 |
eventDistributor.distributeEvent(OsdControlPressEvent(b)); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
osdControlButtonsState = newState; |
| 141 |
} |
| 142 |
|
| 143 |
void InputEventGenerator::triggerOsdControlEventsFromJoystickAxisMotion( |
| 144 |
unsigned axis, int value) |
| 145 |
{ |
| 146 |
auto [neg_button, pos_button] = [&] { |
| 147 |
switch (axis) { |
| 148 |
using enum OsdControlEvent::Button; |
| 149 |
case 0: |
| 150 |
return std::pair{1u << std::to_underlying(LEFT), |
| 151 |
1u << std::to_underlying(RIGHT)}; |
| 152 |
case 1: |
| 153 |
return std::pair{1u << std::to_underlying(UP), |
| 154 |
1u << std::to_underlying(DOWN)}; |
| 155 |
default: |
| 156 |
|
| 157 |
|
| 158 |
return std::pair{0u, 0u}; |
| 159 |
} |
| 160 |
}(); |
| 161 |
|
| 162 |
if (value > 0) { |
| 163 |
|
| 164 |
setNewOsdControlButtonState( |
| 165 |
(osdControlButtonsState | neg_button) & ~pos_button); |
| 166 |
} else if (value < 0) { |
| 167 |
|
| 168 |
setNewOsdControlButtonState( |
| 169 |
(osdControlButtonsState | pos_button) & ~neg_button); |
| 170 |
} else { |
| 171 |
|
| 172 |
setNewOsdControlButtonState( |
| 173 |
osdControlButtonsState | neg_button | pos_button); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
void InputEventGenerator::triggerOsdControlEventsFromJoystickHat(int value) |
| 178 |
{ |
| 179 |
using enum OsdControlEvent::Button; |
| 180 |
unsigned dir = 0; |
| 181 |
if (!(value & SDL_HAT_UP )) dir |= 1 << std::to_underlying(UP); |
| 182 |
if (!(value & SDL_HAT_DOWN )) dir |= 1 << std::to_underlying(DOWN); |
| 183 |
if (!(value & SDL_HAT_LEFT )) dir |= 1 << std::to_underlying(LEFT); |
| 184 |
if (!(value & SDL_HAT_RIGHT)) dir |= 1 << std::to_underlying(RIGHT); |
| 185 |
unsigned ab = osdControlButtonsState & ((1 << std::to_underlying(A)) | |
| 186 |
(1 << std::to_underlying(B))); |
| 187 |
setNewOsdControlButtonState(ab | dir); |
| 188 |
} |
| 189 |
|
| 190 |
void InputEventGenerator::osdControlChangeButton(bool down, unsigned changedButtonMask) |
| 191 |
{ |
| 192 |
auto newButtonState = down |
| 193 |
? osdControlButtonsState & ~changedButtonMask |
| 194 |
: osdControlButtonsState | changedButtonMask; |
| 195 |
setNewOsdControlButtonState(newButtonState); |
| 196 |
} |
| 197 |
|
| 198 |
void InputEventGenerator::triggerOsdControlEventsFromJoystickButtonEvent(unsigned button, bool down) |
| 199 |
{ |
| 200 |
using enum OsdControlEvent::Button; |
| 201 |
osdControlChangeButton( |
| 202 |
down, |
| 203 |
((button & 1) ? (1 << std::to_underlying(B)) |
| 204 |
: (1 << std::to_underlying(A)))); |
| 205 |
} |
| 206 |
|
| 207 |
void InputEventGenerator::triggerOsdControlEventsFromKeyEvent(SDLKey key, bool repeat) |
| 208 |
{ |
| 209 |
unsigned buttonMask = [&] { |
| 210 |
switch (key.sym.sym) { |
| 211 |
using enum OsdControlEvent::Button; |
| 212 |
case SDLK_LEFT: return 1 << std::to_underlying(LEFT); |
| 213 |
case SDLK_RIGHT: return 1 << std::to_underlying(RIGHT); |
| 214 |
case SDLK_UP: return 1 << std::to_underlying(UP); |
| 215 |
case SDLK_DOWN: return 1 << std::to_underlying(DOWN); |
| 216 |
case SDLK_SPACE: return 1 << std::to_underlying(A); |
| 217 |
case SDLK_RETURN: return 1 << std::to_underlying(A); |
| 218 |
case SDLK_ESCAPE: return 1 << std::to_underlying(B); |
| 219 |
default: return 0; |
| 220 |
} |
| 221 |
}(); |
| 222 |
if (buttonMask) { |
| 223 |
if (repeat) { |
| 224 |
osdControlChangeButton(!key.down, buttonMask); |
| 225 |
} |
| 226 |
osdControlChangeButton(key.down, buttonMask); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
static constexpr Uint16 normalizeModifier(SDL_Keycode sym, Uint16 mod) |
| 231 |
{ |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
return (sym == one_of(SDLK_LCTRL, SDLK_LSHIFT, SDLK_LALT, SDLK_LGUI, |
| 238 |
SDLK_RCTRL, SDLK_RSHIFT, SDLK_RALT, SDLK_RGUI, |
| 239 |
SDLK_MODE)) |
| 240 |
? 0 |
| 241 |
: mod; |
| 242 |
} |
| 243 |
|
| 244 |
void InputEventGenerator::handleKeyDown(const SDL_KeyboardEvent& key, uint32_t unicode) |
| 245 |
{ |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
{ |
| 257 |
SDL_Event evt; |
| 258 |
evt.key = SDL_KeyboardEvent{}; |
| 259 |
memcpy(&evt.key, &key, sizeof(key)); |
| 260 |
evt.key.keysym.mod = normalizeModifier(key.keysym.sym, key.keysym.mod); |
| 261 |
evt.key.keysym.unused = unicode; |
| 262 |
Event event = KeyDownEvent(evt); |
| 263 |
triggerOsdControlEventsFromKeyEvent({.sym = key.keysym, .down = true}, key.repeat); |
| 264 |
eventDistributor.distributeEvent(std::move(event)); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
void InputEventGenerator::splitText(uint32_t timestamp, const char* utf8) |
| 269 |
{ |
| 270 |
while (true) { |
| 271 |
auto unicode = utf8::unchecked::next(utf8); |
| 272 |
if (unicode == 0) return; |
| 273 |
eventDistributor.distributeEvent( |
| 274 |
KeyDownEvent::create(timestamp, unicode)); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
void InputEventGenerator::handle(SDL_Event& evt) |
| 279 |
{ |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
auto quitOnce = [&] -> std::optional<Event> { |
| 288 |
if (sendQuit) return {}; |
| 289 |
sendQuit = true; |
| 290 |
return QuitEvent(); |
| 291 |
}; |
| 292 |
|
| 293 |
std::optional<Event> event; |
| 294 |
switch (evt.type) { |
| 295 |
case SDL_KEYUP: |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
{ |
| 312 |
SDL_Event e; |
| 313 |
e.key = SDL_KeyboardEvent{}; |
| 314 |
memcpy(&e.key, &evt.key, sizeof(evt.key)); |
| 315 |
e.key.keysym.mod = normalizeModifier(evt.key.keysym.sym, evt.key.keysym.mod); |
| 316 |
event = KeyUpEvent(e); |
| 317 |
bool down = false; |
| 318 |
bool repeat = false; |
| 319 |
triggerOsdControlEventsFromKeyEvent({.sym = e.key.keysym, .down = down}, repeat); |
| 320 |
} |
| 321 |
break; |
| 322 |
case SDL_KEYDOWN: |
| 323 |
handleKeyDown(evt.key, 0); |
| 324 |
break; |
| 325 |
|
| 326 |
case SDL_KEYMAPCHANGED: |
| 327 |
event = KeyMapChangedEvent(); |
| 328 |
break; |
| 329 |
|
| 330 |
case SDL_MOUSEBUTTONUP: |
| 331 |
event = MouseButtonUpEvent(evt); |
| 332 |
break; |
| 333 |
case SDL_MOUSEBUTTONDOWN: |
| 334 |
event = MouseButtonDownEvent(evt); |
| 335 |
break; |
| 336 |
case SDL_MOUSEWHEEL: |
| 337 |
event = MouseWheelEvent(evt); |
| 338 |
break; |
| 339 |
case SDL_MOUSEMOTION: |
| 340 |
event = MouseMotionEvent(evt); |
| 341 |
if (auto* window = SDL_GL_GetCurrentWindow(); SDL_GetWindowGrab(window)) { |
| 342 |
int w, h; |
| 343 |
SDL_GetWindowSize(window, &w, &h); |
| 344 |
int x, y; |
| 345 |
SDL_GetMouseState(&x, &y); |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
static constexpr int MARGIN = 10; |
| 359 |
int xn = std::clamp(x, MARGIN, w - 1 - MARGIN); |
| 360 |
int yn = std::clamp(y, MARGIN, h - 1 - MARGIN); |
| 361 |
if (xn != x || yn != y) SDL_WarpMouseInWindow(window, xn, yn); |
| 362 |
} |
| 363 |
break; |
| 364 |
case SDL_JOYBUTTONUP: |
| 365 |
if (joystickManager.translateSdlInstanceId(evt)) { |
| 366 |
event = JoystickButtonUpEvent(evt); |
| 367 |
triggerOsdControlEventsFromJoystickButtonEvent( |
| 368 |
evt.jbutton.button, false); |
| 369 |
} |
| 370 |
break; |
| 371 |
case SDL_JOYBUTTONDOWN: |
| 372 |
if (joystickManager.translateSdlInstanceId(evt)) { |
| 373 |
event = JoystickButtonDownEvent(evt); |
| 374 |
triggerOsdControlEventsFromJoystickButtonEvent( |
| 375 |
evt.jbutton.button, true); |
| 376 |
} |
| 377 |
break; |
| 378 |
case SDL_JOYAXISMOTION: { |
| 379 |
if (auto joyId = joystickManager.translateSdlInstanceId(evt)) { |
| 380 |
event = JoystickAxisMotionEvent(evt); |
| 381 |
|
| 382 |
int threshold = joystickManager.getJoyDeadThreshold(*joyId); |
| 383 |
auto value = (evt.jaxis.value < -threshold) ? evt.jaxis.value |
| 384 |
: (evt.jaxis.value > threshold) ? evt.jaxis.value |
| 385 |
: 0; |
| 386 |
triggerOsdControlEventsFromJoystickAxisMotion( |
| 387 |
evt.jaxis.axis, value); |
| 388 |
} |
| 389 |
break; |
| 390 |
} |
| 391 |
case SDL_JOYHATMOTION: |
| 392 |
if (auto joyId = joystickManager.translateSdlInstanceId(evt)) { |
| 393 |
event = JoystickHatEvent(evt); |
| 394 |
triggerOsdControlEventsFromJoystickHat(evt.jhat.value); |
| 395 |
} |
| 396 |
break; |
| 397 |
|
| 398 |
case SDL_JOYDEVICEADDED: |
| 399 |
joystickManager.add(evt.jdevice.which); |
| 400 |
break; |
| 401 |
|
| 402 |
case SDL_JOYDEVICEREMOVED: |
| 403 |
joystickManager.remove(evt.jdevice.which); |
| 404 |
break; |
| 405 |
|
| 406 |
case SDL_TEXTINPUT: |
| 407 |
splitText(evt.text.timestamp, evt.text.text); |
| 408 |
event = TextEvent(evt); |
| 409 |
break; |
| 410 |
|
| 411 |
case SDL_WINDOWEVENT: |
| 412 |
switch (evt.window.event) { |
| 413 |
case SDL_WINDOWEVENT_CLOSE: |
| 414 |
if (WindowEvent::isMainWindow(evt.window.windowID)) { |
| 415 |
event = quitOnce(); |
| 416 |
break; |
| 417 |
} |
| 418 |
[[fallthrough]]; |
| 419 |
default: |
| 420 |
event = WindowEvent(evt); |
| 421 |
break; |
| 422 |
} |
| 423 |
break; |
| 424 |
|
| 425 |
case SDL_DROPFILE: |
| 426 |
event = FileDropEvent( |
| 427 |
FileOperations::getConventionalPath(evt.drop.file)); |
| 428 |
SDL_free(evt.drop.file); |
| 429 |
break; |
| 430 |
|
| 431 |
case SDL_QUIT: |
| 432 |
event = quitOnce(); |
| 433 |
break; |
| 434 |
|
| 435 |
default: |
| 436 |
break; |
| 437 |
} |
| 438 |
|
| 439 |
#if 0 |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
#endif |
| 446 |
|
| 447 |
if (event) eventDistributor.distributeEvent(std::move(*event)); |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
void InputEventGenerator::updateGrab(bool grab) |
| 452 |
{ |
| 453 |
escapeGrabState = ESCAPE_GRAB_WAIT_CMD; |
| 454 |
setGrabInput(grab); |
| 455 |
} |
| 456 |
|
| 457 |
bool InputEventGenerator::signalEvent(const Event& event) |
| 458 |
{ |
| 459 |
std::visit(overloaded{ |
| 460 |
[&](const WindowEvent& e) { |
| 461 |
const auto& evt = e.getSdlWindowEvent(); |
| 462 |
if (e.isMainWindow() && |
| 463 |
evt.event == one_of(SDL_WINDOWEVENT_FOCUS_GAINED, SDL_WINDOWEVENT_FOCUS_LOST)) { |
| 464 |
switch (escapeGrabState) { |
| 465 |
case ESCAPE_GRAB_WAIT_CMD: |
| 466 |
|
| 467 |
break; |
| 468 |
case ESCAPE_GRAB_WAIT_LOST: |
| 469 |
if (evt.event == SDL_WINDOWEVENT_FOCUS_LOST) { |
| 470 |
escapeGrabState = ESCAPE_GRAB_WAIT_GAIN; |
| 471 |
} |
| 472 |
break; |
| 473 |
case ESCAPE_GRAB_WAIT_GAIN: |
| 474 |
if (evt.event == SDL_WINDOWEVENT_FOCUS_GAINED) { |
| 475 |
escapeGrabState = ESCAPE_GRAB_WAIT_CMD; |
| 476 |
} |
| 477 |
setGrabInput(true); |
| 478 |
break; |
| 479 |
default: |
| 480 |
UNREACHABLE; |
| 481 |
} |
| 482 |
} |
| 483 |
}, |
| 484 |
[](const EventBase&) { |
| 485 |
|
| 486 |
|
| 487 |
} |
| 488 |
}, event); |
| 489 |
return false; |
| 490 |
} |
| 491 |
|
| 492 |
void InputEventGenerator::initializeGrab() const |
| 493 |
{ |
| 494 |
setGrabInput(grabInput.getBoolean()); |
| 495 |
} |
| 496 |
|
| 497 |
void InputEventGenerator::setGrabInput(bool grab) const |
| 498 |
{ |
| 499 |
SDL_SetWindowGrab(SDL_GL_GetCurrentWindow(), grab ? SDL_TRUE : SDL_FALSE); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
InputEventGenerator::EscapeGrabCmd::EscapeGrabCmd( |
| 506 |
CommandController& commandController_) |
| 507 |
: Command(commandController_, "escape_grab") |
| 508 |
{ |
| 509 |
} |
| 510 |
|
| 511 |
void InputEventGenerator::EscapeGrabCmd::execute( |
| 512 |
std::span<const TclObject> , TclObject& ) |
| 513 |
{ |
| 514 |
auto& inputEventGenerator = OUTER(InputEventGenerator, escapeGrabCmd); |
| 515 |
if (inputEventGenerator.grabInput.getBoolean()) { |
| 516 |
inputEventGenerator.escapeGrabState = |
| 517 |
InputEventGenerator::ESCAPE_GRAB_WAIT_LOST; |
| 518 |
inputEventGenerator.setGrabInput(false); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
std::string InputEventGenerator::EscapeGrabCmd::help( |
| 523 |
std::span<const TclObject> ) const |
| 524 |
{ |
| 525 |
return "Temporarily release input grab."; |
| 526 |
} |
| 527 |
|
| 528 |
} |