Beispiel #1
0
MoveEvent* TouchDevice::ReadSingleTouchEvent(int touch_device)
{
    MoveEvent moveEvent;
    TOUCH_DEVICE_STATUS tds = DEVICE_TOUCH_NONE;;
    int x, y;

    read(touch_device, &tds, sizeof(tds));
    read(touch_device, &x, sizeof(x));
    read(touch_device, &y, sizeof(y));
    switch (tds)
    {
        case DEVICE_TOUCH_START:
            moveEvent.m_action = MoveEvent::ACTION_DOWN;
            break;
        case DEVICE_TOUCH_END:
            moveEvent.m_action = MoveEvent::ACTION_UP;
            break;
        case DEVICE_TOUCH_UPDATE:
            moveEvent.m_action = MoveEvent::ACTION_MOVE;
            break;
        case DEVICE_TOUCH_NONE:
        case DEVICE_TOUCH_POINTER_DOWN:
        case DEVICE_TOUCH_POINTER_UP:
        default:
            break;
    }
    x = GetScreenHorizonPostion(x);
    y = GetScreenVerticalPostion(y);
    moveEvent.m_x[0] = x;
    moveEvent.m_y[0] = y;
    moveEvent.m_pointerId[0] = 0;
    moveEvent.m_pointerCount = 1;
    moveEvent.m_eventTime = SystemUtil::GetUpdateTimeInMs();
    return moveEvent.Clone();
}
Beispiel #2
0
//maybe just update one of x/y
MoveEvent* TouchDevice::ReadSingleTouchEvent(int _touch_device)
{
    struct input_event iebuf;
    MoveEvent moveEvent;
    int moveAction = MoveEvent::ACTION_MASK;
    int curActionIndex = -1;
    int curIndex = -1;
    static int s_oldX[2] = {-1, -1};
    static int s_oldY[2] = {-1, -1};
    static int index2Slots[2] ={-1, -1};
    static int64_t downTime = -1;
    int readCount = 0;
    for (;;)
    {
        if (read(_touch_device, &iebuf, sizeof(iebuf)) >0 )
        {
            if (EV_ABS == iebuf.type)
            {
                ++readCount;
            }
            DebugPrintf(DLC_GESTURE, "type: %d, code: %d, value: %d", iebuf.type, iebuf.code, iebuf.value);
            
            if (iebuf.type == EV_ABS && iebuf.code == ABS_MT_SLOT)
            {
                s_curSlotId = iebuf.value;
                continue;
            }
            if (iebuf.type == EV_ABS && iebuf.code == ABS_MT_TRACKING_ID && iebuf.value >=0)
            {
                ++curIndex;
                index2Slots[curIndex] = s_curSlotId;
                ++s_curPointerCount;
                if (1 == s_curPointerCount)
                {
                    moveAction = MoveEvent::ACTION_DOWN;
                    downTime = SystemUtil::GetUpdateTimeInMs();
                }
                else if (2 == s_curPointerCount)
                {
                    moveAction = MoveEvent::ACTION_POINTER_DOWN;
                }
                curActionIndex = curIndex;
            }

            if (iebuf.type == EV_ABS && iebuf.code == ABS_MT_TRACKING_ID && iebuf.value ==-1)
            {
                ++curIndex;
                index2Slots[curIndex] = s_curSlotId;
                --s_curPointerCount;
                curActionIndex = curIndex;
                if (1 == s_curPointerCount)
                {
                    moveAction = MoveEvent::ACTION_POINTER_UP;
                    // sometimes we get two ABS_MT_TRACKING_ID in one loop
                    // and must break it in two
                    break;
                }
                else if (0 == s_curPointerCount)
                {
                    moveAction = MoveEvent::ACTION_UP;
                }
            }

            if (iebuf.type == EV_ABS && iebuf.code == ABS_MT_POSITION_X)
            {
                if (-1 == curIndex || s_curSlotId != index2Slots[curIndex])
                {
                    ++curIndex;
                    index2Slots[curIndex] = s_curSlotId;
                }
                if (-1 == curActionIndex)
                {
                    curActionIndex = curIndex;
                }
                s_oldX[s_curSlotId] = GetScreenHorizonPostion(iebuf.value);
                if (MoveEvent::ACTION_MASK == moveAction)
                {
                    moveAction = MoveEvent::ACTION_MOVE;
                }
            }

            if (iebuf.type == EV_ABS && iebuf.code == ABS_MT_POSITION_Y)
            {
                if (-1 == curIndex || s_curSlotId != index2Slots[curIndex])
                {
                    ++curIndex;
                    index2Slots[curIndex] = s_curSlotId;
                }
                if (-1 == curActionIndex)
                {
                    curActionIndex = curIndex;
                }
                s_oldY[s_curSlotId] = GetScreenVerticalPostion(iebuf.value);

                if (MoveEvent::ACTION_MASK == moveAction)
                {
                    moveAction = MoveEvent::ACTION_MOVE;
                }
                if (MoveEvent::ACTION_DOWN == moveAction)
                {
                    // 有可能action_down后还有一个action_pointer_down
                    // 需要拆成两个move event
                    break;
                }
            }

            if (iebuf.type == EV_SYN && iebuf.code == SYN_REPORT && iebuf.value ==0)
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
    // previous read bread before read (0, 0, 0)
    // currently only read (0, 0, 0)
    if (0 == readCount)
    {
        return NULL;
    }
    moveEvent.m_pointerCount = curIndex + 1;
    moveEvent.m_action = moveAction;
    moveEvent.m_downTime = downTime;
    for (int i = 0; i < moveEvent.m_pointerCount; ++i)
    {
        moveEvent.m_x[i] = s_oldX[index2Slots[i]];
        moveEvent.m_y[i] = s_oldY[index2Slots[i]];
        moveEvent.m_pointerId[i] = index2Slots[i];
    }
    // 1. currently there're two fingers on touch screen
    //  but only one finger position information is reported in this loop
    // 2. previously there're two fingers on toush screen 
    //  but only one finger up information is reported in this loop
    // adding another's information
    if (2 == s_curPointerCount && 0 == curIndex
            || (MoveEvent::ACTION_POINTER_UP == moveAction && 0 == curIndex))
    {
        int anotherSlot = 1 - index2Slots[0];
        moveEvent.m_x[1] = s_oldX[anotherSlot];
        moveEvent.m_y[1] = s_oldY[anotherSlot];
        moveEvent.m_pointerId[1] = anotherSlot;
        moveEvent.m_pointerCount = 2;
    }
    moveEvent.m_actionIndex = curActionIndex;
    moveEvent.m_eventTime = SystemUtil::GetUpdateTimeInMs();

    for (int i = 0; i < 2; ++i)
    {
        DebugPrintf(DLC_GESTURE, "Slot %d: (%d, %d)", i, s_oldX[i], s_oldY[i]);
    }
    DebugPrintf(DLC_GESTURE, "Read move event %d: (%d, %d), pointer count: %d, action index: %d",
            moveEvent.GetActionMasked(),
            moveEvent.GetX(),
            moveEvent.GetY(),
            moveEvent.GetPointerCount(),
            moveEvent.GetActionIndex());
    return moveEvent.Clone();
}