Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * Copyright (C) 2004-2006 Constantin Kaplinsky. All Rights Reserved. |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 3 | * |
| 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 Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 19 | |
| 20 | // FIXME: Check cases when screen width/height is not a multiply of 32. |
| 21 | // e.g. 800x600. |
| 22 | |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 23 | #include <strings.h> |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 24 | #include <sys/types.h> |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 25 | #include <sys/stat.h> |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 26 | #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 Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 35 | #include <tx/TXWindow.h> |
| 36 | |
Constantin Kaplinsky | 3f6ad6b | 2006-04-17 14:06:41 +0000 | [diff] [blame] | 37 | #include <vncconfig_unix/QueryConnectDialog.h> |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 38 | |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 39 | #include <signal.h> |
| 40 | #include <X11/X.h> |
| 41 | #include <X11/Xlib.h> |
| 42 | #include <X11/Xutil.h> |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 43 | #ifdef HAVE_XTEST |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 44 | #include <X11/extensions/XTest.h> |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 45 | #endif |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 46 | |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 47 | #include <x0vncserver/Image.h> |
| 48 | #include <x0vncserver/PollingManager.h> |
Constantin Kaplinsky | 0cbad33 | 2006-02-16 14:51:11 +0000 | [diff] [blame] | 49 | #include <x0vncserver/PollingScheduler.h> |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 50 | |
Constantin Kaplinsky | a686690 | 2006-03-02 12:03:30 +0000 | [diff] [blame] | 51 | // XXX Lynx/OS 2.3: protos for select(), bzero() |
| 52 | #ifdef Lynx |
| 53 | #include <sys/proto.h> |
| 54 | #endif |
| 55 | |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 56 | using namespace rfb; |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 57 | using namespace network; |
| 58 | |
| 59 | LogWriter vlog("main"); |
| 60 | |
Constantin Kaplinsky | 602f34d | 2005-09-14 16:11:41 +0000 | [diff] [blame] | 61 | IntParameter pollingCycle("PollingCycle", "Milliseconds per one polling " |
| 62 | "cycle; actual interval may be dynamically " |
Constantin Kaplinsky | ef7ac9b | 2006-02-10 12:26:54 +0000 | [diff] [blame] | 63 | "adjusted to satisfy MaxProcessorUsage setting", 30); |
Constantin Kaplinsky | 602f34d | 2005-09-14 16:11:41 +0000 | [diff] [blame] | 64 | IntParameter maxProcessorUsage("MaxProcessorUsage", "Maximum percentage of " |
| 65 | "CPU time to be consumed", 35); |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 66 | BoolParameter useShm("UseSHM", "Use MIT-SHM extension if available", true); |
| 67 | BoolParameter useOverlay("OverlayMode", "Use overlay mode under " |
| 68 | "IRIX or Solaris", true); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 69 | StringParameter displayname("display", "The X display", ""); |
| 70 | IntParameter rfbport("rfbport", "TCP port to listen for RFB protocol",5900); |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 71 | IntParameter queryConnectTimeout("QueryConnectTimeout", |
| 72 | "Number of seconds to show the Accept Connection dialog before " |
| 73 | "rejecting the connection", |
| 74 | 10); |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 75 | StringParameter hostsFile("HostsFile", "File with IP access control rules", ""); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 76 | |
| 77 | static 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 Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 86 | class QueryConnHandler : public VNCServerST::QueryConnectionHandler, |
| 87 | public QueryResultCallback { |
| 88 | public: |
| 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 | } |
| 122 | private: |
| 123 | Display* display; |
| 124 | VNCServerST* server; |
| 125 | QueryConnectDialog* queryConnectDialog; |
| 126 | network::Socket* queryConnectSock; |
| 127 | }; |
| 128 | |
| 129 | |
| 130 | class XDesktop : public SDesktop, public ColourMap |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 131 | { |
| 132 | public: |
| 133 | XDesktop(Display* dpy_) |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 134 | : dpy(dpy_), pb(0), server(0), image(0), pollmgr(0), |
| 135 | oldButtonMask(0), haveXtest(false), maxButtons(0) |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 136 | { |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 137 | #ifdef HAVE_XTEST |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 138 | 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 Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 148 | #endif |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 149 | vlog.info("XTest extension not present"); |
| 150 | vlog.info("unable to inject events or display while server is grabbed"); |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 151 | #ifdef HAVE_XTEST |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 152 | } |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 153 | #endif |
| 154 | |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 155 | } |
| 156 | virtual ~XDesktop() { |
| 157 | stop(); |
| 158 | } |
| 159 | |
| 160 | // -=- SDesktop interface |
| 161 | |
| 162 | virtual void start(VNCServer* vs) { |
| 163 | |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 164 | // 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 Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 170 | |
| 171 | int dpyWidth = DisplayWidth(dpy, DefaultScreen(dpy)); |
| 172 | int dpyHeight = DisplayHeight(dpy, DefaultScreen(dpy)); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 173 | |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 174 | ImageFactory factory((bool)useShm, (bool)useOverlay); |
| 175 | image = factory.newImage(dpy, dpyWidth, dpyHeight); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 176 | image->get(DefaultRootWindow(dpy)); |
| 177 | |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 178 | pollmgr = new PollingManager(dpy, image, &factory); |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 179 | pollmgr->setVNCServer(vs); |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 180 | |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 181 | pf.bpp = image->xim->bits_per_pixel; |
| 182 | pf.depth = image->xim->depth; |
| 183 | pf.bigEndian = (image->xim->byte_order == MSBFirst); |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 184 | 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 Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 191 | |
| 192 | pb = new FullFramePixelBuffer(pf, dpyWidth, dpyHeight, |
| 193 | (rdr::U8*)image->xim->data, this); |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 194 | server = vs; |
| 195 | server->setPixelBuffer(pb); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 198 | virtual void stop() { |
| 199 | delete pb; |
| 200 | delete pollmgr; |
| 201 | // FIXME: Delete images as well?! |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 204 | inline void poll() { |
| 205 | pollmgr->poll(); |
| 206 | } |
| 207 | |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 208 | virtual void pointerEvent(const Point& pos, int buttonMask) { |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 209 | pollmgr->setPointerPos(pos); |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 210 | #ifdef HAVE_XTEST |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 211 | if (!haveXtest) return; |
| 212 | XTestFakeMotionEvent(dpy, DefaultScreen(dpy), pos.x, pos.y, CurrentTime); |
| 213 | if (buttonMask != oldButtonMask) { |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 214 | for (int i = 0; i < maxButtons; i++) { |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 215 | if ((buttonMask ^ oldButtonMask) & (1<<i)) { |
| 216 | if (buttonMask & (1<<i)) { |
| 217 | XTestFakeButtonEvent(dpy, i+1, True, CurrentTime); |
| 218 | } else { |
| 219 | XTestFakeButtonEvent(dpy, i+1, False, CurrentTime); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | oldButtonMask = buttonMask; |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 225 | #endif |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | virtual void keyEvent(rdr::U32 key, bool down) { |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 229 | #ifdef HAVE_XTEST |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 230 | if (!haveXtest) return; |
| 231 | int keycode = XKeysymToKeycode(dpy, key); |
| 232 | if (keycode) |
| 233 | XTestFakeKeyEvent(dpy, keycode, down, CurrentTime); |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 234 | #endif |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | virtual void clientCutText(const char* str, int len) { |
| 238 | } |
| 239 | |
| 240 | virtual Point getFbSize() { |
| 241 | return Point(pb->width(), pb->height()); |
| 242 | } |
| 243 | |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 244 | // -=- ColourMap callbacks |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 245 | virtual void lookup(int index, int* r, int* g, int* b) { |
| 246 | XColor xc; |
| 247 | xc.pixel = index; |
| 248 | if (index < DisplayCells(dpy,DefaultScreen(dpy))) { |
| 249 | XQueryColor(dpy, DefaultColormap(dpy,DefaultScreen(dpy)), &xc); |
| 250 | } else { |
| 251 | xc.red = xc.green = xc.blue = 0; |
| 252 | } |
| 253 | *r = xc.red; |
| 254 | *g = xc.green; |
| 255 | *b = xc.blue; |
| 256 | } |
| 257 | |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 258 | protected: |
| 259 | Display* dpy; |
| 260 | PixelFormat pf; |
| 261 | PixelBuffer* pb; |
| 262 | VNCServer* server; |
| 263 | Image* image; |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 264 | PollingManager* pollmgr; |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 265 | int oldButtonMask; |
| 266 | bool haveXtest; |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 267 | int maxButtons; |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | |
| 271 | class FileTcpFilter : public TcpFilter |
| 272 | { |
| 273 | |
| 274 | public: |
| 275 | |
| 276 | FileTcpFilter(const char *fname) |
| 277 | : TcpFilter("-"), fileName(NULL), lastModTime(0) |
| 278 | { |
| 279 | if (fname != NULL) |
| 280 | fileName = strdup((char *)fname); |
| 281 | } |
| 282 | |
| 283 | virtual ~FileTcpFilter() |
| 284 | { |
| 285 | if (fileName != NULL) |
| 286 | free(fileName); |
| 287 | } |
| 288 | |
| 289 | virtual bool verifyConnection(Socket* s) |
| 290 | { |
| 291 | if (!reloadRules()) { |
| 292 | vlog.error("Could not read IP filtering rules: rejecting all clients"); |
| 293 | filter.clear(); |
| 294 | filter.push_back(parsePattern("-")); |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | return TcpFilter::verifyConnection(s); |
| 299 | } |
| 300 | |
| 301 | protected: |
| 302 | |
| 303 | bool reloadRules() |
| 304 | { |
| 305 | if (fileName == NULL) |
| 306 | return true; |
| 307 | |
| 308 | struct stat st; |
| 309 | if (stat(fileName, &st) != 0) |
| 310 | return false; |
| 311 | |
| 312 | if (st.st_mtime != lastModTime) { |
| 313 | // Actually reload only if the file was modified |
| 314 | FILE *fp = fopen(fileName, "r"); |
| 315 | if (fp == NULL) |
| 316 | return false; |
| 317 | |
| 318 | // Remove all the rules from the parent class |
| 319 | filter.clear(); |
| 320 | |
| 321 | // Parse the file contents adding rules to the parent class |
| 322 | char buf[32]; |
| 323 | while (readLine(buf, 32, fp)) { |
| 324 | if (buf[0] && strchr("+-?", buf[0])) { |
| 325 | filter.push_back(parsePattern(buf)); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | fclose(fp); |
| 330 | lastModTime = st.st_mtime; |
| 331 | } |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | protected: |
| 336 | |
| 337 | char *fileName; |
| 338 | time_t lastModTime; |
| 339 | |
| 340 | private: |
| 341 | |
| 342 | // |
| 343 | // NOTE: we silently truncate long lines in this function. |
| 344 | // |
| 345 | |
| 346 | bool readLine(char *buf, int bufSize, FILE *fp) |
| 347 | { |
| 348 | if (fp == NULL || buf == NULL || bufSize == 0) |
| 349 | return false; |
| 350 | |
| 351 | if (fgets(buf, bufSize, fp) == NULL) |
| 352 | return false; |
| 353 | |
| 354 | char *ptr = strchr(buf, '\n'); |
| 355 | if (ptr != NULL) { |
| 356 | *ptr = '\0'; // remove newline at the end |
| 357 | } else { |
| 358 | if (!feof(fp)) { |
| 359 | int c; |
| 360 | do { // skip the rest of a long line |
| 361 | c = getc(fp); |
| 362 | } while (c != '\n' && c != EOF); |
| 363 | } |
| 364 | } |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | }; |
| 369 | |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 370 | char* programName; |
| 371 | |
| 372 | static void usage() |
| 373 | { |
| 374 | fprintf(stderr, "\nusage: %s [<parameters>]\n", programName); |
| 375 | fprintf(stderr,"\n" |
| 376 | "Parameters can be turned on with -<param> or off with -<param>=0\n" |
| 377 | "Parameters which take a value can be specified as " |
| 378 | "-<param> <value>\n" |
| 379 | "Other valid forms are <param>=<value> -<param>=<value> " |
| 380 | "--<param>=<value>\n" |
| 381 | "Parameter names are case-insensitive. The parameters are:\n\n"); |
| 382 | Configuration::listParams(79, 14); |
| 383 | exit(1); |
| 384 | } |
| 385 | |
| 386 | int main(int argc, char** argv) |
| 387 | { |
| 388 | initStdIOLoggers(); |
| 389 | LogWriter::setLogParams("*:stderr:30"); |
| 390 | |
| 391 | programName = argv[0]; |
| 392 | Display* dpy; |
| 393 | |
| 394 | for (int i = 1; i < argc; i++) { |
| 395 | if (Configuration::setParam(argv[i])) |
| 396 | continue; |
| 397 | |
| 398 | if (argv[i][0] == '-') { |
| 399 | if (i+1 < argc) { |
| 400 | if (Configuration::setParam(&argv[i][1], argv[i+1])) { |
| 401 | i++; |
| 402 | continue; |
| 403 | } |
| 404 | } |
| 405 | usage(); |
| 406 | } |
| 407 | |
| 408 | usage(); |
| 409 | } |
| 410 | |
| 411 | CharArray dpyStr(displayname.getData()); |
| 412 | if (!(dpy = XOpenDisplay(dpyStr.buf[0] ? dpyStr.buf : 0))) { |
| 413 | fprintf(stderr,"%s: unable to open display \"%s\"\r\n", |
| 414 | programName, XDisplayName(displayname.getData())); |
| 415 | exit(1); |
| 416 | } |
| 417 | |
| 418 | signal(SIGHUP, CleanupSignalHandler); |
| 419 | signal(SIGINT, CleanupSignalHandler); |
| 420 | signal(SIGTERM, CleanupSignalHandler); |
| 421 | |
| 422 | try { |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 423 | TXWindow::init(dpy,"x0vncserver"); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 424 | XDesktop desktop(dpy); |
| 425 | VNCServerST server("x0vncserver", &desktop); |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 426 | QueryConnHandler qcHandler(dpy, &server); |
| 427 | server.setQueryConnectionHandler(&qcHandler); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 428 | |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 429 | TcpListener listener((int)rfbport); |
| 430 | vlog.info("Listening on port %d", (int)rfbport); |
| 431 | |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 432 | FileTcpFilter fileTcpFilter(hostsFile.getData()); |
| 433 | if (strlen(hostsFile.getData()) != 0) |
| 434 | listener.setFilter(&fileTcpFilter); |
| 435 | |
Constantin Kaplinsky | e179b5a | 2006-02-17 09:30:21 +0000 | [diff] [blame] | 436 | PollingScheduler sched((int)pollingCycle, (int)maxProcessorUsage); |
Constantin Kaplinsky | 602f34d | 2005-09-14 16:11:41 +0000 | [diff] [blame] | 437 | |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 438 | while (true) { |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 439 | struct timeval tv; |
| 440 | fd_set rfds; |
| 441 | std::list<Socket*> sockets; |
| 442 | std::list<Socket*>::iterator i; |
| 443 | |
| 444 | // Process any incoming X events |
| 445 | TXWindow::handleXEvents(dpy); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 446 | |
| 447 | FD_ZERO(&rfds); |
| 448 | FD_SET(listener.getFd(), &rfds); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 449 | server.getSockets(&sockets); |
Constantin Kaplinsky | 3f56fa7 | 2006-02-16 11:50:25 +0000 | [diff] [blame] | 450 | int clients_connected = 0; |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 451 | for (i = sockets.begin(); i != sockets.end(); i++) { |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 452 | if ((*i)->isShutdown()) { |
| 453 | server.removeSocket(*i); |
| 454 | delete (*i); |
| 455 | } else { |
| 456 | FD_SET((*i)->getFd(), &rfds); |
| 457 | clients_connected++; |
| 458 | } |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Constantin Kaplinsky | 3f56fa7 | 2006-02-16 11:50:25 +0000 | [diff] [blame] | 461 | if (clients_connected) { |
Constantin Kaplinsky | 0cbad33 | 2006-02-16 14:51:11 +0000 | [diff] [blame] | 462 | int wait_ms = sched.millisRemaining(); |
| 463 | if (wait_ms > 500) { |
Constantin Kaplinsky | 3f56fa7 | 2006-02-16 11:50:25 +0000 | [diff] [blame] | 464 | wait_ms = 500; |
| 465 | } |
| 466 | tv.tv_usec = wait_ms * 1000; |
| 467 | #ifdef DEBUG |
Constantin Kaplinsky | 6a34ca5 | 2006-02-17 11:29:17 +0000 | [diff] [blame] | 468 | // fprintf(stderr, "[%d]\t", wait_ms); |
Constantin Kaplinsky | 3f56fa7 | 2006-02-16 11:50:25 +0000 | [diff] [blame] | 469 | #endif |
| 470 | } else { |
Constantin Kaplinsky | 0cbad33 | 2006-02-16 14:51:11 +0000 | [diff] [blame] | 471 | sched.reset(); |
| 472 | tv.tv_usec = 100000; |
Constantin Kaplinsky | 3f56fa7 | 2006-02-16 11:50:25 +0000 | [diff] [blame] | 473 | } |
| 474 | tv.tv_sec = 0; |
| 475 | |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 476 | // Do the wait... |
Constantin Kaplinsky | e179b5a | 2006-02-17 09:30:21 +0000 | [diff] [blame] | 477 | sched.sleepStarted(); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 478 | int n = select(FD_SETSIZE, &rfds, 0, 0, &tv); |
Constantin Kaplinsky | e179b5a | 2006-02-17 09:30:21 +0000 | [diff] [blame] | 479 | sched.sleepFinished(); |
| 480 | |
Constantin Kaplinsky | 659e900 | 2005-09-09 08:32:02 +0000 | [diff] [blame] | 481 | if (n < 0) { |
| 482 | if (errno == EINTR) { |
| 483 | vlog.debug("interrupted select() system call"); |
| 484 | continue; |
| 485 | } else { |
| 486 | throw rdr::SystemException("select", errno); |
| 487 | } |
| 488 | } |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 489 | |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 490 | // Accept new VNC connections |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 491 | if (FD_ISSET(listener.getFd(), &rfds)) { |
| 492 | Socket* sock = listener.accept(); |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 493 | if (sock) { |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 494 | server.addSocket(sock); |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 495 | } else { |
| 496 | vlog.status("Client connection rejected"); |
| 497 | } |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | server.getSockets(&sockets); |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 501 | |
| 502 | // Nothing more to do if there are no client connections. |
| 503 | if (sockets.empty()) |
| 504 | continue; |
| 505 | |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 506 | // Process events on existing VNC connections |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 507 | for (i = sockets.begin(); i != sockets.end(); i++) { |
Constantin Kaplinsky | a1d2cef | 2006-04-17 08:46:22 +0000 | [diff] [blame] | 508 | if (FD_ISSET((*i)->getFd(), &rfds)) |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 509 | server.processSocketEvent(*i); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | server.checkTimeouts(); |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 513 | |
Constantin Kaplinsky | e47123e | 2006-02-16 16:44:50 +0000 | [diff] [blame] | 514 | if (sched.goodTimeToPoll()) { |
Constantin Kaplinsky | 0cbad33 | 2006-02-16 14:51:11 +0000 | [diff] [blame] | 515 | sched.newPass(); |
Constantin Kaplinsky | d31fd31 | 2005-09-08 19:29:02 +0000 | [diff] [blame] | 516 | desktop.poll(); |
| 517 | } |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | } catch (rdr::Exception &e) { |
| 521 | vlog.error(e.str()); |
| 522 | }; |
| 523 | |
| 524 | return 0; |
| 525 | } |