Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/nxterm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ if(CONFIG_EXAMPLES_NXTERM)
SRCS
nxterm_main.c)
target_sources(apps PRIVATE nxterm_toolbar.c nxterm_wndo.c nxterm_listener.c)
if(CONFIG_EXAMPLES_NXTERM_PTYCONSOLE)
target_sources(apps PRIVATE nxterm_pty.c)
endif()
endif()
37 changes: 37 additions & 0 deletions examples/nxterm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,43 @@ config EXAMPLES_NXTERM_FULLSCREEN
Size the NXTerm window to cover the complete display instead of
centering it at three quarters of the display size.

config EXAMPLES_NXTERM_PTYCONSOLE
bool "PTY-backed keyboard console"
default n
depends on PSEUDOTERM && INPUT_KEYBOARD
depends on !INPUT_KEYBOARD_BYTESTREAM
depends on !NSH_ALTCONDEV && !NSH_USBKBD
---help---
Route NSH through a pseudo-terminal and bridge an event-mode keyboard
to the terminal master. This gives NXTerm a real TTY, translates
NuttX keyboard special-key codes to VT100 input, and permits terminal
generated signals such as Ctrl-C and Ctrl-Z.

config EXAMPLES_NXTERM_NSH_FALLBACK
bool "Fall back to serial NSH"
default n
depends on NSH_CONSOLE
---help---
Run NSH on the original system console if NX or NXTerm initialization
fails. This is useful on framebuffer systems where display startup
failures would otherwise leave no interactive console.

config EXAMPLES_NXTERM_STARTUP_TIMEOUT
int "NX startup timeout"
default 5
range 1 60
depends on EXAMPLES_NXTERM_NSH_FALLBACK
---help---
Number of seconds to wait for the NX server to accept the client
connection before falling back to NSH on the system console.

config EXAMPLES_NXTERM_KBDDEV
string "Keyboard device"
default "/dev/kbda"
depends on EXAMPLES_NXTERM_PTYCONSOLE
---help---
Path of the keyboard event device bridged to the pseudo-terminal.

config EXAMPLES_NXTERM_TOOLBAR_HEIGHT
int "Toolbar height"
default 16
Expand Down
5 changes: 5 additions & 0 deletions examples/nxterm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ include $(APPDIR)/Make.defs
# NuttX NX Console Example.

CSRCS = nxterm_toolbar.c nxterm_wndo.c nxterm_listener.c

ifeq ($(CONFIG_EXAMPLES_NXTERM_PTYCONSOLE),y)
CSRCS += nxterm_pty.c
endif

MAINSRC = nxterm_main.c

# NX built-in application info
Expand Down
7 changes: 7 additions & 0 deletions examples/nxterm/nxterm_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ struct nxterm_state_s
{
volatile bool haveres; /* True: Have screen resolution */
volatile bool connected; /* True: Connected to server */
#ifdef CONFIG_EXAMPLES_NXTERM_NSH_FALLBACK
volatile bool servererr; /* True: Server connection failed */
#endif
sem_t eventsem; /* Control waiting for display events */
pid_t pid; /* Console task ID */
NXHANDLE hnx; /* The connection handler */
Expand Down Expand Up @@ -254,4 +257,8 @@ extern const struct nx_callback_s g_nxtoolcb;

FAR void *nxterm_listener(FAR void *arg);

#ifdef CONFIG_EXAMPLES_NXTERM_PTYCONSOLE
int nxterm_pty_redirect(int nxtermfd);
#endif

#endif /* __APPS_EXAMPLES_NXTERM_NXTERM_INTERNAL_H */
8 changes: 7 additions & 1 deletion examples/nxterm/nxterm_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ FAR void *nxterm_listener(FAR void *arg)

/* Process events forever */

for (;;)
for (; ; )
{
/* Handle the next event. If we were configured blocking, then
* we will stay right here until the next event is received. Since
Expand All @@ -66,7 +66,13 @@ FAR void *nxterm_listener(FAR void *arg)
*/

printf("nxterm_listener: Lost server connection: %d\n", errno);
#ifdef CONFIG_EXAMPLES_NXTERM_NSH_FALLBACK
g_nxterm_vars.servererr = true;
sem_post(&g_nxterm_vars.eventsem);
return NULL;
#else
exit(EXIT_FAILURE);
#endif
}

/* If we received a message, we must be connected */
Expand Down
65 changes: 65 additions & 0 deletions examples/nxterm/nxterm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
Expand Down Expand Up @@ -76,6 +77,9 @@ struct nxterm_state_s g_nxterm_vars;
static int nxterm_initialize(void)
{
struct sched_param param;
#ifdef CONFIG_EXAMPLES_NXTERM_NSH_FALLBACK
struct timespec abstime;
#endif
pthread_t thread;
int ret;

Expand Down Expand Up @@ -142,6 +146,51 @@ static int nxterm_initialize(void)

/* Don't return until we are connected to the server */

#ifdef CONFIG_EXAMPLES_NXTERM_NSH_FALLBACK
pthread_detach(thread);

ret = clock_gettime(CLOCK_REALTIME, &abstime);
if (ret < 0)
{
printf("nxterm_initialize: clock_gettime failed: %d\n", errno);
nx_disconnect(g_nxterm_vars.hnx);
g_nxterm_vars.hnx = NULL;
return ERROR;
}

abstime.tv_sec += CONFIG_EXAMPLES_NXTERM_STARTUP_TIMEOUT;
while (!g_nxterm_vars.connected && !g_nxterm_vars.servererr)
{
/* Wait for the listener thread to report either a connection or
* a server error. The server starts asynchronously, so a timeout
* is also needed when display initialization fails before it can
* send the first event.
*/

ret = sem_timedwait(&g_nxterm_vars.eventsem, &abstime);
if (ret < 0 && errno != EINTR)
{
if (errno == ETIMEDOUT)
{
printf("nxterm_initialize: NX startup timed out\n");
}
else
{
printf("nxterm_initialize: sem_timedwait failed: %d\n",
errno);
}

g_nxterm_vars.servererr = true;
}
}

if (!g_nxterm_vars.connected)
{
nx_disconnect(g_nxterm_vars.hnx);
g_nxterm_vars.hnx = NULL;
return ERROR;
}
#else
while (!g_nxterm_vars.connected)
{
/* Wait for the listener thread to wake us up when we really
Expand All @@ -150,6 +199,7 @@ static int nxterm_initialize(void)

sem_wait(&g_nxterm_vars.eventsem);
}
#endif
}
else
{
Expand Down Expand Up @@ -376,12 +426,22 @@ int main(int argc, FAR char *argv[])
fflush(stdout);
fflush(stderr);

#ifdef CONFIG_EXAMPLES_NXTERM_PTYCONSOLE
ret = nxterm_pty_redirect(fd);
if (ret < 0)
{
printf("nxterm_main: PTY console setup failed: %d\n", -ret);
close(fd);
goto errout_with_driver;
}
#else
dup2(fd, 1);
dup2(fd, 2);

/* And we can close our original driver file descriptor */

close(fd);
#endif

/* And start the console task. It will inherit stdin, stdout, and stderr
* from this task.
Expand All @@ -408,5 +468,10 @@ int main(int argc, FAR char *argv[])
nx_disconnect(g_nxterm_vars.hnx);

errout:
#ifdef CONFIG_EXAMPLES_NXTERM_NSH_FALLBACK
printf("nxterm_main: Falling back to the system console\n");
return nsh_consolemain(argc, argv);
#else
return EXIT_FAILURE;
#endif
}
Loading
Loading