2012-03-27 18:59:15 +02:00
|
|
|
/****************************************************************************
|
2014-09-20 22:51:48 +02:00
|
|
|
* examples/nxterm/nxterm_main.c
|
2012-03-27 18:59:15 +02:00
|
|
|
*
|
2017-05-14 00:53:33 +02:00
|
|
|
* Copyright (C) 2012, 2016-2017 Gregory Nutt. All rights reserved.
|
2012-03-27 18:59:15 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2016-12-01 23:33:45 +01:00
|
|
|
#include <sys/boardctl.h>
|
2012-03-27 18:59:15 +02:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_NX_LCDDRIVER
|
|
|
|
# include <nuttx/lcd/lcd.h>
|
|
|
|
#else
|
2013-12-10 16:23:54 +01:00
|
|
|
# include <nuttx/video/fb.h>
|
2016-04-22 21:38:06 +02:00
|
|
|
# ifdef CONFIG_VNCSERVER
|
|
|
|
# include <nuttx/video/vnc.h>
|
|
|
|
# endif
|
2012-03-27 18:59:15 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <nuttx/nx/nx.h>
|
|
|
|
#include <nuttx/nx/nxglib.h>
|
|
|
|
#include <nuttx/nx/nxfonts.h>
|
2014-09-20 22:51:48 +02:00
|
|
|
#include <nuttx/nx/nxterm.h>
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2017-05-14 00:53:33 +02:00
|
|
|
#include "platform/cxxinitialize.h"
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "nshlib/nshlib.h"
|
2012-03-30 00:20:47 +02:00
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
#include "nxterm_internal.h"
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2017-06-27 17:14:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The NSH telnet console requires networking support (and TCP/IP) */
|
|
|
|
|
|
|
|
#ifndef CONFIG_NET
|
|
|
|
# undef CONFIG_NSH_TELNET
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If Telnet is used and both IPv6 and IPv4 are enabled, then we need to
|
|
|
|
* pick one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
# define ADDR_FAMILY AF_INET6
|
|
|
|
#else
|
|
|
|
# define ADDR_FAMILY AF_INET
|
|
|
|
#endif
|
|
|
|
|
2012-03-27 18:59:15 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* All example global variables are retained in a structure to minimize
|
|
|
|
* the chance of name collisions.
|
|
|
|
*/
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
struct nxterm_state_s g_nxterm_vars;
|
2012-03-27 18:59:15 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-09-20 22:51:48 +02:00
|
|
|
* Name: nxterm_initialize
|
2012-03-27 18:59:15 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
static int nxterm_initialize(void)
|
2012-03-27 18:59:15 +02:00
|
|
|
{
|
|
|
|
struct sched_param param;
|
|
|
|
pthread_t thread;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set the client task priority */
|
|
|
|
|
2015-09-08 17:20:49 +02:00
|
|
|
param.sched_priority = CONFIG_EXAMPLES_NXTERM_CLIENTPRIO;
|
2012-03-27 18:59:15 +02:00
|
|
|
ret = sched_setparam(0, ¶m);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_initialize: sched_setparam failed: %d\n" , ret);
|
2012-03-27 18:59:15 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2016-12-01 23:33:45 +01:00
|
|
|
/* Start the NX server kernel thread */
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2016-12-01 23:33:45 +01:00
|
|
|
ret = boardctl(BOARDIOC_NX_START, 0);
|
|
|
|
if (ret < 0)
|
2012-03-27 18:59:15 +02:00
|
|
|
{
|
2016-12-01 23:33:45 +01:00
|
|
|
printf("nxterm_initialize: Failed to start the NX server: %d\n", errno);
|
2012-03-27 18:59:15 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect to the server */
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
g_nxterm_vars.hnx = nx_connect();
|
|
|
|
if (g_nxterm_vars.hnx)
|
2012-03-27 18:59:15 +02:00
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
|
2016-04-22 21:38:06 +02:00
|
|
|
#ifdef CONFIG_VNCSERVER
|
|
|
|
/* Setup the VNC server to support keyboard/mouse inputs */
|
|
|
|
|
|
|
|
ret = vnc_default_fbinitialize(0, g_nxterm_vars.hnx);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("vnc_default_fbinitialize failed: %d\n", ret);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-27 18:59:15 +02:00
|
|
|
/* Start a separate thread to listen for server events. This is probably
|
|
|
|
* the least efficient way to do this, but it makes this example flow more
|
|
|
|
* smoothly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(void)pthread_attr_init(&attr);
|
2015-09-08 17:20:49 +02:00
|
|
|
param.sched_priority = CONFIG_EXAMPLES_NXTERM_LISTENERPRIO;
|
2012-03-27 18:59:15 +02:00
|
|
|
(void)pthread_attr_setschedparam(&attr, ¶m);
|
2015-09-08 17:20:49 +02:00
|
|
|
(void)pthread_attr_setstacksize(&attr, CONFIG_EXAMPLES_NXTERM_STACKSIZE);
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
ret = pthread_create(&thread, &attr, nxterm_listener, NULL);
|
2012-03-27 18:59:15 +02:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2014-09-20 22:51:48 +02:00
|
|
|
printf("nxterm_initialize: pthread_create failed: %d\n", ret);
|
2012-03-27 18:59:15 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't return until we are connected to the server */
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
while (!g_nxterm_vars.connected)
|
2012-03-27 18:59:15 +02:00
|
|
|
{
|
|
|
|
/* Wait for the listener thread to wake us up when we really
|
|
|
|
* are connected.
|
|
|
|
*/
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
(void)sem_wait(&g_nxterm_vars.eventsem);
|
2012-03-27 18:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_initialize: nx_connect failed: %d\n", errno);
|
2012-03-27 18:59:15 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-09-20 22:51:48 +02:00
|
|
|
* Name: nxterm_task
|
2012-03-27 18:59:15 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
static int nxterm_task(int argc, char **argv)
|
2012-03-30 00:20:47 +02:00
|
|
|
{
|
|
|
|
/* If the console front end is selected, then run it on this thread */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_CONSOLE
|
|
|
|
(void)nsh_consolemain(0, NULL);
|
|
|
|
#endif
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
printf("nxterm_task: Unregister the NX console device\n");
|
|
|
|
(void)nxterm_unregister(g_nxterm_vars.hdrvr);
|
2012-03-30 00:20:47 +02:00
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
printf("nxterm_task: Close the window\n");
|
|
|
|
(void)nxtk_closewindow(g_nxterm_vars.hwnd);
|
2012-03-30 00:20:47 +02:00
|
|
|
|
|
|
|
/* Disconnect from the server */
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
printf("nxterm_task: Disconnect from the server\n");
|
|
|
|
nx_disconnect(g_nxterm_vars.hnx);
|
2012-03-30 00:20:47 +02:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2012-03-27 18:59:15 +02:00
|
|
|
/****************************************************************************
|
2012-03-30 00:20:47 +02:00
|
|
|
* Public Functions
|
2012-03-27 18:59:15 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
/****************************************************************************
|
2014-09-20 22:51:48 +02:00
|
|
|
* Name: nxterm_main
|
2012-03-30 00:20:47 +02:00
|
|
|
****************************************************************************/
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2014-09-06 17:19:15 +02:00
|
|
|
#ifdef CONFIG_BUILD_KERNEL
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2014-09-06 17:19:15 +02:00
|
|
|
#else
|
2014-09-20 22:51:48 +02:00
|
|
|
int nxterm_main(int argc, char **argv)
|
2014-09-06 17:19:15 +02:00
|
|
|
#endif
|
2012-03-27 18:59:15 +02:00
|
|
|
{
|
|
|
|
nxgl_mxpixel_t color;
|
2012-03-30 00:20:47 +02:00
|
|
|
int fd;
|
2012-03-27 18:59:15 +02:00
|
|
|
int ret;
|
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
/* General Initialization *************************************************/
|
2012-03-27 18:59:15 +02:00
|
|
|
/* Reset all global data */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: Started\n");
|
2014-09-20 22:51:48 +02:00
|
|
|
memset(&g_nxterm_vars, 0, sizeof(struct nxterm_state_s));
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
/* Call all C++ static constructors */
|
|
|
|
|
|
|
|
#if defined(CONFIG_HAVE_CXX) && defined(CONFIG_HAVE_CXXINITIALIZE)
|
|
|
|
up_cxxinitialize();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* NSH Initialization *****************************************************/
|
|
|
|
/* Initialize the NSH library */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: Initialize NSH\n");
|
2012-03-30 00:20:47 +02:00
|
|
|
nsh_initialize();
|
|
|
|
|
|
|
|
/* If the Telnet console is selected as a front-end, then start the
|
|
|
|
* Telnet daemon.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_TELNET
|
2017-06-27 17:14:13 +02:00
|
|
|
ret = nsh_telnetstart(ADDR_FAMILY);
|
2012-03-30 00:20:47 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-05-11 21:40:03 +02:00
|
|
|
/* The daemon is NOT running. Report the error then fail...
|
2012-03-30 00:20:47 +02:00
|
|
|
* either with the serial console up or just exiting.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fprintf(stderr, "ERROR: Failed to start TELNET daemon: %d\n", ret);
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-27 18:59:15 +02:00
|
|
|
/* NX Initialization ******************************************************/
|
|
|
|
/* Initialize NX */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: Initialize NX\n");
|
2014-09-20 22:51:48 +02:00
|
|
|
ret = nxterm_initialize();
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: NX handle=%p\n", g_nxterm_vars.hnx);
|
2014-09-20 22:51:48 +02:00
|
|
|
if (!g_nxterm_vars.hnx || ret < 0)
|
2012-03-27 18:59:15 +02:00
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: Failed to get NX handle: %d\n", errno);
|
2012-03-27 18:59:15 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the background to the configured background color */
|
|
|
|
|
2015-09-08 17:20:49 +02:00
|
|
|
printf("nxterm_main: Set background color=%d\n", CONFIG_EXAMPLES_NXTERM_BGCOLOR);
|
|
|
|
color = CONFIG_EXAMPLES_NXTERM_BGCOLOR;
|
2014-09-20 22:51:48 +02:00
|
|
|
ret = nx_setbgcolor(g_nxterm_vars.hnx, &color);
|
2012-03-27 18:59:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: nx_setbgcolor failed: %d\n", errno);
|
2012-03-27 18:59:15 +02:00
|
|
|
goto errout_with_nx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Window Configuration ***************************************************/
|
|
|
|
/* Create a window */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: Create window\n");
|
2014-09-20 22:51:48 +02:00
|
|
|
g_nxterm_vars.hwnd = nxtk_openwindow(g_nxterm_vars.hnx, &g_nxtermcb, NULL);
|
|
|
|
if (!g_nxterm_vars.hwnd)
|
2012-03-27 18:59:15 +02:00
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: nxtk_openwindow failed: %d\n", errno);
|
2012-03-27 18:59:15 +02:00
|
|
|
goto errout_with_nx;
|
|
|
|
}
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: hwnd=%p\n", g_nxterm_vars.hwnd);
|
2012-03-27 18:59:15 +02:00
|
|
|
|
|
|
|
/* Wait until we have the screen resolution. We'll have this immediately
|
|
|
|
* unless we are dealing with the NX server.
|
|
|
|
*/
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
while (!g_nxterm_vars.haveres)
|
2012-03-27 18:59:15 +02:00
|
|
|
{
|
2014-09-20 22:51:48 +02:00
|
|
|
(void)sem_wait(&g_nxterm_vars.eventsem);
|
2012-03-27 18:59:15 +02:00
|
|
|
}
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: Screen resolution (%d,%d)\n", g_nxterm_vars.xres, g_nxterm_vars.yres);
|
2012-03-27 18:59:15 +02:00
|
|
|
|
|
|
|
/* Determine the size and position of the window */
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
g_nxterm_vars.wndo.wsize.w = g_nxterm_vars.xres / 2 + g_nxterm_vars.xres / 4;
|
|
|
|
g_nxterm_vars.wndo.wsize.h = g_nxterm_vars.yres / 2 + g_nxterm_vars.yres / 4;
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
g_nxterm_vars.wpos.x = g_nxterm_vars.xres / 8;
|
|
|
|
g_nxterm_vars.wpos.y = g_nxterm_vars.yres / 8;
|
2012-03-27 18:59:15 +02:00
|
|
|
|
|
|
|
/* Set the window position */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: Set window position to (%d,%d)\n",
|
|
|
|
g_nxterm_vars.wpos.x, g_nxterm_vars.wpos.y);
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
ret = nxtk_setposition(g_nxterm_vars.hwnd, &g_nxterm_vars.wpos);
|
2012-03-27 18:59:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: nxtk_setposition failed: %d\n", errno);
|
2012-03-27 18:59:15 +02:00
|
|
|
goto errout_with_hwnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the window size */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: Set window size to (%d,%d)\n",
|
|
|
|
g_nxterm_vars.wndo.wsize.w, g_nxterm_vars.wndo.wsize.h);
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
ret = nxtk_setsize(g_nxterm_vars.hwnd, &g_nxterm_vars.wndo.wsize);
|
2012-03-27 18:59:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: nxtk_setsize failed: %d\n", errno);
|
2012-03-27 18:59:15 +02:00
|
|
|
goto errout_with_hwnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the toolbar */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: Add toolbar to window\n");
|
2015-09-08 17:20:49 +02:00
|
|
|
ret = nxtk_opentoolbar(g_nxterm_vars.hwnd, CONFIG_EXAMPLES_NXTERM_TOOLBAR_HEIGHT, &g_nxtoolcb, NULL);
|
2012-03-27 18:59:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: nxtk_opentoolbar failed: %d\n", errno);
|
2012-03-27 18:59:15 +02:00
|
|
|
goto errout_with_hwnd;
|
|
|
|
}
|
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
/* Sleep a little bit to allow the server to catch up */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
sleep(2);
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
/* NxTerm Configuration ************************************************/
|
2012-03-27 18:59:15 +02:00
|
|
|
/* Use the window to create an NX console */
|
|
|
|
|
2015-09-08 17:20:49 +02:00
|
|
|
g_nxterm_vars.wndo.wcolor[0] = CONFIG_EXAMPLES_NXTERM_WCOLOR;
|
|
|
|
g_nxterm_vars.wndo.fcolor[0] = CONFIG_EXAMPLES_NXTERM_FONTCOLOR;
|
|
|
|
g_nxterm_vars.wndo.fontid = CONFIG_EXAMPLES_NXTERM_FONTID;
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2015-09-08 17:20:49 +02:00
|
|
|
g_nxterm_vars.hdrvr = nxtk_register(g_nxterm_vars.hwnd, &g_nxterm_vars.wndo, CONFIG_EXAMPLES_NXTERM_MINOR);
|
2014-09-20 22:51:48 +02:00
|
|
|
if (!g_nxterm_vars.hdrvr)
|
2012-03-27 18:59:15 +02:00
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: nxtk_register failed: %d\n", errno);
|
2012-03-27 18:59:15 +02:00
|
|
|
goto errout_with_hwnd;
|
|
|
|
}
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
/* Open the NxTerm driver */
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2015-09-08 17:20:49 +02:00
|
|
|
fd = open(CONFIG_EXAMPLES_NXTERM_DEVNAME, O_WRONLY);
|
2012-03-27 18:59:15 +02:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: open %s read-only failed: %d\n",
|
2015-09-08 17:20:49 +02:00
|
|
|
CONFIG_EXAMPLES_NXTERM_DEVNAME, errno);
|
2012-03-27 18:59:15 +02:00
|
|
|
goto errout_with_driver;
|
|
|
|
}
|
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
/* Start Console Task *****************************************************/
|
2012-03-28 02:10:43 +02:00
|
|
|
/* Now re-direct stdout and stderr so that they use the NX console driver.
|
2017-05-11 21:40:03 +02:00
|
|
|
* Note that stdin is retained (file descriptor 0, probably the serial console).
|
2012-03-28 02:10:43 +02:00
|
|
|
*/
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nxterm_main: Starting the console task\n");
|
|
|
|
fflush(stdout);
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
(void)fflush(stdout);
|
|
|
|
(void)fflush(stderr);
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
(void)fclose(stdout);
|
|
|
|
(void)fclose(stderr);
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
(void)dup2(fd, 1);
|
|
|
|
(void)dup2(fd, 2);
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
/* And we can close our original driver file descriptor */
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
close(fd);
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
/* And start the console task. It will inherit stdin, stdout, and stderr
|
|
|
|
* from this task.
|
|
|
|
*/
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
g_nxterm_vars.pid = task_create("NxTerm", CONFIG_EXAMPLES_NXTERM_PRIO,
|
2014-09-20 22:25:51 +02:00
|
|
|
CONFIG_EXAMPLES_NXTERM_STACKSIZE,
|
2014-09-20 22:51:48 +02:00
|
|
|
nxterm_task, NULL);
|
|
|
|
ASSERT(g_nxterm_vars.pid > 0);
|
2012-03-30 00:20:47 +02:00
|
|
|
return EXIT_SUCCESS;
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
/* Error Exits ************************************************************/
|
2012-03-28 02:10:43 +02:00
|
|
|
|
2012-03-27 18:59:15 +02:00
|
|
|
errout_with_driver:
|
2014-09-20 22:51:48 +02:00
|
|
|
(void)nxterm_unregister(g_nxterm_vars.hdrvr);
|
2012-03-27 18:59:15 +02:00
|
|
|
|
|
|
|
errout_with_hwnd:
|
2014-09-20 22:51:48 +02:00
|
|
|
(void)nxtk_closewindow(g_nxterm_vars.hwnd);
|
2012-03-27 18:59:15 +02:00
|
|
|
|
|
|
|
errout_with_nx:
|
|
|
|
/* Disconnect from the server */
|
|
|
|
|
2014-09-20 22:51:48 +02:00
|
|
|
nx_disconnect(g_nxterm_vars.hnx);
|
2012-03-27 18:59:15 +02:00
|
|
|
errout:
|
2012-03-30 00:20:47 +02:00
|
|
|
return EXIT_FAILURE;
|
2012-03-27 18:59:15 +02:00
|
|
|
}
|