blob: 1f1d0f496ad90c26984f4dc2414d9559339d5070 [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>
Constantin Kaplinsky478f22c2006-04-19 06:13:06 +000033#include <rfb/Timer.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000034#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 Kaplinsky44e99642006-05-20 12:58:38 +000047#include <x0vncserver/Geometry.h>
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000048#include <x0vncserver/Image.h>
49#include <x0vncserver/PollingManager.h>
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +000050#include <x0vncserver/PollingScheduler.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000051
Constantin Kaplinskya6866902006-03-02 12:03:30 +000052// XXX Lynx/OS 2.3: protos for select(), bzero()
53#ifdef Lynx
54#include <sys/proto.h>
55#endif
56
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000057using namespace rfb;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000058using namespace network;
59
Constantin Kaplinsky068693e2006-05-22 09:07:19 +000060LogWriter vlog("Main");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000061
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +000062IntParameter pollingCycle("PollingCycle", "Milliseconds per one polling "
63 "cycle; actual interval may be dynamically "
Constantin Kaplinskyef7ac9b2006-02-10 12:26:54 +000064 "adjusted to satisfy MaxProcessorUsage setting", 30);
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +000065IntParameter maxProcessorUsage("MaxProcessorUsage", "Maximum percentage of "
66 "CPU time to be consumed", 35);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000067BoolParameter useShm("UseSHM", "Use MIT-SHM extension if available", true);
68BoolParameter useOverlay("OverlayMode", "Use overlay mode under "
69 "IRIX or Solaris", true);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000070StringParameter displayname("display", "The X display", "");
71IntParameter rfbport("rfbport", "TCP port to listen for RFB protocol",5900);
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +000072IntParameter queryConnectTimeout("QueryConnectTimeout",
73 "Number of seconds to show the Accept Connection dialog before "
74 "rejecting the connection",
75 10);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000076StringParameter hostsFile("HostsFile", "File with IP access control rules", "");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000077
Constantin Kaplinsky068693e2006-05-22 09:07:19 +000078//
79// CleanupSignalHandler allows C++ object cleanup to happen because
80// it calls exit() rather than the default which is to abort.
81//
82
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000083static void CleanupSignalHandler(int sig)
84{
Constantin Kaplinsky068693e2006-05-22 09:07:19 +000085#ifdef DEBUG
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000086 fprintf(stderr,"CleanupSignalHandler called\n");
Constantin Kaplinsky068693e2006-05-22 09:07:19 +000087#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000088 exit(1);
89}
90
91
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +000092class QueryConnHandler : public VNCServerST::QueryConnectionHandler,
93 public QueryResultCallback {
94public:
95 QueryConnHandler(Display* dpy, VNCServerST* vs)
96 : display(dpy), server(vs), queryConnectDialog(0), queryConnectSock(0) {}
97 ~QueryConnHandler() { delete queryConnectDialog; }
98
99 // -=- VNCServerST::QueryConnectionHandler interface
100 virtual VNCServerST::queryResult queryConnection(network::Socket* sock,
101 const char* userName,
102 char** reason) {
103 if (queryConnectSock) {
104 *reason = strDup("Another connection is currently being queried.");
105 return VNCServerST::REJECT;
106 }
107 if (!userName) userName = "(anonymous)";
108 queryConnectSock = sock;
109 CharArray address(sock->getPeerAddress());
110 delete queryConnectDialog;
111 queryConnectDialog = new QueryConnectDialog(display, address.buf,
112 userName, queryConnectTimeout,
113 this);
114 queryConnectDialog->map();
115 return VNCServerST::PENDING;
116 }
117
118 // -=- QueryResultCallback interface
119 virtual void queryApproved() {
120 server->approveConnection(queryConnectSock, true, 0);
121 queryConnectSock = 0;
122 }
123 virtual void queryRejected() {
124 server->approveConnection(queryConnectSock, false,
125 "Connection rejected by local user");
126 queryConnectSock = 0;
127 }
128private:
129 Display* display;
130 VNCServerST* server;
131 QueryConnectDialog* queryConnectDialog;
132 network::Socket* queryConnectSock;
133};
134
135
136class XDesktop : public SDesktop, public ColourMap
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000137{
138public:
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000139 XDesktop(Display* dpy_, Geometry *geometry_)
140 : dpy(dpy_), geometry(geometry_), pb(0), server(0), image(0), pollmgr(0),
Constantin Kaplinsky49476542006-04-18 09:22:53 +0000141 oldButtonMask(0), haveXtest(false), maxButtons(0), running(false)
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000142 {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000143#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000144 int xtestEventBase;
145 int xtestErrorBase;
146 int major, minor;
147
148 if (XTestQueryExtension(dpy, &xtestEventBase,
149 &xtestErrorBase, &major, &minor)) {
150 XTestGrabControl(dpy, True);
151 vlog.info("XTest extension present - version %d.%d",major,minor);
152 haveXtest = true;
153 } else {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000154#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000155 vlog.info("XTest extension not present");
156 vlog.info("unable to inject events or display while server is grabbed");
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000157#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000158 }
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000159#endif
160
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000161 }
162 virtual ~XDesktop() {
163 stop();
164 }
165
166 // -=- SDesktop interface
167
168 virtual void start(VNCServer* vs) {
169
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000170 // Determine actual number of buttons of the X pointer device.
171 unsigned char btnMap[8];
172 int numButtons = XGetPointerMapping(dpy, btnMap, 8);
173 maxButtons = (numButtons > 8) ? 8 : numButtons;
174 vlog.info("Enabling %d button%s of X pointer device",
175 maxButtons, (maxButtons != 1) ? "s" : "");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000176
Constantin Kaplinsky99d65142006-05-22 05:45:20 +0000177 // Create an image for maintaining framebuffer data.
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000178 ImageFactory factory((bool)useShm, (bool)useOverlay);
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000179 image = factory.newImage(dpy, geometry->width(), geometry->height());
Constantin Kaplinsky068693e2006-05-22 09:07:19 +0000180 vlog.info("Allocated %s", image->classDesc());
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000181
Constantin Kaplinsky99d65142006-05-22 05:45:20 +0000182 // Create polling manager object. It will track screen changes and
183 // keep pixels of the `image' object up to date.
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000184 pollmgr = new PollingManager(dpy, image, &factory,
185 geometry->offsetLeft(),
186 geometry->offsetTop());
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000187 pollmgr->setVNCServer(vs);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000188
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000189 pf.bpp = image->xim->bits_per_pixel;
190 pf.depth = image->xim->depth;
191 pf.bigEndian = (image->xim->byte_order == MSBFirst);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000192 pf.trueColour = image->isTrueColor();
193 pf.redShift = ffs(image->xim->red_mask) - 1;
194 pf.greenShift = ffs(image->xim->green_mask) - 1;
195 pf.blueShift = ffs(image->xim->blue_mask) - 1;
196 pf.redMax = image->xim->red_mask >> pf.redShift;
197 pf.greenMax = image->xim->green_mask >> pf.greenShift;
198 pf.blueMax = image->xim->blue_mask >> pf.blueShift;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000199
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000200 pb = new FullFramePixelBuffer(pf, geometry->width(), geometry->height(),
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000201 (rdr::U8*)image->xim->data, this);
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000202 server = vs;
203 server->setPixelBuffer(pb);
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000204
205 running = true;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000206 }
207
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000208 virtual void stop() {
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000209 running = false;
210
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000211 delete pb;
212 delete pollmgr;
Constantin Kaplinsky93c3f892006-04-19 10:32:33 +0000213 delete image;
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000214
215 pb = 0;
216 pollmgr = 0;
Constantin Kaplinsky93c3f892006-04-19 10:32:33 +0000217 image = 0;
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000218 }
219
220 inline bool isRunning() {
221 return running;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000222 }
223
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000224 inline void poll() {
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000225 if (pollmgr)
226 pollmgr->poll();
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000227 }
228
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000229 virtual void pointerEvent(const Point& pos, int buttonMask) {
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000230 pollmgr->setPointerPos(pos);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000231#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000232 if (!haveXtest) return;
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000233 XTestFakeMotionEvent(dpy, DefaultScreen(dpy),
234 geometry->offsetLeft() + pos.x,
235 geometry->offsetTop() + pos.y,
236 CurrentTime);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000237 if (buttonMask != oldButtonMask) {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000238 for (int i = 0; i < maxButtons; i++) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000239 if ((buttonMask ^ oldButtonMask) & (1<<i)) {
240 if (buttonMask & (1<<i)) {
241 XTestFakeButtonEvent(dpy, i+1, True, CurrentTime);
242 } else {
243 XTestFakeButtonEvent(dpy, i+1, False, CurrentTime);
244 }
245 }
246 }
247 }
248 oldButtonMask = buttonMask;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000249#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000250 }
251
252 virtual void keyEvent(rdr::U32 key, bool down) {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000253#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000254 if (!haveXtest) return;
255 int keycode = XKeysymToKeycode(dpy, key);
256 if (keycode)
257 XTestFakeKeyEvent(dpy, keycode, down, CurrentTime);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000258#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000259 }
260
261 virtual void clientCutText(const char* str, int len) {
262 }
263
264 virtual Point getFbSize() {
265 return Point(pb->width(), pb->height());
266 }
267
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000268 // -=- ColourMap callbacks
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000269 virtual void lookup(int index, int* r, int* g, int* b) {
270 XColor xc;
271 xc.pixel = index;
272 if (index < DisplayCells(dpy,DefaultScreen(dpy))) {
273 XQueryColor(dpy, DefaultColormap(dpy,DefaultScreen(dpy)), &xc);
274 } else {
275 xc.red = xc.green = xc.blue = 0;
276 }
277 *r = xc.red;
278 *g = xc.green;
279 *b = xc.blue;
280 }
281
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000282protected:
283 Display* dpy;
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000284 Geometry* geometry;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000285 PixelFormat pf;
286 PixelBuffer* pb;
287 VNCServer* server;
288 Image* image;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000289 PollingManager* pollmgr;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000290 int oldButtonMask;
291 bool haveXtest;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000292 int maxButtons;
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000293 bool running;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000294};
295
296
297class FileTcpFilter : public TcpFilter
298{
299
300public:
301
302 FileTcpFilter(const char *fname)
303 : TcpFilter("-"), fileName(NULL), lastModTime(0)
304 {
305 if (fname != NULL)
306 fileName = strdup((char *)fname);
307 }
308
309 virtual ~FileTcpFilter()
310 {
311 if (fileName != NULL)
312 free(fileName);
313 }
314
315 virtual bool verifyConnection(Socket* s)
316 {
317 if (!reloadRules()) {
318 vlog.error("Could not read IP filtering rules: rejecting all clients");
319 filter.clear();
320 filter.push_back(parsePattern("-"));
321 return false;
322 }
323
324 return TcpFilter::verifyConnection(s);
325 }
326
327protected:
328
329 bool reloadRules()
330 {
331 if (fileName == NULL)
332 return true;
333
334 struct stat st;
335 if (stat(fileName, &st) != 0)
336 return false;
337
338 if (st.st_mtime != lastModTime) {
339 // Actually reload only if the file was modified
340 FILE *fp = fopen(fileName, "r");
341 if (fp == NULL)
342 return false;
343
344 // Remove all the rules from the parent class
345 filter.clear();
346
347 // Parse the file contents adding rules to the parent class
348 char buf[32];
349 while (readLine(buf, 32, fp)) {
350 if (buf[0] && strchr("+-?", buf[0])) {
351 filter.push_back(parsePattern(buf));
352 }
353 }
354
355 fclose(fp);
356 lastModTime = st.st_mtime;
357 }
358 return true;
359 }
360
361protected:
362
363 char *fileName;
364 time_t lastModTime;
365
366private:
367
368 //
369 // NOTE: we silently truncate long lines in this function.
370 //
371
372 bool readLine(char *buf, int bufSize, FILE *fp)
373 {
374 if (fp == NULL || buf == NULL || bufSize == 0)
375 return false;
376
377 if (fgets(buf, bufSize, fp) == NULL)
378 return false;
379
380 char *ptr = strchr(buf, '\n');
381 if (ptr != NULL) {
382 *ptr = '\0'; // remove newline at the end
383 } else {
384 if (!feof(fp)) {
385 int c;
386 do { // skip the rest of a long line
387 c = getc(fp);
388 } while (c != '\n' && c != EOF);
389 }
390 }
391 return true;
392 }
393
394};
395
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000396char* programName;
397
398static void usage()
399{
400 fprintf(stderr, "\nusage: %s [<parameters>]\n", programName);
401 fprintf(stderr,"\n"
402 "Parameters can be turned on with -<param> or off with -<param>=0\n"
403 "Parameters which take a value can be specified as "
404 "-<param> <value>\n"
405 "Other valid forms are <param>=<value> -<param>=<value> "
406 "--<param>=<value>\n"
407 "Parameter names are case-insensitive. The parameters are:\n\n");
408 Configuration::listParams(79, 14);
409 exit(1);
410}
411
412int main(int argc, char** argv)
413{
414 initStdIOLoggers();
415 LogWriter::setLogParams("*:stderr:30");
416
417 programName = argv[0];
418 Display* dpy;
419
420 for (int i = 1; i < argc; i++) {
421 if (Configuration::setParam(argv[i]))
422 continue;
423
424 if (argv[i][0] == '-') {
425 if (i+1 < argc) {
426 if (Configuration::setParam(&argv[i][1], argv[i+1])) {
427 i++;
428 continue;
429 }
430 }
431 usage();
432 }
433
434 usage();
435 }
436
437 CharArray dpyStr(displayname.getData());
438 if (!(dpy = XOpenDisplay(dpyStr.buf[0] ? dpyStr.buf : 0))) {
439 fprintf(stderr,"%s: unable to open display \"%s\"\r\n",
440 programName, XDisplayName(displayname.getData()));
441 exit(1);
442 }
443
444 signal(SIGHUP, CleanupSignalHandler);
445 signal(SIGINT, CleanupSignalHandler);
446 signal(SIGTERM, CleanupSignalHandler);
447
448 try {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000449 TXWindow::init(dpy,"x0vncserver");
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000450 Geometry geo(DisplayWidth(dpy, DefaultScreen(dpy)),
451 DisplayHeight(dpy, DefaultScreen(dpy)));
452 XDesktop desktop(dpy, &geo);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000453 VNCServerST server("x0vncserver", &desktop);
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000454 QueryConnHandler qcHandler(dpy, &server);
455 server.setQueryConnectionHandler(&qcHandler);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000456
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000457 TcpListener listener((int)rfbport);
458 vlog.info("Listening on port %d", (int)rfbport);
459
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000460 FileTcpFilter fileTcpFilter(hostsFile.getData());
461 if (strlen(hostsFile.getData()) != 0)
462 listener.setFilter(&fileTcpFilter);
463
Constantin Kaplinskye179b5a2006-02-17 09:30:21 +0000464 PollingScheduler sched((int)pollingCycle, (int)maxProcessorUsage);
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +0000465
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000466 while (true) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000467 struct timeval tv;
468 fd_set rfds;
469 std::list<Socket*> sockets;
470 std::list<Socket*>::iterator i;
471
472 // Process any incoming X events
473 TXWindow::handleXEvents(dpy);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000474
475 FD_ZERO(&rfds);
476 FD_SET(listener.getFd(), &rfds);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000477 server.getSockets(&sockets);
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000478 int clients_connected = 0;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000479 for (i = sockets.begin(); i != sockets.end(); i++) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000480 if ((*i)->isShutdown()) {
481 server.removeSocket(*i);
482 delete (*i);
483 } else {
484 FD_SET((*i)->getFd(), &rfds);
485 clients_connected++;
486 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000487 }
488
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000489 if (clients_connected) {
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +0000490 int wait_ms = sched.millisRemaining();
491 if (wait_ms > 500) {
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000492 wait_ms = 500;
493 }
494 tv.tv_usec = wait_ms * 1000;
495#ifdef DEBUG
Constantin Kaplinsky6a34ca52006-02-17 11:29:17 +0000496 // fprintf(stderr, "[%d]\t", wait_ms);
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000497#endif
498 } else {
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +0000499 sched.reset();
500 tv.tv_usec = 100000;
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000501 }
502 tv.tv_sec = 0;
503
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000504 // Do the wait...
Constantin Kaplinskye179b5a2006-02-17 09:30:21 +0000505 sched.sleepStarted();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000506 int n = select(FD_SETSIZE, &rfds, 0, 0, &tv);
Constantin Kaplinskye179b5a2006-02-17 09:30:21 +0000507 sched.sleepFinished();
508
Constantin Kaplinsky659e9002005-09-09 08:32:02 +0000509 if (n < 0) {
510 if (errno == EINTR) {
511 vlog.debug("interrupted select() system call");
512 continue;
513 } else {
514 throw rdr::SystemException("select", errno);
515 }
516 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000517
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000518 // Accept new VNC connections
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000519 if (FD_ISSET(listener.getFd(), &rfds)) {
520 Socket* sock = listener.accept();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000521 if (sock) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000522 server.addSocket(sock);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000523 } else {
524 vlog.status("Client connection rejected");
525 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000526 }
527
Constantin Kaplinsky478f22c2006-04-19 06:13:06 +0000528 Timer::checkTimeouts();
529 server.checkTimeouts();
530
531 // Client list could have been changed.
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000532 server.getSockets(&sockets);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000533
534 // Nothing more to do if there are no client connections.
535 if (sockets.empty())
536 continue;
537
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000538 // Process events on existing VNC connections
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000539 for (i = sockets.begin(); i != sockets.end(); i++) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000540 if (FD_ISSET((*i)->getFd(), &rfds))
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000541 server.processSocketEvent(*i);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000542 }
543
Constantin Kaplinsky478f22c2006-04-19 06:13:06 +0000544 if (desktop.isRunning() && sched.goodTimeToPoll()) {
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +0000545 sched.newPass();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000546 desktop.poll();
547 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000548 }
549
550 } catch (rdr::Exception &e) {
551 vlog.error(e.str());
552 };
553
554 return 0;
555}