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

krfb

  • sources
  • kde-4.12
  • 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 <KDebug>
24 
25 struct RfbServer::Private
26 {
27  QByteArray listeningAddress;
28  int listeningPort;
29  bool passwordRequired;
30  rfbScreenInfoPtr screen;
31  QSocketNotifier *notifier;
32 };
33 
34 RfbServer::RfbServer(QObject *parent)
35  : QObject(parent), d(new Private)
36 {
37  d->listeningAddress = "0.0.0.0";
38  d->listeningPort = 0;
39  d->passwordRequired = true;
40  d->screen = NULL;
41  d->notifier = NULL;
42 
43  RfbServerManager::instance()->registerServer(this);
44 }
45 
46 RfbServer::~RfbServer()
47 {
48  stop();
49  if (d->screen) {
50  rfbScreenCleanup(d->screen);
51  }
52  delete d;
53 
54  RfbServerManager::instance()->unregisterServer(this);
55 }
56 
57 QByteArray RfbServer::listeningAddress() const
58 {
59  return d->listeningAddress;
60 }
61 
62 int RfbServer::listeningPort() const
63 {
64  return d->listeningPort;
65 }
66 
67 bool RfbServer::passwordRequired() const
68 {
69  return d->passwordRequired;
70 }
71 
72 void RfbServer::setListeningAddress(const QByteArray& address)
73 {
74  d->listeningAddress = address;
75 }
76 
77 void RfbServer::setListeningPort(int port)
78 {
79  d->listeningPort = port;
80 }
81 
82 void RfbServer::setPasswordRequired(bool passwordRequired)
83 {
84  d->passwordRequired = passwordRequired;
85 }
86 
87 bool RfbServer::start()
88 {
89  if (!d->screen) {
90  d->screen = RfbServerManager::instance()->newScreen();
91  if (!d->screen) {
92  return false;
93  }
94 
95  // server hooks
96  d->screen->screenData = this;
97  d->screen->newClientHook = newClientHook;
98  d->screen->kbdAddEvent = keyboardHook;
99  d->screen->ptrAddEvent = pointerHook;
100  d->screen->passwordCheck = passwordCheck;
101  d->screen->setXCutText = clipboardHook;
102  } else {
103  //if we already have a screen, stop listening first
104  rfbShutdownServer(d->screen, false);
105  }
106 
107  if (listeningAddress() != "0.0.0.0") {
108  strncpy(d->screen->thisHost, listeningAddress().data(), 254);
109  }
110 
111  if (listeningPort() == 0) {
112  d->screen->autoPort = 1;
113  }
114 
115  d->screen->port = listeningPort();
116 
117  // Disable/Enable password checking
118  if (passwordRequired()) {
119  d->screen->authPasswdData = (void *)1;
120  } else {
121  d->screen->authPasswdData = (void *)0;
122  }
123 
124  kDebug() << "Starting server. Listen port:" << listeningPort()
125  << "Listen Address:" << listeningAddress()
126  << "Password enabled:" << passwordRequired();
127 
128  rfbInitServer(d->screen);
129 
130  if (!rfbIsActive(d->screen)) {
131  kDebug() << "Failed to start server";
132  rfbShutdownServer(d->screen, false);
133  return false;
134  };
135 
136  d->notifier = new QSocketNotifier(d->screen->listenSock, QSocketNotifier::Read, this);
137  d->notifier->setEnabled(true);
138  connect(d->notifier, SIGNAL(activated(int)), this, SLOT(onListenSocketActivated()));
139 
140  return true;
141 }
142 
143 void RfbServer::stop(bool disconnectClients)
144 {
145  if (d->screen) {
146  rfbShutdownServer(d->screen, disconnectClients);
147  if (d->notifier) {
148  d->notifier->setEnabled(false);
149  d->notifier->deleteLater();
150  d->notifier = NULL;
151  }
152  }
153 }
154 
155 void RfbServer::updateScreen(const QList<QRect> & modifiedTiles)
156 {
157  if (d->screen) {
158  QList<QRect>::const_iterator it = modifiedTiles.constBegin();
159  for(; it != modifiedTiles.constEnd(); ++it) {
160  rfbMarkRectAsModified(d->screen, it->x(), it->y(), it->right(), it->bottom());
161  }
162  }
163 }
164 
165 /*
166  * Code copied from vino's bundled libvncserver:
167  * Copyright (C) 2000, 2001 Const Kaplinsky. All Rights Reserved.
168  * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
169  * License: GPL v2 or later
170  */
171 void krfb_rfbSetCursorPosition(rfbScreenInfoPtr screen, rfbClientPtr client, int x, int y)
172 {
173  rfbClientIteratorPtr iterator;
174  rfbClientPtr cl;
175 
176  if (x == screen->cursorX || y == screen->cursorY)
177  return;
178 
179  LOCK(screen->cursorMutex);
180  screen->cursorX = x;
181  screen->cursorY = y;
182  UNLOCK(screen->cursorMutex);
183 
184  /* Inform all clients about this cursor movement. */
185  iterator = rfbGetClientIterator(screen);
186  while ((cl = rfbClientIteratorNext(iterator)) != NULL) {
187  cl->cursorWasMoved = TRUE;
188  }
189  rfbReleaseClientIterator(iterator);
190 
191  /* The cursor was moved by this client, so don't send CursorPos. */
192  if (client) {
193  client->cursorWasMoved = FALSE;
194  }
195 }
196 
197 void RfbServer::updateCursorPosition(const QPoint & position)
198 {
199  if (d->screen) {
200  krfb_rfbSetCursorPosition(d->screen, NULL, position.x(), position.y());
201  }
202 }
203 
204 void RfbServer::onListenSocketActivated()
205 {
206  rfbProcessNewConnection(d->screen);
207 }
208 
209 void RfbServer::pendingClientFinished(RfbClient *client)
210 {
211  kDebug();
212  if (client) {
213  RfbServerManager::instance()->addClient(client);
214  }
215 }
216 
217 //static
218 rfbNewClientAction RfbServer::newClientHook(rfbClientPtr cl)
219 {
220  kDebug() << "New client";
221  RfbServer *server = static_cast<RfbServer*>(cl->screen->screenData);
222 
223  PendingRfbClient *pendingClient = server->newClient(cl);
224  connect(pendingClient, SIGNAL(finished(RfbClient*)),
225  server, SLOT(pendingClientFinished(RfbClient*)));
226 
227  cl->clientGoneHook = clientGoneHook;
228  return RFB_CLIENT_ON_HOLD;
229 }
230 
231 //static
232 void RfbServer::clientGoneHook(rfbClientPtr cl)
233 {
234  kDebug() << "client gone";
235  RfbClient *client = static_cast<RfbClient*>(cl->clientData);
236 
237  RfbServerManager::instance()->removeClient(client);
238  client->deleteLater();
239 }
240 
241 //static
242 rfbBool RfbServer::passwordCheck(rfbClientPtr cl, const char *encryptedPassword, int len)
243 {
244  RfbClient *client = static_cast<RfbClient*>(cl->clientData);
245  return client->checkPassword(QByteArray::fromRawData(encryptedPassword, len));
246 }
247 
248 //static
249 void RfbServer::keyboardHook(rfbBool down, rfbKeySym keySym, rfbClientPtr cl)
250 {
251  RfbClient *client = static_cast<RfbClient*>(cl->clientData);
252  client->handleKeyboardEvent(down ? true : false, keySym);
253 }
254 
255 //static
256 void RfbServer::pointerHook(int bm, int x, int y, rfbClientPtr cl)
257 {
258  RfbClient *client = static_cast<RfbClient*>(cl->clientData);
259  client->handleMouseEvent(bm, x, y);
260 }
261 
262 //static
263 void RfbServer::clipboardHook(char *str, int len, rfbClientPtr cl)
264 {
265  //TODO implement me
266 }
267 
268 #include "rfbserver.moc"
RfbServer
Definition: rfbserver.h:27
_rfbScreenInfo::cursorY
int cursorY
Definition: libvncserver/rfb/rfb.h:309
RfbServer::setListeningPort
void setListeningPort(int port)
Definition: rfbserver.cpp:77
rfbGetClientIterator
rfbClientIteratorPtr rfbGetClientIterator(rfbScreenInfoPtr rfbScreen)
rfbserver.h
rfbMarkRectAsModified
void rfbMarkRectAsModified(rfbScreenInfoPtr rfbScreen, int x1, int y1, int x2, int y2)
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:171
RfbClient::handleKeyboardEvent
virtual void handleKeyboardEvent(bool down, rfbKeySym keySym)
Definition: rfbclient.cpp:106
RfbServer::updateScreen
void updateScreen(const QList< QRect > &modifiedTiles)
Definition: rfbserver.cpp:155
rfbBool
int8_t rfbBool
Definition: rfbproto.h:102
rfbClientIteratorPtr
struct rfbClientIterator * rfbClientIteratorPtr
Definition: libvncserver/rfb/rfb.h:692
UNLOCK
#define UNLOCK(mutex)
Definition: libvncserver/rfb/rfb.h:86
QObject
rfbKeySym
uint32_t rfbKeySym
Definition: rfbproto.h:109
_rfbClientRec::screen
rfbScreenInfoPtr screen
back pointer to the screen
Definition: libvncserver/rfb/rfb.h:420
_rfbClientRec
Definition: libvncserver/rfb/rfb.h:417
LOCK
#define LOCK(mutex)
Definition: libvncserver/rfb/rfb.h:85
rfbIsActive
rfbBool rfbIsActive(rfbScreenInfoPtr screenInfo)
RfbServer::listeningPort
int listeningPort() const
Definition: rfbserver.cpp:62
RfbClient::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:120
rfbClientIteratorNext
rfbClientPtr rfbClientIteratorNext(rfbClientIteratorPtr iterator)
RfbServer::start
bool start()
Definition: rfbserver.cpp:87
RfbServer::stop
void stop(bool disconnectClients=true)
Definition: rfbserver.cpp:143
rfbScreenCleanup
void rfbScreenCleanup(rfbScreenInfoPtr screenInfo)
RfbServer::setPasswordRequired
void setPasswordRequired(bool passwordRequired)
Definition: rfbserver.cpp:82
_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:46
rfbProcessNewConnection
rfbBool rfbProcessNewConnection(rfbScreenInfoPtr rfbScreen)
_rfbScreenInfo
Per-screen (framebuffer) structure.
Definition: libvncserver/rfb/rfb.h:226
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:113
_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:72
_rfbClientRec::clientGoneHook
ClientGoneHookPtr clientGoneHook
Definition: libvncserver/rfb/rfb.h:435
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:67
PendingRfbClient
Definition: pendingrfbclient.h:24
RfbServer::RfbServer
RfbServer(QObject *parent=0)
Definition: rfbserver.cpp:34
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:57
rfbservermanager.h
RfbServer::updateCursorPosition
void updateCursorPosition(const QPoint &position)
Definition: rfbserver.cpp:197
RFB_CLIENT_ON_HOLD
Definition: libvncserver/rfb/rfb.h:124
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:54:10 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
  • 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