/*! \internal

  This function is called whenever there is activity on the mouse device.
  By default, it reads up to 10 mouse move packets and calls mouseChanged()
  for each of them.
*/
void QQnxMouseHandler::socketActivated()
{
    // _mouse_packet is a QNX structure. devi-hid is nice enough to translate
    // the raw byte data from mouse devices into generic format for us.
    _mouse_packet packet;

    int iteration = 0;

    // read mouse events in batches of 10. Since we're getting quite a lot
    // of mouse events, it's better to do them in batches than to return to the
    // event loop every time.
    do {
        int bytesRead = QT_READ(mouseFD, &packet, sizeof(packet));
        if (bytesRead == -1) {
            // EAGAIN means that there are no more mouse events to read
            if (errno != EAGAIN)
                qErrnoWarning(errno, "QQnxMouseHandler: Unable to read from socket");
            return;
        }

        // bytes read should always be equal to the size of a packet.
        Q_ASSERT(bytesRead == sizeof(packet));

        // translate the coordinates from the QNX data structure to Qt coordinates
        // note the swapped y axis
        QPoint pos = mousePos;
        pos += QPoint(packet.dx, -packet.dy);

        // QNX only tells us relative mouse movements, not absolute ones, so limit the
        // cursor position manually to the screen
        limitToScreen(pos);

        // translate the QNX mouse button bitmask to Qt buttons
        int buttons = Qt::NoButton;

        if (packet.hdr.buttons & _POINTER_BUTTON_LEFT)
            buttons |= Qt::LeftButton;
        if (packet.hdr.buttons & _POINTER_BUTTON_MIDDLE)
            buttons |= Qt::MidButton;
        if (packet.hdr.buttons & _POINTER_BUTTON_RIGHT)
            buttons |= Qt::RightButton;

        // call mouseChanged() - this does all the magic to actually move the on-screen
        // mouse cursor.
        mouseChanged(pos, buttons, 0);
    } while (++iteration < 11);
}
void KindleFiveWay::activity(int)
{
    _sn->setEnabled(false);

    input_event_t in;

    read(_fd, &in, sizeof(input_event_t));

    if (_debug)
        qDebug("FiveWay: type %d, code %d, value %d", in.type, in.code, in.value);

    if (in.type == 1)
    {
        QPoint p = pos();

        switch(in.code)
        {
        default:
            break;
        case KDX_KEY_5WPRESS:
        case K3_KEY_5WPRESS:
            // button press
            if (_keypadMode)
            {
                QWSServer::sendKeyEvent('\n', Qt::Key_Return, Qt::NoModifier, in.value != 0, in.value == 2);
            }
            else
            {
                if (in.value)
                    _button = !_button;
            }
            break;
        case KDX_KEY_LARROW:
            // left
            if (_keypadMode)
            {
                QWSServer::sendKeyEvent(0, Qt::Key_Left, Qt::NoModifier, in.value != 0, in.value == 2);
            }
            else if (in.value == 0)
            {
                _left = 0;
            }
            else if (in.value == 1)
            {
                _left = 1;
                p.setX(p.x() - 5);
            }
            else
            {
                if (_left < 6)
                    _left++;

                p.setX(p.x() - 5 * _left);
            }
            break;
        case KDX_KEY_RARROW:
            // right
            if (_keypadMode)
            {
                QWSServer::sendKeyEvent(0, Qt::Key_Right, Qt::NoModifier, in.value != 0, in.value == 2);
            }
            else if (in.value == 0)
            {
                _right = 0;
            }
            else if (in.value == 1)
            {
                _right = 1;
                p.setX(p.x() + 5);
            }
            else
            {
                if (_right < 6)
                    _right++;

                p.setX(p.x() + 5 * _right);
            }
            break;
        case KDX_KEY_UPARROW:
        case K3_KEY_UPARROW:
            // up
            if (_keypadMode)
            {
                QWSServer::sendKeyEvent(0, Qt::Key_Up, Qt::NoModifier, in.value != 0, in.value == 2);
            }
            else if (in.value == 0)
            {
                _up = 0;
            }
            else if (in.value == 1)
            {
                _up = 1;
                p.setY(p.y() - 5);
            }
            else
            {
                if (_up < 6)
                    _up++;

                p.setY(p.y() - 5 * _up);
            }
            break;
        case KDX_KEY_DNARROW:
        case K3_KEY_DNARROW:
            // down
            if (_keypadMode)
            {
                QWSServer::sendKeyEvent(0, Qt::Key_Down, Qt::NoModifier, in.value != 0, in.value == 2);
            }
            else if (in.value == 0)
            {
                _down = 0;
            }
            else if (in.value == 1)
            {
                _down = 1;
                p.setY(p.y() + 5);
            }
            else
            {
                if (_down < 6)
                    _down++;

                p.setY(p.y() + 5 * _down);
            }
            break;
        }

        limitToScreen(p);

        if (!_keypadMode)
            mouseChanged(p, _button ? Qt::LeftButton : 0, 0);
    }

    _sn->setEnabled(true);
}