nxterm_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /****************************************************************************
  2. * apps/examples/nxterm/nxterm_main.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <sys/boardctl.h>
  26. #include <stdint.h>
  27. #include <stdbool.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. #include <errno.h>
  35. #include <debug.h>
  36. #ifdef CONFIG_NX_LCDDRIVER
  37. # include <nuttx/lcd/lcd.h>
  38. #else
  39. # include <nuttx/video/fb.h>
  40. #endif
  41. #include <nuttx/nx/nx.h>
  42. #include <nuttx/nx/nxglib.h>
  43. #include <nuttx/nx/nxfonts.h>
  44. #include <nuttx/nx/nxterm.h>
  45. #include "nshlib/nshlib.h"
  46. #include "nxterm_internal.h"
  47. /****************************************************************************
  48. * Pre-processor Definitions
  49. ****************************************************************************/
  50. /* The NSH telnet console requires networking support (and TCP/IP) */
  51. #ifndef CONFIG_NET
  52. # undef CONFIG_NSH_TELNET
  53. #endif
  54. /****************************************************************************
  55. * Public Data
  56. ****************************************************************************/
  57. /* All example global variables are retained in a structure to minimize
  58. * the chance of name collisions.
  59. */
  60. struct nxterm_state_s g_nxterm_vars;
  61. /****************************************************************************
  62. * Private Functions
  63. ****************************************************************************/
  64. /****************************************************************************
  65. * Name: nxterm_initialize
  66. ****************************************************************************/
  67. static int nxterm_initialize(void)
  68. {
  69. struct sched_param param;
  70. pthread_t thread;
  71. int ret;
  72. /* Set the client task priority */
  73. param.sched_priority = CONFIG_EXAMPLES_NXTERM_CLIENTPRIO;
  74. ret = sched_setparam(0, &param);
  75. if (ret < 0)
  76. {
  77. printf("nxterm_initialize: sched_setparam failed: %d\n" , ret);
  78. return ERROR;
  79. }
  80. /* Start the NX server kernel thread */
  81. ret = boardctl(BOARDIOC_NX_START, 0);
  82. if (ret < 0)
  83. {
  84. printf("nxterm_initialize: Failed to start the NX server: %d\n",
  85. errno);
  86. return ERROR;
  87. }
  88. /* Connect to the server */
  89. g_nxterm_vars.hnx = nx_connect();
  90. if (g_nxterm_vars.hnx)
  91. {
  92. pthread_attr_t attr;
  93. #ifdef CONFIG_VNCSERVER
  94. /* Setup the VNC server to support keyboard/mouse inputs */
  95. struct boardioc_vncstart_s vnc =
  96. {
  97. 0, g_nxterm_vars.hnx
  98. };
  99. ret = boardctl(BOARDIOC_VNC_START, (uintptr_t)&vnc);
  100. if (ret < 0)
  101. {
  102. printf("boardctl(BOARDIOC_VNC_START) failed: %d\n", ret);
  103. nx_disconnect(g_nxterm_vars.hnx);
  104. return ERROR;
  105. }
  106. #endif
  107. /* Start a separate thread to listen for server events. This is
  108. * probably the least efficient way to do this, but it makes this
  109. * example flow more smoothly.
  110. */
  111. pthread_attr_init(&attr);
  112. param.sched_priority = CONFIG_EXAMPLES_NXTERM_LISTENERPRIO;
  113. pthread_attr_setschedparam(&attr, &param);
  114. pthread_attr_setstacksize(&attr, CONFIG_EXAMPLES_NXTERM_STACKSIZE);
  115. ret = pthread_create(&thread, &attr, nxterm_listener, NULL);
  116. if (ret != 0)
  117. {
  118. printf("nxterm_initialize: pthread_create failed: %d\n", ret);
  119. return ERROR;
  120. }
  121. /* Don't return until we are connected to the server */
  122. while (!g_nxterm_vars.connected)
  123. {
  124. /* Wait for the listener thread to wake us up when we really
  125. * are connected.
  126. */
  127. sem_wait(&g_nxterm_vars.eventsem);
  128. }
  129. }
  130. else
  131. {
  132. printf("nxterm_initialize: nx_connect failed: %d\n", errno);
  133. return ERROR;
  134. }
  135. return OK;
  136. }
  137. /****************************************************************************
  138. * Name: nxterm_task
  139. ****************************************************************************/
  140. static int nxterm_task(int argc, char **argv)
  141. {
  142. /* If the console front end is selected, then run it on this thread */
  143. #ifdef CONFIG_NSH_CONSOLE
  144. nsh_consolemain(argc, argv);
  145. #endif
  146. printf("nxterm_task: Unlinking the NX console device\n");
  147. unlink(CONFIG_EXAMPLES_NXTERM_DEVNAME);
  148. printf("nxterm_task: Close the window\n");
  149. nxtk_closewindow(g_nxterm_vars.hwnd);
  150. /* Disconnect from the server */
  151. printf("nxterm_task: Disconnect from the server\n");
  152. nx_disconnect(g_nxterm_vars.hnx);
  153. return EXIT_SUCCESS;
  154. }
  155. /****************************************************************************
  156. * Public Functions
  157. ****************************************************************************/
  158. /****************************************************************************
  159. * Name: nxterm_main
  160. ****************************************************************************/
  161. int main(int argc, FAR char *argv[])
  162. {
  163. struct boardioc_nxterm_create_s nxcreate;
  164. nxgl_mxpixel_t color;
  165. int fd;
  166. int ret;
  167. /* General Initialization *************************************************/
  168. /* Reset all global data */
  169. printf("nxterm_main: Started\n");
  170. memset(&g_nxterm_vars, 0, sizeof(struct nxterm_state_s));
  171. /* NSH Initialization *****************************************************/
  172. /* Initialize the NSH library */
  173. printf("nxterm_main: Initialize NSH\n");
  174. nsh_initialize();
  175. /* If the Telnet console is selected as a front-end, then start the
  176. * Telnet daemon.
  177. */
  178. #ifdef CONFIG_NSH_TELNET
  179. ret = nsh_telnetstart(AF_UNSPEC);
  180. if (ret < 0)
  181. {
  182. /* The daemon is NOT running. Report the error then fail...
  183. * either with the serial console up or just exiting.
  184. */
  185. fprintf(stderr, "ERROR: Failed to start TELNET daemon: %d\n", ret);
  186. }
  187. #endif
  188. /* NX Initialization ******************************************************/
  189. /* Initialize NX */
  190. printf("nxterm_main: Initialize NX\n");
  191. ret = nxterm_initialize();
  192. printf("nxterm_main: NX handle=%p\n", g_nxterm_vars.hnx);
  193. if (!g_nxterm_vars.hnx || ret < 0)
  194. {
  195. printf("nxterm_main: Failed to get NX handle: %d\n", errno);
  196. goto errout;
  197. }
  198. /* Set the background to the configured background color */
  199. printf("nxterm_main: Set background color=%d\n",
  200. CONFIG_EXAMPLES_NXTERM_BGCOLOR);
  201. color = CONFIG_EXAMPLES_NXTERM_BGCOLOR;
  202. ret = nx_setbgcolor(g_nxterm_vars.hnx, &color);
  203. if (ret < 0)
  204. {
  205. printf("nxterm_main: nx_setbgcolor failed: %d\n", errno);
  206. goto errout_with_nx;
  207. }
  208. /* Window Configuration ***************************************************/
  209. /* Create a window */
  210. printf("nxterm_main: Create window\n");
  211. g_nxterm_vars.hwnd = nxtk_openwindow(g_nxterm_vars.hnx, 0, &g_nxtermcb,
  212. NULL);
  213. if (!g_nxterm_vars.hwnd)
  214. {
  215. printf("nxterm_main: nxtk_openwindow failed: %d\n", errno);
  216. goto errout_with_nx;
  217. }
  218. printf("nxterm_main: hwnd=%p\n", g_nxterm_vars.hwnd);
  219. /* Wait until we have the screen resolution. */
  220. while (!g_nxterm_vars.haveres)
  221. {
  222. sem_wait(&g_nxterm_vars.eventsem);
  223. }
  224. printf("nxterm_main: Screen resolution (%d,%d)\n",
  225. g_nxterm_vars.xres, g_nxterm_vars.yres);
  226. /* Determine the size and position of the window */
  227. g_nxterm_vars.wndo.wsize.w = g_nxterm_vars.xres / 2 +
  228. g_nxterm_vars.xres / 4;
  229. g_nxterm_vars.wndo.wsize.h = g_nxterm_vars.yres / 2 +
  230. g_nxterm_vars.yres / 4;
  231. g_nxterm_vars.wpos.x = g_nxterm_vars.xres / 8;
  232. g_nxterm_vars.wpos.y = g_nxterm_vars.yres / 8;
  233. /* Set the window position */
  234. printf("nxterm_main: Set window position to (%d,%d)\n",
  235. g_nxterm_vars.wpos.x, g_nxterm_vars.wpos.y);
  236. ret = nxtk_setposition(g_nxterm_vars.hwnd, &g_nxterm_vars.wpos);
  237. if (ret < 0)
  238. {
  239. printf("nxterm_main: nxtk_setposition failed: %d\n", errno);
  240. goto errout_with_hwnd;
  241. }
  242. /* Set the window size */
  243. printf("nxterm_main: Set window size to (%d,%d)\n",
  244. g_nxterm_vars.wndo.wsize.w, g_nxterm_vars.wndo.wsize.h);
  245. ret = nxtk_setsize(g_nxterm_vars.hwnd, &g_nxterm_vars.wndo.wsize);
  246. if (ret < 0)
  247. {
  248. printf("nxterm_main: nxtk_setsize failed: %d\n", errno);
  249. goto errout_with_hwnd;
  250. }
  251. /* Open the toolbar */
  252. printf("nxterm_main: Add toolbar to window\n");
  253. ret = nxtk_opentoolbar(g_nxterm_vars.hwnd,
  254. CONFIG_EXAMPLES_NXTERM_TOOLBAR_HEIGHT,
  255. &g_nxtoolcb, NULL);
  256. if (ret < 0)
  257. {
  258. printf("nxterm_main: nxtk_opentoolbar failed: %d\n", errno);
  259. goto errout_with_hwnd;
  260. }
  261. /* Sleep a little bit to allow the server to catch up */
  262. sleep(2);
  263. /* NxTerm Configuration ***************************************************/
  264. /* Use the window to create an NX console */
  265. g_nxterm_vars.wndo.wcolor[0] = CONFIG_EXAMPLES_NXTERM_WCOLOR;
  266. g_nxterm_vars.wndo.fcolor[0] = CONFIG_EXAMPLES_NXTERM_FONTCOLOR;
  267. g_nxterm_vars.wndo.fontid = CONFIG_EXAMPLES_NXTERM_FONTID;
  268. nxcreate.nxterm = NULL;
  269. nxcreate.hwnd = g_nxterm_vars.hwnd;
  270. nxcreate.wndo = g_nxterm_vars.wndo;
  271. nxcreate.type = BOARDIOC_XTERM_FRAMED;
  272. nxcreate.minor = CONFIG_EXAMPLES_NXTERM_MINOR;
  273. /* BOARDIOC_NXTERM wants the size of the NxTK main sub-window */
  274. nxcreate.wndo.wsize.w -= (2 * CONFIG_NXTK_BORDERWIDTH);
  275. nxcreate.wndo.wsize.h -= (CONFIG_EXAMPLES_NXTERM_TOOLBAR_HEIGHT +
  276. 2 * CONFIG_NXTK_BORDERWIDTH);
  277. ret = boardctl(BOARDIOC_NXTERM, (uintptr_t)&nxcreate);
  278. if (ret < 0)
  279. {
  280. printf("nxterm_main: boardctl(BOARDIOC_NXTERM) failed: %d\n", errno);
  281. goto errout_with_hwnd;
  282. }
  283. g_nxterm_vars.hdrvr = nxcreate.nxterm;
  284. DEBUGASSERT(g_nxterm_vars.hdrvr != NULL);
  285. /* Open the NxTerm driver */
  286. fd = open(CONFIG_EXAMPLES_NXTERM_DEVNAME, O_WRONLY);
  287. if (fd < 0)
  288. {
  289. printf("nxterm_main: open %s read-only failed: %d\n",
  290. CONFIG_EXAMPLES_NXTERM_DEVNAME, errno);
  291. goto errout_with_driver;
  292. }
  293. /* Start Console Task *****************************************************/
  294. /* Now re-direct stdout and stderr so that they use the NX console driver.
  295. * Note that stdin is retained (file descriptor 0, probably the serial
  296. * console).
  297. */
  298. printf("nxterm_main: Starting the console task\n");
  299. fflush(stdout);
  300. fflush(stderr);
  301. dup2(fd, 1);
  302. dup2(fd, 2);
  303. /* And we can close our original driver file descriptor */
  304. close(fd);
  305. /* And start the console task. It will inherit stdin, stdout, and stderr
  306. * from this task.
  307. */
  308. g_nxterm_vars.pid = task_create("NxTerm", CONFIG_EXAMPLES_NXTERM_PRIO,
  309. CONFIG_EXAMPLES_NXTERM_STACKSIZE,
  310. nxterm_task, NULL);
  311. DEBUGASSERT(g_nxterm_vars.pid > 0);
  312. return EXIT_SUCCESS;
  313. /* Error Exits ************************************************************/
  314. errout_with_driver:
  315. unlink(CONFIG_EXAMPLES_NXTERM_DEVNAME);
  316. errout_with_hwnd:
  317. nxtk_closewindow(g_nxterm_vars.hwnd);
  318. errout_with_nx:
  319. /* Disconnect from the server */
  320. nx_disconnect(g_nxterm_vars.hnx);
  321. errout:
  322. return EXIT_FAILURE;
  323. }