blob: d3423830d59f074a6785658ffb8d5b3f152abf99 [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 Kaplinskybfc4e8c2006-05-22 10:08:56 +000060static LogWriter 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//
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +000079// Allow the main loop terminate itself gracefully on receiving a signal.
Constantin Kaplinsky068693e2006-05-22 09:07:19 +000080//
81
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +000082static bool caughtSignal = false;
83
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000084static void CleanupSignalHandler(int sig)
85{
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +000086 caughtSignal = true;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000087}
88
89
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +000090class QueryConnHandler : public VNCServerST::QueryConnectionHandler,
91 public QueryResultCallback {
92public:
93 QueryConnHandler(Display* dpy, VNCServerST* vs)
94 : display(dpy), server(vs), queryConnectDialog(0), queryConnectSock(0) {}
95 ~QueryConnHandler() { delete queryConnectDialog; }
96
97 // -=- VNCServerST::QueryConnectionHandler interface
98 virtual VNCServerST::queryResult queryConnection(network::Socket* sock,
99 const char* userName,
100 char** reason) {
101 if (queryConnectSock) {
102 *reason = strDup("Another connection is currently being queried.");
103 return VNCServerST::REJECT;
104 }
105 if (!userName) userName = "(anonymous)";
106 queryConnectSock = sock;
107 CharArray address(sock->getPeerAddress());
108 delete queryConnectDialog;
109 queryConnectDialog = new QueryConnectDialog(display, address.buf,
110 userName, queryConnectTimeout,
111 this);
112 queryConnectDialog->map();
113 return VNCServerST::PENDING;
114 }
115
116 // -=- QueryResultCallback interface
117 virtual void queryApproved() {
118 server->approveConnection(queryConnectSock, true, 0);
119 queryConnectSock = 0;
120 }
121 virtual void queryRejected() {
122 server->approveConnection(queryConnectSock, false,
123 "Connection rejected by local user");
124 queryConnectSock = 0;
125 }
126private:
127 Display* display;
128 VNCServerST* server;
129 QueryConnectDialog* queryConnectDialog;
130 network::Socket* queryConnectSock;
131};
132
133
134class XDesktop : public SDesktop, public ColourMap
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000135{
136public:
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000137 XDesktop(Display* dpy_, Geometry *geometry_)
138 : dpy(dpy_), geometry(geometry_), pb(0), server(0), image(0), pollmgr(0),
Constantin Kaplinsky49476542006-04-18 09:22:53 +0000139 oldButtonMask(0), haveXtest(false), maxButtons(0), running(false)
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000140 {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000141#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000142 int xtestEventBase;
143 int xtestErrorBase;
144 int major, minor;
145
146 if (XTestQueryExtension(dpy, &xtestEventBase,
147 &xtestErrorBase, &major, &minor)) {
148 XTestGrabControl(dpy, True);
149 vlog.info("XTest extension present - version %d.%d",major,minor);
150 haveXtest = true;
151 } else {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000152#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000153 vlog.info("XTest extension not present");
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +0000154 vlog.info("Unable to inject events or display while server is grabbed");
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000155#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000156 }
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000157#endif
158
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000159 }
160 virtual ~XDesktop() {
161 stop();
162 }
163
164 // -=- SDesktop interface
165
166 virtual void start(VNCServer* vs) {
167
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000168 // Determine actual number of buttons of the X pointer device.
169 unsigned char btnMap[8];
170 int numButtons = XGetPointerMapping(dpy, btnMap, 8);
171 maxButtons = (numButtons > 8) ? 8 : numButtons;
172 vlog.info("Enabling %d button%s of X pointer device",
173 maxButtons, (maxButtons != 1) ? "s" : "");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000174
Constantin Kaplinsky99d65142006-05-22 05:45:20 +0000175 // Create an image for maintaining framebuffer data.
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000176 ImageFactory factory((bool)useShm, (bool)useOverlay);
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000177 image = factory.newImage(dpy, geometry->width(), geometry->height());
Constantin Kaplinsky068693e2006-05-22 09:07:19 +0000178 vlog.info("Allocated %s", image->classDesc());
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000179
Constantin Kaplinsky99d65142006-05-22 05:45:20 +0000180 // Create polling manager object. It will track screen changes and
181 // keep pixels of the `image' object up to date.
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000182 pollmgr = new PollingManager(dpy, image, &factory,
183 geometry->offsetLeft(),
184 geometry->offsetTop());
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000185 pollmgr->setVNCServer(vs);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000186
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000187 pf.bpp = image->xim->bits_per_pixel;
188 pf.depth = image->xim->depth;
189 pf.bigEndian = (image->xim->byte_order == MSBFirst);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000190 pf.trueColour = image->isTrueColor();
191 pf.redShift = ffs(image->xim->red_mask) - 1;
192 pf.greenShift = ffs(image->xim->green_mask) - 1;
193 pf.blueShift = ffs(image->xim->blue_mask) - 1;
194 pf.redMax = image->xim->red_mask >> pf.redShift;
195 pf.greenMax = image->xim->green_mask >> pf.greenShift;
196 pf.blueMax = image->xim->blue_mask >> pf.blueShift;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000197
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000198 pb = new FullFramePixelBuffer(pf, geometry->width(), geometry->height(),
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000199 (rdr::U8*)image->xim->data, this);
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000200 server = vs;
201 server->setPixelBuffer(pb);
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000202
203 running = true;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000204 }
205
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000206 virtual void stop() {
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000207 running = false;
208
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000209 delete pb;
210 delete pollmgr;
Constantin Kaplinsky93c3f892006-04-19 10:32:33 +0000211 delete image;
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000212
213 pb = 0;
214 pollmgr = 0;
Constantin Kaplinsky93c3f892006-04-19 10:32:33 +0000215 image = 0;
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000216 }
217
218 inline bool isRunning() {
219 return running;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000220 }
221
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000222 inline void poll() {
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000223 if (pollmgr)
224 pollmgr->poll();
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000225 }
226
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000227 virtual void pointerEvent(const Point& pos, int buttonMask) {
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000228 pollmgr->setPointerPos(pos);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000229#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000230 if (!haveXtest) return;
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000231 XTestFakeMotionEvent(dpy, DefaultScreen(dpy),
232 geometry->offsetLeft() + pos.x,
233 geometry->offsetTop() + pos.y,
234 CurrentTime);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000235 if (buttonMask != oldButtonMask) {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000236 for (int i = 0; i < maxButtons; i++) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000237 if ((buttonMask ^ oldButtonMask) & (1<<i)) {
238 if (buttonMask & (1<<i)) {
239 XTestFakeButtonEvent(dpy, i+1, True, CurrentTime);
240 } else {
241 XTestFakeButtonEvent(dpy, i+1, False, CurrentTime);
242 }
243 }
244 }
245 }
246 oldButtonMask = buttonMask;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000247#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000248 }
249
250 virtual void keyEvent(rdr::U32 key, bool down) {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000251#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000252 if (!haveXtest) return;
253 int keycode = XKeysymToKeycode(dpy, key);
254 if (keycode)
255 XTestFakeKeyEvent(dpy, keycode, down, CurrentTime);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000256#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000257 }
258
259 virtual void clientCutText(const char* str, int len) {
260 }
261
262 virtual Point getFbSize() {
263 return Point(pb->width(), pb->height());
264 }
265
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000266 // -=- ColourMap callbacks
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000267 virtual void lookup(int index, int* r, int* g, int* b) {
268 XColor xc;
269 xc.pixel = index;
270 if (index < DisplayCells(dpy,DefaultScreen(dpy))) {
271 XQueryColor(dpy, DefaultColormap(dpy,DefaultScreen(dpy)), &xc);
272 } else {
273 xc.red = xc.green = xc.blue = 0;
274 }
275 *r = xc.red;
276 *g = xc.green;
277 *b = xc.blue;
278 }
279
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000280protected:
281 Display* dpy;
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000282 Geometry* geometry;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000283 PixelFormat pf;
284 PixelBuffer* pb;
285 VNCServer* server;
286 Image* image;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000287 PollingManager* pollmgr;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000288 int oldButtonMask;
289 bool haveXtest;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000290 int maxButtons;
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000291 bool running;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000292};
293
294
295class FileTcpFilter : public TcpFilter
296{
297
298public:
299
300 FileTcpFilter(const char *fname)
301 : TcpFilter("-"), fileName(NULL), lastModTime(0)
302 {
303 if (fname != NULL)
304 fileName = strdup((char *)fname);
305 }
306
307 virtual ~FileTcpFilter()
308 {
309 if (fileName != NULL)
310 free(fileName);
311 }
312
313 virtual bool verifyConnection(Socket* s)
314 {
315 if (!reloadRules()) {
316 vlog.error("Could not read IP filtering rules: rejecting all clients");
317 filter.clear();
318 filter.push_back(parsePattern("-"));
319 return false;
320 }
321
322 return TcpFilter::verifyConnection(s);
323 }
324
325protected:
326
327 bool reloadRules()
328 {
329 if (fileName == NULL)
330 return true;
331
332 struct stat st;
333 if (stat(fileName, &st) != 0)
334 return false;
335
336 if (st.st_mtime != lastModTime) {
337 // Actually reload only if the file was modified
338 FILE *fp = fopen(fileName, "r");
339 if (fp == NULL)
340 return false;
341
342 // Remove all the rules from the parent class
343 filter.clear();
344
345 // Parse the file contents adding rules to the parent class
346 char buf[32];
347 while (readLine(buf, 32, fp)) {
348 if (buf[0] && strchr("+-?", buf[0])) {
349 filter.push_back(parsePattern(buf));
350 }
351 }
352
353 fclose(fp);
354 lastModTime = st.st_mtime;
355 }
356 return true;
357 }
358
359protected:
360
361 char *fileName;
362 time_t lastModTime;
363
364private:
365
366 //
367 // NOTE: we silently truncate long lines in this function.
368 //
369
370 bool readLine(char *buf, int bufSize, FILE *fp)
371 {
372 if (fp == NULL || buf == NULL || bufSize == 0)
373 return false;
374
375 if (fgets(buf, bufSize, fp) == NULL)
376 return false;
377
378 char *ptr = strchr(buf, '\n');
379 if (ptr != NULL) {
380 *ptr = '\0'; // remove newline at the end
381 } else {
382 if (!feof(fp)) {
383 int c;
384 do { // skip the rest of a long line
385 c = getc(fp);
386 } while (c != '\n' && c != EOF);
387 }
388 }
389 return true;
390 }
391
392};
393
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000394char* programName;
395
396static void usage()
397{
398 fprintf(stderr, "\nusage: %s [<parameters>]\n", programName);
399 fprintf(stderr,"\n"
400 "Parameters can be turned on with -<param> or off with -<param>=0\n"
401 "Parameters which take a value can be specified as "
402 "-<param> <value>\n"
403 "Other valid forms are <param>=<value> -<param>=<value> "
404 "--<param>=<value>\n"
405 "Parameter names are case-insensitive. The parameters are:\n\n");
406 Configuration::listParams(79, 14);
407 exit(1);
408}
409
410int main(int argc, char** argv)
411{
412 initStdIOLoggers();
413 LogWriter::setLogParams("*:stderr:30");
414
415 programName = argv[0];
416 Display* dpy;
417
418 for (int i = 1; i < argc; i++) {
419 if (Configuration::setParam(argv[i]))
420 continue;
421
422 if (argv[i][0] == '-') {
423 if (i+1 < argc) {
424 if (Configuration::setParam(&argv[i][1], argv[i+1])) {
425 i++;
426 continue;
427 }
428 }
429 usage();
430 }
431
432 usage();
433 }
434
435 CharArray dpyStr(displayname.getData());
436 if (!(dpy = XOpenDisplay(dpyStr.buf[0] ? dpyStr.buf : 0))) {
437 fprintf(stderr,"%s: unable to open display \"%s\"\r\n",
438 programName, XDisplayName(displayname.getData()));
439 exit(1);
440 }
441
442 signal(SIGHUP, CleanupSignalHandler);
443 signal(SIGINT, CleanupSignalHandler);
444 signal(SIGTERM, CleanupSignalHandler);
445
446 try {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000447 TXWindow::init(dpy,"x0vncserver");
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000448 Geometry geo(DisplayWidth(dpy, DefaultScreen(dpy)),
449 DisplayHeight(dpy, DefaultScreen(dpy)));
450 XDesktop desktop(dpy, &geo);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000451 VNCServerST server("x0vncserver", &desktop);
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000452 QueryConnHandler qcHandler(dpy, &server);
453 server.setQueryConnectionHandler(&qcHandler);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000454
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000455 TcpListener listener((int)rfbport);
456 vlog.info("Listening on port %d", (int)rfbport);
457
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000458 FileTcpFilter fileTcpFilter(hostsFile.getData());
459 if (strlen(hostsFile.getData()) != 0)
460 listener.setFilter(&fileTcpFilter);
461
Constantin Kaplinskye179b5a2006-02-17 09:30:21 +0000462 PollingScheduler sched((int)pollingCycle, (int)maxProcessorUsage);
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +0000463
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +0000464 while (!caughtSignal) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000465 struct timeval tv;
466 fd_set rfds;
467 std::list<Socket*> sockets;
468 std::list<Socket*>::iterator i;
469
470 // Process any incoming X events
471 TXWindow::handleXEvents(dpy);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000472
473 FD_ZERO(&rfds);
474 FD_SET(listener.getFd(), &rfds);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000475 server.getSockets(&sockets);
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000476 int clients_connected = 0;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000477 for (i = sockets.begin(); i != sockets.end(); i++) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000478 if ((*i)->isShutdown()) {
479 server.removeSocket(*i);
480 delete (*i);
481 } else {
482 FD_SET((*i)->getFd(), &rfds);
483 clients_connected++;
484 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000485 }
486
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000487 if (clients_connected) {
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +0000488 int wait_ms = sched.millisRemaining();
489 if (wait_ms > 500) {
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000490 wait_ms = 500;
491 }
492 tv.tv_usec = wait_ms * 1000;
493#ifdef DEBUG
Constantin Kaplinsky6a34ca52006-02-17 11:29:17 +0000494 // fprintf(stderr, "[%d]\t", wait_ms);
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000495#endif
496 } else {
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +0000497 sched.reset();
498 tv.tv_usec = 100000;
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000499 }
500 tv.tv_sec = 0;
501
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000502 // Do the wait...
Constantin Kaplinskye179b5a2006-02-17 09:30:21 +0000503 sched.sleepStarted();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000504 int n = select(FD_SETSIZE, &rfds, 0, 0, &tv);
Constantin Kaplinskye179b5a2006-02-17 09:30:21 +0000505 sched.sleepFinished();
506
Constantin Kaplinsky659e9002005-09-09 08:32:02 +0000507 if (n < 0) {
508 if (errno == EINTR) {
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +0000509 vlog.debug("Interrupted select() system call");
Constantin Kaplinsky659e9002005-09-09 08:32:02 +0000510 continue;
511 } else {
512 throw rdr::SystemException("select", errno);
513 }
514 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000515
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000516 // Accept new VNC connections
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000517 if (FD_ISSET(listener.getFd(), &rfds)) {
518 Socket* sock = listener.accept();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000519 if (sock) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000520 server.addSocket(sock);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000521 } else {
522 vlog.status("Client connection rejected");
523 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000524 }
525
Constantin Kaplinsky478f22c2006-04-19 06:13:06 +0000526 Timer::checkTimeouts();
527 server.checkTimeouts();
528
529 // Client list could have been changed.
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000530 server.getSockets(&sockets);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000531
532 // Nothing more to do if there are no client connections.
533 if (sockets.empty())
534 continue;
535
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000536 // Process events on existing VNC connections
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000537 for (i = sockets.begin(); i != sockets.end(); i++) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000538 if (FD_ISSET((*i)->getFd(), &rfds))
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000539 server.processSocketEvent(*i);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000540 }
541
Constantin Kaplinsky478f22c2006-04-19 06:13:06 +0000542 if (desktop.isRunning() && sched.goodTimeToPoll()) {
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +0000543 sched.newPass();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000544 desktop.poll();
545 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000546 }
547
548 } catch (rdr::Exception &e) {
549 vlog.error(e.str());
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +0000550 return 1;
551 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000552
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +0000553 vlog.info("Terminated");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000554 return 0;
555}