summaryrefslogtreecommitdiffstats
path: root/server.cpp
blob: 97afa1ddb3f8a1db41a20fb32b4a8615977c1c55 (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
/****************************************************************************
**
** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Itemviews NG project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "server.h"
#include "protocol.h"
#include "window.h"
#include <qtcpserver.h>
#include <qtcpsocket.h>
#include <qdebug.h>

Server::Server(QObject *parent)
    : QObject(parent), m_server(0)
{
    // setup the display
    m_view.setScene(&m_scene);
    m_view.show();

    // setup root window
    Window *root = new Window(this, 0, Window::ServerWindow);
    root->setGeometry(QRectF(0, 0, 640, 480));
    m_scene.setSceneRect(0, 0, 640, 480);
    m_scene.addItem(root);
    m_windows.insert(root->id(), root);
    m_windows.insert(0, root); // 0 is an alias for the root

    // start listening
    m_server = new QTcpServer(this);
    QObject::connect(m_server, SIGNAL(newConnection()), this, SLOT(handleConnection()));
    m_server->listen(QHostAddress::LocalHost, 2048);
    qDebug() << "SERVER: running";
}

Server::~Server()
{
    // qgraphicsscene will take care of deleting
    // the windows
}

void Server::handleConnection()
{
    qDebug() << "SERVER: client connected";
    if (QTcpSocket *connection = m_server->nextPendingConnection()) {
        connect(connection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(handleConnectionError()));
        connect(connection, SIGNAL(readyRead()), this, SLOT(handleRequest()));
        connect(connection, SIGNAL(disconnected()), this, SLOT(removeConnection()));
    }
}

void Server::handleRequest()
{
    // read request(s)
    QTcpSocket *connection = qobject_cast<QTcpSocket*>(sender());
    QDataStream stream(connection);
    while (!stream.atEnd()) {
        qDebug() << "SERVER: reading request";
        Request request;
        stream >> request;
        // ### FIXME: verify that message type is request
        switch (request.type) {
        case Request::CreateWindowRequest: {
            quint32 id = createWindow(request.id);
            m_connections.insert(id, connection);
            Response response(Response::CreatedWindowResponse, id);
            qDebug() << "SERVER: sending response";
            stream << response;
            break; }
        case Request::DestroyWindowRequest:
            destroyWindow(request.id);
            break;
        case Request::ShowWindowRequest:
            showWindow(request.id);
            break;
        case Request::HideWindowRequest:
            hideWindow(request.id);
            break;
        case Request::RaiseWindowRequest:
            raiseWindow(request.id);
            break;
        case Request::LowerWindowRequest:
            lowerWindow(request.id);
            break;
        case Request::UpdateWindowRequest:
            updateWindow(request.id, request.rect);
            break;
        case Request::SetWindowGeometryRequest:
            setWindowGeometry(request.id, request.rect);
            break;
        default:
            qWarning() << "SERVER: unknown request type" << request.type;
            break;
        };
    } // while (!stream.atEnd())
}

void Server::handleConnectionError()
{
    QTcpSocket *connection = qobject_cast<QTcpSocket*>(sender());
    qWarning() << connection->errorString();
}

void Server::removeConnection()
{
    qDebug() << "SERVER: client disconnected";
    // remove connection and resources associated with that connection
    QTcpSocket *connection = qobject_cast<QTcpSocket*>(sender());
    QList<quint32> ids = m_connections.keys(connection); // ### linear
    foreach(quint32 id, ids) {
        m_connections.remove(id);
        delete m_windows.take(id);
    }
    connection->deleteLater();
}

quint32 Server::createWindow(quint32 parentId)
{
    qDebug() << "SERVER: create window";
    Window *parent = m_windows.value(parentId, 0);
    Window *window = new Window(this, parent);
    m_windows.insert(window->id(), window);
    return window->id();
}

void Server::destroyWindow(quint32 id)
{
    qDebug() << "SERVER: destroy window";
    delete m_windows.take(id);
}

void Server::showWindow(quint32 id)
{
    qDebug() << "SERVER: show window";
    if (Window *window = m_windows.value(id))
        window->show();
}

void Server::hideWindow(quint32 id)
{
    if (Window *window = m_windows.value(id))
        window->hide();
}

void Server::raiseWindow(quint32 id)
{
    qDebug() << "SERVER: hide window";
    if (Window *window = m_windows.value(id))
        window->setZValue(window->zValue() + 1);
}

void Server::lowerWindow(quint32 id)
{
    qDebug() << "SERVER: lower window";
    if (Window *window = m_windows.value(id))
        window->setZValue(window->zValue() - 1);
}

void Server::updateWindow(quint32 id, const QRectF &rect)
{
    qDebug() << "SERVER: update window";
    if (Window *window = m_windows.value(id))
        window->update(rect);
}

void Server::setWindowGeometry(quint32 id, const QRectF &rect)
{
    qDebug() << "SERVER: setting geometry" << rect << id;
    if (Window *window = m_windows.value(id))
        window->setGeometry(rect);
}

void Server::sendGeometryChangeEvent(quint32 id, const QRectF &rect)
{
    qDebug() << "SERVER: sending geometry change event";
    Event event;
    event.type = Event::GeometryChangeEvent;
    event.id = id;
    event.rect = rect;
    if (QTcpSocket *connection = m_connections.value(id)) {
        QDataStream stream(connection);
        stream << event;
    }
}

void Server::sendMousePressEvent(quint32 id, const QPointF &point)
{
    qDebug() << "SERVER: sending mouse press event";
    Event event;
    event.type = Event::MousePressEvent;
    event.id = id;
    event.rect.setTopLeft(point);
    QTcpSocket *connection = m_connections.value(id);
    QDataStream stream(connection);
    stream << event;
}

void Server::sendMouseReleaseEvent(quint32 id, const QPointF &point)
{
    qDebug() << "SERVER: sending mouse release event";
    Event event;
    event.type = Event::MouseReleaseEvent;
    event.id = id;
    event.rect.setTopLeft(point);
    if (QTcpSocket *connection = m_connections.value(id)) {
        QDataStream stream(connection);
        stream << event;
    }
}

void Server::sendMouseMoveEvent(quint32 id, const QPointF &point)
{
    qDebug() << "SERVER: sending mouse move event";
    Event event;
    event.type = Event::MouseMoveEvent;
    event.id = id;
    event.rect.setTopLeft(point);
    if (QTcpSocket *connection = m_connections.value(id)) {
        QDataStream stream(connection);
        stream << event;
    }
}

void Server::sendKeyPressEvent(quint32 id, quint32 key, ushort unicode, uint modifiers)
{
    Q_UNUSED(key);
    qDebug() << "SERVER: sending key press event";
    Event event;
    event.type = Event::KeyPressEvent;
    event.id = id;
    event.value = key;

    // fishing for hack-of-the-year nominations... but at least this way we retain protocol compatibility
    event.rect = QRect(unicode, modifiers, 1, 1);

    QTcpSocket *connection = m_connections.value(id);
    QDataStream stream(connection);
    stream << event;
}

void Server::sendKeyReleaseEvent(quint32 id, quint32 key, ushort unicode, uint modifiers)
{
    Q_UNUSED(key);
    qDebug() << "SERVER: sending key release event";
    Event event;
    event.type = Event::KeyReleaseEvent;
    event.id = id;
    event.value = key;
    event.rect = QRect(unicode, modifiers, 1, 1); // same hack as above
    if (QTcpSocket *connection = m_connections.value(id)) {
        QDataStream stream(connection);
        stream << event;
    }
}