void CInput::Init() { m_pGraphics = Kernel()->RequestInterface<IEngineGraphics>(); m_pConsole = Kernel()->RequestInterface<IConsole>(); // FIXME: unicode handling: use SDL_StartTextInput/SDL_StopTextInput on inputs MouseModeRelative(); if(!SDL_WasInit(SDL_INIT_JOYSTICK)) { if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) { dbg_msg("joystick", "unable to init SDL joystick: %s", SDL_GetError()); return; } } if(SDL_NumJoysticks() > 0) { m_pJoystick = SDL_JoystickOpen(0); if(!m_pJoystick) { dbg_msg("joystick", "Could not open 0th joystick: %s", SDL_GetError()); return; } dbg_msg("joystick", "Opened Joystick 0"); dbg_msg("joystick", "Name: %s", SDL_JoystickNameForIndex(0)); dbg_msg("joystick", "Number of Axes: %d", SDL_JoystickNumAxes(m_pJoystick)); dbg_msg("joystick", "Number of Buttons: %d", SDL_JoystickNumButtons(m_pJoystick)); dbg_msg("joystick", "Number of Balls: %d", SDL_JoystickNumBalls(m_pJoystick)); } else { dbg_msg("joystick", "No joysticks found"); } }
void CInput::Init() { m_pGraphics = Kernel()->RequestInterface<IEngineGraphics>(); MouseModeRelative(); }
int CInput::Update() { // keep the counter between 1..0xFFFF, 0 means not pressed m_InputCounter = (m_InputCounter%0xFFFF)+1; // these states must always be updated manually because they are not in the GetKeyState from SDL int i = SDL_GetMouseState(NULL, NULL); if(i&SDL_BUTTON(1)) m_aInputState[KEY_MOUSE_1] = 1; // 1 is left if(i&SDL_BUTTON(3)) m_aInputState[KEY_MOUSE_2] = 1; // 3 is right if(i&SDL_BUTTON(2)) m_aInputState[KEY_MOUSE_3] = 1; // 2 is middle if(i&SDL_BUTTON(4)) m_aInputState[KEY_MOUSE_4] = 1; if(i&SDL_BUTTON(5)) m_aInputState[KEY_MOUSE_5] = 1; if(i&SDL_BUTTON(6)) m_aInputState[KEY_MOUSE_6] = 1; if(i&SDL_BUTTON(7)) m_aInputState[KEY_MOUSE_7] = 1; if(i&SDL_BUTTON(8)) m_aInputState[KEY_MOUSE_8] = 1; if(i&SDL_BUTTON(9)) m_aInputState[KEY_MOUSE_9] = 1; { SDL_Event Event; bool IgnoreKeys = false; while(SDL_PollEvent(&Event)) { int Key = -1; int Scancode = 0; int Action = IInput::FLAG_PRESS; switch (Event.type) { case SDL_TEXTINPUT: AddEvent(Event.text.text, 0, IInput::FLAG_TEXT); break; // handle keys case SDL_KEYDOWN: // See SDL_Keymod for possible modifiers: // NONE = 0 // LSHIFT = 1 // RSHIFT = 2 // LCTRL = 64 // RCTRL = 128 // LALT = 256 // RALT = 512 // LGUI = 1024 // RGUI = 2048 // NUM = 4096 // CAPS = 8192 // MODE = 16384 // Sum if you want to ignore multiple modifiers. if(!(Event.key.keysym.mod & g_Config.m_InpIgnoredModifiers)) { Key = KeycodeToKey(Event.key.keysym.sym); Scancode = Event.key.keysym.scancode; } break; case SDL_KEYUP: Action = IInput::FLAG_RELEASE; Key = KeycodeToKey(Event.key.keysym.sym); Scancode = Event.key.keysym.scancode; break; // handle mouse buttons case SDL_MOUSEBUTTONUP: Action = IInput::FLAG_RELEASE; if(Event.button.button == 1) // ignore_convention { m_ReleaseDelta = time_get() - m_LastRelease; m_LastRelease = time_get(); } // fall through case SDL_MOUSEBUTTONDOWN: if(Event.button.button == SDL_BUTTON_LEFT) Key = KEY_MOUSE_1; // ignore_convention if(Event.button.button == SDL_BUTTON_RIGHT) Key = KEY_MOUSE_2; // ignore_convention if(Event.button.button == SDL_BUTTON_MIDDLE) Key = KEY_MOUSE_3; // ignore_convention if(Event.button.button == SDL_BUTTON_X1) Key = KEY_MOUSE_4; // ignore_convention if(Event.button.button == SDL_BUTTON_X2) Key = KEY_MOUSE_5; // ignore_convention if(Event.button.button == 6) Key = KEY_MOUSE_6; // ignore_convention if(Event.button.button == 7) Key = KEY_MOUSE_7; // ignore_convention if(Event.button.button == 8) Key = KEY_MOUSE_8; // ignore_convention if(Event.button.button == 9) Key = KEY_MOUSE_9; // ignore_convention Scancode = Key; break; case SDL_MOUSEWHEEL: if(Event.wheel.y > 0) Key = KEY_MOUSE_WHEEL_UP; // ignore_convention if(Event.wheel.y < 0) Key = KEY_MOUSE_WHEEL_DOWN; // ignore_convention Action |= IInput::FLAG_RELEASE; Scancode = Key; break; case SDL_WINDOWEVENT: // Ignore keys following a focus gain as they may be part of global // shortcuts switch (Event.window.event) { case SDL_WINDOWEVENT_RESIZED: #if defined(SDL_VIDEO_DRIVER_X11) Graphics()->Resize(Event.window.data1, Event.window.data2); #elif defined(__ANDROID__) m_VideoRestartNeeded = 1; #endif break; case SDL_WINDOWEVENT_FOCUS_GAINED: if(m_InputGrabbed) MouseModeRelative(); m_MouseFocus = true; IgnoreKeys = true; break; case SDL_WINDOWEVENT_FOCUS_LOST: m_MouseFocus = false; IgnoreKeys = true; if(m_InputGrabbed) { MouseModeAbsolute(); // Remember that we had relative mouse m_InputGrabbed = true; } break; #if defined(CONF_PLATFORM_MACOSX) // Todo: remove this when fixed in SDL case SDL_WINDOWEVENT_MAXIMIZED: MouseModeAbsolute(); MouseModeRelative(); break; #endif } break; // other messages case SDL_QUIT: return 1; } static bool HoldKeys[512] = {false}; if(Key >= 0 && Key < g_MaxKeys && !IgnoreKeys) { if(Action&IInput::FLAG_PRESS) { m_aInputState[Scancode] = 1; m_aInputCount[Key] = m_InputCounter; // EVENT CALL if(!HoldKeys[Key]) { //LastKey = Key; for(int ijdfg = 0; ijdfg < CLua::Client()->Lua()->GetLuaFiles().size(); ijdfg++) { if(CLua::Client()->Lua()->GetLuaFiles()[ijdfg]->State() != CLuaFile::LUAFILE_STATE_LOADED) continue; LuaRef lfunc = CLua::Client()->Lua()->GetLuaFiles()[ijdfg]->GetFunc("OnKeyPress"); if(lfunc) try { lfunc(IInput::KeyName(Key)); } catch(std::exception &e) { CLua::Client()->Lua()->HandleException(e, CLua::Client()->Lua()->GetLuaFiles()[ijdfg]); } } //CLua::LUA_FIRE_EVENT_V("OnKeyPress", IInput::KeyName(Key)); HoldKeys[Key] = true; } } if(Action&IInput::FLAG_RELEASE) { // EVENT CALL for(int ijdfg = 0; ijdfg < CLua::Client()->Lua()->GetLuaFiles().size(); ijdfg++) { if(CLua::Client()->Lua()->GetLuaFiles()[ijdfg]->State() != CLuaFile::LUAFILE_STATE_LOADED) continue; LuaRef lfunc = CLua::Client()->Lua()->GetLuaFiles()[ijdfg]->GetFunc("OnKeyRelease"); if(lfunc) try { lfunc(IInput::KeyName(Key)); } catch(std::exception &e) { CLua::Client()->Lua()->HandleException(e, CLua::Client()->Lua()->GetLuaFiles()[ijdfg]); } } //CLua::LUA_FIRE_EVENT_V("OnKeyRelease", IInput::KeyName(Key)); HoldKeys[Key] = false; } AddEvent(0, Key, Action); } } } return 0; }
int CInput::Update() { // keep the counter between 1..0xFFFF, 0 means not pressed m_InputCounter = (m_InputCounter%0xFFFF)+1; { int i; const Uint8 *pState = SDL_GetKeyboardState(&i); if(i >= KEY_LAST) i = KEY_LAST-1; mem_copy(m_aInputState, pState, i); } // these states must always be updated manually because they are not in the GetKeyState from SDL int i = SDL_GetMouseState(NULL, NULL); if(i&SDL_BUTTON(1)) m_aInputState[KEY_MOUSE_1] = 1; // 1 is left if(i&SDL_BUTTON(3)) m_aInputState[KEY_MOUSE_2] = 1; // 3 is right if(i&SDL_BUTTON(2)) m_aInputState[KEY_MOUSE_3] = 1; // 2 is middle if(i&SDL_BUTTON(4)) m_aInputState[KEY_MOUSE_4] = 1; if(i&SDL_BUTTON(5)) m_aInputState[KEY_MOUSE_5] = 1; if(i&SDL_BUTTON(6)) m_aInputState[KEY_MOUSE_6] = 1; if(i&SDL_BUTTON(7)) m_aInputState[KEY_MOUSE_7] = 1; if(i&SDL_BUTTON(8)) m_aInputState[KEY_MOUSE_8] = 1; if(i&SDL_BUTTON(9)) m_aInputState[KEY_MOUSE_9] = 1; { SDL_Event Event; while(SDL_PollEvent(&Event)) { int Key = -1; int Scancode = 0; int Action = IInput::FLAG_PRESS; switch (Event.type) { case SDL_TEXTINPUT: AddEvent(Event.text.text, 0, IInput::FLAG_TEXT); break; // handle keys case SDL_KEYDOWN: Key = KeycodeToKey(Event.key.keysym.sym); Scancode = Event.key.keysym.scancode; break; case SDL_KEYUP: Action = IInput::FLAG_RELEASE; Key = KeycodeToKey(Event.key.keysym.sym); Scancode = Event.key.keysym.scancode; break; // handle the joystick events case SDL_JOYBUTTONUP: Action = IInput::FLAG_RELEASE; // fall through case SDL_JOYBUTTONDOWN: Key = Event.jbutton.button + KEY_JOYSTICK_BUTTON_0; Scancode = Key; break; case SDL_JOYHATMOTION: switch (Event.jhat.value) { case SDL_HAT_LEFTUP: Key = KEY_JOY_HAT_LEFTUP; Scancode = Key; m_PreviousHat = Key; break; case SDL_HAT_UP: Key = KEY_JOY_HAT_UP; Scancode = Key; m_PreviousHat = Key; break; case SDL_HAT_RIGHTUP: Key = KEY_JOY_HAT_RIGHTUP; Scancode = Key; m_PreviousHat = Key; break; case SDL_HAT_LEFT: Key = KEY_JOY_HAT_LEFT; Scancode = Key; m_PreviousHat = Key; break; case SDL_HAT_CENTERED: Action = IInput::FLAG_RELEASE; Key = m_PreviousHat; Scancode = m_PreviousHat; m_PreviousHat = 0; break; case SDL_HAT_RIGHT: Key = KEY_JOY_HAT_RIGHT; Scancode = Key; m_PreviousHat = Key; break; case SDL_HAT_LEFTDOWN: Key = KEY_JOY_HAT_LEFTDOWN; Scancode = Key; m_PreviousHat = Key; break; case SDL_HAT_DOWN: Key = KEY_JOY_HAT_DOWN; Scancode = Key; m_PreviousHat = Key; break; case SDL_HAT_RIGHTDOWN: Key = KEY_JOY_HAT_RIGHTDOWN; Scancode = Key; m_PreviousHat = Key; break; } break; // handle mouse buttons case SDL_MOUSEBUTTONUP: Action = IInput::FLAG_RELEASE; // fall through case SDL_MOUSEBUTTONDOWN: if(Event.button.button == SDL_BUTTON_LEFT) Key = KEY_MOUSE_1; // ignore_convention if(Event.button.button == SDL_BUTTON_RIGHT) Key = KEY_MOUSE_2; // ignore_convention if(Event.button.button == SDL_BUTTON_MIDDLE) Key = KEY_MOUSE_3; // ignore_convention if(Event.button.button == 4) Key = KEY_MOUSE_4; // ignore_convention if(Event.button.button == 5) Key = KEY_MOUSE_5; // ignore_convention if(Event.button.button == 6) Key = KEY_MOUSE_6; // ignore_convention if(Event.button.button == 7) Key = KEY_MOUSE_7; // ignore_convention if(Event.button.button == 8) Key = KEY_MOUSE_8; // ignore_convention if(Event.button.button == 9) Key = KEY_MOUSE_9; // ignore_convention if(Event.button.button == SDL_BUTTON_LEFT) { if(Event.button.clicks%2 == 0) m_MouseDoubleClick = true; if(Event.button.clicks == 1) m_MouseDoubleClick = false; } Scancode = Key; break; case SDL_MOUSEWHEEL: if(Event.wheel.y > 0) Key = KEY_MOUSE_WHEEL_UP; // ignore_convention if(Event.wheel.y < 0) Key = KEY_MOUSE_WHEEL_DOWN; // ignore_convention Action |= IInput::FLAG_RELEASE; break; #if defined(CONF_PLATFORM_MACOSX) // Todo SDL: remove this when fixed (mouse state is faulty on start) case SDL_WINDOWEVENT: if(Event.window.event == SDL_WINDOWEVENT_MAXIMIZED) { MouseModeAbsolute(); MouseModeRelative(); } break; #endif // other messages case SDL_QUIT: return 1; } // if(Key != -1) { if(Action&IInput::FLAG_PRESS) { m_aInputState[Scancode] = 1; m_aInputCount[Key] = m_InputCounter; } AddEvent(0, Key, Action); } } } return 0; }