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.

XserverDesktop.cc 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2009-2017 Pierre Ossman for Cendio AB
  3. * Copyright 2014 Brian P. Hinz
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. //
  21. // XserverDesktop.cxx
  22. //
  23. #include <assert.h>
  24. #include <signal.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <strings.h>
  28. #include <unistd.h>
  29. #include <pwd.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <fcntl.h>
  33. #include <sys/utsname.h>
  34. #include <network/Socket.h>
  35. #include <rfb/Exception.h>
  36. #include <rfb/VNCServerST.h>
  37. #include <rfb/LogWriter.h>
  38. #include <rfb/Configuration.h>
  39. #include <rfb/ServerCore.h>
  40. #include "XserverDesktop.h"
  41. #include "vncBlockHandler.h"
  42. #include "vncExtInit.h"
  43. #include "vncHooks.h"
  44. #include "vncSelection.h"
  45. #include "XorgGlue.h"
  46. #include "Input.h"
  47. extern "C" {
  48. void vncSetGlueContext(int screenIndex);
  49. }
  50. using namespace rfb;
  51. using namespace network;
  52. static LogWriter vlog("XserverDesktop");
  53. BoolParameter rawKeyboard("RawKeyboard",
  54. "Send keyboard events straight through and "
  55. "avoid mapping them to the current keyboard "
  56. "layout", false);
  57. IntParameter queryConnectTimeout("QueryConnectTimeout",
  58. "Number of seconds to show the "
  59. "Accept Connection dialog before "
  60. "rejecting the connection",
  61. 10);
  62. XserverDesktop::XserverDesktop(int screenIndex_,
  63. std::list<network::SocketListener*> listeners_,
  64. const char* name, const rfb::PixelFormat &pf,
  65. int width, int height,
  66. void* fbptr, int stride)
  67. : screenIndex(screenIndex_),
  68. server(0), listeners(listeners_),
  69. directFbptr(true),
  70. queryConnectId(0), queryConnectTimer(this)
  71. {
  72. format = pf;
  73. server = new VNCServerST(name, this);
  74. setFramebuffer(width, height, fbptr, stride);
  75. for (std::list<SocketListener*>::iterator i = listeners.begin();
  76. i != listeners.end();
  77. i++) {
  78. vncSetNotifyFd((*i)->getFd(), screenIndex, true, false);
  79. }
  80. }
  81. XserverDesktop::~XserverDesktop()
  82. {
  83. while (!listeners.empty()) {
  84. vncRemoveNotifyFd(listeners.back()->getFd());
  85. delete listeners.back();
  86. listeners.pop_back();
  87. }
  88. if (!directFbptr)
  89. delete [] data;
  90. delete server;
  91. }
  92. void XserverDesktop::blockUpdates()
  93. {
  94. server->blockUpdates();
  95. }
  96. void XserverDesktop::unblockUpdates()
  97. {
  98. server->unblockUpdates();
  99. }
  100. void XserverDesktop::setFramebuffer(int w, int h, void* fbptr, int stride_)
  101. {
  102. ScreenSet layout;
  103. width_ = w;
  104. height_ = h;
  105. if (!directFbptr) {
  106. delete [] data;
  107. directFbptr = true;
  108. }
  109. if (!fbptr) {
  110. fbptr = new rdr::U8[w * h * (format.bpp/8)];
  111. stride_ = w;
  112. directFbptr = false;
  113. }
  114. data = (rdr::U8*)fbptr;
  115. stride = stride_;
  116. vncSetGlueContext(screenIndex);
  117. layout = ::computeScreenLayout(&outputIdMap);
  118. server->setPixelBuffer(this, layout);
  119. }
  120. void XserverDesktop::refreshScreenLayout()
  121. {
  122. vncSetGlueContext(screenIndex);
  123. server->setScreenLayout(::computeScreenLayout(&outputIdMap));
  124. }
  125. void XserverDesktop::start(rfb::VNCServer* vs)
  126. {
  127. // We already own the server object, and we always keep it in a
  128. // ready state
  129. assert(vs == server);
  130. }
  131. void XserverDesktop::stop()
  132. {
  133. }
  134. void XserverDesktop::queryConnection(network::Socket* sock,
  135. const char* userName)
  136. {
  137. int count;
  138. if (queryConnectTimer.isStarted()) {
  139. server->approveConnection(sock, false, "Another connection is currently being queried.");
  140. return;
  141. }
  142. count = vncNotifyQueryConnect();
  143. if (count == 0) {
  144. server->approveConnection(sock, false, "Unable to query the local user to accept the connection.");
  145. return;
  146. }
  147. queryConnectAddress.replaceBuf(sock->getPeerAddress());
  148. if (!userName)
  149. userName = "(anonymous)";
  150. queryConnectUsername.replaceBuf(strDup(userName));
  151. queryConnectId = (uint32_t)(intptr_t)sock;
  152. queryConnectSocket = sock;
  153. queryConnectTimer.start(queryConnectTimeout * 1000);
  154. }
  155. void XserverDesktop::bell()
  156. {
  157. server->bell();
  158. }
  159. void XserverDesktop::setLEDState(unsigned int state)
  160. {
  161. server->setLEDState(state);
  162. }
  163. void XserverDesktop::serverCutText(const char* str, int len)
  164. {
  165. try {
  166. server->serverCutText(str, len);
  167. } catch (rdr::Exception& e) {
  168. vlog.error("XserverDesktop::serverCutText: %s",e.str());
  169. }
  170. }
  171. void XserverDesktop::setDesktopName(const char* name)
  172. {
  173. try {
  174. server->setName(name);
  175. } catch (rdr::Exception& e) {
  176. vlog.error("XserverDesktop::setDesktopName: %s",e.str());
  177. }
  178. }
  179. void XserverDesktop::setCursor(int width, int height, int hotX, int hotY,
  180. const unsigned char *rgbaData)
  181. {
  182. rdr::U8* cursorData;
  183. rdr::U8 *out;
  184. const unsigned char *in;
  185. cursorData = new rdr::U8[width * height * 4];
  186. // Un-premultiply alpha
  187. in = rgbaData;
  188. out = cursorData;
  189. for (int y = 0; y < height; y++) {
  190. for (int x = 0; x < width; x++) {
  191. rdr::U8 alpha;
  192. alpha = in[3];
  193. if (alpha == 0)
  194. alpha = 1; // Avoid division by zero
  195. *out++ = (unsigned)*in++ * 255/alpha;
  196. *out++ = (unsigned)*in++ * 255/alpha;
  197. *out++ = (unsigned)*in++ * 255/alpha;
  198. *out++ = *in++;
  199. }
  200. }
  201. try {
  202. server->setCursor(width, height, Point(hotX, hotY), cursorData);
  203. } catch (rdr::Exception& e) {
  204. vlog.error("XserverDesktop::setCursor: %s",e.str());
  205. }
  206. delete [] cursorData;
  207. }
  208. void XserverDesktop::add_changed(const rfb::Region &region)
  209. {
  210. try {
  211. server->add_changed(region);
  212. } catch (rdr::Exception& e) {
  213. vlog.error("XserverDesktop::add_changed: %s",e.str());
  214. }
  215. }
  216. void XserverDesktop::add_copied(const rfb::Region &dest, const rfb::Point &delta)
  217. {
  218. try {
  219. server->add_copied(dest, delta);
  220. } catch (rdr::Exception& e) {
  221. vlog.error("XserverDesktop::add_copied: %s",e.str());
  222. }
  223. }
  224. void XserverDesktop::handleSocketEvent(int fd, bool read, bool write)
  225. {
  226. try {
  227. if (read) {
  228. if (handleListenerEvent(fd, &listeners, server))
  229. return;
  230. }
  231. if (handleSocketEvent(fd, server, read, write))
  232. return;
  233. vlog.error("Cannot find file descriptor for socket event");
  234. } catch (rdr::Exception& e) {
  235. vlog.error("XserverDesktop::handleSocketEvent: %s",e.str());
  236. }
  237. }
  238. bool XserverDesktop::handleListenerEvent(int fd,
  239. std::list<SocketListener*>* sockets,
  240. SocketServer* sockserv)
  241. {
  242. std::list<SocketListener*>::iterator i;
  243. for (i = sockets->begin(); i != sockets->end(); i++) {
  244. if ((*i)->getFd() == fd)
  245. break;
  246. }
  247. if (i == sockets->end())
  248. return false;
  249. Socket* sock = (*i)->accept();
  250. sock->outStream().setBlocking(false);
  251. vlog.debug("new client, sock %d", sock->getFd());
  252. sockserv->addSocket(sock);
  253. vncSetNotifyFd(sock->getFd(), screenIndex, true, false);
  254. return true;
  255. }
  256. bool XserverDesktop::handleSocketEvent(int fd,
  257. SocketServer* sockserv,
  258. bool read, bool write)
  259. {
  260. std::list<Socket*> sockets;
  261. std::list<Socket*>::iterator i;
  262. sockserv->getSockets(&sockets);
  263. for (i = sockets.begin(); i != sockets.end(); i++) {
  264. if ((*i)->getFd() == fd)
  265. break;
  266. }
  267. if (i == sockets.end())
  268. return false;
  269. if (read)
  270. sockserv->processSocketReadEvent(*i);
  271. if (write)
  272. sockserv->processSocketWriteEvent(*i);
  273. return true;
  274. }
  275. void XserverDesktop::blockHandler(int* timeout)
  276. {
  277. // We don't have a good callback for when we can init input devices[1],
  278. // so we abuse the fact that this routine will be called first thing
  279. // once the dix is done initialising.
  280. // [1] Technically Xvnc has InitInput(), but libvnc.so has nothing.
  281. vncInitInputDevice();
  282. try {
  283. std::list<Socket*> sockets;
  284. std::list<Socket*>::iterator i;
  285. server->getSockets(&sockets);
  286. for (i = sockets.begin(); i != sockets.end(); i++) {
  287. int fd = (*i)->getFd();
  288. if ((*i)->isShutdown()) {
  289. vlog.debug("client gone, sock %d",fd);
  290. vncRemoveNotifyFd(fd);
  291. server->removeSocket(*i);
  292. vncClientGone(fd);
  293. delete (*i);
  294. } else {
  295. /* Update existing NotifyFD to listen for write (or not) */
  296. vncSetNotifyFd(fd, screenIndex, true, (*i)->outStream().bufferUsage() > 0);
  297. }
  298. }
  299. // We are responsible for propagating mouse movement between clients
  300. int cursorX, cursorY;
  301. vncGetPointerPos(&cursorX, &cursorY);
  302. cursorX -= vncGetScreenX(screenIndex);
  303. cursorY -= vncGetScreenY(screenIndex);
  304. if (oldCursorPos.x != cursorX || oldCursorPos.y != cursorY) {
  305. oldCursorPos.x = cursorX;
  306. oldCursorPos.y = cursorY;
  307. server->setCursorPos(oldCursorPos);
  308. }
  309. // Trigger timers and check when the next will expire
  310. int nextTimeout = Timer::checkTimeouts();
  311. if (nextTimeout > 0 && (*timeout == -1 || nextTimeout < *timeout))
  312. *timeout = nextTimeout;
  313. } catch (rdr::Exception& e) {
  314. vlog.error("XserverDesktop::blockHandler: %s",e.str());
  315. }
  316. }
  317. void XserverDesktop::addClient(Socket* sock, bool reverse)
  318. {
  319. vlog.debug("new client, sock %d reverse %d",sock->getFd(),reverse);
  320. sock->outStream().setBlocking(false);
  321. server->addSocket(sock, reverse);
  322. vncSetNotifyFd(sock->getFd(), screenIndex, true, false);
  323. }
  324. void XserverDesktop::disconnectClients()
  325. {
  326. vlog.debug("disconnecting all clients");
  327. return server->closeClients("Disconnection from server end");
  328. }
  329. void XserverDesktop::getQueryConnect(uint32_t* opaqueId,
  330. const char** address,
  331. const char** username,
  332. int *timeout)
  333. {
  334. *opaqueId = queryConnectId;
  335. if (!queryConnectTimer.isStarted()) {
  336. *address = "";
  337. *username = "";
  338. *timeout = 0;
  339. } else {
  340. *address = queryConnectAddress.buf;
  341. *username = queryConnectUsername.buf;
  342. *timeout = queryConnectTimeout;
  343. }
  344. }
  345. void XserverDesktop::approveConnection(uint32_t opaqueId, bool accept,
  346. const char* rejectMsg)
  347. {
  348. if (queryConnectId == opaqueId) {
  349. server->approveConnection(queryConnectSocket, accept, rejectMsg);
  350. queryConnectId = 0;
  351. queryConnectTimer.stop();
  352. }
  353. }
  354. ///////////////////////////////////////////////////////////////////////////
  355. //
  356. // SDesktop callbacks
  357. void XserverDesktop::terminate()
  358. {
  359. kill(getpid(), SIGTERM);
  360. }
  361. void XserverDesktop::pointerEvent(const Point& pos, int buttonMask)
  362. {
  363. vncPointerMove(pos.x + vncGetScreenX(screenIndex),
  364. pos.y + vncGetScreenY(screenIndex));
  365. vncPointerButtonAction(buttonMask);
  366. }
  367. void XserverDesktop::clientCutText(const char* str, int len)
  368. {
  369. vncClientCutText(str, len);
  370. }
  371. unsigned int XserverDesktop::setScreenLayout(int fb_width, int fb_height,
  372. const rfb::ScreenSet& layout)
  373. {
  374. unsigned int result;
  375. char buffer[2048];
  376. vlog.debug("Got request for framebuffer resize to %dx%d",
  377. fb_width, fb_height);
  378. layout.print(buffer, sizeof(buffer));
  379. vlog.debug("%s", buffer);
  380. vncSetGlueContext(screenIndex);
  381. result = ::setScreenLayout(fb_width, fb_height, layout, &outputIdMap);
  382. // Explicitly update the server state with the result as there
  383. // can be corner cases where we don't get feedback from the X core
  384. refreshScreenLayout();
  385. return result;
  386. }
  387. void XserverDesktop::grabRegion(const rfb::Region& region)
  388. {
  389. if (directFbptr)
  390. return;
  391. std::vector<rfb::Rect> rects;
  392. std::vector<rfb::Rect>::iterator i;
  393. region.get_rects(&rects);
  394. for (i = rects.begin(); i != rects.end(); i++) {
  395. rdr::U8 *buffer;
  396. int stride;
  397. buffer = getBufferRW(*i, &stride);
  398. vncGetScreenImage(screenIndex, i->tl.x, i->tl.y, i->width(), i->height(),
  399. (char*)buffer, stride * format.bpp/8);
  400. commitBufferRW(*i);
  401. }
  402. }
  403. void XserverDesktop::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down)
  404. {
  405. if (!rawKeyboard)
  406. keycode = 0;
  407. vncKeyboardEvent(keysym, keycode, down);
  408. }
  409. bool XserverDesktop::handleTimeout(Timer* t)
  410. {
  411. if (t == &queryConnectTimer) {
  412. server->approveConnection(queryConnectSocket, false,
  413. "The attempt to prompt the user to "
  414. "accept the connection failed");
  415. }
  416. return false;
  417. }