Keyboard stop working after a while on Linux X11

Problems building or running the engine, queries about how to use features etc.
Post Reply
fernandot
Halfling
Posts: 46
Joined: Thu Jun 07, 2012 7:22 pm
Location: Brazil
x 28
Contact:

Keyboard stop working after a while on Linux X11

Post by fernandot »

Hi,

I'm developing a multiplataform application that runs on windows and linux. i don't have any problem running it on windows, but when a run it on linux using Non-exclusive input my keyoard stops working after a while.

It only happens when i set x11_keyboard_grab as "false"

Does anyone knows what could cause that?

My input Manager code:

Code: Select all


#include "InputManager.h"
#include <QForeachContainer>

#if defined( __WIN32__ )
LRESULT CALLBACK ChildWndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
switch (Message)
    {
    case WM_SETCURSOR:
        {
        static HCURSOR Cursor = LoadCursorFromFile(L"aliendance.ani");
        SetCursor(Cursor);
        return TRUE;
        }
    }
    return TRUE;
}
#endif

InputManager *InputManager::mInputManager;

InputManager::InputManager()
    :mInputSystem(0)
    , mMouse(0)
    , mKeyboard(0)
{
}

InputManager::~InputManager()
{
    if(mInputSystem)
    {
        if (mMouse)
        {
            mInputSystem->destroyInputObject(mMouse);
            mMouse = 0;
        }

        if (mKeyboard)
        {
            mInputSystem->destroyInputObject(mKeyboard);
            mKeyboard = 0;
        }

        if (mJoysticks.size() > 0)
        {
            itJoystick    = mJoysticks.begin();
            itJoystickEnd = mJoysticks.end();
            for(; itJoystick != itJoystickEnd; ++itJoystick)
            {
                    mInputSystem->destroyInputObject(*itJoystick);
            }
        }

        mInputSystem->destroyInputSystem(mInputSystem);
        mInputSystem = 0;

        mKeyListeners.clear();
        mMouseListeners.clear();
        mJoystickListeners.clear();
    }
}

InputManager* InputManager::getSingletonPtr()
{
    if (!mInputManager) {
            mInputManager = new InputManager();
    }

    return mInputManager;
}

void InputManager::Init(Ogre::RenderWindow *renderWindow)
{
    if(!mInputSystem)
    {
        OIS::ParamList paramList;
        size_t windowHnd = 0;
        std::ostringstream windowHndStr;

        renderWindow->getCustomAttribute("WINDOW", &windowHnd);

        windowHndStr << (unsigned int) windowHnd;
        paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));

#if defined OIS_WIN32_PLATFORM
paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND" )));
paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
paramList.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_FOREGROUND")));
paramList.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_NONEXCLUSIVE")));
#elif defined OIS_LINUX_PLATFORM
paramList.insert(std::make_pair(std::string("x11_mouse_grab"), std::string("false")));
paramList.insert(std::make_pair(std::string("x11_mouse_hide"), std::string("false")));
paramList.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("false")));
paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
#endif

        mInputSystem = OIS::InputManager::createInputSystem(paramList);

        //Ogre::WindowEventUtilities::addWindowEventListener(renderWindow, this);

        //if (mInputSystem->numKeyboards() > 0)
        //{
                mKeyboard = static_cast<OIS::Keyboard*>(mInputSystem->createInputObject(OIS::OISKeyboard, true));
                mKeyboard->setEventCallback(this);
        //}

        //if (mInputSystem->numMice() > 0)
        //{
            mMouse = static_cast<OIS::Mouse*>(mInputSystem->createInputObject(OIS::OISMouse, true));
            mMouse->setEventCallback(this);

            unsigned int width, height, depth;
            int left, top;
            renderWindow->getMetrics(width, height, depth, left, top);

            this->setWindowExtents(width, height);
        //}

        //if (mInputSystem->numJoySticks() > 0)
        //{
                //mJoysticks.resize(mInputSystem->numJoySticks());

            itJoystick    = mJoysticks.begin();
            itJoystickEnd = mJoysticks.end();
            for(; itJoystick != itJoystickEnd; ++itJoystick)
            {
                try
                {
                    (*itJoystick) = static_cast<OIS::JoyStick*>(mInputSystem->createInputObject(OIS::OISJoyStick, true));
                }
                catch (...)
                {
                    (*itJoystick) = 0;
                }

                (*itJoystick)->setEventCallback(this);
            }
        //}
    }
}

void InputManager::Quit()
{
    mQuit = true;
}

void InputManager::Capture()
{
    //if (mMouse) {
        mMouse->capture();
    //}

    //if (mKeyboard) {
        mKeyboard->capture();
    //}

    if (mJoysticks.size() > 0)
    {
        itJoystick    = mJoysticks.begin();
        itJoystickEnd = mJoysticks.end();
        for(; itJoystick != itJoystickEnd; ++itJoystick) {
                (*itJoystick)->capture();
        }
    }
}

void InputManager::addKeyListener(OIS::KeyListener *keyListener, const std::string& name)
{
    if (mKeyboard)
    {
        itKeyListener = mKeyListeners.find(name);
        if (itKeyListener == mKeyListeners.end()) {
                mKeyListeners[name] = keyListener;
        }
        else {
    // Duplicate Item
        }
    }
}

void InputManager::addMouseListener(OIS::MouseListener *mouseListener, const std::string& name)
{
    if (mMouse)
    {
        itMouseListener = mMouseListeners.find(name);
        if (itMouseListener == mMouseListeners.end()) {
                mMouseListeners[name] = mouseListener;
        }
        else {
                // Duplicate Item
        }
    }
}

void InputManager::addJoystickListener(OIS::JoyStickListener *joystickListener, const std::string& name)
{
    if (mJoysticks.size() > 0)
    {
        itJoystickListener = mJoystickListeners.find( name );
        if (itJoystickListener == mJoystickListeners.end()) {
            mJoystickListeners[name] = joystickListener;
        }
        else {
                // Duplicate Item
        }
    }
}
void InputManager::CleanListeners()
{
    foreach (std::string name, mRemoveKeyListeners)
    {
        itKeyListener = mKeyListeners.find(name);
        if (itKeyListener != mKeyListeners.end()) {
                mKeyListeners.erase(itKeyListener);
        }
    }
    foreach (std::string name, mRemoveMouseListeners)
    {
        itMouseListener = mMouseListeners.find( name );
        if (itMouseListener != mMouseListeners.end()) {
                mMouseListeners.erase(itMouseListener);
        }
    }
    foreach (std::string name, mRemoveJoystickListeners)
    {
        itJoystickListener = mJoystickListeners.find(name);
        if (itJoystickListener != mJoystickListeners.end()) {
            mJoystickListeners.erase( itJoystickListener );
        }
    }
    foreach (OIS::KeyListener *keyListener, mRemoveKeyListeners2)
    {
        itKeyListener    = mKeyListeners.begin();
        itKeyListenerEnd = mKeyListeners.end();

        for (; itKeyListener != itKeyListenerEnd; ++itKeyListener)
        {
            if (itKeyListener->second == keyListener)
            {
                    mKeyListeners.erase(itKeyListener);
                    itKeyListener = mKeyListeners.end();
                    break;
            }
        }

    }
    foreach (OIS::MouseListener * mouseListener, mRemoveMouseListeners2)
    {
        itMouseListener    = mMouseListeners.begin();
        itMouseListenerEnd = mMouseListeners.end();

        for(; itMouseListener != itMouseListenerEnd; ++itMouseListener)
        {
                if (itMouseListener->second == mouseListener)
                {
                        mMouseListeners.erase(itMouseListener);
                        break;
                }
        }
    }
    foreach (OIS::JoyStickListener* joystickListener, mRemoveJoystickListeners2)
    {
        itJoystickListener    = mJoystickListeners.begin();
        itJoystickListenerEnd = mJoystickListeners.end();

        for(; itJoystickListener != itJoystickListenerEnd; ++itJoystickListener)
        {
                if (itJoystickListener->second == joystickListener)
                {
                        mJoystickListeners.erase( itJoystickListener);
                        break;
                }
        }
    }

}

