vnc_server.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /****************************************************************************
  2. * graphics/vnc/server/vnc_server.h
  3. *
  4. * Copyright (C) 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. #ifndef __GRAPHICS_VNC_SERVER_VNC_SERVER_H
  36. #define __GRAPHICS_VNC_SERVER_VNC_SERVER_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <stdint.h>
  42. #include <semaphore.h>
  43. #include <pthread.h>
  44. #include <queue.h>
  45. #include <nuttx/video/fb.h>
  46. #include <nuttx/video/rfb.h>
  47. #include <nuttx/video/vnc.h>
  48. #include <nuttx/nx/nxglib.h>
  49. #include <nuttx/nx/nx.h>
  50. #include <nuttx/net/net.h>
  51. /****************************************************************************
  52. * Pre-processor Definitions
  53. ****************************************************************************/
  54. /* Configuration */
  55. #ifndef CONFIG_NET_TCP_READAHEAD
  56. # error CONFIG_NET_TCP_READAHEAD must be set to use VNC
  57. #endif
  58. #ifndef CONFIG_NX_UPDATE
  59. # error CONFIG_NX_UPDATE must be set to use VNC
  60. #endif
  61. #if !defined(CONFIG_VNCSERVER_PROTO3p3) && !defined(CONFIG_VNCSERVER_PROTO3p8)
  62. # error No VNC protocol selected
  63. #endif
  64. #if defined(CONFIG_VNCSERVER_PROTO3p3) && defined(CONFIG_VNCSERVER_PROTO3p8)
  65. # error Too many VNC protocols selected
  66. #endif
  67. #ifndef CONFIG_VNCSERVER_NDISPLAYS
  68. # define CONFIG_VNCSERVER_NDISPLAYS 1
  69. #endif
  70. #if defined(CONFIG_VNCSERVER_COLORFMT_RGB8)
  71. # define RFB_COLORFMT FB_FMT_RGB8_332
  72. # define RFB_BITSPERPIXEL 8
  73. # define RFB_PIXELDEPTH 8
  74. # define RFB_TRUECOLOR 1
  75. # define RFB_RMAX 0x07
  76. # define RFB_GMAX 0x07
  77. # define RFB_BMAX 0x03
  78. # define RFB_RSHIFT 5
  79. # define RFB_GSHIFT 2
  80. # define RFB_BSHIFT 0
  81. #elif defined(CONFIG_VNCSERVER_COLORFMT_RGB16)
  82. # define RFB_COLORFMT FB_FMT_RGB16_565
  83. # define RFB_BITSPERPIXEL 16
  84. # define RFB_PIXELDEPTH 16
  85. # define RFB_TRUECOLOR 1
  86. # define RFB_RMAX 0x001f
  87. # define RFB_GMAX 0x003f
  88. # define RFB_BMAX 0x001f
  89. # define RFB_RSHIFT 11
  90. # define RFB_GSHIFT 5
  91. # define RFB_BSHIFT 0
  92. #elif defined(CONFIG_VNCSERVER_COLORFMT_RGB32)
  93. # define RFB_COLORFMT FB_FMT_RGB32
  94. # define RFB_BITSPERPIXEL 32
  95. # define RFB_PIXELDEPTH 24
  96. # define RFB_TRUECOLOR 1
  97. # define RFB_RMAX 0x000000ff
  98. # define RFB_GMAX 0x000000ff
  99. # define RFB_BMAX 0x000000ff
  100. # define RFB_RSHIFT 16
  101. # define RFB_GSHIFT 8
  102. # define RFB_BSHIFT 0
  103. #else
  104. # error Unspecified/unsupported color format
  105. #endif
  106. #ifndef CONFIG_VNCSERVER_SCREENWIDTH
  107. # define CONFIG_VNCSERVER_SCREENWIDTH 320
  108. #endif
  109. #ifndef CONFIG_VNCSERVER_SCREENHEIGHT
  110. # define CONFIG_VNCSERVER_SCREENHEIGHT 240
  111. #endif
  112. #ifndef CONFIG_VNCSERVER_NAME
  113. # define CONFIG_VNCSERVER_NAME "NuttX"
  114. #endif
  115. #ifndef CONFIG_VNCSERVER_PRIO
  116. # define CONFIG_VNCSERVER_PRIO 100
  117. #endif
  118. #ifndef CONFIG_VNCSERVER_STACKSIZE
  119. # define CONFIG_VNCSERVER_STACKSIZE 2048
  120. #endif
  121. #ifndef CONFIG_VNCSERVER_UPDATER_PRIO
  122. # define CONFIG_VNCSERVER_UPDATER_PRIO 100
  123. #endif
  124. #ifndef CONFIG_VNCSERVER_UPDATER_STACKSIZE
  125. # define CONFIG_VNCSERVER_UPDATER_STACKSIZE 2048
  126. #endif
  127. #ifndef CONFIG_VNCSERVER_INBUFFER_SIZE
  128. # define CONFIG_VNCSERVER_INBUFFER_SIZE 80
  129. #endif
  130. #ifndef CONFIG_VNCSERVER_NUPDATES
  131. # define CONFIG_VNCSERVER_NUPDATES 48
  132. #endif
  133. #ifndef CONFIG_VNCSERVER_UPDATE_BUFSIZE
  134. # define CONFIG_VNCSERVER_UPDATE_BUFSIZE 4096
  135. #endif
  136. #define VNCSERVER_UPDATE_BUFSIZE \
  137. (CONFIG_VNCSERVER_UPDATE_BUFSIZE + SIZEOF_RFB_FRAMEBUFFERUPDATE_S(0))
  138. /* Local framebuffer characteristics in bytes */
  139. #define RFB_BYTESPERPIXEL ((RFB_BITSPERPIXEL + 7) >> 3)
  140. #define RFB_STRIDE (RFB_BYTESPERPIXEL * CONFIG_VNCSERVER_SCREENWIDTH)
  141. #define RFB_SIZE (RFB_STRIDE * CONFIG_VNCSERVER_SCREENHEIGHT)
  142. /* RFB Port Number */
  143. #define RFB_PORT_BASE 5900
  144. #define RFB_MAX_DISPLAYS CONFIG_VNCSERVER_NDISPLAYS
  145. #define RFB_DISPLAY_PORT(d) (RFB_PORT_BASE + (d))
  146. /* Miscellaneous */
  147. #ifndef MIN
  148. # define MIN(a,b) (((a) < (b)) ? (a) : (b))
  149. #endif
  150. #ifndef MAX
  151. # define MAX(a,b) (((a) > (b)) ? (a) : (b))
  152. #endif
  153. /* Debug */
  154. #ifdef CONFIG_VNCSERVER_UPDATE_DEBUG
  155. # ifdef CONFIG_CPP_HAVE_VARARGS
  156. # define upderr(format, ...) _err(format, ##__VA_ARGS__)
  157. # define updinfo(format, ...) _info(format, ##__VA_ARGS__)
  158. # define updinfo(format, ...) _info(format, ##__VA_ARGS__)
  159. # else
  160. # define upderr _err
  161. # define updwarn _warn
  162. # define updinfo _info
  163. # endif
  164. #else
  165. # ifdef CONFIG_CPP_HAVE_VARARGS
  166. # define upderr(x...)
  167. # define updwarn(x...)
  168. # define updinfo(x...)
  169. # else
  170. # define upderr (void)
  171. # define updwarn (void)
  172. # define updinfo (void)
  173. # endif
  174. #endif
  175. /****************************************************************************
  176. * Public Types
  177. ****************************************************************************/
  178. /* This enumeration indicates the state of the VNC server */
  179. enum vnc_server_e
  180. {
  181. VNCSERVER_UNINITIALIZED = 0, /* Initial state */
  182. VNCSERVER_INITIALIZED, /* State structured initialized, but not connected */
  183. VNCSERVER_CONNECTED, /* Connect to a client, but not yet configured */
  184. VNCSERVER_CONFIGURED, /* Configured and ready to transfer graphics */
  185. VNCSERVER_RUNNING, /* Running and activly transferring graphics */
  186. VNCSERVER_STOPPING, /* The updater has been asked to stop */
  187. VNCSERVER_STOPPED /* The updater has stopped */
  188. };
  189. /* This structure is used to queue FrameBufferUpdate event. It includes a
  190. * pointer to support singly linked list.
  191. */
  192. struct vnc_fbupdate_s
  193. {
  194. FAR struct vnc_fbupdate_s *flink;
  195. bool whupd; /* True: whole screen update */
  196. struct nxgl_rect_s rect; /* The enqueued update rectangle */
  197. };
  198. struct vnc_session_s
  199. {
  200. /* Connection data */
  201. struct socket listen; /* Listen socket */
  202. struct socket connect; /* Connected socket */
  203. volatile uint8_t state; /* See enum vnc_server_e */
  204. volatile uint8_t nwhupd; /* Number of whole screen updates queued */
  205. volatile bool change; /* True: Frambebuffer data change since last whole screen update */
  206. /* Display geometry and color characteristics */
  207. uint8_t display; /* Display number (for debug) */
  208. volatile uint8_t colorfmt; /* Remote color format (See include/nuttx/fb.h) */
  209. volatile uint8_t bpp; /* Remote bits per pixel */
  210. volatile bool bigendian; /* True: Remote expect data in big-endian format */
  211. volatile bool rre; /* True: Remote supports RRE encoding */
  212. FAR uint8_t *fb; /* Allocated local frame buffer */
  213. /* VNC client input support */
  214. vnc_kbdout_t kbdout; /* Callout when keyboard input is received */
  215. vnc_mouseout_t mouseout; /* Callout when keyboard input is received */
  216. FAR void *arg; /* Argument that accompanies the callouts */
  217. /* Updater information */
  218. pthread_t updater; /* Updater thread ID */
  219. /* Update list information */
  220. struct vnc_fbupdate_s updpool[CONFIG_VNCSERVER_NUPDATES];
  221. sq_queue_t updfree;
  222. sq_queue_t updqueue;
  223. sem_t freesem;
  224. sem_t queuesem;
  225. /* I/O buffers for misc network send/receive */
  226. uint8_t inbuf[CONFIG_VNCSERVER_INBUFFER_SIZE];
  227. uint8_t outbuf[VNCSERVER_UPDATE_BUFSIZE];
  228. };
  229. /* This structure is used to communicate start-up status between the server
  230. * the framebuffer driver.
  231. */
  232. struct fb_startup_s
  233. {
  234. sem_t fbinit; /* Wait for session creation */
  235. sem_t fbconnect; /* Wait for client connection */
  236. int16_t result; /* OK: successfully initialized */
  237. };
  238. /* The size of the color type in the local framebuffer */
  239. #if defined(CONFIG_VNCSERVER_COLORFMT_RGB8)
  240. typedef uint8_t lfb_color_t;
  241. #elif defined(CONFIG_VNCSERVER_COLORFMT_RGB16)
  242. typedef uint16_t lfb_color_t;
  243. #elif defined(CONFIG_VNCSERVER_COLORFMT_RGB32)
  244. typedef uint32_t lfb_color_t;
  245. #else
  246. # error Unspecified/unsupported color format
  247. #endif
  248. /* Color conversion function pointer types */
  249. typedef CODE uint8_t (*vnc_convert8_t) (lfb_color_t rgb);
  250. typedef CODE uint16_t (*vnc_convert16_t)(lfb_color_t rgb);
  251. typedef CODE uint32_t (*vnc_convert32_t)(lfb_color_t rgb);
  252. /****************************************************************************
  253. * Public Data
  254. ****************************************************************************/
  255. #ifdef __cplusplus
  256. #define EXTERN extern "C"
  257. extern "C"
  258. {
  259. #else
  260. #define EXTERN extern
  261. #endif
  262. /* Given a display number as an index, the following array can be used to
  263. * look-up the session structure for that display.
  264. */
  265. EXTERN FAR struct vnc_session_s *g_vnc_sessions[RFB_MAX_DISPLAYS];
  266. /* Used to synchronize the server thread with the framebuffer driver. */
  267. EXTERN struct fb_startup_s g_fbstartup[RFB_MAX_DISPLAYS];
  268. /****************************************************************************
  269. * Public Function Prototypes
  270. ****************************************************************************/
  271. /****************************************************************************
  272. * Name: vnc_server
  273. *
  274. * Description:
  275. * The VNC server daemon. This daemon is implemented as a kernel thread.
  276. *
  277. * Input Parameters:
  278. * Standard kernel thread arguments (all ignored)
  279. *
  280. * Returned Value:
  281. * This function does not return.
  282. *
  283. ****************************************************************************/
  284. int vnc_server(int argc, FAR char *argv[]);
  285. /****************************************************************************
  286. * Name: vnc_negotiate
  287. *
  288. * Description:
  289. * Perform the VNC initialization sequence after the client has successfully
  290. * connected to the server. Negotiate security, framebuffer and color
  291. * properties.
  292. *
  293. * Input Parameters:
  294. * session - An instance of the session structure.
  295. *
  296. * Returned Value:
  297. * Returns zero (OK) on success; a negated errno value on failure.
  298. *
  299. ****************************************************************************/
  300. int vnc_negotiate(FAR struct vnc_session_s *session);
  301. /****************************************************************************
  302. * Name: vnc_client_pixelformat
  303. *
  304. * Description:
  305. * A Client-to-Sever SetPixelFormat message has been received. We need to
  306. * immediately switch the output color format that we generate.
  307. *
  308. * Input Parameters:
  309. * session - An instance of the session structure.
  310. * pixelfmt - The pixel from from the received SetPixelFormat message
  311. *
  312. * Returned Value:
  313. * Returns zero (OK) on success; a negated errno value on failure.
  314. *
  315. ****************************************************************************/
  316. int vnc_client_pixelformat(FAR struct vnc_session_s *session,
  317. FAR struct rfb_pixelfmt_s *pixelfmt);
  318. /****************************************************************************
  319. * Name: vnc_client_encodings
  320. *
  321. * Description:
  322. * Pick out any mutually supported encodings from the Client-to-Server
  323. * SetEncodings message
  324. *
  325. * Input Parameters:
  326. * session - An instance of the session structure.
  327. * encodings - The received SetEncodings message
  328. *
  329. * Returned Value:
  330. * At present, always returns OK
  331. *
  332. ****************************************************************************/
  333. int vnc_client_encodings(FAR struct vnc_session_s *session,
  334. FAR struct rfb_setencodings_s *encodings);
  335. /****************************************************************************
  336. * Name: vnc_start_updater
  337. *
  338. * Description:
  339. * Start the updater thread
  340. *
  341. * Input Parameters:
  342. * session - An instance of the session structure.
  343. *
  344. * Returned Value:
  345. * Zero (OK) is returned on success; a negated errno value is returned on
  346. * any failure.
  347. *
  348. ****************************************************************************/
  349. int vnc_start_updater(FAR struct vnc_session_s *session);
  350. /****************************************************************************
  351. * Name: vnc_stop_updater
  352. *
  353. * Description:
  354. * Stop the updater thread
  355. *
  356. * Input Parameters:
  357. * session - An instance of the session structure.
  358. *
  359. * Returned Value:
  360. * Zero (OK) is returned on success; a negated errno value is returned on
  361. * any failure.
  362. *
  363. ****************************************************************************/
  364. int vnc_stop_updater(FAR struct vnc_session_s *session);
  365. /****************************************************************************
  366. * Name: vnc_update_rectangle
  367. *
  368. * Description:
  369. * Queue an update of the specified rectangular region on the display.
  370. *
  371. * Input Parameters:
  372. * session - An instance of the session structure.
  373. * rect - The rectanglular region to be updated.
  374. * change - True: Frame buffer data has changed
  375. *
  376. * Returned Value:
  377. * Zero (OK) is returned on success; a negated errno value is returned on
  378. * any failure.
  379. *
  380. ****************************************************************************/
  381. int vnc_update_rectangle(FAR struct vnc_session_s *session,
  382. FAR const struct nxgl_rect_s *rect,
  383. bool change);
  384. /****************************************************************************
  385. * Name: vnc_receiver
  386. *
  387. * Description:
  388. * This function handles all Client-to-Server messages.
  389. *
  390. * Input Parameters:
  391. * session - An instance of the session structure.
  392. *
  393. * Returned Value:
  394. * At present, always returns OK
  395. *
  396. ****************************************************************************/
  397. int vnc_receiver(FAR struct vnc_session_s *session);
  398. /****************************************************************************
  399. * Name: vnc_rre
  400. *
  401. * Description:
  402. * This function does not really do RRE encoding. It just checks if the
  403. * update region is one color then uses the RRE encoding format to send
  404. * the constant color rectangle.
  405. *
  406. * Input Parameters:
  407. * session - An instance of the session structure.
  408. * rect - Describes the rectangle in the local framebuffer.
  409. *
  410. * Returned Value:
  411. * Zero is returned if RRE coding was not performed (but not error was)
  412. * encountered. Otherwise, the size of the framebuffer update message
  413. * is returned on success or a negated errno value is returned on failure
  414. * that indicates the nature of the failure. A failure is only
  415. * returned in cases of a network failure and unexpected internal failures.
  416. *
  417. ****************************************************************************/
  418. int vnc_rre(FAR struct vnc_session_s *session, FAR struct nxgl_rect_s *rect);
  419. /****************************************************************************
  420. * Name: vnc_raw
  421. *
  422. * Description:
  423. * As a fallback, send the framebuffer update using the RAW encoding which
  424. * must be supported by all VNC clients.
  425. *
  426. * Input Parameters:
  427. * session - An instance of the session structure.
  428. * rect - Describes the rectangle in the local framebuffer.
  429. *
  430. * Returned Value:
  431. * Zero (OK) on success; A negated errno value is returned on failure that
  432. * indicates the nature of the failure. A failure is only returned
  433. * in cases of a network failure and unexpected internal failures.
  434. *
  435. ****************************************************************************/
  436. int vnc_raw(FAR struct vnc_session_s *session, FAR struct nxgl_rect_s *rect);
  437. /****************************************************************************
  438. * Name: vnc_key_map
  439. *
  440. * Description:
  441. * Map the receive X11 keysym into something understood by NuttX and route
  442. * that through NX to the appropriate window.
  443. *
  444. * Input Parameters:
  445. * session - An instance of the session structure.
  446. * keysym - The X11 keysym value (see include/nuttx/inputx11_keysymdef)
  447. * keydown - True: Key pressed; False: Key released
  448. *
  449. * Returned Value:
  450. * None
  451. *
  452. ****************************************************************************/
  453. #ifdef CONFIG_NX_KBD
  454. void vnc_key_map(FAR struct vnc_session_s *session, uint16_t keysym,
  455. bool keydown);
  456. #endif
  457. /****************************************************************************
  458. * Name: vnc_convert_rgbNN
  459. *
  460. * Description:
  461. * Convert the native framebuffer color format (either RGB8 3:3:2,
  462. * RGB16 5:6:5, or RGB32 8:8:8) to the remote framebuffer color format
  463. * (either RGB8 2:2:2, RGB8 3:3:2, RGB16 5:5:5, RGB16 5:6:5, or RGB32
  464. * 8:8:8)
  465. *
  466. * Input Parameters:
  467. * pixel - The src color in local framebuffer format.
  468. *
  469. * Returned Value:
  470. * The pixel in the remote framebuffer color format.
  471. *
  472. ****************************************************************************/
  473. uint8_t vnc_convert_rgb8_222(lfb_color_t rgb);
  474. uint8_t vnc_convert_rgb8_332(lfb_color_t rgb);
  475. uint16_t vnc_convert_rgb16_555(lfb_color_t rgb);
  476. uint16_t vnc_convert_rgb16_565(lfb_color_t rgb);
  477. uint32_t vnc_convert_rgb32_888(lfb_color_t rgb);
  478. /****************************************************************************
  479. * Name: vnc_colors
  480. *
  481. * Description:
  482. * Test the update rectangle to see if it contains complex colors. If it
  483. * contains only a few colors, then it may be a candidate for some type
  484. * run-length encoding.
  485. *
  486. * Input Parameters:
  487. * session - An instance of the session structure.
  488. * rect - The update region in the local frame buffer.
  489. * maxcolors - The maximum number of colors that should be returned. This
  490. * currently cannot exceed eight.
  491. * colors - The top 'maxcolors' most frequency colors are returned.
  492. *
  493. * Returned Value:
  494. * The number of valid colors in the colors[] array are returned, the
  495. * first entry being the most frequent. A negated errno value is returned
  496. * if the colors cannot be determined. This would be the case if the color
  497. * depth is > 8 and there are more than 'maxcolors' colors in the update
  498. * rectangle.
  499. *
  500. ****************************************************************************/
  501. int vnc_colors(FAR struct vnc_session_s *session, FAR struct nxgl_rect_s *rect,
  502. unsigned int maxcolors, FAR lfb_color_t *colors);
  503. #undef EXTERN
  504. #ifdef __cplusplus
  505. }
  506. #endif
  507. #endif /* __GRAPHICS_VNC_SERVER_VNC_SERVER_H */