• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

krfb

  • sources
  • kde-4.14
  • kdenetwork
  • krfb
  • krfb
rfbserver.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009-2010 Collabora Ltd <info@collabora.co.uk>
3  @author George Goldberg <george.goldberg@collabora.co.uk>
4  @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>
5  Copyright (C) 2007 Alessandro Praduroux <pradu@pradu.it>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #include "rfbserver.h"
21 #include "rfbservermanager.h"
22 #include <QtCore/QSocketNotifier>
23 #include <QApplication>
24 #include <QClipboard>
25 #include <KDebug>
26 
27 struct RfbServer::Private
28 {
29  QByteArray listeningAddress;
30  int listeningPort;
31  bool passwordRequired;
32  rfbScreenInfoPtr screen;
33  QSocketNotifier *notifier;
34 };
35 
36 RfbServer::RfbServer(QObject *parent)
37  : QObject(parent), d(new Private)
38 {
39  d->listeningAddress = "0.0.0.0";
40  d->listeningPort = 0;
41  d->passwordRequired = true;
42  d->screen = NULL;
43  d->notifier = NULL;
44 
45  RfbServerManager::instance()->registerServer(this);
46 }
47 
48 RfbServer::~RfbServer()
49 {
50  if (d->screen) {
51  rfbScreenCleanup(d->screen);
52  }
53  delete d;
54 
55  RfbServerManager::instance()->unregisterServer(this);
56 }
57 
58 QByteArray RfbServer::listeningAddress() const
59 {
60  return d->listeningAddress;
61 }
62 
63 int RfbServer::listeningPort() const
64 {
65  return d->listeningPort;
66 }
67 
68 bool RfbServer::passwordRequired() const
69 {
70  return d->passwordRequired;
71 }
72 
73 void RfbServer::setListeningAddress(const QByteArray& address)
74 {
75  d->listeningAddress = address;
76 }
77 
78 void RfbServer::setListeningPort(int port)
79 {
80  d->listeningPort = port;
81 }
82 
83 void RfbServer::setPasswordRequired(bool passwordRequired)
84 {
85  d->passwordRequired = passwordRequired;
86 }
87 
88 bool RfbServer::start()
89 {
90  if (!d->screen) {
91  d->screen = RfbServerManager::instance()->newScreen();
92  if (!d->screen) {
93  return false;
94  }
95 
96  // server hooks
97  d->screen->screenData = this;
98  d->screen->newClientHook = newClientHook;
99  d->screen->kbdAddEvent = keyboardHook;
100  d->screen->ptrAddEvent = pointerHook;
101  d->screen->passwordCheck = passwordCheck;
102  d->screen->setXCutText = clipboardHook;
103  } else {
104  //if we already have a screen, stop listening first
105  rfbShutdownServer(d->screen, false);
106  }
107 
108  if (listeningAddress() != "0.0.0.0") {
109  strncpy(d->screen->thisHost, listeningAddress().data(), 254);
110  }
111 
112  if (listeningPort() == 0) {
113  d->screen->autoPort = 1;
114  }
115 
116  d->screen->port = listeningPort();
117 
118  // Disable/Enable password checking
119  if (passwordRequired()) {
120  d->screen->authPasswdData = (void *)1;
121  } else {
122  d->screen->authPasswdData = (void *)0;
123  }
124 
125  kDebug() << "Starting server. Listen port:" << listeningPort()
126  << "Listen Address:" << listeningAddress()
127  << "Password enabled:" << passwordRequired();
128 
129  rfbInitServer(d->screen);
130 
131  if (!rfbIsActive(d->screen)) {
132  kDebug() << "Failed to start server";
133  rfbShutdownServer(d->screen, false);
134  return false;
135  };
136 
137  d->notifier = new QSocketNotifier(d->screen->listenSock, QSocketNotifier::Read, this);
138  d->notifier->setEnabled(true);
139  connect(d->notifier, SIGNAL(activated(int)), this, SLOT(onListenSocketActivated()));
140  connect(QApplication::clipboard(), SIGNAL(dataChanged()),
141  this, SLOT(krfbSendServerCutText()));
142 
143  return true;
144 }
145 
146 void RfbServer::stop(bool disconnectClients)
147 {
148  if (d->screen) {
149  rfbShutdownServer(d->screen, disconnectClients);
150  if (d->notifier) {
151  d->notifier->setEnabled(false);
152  d->notifier->deleteLater();
153  d->notifier = NULL;
154  }
155  }
156 }
157 
158 void RfbServer::updateScreen(const QList<QRect> & modifiedTiles)
159 {
160  if (d->screen) {
161  QList<QRect>::const_iterator it = modifiedTiles.constBegin();
162  for(; it != modifiedTiles.constEnd(); ++it) {
163  rfbMarkRectAsModified(d->screen, it->x(), it->y(), it->right(), it->bottom());
164  }
165  }
166 }
167 
168 /*
169  * Code copied from vino's bundled libvncserver:
170  * Copyright (C) 2000, 2001 Const Kaplinsky. All Rights Reserved.
171  * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
172  * License: GPL v2 or later
173  */
174 void krfb_rfbSetCursorPosition(rfbScreenInfoPtr screen, rfbClientPtr client, int x, int y)
175 {
176  rfbClientIteratorPtr iterator;
177  rfbClientPtr cl;
178 
179  if (x == screen->cursorX || y == screen->cursorY)
180  return;
181 
182  LOCK(screen->cursorMutex);
183  screen->cursorX = x;
184  screen->cursorY = y;
185  UNLOCK(screen->cursorMutex);
186 
187  /* Inform all clients about this cursor movement. */
188  iterator = rfbGetClientIterator(screen);
189  while ((cl = rfbClientIteratorNext(iterator)) != NULL) {
190  cl->cursorWasMoved = TRUE;
191  }
192  rfbReleaseClientIterator(iterator);
193 
194  /* The cursor was moved by this client, so don't send CursorPos. */
195  if (client) {
196  client->cursorWasMoved = FALSE;
197  }
198 }
199 
200 void RfbServer::updateCursorPosition(const QPoint & position)
201 {
202  if (d->screen) {
203  krfb_rfbSetCursorPosition(d->screen, NULL, position.x(), position.y());
204  }
205 }
206 
207 void RfbServer::krfbSendServerCutText()
208 {
209  if(d->screen) {
210  QString text = QApplication::clipboard()->text();
211  rfbSendServerCutText(d->screen,
212  text.toLocal8Bit().data(),text.length());
213  }
214 }
215 
216 void RfbServer::onListenSocketActivated()
217 {
218  rfbProcessNewConnection(d->screen);
219 }
220 
221 void RfbServer::pendingClientFinished(RfbClient *client)
222 {
223  kDebug();
224  if (client) {
225  RfbServerManager::instance()->addClient(client);
226  client->getRfbClientPtr()->clientGoneHook = clientGoneHook;
227  }
228 }
229 
230 //static
231 rfbNewClientAction RfbServer::newClientHook(rfbClientPtr cl)
232 {
233  kDebug() << "New client";
234  RfbServer *server = static_cast<RfbServer*>(cl->screen->screenData);
235 
236  PendingRfbClient *pendingClient = server->newClient(cl);
237  connect(pendingClient, SIGNAL(finished(RfbClient*)),
238  server, SLOT(pendingClientFinished(RfbClient*)));
239 
240  return RFB_CLIENT_ON_HOLD;
241 }
242 
243 //static
244 void RfbServer::clientGoneHook(rfbClientPtr cl)
245 {
246  kDebug() << "client gone";
247  RfbClient *client = static_cast<RfbClient*>(cl->clientData);
248 
249  RfbServerManager::instance()->removeClient(client);
250  client->deleteLater();
251 }
252 
253 //static
254 rfbBool RfbServer::passwordCheck(rfbClientPtr cl, const char *encryptedPassword, int len)
255 {
256  PendingRfbClient *client = static_cast<PendingRfbClient*>(cl->clientData);
257  Q_ASSERT(client);
258  return client->checkPassword(QByteArray::fromRawData(encryptedPassword, len));
259 }
260 
261 //static
262 void RfbServer::keyboardHook(rfbBool down, rfbKeySym keySym, rfbClientPtr cl)
263 {
264  RfbClient *client = static_cast<RfbClient*>(cl->clientData);
265  client->handleKeyboardEvent(down ? true : false, keySym);
266 }
267 
268 //static
269 void RfbServer::pointerHook(int bm, int x, int y, rfbClientPtr cl)
270 {
271  RfbClient *client = static_cast<RfbClient*>(cl->clientData);
272  client->handleMouseEvent(bm, x, y);
273 }
274 
275 //static
276 void RfbServer::clipboardHook(char *str, int len, rfbClientPtr cl)
277 {
278  QApplication::clipboard()->setText(QString::fromLocal8Bit(str,len));
279 }
280 
281 #include "rfbserver.moc"
RfbClient::getRfbClientPtr
rfbClientPtr getRfbClientPtr()
Definition: rfbclient.cpp:106
RfbServer
Definition: rfbserver.h:27
_rfbScreenInfo::cursorY
int cursorY
Definition: libvncserver/rfb/rfb.h:309
RfbServer::setListeningPort
void setListeningPort(int port)
Definition: rfbserver.cpp:78
rfbGetClientIterator
rfbClientIteratorPtr rfbGetClientIterator(rfbScreenInfoPtr rfbScreen)
rfbserver.h
rfbMarkRectAsModified
void rfbMarkRectAsModified(rfbScreenInfoPtr rfbScreen, int x1, int y1, int x2, int y2)
QSocketNotifier
QByteArray
rfbSendServerCutText
void rfbSendServerCutText(rfbScreenInfoPtr rfbScreen, char *str, int len)
rfbInitServer
void rfbInitServer(rfbScreenInfoPtr rfbScreen)
TRUE
#define TRUE
Definition: rfbproto.h:106
krfb_rfbSetCursorPosition
void krfb_rfbSetCursorPosition(rfbScreenInfoPtr screen, rfbClientPtr client, int x, int y)
Definition: rfbserver.cpp:174
RfbClient::handleKeyboardEvent
virtual void handleKeyboardEvent(bool down, rfbKeySym keySym)
Definition: rfbclient.cpp:111
RfbServer::updateScreen
void updateScreen(const QList< QRect > &modifiedTiles)
Definition: rfbserver.cpp:158
rfbBool
int8_t rfbBool
Definition: rfbproto.h:102
rfbClientIteratorPtr
struct rfbClientIterator * rfbClientIteratorPtr
Definition: libvncserver/rfb/rfb.h:692
QByteArray::fromRawData
QByteArray fromRawData(const char *data, int size)
QPoint
UNLOCK
#define UNLOCK(mutex)
Definition: libvncserver/rfb/rfb.h:86
rfbKeySym
uint32_t rfbKeySym
Definition: rfbproto.h:109
_rfbClientRec::screen
rfbScreenInfoPtr screen
back pointer to the screen
Definition: libvncserver/rfb/rfb.h:420
QPoint::x
int x() const
QPoint::y
int y() const
PendingRfbClient::checkPassword
virtual bool checkPassword(const QByteArray &encryptedPassword)
This method is supposed to check if the provided encryptedPassword matches the criteria for authentic...
Definition: rfbclient.cpp:205
_rfbClientRec
Definition: libvncserver/rfb/rfb.h:417
LOCK
#define LOCK(mutex)
Definition: libvncserver/rfb/rfb.h:85
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
rfbIsActive
rfbBool rfbIsActive(rfbScreenInfoPtr screenInfo)
RfbServer::listeningPort
int listeningPort() const
Definition: rfbserver.cpp:63
QApplication::clipboard
QClipboard * clipboard()
QObject
rfbClientIteratorNext
rfbClientPtr rfbClientIteratorNext(rfbClientIteratorPtr iterator)
RfbServer::start
virtual bool start()
Definition: rfbserver.cpp:88
RfbServer::stop
virtual void stop(bool disconnectClients=true)
Definition: rfbserver.cpp:146
rfbScreenCleanup
void rfbScreenCleanup(rfbScreenInfoPtr screenInfo)
QObject::deleteLater
void deleteLater()
QString
QList< QRect >
RfbServer::setPasswordRequired
void setPasswordRequired(bool passwordRequired)
Definition: rfbserver.cpp:83
_rfbClientRec::cursorWasMoved
rfbBool cursorWasMoved
cursor position update should be sent
Definition: libvncserver/rfb/rfb.h:573
rfbShutdownServer
void rfbShutdownServer(rfbScreenInfoPtr rfbScreen, rfbBool disconnectClients)
RfbServer::~RfbServer
virtual ~RfbServer()
Definition: rfbserver.cpp:48
QString::toLocal8Bit
QByteArray toLocal8Bit() const
rfbProcessNewConnection
rfbBool rfbProcessNewConnection(rfbScreenInfoPtr rfbScreen)
_rfbScreenInfo
Per-screen (framebuffer) structure.
Definition: libvncserver/rfb/rfb.h:226
QClipboard::text
QString text(Mode mode) const
Phonon::BackendCapabilities::notifier
Notifier * notifier()
RfbClient
Definition: krfb/rfbclient.h:26
RfbServerManager::instance
static RfbServerManager * instance()
Definition: rfbservermanager.cpp:87
RfbClient::handleMouseEvent
virtual void handleMouseEvent(int buttonMask, int x, int y)
Definition: rfbclient.cpp:118
_rfbScreenInfo::cursorX
int cursorX
Definition: libvncserver/rfb/rfb.h:309
RfbServer::newClient
virtual PendingRfbClient * newClient(rfbClientPtr client)=0
rfbReleaseClientIterator
void rfbReleaseClientIterator(rfbClientIteratorPtr iterator)
RfbServer::setListeningAddress
void setListeningAddress(const QByteArray &address)
Definition: rfbserver.cpp:73
QString::length
int length() const
_rfbClientRec::clientGoneHook
ClientGoneHookPtr clientGoneHook
Definition: libvncserver/rfb/rfb.h:435
QByteArray::data
char * data()
FALSE
#define FALSE
Definition: rfbproto.h:104
_rfbScreenInfo::screenData
void * screenData
some screen specific data can be put into a struct where screenData points to.
Definition: libvncserver/rfb/rfb.h:247
RfbServer::passwordRequired
bool passwordRequired() const
Definition: rfbserver.cpp:68
PendingRfbClient
Definition: krfb/rfbclient.h:72
RfbServer::RfbServer
RfbServer(QObject *parent=0)
Definition: rfbserver.cpp:36
QClipboard::setText
void setText(const QString &text, Mode mode)
rfbNewClientAction
rfbNewClientAction
Definition: libvncserver/rfb/rfb.h:122
_rfbClientRec::clientData
void * clientData
private data.
Definition: libvncserver/rfb/rfb.h:434
RfbServer::listeningAddress
QByteArray listeningAddress() const
Definition: rfbserver.cpp:58
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
rfbservermanager.h
RfbServer::updateCursorPosition
void updateCursorPosition(const QPoint &position)
Definition: rfbserver.cpp:200
RFB_CLIENT_ON_HOLD
Definition: libvncserver/rfb/rfb.h:124
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

krfb

Skip menu "krfb"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal