2012-05-20 18:05:15 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* graphics/nxterm/nxterm_kbdin.c
|
2012-05-20 18:05:15 +02:00
|
|
|
*
|
2021-02-05 11:12:53 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-05-20 18:05:15 +02:00
|
|
|
*
|
2021-02-05 11:12:53 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-05-20 18:05:15 +02:00
|
|
|
*
|
2021-02-05 11:12:53 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-05-20 18:05:15 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2020-11-24 04:50:41 +01:00
|
|
|
#include <inttypes.h>
|
2012-05-20 18:05:15 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <assert.h>
|
2014-07-10 23:29:41 +02:00
|
|
|
#include <poll.h>
|
2012-05-20 18:05:15 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 22:09:28 +01:00
|
|
|
#include <nuttx/irq.h>
|
|
|
|
|
2014-09-20 23:53:28 +02:00
|
|
|
#include "nxterm.h"
|
2012-05-20 18:05:15 +02:00
|
|
|
|
2014-09-20 22:18:08 +02:00
|
|
|
#ifdef CONFIG_NXTERM_NXKBDIN
|
2012-05-20 18:05:15 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-09-20 23:53:28 +02:00
|
|
|
* Name: nxterm_pollnotify
|
2012-05-20 18:05:15 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2019-05-22 02:57:54 +02:00
|
|
|
static void nxterm_pollnotify(FAR struct nxterm_state_s *priv,
|
|
|
|
pollevent_t eventset)
|
2012-05-20 18:05:15 +02:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* This function may be called from an interrupt handler */
|
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
flags = enter_critical_section();
|
|
|
|
poll_notify(priv->fds, CONFIG_NXTERM_NPOLLWAITERS, eventset);
|
|
|
|
leave_critical_section(flags);
|
2012-05-20 18:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-09-20 23:53:28 +02:00
|
|
|
* Name: nxterm_read
|
2012-05-20 18:05:15 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2014-09-20 23:01:50 +02:00
|
|
|
* The optional NxTerm read method
|
2012-05-20 18:05:15 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-20 23:53:28 +02:00
|
|
|
ssize_t nxterm_read(FAR struct file *filep, FAR char *buffer, size_t len)
|
2012-05-20 18:05:15 +02:00
|
|
|
{
|
2014-09-20 23:53:28 +02:00
|
|
|
FAR struct nxterm_state_s *priv;
|
2012-05-20 18:05:15 +02:00
|
|
|
ssize_t nread;
|
|
|
|
char ch;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Recover our private state structure */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv);
|
2014-09-20 23:53:28 +02:00
|
|
|
priv = (FAR struct nxterm_state_s *)filep->f_priv;
|
2012-05-20 18:05:15 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the driver structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2012-05-20 18:05:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
gerr("ERROR: nxmutex_lock failed\n");
|
2012-05-20 18:05:15 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop until something is read */
|
|
|
|
|
|
|
|
for (nread = 0; nread < len; )
|
|
|
|
{
|
|
|
|
/* Get the next byte from the buffer */
|
|
|
|
|
|
|
|
if (priv->head == priv->tail)
|
|
|
|
{
|
|
|
|
/* The circular buffer is empty. Did we read anything? */
|
|
|
|
|
|
|
|
if (nread > 0)
|
|
|
|
{
|
|
|
|
/* Yes.. break out to return what we have. */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-09-26 14:25:03 +02:00
|
|
|
/* If the driver was opened with O_NONBLOCK option, then
|
|
|
|
* don't wait. Just return EGAIN.
|
2012-05-20 18:05:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
nread = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, wait for something to be written to the circular
|
2020-09-26 14:25:03 +02:00
|
|
|
* buffer. Increment the number of waiters so that the
|
|
|
|
* nxterm_write() will not that it needs to post the semaphore
|
|
|
|
* to wake us up.
|
2012-05-20 18:05:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
priv->nwaiters++;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2012-05-20 18:05:15 +02:00
|
|
|
|
|
|
|
/* We may now be pre-empted! But that should be okay because we
|
|
|
|
* have already incremented nwaiters. Pre-emption is disabled
|
|
|
|
* but will be re-enabled while we are waiting.
|
|
|
|
*/
|
|
|
|
|
2017-10-07 17:40:02 +02:00
|
|
|
ret = nxsem_wait(&priv->waitsem);
|
2012-05-20 18:05:15 +02:00
|
|
|
|
|
|
|
/* Did we successfully get the waitsem? */
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Yes... then retake the mutual exclusion mutex */
|
2012-05-20 18:05:15 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2012-05-20 18:05:15 +02:00
|
|
|
}
|
|
|
|
|
2023-07-05 14:41:28 +02:00
|
|
|
priv->nwaiters--;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Was the mutex wait successful? Did we successful re-take the
|
|
|
|
* mutual exclusion mutex?
|
2012-05-20 18:05:15 +02:00
|
|
|
*/
|
|
|
|
|
2014-04-14 00:22:22 +02:00
|
|
|
if (ret < 0)
|
2012-05-20 18:05:15 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
/* No.. One of the two nxmutex_lock's failed. */
|
2012-05-20 18:05:15 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
gerr("ERROR: nxmutex_lock failed\n");
|
2012-05-20 20:56:14 +02:00
|
|
|
|
2012-05-20 18:05:15 +02:00
|
|
|
/* Were we awakened by a signal? Did we read anything before
|
|
|
|
* we received the signal?
|
|
|
|
*/
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
if (ret != -EINTR || nread >= 0)
|
2012-05-20 18:05:15 +02:00
|
|
|
{
|
|
|
|
/* Yes.. return the error. */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
nread = ret;
|
2012-05-20 18:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Break out to return what we have. Note, we can't exactly
|
|
|
|
* "break" out because whichever error occurred, we do not hold
|
2022-09-06 08:18:45 +02:00
|
|
|
* the exclusion mutex.
|
2012-05-20 18:05:15 +02:00
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_without_lock;
|
2012-05-20 18:05:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The circular buffer is not empty, get the next byte from the
|
|
|
|
* tail index.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ch = priv->rxbuffer[priv->tail];
|
|
|
|
|
|
|
|
/* Increment the tail index and re-enable interrupts */
|
|
|
|
|
2014-09-20 22:18:08 +02:00
|
|
|
if (++priv->tail >= CONFIG_NXTERM_KBDBUFSIZE)
|
2012-05-20 18:05:15 +02:00
|
|
|
{
|
|
|
|
priv->tail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the character to the user buffer */
|
|
|
|
|
|
|
|
buffer[nread] = ch;
|
|
|
|
nread++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Relinquish the mutual exclusion mutex */
|
2012-05-20 18:05:15 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2012-05-20 18:05:15 +02:00
|
|
|
|
|
|
|
/* Notify all poll/select waiters that they can write to the FIFO */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_without_lock:
|
2012-05-20 18:05:15 +02:00
|
|
|
if (nread > 0)
|
|
|
|
{
|
2014-09-20 23:53:28 +02:00
|
|
|
nxterm_pollnotify(priv, POLLOUT);
|
2012-05-20 18:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the number of characters actually read */
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-09-20 23:53:28 +02:00
|
|
|
* Name: nxterm_poll
|
2012-05-20 18:05:15 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-20 23:53:28 +02:00
|
|
|
int nxterm_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
|
2012-05-20 18:05:15 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2014-09-20 23:53:28 +02:00
|
|
|
FAR struct nxterm_state_s *priv;
|
2012-05-20 18:05:15 +02:00
|
|
|
pollevent_t eventset;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2012-05-20 18:05:15 +02:00
|
|
|
priv = inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2012-05-20 18:05:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
gerr("ERROR: nxmutex_lock failed\n");
|
2012-05-20 18:05:15 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we setting up the poll? Or tearing it down? */
|
|
|
|
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
/* This is a request to set up the poll. Find an available
|
|
|
|
* slot for the poll structure reference
|
|
|
|
*/
|
|
|
|
|
2014-09-20 22:18:08 +02:00
|
|
|
for (i = 0; i < CONFIG_NXTERM_NPOLLWAITERS; i++)
|
2012-05-20 18:05:15 +02:00
|
|
|
{
|
|
|
|
/* Find an available slot */
|
|
|
|
|
|
|
|
if (!priv->fds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
|
|
|
|
|
|
|
priv->fds[i] = fds;
|
2023-11-19 12:19:53 +01:00
|
|
|
fds->priv = &priv->fds[i];
|
2012-05-20 18:05:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-20 22:18:08 +02:00
|
|
|
if (i >= CONFIG_NXTERM_NPOLLWAITERS)
|
2012-05-20 18:05:15 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
gerr("ERROR: Too many poll waiters\n");
|
2012-05-20 20:56:14 +02:00
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
2012-05-20 18:05:15 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should immediately notify on any of the requested events?
|
|
|
|
* This driver is always available for transmission.
|
|
|
|
*/
|
|
|
|
|
|
|
|
eventset = POLLOUT;
|
|
|
|
|
|
|
|
/* Check if the receive buffer is empty */
|
|
|
|
|
|
|
|
if (priv->head != priv->tail)
|
2020-09-26 14:25:03 +02:00
|
|
|
{
|
|
|
|
eventset |= POLLIN;
|
|
|
|
}
|
2012-05-20 18:05:15 +02:00
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
poll_notify(&fds, 1, eventset);
|
2012-05-20 18:05:15 +02:00
|
|
|
}
|
|
|
|
else if (fds->priv)
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
|
|
|
struct pollfd **slot = (struct pollfd **)fds->priv;
|
|
|
|
|
2019-03-17 18:34:45 +01:00
|
|
|
#ifdef CONFIG_DEBUG_GRAPHICS
|
2012-05-20 18:05:15 +02:00
|
|
|
if (!slot)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
gerr("ERROR: No slot\n");
|
2012-05-20 20:56:14 +02:00
|
|
|
|
2012-05-20 18:05:15 +02:00
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
2022-02-01 17:55:50 +01:00
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
2012-05-20 18:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2012-05-20 18:05:15 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-09-20 23:53:28 +02:00
|
|
|
* Name: nxterm_kbdin
|
2012-05-20 18:05:15 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function should be driven by the window kbdin callback function
|
2014-09-20 23:01:50 +02:00
|
|
|
* (see nx.h). When the NxTerm is the top window and keyboard input is
|
2012-05-20 18:05:15 +02:00
|
|
|
* received on the top window, that window callback should be directed to
|
2014-07-30 22:26:50 +02:00
|
|
|
* this function. This function will buffer the keyboard data and makE
|
2014-09-20 23:01:50 +02:00
|
|
|
* it available to the NxTerm as stdin.
|
2012-05-20 18:05:15 +02:00
|
|
|
*
|
2014-09-20 23:01:50 +02:00
|
|
|
* If CONFIG_NXTERM_NXKBDIN is not selected, then the NxTerm will
|
2012-05-20 18:05:15 +02:00
|
|
|
* receive its input from stdin (/dev/console). This works great but
|
|
|
|
* cannot be shared between different windows. Chaos will ensue if you
|
2014-09-20 23:01:50 +02:00
|
|
|
* try to support multiple NxTerm windows without CONFIG_NXTERM_NXKBDIN
|
2012-05-20 18:05:15 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* handle - A handle previously returned by nx_register, nxtk_register, or
|
|
|
|
* nxtool_register.
|
|
|
|
* buffer - The array of characters
|
|
|
|
* buflen - The number of characters that are available in buffer[]
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-20 23:53:28 +02:00
|
|
|
void nxterm_kbdin(NXTERM handle, FAR const uint8_t *buffer, uint8_t buflen)
|
2012-05-20 18:05:15 +02:00
|
|
|
{
|
2014-09-20 23:53:28 +02:00
|
|
|
FAR struct nxterm_state_s *priv;
|
2012-05-20 18:05:15 +02:00
|
|
|
ssize_t nwritten;
|
|
|
|
int nexthead;
|
|
|
|
char ch;
|
2012-05-21 00:10:34 +02:00
|
|
|
int ret;
|
2012-05-20 18:05:15 +02:00
|
|
|
|
2020-11-24 04:50:41 +01:00
|
|
|
ginfo("buflen=%" PRId8 "\n", buflen);
|
2012-05-20 18:05:15 +02:00
|
|
|
DEBUGASSERT(handle);
|
|
|
|
|
|
|
|
/* Get the reference to the driver structure from the handle */
|
|
|
|
|
2014-09-20 23:53:28 +02:00
|
|
|
priv = (FAR struct nxterm_state_s *)handle;
|
2012-05-20 18:05:15 +02:00
|
|
|
|
2012-05-21 00:10:34 +02:00
|
|
|
/* Get exclusive access to the driver structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2012-05-21 00:10:34 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
gerr("ERROR: nxmutex_lock failed\n");
|
2012-05-21 00:10:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Loop until all of the bytes have been written. This function may be
|
|
|
|
* called from an interrupt handler! Semaphores cannot be used!
|
|
|
|
*
|
|
|
|
* The write logic only needs to modify the head index. Therefore,
|
|
|
|
* there is a difference in the way that head and tail are protected:
|
|
|
|
* tail is protected with a semaphore; tail is protected by disabling
|
|
|
|
* interrupts.
|
|
|
|
*/
|
2012-05-20 18:05:15 +02:00
|
|
|
|
|
|
|
for (nwritten = 0; nwritten < buflen; nwritten++)
|
|
|
|
{
|
|
|
|
/* Add the next character */
|
|
|
|
|
|
|
|
ch = buffer[nwritten];
|
|
|
|
|
|
|
|
/* Calculate the write index AFTER the next byte is add to the ring
|
|
|
|
* buffer
|
|
|
|
*/
|
|
|
|
|
|
|
|
nexthead = priv->head + 1;
|
2014-09-20 22:18:08 +02:00
|
|
|
if (nexthead >= CONFIG_NXTERM_KBDBUFSIZE)
|
2012-05-20 18:05:15 +02:00
|
|
|
{
|
|
|
|
nexthead = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Would the next write overflow the circular buffer? */
|
|
|
|
|
|
|
|
if (nexthead == priv->tail)
|
|
|
|
{
|
2020-09-26 14:25:03 +02:00
|
|
|
/* Yes... Return an indication that nothing was saved in
|
|
|
|
* the buffer.
|
|
|
|
*/
|
2012-05-20 18:05:15 +02:00
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
gerr("ERROR: Keyboard data overrun\n");
|
2012-05-20 18:05:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No... copy the byte */
|
|
|
|
|
|
|
|
priv->rxbuffer[priv->head] = ch;
|
|
|
|
priv->head = nexthead;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Was anything written? */
|
|
|
|
|
|
|
|
if (nwritten > 0)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2020-09-26 14:25:03 +02:00
|
|
|
/* Notify all poll/select waiters that they can read from the FIFO */
|
|
|
|
|
|
|
|
nxterm_pollnotify(priv, POLLIN);
|
|
|
|
|
2012-05-20 18:05:15 +02:00
|
|
|
for (i = 0; i < priv->nwaiters; i++)
|
|
|
|
{
|
2020-09-26 14:25:03 +02:00
|
|
|
/* Yes.. Notify all of the waiting readers that more data is
|
|
|
|
* available
|
|
|
|
*/
|
2012-05-20 18:05:15 +02:00
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->waitsem);
|
2012-05-20 18:05:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2012-05-20 18:05:15 +02:00
|
|
|
}
|
|
|
|
|
2014-09-20 22:18:08 +02:00
|
|
|
#endif /* CONFIG_NXTERM_NXKBDIN */
|