qapplication_qws.cpp

来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 2,229 行 · 第 1/5 页

CPP
2,229
字号
void QWSDisplay::Data::translateExpose(QWSRegionModifiedEvent *re, const QPoint &p){    for (int i = 0; i < re->simpleData.nrectangles; i++)        re->rectangles[i].translate(p.x(), p.y());}#endifvoid QWSDisplay::Data::create(int n){    QWSCreateCommand cmd(n);    sendCommand(cmd);}void QWSDisplay::Data::flushCommands(){#ifndef QT_NO_QWS_MULTIPROCESS    if  (csocket)        csocket->flush();#endif}void QWSDisplay::Data::sendCommand(QWSCommand & cmd){#ifndef QT_NO_QWS_MULTIPROCESS    if  (csocket)        cmd.write(csocket);    else#endif        qt_server_enqueue(&cmd);}void QWSDisplay::Data::sendSynchronousCommand(QWSCommand & cmd){#ifndef QT_NO_QWS_MULTIPROCESS    if  (csocket) {        lockClient(QWSLock::Communication);        cmd.write(csocket);        csocket->waitForBytesWritten();        waitClient(QWSLock::Communication);    } else#endif        qt_server_enqueue(&cmd);}int QWSDisplay::Data::takeId(){    if (unused_identifiers.count() == 10)        create(15);    if (unused_identifiers.count() == 0)        waitForCreation();    return unused_identifiers.takeFirst();}void QWSDisplay::Data::setMouseFilter(void (*filter)(QWSMouseEvent*)){    mouseFilter = filter;}QWSLock* QWSDisplay::Data::clientLock = 0;#ifndef QT_NO_QWS_MULTIPROCESSvoid Q_GUI_EXPORT qt_app_reinit( const QString& newAppName ){    qt_fbdpy->d->reinit( newAppName );}#endifclass QDesktopWidget;extern QDesktopWidget *qt_desktopWidget;#ifndef QT_NO_QWS_MULTIPROCESSvoid QWSDisplay::Data::reinit( const QString& newAppName ){    Q_ASSERT(csocket);    delete connected_event;    connected_event = 0;//    region_ack = 0;    delete mouse_event;    mouse_event = 0;//    region_event = 0;    region_offset_window = 0;#ifndef QT_NO_COP    delete qcop_response;    qcop_response = 0;#endif    delete current_event;    current_event = 0;#ifdef QAPPLICATION_EXTRA_DEBUG    mouse_event_count = 0;#endif    mouseFilter = 0;    qt_desktopWidget = 0;    delete QWSDisplay::Data::clientLock;    QWSDisplay::Data::clientLock = 0;    QString pipe = qws_qtePipeFilename();    // QWS client    // Cleanup all cached ids    unused_identifiers.clear();    delete csocket;    appName = newAppName;    qApp->setObjectName( appName );    csocket = new QWSSocket();    QObject::connect(csocket, SIGNAL(disconnected()),                     qApp, SLOT(quit()));    csocket->connectToLocalFile(pipe);    QWSDisplay::Data::clientLock = new QWSLock();    QWSIdentifyCommand cmd;    cmd.setId(appName, QWSDisplay::Data::clientLock->id());#ifndef QT_NO_SXE    QTransportAuth *a = QTransportAuth::getInstance();    QTransportAuth::Data *d = a->connectTransport(            QTransportAuth::UnixStreamSock |            QTransportAuth::Trusted,            csocket->socketDescriptor());    QAuthDevice *ad = a->authBuf( d, csocket );    ad->setClient( reinterpret_cast<void*>(csocket) );    cmd.write(ad);#else    cmd.write(csocket);#endif    // wait for connect confirmation    waitForConnection();    qws_client_id = connected_event->simpleData.clientId;    if (!QWSDisplay::initLock(pipe, false))        qFatal("Cannot get display lock");    if (shm.attach(connected_event->simpleData.servershmid)) {        QScreen *s = qt_get_screen(qws_display_id, qws_display_spec.constData());        sharedRamSize += s->memoryNeeded(qws_display_spec.constData());    } else {        perror("QWSDisplay::Data::init");        qFatal("Client can't attach to main ram memory.");    }    qApp->desktop();    // We wait for creation mainly so that we can process important    // initialization events such as MaxWindowRect that are sent    // before object id creation.  Waiting here avoids later window    // resizing since we have the MWR before windows are displayed.    waitForCreation();    sharedRam = static_cast<uchar *>(shm.address());    sharedRamSize -= sizeof(int);    qt_last_x = reinterpret_cast<int *>(sharedRam + sharedRamSize);    sharedRamSize -= sizeof(int);    qt_last_y = reinterpret_cast<int *>(sharedRam + sharedRamSize);#ifndef QT_NO_COP    QCopChannel::reregisterAll();#endif    csocket->flush();}#endifvoid QWSDisplay::Data::init(){    connected_event = 0;//    region_ack = 0;    mouse_event = 0;    mouse_state = -1;    mouse_winid = 0;//    region_event = 0;    region_offset_window = 0;#ifndef QT_NO_COP    qcop_response = 0;#endif    current_event = 0;#ifdef QAPPLICATION_EXTRA_DEBUG    mouse_event_count = 0;#endif    mouseFilter = 0;    QString pipe = qws_qtePipeFilename();    sharedRamSize = qwsSharedRamSize;#ifndef QT_NO_QWS_MULTIPROCESS    if (csocket)    {        // QWS client        connectToPipe();        QWSDisplay::Data::clientLock = new QWSLock();        QWSIdentifyCommand cmd;        cmd.setId(appName, QWSDisplay::Data::clientLock->id());#ifndef QT_NO_SXE        QTransportAuth *a = QTransportAuth::getInstance();        QTransportAuth::Data *d = a->connectTransport(                QTransportAuth::UnixStreamSock |                QTransportAuth::Trusted,                csocket->socketDescriptor());        QAuthDevice *ad = a->authBuf( d, csocket );        ad->setClient( reinterpret_cast<void*>(csocket) );        cmd.write(ad);#else        cmd.write(csocket);#endif        // create(30); // not necessary, server will send ids anyway        waitForConnection();        qws_client_id = connected_event->simpleData.clientId;        // now we want to get the exact display spec to use if we haven't        // specified anything.        if (qws_display_spec.at(0) == ':')            qws_display_spec = connected_event->display;        if (!QWSDisplay::initLock(pipe, false))            qFatal("Cannot get display lock");        if (shm.attach(connected_event->simpleData.servershmid)) {            QScreen *s = qt_get_screen(qws_display_id, qws_display_spec.constData());            sharedRamSize += s->memoryNeeded(qws_display_spec.constData());        } else {            perror("QWSDisplay::Data::init");            qFatal("Client can't attach to main ram memory.");        }        sharedRam = static_cast<uchar *>(shm.address());        // We wait for creation mainly so that we can process important        // initialization events such as MaxWindowRect that are sent        // before object id creation.  Waiting here avoids later window        // resizing since we have the MWR before windows are displayed.        waitForCreation();    } else#endif    {        create(30);        // QWS server        if (!QWSDisplay::initLock(pipe, true))            qFatal("Cannot get display lock");        QScreen *s = qt_get_screen(qws_display_id, qws_display_spec.constData());        sharedRamSize += s->memoryNeeded(qws_display_spec.constData());#ifndef QT_NO_QWS_MULTIPROCESS        if (!shm.create(sharedRamSize)) {            perror("Cannot create main ram shared memory\n");            qFatal("Unable to allocate %d bytes of shared memory", sharedRamSize);        }        qt_servershmid = shm.id();        sharedRam = static_cast<uchar *>(shm.address());#else        sharedRam=static_cast<uchar *>(malloc(sharedRamSize));#endif        // Need to zero index count at end of block, might as well zero        // the rest too        memset(sharedRam,0,sharedRamSize);        QWSIdentifyCommand cmd;        qt_server_enqueue(&cmd);    }    // Allow some memory for the graphics driver too    //### Note that sharedRamSize() has side effects; it must be called    //### once, and only once, and before initDevice()    sharedRamSize -= qt_screen->sharedRamSize(sharedRam+sharedRamSize);#ifndef QT_NO_QWS_MULTIPROCESS    if(!csocket)#endif    {        //QWS server process        qt_screen->initDevice();    }    sharedRamSize -= sizeof(int);    qt_last_x = reinterpret_cast<int *>(sharedRam + sharedRamSize);    sharedRamSize -= sizeof(int);    qt_last_y = reinterpret_cast<int *>(sharedRam + sharedRamSize);    /* Initialise framebuffer memory manager */    /* Add 4k for luck and to avoid clobbering hardware cursor *///    int screensize=qt_screen->screenSize();//     memorymanager=new QMemoryManager(qt_screen->base()+screensize+4096,//         qt_screen->totalSize()-(screensize+4096),0);// #ifndef QT_NO_QWS_MULTIPROCESS//     rgnMan = new QWSRegionManager(pipe, csocket);// #else//     rgnMan = new QWSRegionManager(pipe, 0); //####### not necessary// #endif#ifndef QT_NO_QWS_MULTIPROCESS    if (csocket)        csocket->flush();#endif}QWSEvent* QWSDisplay::Data::readMore(){#ifdef QT_NO_QWS_MULTIPROCESS    return incoming.isEmpty() ? 0 : incoming.takeFirst();#else    if (!csocket)        return incoming.isEmpty() ? 0 : incoming.takeFirst();    // read next event    if (!current_event) {        int event_type = qws_read_uint(csocket);        if (event_type >= 0) {            current_event = QWSEvent::factory(event_type);        }    }    if (current_event) {        if (current_event->read(csocket)) {            // Finished reading a whole event.            QWSEvent* result = current_event;            current_event = 0;            return result;        }    }    // Not finished reading a whole event.    return 0;#endif}void QWSDisplay::Data::fillQueue(){    QWSServer::processEventQueue();    QWSEvent *e = readMore();    while (e) {        if (e->type == QWSEvent::Connected) {            connected_event = static_cast<QWSConnectedEvent *>(e);            return;        } else if (e->type == QWSEvent::Creation) {            QWSCreationEvent *ce = static_cast<QWSCreationEvent*>(e);            int id = ce->simpleData.objectid;            int count = ce->simpleData.count;            for (int i = 0; i < count; ++i)                unused_identifiers.append(id++);            delete e;        } else if (e->type == QWSEvent::Mouse) {            if (!qt_screen) {                delete e;            } else {                QWSMouseEvent *me = static_cast<QWSMouseEvent*>(e);                if (mouseFilter)                    mouseFilter(me);#ifdef QAPPLICATION_EXTRA_DEBUG                static const char *defaultAction= "INITIAL";                const char * action = defaultAction;#endif                delete mouse_event;                if (mouse_winid != me->window ()                    || mouse_state != me->simpleData.state) {                        queue.append(me);                        mouse_winid = me->window();                        mouse_state = me->simpleData.state;                        mouse_event = 0;#ifdef QAPPLICATION_EXTRA_DEBUG                        mouse_event_count = 0;                        action = "ENQUEUE";#endif                } else {#ifdef QAPPLICATION_EXTRA_DEBUG                    if (mouse_event)                        action = "COMPRESS";                    mouse_event_count++;#endif                    mouse_event = me;                }#ifdef QAPPLICATION_EXTRA_DEBUG                if (me->simpleData.state !=0 || action != defaultAction || mouse_event_count != 0)                    qDebug("fillQueue %s (%d,%d), state %x win %d count %d", action,                           me->simpleData.x_root, me->simpleData.y_root, me->simpleData.state,                           me->window(), mouse_event_count);#endif            }#if 0        } else if (e->type == QWSEvent::RegionModified) {            QWSRegionModifiedEvent *re = static_cast<QWSRegionModifiedEvent *>(e);            if (re->simpleData.is_ack) {                region_ack = re;                region_offset = QPoint();                region_offset_window = 0;            } else {                if (region_offset_window == re->window() && !region_offset.isNull()) {//                    qDebug("Rgn Adjust a %d, %d", region_offset.x(), region_offset.y());                    translateExpose(re, region_offset);                }                if (!region_event || re->window() == region_event->window()) {                    if (region_event) {                        QRegion r1;                        r1.setRects(re->rectangles, re->simpleData.nrectangles);                        QRegion r2;                        r2.setRects(region_event->rectangles,                                region_event->simpleData.nrectangles);                        QRegion ur(r1 + r2);                        region_event->setData(reinterpret_cast<const char *>(ur.rects().constData()),                                ur.rects().count() * sizeof(QRect), true);                        region_event->simpleData.nrectangles = ur.rects().count();                        delete e;                    } else {                        region_event = re;                    }                } else {                    queue.append(e);                }            }#endif // 0 (RegionModified)        } else if (e->type==QWSEvent::MaxWindowRect && qt_screen) {            // Process this ASAP, in case new widgets are created (startup)            setMaxWindowRect((static_cast<QWSMaxWindowRectEvent*>(e))->simpleData.rect);            delete e;#ifndef QT_NO_COP        } else if (e->type == QWSEvent::QCopMessage) {            QWSQCopMessageEvent *pe = static_cast<QWSQCopMessageEvent*>(e);            if (pe->simpleData.is_response) {                qcop_response = pe;            } else {                queue.append(e);            }#endif        } else {            queue.append(e);        }        //debugQueue();        e = readMore();    }}#if 0void QWSDisplay::Data::offsetPendingExpose(int window, const QPoint &offset){

⌨️ 快捷键说明

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