void InputManager::removeKeyListener(const std::string& name)
{
    mRemoveKeyListeners.push_back(name);
}

void InputManager::removeMouseListener(const std::string& name)
{
    mRemoveMouseListeners.push_back(name);
}

void InputManager::removeJoystickListener(const std::string& name)
{
    mRemoveJoystickListeners.push_back(name);
}

void InputManager::removeKeyListener(OIS::KeyListener *keyListener)
{
    mRemoveKeyListeners2.push_back(keyListener);
}

void InputManager::removeMouseListener(OIS::MouseListener *mouseListener)
{
    mRemoveMouseListeners2.push_back(mouseListener);
}

void InputManager::removeJoystickListener(OIS::JoyStickListener *joystickListener)
{
    mRemoveJoystickListeners2.push_back(joystickListener);
}

void InputManager::removeAllListeners()
{
    mKeyListeners.clear();
    mMouseListeners.clear();
    mJoystickListeners.clear();
}

void InputManager::removeAllKeyListeners()
{
    mKeyListeners.clear();
}

void InputManager::removeAllMouseListeners()
{
    mMouseListeners.clear();
}

void InputManager::removeAllJoystickListeners()
{
    mJoystickListeners.clear();
}

void InputManager::setWindowExtents(int width, int height)
{
    const OIS::MouseState &mouseState = mMouse->getMouseState();
    mouseState.width  = width;
    mouseState.height = height;
}

OIS::Mouse* InputManager::getMouse()
{
    return mMouse;
}

OIS::Keyboard* InputManager::getKeyboard()
{
    return mKeyboard;
}

OIS::JoyStick* InputManager::getJoystick(unsigned int index)
{
    if (index < mJoysticks.size()) {
            return mJoysticks[index];
    }

    return 0;
}

int InputManager::getNumOfJoysticks()
{
    return (int) mJoysticks.size();
}

bool InputManager::keyPressed(const OIS::KeyEvent &e)
{
    CleanListeners();
    itKeyListener    = mKeyListeners.begin();
    itKeyListenerEnd = mKeyListeners.end();
    std::map<std::string, OIS::KeyListener*>::iterator it;

    for (; itKeyListener != itKeyListenerEnd; ) {
        it = itKeyListener;
            if (!itKeyListener->second->keyPressed(e))
                    break;
                    if(itKeyListener==it)
            ++itKeyListener;
    }

    return true;
}

bool InputManager::keyReleased(const OIS::KeyEvent &e)
{
    CleanListeners();
    itKeyListener    = mKeyListeners.begin();
    itKeyListenerEnd = mKeyListeners.end();

    for (; itKeyListener != itKeyListenerEnd; ++itKeyListener) {
            if (!itKeyListener->second->keyReleased(e))
                    break;
    }

    return true;
}

bool InputManager::mouseMoved(const OIS::MouseEvent &e)
{
    CleanListeners();
    itMouseListener    = mMouseListeners.begin();
    itMouseListenerEnd = mMouseListeners.end();

    for (; itMouseListener != itMouseListenerEnd; ++itMouseListener) {
            if (!itMouseListener->second->mouseMoved(e))
                    break;
    }

    return true;
}

bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
{
    CleanListeners();
    itMouseListener    = mMouseListeners.begin();
    itMouseListenerEnd = mMouseListeners.end();

    for (; itMouseListener != itMouseListenerEnd; ++itMouseListener) {
            if (!itMouseListener->second->mousePressed(e, id))
                    break;
    }

    return true;
}
bool InputManager::mouseReleased( const OIS::MouseEvent &e, OIS::MouseButtonID id)
{
    CleanListeners();
    itMouseListener    = mMouseListeners.begin();
    itMouseListenerEnd = mMouseListeners.end();

    for (; itMouseListener != itMouseListenerEnd; ++itMouseListener) {
            if (!itMouseListener->second->mouseReleased(e, id))
                    break;
    }

    return true;
}

