2012-03-27 02:23:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* nuttx/graphics/nxconsole/nxcon_driver.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 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 <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
|
|
|
#include "nxcon_internal.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nxcon_open(FAR struct file *filep);
|
|
|
|
static ssize_t nxcon_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
/* This is the common NX driver file operations */
|
|
|
|
|
2012-05-20 18:05:15 +02:00
|
|
|
#ifdef CONFIG_NXCONSOLE_NXKBDIN
|
|
|
|
|
|
|
|
const struct file_operations g_nxcon_drvrops =
|
|
|
|
{
|
|
|
|
nxcon_open, /* open */
|
|
|
|
0, /* close */
|
|
|
|
nxcon_read, /* read */
|
|
|
|
nxcon_write, /* write */
|
|
|
|
0, /* seek */
|
|
|
|
0 /* ioctl */
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
,
|
|
|
|
nxcon_poll /* poll */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#else /* CONFIG_NXCONSOLE_NXKBDIN */
|
|
|
|
|
2012-03-27 02:23:40 +02:00
|
|
|
const struct file_operations g_nxcon_drvrops =
|
|
|
|
{
|
|
|
|
nxcon_open, /* open */
|
|
|
|
0, /* close */
|
|
|
|
0, /* read */
|
|
|
|
nxcon_write, /* write */
|
|
|
|
0, /* seek */
|
|
|
|
0 /* ioctl */
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
2012-05-20 18:05:15 +02:00
|
|
|
,
|
|
|
|
0 /* poll */
|
2012-03-27 02:23:40 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2012-05-20 18:05:15 +02:00
|
|
|
#endif /* CONFIG_NXCONSOLE_NXKBDIN */
|
|
|
|
|
2012-03-27 02:23:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxcon_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nxcon_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct nxcon_state_s *priv = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(filep && filep->f_inode);
|
|
|
|
|
|
|
|
/* Get the driver structure from the inode */
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
priv = (FAR struct nxcon_state_s *)inode->i_private;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Verify that the driver is opened for write-only access */
|
|
|
|
|
2012-05-20 18:05:15 +02:00
|
|
|
#ifndef CONFIG_NXCONSOLE_NXKBDIN
|
2012-03-28 00:33:15 +02:00
|
|
|
if ((filep->f_oflags & O_RDOK) != 0)
|
2012-03-27 02:23:40 +02:00
|
|
|
{
|
2012-05-20 20:56:14 +02:00
|
|
|
gdbg("ERROR: Attempted open with read access\n");
|
2012-03-27 02:23:40 +02:00
|
|
|
return -EACCES;
|
|
|
|
}
|
2012-05-20 18:05:15 +02:00
|
|
|
#endif
|
2012-03-27 02:23:40 +02:00
|
|
|
|
|
|
|
/* Assign the driver structure to the file */
|
|
|
|
|
|
|
|
filep->f_priv = priv;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxcon_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t nxcon_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct nxcon_state_s *priv;
|
2012-03-30 20:42:40 +02:00
|
|
|
enum nxcon_vt100state_e state;
|
2012-03-28 18:06:56 +02:00
|
|
|
ssize_t remaining;
|
2012-03-27 02:23:40 +02:00
|
|
|
char ch;
|
2012-03-27 21:40:49 +02:00
|
|
|
int ret;
|
2012-03-27 02:23:40 +02:00
|
|
|
|
|
|
|
/* Recover our private state structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep && filep->f_priv);
|
|
|
|
priv = (FAR struct nxcon_state_s *)filep->f_priv;
|
|
|
|
|
2012-03-27 21:40:49 +02:00
|
|
|
/* Get exclusive access */
|
|
|
|
|
2012-03-30 00:20:47 +02:00
|
|
|
ret = nxcon_semwait(priv);
|
2012-03-27 21:40:49 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-31 18:26:32 +02:00
|
|
|
/* Hide the cursor while we update the display */
|
|
|
|
|
|
|
|
nxcon_hidecursor(priv);
|
|
|
|
|
2012-03-27 02:23:40 +02:00
|
|
|
/* Loop writing each character to the display */
|
|
|
|
|
2012-03-28 18:06:56 +02:00
|
|
|
for (remaining = (ssize_t)buflen; remaining > 0; remaining--)
|
2012-03-27 02:23:40 +02:00
|
|
|
{
|
2012-03-30 20:42:40 +02:00
|
|
|
/* Get the next character from the user buffer */
|
2012-03-27 18:59:15 +02:00
|
|
|
|
|
|
|
ch = *buffer++;
|
|
|
|
|
2012-03-30 20:42:40 +02:00
|
|
|
/* Check if this character is part of a VT100 escape sequence */
|
2012-03-27 02:23:40 +02:00
|
|
|
|
2012-03-30 20:42:40 +02:00
|
|
|
do
|
2012-03-27 02:23:40 +02:00
|
|
|
{
|
2012-03-30 20:42:40 +02:00
|
|
|
/* Is the character part of a VT100 escape sequnce? */
|
2012-03-27 02:23:40 +02:00
|
|
|
|
2012-03-30 20:42:40 +02:00
|
|
|
state = nxcon_vt100(priv, ch);
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
/* Character is not part of a VT100 escape sequence (and no
|
|
|
|
* characters are buffer.
|
|
|
|
*/
|
2012-03-27 02:23:40 +02:00
|
|
|
|
2012-03-30 20:42:40 +02:00
|
|
|
default:
|
|
|
|
case VT100_NOT_CONSUMED:
|
|
|
|
{
|
|
|
|
/* We can output the character to the window */
|
2012-03-27 02:23:40 +02:00
|
|
|
|
2012-03-30 20:42:40 +02:00
|
|
|
nxcon_putc(priv, (uint8_t)ch);
|
|
|
|
}
|
|
|
|
break;
|
2012-03-27 18:59:15 +02:00
|
|
|
|
2012-03-30 20:42:40 +02:00
|
|
|
/* The full VT100 escape sequence was processed (and the new
|
|
|
|
* character was consumed)
|
|
|
|
*/
|
2012-03-27 02:23:40 +02:00
|
|
|
|
2012-03-30 20:42:40 +02:00
|
|
|
case VT100_PROCESSED:
|
2012-03-27 02:23:40 +02:00
|
|
|
|
2012-03-30 20:42:40 +02:00
|
|
|
/* Character was consumed as part of the VT100 escape processing
|
|
|
|
* (but the escape sequence is still incomplete.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case VT100_CONSUMED:
|
|
|
|
{
|
|
|
|
/* Do nothing... the VT100 logic owns the character */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Invalid/unsupported character in escape sequence */
|
|
|
|
|
|
|
|
case VT100_ABORT:
|
|
|
|
{
|
|
|
|
int i;
|
2012-03-27 02:23:40 +02:00
|
|
|
|
2012-03-30 20:42:40 +02:00
|
|
|
/* Add the first unhandled character to the window */
|
2012-03-27 02:23:40 +02:00
|
|
|
|
2012-03-30 20:42:40 +02:00
|
|
|
nxcon_putc(priv, (uint8_t)priv->seq[0]);
|
|
|
|
|
|
|
|
/* Move all buffer characters down one */
|
|
|
|
|
|
|
|
for (i = 1; i < priv->nseq; i++)
|
|
|
|
{
|
|
|
|
priv->seq[i-1] = priv->seq[i];
|
|
|
|
}
|
|
|
|
priv->nseq--;
|
|
|
|
|
|
|
|
/* Then loop again and check if what remains is part of a
|
|
|
|
* VT100 escape sequence. We could speed this up by
|
|
|
|
* checking if priv->seq[0] == ASCII_ESC.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (state == VT100_ABORT);
|
2012-03-27 02:23:40 +02:00
|
|
|
}
|
|
|
|
|
2012-03-31 18:26:32 +02:00
|
|
|
/* Show the cursor at its new position */
|
|
|
|
|
|
|
|
nxcon_showcursor(priv);
|
2012-03-30 00:20:47 +02:00
|
|
|
nxcon_sempost(priv);
|
2012-03-28 18:06:56 +02:00
|
|
|
return (ssize_t)buflen;
|
2012-03-27 02:23:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|