You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vncconfig.cxx 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2012-2016 Pierre Ossman for Cendio AB
  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. */
  19. //
  20. // VNC server configuration utility
  21. //
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <sys/time.h>
  29. #include <sys/types.h>
  30. #include <unistd.h>
  31. #include <errno.h>
  32. #include <signal.h>
  33. #include <X11/X.h>
  34. #include <X11/Xlib.h>
  35. #include <X11/Xatom.h>
  36. #include <X11/Xutil.h>
  37. #include <X11/keysym.h>
  38. #include "vncExt.h"
  39. #include <rdr/Exception.h>
  40. #include <rfb/Configuration.h>
  41. #include <rfb/Logger_stdio.h>
  42. #include <rfb/LogWriter.h>
  43. #include "TXWindow.h"
  44. #include "TXCheckbox.h"
  45. #include "TXLabel.h"
  46. #include "QueryConnectDialog.h"
  47. using namespace rfb;
  48. static LogWriter vlog("vncconfig");
  49. StringParameter displayname("display", "The X display", "");
  50. BoolParameter noWindow("nowin", "Don't display a window", 0);
  51. BoolParameter iconic("iconic", "Start with window iconified", 0);
  52. #define ACCEPT_CUT_TEXT "AcceptCutText"
  53. #define SEND_CUT_TEXT "SendCutText"
  54. #define SET_PRIMARY "SetPrimary"
  55. #define SEND_PRIMARY "SendPrimary"
  56. char* programName = 0;
  57. Display* dpy;
  58. int vncExtEventBase, vncExtErrorBase;
  59. static bool getBoolParam(Display* dpy, const char* param) {
  60. char* data;
  61. int len;
  62. if (XVncExtGetParam(dpy, param, &data, &len)) {
  63. if (strcmp(data,"1") == 0) return true;
  64. }
  65. return false;
  66. }
  67. class VncConfigWindow : public TXWindow, public TXEventHandler,
  68. public TXDeleteWindowCallback,
  69. public TXCheckboxCallback,
  70. public QueryResultCallback {
  71. public:
  72. VncConfigWindow(Display* dpy)
  73. : TXWindow(dpy, 300, 100),
  74. acceptClipboard(dpy, "Accept clipboard from viewers", this, false, this),
  75. setPrimaryCB(dpy, "Also set primary selection", this, false, this),
  76. sendClipboard(dpy, "Send clipboard to viewers", this, false, this),
  77. sendPrimaryCB(dpy, "Send primary selection to viewers", this,false,this),
  78. queryConnectDialog(0)
  79. {
  80. int y = yPad;
  81. acceptClipboard.move(xPad, y);
  82. acceptClipboard.checked(getBoolParam(dpy, ACCEPT_CUT_TEXT));
  83. y += acceptClipboard.height();
  84. setPrimaryCB.move(xPad + 10, y);
  85. setPrimaryCB.checked(getBoolParam(dpy, SET_PRIMARY));
  86. setPrimaryCB.disabled(!acceptClipboard.checked());
  87. y += setPrimaryCB.height();
  88. sendClipboard.move(xPad, y);
  89. sendClipboard.checked(getBoolParam(dpy, SEND_CUT_TEXT));
  90. y += sendClipboard.height();
  91. sendPrimaryCB.move(xPad + 10, y);
  92. sendPrimaryCB.checked(getBoolParam(dpy, SEND_PRIMARY));
  93. sendPrimaryCB.disabled(!sendClipboard.checked());
  94. y += sendPrimaryCB.height();
  95. setEventHandler(this);
  96. toplevel("VNC config", this, 0, 0, 0, iconic);
  97. XVncExtSelectInput(dpy, win(), VncExtQueryConnectMask);
  98. }
  99. // handleEvent()
  100. virtual void handleEvent(TXWindow* /*w*/, XEvent* ev) {
  101. if (ev->type == vncExtEventBase + VncExtQueryConnectNotify) {
  102. vlog.debug("query connection event");
  103. if (queryConnectDialog)
  104. delete queryConnectDialog;
  105. queryConnectDialog = 0;
  106. char* qcAddress;
  107. char* qcUser;
  108. int qcTimeout;
  109. if (XVncExtGetQueryConnect(dpy, &qcAddress, &qcUser,
  110. &qcTimeout, &queryConnectId)) {
  111. if (qcTimeout)
  112. queryConnectDialog = new QueryConnectDialog(dpy, qcAddress,
  113. qcUser, qcTimeout,
  114. this);
  115. if (queryConnectDialog)
  116. queryConnectDialog->map();
  117. XFree(qcAddress);
  118. XFree(qcUser);
  119. }
  120. }
  121. }
  122. // TXDeleteWindowCallback method
  123. virtual void deleteWindow(TXWindow* /*w*/) {
  124. exit(1);
  125. }
  126. // TXCheckboxCallback method
  127. virtual void checkboxSelect(TXCheckbox* checkbox) {
  128. if (checkbox == &acceptClipboard) {
  129. XVncExtSetParam(dpy, (acceptClipboard.checked()
  130. ? ACCEPT_CUT_TEXT "=1" : ACCEPT_CUT_TEXT "=0"));
  131. setPrimaryCB.disabled(!acceptClipboard.checked());
  132. } else if (checkbox == &sendClipboard) {
  133. XVncExtSetParam(dpy, (sendClipboard.checked()
  134. ? SEND_CUT_TEXT "=1" : SEND_CUT_TEXT "=0"));
  135. sendPrimaryCB.disabled(!sendClipboard.checked());
  136. } else if (checkbox == &setPrimaryCB) {
  137. XVncExtSetParam(dpy, (setPrimaryCB.checked()
  138. ? SET_PRIMARY "=1" : SET_PRIMARY "=0"));
  139. } else if (checkbox == &sendPrimaryCB) {
  140. XVncExtSetParam(dpy, (sendPrimaryCB.checked()
  141. ? SEND_PRIMARY "=1" : SEND_PRIMARY "=0"));
  142. }
  143. }
  144. // QueryResultCallback interface
  145. virtual void queryApproved() {
  146. XVncExtApproveConnect(dpy, queryConnectId, 1);
  147. }
  148. virtual void queryRejected() {
  149. XVncExtApproveConnect(dpy, queryConnectId, 0);
  150. }
  151. private:
  152. TXCheckbox acceptClipboard, setPrimaryCB;
  153. TXCheckbox sendClipboard, sendPrimaryCB;
  154. QueryConnectDialog* queryConnectDialog;
  155. void* queryConnectId;
  156. };
  157. static void usage()
  158. {
  159. fprintf(stderr,"usage: %s [parameters]\n",
  160. programName);
  161. fprintf(stderr," %s [parameters] -connect <host>[:<port>]\n",
  162. programName);
  163. fprintf(stderr," %s [parameters] -disconnect\n", programName);
  164. fprintf(stderr," %s [parameters] [-set] <Xvnc-param>=<value> ...\n",
  165. programName);
  166. fprintf(stderr," %s [parameters] -list\n", programName);
  167. fprintf(stderr," %s [parameters] -get <param>\n", programName);
  168. fprintf(stderr," %s [parameters] -desc <param>\n",programName);
  169. fprintf(stderr,"\n"
  170. "Parameters can be turned on with -<param> or off with -<param>=0\n"
  171. "Parameters which take a value can be specified as "
  172. "-<param> <value>\n"
  173. "Other valid forms are <param>=<value> -<param>=<value> "
  174. "--<param>=<value>\n"
  175. "Parameter names are case-insensitive. The parameters are:\n\n");
  176. Configuration::listParams(79, 14);
  177. exit(1);
  178. }
  179. void removeArgs(int* argc, char** argv, int first, int n)
  180. {
  181. if (first + n > *argc) return;
  182. for (int i = first + n; i < *argc; i++)
  183. argv[i-n] = argv[i];
  184. *argc -= n;
  185. }
  186. int main(int argc, char** argv)
  187. {
  188. programName = argv[0];
  189. rfb::initStdIOLoggers();
  190. rfb::LogWriter::setLogParams("*:stderr:30");
  191. // Process vncconfig's own parameters first, then we process the
  192. // other arguments when we have the X display.
  193. int i;
  194. for (i = 1; i < argc; i++) {
  195. if (Configuration::setParam(argv[i]))
  196. continue;
  197. if (argv[i][0] == '-' && i+1 < argc &&
  198. Configuration::setParam(&argv[i][1], argv[i+1])) {
  199. i++;
  200. continue;
  201. }
  202. break;
  203. }
  204. CharArray displaynameStr(displayname.getData());
  205. if (!(dpy = XOpenDisplay(displaynameStr.buf))) {
  206. fprintf(stderr,"%s: unable to open display \"%s\"\n",
  207. programName, XDisplayName(displaynameStr.buf));
  208. exit(1);
  209. }
  210. if (!XVncExtQueryExtension(dpy, &vncExtEventBase, &vncExtErrorBase)) {
  211. fprintf(stderr,"No VNC extension on display %s\n",
  212. XDisplayName(displaynameStr.buf));
  213. exit(1);
  214. }
  215. if (i < argc) {
  216. for (; i < argc; i++) {
  217. if (strcmp(argv[i], "-connect") == 0) {
  218. i++;
  219. if (i >= argc) usage();
  220. if (!XVncExtConnect(dpy, argv[i])) {
  221. fprintf(stderr,"connecting to %s failed\n",argv[i]);
  222. }
  223. } else if (strcmp(argv[i], "-disconnect") == 0) {
  224. if (!XVncExtConnect(dpy, "")) {
  225. fprintf(stderr,"disconnecting all clients failed\n");
  226. }
  227. } else if (strcmp(argv[i], "-get") == 0) {
  228. i++;
  229. if (i >= argc) usage();
  230. char* data;
  231. int len;
  232. if (XVncExtGetParam(dpy, argv[i], &data, &len)) {
  233. printf("%.*s\n",len,data);
  234. } else {
  235. fprintf(stderr,"getting param %s failed\n",argv[i]);
  236. }
  237. XFree(data);
  238. } else if (strcmp(argv[i], "-desc") == 0) {
  239. i++;
  240. if (i >= argc) usage();
  241. char* desc = XVncExtGetParamDesc(dpy, argv[i]);
  242. if (desc) {
  243. printf("%s\n",desc);
  244. } else {
  245. fprintf(stderr,"getting description for param %s failed\n",argv[i]);
  246. }
  247. XFree(desc);
  248. } else if (strcmp(argv[i], "-list") == 0) {
  249. int nParams;
  250. char** list = XVncExtListParams(dpy, &nParams);
  251. for (int i = 0; i < nParams; i++) {
  252. printf("%s\n",list[i]);
  253. }
  254. XVncExtFreeParamList(list);
  255. } else if (strcmp(argv[i], "-set") == 0) {
  256. i++;
  257. if (i >= argc) usage();
  258. if (!XVncExtSetParam(dpy, argv[i])) {
  259. fprintf(stderr,"setting param %s failed\n",argv[i]);
  260. }
  261. } else if (XVncExtSetParam(dpy, argv[i])) {
  262. fprintf(stderr,"set parameter %s\n",argv[i]);
  263. } else {
  264. usage();
  265. }
  266. }
  267. return 0;
  268. }
  269. try {
  270. TXWindow::init(dpy,"Vncconfig");
  271. VncConfigWindow w(dpy);
  272. if (!noWindow) w.map();
  273. while (true) {
  274. struct timeval tv;
  275. struct timeval* tvp = 0;
  276. // Process any incoming X events
  277. TXWindow::handleXEvents(dpy);
  278. // Process expired timers and get the time until the next one
  279. int timeoutMs = Timer::checkTimeouts();
  280. if (timeoutMs) {
  281. tv.tv_sec = timeoutMs / 1000;
  282. tv.tv_usec = (timeoutMs % 1000) * 1000;
  283. tvp = &tv;
  284. }
  285. // If there are X requests pending then poll, don't wait!
  286. if (XPending(dpy)) {
  287. tv.tv_usec = tv.tv_sec = 0;
  288. tvp = &tv;
  289. }
  290. // Wait for X events, VNC traffic, or the next timer expiry
  291. fd_set rfds;
  292. FD_ZERO(&rfds);
  293. FD_SET(ConnectionNumber(dpy), &rfds);
  294. int n = select(FD_SETSIZE, &rfds, 0, 0, tvp);
  295. if (n < 0) throw rdr::SystemException("select",errno);
  296. }
  297. XCloseDisplay(dpy);
  298. } catch (rdr::Exception &e) {
  299. vlog.error("%s", e.str());
  300. }
  301. return 0;
  302. }