コード例 #1
0
ファイル: setjoystick.cpp プロジェクト: jamesblunt/antimicro
/**
 * @brief Perform a release of all elements of a set. Stick and vdpad
 *     releases will be handled by the associated button or axis.
 */
void SetJoystick::release()
{
    QHashIterator<int, JoyAxis*> iterAxes(axes);
    while (iterAxes.hasNext())
    {
        JoyAxis *axis = iterAxes.next().value();
        axis->clearPendingEvent();
        axis->joyEvent(axis->getCurrentThrottledDeadValue(), true);
    }

    QHashIterator<int, JoyDPad*> iterDPads(hats);
    while (iterDPads.hasNext())
    {
        JoyDPad *dpad = iterDPads.next().value();
        dpad->clearPendingEvent();
        dpad->joyEvent(0, true);
    }

    QHashIterator<int, JoyButton*> iterButtons(buttons);
    while (iterButtons.hasNext())
    {
        JoyButton *button = iterButtons.next().value();
        button->clearPendingEvent();
        button->joyEvent(false, true);
    }
}
コード例 #2
0
ファイル: setjoystick.cpp プロジェクト: jlsandell/antimicro
void SetJoystick::release()
{
    QHashIterator<int, JoyButton*> iter(buttons);
    while (iter.hasNext())
    {
        JoyButton *button = iter.next().value();
        button->joyEvent(false, true);
    }

    QHashIterator<int, JoyAxis*> iter2(axes);
    while (iter2.hasNext())
    {
        JoyAxis *axis = iter2.next().value();
        axis->joyEvent(axis->getCurrentThrottledDeadValue(), true);
    }

    QHashIterator<int, JoyDPad*> iter3(hats);
    while (iter3.hasNext())
    {
        JoyDPad *dpad = iter3.next().value();
        dpad->joyEvent(0, true);
    }
}
コード例 #3
0
ファイル: inputdevice.cpp プロジェクト: Xatory/antimicro
void InputDevice::setActiveSetNumber(int index)
{
    if ((index >= 0 && index < NUMBER_JOYSETS) && (index != active_set))
    {
        QList<bool> buttonstates;
        QList<int> axesstates;
        QList<int> dpadstates;

        // Grab current states for all elements in old set
        SetJoystick *current_set = joystick_sets.value(active_set);
        SetJoystick *old_set = current_set;
        for (int i = 0; i < current_set->getNumberButtons(); i++)
        {
            JoyButton *button = current_set->getJoyButton(i);
            buttonstates.append(button->getButtonState());
        }

        for (int i = 0; i < current_set->getNumberAxes(); i++)
        {
            JoyAxis *axis = current_set->getJoyAxis(i);
            axesstates.append(axis->getCurrentRawValue());
        }

        for (int i = 0; i < current_set->getNumberHats(); i++)
        {
            JoyDPad *dpad = current_set->getJoyDPad(i);
            dpadstates.append(dpad->getCurrentDirection());
        }

        // Release all current pressed elements and change set number
        joystick_sets.value(active_set)->release();
        active_set = index;

        // Activate all buttons in the switched set
        current_set = joystick_sets.value(active_set);
        for (int i = 0; i < current_set->getNumberButtons(); i++)
        {
            bool value = buttonstates.at(i);
            bool tempignore = true;
            JoyButton *button = current_set->getJoyButton(i);
            JoyButton *oldButton = old_set->getJoyButton(i);
            if (button->getChangeSetCondition() == JoyButton::SetChangeWhileHeld)
            {
                if (value)
                {
                    if (oldButton->getChangeSetCondition() == JoyButton::SetChangeWhileHeld && oldButton->getWhileHeldStatus())
                    {
                        // Button from old set involved in a while held set
                        // change. Carry over to new set button to ensure
                        // set changes are done in the proper order.
                        button->setWhileHeldStatus(true);
                    }
                    else if (!button->getWhileHeldStatus())
                    {
                        // Ensure that set change events are performed if needed.
                        tempignore = false;
                    }
                }
                else
                {
                    // Ensure that set change events are performed if needed.
                    button->setWhileHeldStatus(false);
                    //tempignore = false;
                }
            }

            button->joyEvent(value, tempignore);
        }

        // Activate all axis buttons in the switched set
        for (int i = 0; i < current_set->getNumberAxes(); i++)
        {
            int value = axesstates.at(i);
            bool tempignore = true;
            JoyAxis *axis = current_set->getJoyAxis(i);
            JoyAxisButton *oldButton = old_set->getJoyAxis(i)->getAxisButtonByValue(value);
            JoyAxisButton *button = axis->getAxisButtonByValue(value);

            if (button && oldButton)
            {
                if (button->getChangeSetCondition() == JoyButton::SetChangeWhileHeld)
                {
                    if (oldButton->getChangeSetCondition() == JoyButton::SetChangeWhileHeld && oldButton->getWhileHeldStatus())
                    {
                        // Button from old set involved in a while held set
                        // change. Carry over to new set button to ensure
                        // set changes are done in the proper order.
                        button->setWhileHeldStatus(true);
                    }
                    else if (!button->getWhileHeldStatus())
                    {
                        // Ensure that set change events are performed if needed.
                        tempignore = false;
                    }
                }
            }
            else if (!button)
            {
                // Ensure that set change events are performed if needed.
                axis->getPAxisButton()->setWhileHeldStatus(false);
                axis->getNAxisButton()->setWhileHeldStatus(false);
            }

            axis->joyEvent(value, tempignore);
        }

        // Activate all dpad buttons in the switched set
        for (int i = 0; i < current_set->getNumberHats(); i++)
        {
            int value = dpadstates.at(i);
            bool tempignore = true;
            JoyDPad *dpad = current_set->getJoyDPad(i);
            JoyDPadButton *button = dpad->getJoyButton(value);
            JoyDPadButton *oldButton = old_set->getJoyDPad(i)->getJoyButton(value);

            if (button && oldButton)
            {
                if (button->getChangeSetCondition() == JoyButton::SetChangeWhileHeld)
                {
                    if (value)
                    {
                        if (oldButton->getChangeSetCondition() == JoyButton::SetChangeWhileHeld && oldButton->getWhileHeldStatus())
                        {
                            // Button from old set involved in a while held set
                            // change. Carry over to new set button to ensure
                            // set changes are done in the proper order.
                            button->setWhileHeldStatus(true);
                        }
                        else if (!button->getWhileHeldStatus())
                        {
                            // Ensure that set change events are performed if needed.
                            tempignore = false;
                        }
                    }
                }
            }
            else if (!button)
            {
                QHashIterator<int, JoyDPadButton*> iter(*dpad->getJoyButtons());
                while (iter.hasNext())
                {
                    // Ensure that set change events are performed if needed.
                    JoyDPadButton *button = iter.next().value();
                    button->setWhileHeldStatus(false);
                }
            }

            dpad->joyEvent(value, tempignore);
        }
    }
}
コード例 #4
0
ファイル: inputdaemon.cpp プロジェクト: panzi/antimicro
InputDaemon::InputDaemon(QHash<SDL_JoystickID, InputDevice*> *joysticks, bool graphical, QObject *parent) :
#else
InputDaemon::InputDaemon(QHash<int, InputDevice*> *joysticks, bool graphical, QObject *parent) :
#endif
    QObject(parent)
{
    this->joysticks = joysticks;
    this->stopped = false;
    this->graphical = graphical;

    eventWorker = new SDLEventReader(joysticks);
    thread = new QThread();
    eventWorker->moveToThread(thread);

    if (graphical)
    {
        connect(thread, SIGNAL(started()), eventWorker, SLOT(performWork()));
        connect(eventWorker, SIGNAL(eventRaised()), this, SLOT(run()));
        thread->start();
    }
    refreshJoysticks();
}

