Beispiel #1
0
void SDLIntegration::update(osgViewer::Viewer& viewer)
{
    if (_joystick)
    {
    
        ValueList newAxisValues;
        ValueList newButtonValues;
        
        capture(newAxisValues, newButtonValues);
        
        unsigned int mouseXaxis = 0;
        unsigned int mouseYaxis = 1;
        
        float prev_mx = (float)_axisValues[mouseXaxis]/32767.0f;
        float prev_my = -(float)_axisValues[mouseYaxis]/32767.0f;

        float mx = (float)newAxisValues[mouseXaxis]/32767.0f;
        float my = -(float)newAxisValues[mouseYaxis]/32767.0f;
        

        osgGA::EventQueue* eq = viewer.getEventQueue();
        double time = eq ? eq->getTime() : 0.0;
        osgGA::GUIEventAdapter* previous_event = eq->getCurrentEventState();
        float projected_mx = previous_event->getXmin() + (mx+1.0)*0.5*(previous_event->getXmax()-previous_event->getXmin());
        float projected_my = previous_event->getYmin() + (my+1.0)*0.5*(previous_event->getYmax()-previous_event->getYmin());

        if (mx!=prev_mx || my!=prev_my)
        {
            eq->mouseMotion(projected_mx, projected_my, time);
        }


        if (_verbose)
        {
            for(int ai=0; ai<_numAxes; ++ai)
            {
                if (newAxisValues[ai]!=_axisValues[ai])
                {
                    std::cout<<"axis "<<ai<<" moved to "<<newAxisValues[ai]<<std::endl;
                }
            }
        }
                
        for(int bi=0; bi<_numButtons; ++bi)
        {
            if (newButtonValues[bi]!=_buttonValues[bi])
            {
                if (_verbose)
                {
                    std::cout<<"button "<<bi<<" changed to "<<newButtonValues[bi]<<std::endl;
                }

                int key =  getKeyMapping(bi);
                int mouseButton =  getMouseButtonMapping(bi);

                if (mouseButton>0)
                {
                    if (newButtonValues[bi]==0) eq->mouseButtonRelease(projected_mx,projected_my,mouseButton,time);
                    else  eq->mouseButtonPress(projected_mx,projected_my,mouseButton,time);
                }
                else if (key>0)
                {

                    if (newButtonValues[bi]==0) eq->keyRelease(key,time);
                    else eq->keyPress(key,time);
                }
            }
        }
        
        _axisValues.swap(newAxisValues);
        _buttonValues.swap(newButtonValues);
        
    }

}
Beispiel #2
0
static int _EVT_eventThread(
#endif
    void *arg)
{
    event_t                 evt;
    int                     c;
    char                    ch;
    static struct kbentry   ke;
    static ushort           repeatKey[128] = {0};
    fd_set                  s;
    struct timeval          *ptv = NULL;

    for (;;) {
        FD_ZERO(&s);
        FD_SET(_PM_console_fd, &s);
        while (select(_PM_console_fd+1, &s, NULL, NULL, ptv) > 0) {
            if (read(_PM_console_fd, &ch, 1) > 0) {
                evt.when = _EVT_getTicks();
                ENTER_CRITICAL_SECTION();
                if (_PM_haveConsole) {
                    /* Handle keyboard for real consoles */
                    c = ch & 0x7F;
                    if (ch & 0x80) {
                        /* Key released */
                        evt.what = EVT_KEYUP;
                        switch (c) {
                            case KB_leftShift:
                                _PM_modifiers &= ~EVT_LEFTSHIFT;
                                break;
                            case KB_rightShift:
                                _PM_modifiers &= ~EVT_RIGHTSHIFT;
                                break;
                            case 29:
                                _PM_modifiers &= ~(EVT_LEFTCTRL|EVT_CTRLSTATE);
                                break;
                            case 97:            /* Control */
                                _PM_modifiers &= ~EVT_CTRLSTATE;
                                break;
                            case 56:
                                _PM_modifiers &= ~(EVT_LEFTALT|EVT_ALTSTATE);
                                break;
                            case 100:
                                _PM_modifiers &= ~EVT_ALTSTATE;
                                break;
                            }
                        evt.modifiers = _PM_modifiers;
                        evt.message = keyUpMsg[c];
                        if (EVT.count < EVENTQSIZE)
                            addEvent(&evt);
                        keyUpMsg[c] = 0;
                        repeatKey[c] = 0;
                        }
                    else {
                        /* Key pressed */
                        evt.what = EVT_KEYDOWN;
                        switch (c) {
                            case KB_leftShift:
                                _PM_modifiers |= EVT_LEFTSHIFT;
                                break;
                            case KB_rightShift:
                                _PM_modifiers |= EVT_RIGHTSHIFT;
                                break;
                            case 29:
                                _PM_modifiers |= EVT_LEFTCTRL|EVT_CTRLSTATE;
                                break;
                            case 97:            /* Control */
                                _PM_modifiers |= EVT_CTRLSTATE;
                                break;
                            case 56:
                                _PM_modifiers |= EVT_LEFTALT|EVT_ALTSTATE;
                                break;
                            case 100:
                                _PM_modifiers |= EVT_ALTSTATE;
                                break;
                            case KB_capsLock:   /* Caps Lock */
                                _PM_leds ^= LED_CAP;
                                ioctl(_PM_console_fd, KDSETLED, _PM_leds);
                                break;
                            case KB_numLock:    /* Num Lock */
                                _PM_leds ^= LED_NUM;
                                ioctl(_PM_console_fd, KDSETLED, _PM_leds);
                                break;
                            case KB_scrollLock: /* Scroll Lock */
                                _PM_leds ^= LED_SCR;
                                ioctl(_PM_console_fd, KDSETLED, _PM_leds);
                                break;
                            }
                        evt.modifiers = _PM_modifiers;
                        if (keyUpMsg[c]) {
                            evt.what = EVT_KEYREPEAT;
                            evt.message = keyUpMsg[c] | (repeatKey[c]++ << 16);
                            }
                        else {
                            int asc;
    
                            evt.message = getKeyMapping(keymaps, NB_KEYMAPS, c) << 8;
                            if ((_PM_leds & LED_NUM) && (getKeyMapping(keypad, NB_KEYPAD, c) != c))
                                asc = getKeyMapping(keypad, NB_KEYPAD, c);
                            else {
                                ke.kb_index = c;
                                ke.kb_table = 0;
                                if ((_PM_modifiers & EVT_SHIFTKEY) || (_PM_leds & LED_CAP))
                                    ke.kb_table |= K_SHIFTTAB;
                                if (_PM_modifiers & (EVT_LEFTALT | EVT_ALTSTATE))
                                    ke.kb_table |= K_ALTTAB;
                                if (ioctl(_PM_console_fd, KDGKBENT, (unsigned long)&ke) < 0)
                                    perror("ioctl(KDGKBENT)");
                                switch (c) {
                                    case 14:
                                        asc = ASCII_backspace;
                                        break;
                                    case 15:
                                        asc = ASCII_tab;
                                        break;
                                    case 28:
                                    case 96:
                                        asc = ASCII_enter;
                                        break;
                                    case 1:
                                        asc = ASCII_esc;
                                    default:
                                        asc = ke.kb_value & 0xFF;
                                        if (asc < 0x1B)
                                            asc = 0;
                                        break;
                                    }
                                }
                            if ((_PM_modifiers & (EVT_CTRLSTATE|EVT_LEFTCTRL)) && isalpha(asc))
                                evt.message |= toupper(asc) - 'A' + 1;
                            else
                                evt.message |= asc;
                            keyUpMsg[c] = evt.message;
                            repeatKey[c]++;
                            }
                        if (EVT.count < EVENTQSIZE)
                            addEvent(&evt);
                        }
                    }
                else {                                            
                    /* Handle serial/SSH console support with the keyboard */
                    // TODO: Add some basic translation for arrow keys etc!
                    evt.what = EVT_KEYDOWN;
                    evt.modifiers = 0;
                    evt.message = ch;
                    if (EVT.count < EVENTQSIZE)
                        addEvent(&evt);
                    evt.what = EVT_KEYUP;
                    if (EVT.count < EVENTQSIZE)
                        addEvent(&evt);
                    }                                
                LEAVE_CRITICAL_SECTION();
                }
            FD_ZERO(&s);
            FD_SET(_PM_console_fd, &s);
            }
        }
    return 0;
}