bool InputManager::povMoved(const OIS::JoyStickEvent &e, int pov)
{
    CleanListeners();
    itJoystickListener    = mJoystickListeners.begin();
    itJoystickListenerEnd = mJoystickListeners.end();

    for (; itJoystickListener != itJoystickListenerEnd; ++itJoystickListener) {
            if (!itJoystickListener->second->povMoved(e, pov))
                    break;
    }

    return true;
}

bool InputManager::axisMoved(const OIS::JoyStickEvent &e, int axis)
{
    CleanListeners();
    itJoystickListener    = mJoystickListeners.begin();
    itJoystickListenerEnd = mJoystickListeners.end();

    for (; itJoystickListener != itJoystickListenerEnd; ++itJoystickListener) {
            if (!itJoystickListener->second->axisMoved(e, axis))
                    break;
    }

    return true;
}

bool InputManager::sliderMoved(const OIS::JoyStickEvent &e, int sliderID)
{
    CleanListeners();
    itJoystickListener    = mJoystickListeners.begin();
    itJoystickListenerEnd = mJoystickListeners.end();

    for (; itJoystickListener != itJoystickListenerEnd; ++itJoystickListener) {
            if (!itJoystickListener->second->sliderMoved(e, sliderID))
                    break;
    }

    return true;
}

bool InputManager::buttonPressed(const OIS::JoyStickEvent &e, int button)
{
    CleanListeners();
    itJoystickListener    = mJoystickListeners.begin();
    itJoystickListenerEnd = mJoystickListeners.end();

    for (; itJoystickListener != itJoystickListenerEnd; ++itJoystickListener) {
            if (!itJoystickListener->second->buttonPressed(e, button))
                    break;
    }

    return true;
}

bool InputManager::buttonReleased(const OIS::JoyStickEvent &e, int button)
{
    CleanListeners();
    itJoystickListener    = mJoystickListeners.begin();
    itJoystickListenerEnd = mJoystickListeners.end();

    for (; itJoystickListener != itJoystickListenerEnd; ++itJoystickListener) {
            if (!itJoystickListener->second->buttonReleased(e, button))
                    break;
    }

    return true;
}
agnoob
Gnoblar
Posts: 7
Joined: Wed Jun 24, 2009 7:41 am

Re: Keyboard stop working after a while on Linux X11

Post by agnoob »

I just ran into this same problem (and spent more time than I want to admit tracking it down). My keyboard would stop responding after about 20 seconds (even if nothing happening in game/ogre). I could work around it by setting x11_keyboard_grab to true. But that wasn't an acceptable fix to me.

The problem went away when I switched back to OIS-1.2.0 (it was OIS-1.3 that didn't work for me in Gentoo Linux). I'm assuming it is a bug. There seems to be existing reports of it. Hopefully it will be solved going forward.
xelfe
Gnoblar
Posts: 10
Joined: Wed Aug 14, 2013 5:24 am

Re: Keyboard stop working after a while on Linux X11

Post by xelfe »

I bump this post. I have the same problem on linux. There's any way to fix this without downgrade OIS ?
Xelfe from Xsilium project
https://github.com/xsilium-frameworks
underworldguardian
Gnoblar
Posts: 12
Joined: Thu Nov 13, 2014 3:08 pm

Re: Keyboard stop working after a while on Linux X11

Post by underworldguardian »

I bump this post as well. If I set

Code: Select all

 pl.insert(std::make_pair(std::string("x11_keyboard_grab"),std::string("false")));
the Keyboard stops responding after 31 seconds. I used a program internal timer to find it out.

After 31 seconds, none of the keyboard events get through to the keyPressed(...) and keyReleased(...) methods. I use buffered input with OIS 1.3.0 Anybody knows a fix for that? It seems that we have to downgrade, because OIS development seems to be quite dead.
Post Reply