blob: 80483bf89f0d58aeff1977e5126979e325badf21 [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
Constantin Kaplinskyb9c73492006-05-23 06:33:03 +0000134//
135// XPixelBuffer is a modification of FullFramePixelBuffer that does
136// not always return buffer width in getStride().
137//
138
139class XPixelBuffer : public FullFramePixelBuffer
140{
141public:
142 XPixelBuffer(const PixelFormat& pf, int width, int height,
143 rdr::U8* data_, ColourMap* cm, int stride_) :
144 FullFramePixelBuffer(pf, width, height, data_, cm), stride(stride_)
145 {
146 }
147
148 virtual int getStride() const { return stride; }
149
150protected:
151 int stride;
152};
153
154
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000155class XDesktop : public SDesktop, public ColourMap
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000156{
157public:
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000158 XDesktop(Display* dpy_, Geometry *geometry_)
159 : dpy(dpy_), geometry(geometry_), pb(0), server(0), image(0), pollmgr(0),
Constantin Kaplinsky49476542006-04-18 09:22:53 +0000160 oldButtonMask(0), haveXtest(false), maxButtons(0), running(false)
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000161 {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000162#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000163 int xtestEventBase;
164 int xtestErrorBase;
165 int major, minor;
166
167 if (XTestQueryExtension(dpy, &xtestEventBase,
168 &xtestErrorBase, &major, &minor)) {
169 XTestGrabControl(dpy, True);
170 vlog.info("XTest extension present - version %d.%d",major,minor);
171 haveXtest = true;
172 } else {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000173#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000174 vlog.info("XTest extension not present");
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +0000175 vlog.info("Unable to inject events or display while server is grabbed");
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000176#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000177 }
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000178#endif
179
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000180 }
181 virtual ~XDesktop() {
182 stop();
183 }
184
185 // -=- SDesktop interface
186
187 virtual void start(VNCServer* vs) {
188
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000189 // Determine actual number of buttons of the X pointer device.
190 unsigned char btnMap[8];
191 int numButtons = XGetPointerMapping(dpy, btnMap, 8);
192 maxButtons = (numButtons > 8) ? 8 : numButtons;
193 vlog.info("Enabling %d button%s of X pointer device",
194 maxButtons, (maxButtons != 1) ? "s" : "");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000195
Constantin Kaplinsky99d65142006-05-22 05:45:20 +0000196 // Create an image for maintaining framebuffer data.
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000197 ImageFactory factory((bool)useShm, (bool)useOverlay);
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000198 image = factory.newImage(dpy, geometry->width(), geometry->height());
Constantin Kaplinsky068693e2006-05-22 09:07:19 +0000199 vlog.info("Allocated %s", image->classDesc());
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000200
Constantin Kaplinsky99d65142006-05-22 05:45:20 +0000201 // Create polling manager object. It will track screen changes and
202 // keep pixels of the `image' object up to date.
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000203 pollmgr = new PollingManager(dpy, image, &factory,
204 geometry->offsetLeft(),
205 geometry->offsetTop());
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000206 pollmgr->setVNCServer(vs);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000207
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000208 pf.bpp = image->xim->bits_per_pixel;
209 pf.depth = image->xim->depth;
210 pf.bigEndian = (image->xim->byte_order == MSBFirst);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000211 pf.trueColour = image->isTrueColor();
212 pf.redShift = ffs(image->xim->red_mask) - 1;
213 pf.greenShift = ffs(image->xim->green_mask) - 1;
214 pf.blueShift = ffs(image->xim->blue_mask) - 1;
215 pf.redMax = image->xim->red_mask >> pf.redShift;
216 pf.greenMax = image->xim->green_mask >> pf.greenShift;
217 pf.blueMax = image->xim->blue_mask >> pf.blueShift;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000218
Constantin Kaplinskyb9c73492006-05-23 06:33:03 +0000219 // Calculate the number of pixels in a row, with padding included.
220 int stride = image->xim->bytes_per_line * 8 / image->xim->bits_per_pixel;
221
222 // Provide pixel buffer to the server object.
223 pb = new XPixelBuffer(pf, geometry->width(), geometry->height(),
224 (rdr::U8*)image->xim->data, this, stride);
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000225 server = vs;
226 server->setPixelBuffer(pb);
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000227
228 running = true;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000229 }
230
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000231 virtual void stop() {
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000232 running = false;
233
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000234 delete pb;
235 delete pollmgr;
Constantin Kaplinsky93c3f892006-04-19 10:32:33 +0000236 delete image;
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000237
238 pb = 0;
239 pollmgr = 0;
Constantin Kaplinsky93c3f892006-04-19 10:32:33 +0000240 image = 0;
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000241 }
242
243 inline bool isRunning() {
244 return running;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000245 }
246
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000247 inline void poll() {
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000248 if (pollmgr)
249 pollmgr->poll();
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000250 }
251
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000252 virtual void pointerEvent(const Point& pos, int buttonMask) {
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000253 pollmgr->setPointerPos(pos);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000254#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000255 if (!haveXtest) return;
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000256 XTestFakeMotionEvent(dpy, DefaultScreen(dpy),
257 geometry->offsetLeft() + pos.x,
258 geometry->offsetTop() + pos.y,
259 CurrentTime);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000260 if (buttonMask != oldButtonMask) {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000261 for (int i = 0; i < maxButtons; i++) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000262 if ((buttonMask ^ oldButtonMask) & (1<<i)) {
263 if (buttonMask & (1<<i)) {
264 XTestFakeButtonEvent(dpy, i+1, True, CurrentTime);
265 } else {
266 XTestFakeButtonEvent(dpy, i+1, False, CurrentTime);
267 }
268 }
269 }
270 }
271 oldButtonMask = buttonMask;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000272#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000273 }
274
275 virtual void keyEvent(rdr::U32 key, bool down) {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000276#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000277 if (!haveXtest) return;
278 int keycode = XKeysymToKeycode(dpy, key);
279 if (keycode)
280 XTestFakeKeyEvent(dpy, keycode, down, CurrentTime);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000281#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000282 }
283
284 virtual void clientCutText(const char* str, int len) {
285 }
286
287 virtual Point getFbSize() {
288 return Point(pb->width(), pb->height());
289 }
290
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000291 // -=- ColourMap callbacks
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000292 virtual void lookup(int index, int* r, int* g, int* b) {
293 XColor xc;
294 xc.pixel = index;
295 if (index < DisplayCells(dpy,DefaultScreen(dpy))) {
296 XQueryColor(dpy, DefaultColormap(dpy,DefaultScreen(dpy)), &xc);
297 } else {
298 xc.red = xc.green = xc.blue = 0;
299 }
300 *r = xc.red;
301 *g = xc.green;
302 *b = xc.blue;
303 }
304
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000305protected:
306 Display* dpy;
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000307 Geometry* geometry;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000308 PixelFormat pf;
309 PixelBuffer* pb;
310 VNCServer* server;
311 Image* image;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000312 PollingManager* pollmgr;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000313 int oldButtonMask;
314 bool haveXtest;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000315 int maxButtons;
Constantin Kaplinskyb38ceeb2006-04-18 08:57:17 +0000316 bool running;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000317};
318
319
320class FileTcpFilter : public TcpFilter
321{
322
323public:
324
325 FileTcpFilter(const char *fname)
326 : TcpFilter("-"), fileName(NULL), lastModTime(0)
327 {
328 if (fname != NULL)
329 fileName = strdup((char *)fname);
330 }
331
332 virtual ~FileTcpFilter()
333 {
334 if (fileName != NULL)
335 free(fileName);
336 }
337
338 virtual bool verifyConnection(Socket* s)
339 {
340 if (!reloadRules()) {
341 vlog.error("Could not read IP filtering rules: rejecting all clients");
342 filter.clear();
343 filter.push_back(parsePattern("-"));
344 return false;
345 }
346
347 return TcpFilter::verifyConnection(s);
348 }
349
350protected:
351
352 bool reloadRules()
353 {
354 if (fileName == NULL)
355 return true;
356
357 struct stat st;
358 if (stat(fileName, &st) != 0)
359 return false;
360
361 if (st.st_mtime != lastModTime) {
362 // Actually reload only if the file was modified
363 FILE *fp = fopen(fileName, "r");
364 if (fp == NULL)
365 return false;
366
367 // Remove all the rules from the parent class
368 filter.clear();
369
370 // Parse the file contents adding rules to the parent class
371 char buf[32];
372 while (readLine(buf, 32, fp)) {
373 if (buf[0] && strchr("+-?", buf[0])) {
374 filter.push_back(parsePattern(buf));
375 }
376 }
377
378 fclose(fp);
379 lastModTime = st.st_mtime;
380 }
381 return true;
382 }
383
384protected:
385
386 char *fileName;
387 time_t lastModTime;
388
389private:
390
391 //
392 // NOTE: we silently truncate long lines in this function.
393 //
394
395 bool readLine(char *buf, int bufSize, FILE *fp)
396 {
397 if (fp == NULL || buf == NULL || bufSize == 0)
398 return false;
399
400 if (fgets(buf, bufSize, fp) == NULL)
401 return false;
402
403 char *ptr = strchr(buf, '\n');
404 if (ptr != NULL) {
405 *ptr = '\0'; // remove newline at the end
406 } else {
407 if (!feof(fp)) {
408 int c;
409 do { // skip the rest of a long line
410 c = getc(fp);
411 } while (c != '\n' && c != EOF);
412 }
413 }
414 return true;
415 }
416
417};
418
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000419char* programName;
420
421static void usage()
422{
423 fprintf(stderr, "\nusage: %s [<parameters>]\n", programName);
424 fprintf(stderr,"\n"
425 "Parameters can be turned on with -<param> or off with -<param>=0\n"
426 "Parameters which take a value can be specified as "
427 "-<param> <value>\n"
428 "Other valid forms are <param>=<value> -<param>=<value> "
429 "--<param>=<value>\n"
430 "Parameter names are case-insensitive. The parameters are:\n\n");
431 Configuration::listParams(79, 14);
432 exit(1);
433}
434
435int main(int argc, char** argv)
436{
437 initStdIOLoggers();
438 LogWriter::setLogParams("*:stderr:30");
439
440 programName = argv[0];
441 Display* dpy;
442
443 for (int i = 1; i < argc; i++) {
444 if (Configuration::setParam(argv[i]))
445 continue;
446
447 if (argv[i][0] == '-') {
448 if (i+1 < argc) {
449 if (Configuration::setParam(&argv[i][1], argv[i+1])) {
450 i++;
451 continue;
452 }
453 }
454 usage();
455 }
456
457 usage();
458 }
459
460 CharArray dpyStr(displayname.getData());
461 if (!(dpy = XOpenDisplay(dpyStr.buf[0] ? dpyStr.buf : 0))) {
462 fprintf(stderr,"%s: unable to open display \"%s\"\r\n",
463 programName, XDisplayName(displayname.getData()));
464 exit(1);
465 }
466
467 signal(SIGHUP, CleanupSignalHandler);
468 signal(SIGINT, CleanupSignalHandler);
469 signal(SIGTERM, CleanupSignalHandler);
470
471 try {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000472 TXWindow::init(dpy,"x0vncserver");
Constantin Kaplinsky44e99642006-05-20 12:58:38 +0000473 Geometry geo(DisplayWidth(dpy, DefaultScreen(dpy)),
474 DisplayHeight(dpy, DefaultScreen(dpy)));
475 XDesktop desktop(dpy, &geo);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000476 VNCServerST server("x0vncserver", &desktop);
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000477 QueryConnHandler qcHandler(dpy, &server);
478 server.setQueryConnectionHandler(&qcHandler);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000479
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000480 TcpListener listener((int)rfbport);
481 vlog.info("Listening on port %d", (int)rfbport);
482
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000483 FileTcpFilter fileTcpFilter(hostsFile.getData());
484 if (strlen(hostsFile.getData()) != 0)
485 listener.setFilter(&fileTcpFilter);
486
Constantin Kaplinskye179b5a2006-02-17 09:30:21 +0000487 PollingScheduler sched((int)pollingCycle, (int)maxProcessorUsage);
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +0000488
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +0000489 while (!caughtSignal) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000490 struct timeval tv;
491 fd_set rfds;
492 std::list<Socket*> sockets;
493 std::list<Socket*>::iterator i;
494
495 // Process any incoming X events
496 TXWindow::handleXEvents(dpy);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000497
498 FD_ZERO(&rfds);
499 FD_SET(listener.getFd(), &rfds);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000500 server.getSockets(&sockets);
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000501 int clients_connected = 0;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000502 for (i = sockets.begin(); i != sockets.end(); i++) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000503 if ((*i)->isShutdown()) {
504 server.removeSocket(*i);
505 delete (*i);
506 } else {
507 FD_SET((*i)->getFd(), &rfds);
508 clients_connected++;
509 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000510 }
511
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000512 if (clients_connected) {
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +0000513 int wait_ms = sched.millisRemaining();
514 if (wait_ms > 500) {
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000515 wait_ms = 500;
516 }
517 tv.tv_usec = wait_ms * 1000;
518#ifdef DEBUG
Constantin Kaplinsky6a34ca52006-02-17 11:29:17 +0000519 // fprintf(stderr, "[%d]\t", wait_ms);
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000520#endif
521 } else {
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +0000522 sched.reset();
523 tv.tv_usec = 100000;
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000524 }
525 tv.tv_sec = 0;
526
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000527 // Do the wait...
Constantin Kaplinskye179b5a2006-02-17 09:30:21 +0000528 sched.sleepStarted();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000529 int n = select(FD_SETSIZE, &rfds, 0, 0, &tv);
Constantin Kaplinskye179b5a2006-02-17 09:30:21 +0000530 sched.sleepFinished();
531
Constantin Kaplinsky659e9002005-09-09 08:32:02 +0000532 if (n < 0) {
533 if (errno == EINTR) {
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +0000534 vlog.debug("Interrupted select() system call");
Constantin Kaplinsky659e9002005-09-09 08:32:02 +0000535 continue;
536 } else {
537 throw rdr::SystemException("select", errno);
538 }
539 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000540
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000541 // Accept new VNC connections
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000542 if (FD_ISSET(listener.getFd(), &rfds)) {
543 Socket* sock = listener.accept();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000544 if (sock) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000545 server.addSocket(sock);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000546 } else {
547 vlog.status("Client connection rejected");
548 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000549 }
550
Constantin Kaplinsky478f22c2006-04-19 06:13:06 +0000551 Timer::checkTimeouts();
552 server.checkTimeouts();
553
554 // Client list could have been changed.
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000555 server.getSockets(&sockets);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000556
557 // Nothing more to do if there are no client connections.
558 if (sockets.empty())
559 continue;
560
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000561 // Process events on existing VNC connections
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000562 for (i = sockets.begin(); i != sockets.end(); i++) {
Constantin Kaplinskya1d2cef2006-04-17 08:46:22 +0000563 if (FD_ISSET((*i)->getFd(), &rfds))
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000564 server.processSocketEvent(*i);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000565 }
566
Constantin Kaplinsky478f22c2006-04-19 06:13:06 +0000567 if (desktop.isRunning() && sched.goodTimeToPoll()) {
Constantin Kaplinsky0cbad332006-02-16 14:51:11 +0000568 sched.newPass();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000569 desktop.poll();
570 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000571 }
572
573 } catch (rdr::Exception &e) {
574 vlog.error(e.str());
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +0000575 return 1;
576 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000577
Constantin Kaplinskybfc4e8c2006-05-22 10:08:56 +0000578 vlog.info("Terminated");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000579 return 0;
580}