| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | #include "input.h" |
| 4 | |
| 5 | #include <base/dbg.h> |
| 6 | #include <base/log.h> |
| 7 | #include <base/str.h> |
| 8 | #include <base/time.h> |
| 9 | #include <base/windows.h> |
| 10 | |
| 11 | #include <engine/client/keyboard.h> |
| 12 | #include <engine/console.h> |
| 13 | #include <engine/graphics.h> |
| 14 | #include <engine/input.h> |
| 15 | #include <engine/keys.h> |
| 16 | #include <engine/shared/config.h> |
| 17 | |
| 18 | #include <SDL.h> |
| 19 | |
| 20 | #include <algorithm> |
| 21 | |
| 22 | // support older SDL version (pre 2.0.6) |
| 23 | #ifndef SDL_JOYSTICK_AXIS_MIN |
| 24 | #define SDL_JOYSTICK_AXIS_MIN (-32768) |
| 25 | #endif |
| 26 | #ifndef SDL_JOYSTICK_AXIS_MAX |
| 27 | #define SDL_JOYSTICK_AXIS_MAX 32767 |
| 28 | #endif |
| 29 | |
| 30 | #if defined(CONF_FAMILY_WINDOWS) |
| 31 | #include <windows.h> |
| 32 | // windows.h must be included before imm.h, but clang-format requires includes to be sorted alphabetically, hence this comment. |
| 33 | #include <imm.h> |
| 34 | #endif |
| 35 | |
| 36 | // for platform specific features that aren't available or are broken in SDL |
| 37 | #include <SDL_syswm.h> |
| 38 | #ifdef KeyPress |
| 39 | #undef KeyPress // Undo pollution from X11/Xlib.h included by SDL_syswm.h on Linux |
| 40 | #endif |
| 41 | |
| 42 | void CInput::AddKeyEvent(int Key, int Flags) |
| 43 | { |
| 44 | dbg_assert(Key >= KEY_FIRST && Key < KEY_LAST, "Key invalid: %d" , Key); |
| 45 | dbg_assert((Flags & (FLAG_PRESS | FLAG_RELEASE)) != 0 && (Flags & ~(FLAG_PRESS | FLAG_RELEASE | FLAG_REPEAT)) == 0, "Flags invalid (unknown flag): %d" , Flags); |
| 46 | dbg_assert((Flags & FLAG_REPEAT) == 0 || (Flags & FLAG_PRESS) != 0, "Flags invalid (key repeat implies key press): %d" , Flags); |
| 47 | |
| 48 | CEvent Event; |
| 49 | Event.m_Key = Key; |
| 50 | Event.m_Flags = Flags; |
| 51 | Event.m_aText[0] = '\0'; |
| 52 | Event.m_InputCount = m_InputCounter; |
| 53 | m_vInputEvents.emplace_back(args&: Event); |
| 54 | |
| 55 | if(Flags & IInput::FLAG_PRESS) |
| 56 | { |
| 57 | m_aCurrentKeyStates[Key] = true; |
| 58 | m_aFrameKeyStates[Key] = true; |
| 59 | } |
| 60 | if(Flags & IInput::FLAG_RELEASE) |
| 61 | { |
| 62 | m_aCurrentKeyStates[Key] = false; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void CInput::AddTextEvent(const char *pText) |
| 67 | { |
| 68 | CEvent Event; |
| 69 | Event.m_Key = KEY_UNKNOWN; |
| 70 | Event.m_Flags = FLAG_TEXT; |
| 71 | str_copy(dst&: Event.m_aText, src: pText); |
| 72 | Event.m_InputCount = m_InputCounter; |
| 73 | m_vInputEvents.emplace_back(args&: Event); |
| 74 | } |
| 75 | |
| 76 | CInput::CInput() |
| 77 | { |
| 78 | std::fill(first: std::begin(arr&: m_aCurrentKeyStates), last: std::end(arr&: m_aCurrentKeyStates), value: false); |
| 79 | std::fill(first: std::begin(arr&: m_aFrameKeyStates), last: std::end(arr&: m_aFrameKeyStates), value: false); |
| 80 | |
| 81 | m_vInputEvents.reserve(n: 32); |
| 82 | m_LastUpdate = 0; |
| 83 | m_UpdateTime = 0.0f; |
| 84 | |
| 85 | m_InputCounter = 1; |
| 86 | m_InputGrabbed = false; |
| 87 | |
| 88 | m_MouseFocus = true; |
| 89 | |
| 90 | m_CompositionCursor = 0; |
| 91 | m_CandidateSelectedIndex = -1; |
| 92 | |
| 93 | m_aDropFile[0] = '\0'; |
| 94 | } |
| 95 | |
| 96 | void CInput::Init() |
| 97 | { |
| 98 | StopTextInput(); |
| 99 | |
| 100 | m_pGraphics = Kernel()->RequestInterface<IEngineGraphics>(); |
| 101 | m_pConsole = Kernel()->RequestInterface<IConsole>(); |
| 102 | m_pConfigManager = Kernel()->RequestInterface<IConfigManager>(); |
| 103 | |
| 104 | MouseModeRelative(); |
| 105 | |
| 106 | InitJoysticks(); |
| 107 | } |
| 108 | |
| 109 | void CInput::Shutdown() |
| 110 | { |
| 111 | CloseJoysticks(); |
| 112 | } |
| 113 | |
| 114 | void CInput::InitJoysticks() |
| 115 | { |
| 116 | if(!SDL_WasInit(SDL_INIT_JOYSTICK)) |
| 117 | { |
| 118 | if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) |
| 119 | { |
| 120 | log_error("joystick" , "Unable to init SDL joystick system: %s" , SDL_GetError()); |
| 121 | return; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | const int NumJoysticks = SDL_NumJoysticks(); |
| 126 | log_info("joystick" , "%d joystick(s) found" , NumJoysticks); |
| 127 | for(int i = 0; i < NumJoysticks; i++) |
| 128 | OpenJoystick(JoystickIndex: i); |
| 129 | UpdateActiveJoystick(); |
| 130 | |
| 131 | Console()->Chain(pName: "inp_controller_guid" , pfnChainFunc: ConchainJoystickGuidChanged, pUser: this); |
| 132 | } |
| 133 | |
| 134 | bool CInput::OpenJoystick(int JoystickIndex) |
| 135 | { |
| 136 | SDL_Joystick *pJoystick = SDL_JoystickOpen(device_index: JoystickIndex); |
| 137 | if(!pJoystick) |
| 138 | { |
| 139 | log_error("joystick" , "Could not open joystick %d: '%s'" , JoystickIndex, SDL_GetError()); |
| 140 | return false; |
| 141 | } |
| 142 | if(std::find_if(first: m_vJoysticks.begin(), last: m_vJoysticks.end(), pred: [pJoystick](const CJoystick &Joystick) -> bool { return Joystick.m_pDelegate == pJoystick; }) != m_vJoysticks.end()) |
| 143 | { |
| 144 | // Joystick has already been added, drop the extra reference from SDL_JoystickOpen |
| 145 | SDL_JoystickClose(joystick: pJoystick); |
| 146 | return false; |
| 147 | } |
| 148 | m_vJoysticks.emplace_back(args: this, args: m_vJoysticks.size(), args&: pJoystick); |
| 149 | const CJoystick &Joystick = m_vJoysticks[m_vJoysticks.size() - 1]; |
| 150 | log_info("joystick" , "Opened joystick %d '%s' (%d axes, %d buttons, %d balls, %d hats)" , JoystickIndex, Joystick.GetName(), |
| 151 | Joystick.GetNumAxes(), Joystick.GetNumButtons(), Joystick.GetNumBalls(), Joystick.GetNumHats()); |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | void CInput::UpdateActiveJoystick() |
| 156 | { |
| 157 | m_pActiveJoystick = nullptr; |
| 158 | if(m_vJoysticks.empty()) |
| 159 | return; |
| 160 | for(auto &Joystick : m_vJoysticks) |
| 161 | { |
| 162 | if(str_comp(a: Joystick.GetGUID(), b: g_Config.m_InpControllerGUID) == 0) |
| 163 | { |
| 164 | m_pActiveJoystick = &Joystick; |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | // Fall back to first joystick if no match was found |
| 169 | if(!m_pActiveJoystick) |
| 170 | m_pActiveJoystick = &m_vJoysticks.front(); |
| 171 | } |
| 172 | |
| 173 | void CInput::ConchainJoystickGuidChanged(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData) |
| 174 | { |
| 175 | pfnCallback(pResult, pCallbackUserData); |
| 176 | if(pResult->NumArguments() == 1) |
| 177 | { |
| 178 | static_cast<CInput *>(pUserData)->UpdateActiveJoystick(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | float CInput::GetJoystickDeadzone() |
| 183 | { |
| 184 | return std::min(a: g_Config.m_InpControllerTolerance / 50.0f, b: 0.995f); |
| 185 | } |
| 186 | |
| 187 | CInput::CJoystick::CJoystick(CInput *pInput, int Index, SDL_Joystick *pDelegate) |
| 188 | { |
| 189 | m_pInput = pInput; |
| 190 | m_Index = Index; |
| 191 | m_pDelegate = pDelegate; |
| 192 | m_NumAxes = SDL_JoystickNumAxes(joystick: pDelegate); |
| 193 | m_NumButtons = SDL_JoystickNumButtons(joystick: pDelegate); |
| 194 | m_NumBalls = SDL_JoystickNumBalls(joystick: pDelegate); |
| 195 | m_NumHats = SDL_JoystickNumHats(joystick: pDelegate); |
| 196 | str_copy(dst&: m_aName, src: SDL_JoystickName(joystick: pDelegate)); |
| 197 | SDL_JoystickGetGUIDString(guid: SDL_JoystickGetGUID(joystick: pDelegate), pszGUID: m_aGUID, cbGUID: sizeof(m_aGUID)); |
| 198 | m_InstanceId = SDL_JoystickInstanceID(joystick: pDelegate); |
| 199 | } |
| 200 | |
| 201 | void CInput::CloseJoysticks() |
| 202 | { |
| 203 | if(SDL_WasInit(SDL_INIT_JOYSTICK)) |
| 204 | SDL_QuitSubSystem(SDL_INIT_JOYSTICK); |
| 205 | |
| 206 | m_vJoysticks.clear(); |
| 207 | m_pActiveJoystick = nullptr; |
| 208 | } |
| 209 | |
| 210 | void CInput::SetActiveJoystick(size_t Index) |
| 211 | { |
| 212 | m_pActiveJoystick = &m_vJoysticks[Index]; |
| 213 | str_copy(dst&: g_Config.m_InpControllerGUID, src: m_pActiveJoystick->GetGUID()); |
| 214 | } |
| 215 | |
| 216 | float CInput::CJoystick::GetAxisValue(int Axis) |
| 217 | { |
| 218 | return (SDL_JoystickGetAxis(joystick: m_pDelegate, axis: Axis) - SDL_JOYSTICK_AXIS_MIN) / (float)(SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN) * 2.0f - 1.0f; |
| 219 | } |
| 220 | |
| 221 | void CInput::CJoystick::GetJoystickHatKeys(int Hat, int HatValue, int (&aHatKeys)[2]) |
| 222 | { |
| 223 | if(HatValue & SDL_HAT_UP) |
| 224 | aHatKeys[0] = KEY_JOY_HAT0_UP + Hat * NUM_JOYSTICK_BUTTONS_PER_HAT; |
| 225 | else if(HatValue & SDL_HAT_DOWN) |
| 226 | aHatKeys[0] = KEY_JOY_HAT0_DOWN + Hat * NUM_JOYSTICK_BUTTONS_PER_HAT; |
| 227 | else |
| 228 | aHatKeys[0] = KEY_UNKNOWN; |
| 229 | |
| 230 | if(HatValue & SDL_HAT_LEFT) |
| 231 | aHatKeys[1] = KEY_JOY_HAT0_LEFT + Hat * NUM_JOYSTICK_BUTTONS_PER_HAT; |
| 232 | else if(HatValue & SDL_HAT_RIGHT) |
| 233 | aHatKeys[1] = KEY_JOY_HAT0_RIGHT + Hat * NUM_JOYSTICK_BUTTONS_PER_HAT; |
| 234 | else |
| 235 | aHatKeys[1] = KEY_UNKNOWN; |
| 236 | } |
| 237 | |
| 238 | void CInput::CJoystick::GetHatValue(int Hat, int (&aHatKeys)[2]) |
| 239 | { |
| 240 | GetJoystickHatKeys(Hat, HatValue: SDL_JoystickGetHat(joystick: m_pDelegate, hat: Hat), aHatKeys); |
| 241 | } |
| 242 | |
| 243 | bool CInput::CJoystick::Relative(float *pX, float *pY) |
| 244 | { |
| 245 | if(!Input()->m_MouseFocus || !Input()->m_InputGrabbed || !g_Config.m_InpControllerEnable) |
| 246 | return false; |
| 247 | |
| 248 | const vec2 RawJoystickPos = vec2(GetAxisValue(Axis: g_Config.m_InpControllerX), GetAxisValue(Axis: g_Config.m_InpControllerY)); |
| 249 | const float Len = length(a: RawJoystickPos); |
| 250 | const float DeadZone = Input()->GetJoystickDeadzone(); |
| 251 | if(Len > DeadZone) |
| 252 | { |
| 253 | const float Factor = 2500.0f * Input()->GetUpdateTime() * std::max(a: (Len - DeadZone) / (1.0f - DeadZone), b: 0.001f) / Len; |
| 254 | *pX = RawJoystickPos.x * Factor; |
| 255 | *pY = RawJoystickPos.y * Factor; |
| 256 | return true; |
| 257 | } |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | bool CInput::CJoystick::Absolute(float *pX, float *pY) |
| 262 | { |
| 263 | if(!Input()->m_MouseFocus || !Input()->m_InputGrabbed || !g_Config.m_InpControllerEnable) |
| 264 | return false; |
| 265 | |
| 266 | const vec2 RawJoystickPos = vec2(GetAxisValue(Axis: g_Config.m_InpControllerX), GetAxisValue(Axis: g_Config.m_InpControllerY)); |
| 267 | const float DeadZone = Input()->GetJoystickDeadzone(); |
| 268 | if(dot(a: RawJoystickPos, b: RawJoystickPos) > DeadZone * DeadZone) |
| 269 | { |
| 270 | *pX = RawJoystickPos.x; |
| 271 | *pY = RawJoystickPos.y; |
| 272 | return true; |
| 273 | } |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | bool CInput::MouseRelative(float *pX, float *pY) |
| 278 | { |
| 279 | if(!m_MouseFocus || !m_InputGrabbed) |
| 280 | return false; |
| 281 | |
| 282 | ivec2 Relative; |
| 283 | SDL_GetRelativeMouseState(x: &Relative.x, y: &Relative.y); |
| 284 | |
| 285 | *pX = Relative.x; |
| 286 | *pY = Relative.y; |
| 287 | return *pX != 0.0f || *pY != 0.0f; |
| 288 | } |
| 289 | |
| 290 | void CInput::MouseModeAbsolute() |
| 291 | { |
| 292 | m_InputGrabbed = false; |
| 293 | SDL_SetRelativeMouseMode(enabled: SDL_FALSE); |
| 294 | Graphics()->SetWindowGrab(false); |
| 295 | } |
| 296 | |
| 297 | void CInput::MouseModeRelative() |
| 298 | { |
| 299 | m_InputGrabbed = true; |
| 300 | SDL_SetRelativeMouseMode(enabled: SDL_TRUE); |
| 301 | Graphics()->SetWindowGrab(true); |
| 302 | // Clear pending relative mouse motion |
| 303 | SDL_GetRelativeMouseState(x: nullptr, y: nullptr); |
| 304 | } |
| 305 | |
| 306 | vec2 CInput::NativeMousePos() const |
| 307 | { |
| 308 | ivec2 Position; |
| 309 | SDL_GetMouseState(x: &Position.x, y: &Position.y); |
| 310 | return vec2(Position.x, Position.y); |
| 311 | } |
| 312 | |
| 313 | bool CInput::NativeMousePressed(int Index) const |
| 314 | { |
| 315 | int i = SDL_GetMouseState(x: nullptr, y: nullptr); |
| 316 | return (i & SDL_BUTTON(Index)) != 0; |
| 317 | } |
| 318 | |
| 319 | const std::vector<IInput::CTouchFingerState> &CInput::TouchFingerStates() const |
| 320 | { |
| 321 | return m_vTouchFingerStates; |
| 322 | } |
| 323 | |
| 324 | void CInput::ClearTouchDeltas() |
| 325 | { |
| 326 | for(CTouchFingerState &TouchFingerState : m_vTouchFingerStates) |
| 327 | { |
| 328 | TouchFingerState.m_Delta = vec2(0.0f, 0.0f); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | std::string CInput::GetClipboardText() |
| 333 | { |
| 334 | char *pClipboardText = SDL_GetClipboardText(); |
| 335 | std::string ClipboardText = pClipboardText; |
| 336 | SDL_free(mem: pClipboardText); |
| 337 | return ClipboardText; |
| 338 | } |
| 339 | |
| 340 | void CInput::SetClipboardText(const char *pText) |
| 341 | { |
| 342 | SDL_SetClipboardText(text: pText); |
| 343 | } |
| 344 | |
| 345 | void CInput::StartTextInput() |
| 346 | { |
| 347 | // enable system messages for IME |
| 348 | SDL_EventState(type: SDL_SYSWMEVENT, SDL_ENABLE); |
| 349 | SDL_StartTextInput(); |
| 350 | } |
| 351 | |
| 352 | void CInput::StopTextInput() |
| 353 | { |
| 354 | SDL_StopTextInput(); |
| 355 | // disable system messages for performance |
| 356 | SDL_EventState(type: SDL_SYSWMEVENT, SDL_DISABLE); |
| 357 | m_CompositionString = "" ; |
| 358 | m_CompositionCursor = 0; |
| 359 | m_vCandidates.clear(); |
| 360 | } |
| 361 | |
| 362 | void CInput::EnsureScreenKeyboardShown() |
| 363 | { |
| 364 | if(!SDL_HasScreenKeyboardSupport() || |
| 365 | Graphics()->IsScreenKeyboardShown()) |
| 366 | { |
| 367 | return; |
| 368 | } |
| 369 | SDL_StopTextInput(); |
| 370 | SDL_StartTextInput(); |
| 371 | } |
| 372 | |
| 373 | void CInput::ConsumeEvents(std::function<void(const CEvent &Event)> Consumer) const |
| 374 | { |
| 375 | for(const CEvent &Event : m_vInputEvents) |
| 376 | { |
| 377 | // Only propagate valid input events |
| 378 | if(Event.m_InputCount == m_InputCounter) |
| 379 | { |
| 380 | Consumer(Event); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | void CInput::Clear() |
| 386 | { |
| 387 | std::fill(first: std::begin(arr&: m_aFrameKeyStates), last: std::end(arr&: m_aFrameKeyStates), value: false); |
| 388 | m_vInputEvents.clear(); |
| 389 | ClearTouchDeltas(); |
| 390 | } |
| 391 | |
| 392 | float CInput::GetUpdateTime() const |
| 393 | { |
| 394 | return m_UpdateTime; |
| 395 | } |
| 396 | |
| 397 | bool CInput::KeyIsPressed(int Key) const |
| 398 | { |
| 399 | dbg_assert(Key >= KEY_FIRST && Key < KEY_LAST, "Key invalid: %d" , Key); |
| 400 | return m_aCurrentKeyStates[Key]; |
| 401 | } |
| 402 | |
| 403 | bool CInput::KeyPress(int Key) const |
| 404 | { |
| 405 | dbg_assert(Key >= KEY_FIRST && Key < KEY_LAST, "Key invalid: %d" , Key); |
| 406 | return m_aFrameKeyStates[Key]; |
| 407 | } |
| 408 | |
| 409 | int CInput::FindKeyByName(const char *pKeyName) const |
| 410 | { |
| 411 | // check for numeric |
| 412 | if(pKeyName[0] == '&') |
| 413 | { |
| 414 | int Key = str_toint(str: pKeyName + 1); |
| 415 | if(Key > KEY_FIRST && Key < KEY_LAST) |
| 416 | return Key; // numeric |
| 417 | } |
| 418 | |
| 419 | // search for key |
| 420 | for(int Key = KEY_FIRST; Key < KEY_LAST; Key++) |
| 421 | { |
| 422 | if(str_comp_nocase(a: pKeyName, b: KeyName(Key)) == 0) |
| 423 | return Key; |
| 424 | } |
| 425 | |
| 426 | return KEY_UNKNOWN; |
| 427 | } |
| 428 | |
| 429 | void CInput::HandleJoystickAxisMotionEvent(const SDL_JoyAxisEvent &Event) |
| 430 | { |
| 431 | if(!g_Config.m_InpControllerEnable) |
| 432 | return; |
| 433 | CJoystick *pJoystick = GetActiveJoystick(); |
| 434 | if(!pJoystick || pJoystick->GetInstanceId() != Event.which) |
| 435 | return; |
| 436 | if(Event.axis >= NUM_JOYSTICK_AXES) |
| 437 | return; |
| 438 | |
| 439 | const int LeftKey = KEY_JOY_AXIS_0_LEFT + 2 * Event.axis; |
| 440 | const int RightKey = LeftKey + 1; |
| 441 | const float DeadZone = GetJoystickDeadzone(); |
| 442 | |
| 443 | if(Event.value <= SDL_JOYSTICK_AXIS_MIN * DeadZone && !m_aCurrentKeyStates[LeftKey]) |
| 444 | { |
| 445 | AddKeyEvent(Key: LeftKey, Flags: IInput::FLAG_PRESS); |
| 446 | } |
| 447 | else if(Event.value > SDL_JOYSTICK_AXIS_MIN * DeadZone && m_aCurrentKeyStates[LeftKey]) |
| 448 | { |
| 449 | AddKeyEvent(Key: LeftKey, Flags: IInput::FLAG_RELEASE); |
| 450 | } |
| 451 | |
| 452 | if(Event.value >= SDL_JOYSTICK_AXIS_MAX * DeadZone && !m_aCurrentKeyStates[RightKey]) |
| 453 | { |
| 454 | AddKeyEvent(Key: RightKey, Flags: IInput::FLAG_PRESS); |
| 455 | } |
| 456 | else if(Event.value < SDL_JOYSTICK_AXIS_MAX * DeadZone && m_aCurrentKeyStates[RightKey]) |
| 457 | { |
| 458 | AddKeyEvent(Key: RightKey, Flags: IInput::FLAG_RELEASE); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void CInput::HandleJoystickButtonEvent(const SDL_JoyButtonEvent &Event) |
| 463 | { |
| 464 | if(!g_Config.m_InpControllerEnable) |
| 465 | return; |
| 466 | CJoystick *pJoystick = GetActiveJoystick(); |
| 467 | if(!pJoystick || pJoystick->GetInstanceId() != Event.which) |
| 468 | return; |
| 469 | if(Event.button >= NUM_JOYSTICK_BUTTONS) |
| 470 | return; |
| 471 | |
| 472 | const int Key = Event.button + KEY_JOYSTICK_BUTTON_0; |
| 473 | |
| 474 | if(Event.type == SDL_JOYBUTTONDOWN) |
| 475 | { |
| 476 | AddKeyEvent(Key, Flags: IInput::FLAG_PRESS); |
| 477 | } |
| 478 | else if(Event.type == SDL_JOYBUTTONUP) |
| 479 | { |
| 480 | AddKeyEvent(Key, Flags: IInput::FLAG_RELEASE); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | void CInput::HandleJoystickHatMotionEvent(const SDL_JoyHatEvent &Event) |
| 485 | { |
| 486 | if(!g_Config.m_InpControllerEnable) |
| 487 | return; |
| 488 | CJoystick *pJoystick = GetActiveJoystick(); |
| 489 | if(!pJoystick || pJoystick->GetInstanceId() != Event.which) |
| 490 | return; |
| 491 | if(Event.hat >= NUM_JOYSTICK_HATS) |
| 492 | return; |
| 493 | |
| 494 | int aHatKeys[2]; |
| 495 | CJoystick::GetJoystickHatKeys(Hat: Event.hat, HatValue: Event.value, aHatKeys); |
| 496 | |
| 497 | for(int Key = KEY_JOY_HAT0_UP + Event.hat * NUM_JOYSTICK_BUTTONS_PER_HAT; Key <= KEY_JOY_HAT0_RIGHT + Event.hat * NUM_JOYSTICK_BUTTONS_PER_HAT; Key++) |
| 498 | { |
| 499 | if(Key != aHatKeys[0] && Key != aHatKeys[1] && m_aCurrentKeyStates[Key]) |
| 500 | { |
| 501 | AddKeyEvent(Key, Flags: IInput::FLAG_RELEASE); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | for(int CurrentKey : aHatKeys) |
| 506 | { |
| 507 | if(CurrentKey != KEY_UNKNOWN && !m_aCurrentKeyStates[CurrentKey]) |
| 508 | { |
| 509 | AddKeyEvent(Key: CurrentKey, Flags: IInput::FLAG_PRESS); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | void CInput::HandleJoystickAddedEvent(const SDL_JoyDeviceEvent &Event) |
| 515 | { |
| 516 | if(OpenJoystick(JoystickIndex: Event.which)) |
| 517 | { |
| 518 | UpdateActiveJoystick(); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | void CInput::HandleJoystickRemovedEvent(const SDL_JoyDeviceEvent &Event) |
| 523 | { |
| 524 | auto RemovedJoystick = std::find_if(first: m_vJoysticks.begin(), last: m_vJoysticks.end(), pred: [Event](const CJoystick &Joystick) -> bool { return Joystick.GetInstanceId() == Event.which; }); |
| 525 | if(RemovedJoystick != m_vJoysticks.end()) |
| 526 | { |
| 527 | log_info("joystick" , "Closed joystick %d '%s'" , (*RemovedJoystick).GetIndex(), (*RemovedJoystick).GetName()); |
| 528 | // NOTE: The SDL_QuitSubSystem() in CloseJoysticks() only runs at shutdown, |
| 529 | // so a joystick removed mid-session must be closed here explicitly, |
| 530 | // otherwise every plug/unplug cycle leaks its SDL_Joystick allocation until restart. |
| 531 | SDL_JoystickClose(joystick: (*RemovedJoystick).m_pDelegate); |
| 532 | auto NextJoystick = m_vJoysticks.erase(position: RemovedJoystick); |
| 533 | // Adjust indices of following joysticks |
| 534 | while(NextJoystick != m_vJoysticks.end()) |
| 535 | { |
| 536 | (*NextJoystick).m_Index--; |
| 537 | ++NextJoystick; |
| 538 | } |
| 539 | UpdateActiveJoystick(); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | vec2 CInput::TouchPositionToViewport(vec2 Position) const |
| 544 | { |
| 545 | // Touch positions are normalized to the drawable area, whereas the rendered image can |
| 546 | // be smaller than it and offset horizontally within it. Positions on the area that is |
| 547 | // not rendered to are clamped to the image. |
| 548 | const vec2 Scaled = (Position * Graphics()->DrawableSize() - vec2(Graphics()->ViewportX(), 0.0f)) / Graphics()->ScreenSize(); |
| 549 | return vec2(std::clamp(val: Scaled.x, lo: 0.0f, hi: 1.0f), std::clamp(val: Scaled.y, lo: 0.0f, hi: 1.0f)); |
| 550 | } |
| 551 | |
| 552 | vec2 CInput::TouchDeltaToViewport(vec2 Delta) const |
| 553 | { |
| 554 | // The scale between the drawable area and the rendered image applies to deltas as well. |
| 555 | const vec2 Scaled = Delta * Graphics()->DrawableSize() / Graphics()->ScreenSize(); |
| 556 | return vec2(std::clamp(val: Scaled.x, lo: -1.0f, hi: 1.0f), std::clamp(val: Scaled.y, lo: -1.0f, hi: 1.0f)); |
| 557 | } |
| 558 | |
| 559 | void CInput::HandleTouchDownEvent(const SDL_TouchFingerEvent &Event) |
| 560 | { |
| 561 | CTouchFingerState TouchFingerState; |
| 562 | TouchFingerState.m_Finger.m_DeviceId = Event.touchId; |
| 563 | TouchFingerState.m_Finger.m_FingerId = Event.fingerId; |
| 564 | TouchFingerState.m_Position = TouchPositionToViewport(Position: vec2(Event.x, Event.y)); |
| 565 | TouchFingerState.m_Delta = TouchDeltaToViewport(Delta: vec2(Event.dx, Event.dy)); |
| 566 | TouchFingerState.m_PressTime = time_get_nanoseconds(); |
| 567 | m_vTouchFingerStates.emplace_back(args&: TouchFingerState); |
| 568 | } |
| 569 | |
| 570 | void CInput::HandleTouchUpEvent(const SDL_TouchFingerEvent &Event) |
| 571 | { |
| 572 | auto FoundState = std::find_if(first: m_vTouchFingerStates.begin(), last: m_vTouchFingerStates.end(), pred: [Event](const CTouchFingerState &State) { |
| 573 | return State.m_Finger.m_DeviceId == Event.touchId && State.m_Finger.m_FingerId == Event.fingerId; |
| 574 | }); |
| 575 | if(FoundState != m_vTouchFingerStates.end()) |
| 576 | { |
| 577 | m_vTouchFingerStates.erase(position: FoundState); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void CInput::HandleTouchMotionEvent(const SDL_TouchFingerEvent &Event) |
| 582 | { |
| 583 | auto FoundState = std::find_if(first: m_vTouchFingerStates.begin(), last: m_vTouchFingerStates.end(), pred: [Event](const CTouchFingerState &State) { |
| 584 | return State.m_Finger.m_DeviceId == Event.touchId && State.m_Finger.m_FingerId == Event.fingerId; |
| 585 | }); |
| 586 | if(FoundState != m_vTouchFingerStates.end()) |
| 587 | { |
| 588 | FoundState->m_Position = TouchPositionToViewport(Position: vec2(Event.x, Event.y)); |
| 589 | FoundState->m_Delta += TouchDeltaToViewport(Delta: vec2(Event.dx, Event.dy)); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | void CInput::HandleTextEditingEvent(const char *pText, int Start, int Length) |
| 594 | { |
| 595 | if(pText[0] != '\0') |
| 596 | { |
| 597 | m_CompositionString = pText; |
| 598 | m_CompositionCursor = 0; |
| 599 | for(int i = 0; i < Start; i++) |
| 600 | { |
| 601 | m_CompositionCursor = str_utf8_forward(str: m_CompositionString.c_str(), cursor: m_CompositionCursor); |
| 602 | } |
| 603 | // Length is currently unused on Windows and will always be 0, so we don't support selecting composition text |
| 604 | AddTextEvent(pText: "" ); |
| 605 | } |
| 606 | else |
| 607 | { |
| 608 | m_CompositionString = "" ; |
| 609 | m_CompositionCursor = 0; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | void CInput::SetCompositionWindowPosition(float X, float Y, float H) |
| 614 | { |
| 615 | SDL_Rect Rect; |
| 616 | Rect.x = X / m_pGraphics->ScreenHiDPIScale(); |
| 617 | Rect.y = Y / m_pGraphics->ScreenHiDPIScale(); |
| 618 | Rect.h = H / m_pGraphics->ScreenHiDPIScale(); |
| 619 | Rect.w = 0; |
| 620 | SDL_SetTextInputRect(rect: &Rect); |
| 621 | } |
| 622 | |
| 623 | static int TranslateKeyEventKey(const SDL_KeyboardEvent &KeyEvent) |
| 624 | { |
| 625 | // See SDL_Keymod for possible modifiers: |
| 626 | // NONE = 0 |
| 627 | // LSHIFT = 1 |
| 628 | // RSHIFT = 2 |
| 629 | // LCTRL = 64 |
| 630 | // RCTRL = 128 |
| 631 | // LALT = 256 |
| 632 | // RALT = 512 |
| 633 | // LGUI = 1024 |
| 634 | // RGUI = 2048 |
| 635 | // NUM = 4096 |
| 636 | // CAPS = 8192 |
| 637 | // MODE = 16384 |
| 638 | // Sum if you want to ignore multiple modifiers. |
| 639 | if(KeyEvent.keysym.mod & g_Config.m_InpIgnoredModifiers) |
| 640 | { |
| 641 | return KEY_UNKNOWN; |
| 642 | } |
| 643 | |
| 644 | int Key = g_Config.m_InpTranslatedKeys ? SDL_GetScancodeFromKey(key: KeyEvent.keysym.sym) : KeyEvent.keysym.scancode; |
| 645 | |
| 646 | #if defined(CONF_PLATFORM_ANDROID) |
| 647 | // Translate the Android back-button to the escape-key so it can be used to open/close the menu, close popups etc. |
| 648 | if(Key == KEY_AC_BACK) |
| 649 | { |
| 650 | Key = KEY_ESCAPE; |
| 651 | } |
| 652 | #endif |
| 653 | |
| 654 | return Key; |
| 655 | } |
| 656 | |
| 657 | static int TranslateMouseButtonEventKey(const SDL_MouseButtonEvent &MouseButtonEvent) |
| 658 | { |
| 659 | switch(MouseButtonEvent.button) |
| 660 | { |
| 661 | case SDL_BUTTON_LEFT: |
| 662 | return KEY_MOUSE_1; |
| 663 | case SDL_BUTTON_RIGHT: |
| 664 | return KEY_MOUSE_2; |
| 665 | case SDL_BUTTON_MIDDLE: |
| 666 | return KEY_MOUSE_3; |
| 667 | case SDL_BUTTON_X1: |
| 668 | return KEY_MOUSE_4; |
| 669 | case SDL_BUTTON_X2: |
| 670 | return KEY_MOUSE_5; |
| 671 | case 6: |
| 672 | return KEY_MOUSE_6; |
| 673 | case 7: |
| 674 | return KEY_MOUSE_7; |
| 675 | case 8: |
| 676 | return KEY_MOUSE_8; |
| 677 | case 9: |
| 678 | return KEY_MOUSE_9; |
| 679 | default: |
| 680 | return KEY_UNKNOWN; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | static int TranslateMouseWheelEventKey(const SDL_MouseWheelEvent &MouseWheelEvent) |
| 685 | { |
| 686 | if(MouseWheelEvent.y > 0) |
| 687 | { |
| 688 | return KEY_MOUSE_WHEEL_UP; |
| 689 | } |
| 690 | else if(MouseWheelEvent.y < 0) |
| 691 | { |
| 692 | return KEY_MOUSE_WHEEL_DOWN; |
| 693 | } |
| 694 | else if(MouseWheelEvent.x > 0) |
| 695 | { |
| 696 | return KEY_MOUSE_WHEEL_RIGHT; |
| 697 | } |
| 698 | else if(MouseWheelEvent.x < 0) |
| 699 | { |
| 700 | return KEY_MOUSE_WHEEL_LEFT; |
| 701 | } |
| 702 | else |
| 703 | { |
| 704 | return KEY_UNKNOWN; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | #if defined(CONF_PLATFORM_EMSCRIPTEN) |
| 709 | extern "C" { |
| 710 | |
| 711 | // This will be called from Emscripten JS code |
| 712 | bool EmscriptenQuit = false; |
| 713 | void EmscriptenCallbackQuit() |
| 714 | { |
| 715 | EmscriptenQuit = true; |
| 716 | } |
| 717 | } |
| 718 | #endif |
| 719 | |
| 720 | int CInput::Update() |
| 721 | { |
| 722 | const int64_t Now = time_get(); |
| 723 | if(m_LastUpdate != 0) |
| 724 | { |
| 725 | const float Diff = (Now - m_LastUpdate) / (float)time_freq(); |
| 726 | m_UpdateTime = m_UpdateTime == 0.0f ? Diff : (m_UpdateTime * 0.8f + Diff * 0.2f); |
| 727 | } |
| 728 | m_LastUpdate = Now; |
| 729 | |
| 730 | // keep the counter between 1..0xFFFFFFFF, 0 means not pressed |
| 731 | m_InputCounter = (m_InputCounter % std::numeric_limits<decltype(m_InputCounter)>::max()) + 1; |
| 732 | |
| 733 | SDL_Event Event; |
| 734 | bool IgnoreKeys = false; |
| 735 | |
| 736 | const auto &&AddKeyEventChecked = [&](int Key, int Flags) { |
| 737 | if(Key != KEY_UNKNOWN && !IgnoreKeys && (!(Flags & IInput::FLAG_PRESS) || !HasComposition())) |
| 738 | { |
| 739 | AddKeyEvent(Key, Flags); |
| 740 | } |
| 741 | }; |
| 742 | |
| 743 | #if defined(CONF_PLATFORM_EMSCRIPTEN) |
| 744 | if(EmscriptenQuit) |
| 745 | { |
| 746 | return 1; |
| 747 | } |
| 748 | #endif |
| 749 | |
| 750 | while(SDL_PollEvent(event: &Event)) |
| 751 | { |
| 752 | switch(Event.type) |
| 753 | { |
| 754 | case SDL_SYSWMEVENT: |
| 755 | ProcessSystemMessage(pMsg: Event.syswm.msg); |
| 756 | break; |
| 757 | |
| 758 | case SDL_TEXTEDITING: |
| 759 | HandleTextEditingEvent(pText: Event.edit.text, Start: Event.edit.start, Length: Event.edit.length); |
| 760 | break; |
| 761 | |
| 762 | #if SDL_VERSION_ATLEAST(2, 0, 22) |
| 763 | case SDL_TEXTEDITING_EXT: |
| 764 | HandleTextEditingEvent(pText: Event.editExt.text, Start: Event.editExt.start, Length: Event.editExt.length); |
| 765 | SDL_free(mem: Event.editExt.text); |
| 766 | break; |
| 767 | #endif |
| 768 | |
| 769 | case SDL_TEXTINPUT: |
| 770 | m_CompositionString = "" ; |
| 771 | m_CompositionCursor = 0; |
| 772 | AddTextEvent(pText: Event.text.text); |
| 773 | break; |
| 774 | |
| 775 | // handle keys |
| 776 | case SDL_KEYDOWN: |
| 777 | AddKeyEventChecked(TranslateKeyEventKey(KeyEvent: Event.key), IInput::FLAG_PRESS | (Event.key.repeat != 0 ? FLAG_REPEAT : 0)); |
| 778 | break; |
| 779 | |
| 780 | case SDL_KEYUP: |
| 781 | AddKeyEventChecked(TranslateKeyEventKey(KeyEvent: Event.key), IInput::FLAG_RELEASE); |
| 782 | break; |
| 783 | |
| 784 | // handle the joystick events |
| 785 | case SDL_JOYAXISMOTION: |
| 786 | HandleJoystickAxisMotionEvent(Event: Event.jaxis); |
| 787 | break; |
| 788 | |
| 789 | case SDL_JOYBUTTONUP: |
| 790 | case SDL_JOYBUTTONDOWN: |
| 791 | HandleJoystickButtonEvent(Event: Event.jbutton); |
| 792 | break; |
| 793 | |
| 794 | case SDL_JOYHATMOTION: |
| 795 | HandleJoystickHatMotionEvent(Event: Event.jhat); |
| 796 | break; |
| 797 | |
| 798 | case SDL_JOYDEVICEADDED: |
| 799 | HandleJoystickAddedEvent(Event: Event.jdevice); |
| 800 | break; |
| 801 | |
| 802 | case SDL_JOYDEVICEREMOVED: |
| 803 | HandleJoystickRemovedEvent(Event: Event.jdevice); |
| 804 | break; |
| 805 | |
| 806 | // handle mouse buttons |
| 807 | case SDL_MOUSEBUTTONDOWN: |
| 808 | AddKeyEventChecked(TranslateMouseButtonEventKey(MouseButtonEvent: Event.button), IInput::FLAG_PRESS); |
| 809 | break; |
| 810 | |
| 811 | case SDL_MOUSEBUTTONUP: |
| 812 | AddKeyEventChecked(TranslateMouseButtonEventKey(MouseButtonEvent: Event.button), IInput::FLAG_RELEASE); |
| 813 | break; |
| 814 | |
| 815 | case SDL_MOUSEWHEEL: |
| 816 | AddKeyEventChecked(TranslateMouseWheelEventKey(MouseWheelEvent: Event.wheel), IInput::FLAG_PRESS | IInput::FLAG_RELEASE); |
| 817 | break; |
| 818 | |
| 819 | case SDL_FINGERDOWN: |
| 820 | HandleTouchDownEvent(Event: Event.tfinger); |
| 821 | break; |
| 822 | |
| 823 | case SDL_FINGERUP: |
| 824 | HandleTouchUpEvent(Event: Event.tfinger); |
| 825 | break; |
| 826 | |
| 827 | case SDL_FINGERMOTION: |
| 828 | HandleTouchMotionEvent(Event: Event.tfinger); |
| 829 | break; |
| 830 | |
| 831 | case SDL_WINDOWEVENT: |
| 832 | // Ignore keys following a focus gain as they may be part of global |
| 833 | // shortcuts |
| 834 | switch(Event.window.event) |
| 835 | { |
| 836 | #if SDL_VERSION_ATLEAST(2, 0, 18) |
| 837 | case SDL_WINDOWEVENT_DISPLAY_CHANGED: |
| 838 | Graphics()->SwitchWindowScreen(Index: Event.display.data1, MoveToCenter: false); |
| 839 | break; |
| 840 | #endif |
| 841 | case SDL_WINDOWEVENT_MOVED: |
| 842 | Graphics()->Move(x: Event.window.data1, y: Event.window.data2); |
| 843 | break; |
| 844 | // listen to size changes, this includes our manual changes and the ones by the window manager |
| 845 | case SDL_WINDOWEVENT_SIZE_CHANGED: |
| 846 | Graphics()->GotResized(w: Event.window.data1, h: Event.window.data2, RefreshRate: -1); |
| 847 | break; |
| 848 | case SDL_WINDOWEVENT_FOCUS_GAINED: |
| 849 | if(m_InputGrabbed) |
| 850 | { |
| 851 | #if defined(CONF_PLATFORM_MACOS) // Todo: remove this when fixed in SDL: https://github.com/libsdl-org/SDL/issues/13920 |
| 852 | MouseModeAbsolute(); |
| 853 | #endif |
| 854 | MouseModeRelative(); |
| 855 | // Clear pending relative mouse motion |
| 856 | SDL_GetRelativeMouseState(x: nullptr, y: nullptr); |
| 857 | } |
| 858 | m_MouseFocus = true; |
| 859 | IgnoreKeys = true; |
| 860 | break; |
| 861 | case SDL_WINDOWEVENT_FOCUS_LOST: |
| 862 | std::fill(first: std::begin(arr&: m_aCurrentKeyStates), last: std::end(arr&: m_aCurrentKeyStates), value: false); |
| 863 | m_MouseFocus = false; |
| 864 | IgnoreKeys = true; |
| 865 | if(m_InputGrabbed) |
| 866 | { |
| 867 | MouseModeAbsolute(); |
| 868 | // Remember that we had relative mouse |
| 869 | m_InputGrabbed = true; |
| 870 | } |
| 871 | break; |
| 872 | case SDL_WINDOWEVENT_MINIMIZED: |
| 873 | #if defined(CONF_PLATFORM_ANDROID) // Save the config when minimized on Android. |
| 874 | m_pConfigManager->Save(); |
| 875 | #endif |
| 876 | Graphics()->WindowDestroyNtf(WindowId: Event.window.windowID); |
| 877 | break; |
| 878 | |
| 879 | case SDL_WINDOWEVENT_MAXIMIZED: |
| 880 | #if defined(CONF_PLATFORM_MACOS) // Todo: remove this when fixed in SDL: https://github.com/libsdl-org/SDL/issues/13920 |
| 881 | MouseModeAbsolute(); |
| 882 | MouseModeRelative(); |
| 883 | #endif |
| 884 | [[fallthrough]]; |
| 885 | case SDL_WINDOWEVENT_RESTORED: |
| 886 | Graphics()->WindowCreateNtf(WindowId: Event.window.windowID); |
| 887 | break; |
| 888 | } |
| 889 | break; |
| 890 | |
| 891 | #if defined(CONF_PLATFORM_IOS) |
| 892 | // Save the config before the app is suspended on iOS, it can be killed |
| 893 | // without any further notice afterwards. |
| 894 | case SDL_APP_WILLENTERBACKGROUND: |
| 895 | m_pConfigManager->Save(); |
| 896 | break; |
| 897 | #endif |
| 898 | |
| 899 | // other messages |
| 900 | case SDL_QUIT: |
| 901 | return 1; |
| 902 | |
| 903 | case SDL_DROPFILE: |
| 904 | str_copy(dst&: m_aDropFile, src: Event.drop.file); |
| 905 | SDL_free(mem: Event.drop.file); |
| 906 | break; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | void CInput::ProcessSystemMessage(SDL_SysWMmsg *pMsg) |
| 914 | { |
| 915 | #if defined(CONF_FAMILY_WINDOWS) |
| 916 | // Todo SDL: remove this after SDL2 supports IME candidates |
| 917 | if(pMsg->subsystem == SDL_SYSWM_WINDOWS && pMsg->msg.win.msg == WM_IME_NOTIFY) |
| 918 | { |
| 919 | switch(pMsg->msg.win.wParam) |
| 920 | { |
| 921 | case IMN_OPENCANDIDATE: |
| 922 | case IMN_CHANGECANDIDATE: |
| 923 | { |
| 924 | HWND WindowHandle = pMsg->msg.win.hwnd; |
| 925 | HIMC ImeContext = ImmGetContext(WindowHandle); |
| 926 | DWORD Size = ImmGetCandidateListW(ImeContext, 0, nullptr, 0); |
| 927 | LPCANDIDATELIST pCandidateList = nullptr; |
| 928 | if(Size > 0) |
| 929 | { |
| 930 | pCandidateList = (LPCANDIDATELIST)malloc(Size); |
| 931 | Size = ImmGetCandidateListW(ImeContext, 0, pCandidateList, Size); |
| 932 | } |
| 933 | m_vCandidates.clear(); |
| 934 | if(pCandidateList && Size > 0) |
| 935 | { |
| 936 | m_vCandidates.reserve(std::min(pCandidateList->dwCount - pCandidateList->dwPageStart, pCandidateList->dwPageSize)); |
| 937 | for(DWORD i = pCandidateList->dwPageStart; i < pCandidateList->dwCount && (int)m_vCandidates.size() < (int)pCandidateList->dwPageSize; i++) |
| 938 | { |
| 939 | LPCWSTR pCandidate = (LPCWSTR)((DWORD_PTR)pCandidateList + pCandidateList->dwOffset[i]); |
| 940 | m_vCandidates.push_back(windows_wide_to_utf8(pCandidate).value_or("<invalid candidate>" )); |
| 941 | } |
| 942 | m_CandidateSelectedIndex = pCandidateList->dwSelection - pCandidateList->dwPageStart; |
| 943 | } |
| 944 | else |
| 945 | { |
| 946 | m_CandidateSelectedIndex = -1; |
| 947 | } |
| 948 | free(pCandidateList); |
| 949 | ImmReleaseContext(WindowHandle, ImeContext); |
| 950 | break; |
| 951 | } |
| 952 | case IMN_CLOSECANDIDATE: |
| 953 | m_vCandidates.clear(); |
| 954 | m_CandidateSelectedIndex = -1; |
| 955 | break; |
| 956 | } |
| 957 | } |
| 958 | #endif |
| 959 | } |
| 960 | |
| 961 | #if defined(CONF_PLATFORM_EMSCRIPTEN) |
| 962 | extern "C" { |
| 963 | |
| 964 | // This will be called from Emscripten JS code |
| 965 | char aEmscriptenDropFile[IO_MAX_PATH_LENGTH] = "" ; |
| 966 | void EmscriptenCallbackDropFile(const char *pFile) |
| 967 | { |
| 968 | str_copy(aEmscriptenDropFile, pFile); |
| 969 | } |
| 970 | } |
| 971 | #endif |
| 972 | |
| 973 | bool CInput::GetDropFile(char *aBuf, int Len) |
| 974 | { |
| 975 | #if defined(CONF_PLATFORM_EMSCRIPTEN) |
| 976 | if(aEmscriptenDropFile[0] != '\0') |
| 977 | { |
| 978 | str_copy(aBuf, aEmscriptenDropFile, Len); |
| 979 | aEmscriptenDropFile[0] = '\0'; |
| 980 | return true; |
| 981 | } |
| 982 | #endif |
| 983 | if(m_aDropFile[0] != '\0') |
| 984 | { |
| 985 | str_copy(dst: aBuf, src: m_aDropFile, dst_size: Len); |
| 986 | m_aDropFile[0] = '\0'; |
| 987 | return true; |
| 988 | } |
| 989 | return false; |
| 990 | } |
| 991 | |
| 992 | IEngineInput *CreateEngineInput() |
| 993 | { |
| 994 | return new CInput; |
| 995 | } |
| 996 | |