⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qmousepc_qws.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        init();    }    void init()    {        setflags(B1200|CS7);        // 60Hz        if (write(fd, "R", 1)!=1) {            badness = 100;            return;        }        tcflush(fd,TCIOFLUSH);    }    int tryData()    {        if (!(buffer[0] & 0x40)) {            if (buffer[0] == 0x20 && (bstate & Qt::MidButton)) {                mman=1; // mouseman extension            }            return 1;        }        int extra = mman&&(bstate & Qt::MidButton);        if (nbuf >= 3+extra) {            int nbstate = 0;            if (buffer[0] == 0x40 && !bstate && !buffer[1] && !buffer[2]) {                nbstate = Qt::MidButton;            } else {                nbstate = ((buffer[0] & 0x20) >> 5)                        | ((buffer[0] & 0x10) >> 3);                if (extra && buffer[3] == 0x20)                    nbstate = Qt::MidButton;            }            if (buffer[1] & 0x40) {                badness++;                return 1;            } else {                motion +=                    QPoint((signed char)((buffer[0]&0x3)<<6)                            |(signed char)(buffer[1]&0x3f),                           (signed char)((buffer[0]&0xc)<<4)                            |(signed char)(buffer[2]&0x3f));                if (motion.x() || motion.y() || bstate != nbstate) {                    bstate = nbstate;                    goodness++;                } else {                    badness++;                    return 1;                }                return 3+extra;            }        }        return 0;    }};//===========================================================================class QWSPcMouseHandlerPrivate : public QObject{    Q_OBJECTpublic:    QWSPcMouseHandlerPrivate(QWSPcMouseHandler *h, const QString &, const QString &);    ~QWSPcMouseHandlerPrivate();    void suspend();    void resume();private:    enum { max_dev=32 };    QWSPcMouseSubHandler *sub[max_dev];    QList<QSocketNotifier*> notifiers;    int nsub;    int retries;private slots:    void readMouseData(int);private:    void openDevices();    void closeDevices();    void notify(int fd);    bool sendEvent(QWSPcMouseSubHandler& h);private:    QWSPcMouseHandler *handler;    QString driver;    QString device;    qreal accel;    int accel_limit;};QWSPcMouseHandler::QWSPcMouseHandler(const QString &driver, const QString &device)    : QWSMouseHandler(driver, device){    d = new QWSPcMouseHandlerPrivate(this, driver, device);}QWSPcMouseHandler::~QWSPcMouseHandler(){    delete d;}void QWSPcMouseHandler::suspend(){    d->suspend();}void QWSPcMouseHandler::resume(){    d->resume();}QWSPcMouseHandlerPrivate::QWSPcMouseHandlerPrivate(QWSPcMouseHandler *h,    const QString &drv, const QString &arg)    : handler(h), driver(drv){    QStringList args = arg.split(QLatin1Char(':'), QString::SkipEmptyParts);    int index;    accel = qreal(2.0);    QRegExp accelRegex(QLatin1String("^accel=(\\d+\\.?\\d*)$"));    index = args.indexOf(accelRegex);    if (index >= 0) {        accel = qreal(accelRegex.cap(1).toDouble());        args.removeAt(index);    }    accel_limit = 5;    QRegExp accelLimitRegex(QLatin1String("^accel_limit=(\\d+)$"));    index = args.indexOf(accelLimitRegex);    if (index >= 0) {        accel_limit = accelLimitRegex.cap(1).toInt();        args.removeAt(index);    }    device = args.join(QString());    retries = 0;    openDevices();}QWSPcMouseHandlerPrivate::~QWSPcMouseHandlerPrivate(){    closeDevices();}/*QWSPcMouseHandler::UsageResult QWSPcMouseHandler::useDev(Dev& d){    if (d.nbuf >= mouseData[d.protocol].bytesPerPacket) {        uchar *mb = d.buf;        int bstate = 0;        int dx = 0;        int dy = 0;        switch (mouseProtocol) {            case MouseMan:            case IntelliMouse:            {                bstate = mb[0] & 0x7; // assuming Qt::*Button order                int overflow = (mb[0]>>6)& 0x03;                if (mouseProtocol == MouseMan && overflow) {                    //### wheel events signalled with overflow bit, ignore for now                }                else {                    bool xs = mb[0] & 0x10;                    bool ys = mb[0] & 0x20;                    dx = xs ? mb[1]-256 : mb[1];                    dy = ys ? mb[2]-256 : mb[2];                }                break;            }            case Microsoft:                if (((mb[0] & 0x20) >> 3)) {                    bstate |= Qt::LeftButton;                }                if (((mb[0] & 0x10) >> 4)) {                    bstate |= Qt::RightButton;                }                dx=(signed char)(((mb[0] & 0x03) << 6) | (mb[1] & 0x3f));                dy=-(signed char)(((mb[0] & 0x0c) << 4) | (mb[2] & 0x3f));                break;        }    }    }*/bool QWSPcMouseHandlerPrivate::sendEvent(QWSPcMouseSubHandler& h){    if (h.reliable()) {        QPoint motion = h.takeMotion();        if (qAbs(motion.x()) > accel_limit || qAbs(motion.y()) > accel_limit)            motion *= accel;        QPoint newPos = handler->pos() + motion;        if (qt_screen->isTransformed()) {            QSize s = QSize(qt_screen->width(), qt_screen->height());            newPos = qt_screen->mapToDevice(newPos, s);        }        handler->limitToScreen(newPos);        handler->mouseChanged(newPos, h.buttonState(), h.takeWheel());        return true;    } else {        h.takeMotion();        if (h.buttonState() & (Qt::RightButton|Qt::MidButton)) {            // Strange for the user to press right or middle without            // a moving mouse!            h.worse();        }        return false;    }}void QWSPcMouseHandlerPrivate::openDevices(){    nsub=0;    int fd = -1;    if (!driver.isEmpty() && driver != QLatin1String("Auto")) {        // Manually specified mouse        QByteArray dev = device.toLatin1();        if (driver == QLatin1String("IntelliMouse")) {            if (dev.isEmpty())                dev = "/dev/psaux";            fd = open(dev, O_RDWR | O_NDELAY);            if (fd >= 0)                sub[nsub++] = new QWSPcMouseSubHandler_intellimouse(fd);        } else if (driver == QLatin1String("Microsoft")) {            if (dev.isEmpty())                dev = "/dev/ttyS0";            fd = open(dev, O_RDWR | O_NDELAY);            if (fd >= 0)                sub[nsub++] = new QWSPcMouseSubHandler_ms(fd);        } else if (driver == QLatin1String("MouseSystems")) {            if (dev.isEmpty())                dev = "/dev/ttyS0";            fd = open(dev, O_RDWR | O_NDELAY);            if (fd >= 0)                sub[nsub++] = new QWSPcMouseSubHandler_mousesystems(fd);        } else if (driver == QLatin1String("MouseMan")) {            if (dev.isEmpty())                dev = "/dev/psaux";            fd = open(dev, O_RDWR | O_NDELAY);            if (fd >= 0)                sub[nsub++] = new QWSPcMouseSubHandler_mouseman(fd);        }        if (fd >= 0)            notify(fd);	    else                qCritical("Error opening mouse device '%s': %s",                          dev.constData(), strerror(errno));    } else {        // Try automatically        fd = open("/dev/psaux", O_RDWR | O_NDELAY);        if (fd >= 0) {            sub[nsub++] = new QWSPcMouseSubHandler_intellimouse(fd);            notify(fd);        }        fd = open("/dev/input/mice", O_RDWR | O_NDELAY);        if (fd >= 0) {            sub[nsub++] = new QWSPcMouseSubHandler_intellimouse(fd);            notify(fd);            //qDebug("/dev/input/mice fd %d #%d", fd, nsub-1);        }// include the code below to auto-detect serial mice, and to mess up// any sort of serial communication#if 0        const char fn[4][11] = { "/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3" };        for (int ch = 0; ch < 4; ++ch) {            fd = open(fn[ch], O_RDWR | O_NDELAY);            if (fd >= 0) {                //sub[nsub++] = new QWSPcMouseSubHandler_intellimouse(fd);                sub[nsub++] = new QWSPcMouseSubHandler_mousesystems(fd);                sub[nsub++] = new QWSPcMouseSubHandler_ms(fd);                notify(fd);            }        }#endif    }}void QWSPcMouseHandlerPrivate::closeDevices(){    int pfd=-1;    for (int i=0; i<nsub; i++) {        sub[i]->closeIfNot(pfd);        delete sub[i];    }    qDeleteAll(notifiers);    notifiers.clear();}void QWSPcMouseHandlerPrivate::suspend(){    for (int i=0; i<notifiers.size(); ++i)        notifiers.at(i)->setEnabled(false);}void QWSPcMouseHandlerPrivate::resume(){    for (int i=0; i<nsub; i++)        sub[i]->initState();    for (int i=0; i<notifiers.size(); ++i)        notifiers.at(i)->setEnabled(true);}void QWSPcMouseHandlerPrivate::notify(int fd){    QSocketNotifier *mouseNotifier        = new QSocketNotifier(fd, QSocketNotifier::Read, this);    connect(mouseNotifier, SIGNAL(activated(int)),this, SLOT(readMouseData(int)));    notifiers.append(mouseNotifier);}void QWSPcMouseHandlerPrivate::readMouseData(int fd){    for (;;) {        uchar buf[8];        int n = read(fd, buf, 8);        if (n<=0)            break;        for (int i=0; i<nsub; i++) {            QWSPcMouseSubHandler& h = *sub[i];            if (h.file() == fd) {                h.appendData(buf,n);                for (;;) {                    switch (h.useData()) {                      case QWSPcMouseSubHandler::Button:                        sendEvent(h);                        break;                      case QWSPcMouseSubHandler::Insufficient:                        goto breakbreak;                      case QWSPcMouseSubHandler::Motion:                        break;                    }                }                breakbreak:                    ;            }        }    }    bool any_reliable=false;    for (int i=0; i<nsub; i++) {        QWSPcMouseSubHandler& h = *sub[i];        if (h.motionPending())            sendEvent(h);        any_reliable = any_reliable || h.reliable();    }    if (any_reliable) {        // ... get rid of all unreliable ones?  All bad ones?    } else if (retries < 2) {        // Try again - maybe the mouse was being moved when we tried to init.        closeDevices();        openDevices();        retries++;    }}#include "qmousepc_qws.moc"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -