summaryrefslogtreecommitdiffstats
path: root/qgraphicssystem_dd.cpp
blob: 28f8c18c8d36a4b9dbe75de570e100fc5adf72a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights.  These rights are described in the Nokia Qt LGPL
** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qgraphicssystem_dd.h"

#include <QtGui/private/qapplication_p.h>
#include <QtGui/private/qpixmap_raster_p.h>

#include <QtGui/qapplication.h>
#include <QtGui/qevent.h>

#include <QtNetwork/qhostaddress.h>

#include <QtCore/qbytearray.h>
#include <QtCore/qdebug.h>

QT_BEGIN_NAMESPACE

//
// window surface
//

QDevDaysWindowSurface::QDevDaysWindowSurface(QWidget *window, QDevDaysGraphicsSystem *system)
    : QWindowSurface(window), m_system(system)
{
    quint32 parentId = system->m_ids.value(window->parentWidget(), 0);

    Request request(Request::CreateRequest, parentId);
    system->sendRequest(request);

    Response response;
    system->waitForResponse(response);
    m_id = response.id;

    m_system->m_surfaces.insert(m_id, this);
    m_system->m_ids.insert(window, m_id);
}

QDevDaysWindowSurface::~QDevDaysWindowSurface()
{
    Request request(Request::DestroyRequest, m_id);
    m_system->sendRequest(request);
    m_system->m_surfaces.remove(m_id);
    m_system->m_ids.remove(window());
}

QPaintDevice *QDevDaysWindowSurface::paintDevice()
{
    qDebug() << "WINDOW: paint device" << m_image.size();
    return &m_image;
}

void QDevDaysWindowSurface::flush(QWidget *widget, const QRegion &region, const QPoint &offset)
{
    Q_UNUSED(widget);
    Q_UNUSED(region);
    Q_UNUSED(offset);
    qDebug() << "WINDOW: flush";
    // send an update request to the server
    Request request(Request::UpdateRequest, m_id); // ### rect
    m_system->sendRequest(request);
    // we don't expect any response
}

void QDevDaysWindowSurface::setGeometry(const QRect &rect)
{
    qDebug() << "WINDOW: setGeometry";
    QWindowSurface::setGeometry(rect);

    // create or resize the shared memory
    qint32 byteCount = rect.width() * rect.height() * sizeof(quint32); // ### server should hold the pixel format
    qDebug() << "WINDOW: byte count" << byteCount;
    if (byteCount > m_shared.size()) {
        if (m_shared.key().isEmpty())
            m_shared.setKey(QString::number(m_id));
        else
            m_shared.detach();
        if (!m_shared.create(byteCount) && !m_shared.attach())
            qWarning() << m_shared.errorString() << m_shared.key();
        qDebug() << "WINDOW: data" << m_shared.data() << "key" << m_shared.key();
    }

    Request request(Request::SetGeometryRequest, m_id, rect);
    m_system->sendRequest(request);
}

bool QDevDaysWindowSurface::scroll(const QRegion &area, int dx, int dy)
{
    // the server doesn't have scroll
    return QWindowSurface::scroll(area, dx, dy);
}

void QDevDaysWindowSurface::beginPaint(const QRegion &region)
{
    Q_UNUSED(region);
    qDebug() << "WINDOW: begin paint" << m_id << geometry();
    // lock the surface shared memory before painting
    if (m_shared.lock() && m_shared.data())
        m_image = QImage((uchar*)m_shared.data(), geometry().width(), geometry().height(), m_system->screens().first()->format());
    else
        qDebug() << m_shared.errorString();
    qDebug() << "WINDOW: image" << m_image.size() << "geometry" << geometry() << "data" << m_shared.data() << "key" << m_shared.key();
}

void QDevDaysWindowSurface::endPaint(const QRegion &region)
{
    Q_UNUSED(region);
    qDebug() << "WINDOW: end paint";
    // unlock the surface shared memory after painting
    // also make sure that the paint device is invalid
    if (m_shared.unlock())
        m_image = QImage();
    else
        qDebug() << "WINDOW: failed to unlock shared memory";
}

void QDevDaysWindowSurface::handleMouseEvent(const Event &event)
{
    QEvent::Type type = QEvent::None;
    QPoint position = event.rect.topLeft().toPoint();
    switch (event.type) {
    case Event::MousePressEvent:
        type = QEvent::MouseButtonPress;
        break;
    case Event::MouseReleaseEvent:
        type = QEvent::MouseButtonPress;
        break;
    default:
        break;
    }
    QMouseEvent mouseEvent(type, position, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
    QApplicationPrivate::handleMouseEvent(window(), mouseEvent);
}

void QDevDaysWindowSurface::handleGeometryChanged(const Event &event)
{
    QApplicationPrivate::handleGeometryChange(window(), event.rect.toRect());
}

//
// graphics system
//

QDevDaysGraphicsSystem::QDevDaysGraphicsSystem()
    : QObject(0)
{
    qDebug() << "SYSTEM: constructor";
    connectToServer();
}

QPixmapData *QDevDaysGraphicsSystem::createPixmapData(QPixmapData::PixelType type) const
{
    qDebug() << "SYSTEM: create pixmap data";
    // the window system has no server-side pixmap type
    return new QRasterPixmapData(type);
}

QWindowSurface *QDevDaysGraphicsSystem::createWindowSurface(QWidget *widget) const
{
    qDebug() << "SYSTEM: create window surface";
    // send create request to the server
    Q_ASSERT(widget);
    if (widget->windowType() == Qt::Desktop)
        return 0;
    return new QDevDaysWindowSurface(widget, const_cast<QDevDaysGraphicsSystem*>(this));
}

QList<QGraphicsSystemScreen*> QDevDaysGraphicsSystem::screens() const
{
    qDebug() << "SYSTEM: screens";
    return QList<QGraphicsSystemScreen*>() << &m_screen;
}

bool QDevDaysGraphicsSystem::connectToServer()
{
    qDebug() << "SYSTEM: connect to server";
    m_connection.connectToHost(QHostAddress::LocalHost, 2048);
    if (m_connection.waitForConnected()) {
        QObject::connect(&m_connection, SIGNAL(readyRead()), this, SLOT(eventDispatcher()));
        QObject::connect(&m_connection, SIGNAL(disconnected()), QApplication::instance(), SLOT(quit()));
        qDebug() << "SYSTEM: connected to server";
        return true;
    }
    qDebug() << "SYSTEM: unable to connect to server";
    return false;
}

bool QDevDaysGraphicsSystem::sendRequest(const Request &request)
{
    qDebug() << "SYSTEM: sending request";
    QDataStream stream(&m_connection);
    stream << (*static_cast<const Message*>(&request));
    return m_connection.isValid();
}

bool QDevDaysGraphicsSystem::waitForResponse(Response &response)
{
    qDebug() << "SYSTEM: waiting for response";
    if (m_connection.waitForReadyRead(-1)) {
        if (m_message.message == Message::ResponseMessage) {
            qDebug() << "SYSTEM: received response";
            response.type = m_message.type;
            response.id = m_message.id;
            response.rect = m_message.rect;
            // clear internal message
            m_message.message = Message::InvalidMessage;
            return true;
        }
    }
    //handleConnectionError();
    return false;
}

void QDevDaysGraphicsSystem::eventDispatcher()
{
    qDebug() << "SYSTEM: event dispatcher";
    while (m_connection.bytesAvailable()) {
        QDataStream in(&m_connection);
        in >> m_message;
        switch (m_message.message) {
        case Event::EventMessage:
            qDebug() << "SYSTEM: received event" << m_message.type;
            QDevDaysWindowSurface *surface = m_surfaces.value(m_message.id);
            switch (m_message.type) {
            case Event::MousePressEvent: {
                 // ### we don't support multiple buttons or keyboard modifiers
                QMouseEvent me(QEvent::MouseButtonPress, m_message.rect.topLeft().toPoint(), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
                QApplicationPrivate::handleMouseEvent(surface->window(), me);
                break; }
            case Event::MouseReleaseEvent: {
                 // ### we don't support multiple buttons or keyboard modifiers
                QMouseEvent me(QEvent::MouseButtonRelease, m_message.rect.topLeft().toPoint(), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
                QApplicationPrivate::handleMouseEvent(surface->window(), me);
                break; }
            }
            break;
        case Response::ResponseMessage:
        case Request::RequestMessage:
        default:
            qDebug() << "SYSTEM: received wrong message type" << m_message.type << m_message.id;
            break;
        }
        //if (!m_connection.isValid())
        //    handleConnectionError();
    }
}

QT_END_NAMESPACE