Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | // |
| 19 | // VNC server configuration utility |
| 20 | // |
| 21 | |
| 22 | #include <string.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <sys/time.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | #include <signal.h> |
| 31 | #include <X11/X.h> |
| 32 | #include <X11/Xlib.h> |
| 33 | #include <X11/Xatom.h> |
| 34 | #include <X11/Xutil.h> |
| 35 | #include <X11/keysym.h> |
| 36 | #include "vncExt.h" |
| 37 | #include <rdr/Exception.h> |
| 38 | #include <rfb/Configuration.h> |
| 39 | #include <rfb/Logger_stdio.h> |
| 40 | #include <rfb/LogWriter.h> |
| 41 | #include <rfb/Timer.h> |
| 42 | #include "TXWindow.h" |
| 43 | #include "TXCheckbox.h" |
| 44 | #include "TXLabel.h" |
| 45 | #include "QueryConnectDialog.h" |
| 46 | |
| 47 | using namespace rfb; |
| 48 | |
| 49 | LogWriter vlog("vncconfig"); |
| 50 | |
| 51 | StringParameter displayname("display", "The X display", ""); |
| 52 | BoolParameter noWindow("nowin", "Don't display a window", 0); |
| 53 | BoolParameter iconic("iconic", "Start with window iconified", 0); |
| 54 | BoolParameter sendPrimary("SendPrimary", "Send the PRIMARY as well as the " |
| 55 | "CLIPBOARD selection", true); |
| 56 | IntParameter pollTime("poll", |
| 57 | "How often to poll for clipboard changes in ms", 0); |
| 58 | |
| 59 | inline const char* selectionName(Atom sel) { |
| 60 | if (sel == xaCLIPBOARD) return "CLIPBOARD"; |
| 61 | if (sel == XA_PRIMARY) return "PRIMARY"; |
| 62 | return "unknown"; |
| 63 | } |
| 64 | |
| 65 | #define ACCEPT_CUT_TEXT "AcceptCutText" |
| 66 | #define SEND_CUT_TEXT "SendCutText" |
| 67 | |
| 68 | char* programName = 0; |
| 69 | Display* dpy; |
| 70 | int vncExtEventBase, vncExtErrorBase; |
| 71 | |
| 72 | static bool getBoolParam(Display* dpy, const char* param) { |
| 73 | char* data; |
| 74 | int len; |
| 75 | if (XVncExtGetParam(dpy, param, &data, &len)) { |
| 76 | if (strcmp(data,"1") == 0) return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | class VncConfigWindow : public TXWindow, public TXEventHandler, |
| 82 | public TXDeleteWindowCallback, |
| 83 | public TXCheckboxCallback, |
| 84 | public rfb::Timer::Callback, |
| 85 | public QueryResultCallback { |
| 86 | public: |
| 87 | VncConfigWindow(Display* dpy) |
| 88 | : TXWindow(dpy, 300, 100), cutText(0), cutTextLen(0), |
| 89 | acceptClipboard(dpy, "Accept clipboard from viewers", this, false, this), |
| 90 | sendClipboard(dpy, "Send clipboard to viewers", this, false, this), |
| 91 | sendPrimaryCB(dpy, "Send primary selection to viewers", this,false,this), |
| 92 | pollTimer(this), |
| 93 | queryConnectDialog(0) |
| 94 | { |
| 95 | selection[0] = selection[1] = 0; |
| 96 | selectionLen[0] = selectionLen[1] = 0; |
| 97 | int y = yPad; |
| 98 | acceptClipboard.move(xPad, y); |
| 99 | acceptClipboard.checked(getBoolParam(dpy, ACCEPT_CUT_TEXT)); |
| 100 | y += acceptClipboard.height(); |
| 101 | sendClipboard.move(xPad, y); |
| 102 | sendClipboard.checked(getBoolParam(dpy, SEND_CUT_TEXT)); |
| 103 | y += sendClipboard.height(); |
| 104 | sendPrimaryCB.move(xPad, y); |
| 105 | sendPrimaryCB.checked(sendPrimary); |
| 106 | sendPrimaryCB.disabled(!sendClipboard.checked()); |
| 107 | y += sendPrimaryCB.height(); |
| 108 | setEventHandler(this); |
| 109 | toplevel("VNC config", this, 0, 0, 0, iconic); |
| 110 | XVncExtSelectInput(dpy, win(), |
| 111 | VncExtClientCutTextMask| |
| 112 | VncExtSelectionChangeMask| |
| 113 | VncExtQueryConnectMask); |
| 114 | XConvertSelection(dpy, XA_PRIMARY, XA_STRING, |
| 115 | XA_PRIMARY, win(), CurrentTime); |
| 116 | XConvertSelection(dpy, xaCLIPBOARD, XA_STRING, |
| 117 | xaCLIPBOARD, win(), CurrentTime); |
| 118 | if (pollTime != 0) |
| 119 | pollTimer.start(pollTime); |
| 120 | } |
| 121 | |
| 122 | // handleEvent(). If we get a ClientCutTextNotify event from Xvnc, set the |
| 123 | // primary and clipboard selections to the clientCutText. If we get a |
| 124 | // SelectionChangeNotify event from Xvnc, set the serverCutText to the value |
| 125 | // of the new selection. |
| 126 | |
| 127 | virtual void handleEvent(TXWindow* w, XEvent* ev) { |
| 128 | if (acceptClipboard.checked()) { |
| 129 | if (ev->type == vncExtEventBase + VncExtClientCutTextNotify) { |
| 130 | XVncExtClientCutTextEvent* cutEv = (XVncExtClientCutTextEvent*)ev; |
| 131 | if (cutText) |
| 132 | XFree(cutText); |
| 133 | cutText = 0; |
| 134 | if (XVncExtGetClientCutText(dpy, &cutText, &cutTextLen)) { |
| 135 | vlog.debug("Got client cut text: '%.*s%s'", |
| 136 | cutTextLen<9?cutTextLen:8, cutText, |
| 137 | cutTextLen<9?"":"..."); |
| 138 | XStoreBytes(dpy, cutText, cutTextLen); |
| 139 | ownSelection(XA_PRIMARY, cutEv->time); |
| 140 | ownSelection(xaCLIPBOARD, cutEv->time); |
| 141 | delete [] selection[0]; |
| 142 | delete [] selection[1]; |
| 143 | selection[0] = selection[1] = 0; |
| 144 | selectionLen[0] = selectionLen[1] = 0; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | if (sendClipboard.checked()) { |
| 149 | if (ev->type == vncExtEventBase + VncExtSelectionChangeNotify) { |
| 150 | vlog.debug("selection change event"); |
| 151 | XVncExtSelectionChangeEvent* selEv = (XVncExtSelectionChangeEvent*)ev; |
| 152 | if (selEv->selection == xaCLIPBOARD || |
| 153 | (selEv->selection == XA_PRIMARY && sendPrimaryCB.checked())) { |
| 154 | if (!selectionOwner(selEv->selection)) |
| 155 | XConvertSelection(dpy, selEv->selection, XA_STRING, |
| 156 | selEv->selection, win(), CurrentTime); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | if (ev->type == vncExtEventBase + VncExtQueryConnectNotify) { |
| 161 | vlog.debug("query connection event"); |
| 162 | if (queryConnectDialog) |
| 163 | delete queryConnectDialog; |
| 164 | queryConnectDialog = 0; |
| 165 | char* qcAddress; |
| 166 | char* qcUser; |
| 167 | int qcTimeout; |
| 168 | if (XVncExtGetQueryConnect(dpy, &qcAddress, &qcUser, |
| 169 | &qcTimeout, &queryConnectId)) { |
| 170 | if (qcTimeout) |
| 171 | queryConnectDialog = new QueryConnectDialog(dpy, qcAddress, |
| 172 | qcUser, qcTimeout, |
| 173 | this); |
| 174 | if (queryConnectDialog) |
| 175 | queryConnectDialog->map(); |
| 176 | XFree(qcAddress); |
| 177 | XFree(qcUser); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | // selectionRequest() is called when we are the selection owner and another X |
| 184 | // client has requested the selection. We simply put the server's cut text |
| 185 | // into the requested property. TXWindow will handle the rest. |
| 186 | bool selectionRequest(Window requestor, Atom selection, Atom property) |
| 187 | { |
| 188 | if (cutText) |
| 189 | XChangeProperty(dpy, requestor, property, XA_STRING, 8, |
| 190 | PropModeReplace, (unsigned char*)cutText, |
| 191 | cutTextLen); |
| 192 | return cutText; |
| 193 | } |
| 194 | |
| 195 | // selectionNotify() is called when we have requested the selection from the |
| 196 | // selection owner. |
| 197 | void selectionNotify(XSelectionEvent* ev, Atom type, int format, |
| 198 | int nitems, void* data) |
| 199 | { |
| 200 | if (ev->requestor != win() || ev->target != XA_STRING) |
| 201 | return; |
| 202 | |
| 203 | if (data && format == 8) { |
| 204 | int i = (ev->selection == XA_PRIMARY ? 0 : 1); |
| 205 | if (selectionLen[i] == nitems && memcmp(selection[i], data, nitems) == 0) |
| 206 | return; |
| 207 | delete [] selection[i]; |
| 208 | selection[i] = new char[nitems]; |
| 209 | memcpy(selection[i], data, nitems); |
| 210 | selectionLen[i] = nitems; |
| 211 | if (cutTextLen == nitems && memcmp(cutText, data, nitems) == 0) { |
| 212 | vlog.debug("ignoring duplicate cut text"); |
| 213 | return; |
| 214 | } |
| 215 | if (cutText) |
| 216 | XFree(cutText); |
| 217 | cutText = (char*)malloc(nitems); // assuming XFree() same as free() |
| 218 | memcpy(cutText, data, nitems); |
| 219 | cutTextLen = nitems; |
| 220 | vlog.debug("sending %s selection as server cut text: '%.*s%s'", |
| 221 | selectionName(ev->selection),cutTextLen<9?cutTextLen:8, |
| 222 | cutText, cutTextLen<9?"":"..."); |
| 223 | XVncExtSetServerCutText(dpy, cutText, cutTextLen); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // TXDeleteWindowCallback method |
| 228 | virtual void deleteWindow(TXWindow* w) { |
| 229 | exit(1); |
| 230 | } |
| 231 | |
| 232 | // TXCheckboxCallback method |
| 233 | virtual void checkboxSelect(TXCheckbox* checkbox) { |
| 234 | if (checkbox == &acceptClipboard) { |
| 235 | XVncExtSetParam(dpy, (acceptClipboard.checked() |
| 236 | ? ACCEPT_CUT_TEXT "=1" : ACCEPT_CUT_TEXT "=0")); |
| 237 | } else if (checkbox == &sendClipboard) { |
| 238 | XVncExtSetParam(dpy, (sendClipboard.checked() |
| 239 | ? SEND_CUT_TEXT "=1" : SEND_CUT_TEXT "=0")); |
| 240 | sendPrimaryCB.disabled(!sendClipboard.checked()); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // rfb::Timer::Callback interface |
| 245 | virtual bool handleTimeout(rfb::Timer* timer) { |
| 246 | if (sendPrimaryCB.checked() && !selectionOwner(XA_PRIMARY)) |
| 247 | XConvertSelection(dpy, XA_PRIMARY, XA_STRING, |
| 248 | XA_PRIMARY, win(), CurrentTime); |
| 249 | if (!selectionOwner(xaCLIPBOARD)) |
| 250 | XConvertSelection(dpy, xaCLIPBOARD, XA_STRING, |
| 251 | xaCLIPBOARD, win(), CurrentTime); |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | // QueryResultCallback interface |
| 256 | virtual void queryApproved() { |
| 257 | XVncExtApproveConnect(dpy, queryConnectId, 1); |
| 258 | } |
| 259 | virtual void queryRejected() { |
| 260 | XVncExtApproveConnect(dpy, queryConnectId, 0); |
| 261 | } |
| 262 | |
| 263 | private: |
| 264 | char* cutText; |
| 265 | int cutTextLen; |
| 266 | char* selection[2]; |
| 267 | int selectionLen[2]; |
| 268 | TXCheckbox acceptClipboard, sendClipboard, sendPrimaryCB; |
| 269 | rfb::Timer pollTimer; |
| 270 | |
| 271 | QueryConnectDialog* queryConnectDialog; |
| 272 | void* queryConnectId; |
| 273 | }; |
| 274 | |
| 275 | static void usage() |
| 276 | { |
| 277 | fprintf(stderr,"usage: %s [parameters]\n", |
| 278 | programName); |
| 279 | fprintf(stderr," %s [parameters] -connect <host>[:<port>]\n", |
| 280 | programName); |
| 281 | fprintf(stderr," %s [parameters] -disconnect\n", programName); |
| 282 | fprintf(stderr," %s [parameters] [-set] <Xvnc-param>=<value> ...\n", |
| 283 | programName); |
| 284 | fprintf(stderr," %s [parameters] -list\n", programName); |
| 285 | fprintf(stderr," %s [parameters] -get <param>\n", programName); |
| 286 | fprintf(stderr," %s [parameters] -desc <param>\n",programName); |
| 287 | fprintf(stderr,"\n" |
| 288 | "Parameters can be turned on with -<param> or off with -<param>=0\n" |
| 289 | "Parameters which take a value can be specified as " |
| 290 | "-<param> <value>\n" |
| 291 | "Other valid forms are <param>=<value> -<param>=<value> " |
| 292 | "--<param>=<value>\n" |
| 293 | "Parameter names are case-insensitive. The parameters are:\n\n"); |
| 294 | Configuration::listParams(79, 14); |
| 295 | exit(1); |
| 296 | } |
| 297 | |
| 298 | void removeArgs(int* argc, char** argv, int first, int n) |
| 299 | { |
| 300 | if (first + n > *argc) return; |
| 301 | for (int i = first + n; i < *argc; i++) |
| 302 | argv[i-n] = argv[i]; |
| 303 | *argc -= n; |
| 304 | } |
| 305 | |
| 306 | int main(int argc, char** argv) |
| 307 | { |
| 308 | programName = argv[0]; |
| 309 | rfb::initStdIOLoggers(); |
| 310 | rfb::LogWriter::setLogParams("*:stderr:30"); |
| 311 | |
| 312 | // Process vncconfig's own parameters first, then we process the |
| 313 | // other arguments when we have the X display. |
| 314 | int i; |
| 315 | for (i = 1; i < argc; i++) { |
| 316 | if (Configuration::setParam(argv[i])) |
| 317 | continue; |
| 318 | |
| 319 | if (argv[i][0] == '-' && i+1 < argc && |
| 320 | Configuration::setParam(&argv[i][1], argv[i+1])) { |
| 321 | i++; |
| 322 | continue; |
| 323 | } |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | CharArray displaynameStr(displayname.getData()); |
| 328 | if (!(dpy = XOpenDisplay(displaynameStr.buf))) { |
| 329 | fprintf(stderr,"%s: unable to open display \"%s\"\n", |
| 330 | programName, XDisplayName(displaynameStr.buf)); |
| 331 | exit(1); |
| 332 | } |
| 333 | |
| 334 | if (!XVncExtQueryExtension(dpy, &vncExtEventBase, &vncExtErrorBase)) { |
| 335 | fprintf(stderr,"No VNC extension on display %s\n", |
| 336 | XDisplayName(displaynameStr.buf)); |
| 337 | exit(1); |
| 338 | } |
| 339 | |
| 340 | if (i < argc) { |
| 341 | for (; i < argc; i++) { |
| 342 | if (strcmp(argv[i], "-connect") == 0) { |
| 343 | i++; |
| 344 | if (i >= argc) usage(); |
| 345 | if (!XVncExtConnect(dpy, argv[i])) { |
| 346 | fprintf(stderr,"connecting to %s failed\n",argv[i]); |
| 347 | } |
| 348 | } else if (strcmp(argv[i], "-disconnect") == 0) { |
| 349 | if (!XVncExtConnect(dpy, "")) { |
| 350 | fprintf(stderr,"disconnecting all clients failed\n"); |
| 351 | } |
| 352 | } else if (strcmp(argv[i], "-get") == 0) { |
| 353 | i++; |
| 354 | if (i >= argc) usage(); |
| 355 | char* data; |
| 356 | int len; |
| 357 | if (XVncExtGetParam(dpy, argv[i], &data, &len)) { |
| 358 | printf("%.*s\n",len,data); |
| 359 | } else { |
| 360 | fprintf(stderr,"getting param %s failed\n",argv[i]); |
| 361 | } |
| 362 | XFree(data); |
| 363 | } else if (strcmp(argv[i], "-desc") == 0) { |
| 364 | i++; |
| 365 | if (i >= argc) usage(); |
| 366 | char* desc = XVncExtGetParamDesc(dpy, argv[i]); |
| 367 | if (desc) { |
| 368 | printf("%s\n",desc); |
| 369 | } else { |
| 370 | fprintf(stderr,"getting description for param %s failed\n",argv[i]); |
| 371 | } |
| 372 | XFree(desc); |
| 373 | } else if (strcmp(argv[i], "-list") == 0) { |
| 374 | int nParams; |
| 375 | char** list = XVncExtListParams(dpy, &nParams); |
| 376 | for (int i = 0; i < nParams; i++) { |
| 377 | printf("%s\n",list[i]); |
| 378 | } |
| 379 | XVncExtFreeParamList(list); |
| 380 | } else if (strcmp(argv[i], "-set") == 0) { |
| 381 | i++; |
| 382 | if (i >= argc) usage(); |
| 383 | if (!XVncExtSetParam(dpy, argv[i])) { |
| 384 | fprintf(stderr,"setting param %s failed\n",argv[i]); |
| 385 | } |
| 386 | } else if (XVncExtSetParam(dpy, argv[i])) { |
| 387 | fprintf(stderr,"set parameter %s\n",argv[i]); |
| 388 | } else { |
| 389 | usage(); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | try { |
| 397 | TXWindow::init(dpy,"Vncconfig"); |
| 398 | |
| 399 | VncConfigWindow w(dpy); |
| 400 | if (!noWindow) w.map(); |
| 401 | |
| 402 | while (true) { |
| 403 | struct timeval tv; |
| 404 | struct timeval* tvp = 0; |
| 405 | |
| 406 | // Process any incoming X events |
| 407 | TXWindow::handleXEvents(dpy); |
| 408 | |
| 409 | // Process expired timers and get the time until the next one |
| 410 | int timeoutMs = Timer::checkTimeouts(); |
| 411 | if (timeoutMs) { |
| 412 | tv.tv_sec = timeoutMs / 1000; |
| 413 | tv.tv_usec = (timeoutMs % 1000) * 1000; |
| 414 | tvp = &tv; |
| 415 | } |
| 416 | |
| 417 | // If there are X requests pending then poll, don't wait! |
| 418 | if (XPending(dpy)) { |
| 419 | tv.tv_usec = tv.tv_sec = 0; |
| 420 | tvp = &tv; |
| 421 | } |
| 422 | |
| 423 | // Wait for X events, VNC traffic, or the next timer expiry |
| 424 | fd_set rfds; |
| 425 | FD_ZERO(&rfds); |
| 426 | FD_SET(ConnectionNumber(dpy), &rfds); |
| 427 | int n = select(FD_SETSIZE, &rfds, 0, 0, tvp); |
| 428 | if (n < 0) throw rdr::SystemException("select",errno); |
| 429 | } |
| 430 | |
| 431 | XCloseDisplay(dpy); |
| 432 | |
| 433 | } catch (rdr::Exception &e) { |
| 434 | vlog.error(e.str()); |
| 435 | } |
| 436 | |
| 437 | return 0; |
| 438 | } |