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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 <stdio.h>
  25. #include <stdlib.h>
  26. #include <strings.h>
  27. #include <unistd.h>
  28. #include <pwd.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #include <sys/utsname.h>
  33. #include <network/Socket.h>
  34. #include <rfb/Exception.h>
  35. #include <rfb/VNCServerST.h>
  36. #include <rfb/LogWriter.h>
  37. #include <rfb/Configuration.h>
  38. #include <rfb/ServerCore.h>
  39. #include "XserverDesktop.h"
  40. #include "vncBlockHandler.h"
  41. #include "vncExtInit.h"
  42. #include "vncHooks.h"
  43. #include "vncSelection.h"
  44. #include "XorgGlue.h"
  45. #include "Input.h"
  46. extern "C" {
  47. void vncSetGlueContext(int screenIndex);
  48. }
  49. using namespace rfb;
  50. using namespace network;
  51. static LogWriter vlog("XserverDesktop");
  52. BoolParameter rawKeyboard("RawKeyboard",
  53. "Send keyboard events straight through and "
  54. "avoid mapping them to the current keyboard "
  55. "layout", false);
  56. IntParameter queryConnectTimeout("QueryConnectTimeout",
  57. "Number of seconds to show the "
  58. "Accept Connection dialog before "
  59. "rejecting the connection",
  60. 10);
  61. XserverDesktop::XserverDesktop(int screenIndex_,
  62. std::list<network::SocketListener*> listeners_,
  63. const char* name, const rfb::PixelFormat &pf,
  64. int width, int height,
  65. void* fbptr, int stride)
  66. : screenIndex(screenIndex_),
  67. server(0), listeners(listeners_),
  68. directFbptr(true),
  69. queryConnectId(0), queryConnectTimer(this)
  70. {
  71. format = pf;
  72. server = new VNCServerST(name, this);
  73. setFramebuffer(width, height, fbptr, stride);
  74. server->setQueryConnectionHandler(this);
  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. rfb::VNCServerST::queryResult
  126. XserverDesktop::queryConnection(network::Socket* sock,
  127. const char* userName,
  128. char** reason)
  129. {
  130. int count;
  131. if (queryConnectTimer.isStarted()) {
  132. *reason = strDup("Another connection is currently being queried.");
  133. return rfb::VNCServerST::REJECT;
  134. }
  135. count = vncNotifyQueryConnect();
  136. if (count == 0) {
  137. *reason = strDup("Unable to query the local user to accept the connection.");
  138. return rfb::VNCServerST::REJECT;
  139. }
  140. queryConnectAddress.replaceBuf(sock->getPeerAddress());
  141. if (!userName)
  142. userName = "(anonymous)";
  143. queryConnectUsername.replaceBuf(strDup(userName));
  144. queryConnectId = (uint32_t)(intptr_t)sock;
  145. queryConnectSocket = sock;
  146. queryConnectTimer.start(queryConnectTimeout * 1000);
  147. return rfb::VNCServerST::PENDING;
  148. }
  149. void XserverDesktop::bell()
  150. {
  151. server->bell();
  152. }
  153. void XserverDesktop::setLEDState(unsigned int state)
  154. {
  155. server->setLEDState(state);
  156. }
  157. void XserverDesktop::serverCutText(const char* str, int len)
  158. {
  159. try {
  160. server->serverCutText(str, len);
  161. } catch (rdr::Exception& e) {
  162. vlog.error("XserverDesktop::serverCutText: %s",e.str());
  163. }
  164. }
  165. void XserverDesktop::setDesktopName(const char* name)
  166. {
  167. try {
  168. server->setName(name);
  169. } catch (rdr::Exception& e) {
  170. vlog.error("XserverDesktop::setDesktopName: %s",e.str());
  171. }
  172. }
  173. void XserverDesktop::setCursor(int width, int height, int hotX, int hotY,
  174. const unsigned char *rgbaData)
  175. {
  176. rdr::U8* cursorData;
  177. rdr::U8 *out;
  178. const unsigned char *in;
  179. cursorData = new rdr::U8[width * height * 4];
  180. // Un-premultiply alpha
  181. in = rgbaData;
  182. out = cursorData;
  183. for (int y = 0; y < height; y++) {
  184. for (int x = 0; x < width; x++) {
  185. rdr::U8 alpha;
  186. alpha = in[3];
  187. if (alpha == 0)
  188. alpha = 1; // Avoid division by zero
  189. *out++ = (unsigned)*in++ * 255/alpha;
  190. *out++ = (unsigned)*in++ * 255/alpha;
  191. *out++ = (unsigned)*in++ * 255/alpha;
  192. *out++ = *in++;
  193. }
  194. }
  195. try {
  196. server->setCursor(width, height, Point(hotX, hotY), cursorData);
  197. } catch (rdr::Exception& e) {
  198. vlog.error("XserverDesktop::setCursor: %s",e.str());
  199. }
  200. delete [] cursorData;
  201. }
  202. void XserverDesktop::add_changed(const rfb::Region &region)
  203. {
  204. try {
  205. server->add_changed(region);
  206. } catch (rdr::Exception& e) {
  207. vlog.error("XserverDesktop::add_changed: %s",e.str());
  208. }
  209. }
  210. void XserverDesktop::add_copied(const rfb::Region &dest, const rfb::Point &delta)
  211. {
  212. try {
  213. server->add_copied(dest, delta);
  214. } catch (rdr::Exception& e) {
  215. vlog.error("XserverDesktop::add_copied: %s",e.str());
  216. }
  217. }
  218. void XserverDesktop::handleSocketEvent(int fd, bool read, bool write)
  219. {
  220. try {
  221. if (read) {
  222. if (handleListenerEvent(fd, &listeners, server))
  223. return;
  224. }
  225. if (handleSocketEvent(fd, server, read, write))
  226. return;
  227. vlog.error("Cannot find file descriptor for socket event");
  228. } catch (rdr::Exception& e) {
  229. vlog.error("XserverDesktop::handleSocketEvent: %s",e.str());
  230. }
  231. }
  232. bool XserverDesktop::handleListenerEvent(int fd,
  233. std::list<SocketListener*>* sockets,
  234. SocketServer* sockserv)
  235. {
  236. std::list<SocketListener*>::iterator i;
  237. for (i = sockets->begin(); i != sockets->end(); i++) {
  238. if ((*i)->getFd() == fd)
  239. break;
  240. }
  241. if (i == sockets->end())
  242. return false;
  243. Socket* sock = (*i)->accept();
  244. sock->outStream().setBlocking(false);
  245. vlog.debug("new client, sock %d", sock->getFd());
  246. sockserv->addSocket(sock);
  247. vncSetNotifyFd(sock->getFd(), screenIndex, true, false);
  248. return true;
  249. }
  250. bool XserverDesktop::handleSocketEvent(int fd,
  251. SocketServer* sockserv,
  252. bool read, bool write)
  253. {
  254. std::list<Socket*> sockets;
  255. std::list<Socket*>::iterator i;
  256. sockserv->getSockets(&sockets);
  257. for (i = sockets.begin(); i != sockets.end(); i++) {
  258. if ((*i)->getFd() == fd)
  259. break;
  260. }
  261. if (i == sockets.end())
  262. return false;
  263. if (read)
  264. sockserv->processSocketReadEvent(*i);
  265. if (write)
  266. sockserv->processSocketWriteEvent(*i);
  267. return true;
  268. }
  269. void XserverDesktop::blockHandler(int* timeout)
  270. {
  271. // We don't have a good callback for when we can init input devices[1],
  272. // so we abuse the fact that this routine will be called first thing
  273. // once the dix is done initialising.
  274. // [1] Technically Xvnc has InitInput(), but libvnc.so has nothing.
  275. vncInitInputDevice();
  276. try {
  277. std::list<Socket*> sockets;
  278. std::list<Socket*>::iterator i;
  279. server->getSockets(&sockets);
  280. for (i = sockets.begin(); i != sockets.end(); i++) {
  281. int fd = (*i)->getFd();
  282. if ((*i)->isShutdown()) {
  283. vlog.debug("client gone, sock %d",fd);
  284. vncRemoveNotifyFd(fd);
  285. server->removeSocket(*i);
  286. vncClientGone(fd);
  287. delete (*i);
  288. } else {
  289. /* Update existing NotifyFD to listen for write (or not) */
  290. vncSetNotifyFd(fd, screenIndex, true, (*i)->outStream().bufferUsage() > 0);
  291. }
  292. }
  293. // We are responsible for propagating mouse movement between clients
  294. int cursorX, cursorY;
  295. vncGetPointerPos(&cursorX, &cursorY);
  296. cursorX -= vncGetScreenX(screenIndex);
  297. cursorY -= vncGetScreenY(screenIndex);
  298. if (oldCursorPos.x != cursorX || oldCursorPos.y != cursorY) {
  299. oldCursorPos.x = cursorX;
  300. oldCursorPos.y = cursorY;
  301. server->setCursorPos(oldCursorPos);
  302. }
  303. // Trigger timers and check when the next will expire
  304. int nextTimeout = server->checkTimeouts();
  305. if (nextTimeout > 0 && (*timeout == -1 || nextTimeout < *timeout))
  306. *timeout = nextTimeout;
  307. } catch (rdr::Exception& e) {
  308. vlog.error("XserverDesktop::blockHandler: %s",e.str());
  309. }
  310. }
  311. void XserverDesktop::addClient(Socket* sock, bool reverse)
  312. {
  313. vlog.debug("new client, sock %d reverse %d",sock->getFd(),reverse);
  314. sock->outStream().setBlocking(false);
  315. server->addSocket(sock, reverse);
  316. vncSetNotifyFd(sock->getFd(), screenIndex, true, false);
  317. }
  318. void XserverDesktop::disconnectClients()
  319. {
  320. vlog.debug("disconnecting all clients");
  321. return server->closeClients("Disconnection from server end");
  322. }
  323. void XserverDesktop::getQueryConnect(uint32_t* opaqueId,
  324. const char** address,
  325. const char** username,
  326. int *timeout)
  327. {
  328. *opaqueId = queryConnectId;
  329. if (!queryConnectTimer.isStarted()) {
  330. *address = "";
  331. *username = "";
  332. *timeout = 0;
  333. } else {
  334. *address = queryConnectAddress.buf;
  335. *username = queryConnectUsername.buf;
  336. *timeout = queryConnectTimeout;
  337. }
  338. }
  339. void XserverDesktop::approveConnection(uint32_t opaqueId, bool accept,
  340. const char* rejectMsg)
  341. {
  342. if (queryConnectId == opaqueId) {
  343. server->approveConnection(queryConnectSocket, accept, rejectMsg);
  344. queryConnectId = 0;
  345. queryConnectTimer.stop();
  346. }
  347. }
  348. ///////////////////////////////////////////////////////////////////////////
  349. //
  350. // SDesktop callbacks
  351. void XserverDesktop::pointerEvent(const Point& pos, int buttonMask)
  352. {
  353. vncPointerMove(pos.x + vncGetScreenX(screenIndex),
  354. pos.y + vncGetScreenY(screenIndex));
  355. vncPointerButtonAction(buttonMask);
  356. }
  357. void XserverDesktop::clientCutText(const char* str, int len)
  358. {
  359. vncClientCutText(str, len);
  360. }
  361. unsigned int XserverDesktop::setScreenLayout(int fb_width, int fb_height,
  362. const rfb::ScreenSet& layout)
  363. {
  364. unsigned int result;
  365. char buffer[2048];
  366. vlog.debug("Got request for framebuffer resize to %dx%d",
  367. fb_width, fb_height);
  368. layout.print(buffer, sizeof(buffer));
  369. vlog.debug("%s", buffer);
  370. vncSetGlueContext(screenIndex);
  371. result = ::setScreenLayout(fb_width, fb_height, layout, &outputIdMap);
  372. // Explicitly update the server state with the result as there
  373. // can be corner cases where we don't get feedback from the X core
  374. refreshScreenLayout();
  375. return result;
  376. }
  377. void XserverDesktop::grabRegion(const rfb::Region& region)
  378. {
  379. if (directFbptr)
  380. return;
  381. std::vector<rfb::Rect> rects;
  382. std::vector<rfb::Rect>::iterator i;
  383. region.get_rects(&rects);
  384. for (i = rects.begin(); i != rects.end(); i++) {
  385. rdr::U8 *buffer;
  386. int stride;
  387. buffer = getBufferRW(*i, &stride);
  388. vncGetScreenImage(screenIndex, i->tl.x, i->tl.y, i->width(), i->height(),
  389. (char*)buffer, stride * format.bpp/8);
  390. commitBufferRW(*i);
  391. }
  392. }
  393. void XserverDesktop::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down)
  394. {
  395. if (!rawKeyboard)
  396. keycode = 0;
  397. vncKeyboardEvent(keysym, keycode, down);
  398. }
  399. bool XserverDesktop::handleTimeout(Timer* t)
  400. {
  401. if (t == &queryConnectTimer) {
  402. server->approveConnection(queryConnectSocket, false,
  403. "The attempt to prompt the user to "
  404. "accept the connection failed");
  405. }
  406. return false;
  407. }