void process_in_thread(int joy_fd, JoystickToMIDIMapper *joystickToMIDIMapper)
{
    struct js_event event;

    while (started)
    {
        while (read (joy_fd, &event, sizeof(struct js_event)) > 0)
        {
            if (event.type & JS_EVENT_INIT)
            {
                // TODO Support init events in JoystickEvent?
                printEventInfo(event);
            }

            if (event.type == JS_EVENT_BUTTON)
            {
                //printEventInfo(event);
                bool buttonDown = static_cast<bool>(event.value);
                JoystickEvent::Type eventType = buttonDown ? JoystickEvent::ButtonDown : JoystickEvent::ButtonUp;
                joystickToMIDIMapper->handleJoystickEvent(JoystickEvent(eventType, event.number, event.value, joy_fd));
            }

            if (event.type == JS_EVENT_AXIS)
            {
                //printEventInfo(event);
                float value = mapToFloat(event.value);
                joystickToMIDIMapper->handleJoystickEvent(JoystickEvent(JoystickEvent::Axis, event.number, value, joy_fd));
            }
        }

        // When reading events in non-blocking mode a return does not necessarily
        // mean that there was an error because -1 is returned when there are no
        // more events to read. We have to check for real errors using errno.
        if (errno != EAGAIN)
        {
            // TODO Is executed when the joystick is pulled out. Handle correctly!
            throw std::exception();
        }

        // TODO MGR: Is this the way to do it? It seems clumsy (but it works).
        std::chrono::milliseconds duration(5);
        std::this_thread::sleep_for(duration);
    }
}
示例#2
0
void next_event (packet * next_pkt){

    if(SDL_PollEvent(&curEvent)){

        if(printMode == VERBOSE){

            printEventInfo(&curEvent, 
                           ((ctrlmode == GAMEPAD) ?
                            (SDL_JOYAXISMOTION |
                             SDL_JOYBUTTONUP |
                             SDL_JOYBUTTONDOWN |
                             SDL_KEYDOWN |
                             SDL_KEYUP) :
                            (SDL_KEYDOWN |
                             SDL_KEYUP)));
        }

        if(curEvent.type == SDL_JOYAXISMOTION){

            SDL_JoyAxisEvent jevent = curEvent.jaxis;

            switch(jevent.axis){

                case XLSTICK_X:

                    if(jevent.value < JOY_THRESH_LEFT)
                        cur_turn = TRN_LEFT;
                    
                    else if(jevent.value > JOY_THRESH_RIGHT)
                        cur_turn = TRN_RIGHT;

                    else
                        cur_turn = TRN_NONE;

                    next_pkt->type = PKT_TURN;
                    next_pkt->value = cur_turn;
                    break;

                case XLSTICK_Y:

                    if(jevent.value > JOY_THRESH_DOWN)
                        cur_move = MOV_BKD;

                    else if(jevent.value < JOY_THRESH_UP)
                        cur_move = MOV_FWD;

                    else
                        cur_move = MOV_STOP;

                    next_pkt->type = PKT_MOVE;
                    next_pkt->value = cur_move;
                    break;
                
                case XRSTICK_X:

                    if(jevent.value < JOY_THRESH_LEFT)
                        cur_h_aim = AIM_H_LEFT;
                    
                    else if(jevent.value > JOY_THRESH_RIGHT)
                        cur_h_aim = AIM_H_RIGHT;

                    else
                        cur_h_aim = AIM_H_STRGHT;

                    next_pkt->type = PKT_AIM_H;
                    next_pkt->value = cur_h_aim;
                    break;

                case XRSTICK_Y:

                    if(jevent.value > JOY_THRESH_DOWN)
                        cur_v_aim = AIM_V_DWN;
                    
                    else if(jevent.value < JOY_THRESH_UP)
                        cur_v_aim = AIM_V_UP;

                    else
                        cur_v_aim = AIM_V_STRGHT;

                    next_pkt->type = PKT_AIM_V;
                    next_pkt->value = cur_v_aim;
                    break;

                case XRTRIG:

                    if(jevent.value < TRIG_THRESH)
                        cur_fire = FIRE_OFF;

                    else
                        cur_fire = FIRE_ON;

                    next_pkt->type = PKT_FIRE;
                    next_pkt->value = cur_fire;
                    break;
            }
        }

        else if((curEvent.type == SDL_JOYBUTTONDOWN) &&
                (curEvent.type == SDL_JOYBUTTONUP)){

            SDL_JoyButtonEvent bevent = curEvent.jbutton;

            switch(bevent.button){

                case XBTN_LBUMP:

                    if(bevent.state == SDL_PRESSED)
                        cur_l_strf = STRF_L_ON;

                    else
                        cur_l_strf = STRF_L_OFF;

                    next_pkt->type = PKT_STRF_L;
                    next_pkt->value = cur_l_strf;
                    break;

                case XBTN_RBUMP:

                    if(bevent.state == SDL_PRESSED)
                        cur_r_strf = STRF_R_ON;

                    else
                        cur_r_strf = STRF_R_OFF;

                    next_pkt->type = PKT_STRF_R;
                    next_pkt->value = cur_r_strf;
                    break;
            }
        }
                 
        else if(curEvent.type == SDL_KEYDOWN &&
           (curEvent.key.keysym.sym == SDLK_q) && 
           (curEvent.key.keysym.mod & KMOD_CTRL)){

            quit_base();
        }
    }
    else                                // If no pending events
        next_pkt->type = PKT_STDBY;
}