blob: ae140fb3a23ff65b3fb8db41b07ca83e3b7a8aca [file] [log] [blame]
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright (C) 2004-2006 Constantin Kaplinsky. All Rights Reserved.
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000019
20// FIXME: Check cases when screen width/height is not a multiply of 32.
21// e.g. 800x600.
22
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000023#include <strings.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000024#include <sys/types.h>
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000025#include <sys/stat.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000026#include <unistd.h>
27#include <errno.h>
28#include <rfb/Logger_stdio.h>
29#include <rfb/LogWriter.h>
30#include <rfb/VNCServerST.h>
31#include <rfb/Configuration.h>
32#include <rfb/SSecurityFactoryStandard.h>
33
34#include <network/TcpSocket.h>
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +000035#include <tx/TXWindow.h>
36
Constantin Kaplinsky3f6ad6b2006-04-17 14:06:41 +000037#include <vncconfig_unix/QueryConnectDialog.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000038
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000039#include <signal.h>
40#include <X11/X.h>
41#include <X11/Xlib.h>
42#include <X11/Xutil.h>
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000043#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000044#include <X11/extensions/XTest.h>
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000045#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000046
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000047#include <x0vncserver/Image.h>
48#include <x0vncserver/PollingManager.h>
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +000049#include <x0vncserver/PollingScheduler.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000050
Constantin Kaplinskya6866902006-03-02 12:03:30 +000051// XXX Lynx/OS 2.3: protos for select(), bzero()
52#ifdef Lynx
53#include <sys/proto.h>
54#endif
55
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000056using namespace rfb;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000057using namespace network;
58
59LogWriter vlog("main");
60
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +000061IntParameter pollingCycle("PollingCycle", "Milliseconds per one polling "
62 "cycle; actual interval may be dynamically "
Constantin Kaplinskyef7ac9b2006-02-10 12:26:54 +000063 "adjusted to satisfy MaxProcessorUsage setting", 30);
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +000064IntParameter maxProcessorUsage("MaxProcessorUsage", "Maximum percentage of "
65 "CPU time to be consumed", 35);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000066BoolParameter useShm("UseSHM", "Use MIT-SHM extension if available", true);
67BoolParameter useOverlay("OverlayMode", "Use overlay mode under "
68 "IRIX or Solaris", true);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000069StringParameter displayname("display", "The X display", "");
70IntParameter rfbport("rfbport", "TCP port to listen for RFB protocol",5900);
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +000071IntParameter queryConnectTimeout("QueryConnectTimeout",
72 "Number of seconds to show the Accept Connection dialog before "
73 "rejecting the connection",
74 10);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000075StringParameter hostsFile("HostsFile", "File with IP access control rules", "");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000076
77static void CleanupSignalHandler(int sig)
78{
79 // CleanupSignalHandler allows C++ object cleanup to happen because it calls
80 // exit() rather than the default which is to abort.
81 fprintf(stderr,"CleanupSignalHandler called\n");
82 exit(1);
83}
84
85
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +000086class QueryConnHandler : public VNCServerST::QueryConnectionHandler,
87 public QueryResultCallback {
88public:
89 QueryConnHandler(Display* dpy, VNCServerST* vs)
90 : display(dpy), server(vs), queryConnectDialog(0), queryConnectSock(0) {}
91 ~QueryConnHandler() { delete queryConnectDialog; }
92
93 // -=- VNCServerST::QueryConnectionHandler interface
94 virtual VNCServerST::queryResult queryConnection(network::Socket* sock,
95 const char* userName,
96 char** reason) {
97 if (queryConnectSock) {
98 *reason = strDup("Another connection is currently being queried.");
99 return VNCServerST::REJECT;
100 }
101 if (!userName) userName = "(anonymous)";
102 queryConnectSock = sock;
103 CharArray address(sock->getPeerAddress());
104 delete queryConnectDialog;
105 queryConnectDialog = new QueryConnectDialog(display, address.buf,
106 userName, queryConnectTimeout,
107 this);
108 queryConnectDialog->map();
109 return VNCServerST::PENDING;
110 }
111
112 // -=- QueryResultCallback interface
113 virtual void queryApproved() {
114 server->approveConnection(queryConnectSock, true, 0);
115 queryConnectSock = 0;
116 }
117 virtual void queryRejected() {
118 server->approveConnection(queryConnectSock, false,
119 "Connection rejected by local user");
120 queryConnectSock = 0;
121 }
122private:
123 Display* display;
124 VNCServerST* server;
125 QueryConnectDialog* queryConnectDialog;
126 network::Socket* queryConnectSock;
127};
128
129
130class XDesktop : public SDesktop, public ColourMap
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000131{
132public:
133 XDesktop(Display* dpy_)
Constantin Kaplinsky49476542006-04-18 09:22:53 +0000134 : dpy(dpy_), pb(0), server(0), image(0), pollmgr(0),
135 oldButtonMask(0), haveXtest(false), maxButtons(0), running(false)
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000136 {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000137#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000138 int xtestEventBase;
139 int xtestErrorBase;
140 int major, minor;
141
142 if (XTestQueryExtension(dpy, &xtestEventBase,
143 &xtestErrorBase, &major, &minor)) {
144 XTestGrabControl(dpy, True);
145 vlog.info("XTest extension present - version %d.%d",major,minor);
146 haveXtest = true;
147 } else {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000148#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000149 vlog.info("XTest extension not present");
150 vlog.info("unable to inject events or display while server is grabbed");
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000151#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000152 }
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000153#endif
154
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000155 }
156 virtual ~XDesktop() {
157 stop();
158 }
159
160 // -=- SDesktop interface
161
162 virtual void start(VNCServer* vs) {
163
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000164 // Determine actual number of buttons of the X pointer device.
165 unsigned char btnMap[8];
166 int numButtons = XGetPointerMapping(dpy, btnMap, 8);
167 maxButtons = (numButtons > 8) ? 8 : numButtons;
168 vlog.info("Enabling %d button%s of X pointer device",
169 maxButtons, (maxButtons != 1) ? "s" : "");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000170
171 int dpyWidth = DisplayWidth(dpy, DefaultScreen(dpy));
172 int dpyHeight = DisplayHeight(dpy, DefaultScreen(dpy));
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000173
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000174 ImageFactory factory((bool)useShm, (bool)useOverlay);
175 image = factory.newImage(dpy, dpyWidth, dpyHeight);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000176 image->get(DefaultRootWindow(dpy));
177
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000178 pollmgr = new PollingManager(dpy, image, &factory);
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000179 pollmgr->setVNCServer(vs);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000180
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000181 pf.bpp = image->xim->bits_per_pixel;
182 pf.depth = image->xim->depth;
183 pf.bigEndian = (image->xim->byte_order == MSBFirst);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000184 pf.trueColour = image->isTrueColor();
185 pf.redShift = ffs(image->xim->red_mask) - 1;
186 pf.greenShift = ffs(image->xim->green_mask) - 1;
187 pf.blueShift = ffs(image->xim->blue_mask) - 1;
188 pf.redMax = image->xim->red_mask >> pf.redShift;
189 pf.greenMax = image->xim->green_mask >> pf.greenShift;
190 pf.blueMax = image->xim->blue_mask >> pf.blueShift;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000191
192 pb = new FullFramePixelBuffer(pf, dpyWidth, dpyHeight,
193 (rdr::U8*)image->xim->data, this);
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000194 server = vs;
195 server->setPixelBuffer(pb);
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000196
197 running = true;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000198 }
199
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000200 virtual void stop() {
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000201 running = false;
202
203 // FIXME: Delete images as well?!
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000204 delete pb;
205 delete pollmgr;
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000206
207 pb = 0;
208 pollmgr = 0;
209 }
210
211 inline bool isRunning() {
212 return running;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000213 }
214
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000215 inline void poll() {
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000216 if (pollmgr)
217 pollmgr->poll();
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000218 }
219
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000220 virtual void pointerEvent(const Point& pos, int buttonMask) {
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000221 pollmgr->setPointerPos(pos);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000222#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000223 if (!haveXtest) return;
224 XTestFakeMotionEvent(dpy, DefaultScreen(dpy), pos.x, pos.y, CurrentTime);
225 if (buttonMask != oldButtonMask) {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000226 for (int i = 0; i < maxButtons; i++) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000227 if ((buttonMask ^ oldButtonMask) & (1<<i)) {
228 if (buttonMask & (1<<i)) {
229 XTestFakeButtonEvent(dpy, i+1, True, CurrentTime);
230 } else {
231 XTestFakeButtonEvent(dpy, i+1, False, CurrentTime);
232 }
233 }
234 }
235 }
236 oldButtonMask = buttonMask;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000237#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000238 }
239
240 virtual void keyEvent(rdr::U32 key, bool down) {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000241#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000242 if (!haveXtest) return;
243 int keycode = XKeysymToKeycode(dpy, key);
244 if (keycode)
245 XTestFakeKeyEvent(dpy, keycode, down, CurrentTime);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000246#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000247 }
248
249 virtual void clientCutText(const char* str, int len) {
250 }
251
252 virtual Point getFbSize() {
253 return Point(pb->width(), pb->height());
254 }
255
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000256 // -=- ColourMap callbacks
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000257 virtual void lookup(int index, int* r, int* g, int* b) {
258 XColor xc;
259 xc.pixel = index;
260 if (index < DisplayCells(dpy,DefaultScreen(dpy))) {
261 XQueryColor(dpy, DefaultColormap(dpy,DefaultScreen(dpy)), &xc);
262 } else {
263 xc.red = xc.green = xc.blue = 0;
264 }
265 *r = xc.red;
266 *g = xc.green;
267 *b = xc.blue;
268 }
269
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000270protected:
271 Display* dpy;
272 PixelFormat pf;
273 PixelBuffer* pb;
274 VNCServer* server;
275 Image* image;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000276 PollingManager* pollmgr;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000277 int oldButtonMask;
278 bool haveXtest;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000279 int maxButtons;
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000280 bool running;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000281};
282
283
284class FileTcpFilter : public TcpFilter
285{
286
287public:
288
289 FileTcpFilter(const char *fname)
290 : TcpFilter("-"), fileName(NULL), lastModTime(0)
291 {
292 if (fname != NULL)
293 fileName = strdup((char *)fname);
294 }
295
296 virtual ~FileTcpFilter()
297 {
298 if (fileName != NULL)
299 free(fileName);
300 }
301
302 virtual bool verifyConnection(Socket* s)
303 {
304 if (!reloadRules()) {
305 vlog.error("Could not read IP filtering rules: rejecting all clients");
306 filter.clear();
307 filter.push_back(parsePattern("-"));
308 return false;
309 }
310
311 return TcpFilter::verifyConnection(s);
312 }
313
314protected:
315
316 bool reloadRules()
317 {
318 if (fileName == NULL)
319 return true;
320
321 struct stat st;
322 if (stat(fileName, &st) != 0)
323 return false;
324
325 if (st.st_mtime != lastModTime) {
326 // Actually reload only if the file was modified
327 FILE *fp = fopen(fileName, "r");
328 if (fp == NULL)
329 return false;
330
331 // Remove all the rules from the parent class
332 filter.clear();
333
334 // Parse the file contents adding rules to the parent class
335 char buf[32];
336 while (readLine(buf, 32, fp)) {
337 if (buf[0] && strchr("+-?", buf[0])) {
338 filter.push_back(parsePattern(buf));
339 }
340 }
341
342 fclose(fp);
343 lastModTime = st.st_mtime;
344 }
345 return true;
346 }
347
348protected:
349
350 char *fileName;
351 time_t lastModTime;
352
353private:
354
355 //
356 // NOTE: we silently truncate long lines in this function.
357 //
358
359 bool readLine(char *buf, int bufSize, FILE *fp)
360 {
361 if (fp == NULL || buf == NULL || bufSize == 0)
362 return false;
363
364 if (fgets(buf, bufSize, fp) == NULL)
365 return false;
366
367 char *ptr = strchr(buf, '\n');
368 if (ptr != NULL) {
369 *ptr = '\0'; // remove newline at the end
370 } else {
371 if (!feof(fp)) {
372 int c;
373 do { // skip the rest of a long line
374 c = getc(fp);
375 } while (c != '\n' && c != EOF);
376 }
377 }
378 return true;
379 }
380
381};
382
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000383char* programName;
384
385static void usage()
386{
387 fprintf(stderr, "\nusage: %s [<parameters>]\n", programName);
388 fprintf(stderr,"\n"
389 "Parameters can be turned on with -<param> or off with -<param>=0\n"
390 "Parameters which take a value can be specified as "
391 "-<param> <value>\n"
392 "Other valid forms are <param>=<value> -<param>=<value> "
393 "--<param>=<value>\n"
394 "Parameter names are case-insensitive. The parameters are:\n\n");
395 Configuration::listParams(79, 14);
396 exit(1);
397}
398
399int main(int argc, char** argv)
400{
401 initStdIOLoggers();
402 LogWriter::setLogParams("*:stderr:30");
403
404 programName = argv[0];
405 Display* dpy;
406
407 for (int i = 1; i < argc; i++) {
408 if (Configuration::setParam(argv[i]))
409 continue;
410
411 if (argv[i][0] == '-') {
412 if (i+1 < argc) {
413 if (Configuration::setParam(&argv[i][1], argv[i+1])) {
414 i++;
415 continue;
416 }
417 }
418 usage();
419 }
420
421 usage();
422 }
423
424 CharArray dpyStr(displayname.getData());
425 if (!(dpy = XOpenDisplay(dpyStr.buf[0] ? dpyStr.buf : 0))) {
426 fprintf(stderr,"%s: unable to open display \"%s\"\r\n",
427 programName, XDisplayName(displayname.getData()));
428 exit(1);
429 }
430
431 signal(SIGHUP, CleanupSignalHandler);
432 signal(SIGINT, CleanupSignalHandler);
433 signal(SIGTERM, CleanupSignalHandler);
434
435 try {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000436 TXWindow::init(dpy,"x0vncserver");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000437 XDesktop desktop(dpy);
438 VNCServerST server("x0vncserver", &desktop);
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000439 QueryConnHandler qcHandler(dpy, &server);
440 server.setQueryConnectionHandler(&qcHandler);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000441
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000442 TcpListener listener((int)rfbport);
443 vlog.info("Listening on port %d", (int)rfbport);
444
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000445 FileTcpFilter fileTcpFilter(hostsFile.getData());
446 if (strlen(hostsFile.getData()) != 0)
447 listener.setFilter(&fileTcpFilter);
448
Constantin Kaplinskye179b5a2006-02-17 09:30:21 +0000449 PollingScheduler sched((int)pollingCycle, (int)maxProcessorUsage);
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +0000450
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000451 while (true) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000452 struct timeval tv;
453 fd_set rfds;
454 std::list<Socket*> sockets;
455 std::list<Socket*>::iterator i;
456
457 // Process any incoming X events
458 TXWindow::handleXEvents(dpy);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000459
460 FD_ZERO(&rfds);
461 FD_SET(listener.getFd(), &rfds);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000462 server.getSockets(&sockets);
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000463 int clients_connected = 0;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000464 for (i = sockets.begin(); i != sockets.end(); i++) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000465 if ((*i)->isShutdown()) {
466 server.removeSocket(*i);
467 delete (*i);
468 } else {
469 FD_SET((*i)->getFd(), &rfds);
470 clients_connected++;
471 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000472 }
473
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000474 if (clients_connected) {
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +0000475 int wait_ms = sched.millisRemaining();
476 if (wait_ms > 500) {
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000477 wait_ms = 500;
478 }
479 tv.tv_usec = wait_ms * 1000;
480#ifdef DEBUG
Constantin Kaplinsky6a34ca52006-02-17 11:29:17 +0000481 // fprintf(stderr, "[%d]\t", wait_ms);
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000482#endif
483 } else {
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +0000484 sched.reset();
485 tv.tv_usec = 100000;
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000486 }
487 tv.tv_sec = 0;
488
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000489 // Do the wait...
Constantin Kaplinskye179b5a2006-02-17 09:30:21 +0000490 sched.sleepStarted();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000491 int n = select(FD_SETSIZE, &rfds, 0, 0, &tv);
Constantin Kaplinskye179b5a2006-02-17 09:30:21 +0000492 sched.sleepFinished();
493
Constantin Kaplinsky659e9002005-09-09 08:32:02 +0000494 if (n < 0) {
495 if (errno == EINTR) {
496 vlog.debug("interrupted select() system call");
497 continue;
498 } else {
499 throw rdr::SystemException("select", errno);
500 }
501 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000502
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000503 // Accept new VNC connections
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000504 if (FD_ISSET(listener.getFd(), &rfds)) {
505 Socket* sock = listener.accept();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000506 if (sock) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000507 server.addSocket(sock);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000508 } else {
509 vlog.status("Client connection rejected");
510 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000511 }
512
513 server.getSockets(&sockets);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000514
515 // Nothing more to do if there are no client connections.
516 if (sockets.empty())
517 continue;
518
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000519 // Process events on existing VNC connections
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000520 for (i = sockets.begin(); i != sockets.end(); i++) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000521 if (FD_ISSET((*i)->getFd(), &rfds))
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000522 server.processSocketEvent(*i);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000523 }
524
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000525 // Don't poll if the desktop object in not ready.
526 if (!desktop.isRunning())
527 continue;
528
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000529 server.checkTimeouts();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000530
Constantin Kaplinskye47123e2006-02-16 16:44:50 +0000531 if (sched.goodTimeToPoll()) {
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +0000532 sched.newPass();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000533 desktop.poll();
534 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000535 }
536
537 } catch (rdr::Exception &e) {
538 vlog.error(e.str());
539 };
540
541 return 0;
542}