2019-05-08 19:15:44 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2019-05-10 23:22:27 +02:00
|
|
|
// apps/graphics/twm4nx/src/cnxterm.cxx
|
2019-05-08 19:15:44 +02:00
|
|
|
// NxTerm window
|
|
|
|
//
|
|
|
|
// Copyright (C) 2019 Gregory Nutt. All rights reserved.
|
|
|
|
// 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 <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cunistd>
|
2019-05-12 19:57:45 +02:00
|
|
|
#include <cfcntl>
|
2019-05-08 19:15:44 +02:00
|
|
|
#include <ctime>
|
2019-05-12 19:57:45 +02:00
|
|
|
#include <cassert>
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
#include <sys/boardctl.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include "nshlib/nshlib.h"
|
|
|
|
|
|
|
|
#include "graphics/nxwidgets/cwidgetcontrol.hxx"
|
|
|
|
|
|
|
|
#include "graphics/nxglyphs.hxx"
|
|
|
|
#include "graphics/twm4nx/twm4nx_config.hxx"
|
2019-05-12 19:57:45 +02:00
|
|
|
#include "graphics/twm4nx/ctwm4nx.hxx"
|
|
|
|
#include "graphics/twm4nx/cwindow.hxx"
|
|
|
|
#include "graphics/twm4nx/cwindowfactory.hxx"
|
|
|
|
#include "graphics/twm4nx/cmainmenu.hxx"
|
2019-05-20 19:01:46 +02:00
|
|
|
|
|
|
|
#include "graphics/twm4nx/apps/nxterm_config.hxx"
|
|
|
|
#include "graphics/twm4nx/apps/cnxterm.hxx"
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Pre-Processor Definitions
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Configuration ////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_USBKBD
|
2019-05-11 21:17:19 +02:00
|
|
|
# warning You probably do not really want CONFIG_NSH_USBKBD, try CONFIG_TWM4NX_KEYBOARD_USBHOST
|
2019-05-08 19:15:44 +02:00
|
|
|
#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
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Private Types
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
namespace Twm4Nx
|
|
|
|
{
|
2019-06-01 21:00:02 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Private Types
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-05-08 19:15:44 +02:00
|
|
|
/**
|
2019-06-01 21:00:02 +02:00
|
|
|
* This structure is used to pass start up parameters to the NxTerm task
|
|
|
|
* and to assure the the NxTerm is successfully started.
|
2019-05-08 19:15:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct SNxTerm
|
|
|
|
{
|
|
|
|
FAR void *console; /**< The console 'this' pointer use with on_exit() */
|
|
|
|
sem_t exclSem; /**< Sem that gives exclusive access to this structure */
|
|
|
|
sem_t waitSem; /**< Sem that posted when the task is initialized */
|
|
|
|
NXTKWINDOW hwnd; /**< Window handle */
|
|
|
|
NXTERM nxterm; /**< NxTerm handle */
|
|
|
|
int minor; /**< Next device minor number */
|
|
|
|
struct nxterm_window_s wndo; /**< Describes the NxTerm window */
|
2019-05-12 19:57:45 +02:00
|
|
|
bool success; /**< True if successfully initialized */
|
2019-05-08 19:15:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Private Data
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
2019-06-01 21:00:02 +02:00
|
|
|
* This global data structure is used to pass start parameters to NxTerm
|
|
|
|
* task and to assure that the NxTerm is successfully started.
|
2019-05-08 19:15:44 +02:00
|
|
|
*/
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
static struct SNxTerm GNxTermVars;
|
2019-05-08 19:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CNxTerm Method Implementations
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
using namespace Twm4Nx;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CNxTerm constructor
|
|
|
|
*
|
2019-05-12 19:57:45 +02:00
|
|
|
* @param twm4nx. The Twm4Nx session instance
|
2019-05-08 19:15:44 +02:00
|
|
|
*/
|
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
CNxTerm::CNxTerm(FAR CTwm4Nx *twm4nx)
|
2019-05-08 19:15:44 +02:00
|
|
|
{
|
2019-05-12 19:57:45 +02:00
|
|
|
// Save the context data
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
m_twm4nx = twm4nx;
|
|
|
|
m_nxtermWindow = (FAR CWindow *)0;
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
// The NxTerm is not running
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
m_pid = (pid_t)-1;
|
|
|
|
m_NxTerm = (NXTERM)0;
|
2019-05-08 19:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CNxTerm destructor
|
|
|
|
*/
|
|
|
|
|
|
|
|
CNxTerm::~CNxTerm(void)
|
|
|
|
{
|
|
|
|
// There would be a problem if we were stopped with the NxTerm task
|
|
|
|
// running... that should never happen but we'll check anyway:
|
|
|
|
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-12 19:57:45 +02:00
|
|
|
* CNxTerm initializers. Perform miscellaneous post-construction
|
|
|
|
* initialization that may fail (and hence is not appropriate to be
|
|
|
|
* done in the constructor)
|
|
|
|
*
|
|
|
|
* @return True if the NxTerm application was successfully initialized.
|
2019-05-08 19:15:44 +02:00
|
|
|
*/
|
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
bool CNxTerm::initialize(void)
|
2019-05-08 19:15:44 +02:00
|
|
|
{
|
2019-05-12 19:57:45 +02:00
|
|
|
// Call CWindowFactory::createWindow() to create a window for the NxTerm
|
|
|
|
// application. Customizations:
|
|
|
|
//
|
|
|
|
// Flags: WFLAGS_NO_MENU_BUTTON indicates that there is no menu associated
|
|
|
|
// with the NxTerm application window
|
2019-06-01 21:00:02 +02:00
|
|
|
// Null Icon manager means to use the system, common Icon Manager
|
2019-05-12 19:57:45 +02:00
|
|
|
|
|
|
|
NXWidgets::CNxString name("NuttShell");
|
|
|
|
|
2019-05-13 01:40:02 +02:00
|
|
|
uint8_t wflags = (WFLAGS_NO_MENU_BUTTON | WFLAGS_HIDDEN);
|
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
FAR CWindowFactory *factory = m_twm4nx->getWindowFactory();
|
|
|
|
m_nxtermWindow = factory->createWindow(name, &CONFIG_TWM4NX_NXTERM_ICON,
|
2019-05-13 01:40:02 +02:00
|
|
|
(FAR CIconMgr *)0, wflags);
|
2019-05-12 19:57:45 +02:00
|
|
|
if (m_nxtermWindow == (FAR CWindow *)0)
|
|
|
|
{
|
|
|
|
twmerr("ERROR: Failed to create CWindow\n");
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
// Configure events needed by the NxTerm applications
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
struct SAppEvents events;
|
|
|
|
events.eventObj = (FAR void *)this;
|
|
|
|
events.redrawEvent = EVENT_NXTERM_REDRAW;
|
2019-05-16 21:51:57 +02:00
|
|
|
events.resizeEvent = EVENT_NXTERM_RESIZE;
|
2019-05-12 19:57:45 +02:00
|
|
|
events.mouseEvent = EVENT_NXTERM_XYINPUT;
|
|
|
|
events.kbdEvent = EVENT_NXTERM_KBDINPUT;
|
|
|
|
events.closeEvent = EVENT_NXTERM_CLOSE;
|
2019-05-20 19:01:46 +02:00
|
|
|
events.deleteEvent = EVENT_NXTERM_DELETE;
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
bool success = m_nxtermWindow->configureEvents(events);
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
delete m_nxtermWindow;
|
|
|
|
m_nxtermWindow = (FAR CWindow *)0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-13 01:40:02 +02:00
|
|
|
return m_nxtermWindow->showWindow();
|
2019-05-08 19:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the application (perhaps in the minimized state).
|
|
|
|
*
|
|
|
|
* @return True if the application was successfully started.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool CNxTerm::run(void)
|
|
|
|
{
|
|
|
|
// Some sanity checking
|
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
if (m_pid >= 0 || m_NxTerm != 0)
|
2019-05-08 19:15:44 +02:00
|
|
|
{
|
2019-05-09 21:31:12 +02:00
|
|
|
twmerr("ERROR: All ready running or connected\n");
|
2019-05-08 19:15:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get exclusive access to the global data structure
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
if (sem_wait(&GNxTermVars.exclSem) != 0)
|
2019-05-08 19:15:44 +02:00
|
|
|
{
|
|
|
|
// This might fail if a signal is received while we are waiting.
|
|
|
|
|
2019-05-09 21:31:12 +02:00
|
|
|
twmerr("ERROR: Failed to get semaphore\n");
|
2019-05-08 19:15:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the widget control associated with the NXTK window
|
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
NXWidgets::CWidgetControl *control = m_nxtermWindow->getWidgetControl();
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
// Get the window handle from the widget control
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
GNxTermVars.hwnd = control->getWindowHandle();
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
// Describe the NxTerm
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
GNxTermVars.wndo.wcolor[0] = CONFIG_TWM4NX_NXTERM_WCOLOR;
|
|
|
|
GNxTermVars.wndo.fcolor[0] = CONFIG_TWM4NX_NXTERM_FONTCOLOR;
|
|
|
|
GNxTermVars.wndo.fontid = CONFIG_TWM4NX_NXTERM_FONTID;
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
// Remember the device minor number (before it is incremented)
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
m_minor = GNxTermVars.minor;
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
// Get the size of the window
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
if (!m_nxtermWindow->getWindowSize(&GNxTermVars.wndo.wsize))
|
2019-05-12 19:57:45 +02:00
|
|
|
{
|
|
|
|
twmerr("ERROR: getWindowSize() failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
// Start the NxTerm task
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
GNxTermVars.console = (FAR void *)this;
|
|
|
|
GNxTermVars.success = false;
|
|
|
|
GNxTermVars.nxterm = 0;
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
sched_lock();
|
2019-05-11 21:17:19 +02:00
|
|
|
m_pid = task_create("NxTerm", CONFIG_TWM4NX_NXTERM_PRIO,
|
|
|
|
CONFIG_TWM4NX_NXTERM_STACKSIZE, nxterm,
|
2019-05-08 19:15:44 +02:00
|
|
|
(FAR char * const *)0);
|
|
|
|
|
|
|
|
// Did we successfully start the NxTerm task?
|
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
bool success = true;
|
2019-05-08 19:15:44 +02:00
|
|
|
if (m_pid < 0)
|
|
|
|
{
|
2019-05-09 21:31:12 +02:00
|
|
|
twmerr("ERROR: Failed to create the NxTerm task\n");
|
2019-05-12 19:57:45 +02:00
|
|
|
success = false;
|
2019-05-08 19:15:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Wait for up to two seconds for the task to initialize
|
|
|
|
|
|
|
|
struct timespec abstime;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &abstime);
|
|
|
|
abstime.tv_sec += 2;
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
int ret = sem_timedwait(&GNxTermVars.waitSem, &abstime);
|
2019-05-08 19:15:44 +02:00
|
|
|
sched_unlock();
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
if (ret == OK && GNxTermVars.success)
|
2019-05-08 19:15:44 +02:00
|
|
|
{
|
2019-05-12 19:57:45 +02:00
|
|
|
#ifdef CONFIG_NXTERM_NXKBDIN
|
2019-05-08 19:15:44 +02:00
|
|
|
// Re-direct NX keyboard input to the new NxTerm driver
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
DEBUGASSERT(GNxTermVars.nxterm != 0);
|
|
|
|
m_nxtermWindow->redirectNxTerm(GNxTermVars.nxterm);
|
2019-05-08 19:15:44 +02:00
|
|
|
#endif
|
|
|
|
// Save the handle to use in the stop method
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
m_NxTerm = GNxTermVars.nxterm;
|
2019-05-08 19:15:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// sem_timedwait failed OR the NxTerm task reported a
|
|
|
|
// failure. Stop the application
|
|
|
|
|
2019-05-09 21:31:12 +02:00
|
|
|
twmerr("ERROR: Failed start the NxTerm task\n");
|
2019-05-08 19:15:44 +02:00
|
|
|
stop();
|
2019-05-12 19:57:45 +02:00
|
|
|
success = false;
|
2019-05-08 19:15:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
sem_post(&GNxTermVars.exclSem);
|
2019-05-12 19:57:45 +02:00
|
|
|
return success;
|
2019-05-08 19:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-01 21:00:02 +02:00
|
|
|
* This is the close window event handler. It will stop the NxTerm
|
|
|
|
* application thread.
|
2019-05-08 19:15:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
void CNxTerm::stop(void)
|
|
|
|
{
|
|
|
|
// Delete the NxTerm task if it is still running (this could strand
|
2019-05-12 19:57:45 +02:00
|
|
|
// resources).
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
if (m_pid >= 0)
|
|
|
|
{
|
|
|
|
pid_t pid = m_pid;
|
2019-05-12 19:57:45 +02:00
|
|
|
m_pid = (pid_t)-1;
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
// Then delete the NSH task, possibly stranding resources
|
|
|
|
|
|
|
|
task_delete(pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the NX console device
|
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
if (m_NxTerm)
|
2019-05-08 19:15:44 +02:00
|
|
|
{
|
2019-05-12 19:57:45 +02:00
|
|
|
#ifdef CONFIG_NXTERM_NXKBDIN
|
2019-05-08 19:15:44 +02:00
|
|
|
// Re-store NX keyboard input routing
|
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
m_nxtermWindow->redirectNxTerm((NXTERM)0);
|
2019-05-08 19:15:44 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Unlink the NxTerm driver
|
|
|
|
// Construct the driver name using this minor number
|
|
|
|
|
|
|
|
char devname[32];
|
|
|
|
snprintf(devname, 32, "/dev/nxterm%d", m_minor);
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
unlink(devname);
|
2019-05-12 19:57:45 +02:00
|
|
|
m_NxTerm = 0;
|
2019-05-08 19:15:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the NxTerm task. This function first redirects output to the
|
|
|
|
* console window then calls to start the NSH logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int CNxTerm::nxterm(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
// To stop compiler complaining about "jump to label crosses initialization
|
|
|
|
// of 'int fd'
|
|
|
|
|
|
|
|
int fd = -1;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
// Use the window handle to create the NX console
|
|
|
|
|
|
|
|
struct boardioc_nxterm_create_s nxcreate;
|
|
|
|
|
|
|
|
nxcreate.nxterm = (FAR void *)0;
|
2019-06-01 21:00:02 +02:00
|
|
|
nxcreate.hwnd = GNxTermVars.hwnd;
|
|
|
|
nxcreate.wndo = GNxTermVars.wndo;
|
2019-05-08 19:15:44 +02:00
|
|
|
nxcreate.type = BOARDIOC_XTERM_FRAMED;
|
2019-06-01 21:00:02 +02:00
|
|
|
nxcreate.minor = GNxTermVars.minor;
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
ret = boardctl(BOARDIOC_NXTERM, (uintptr_t)&nxcreate);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-05-09 21:31:12 +02:00
|
|
|
twmerr("ERROR: boardctl(BOARDIOC_NXTERM) failed: %d\n", errno);
|
2019-05-08 19:15:44 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
GNxTermVars.nxterm = nxcreate.nxterm;
|
|
|
|
DEBUGASSERT(GNxTermVars.nxterm != NULL);
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
// Construct the driver name using this minor number
|
|
|
|
|
|
|
|
char devname[32];
|
2019-06-01 21:00:02 +02:00
|
|
|
snprintf(devname, 32, "/dev/nxterm%d", GNxTermVars.minor);
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
// Increment the minor number while it is protect by the semaphore
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
GNxTermVars.minor++;
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
// Open the NxTerm driver
|
|
|
|
|
|
|
|
#ifdef CONFIG_NXTERM_NXKBDIN
|
|
|
|
fd = open(devname, O_RDWR);
|
|
|
|
#else
|
|
|
|
fd = open(devname, O_WRONLY);
|
|
|
|
#endif
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2019-05-09 21:31:12 +02:00
|
|
|
twmerr("ERROR: Failed open the console device\n");
|
2020-01-02 13:09:50 +01:00
|
|
|
unlink(devname);
|
2019-05-08 19:15:44 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now re-direct stdout and stderr so that they use the NX console driver.
|
|
|
|
// Notes: (1) stdin is retained (file descriptor 0, probably the the serial
|
|
|
|
// console). (2) Don't bother trying to put debug instrumentation in the
|
|
|
|
// following becaue it will end up in the NxTerm window.
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
std::fflush(stdout);
|
|
|
|
std::fflush(stderr);
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NXTERM_NXKBDIN
|
2020-01-02 13:09:50 +01:00
|
|
|
std::fclose(stdin);
|
2019-05-08 19:15:44 +02:00
|
|
|
#endif
|
2020-01-02 13:09:50 +01:00
|
|
|
std::fclose(stdout);
|
|
|
|
std::fclose(stderr);
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NXTERM_NXKBDIN
|
2020-01-02 13:09:50 +01:00
|
|
|
std::dup2(fd, 0);
|
2019-05-08 19:15:44 +02:00
|
|
|
#endif
|
2020-01-02 13:09:50 +01:00
|
|
|
std::dup2(fd, 1);
|
|
|
|
std::dup2(fd, 2);
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NXTERM_NXKBDIN
|
2020-01-02 13:09:50 +01:00
|
|
|
std::fdopen(0, "r");
|
2019-05-08 19:15:44 +02:00
|
|
|
#endif
|
2020-01-02 13:09:50 +01:00
|
|
|
std::fdopen(1, "w");
|
|
|
|
std::fdopen(2, "w");
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
// And we can close our original driver file descriptor
|
|
|
|
|
|
|
|
if (fd > 2)
|
|
|
|
{
|
|
|
|
std::close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inform the parent thread that we successfully initialized
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
GNxTermVars.success = true;
|
|
|
|
sem_post(&GNxTermVars.waitSem);
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
// Run the NSH console
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_CONSOLE
|
2020-01-02 13:09:50 +01:00
|
|
|
nsh_consolemain(argc, argv);
|
2019-05-08 19:15:44 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// We get here if the NSH console should exits. nsh_consolemain() ALWAYS
|
|
|
|
// exits by calling nsh_exit() (which is a pointer to nsh_consoleexit())
|
|
|
|
// which, in turn, calls exit()
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
|
|
|
errout:
|
2019-06-01 21:00:02 +02:00
|
|
|
GNxTermVars.nxterm = 0;
|
|
|
|
GNxTermVars.success = false;
|
|
|
|
sem_post(&GNxTermVars.waitSem);
|
2019-05-08 19:15:44 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-05-11 23:48:46 +02:00
|
|
|
/**
|
|
|
|
* Handle Twm4Nx events. This overrides a method from CTwm4NXEvent
|
|
|
|
*
|
|
|
|
* @param eventmsg. The received NxWidget WINDOW event message.
|
|
|
|
* @return True if the message was properly handled. false is
|
|
|
|
* return on any failure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool CNxTerm::event(FAR struct SEventMsg *eventmsg)
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
switch (eventmsg->eventID)
|
|
|
|
{
|
2019-05-16 21:51:57 +02:00
|
|
|
case EVENT_NXTERM_REDRAW: // Redraw event
|
2019-05-12 19:57:45 +02:00
|
|
|
redraw(); // Redraw the whole window
|
|
|
|
break;
|
|
|
|
|
2019-05-16 21:51:57 +02:00
|
|
|
case EVENT_NXTERM_RESIZE: // Resize event
|
|
|
|
resize(); // Size the NxTerm window
|
|
|
|
break;
|
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
case EVENT_NXTERM_CLOSE: // Window close event
|
|
|
|
stop(); // Stop the NxTerm thread
|
|
|
|
break;
|
|
|
|
|
2019-05-11 23:48:46 +02:00
|
|
|
default:
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2019-05-08 19:15:44 +02:00
|
|
|
/**
|
2019-05-16 21:51:57 +02:00
|
|
|
* Redraw the entire window. The application has been maximized, resized or
|
|
|
|
* moved to the top of the hierarchy. This method is call from CTwm4Nx when
|
|
|
|
* the application window must be displayed
|
2019-05-08 19:15:44 +02:00
|
|
|
*/
|
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
void CNxTerm::redraw(void)
|
2019-05-08 19:15:44 +02:00
|
|
|
{
|
2019-05-12 19:57:45 +02:00
|
|
|
// Get the size of the window
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
struct nxgl_size_s windowSize;
|
2020-01-02 13:09:50 +01:00
|
|
|
m_nxtermWindow->getWindowSize(&windowSize);
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
// Redraw the entire NxTerm window
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-05-16 21:51:57 +02:00
|
|
|
struct boardioc_nxterm_ioctl_s iocargs;
|
|
|
|
struct nxtermioc_redraw_s redraw;
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
redraw.handle = m_NxTerm;
|
|
|
|
redraw.rect.pt1.x = 0;
|
|
|
|
redraw.rect.pt1.y = 0;
|
|
|
|
redraw.rect.pt2.x = windowSize.w - 1;
|
|
|
|
redraw.rect.pt2.y = windowSize.h - 1;
|
|
|
|
redraw.more = false;
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-05-16 21:51:57 +02:00
|
|
|
iocargs.cmd = NXTERMIOC_NXTERM_REDRAW;
|
|
|
|
iocargs.arg = (uintptr_t)&redraw;
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
boardctl(BOARDIOC_NXTERM_IOCTL, (uintptr_t)&iocargs);
|
2019-05-16 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-23 18:20:12 +02:00
|
|
|
* Inform NxTerm of a new window size.
|
2019-05-16 21:51:57 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
void CNxTerm::resize(void)
|
|
|
|
{
|
|
|
|
struct nxtermioc_resize_s resize;
|
|
|
|
|
|
|
|
// Get the size of the window
|
|
|
|
|
|
|
|
resize.handle = m_NxTerm;
|
2020-01-02 13:09:50 +01:00
|
|
|
m_nxtermWindow->getWindowSize(&resize.size);
|
2019-05-16 21:51:57 +02:00
|
|
|
|
|
|
|
// Inform NxTerm of the new size
|
|
|
|
|
|
|
|
struct boardioc_nxterm_ioctl_s iocargs;
|
|
|
|
iocargs.cmd = NXTERMIOC_NXTERM_RESIZE;
|
|
|
|
iocargs.arg = (uintptr_t)&resize;
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
boardctl(BOARDIOC_NXTERM_IOCTL, (uintptr_t)&iocargs);
|
2019-05-08 19:15:44 +02:00
|
|
|
}
|
|
|
|
|
2019-05-11 23:48:46 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CNxTermFactory Method Implementations
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
/**
|
2019-05-11 23:48:46 +02:00
|
|
|
* CNxTermFactory Initializer. Performs parts of the instance
|
2019-05-20 19:01:46 +02:00
|
|
|
* construction that may fail. In this implementation, it will
|
2019-05-11 23:48:46 +02:00
|
|
|
* initialize the NSH library and register an menu item in the
|
|
|
|
* Main Menu.
|
2019-05-08 19:15:44 +02:00
|
|
|
*/
|
|
|
|
|
2019-05-11 23:48:46 +02:00
|
|
|
bool CNxTermFactory::initialize(FAR CTwm4Nx *twm4nx)
|
2019-05-11 21:17:19 +02:00
|
|
|
{
|
2019-05-13 01:40:02 +02:00
|
|
|
// Save the session instance
|
|
|
|
|
|
|
|
m_twm4nx = twm4nx;
|
|
|
|
|
2019-05-11 23:48:46 +02:00
|
|
|
// Initialize the NSH library
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-05-11 23:48:46 +02:00
|
|
|
if (!nshlibInitialize())
|
2019-05-08 19:15:44 +02:00
|
|
|
{
|
2019-05-11 23:48:46 +02:00
|
|
|
twmerr("ERROR: Failed to initialize the NSH library\n");
|
|
|
|
return false;
|
2019-05-08 19:15:44 +02:00
|
|
|
}
|
|
|
|
|
2019-05-11 23:48:46 +02:00
|
|
|
// Register an entry with the Main menu. When selected, this will
|
|
|
|
// Case the start
|
2019-05-08 19:15:44 +02:00
|
|
|
|
2019-05-11 23:48:46 +02:00
|
|
|
FAR CMainMenu *cmain = twm4nx->getMainMenu();
|
|
|
|
return cmain->addApplication(this);
|
2019-05-08 19:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* One time NSH initialization. This function must be called exactly
|
|
|
|
* once during the boot-up sequence to initialize the NSH library.
|
|
|
|
*
|
|
|
|
* @return True on successful initialization
|
|
|
|
*/
|
|
|
|
|
2019-05-11 23:48:46 +02:00
|
|
|
bool CNxTermFactory::nshlibInitialize(void)
|
2019-05-08 19:15:44 +02:00
|
|
|
{
|
|
|
|
// Initialize the global data structure
|
|
|
|
|
2019-06-01 21:00:02 +02:00
|
|
|
sem_init(&GNxTermVars.exclSem, 0, 1);
|
|
|
|
sem_init(&GNxTermVars.waitSem, 0, 0);
|
2019-05-08 19:15:44 +02:00
|
|
|
|
|
|
|
// Initialize the NSH library
|
|
|
|
|
|
|
|
nsh_initialize();
|
|
|
|
|
|
|
|
// If the Telnet console is selected as a front-end, then start the
|
|
|
|
// Telnet daemon.
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_TELNET
|
|
|
|
int ret = nsh_telnetstart(ADDR_FAMILY);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
// The daemon is NOT running!
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-11 23:48:46 +02:00
|
|
|
/**
|
2019-05-20 19:01:46 +02:00
|
|
|
* Handle CNxTermFactory events. This overrides a method from
|
2019-05-13 01:40:02 +02:00
|
|
|
* CTwm4NXEvent
|
2019-05-11 23:48:46 +02:00
|
|
|
*
|
2019-05-13 01:40:02 +02:00
|
|
|
* @param eventmsg. The received NxWidget WINDOW event message.
|
|
|
|
* @return True if the message was properly handled. false is
|
|
|
|
* return on any failure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool CNxTermFactory::event(FAR struct SEventMsg *eventmsg)
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
switch (eventmsg->eventID)
|
|
|
|
{
|
2019-05-20 19:01:46 +02:00
|
|
|
case EVENT_NXTERM_START: // Main menu selection
|
2019-05-13 01:40:02 +02:00
|
|
|
startFunction(); // Create a new NxTerm instance
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and start a new instance of CNxTerm.
|
2019-05-11 23:48:46 +02:00
|
|
|
*/
|
|
|
|
|
2019-05-13 01:40:02 +02:00
|
|
|
bool CNxTermFactory::startFunction(void)
|
2019-05-11 23:48:46 +02:00
|
|
|
{
|
2019-05-12 19:57:45 +02:00
|
|
|
// Instantiate the Nxterm application, providing only the session session
|
|
|
|
// instance to the constructor
|
2019-05-11 23:48:46 +02:00
|
|
|
|
2019-05-13 01:40:02 +02:00
|
|
|
CNxTerm *nxterm = new CNxTerm(m_twm4nx);
|
2019-05-12 19:57:45 +02:00
|
|
|
if (!nxterm)
|
2019-05-11 23:48:46 +02:00
|
|
|
{
|
2019-05-12 19:57:45 +02:00
|
|
|
twmerr("ERROR: Failed to instantiate CNxTerm\n");
|
2019-05-11 23:48:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
// Initialize the NxTerm application
|
2019-05-11 23:48:46 +02:00
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
if (!nxterm->initialize())
|
2019-05-11 23:48:46 +02:00
|
|
|
{
|
2019-05-12 19:57:45 +02:00
|
|
|
twmerr("ERROR: Failed to initialize CNxTerm instance\n");
|
|
|
|
delete nxterm;
|
2019-05-11 23:48:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
// Start the NxTerm application instance
|
2019-05-11 23:48:46 +02:00
|
|
|
|
2019-05-12 19:57:45 +02:00
|
|
|
if (!nxterm->run())
|
2019-05-11 23:48:46 +02:00
|
|
|
{
|
2019-05-12 19:57:45 +02:00
|
|
|
twmerr("ERROR: Failed to start the NxTerm application\n");
|
|
|
|
delete nxterm;
|
|
|
|
return false;
|
2019-05-11 23:48:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|