InputDaemon::~InputDaemon()
{
    if (eventWorker)
    {
        quit();
    }

    if (thread)
    {
        thread->quit();
        thread->wait();
        delete thread;
        thread = 0;
    }
}

void InputDaemon::run ()
{
    SDL_Event event;
#ifdef USE_SDL_2
    event.type = SDL_FIRSTEVENT;

#else
    event.type = SDL_NOEVENT;
#endif

    if (!stopped)
    {
        event = eventWorker->getCurrentEvent();

        do
        {
            switch (event.type)
            {
                case SDL_JOYBUTTONDOWN:
                {
#ifdef USE_SDL_2
                    InputDevice *joy = trackjoysticks.value(event.jbutton.which);
#else
                    InputDevice *joy = joysticks->value(event.jbutton.which);
#endif
                    if (joy)
                    {
                        SetJoystick* set = joy->getActiveSetJoystick();
                        JoyButton *button = set->getJoyButton(event.jbutton.button);

                        if (button)
                        {
                            button->joyEvent(true);
                        }
                    }

                    break;
                }

                case SDL_JOYBUTTONUP:
                {
#ifdef USE_SDL_2
                    InputDevice *joy = trackjoysticks.value(event.jbutton.which);
#else
                    InputDevice *joy = joysticks->value(event.jbutton.which);
#endif
                    if (joy)
                    {
                        SetJoystick* set = joy->getActiveSetJoystick();
                        JoyButton *button = set->getJoyButton(event.jbutton.button);

                        if (button)
                        {
                            button->joyEvent(false);
                        }
                    }

                    break;
                }

                case SDL_JOYAXISMOTION:
                {
#ifdef USE_SDL_2
                    InputDevice *joy = trackjoysticks.value(event.jaxis.which);
#else
                    InputDevice *joy = joysticks->value(event.jaxis.which);
#endif
                    if (joy)
                    {
                        SetJoystick* set = joy->getActiveSetJoystick();
                        JoyAxis *axis = set->getJoyAxis(event.jaxis.axis);
                        if (axis)
                        {
                            axis->joyEvent(event.jaxis.value);
                        }
                    }

                    break;
                }

                case SDL_JOYHATMOTION:
                {
#ifdef USE_SDL_2
                    InputDevice *joy = trackjoysticks.value(event.jhat.which);
#else
                    InputDevice *joy = joysticks->value(event.jhat.which);
#endif
                    if (joy)
                    {
                        SetJoystick* set = joy->getActiveSetJoystick();
                        JoyDPad *dpad = set->getJoyDPad(event.jhat.hat);
                        if (dpad)
                        {
                            dpad->joyEvent(event.jhat.value);
                        }
                    }

                    break;
                }

#ifdef USE_SDL_2
                case SDL_CONTROLLERAXISMOTION:
                {
                    InputDevice *joy = trackcontrollers.value(event.caxis.which);
                    if (joy)
                    {
                        SetJoystick* set = joy->getActiveSetJoystick();
                        JoyAxis *axis = set->getJoyAxis(event.caxis.axis);
                        if (axis)
                        {
                            axis->joyEvent(event.caxis.value);
                        }
                    }
                    break;
                }

                case SDL_CONTROLLERBUTTONDOWN:
                case SDL_CONTROLLERBUTTONUP:
                {
                    InputDevice *joy = trackcontrollers.value(event.cbutton.which);
                    if (joy)
                    {
                        SetJoystick* set = joy->getActiveSetJoystick();
                        JoyButton *button = set->getJoyButton(event.cbutton.button);

                        if (button)
                        {
                            button->joyEvent(event.type == SDL_CONTROLLERBUTTONDOWN ? true : false);
                        }
                    }

                    break;
                }

                case SDL_JOYDEVICEREMOVED:
                {
                    InputDevice *device = joysticks->value(event.jdevice.which);
                    if (device)
                    {
                        removeDevice(device);
                    }

                    break;
                }

                case SDL_JOYDEVICEADDED:
                {
                    addInputDevice(event.jdevice.which);
                    break;
                }
#endif

                case SDL_QUIT:
                {
                    stopped = true;
                    break;
                }

                default:
                    break;
            }
        }
        while (SDL_PollEvent(&event) > 0);
    }

    if (stopped)
    {
        if (joysticks->count() > 0)
        {
            emit complete(joysticks->value(0));
        }
        emit complete();
        stopped = false;

        // Check for a grabbed instance of an SDL_QUIT event. If the last event was
        // not an SDL_QUIT event, push an event onto the queue so SdlEventReader
        // will finish properly.
#ifdef USE_SDL_2
        if (event.type != SDL_FIRSTEVENT && event.type != SDL_QUIT)
#else
        if (event.type != SDL_NOEVENT && event.type != SDL_QUIT)
#endif
        {
            event.type = SDL_QUIT;
            SDL_PushEvent(&event);
            QTimer::singleShot(0, eventWorker, SLOT(performWork()));
        }
    }
    else
    {
        QTimer::singleShot(0, eventWorker, SLOT(performWork()));
    }
}