* graphics/nxterm/nxterm_kbdin.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <inttypes.h>
#include <fcntl.h>
#include <sched.h>
#include <assert.h>
#include <poll.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/spinlock.h>
#include "nxterm.h"
#ifdef CONFIG_NXTERM_NXKBDIN
* Private Functions
****************************************************************************/
* Name: nxterm_pollnotify
****************************************************************************/
static void nxterm_pollnotify(FAR struct nxterm_state_s *priv,
pollevent_t eventset)
{
irqstate_t flags;
flags = spin_lock_irqsave_nopreempt(&priv->spinlock);
poll_notify(priv->fds, CONFIG_NXTERM_NPOLLWAITERS, eventset);
spin_unlock_irqrestore_nopreempt(&priv->spinlock, flags);
}
* Public Functions
****************************************************************************/
* Name: nxterm_read
*
* Description:
* The optional NxTerm read method
*
****************************************************************************/
ssize_t nxterm_read(FAR struct file *filep, FAR char *buffer, size_t len)
{
FAR struct nxterm_state_s *priv;
ssize_t nread;
char ch;
int ret;
DEBUGASSERT(filep->f_priv);
priv = (FAR struct nxterm_state_s *)filep->f_priv;
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
gerr("ERROR: nxmutex_lock failed\n");
return ret;
}
for (nread = 0; nread < len; )
{
if (priv->head == priv->tail)
{
if (nread > 0)
{
break;
}
* don't wait. Just return EGAIN.
*/
if (filep->f_oflags & O_NONBLOCK)
{
nread = -EAGAIN;
break;
}
* buffer. Increment the number of waiters so that the
* nxterm_write() will not that it needs to post the semaphore
* to wake us up.
*/
priv->nwaiters++;
nxmutex_unlock(&priv->lock);
* have already incremented nwaiters. Pre-emption is disabled
* but will be re-enabled while we are waiting.
*/
ret = nxsem_wait(&priv->waitsem);
if (ret >= 0)
{
ret = nxmutex_lock(&priv->lock);
}
priv->nwaiters--;
* mutual exclusion mutex?
*/
if (ret < 0)
{
gerr("ERROR: nxmutex_lock failed\n");
* we received the signal?
*/
if (ret != -EINTR || nread >= 0)
{
nread = ret;
}
* "break" out because whichever error occurred, we do not hold
* the exclusion mutex.
*/
goto errout_without_lock;
}
}
else
{
* tail index.
*/
ch = priv->rxbuffer[priv->tail];
if (++priv->tail >= CONFIG_NXTERM_KBDBUFSIZE)
{
priv->tail = 0;
}
buffer[nread] = ch;
nread++;
}
}
nxmutex_unlock(&priv->lock);
errout_without_lock:
if (nread > 0)
{
nxterm_pollnotify(priv, POLLOUT);
}
return nread;
}
* Name: nxterm_poll
****************************************************************************/
int nxterm_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
{
FAR struct inode *inode = filep->f_inode;
FAR struct nxterm_state_s *priv;
pollevent_t eventset;
irqstate_t flags;
int ret;
int i;
DEBUGASSERT(inode->i_private);
priv = inode->i_private;
flags = spin_lock_irqsave_nopreempt(&priv->spinlock);
if (setup)
{
* slot for the poll structure reference
*/
for (i = 0; i < CONFIG_NXTERM_NPOLLWAITERS; i++)
{
if (!priv->fds[i])
{
priv->fds[i] = fds;
fds->priv = &priv->fds[i];
break;
}
}
if (i >= CONFIG_NXTERM_NPOLLWAITERS)
{
gerr("ERROR: Too many poll waiters\n");
fds->priv = NULL;
ret = -EBUSY;
goto errout;
}
* This driver is always available for transmission.
*/
eventset = POLLOUT;
if (priv->head != priv->tail)
{
eventset |= POLLIN;
}
poll_notify(&fds, 1, eventset);
}
else if (fds->priv)
{
FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
#ifdef CONFIG_DEBUG_GRAPHICS
if (!slot)
{
gerr("ERROR: No slot\n");
ret = -EIO;
goto errout;
}
#endif
*slot = NULL;
fds->priv = NULL;
}
errout:
spin_unlock_irqrestore_nopreempt(&priv->spinlock, flags);
return ret;
}
* Name: nxterm_kbdin
*
* Description:
* This function should be driven by the window kbdin callback function
* (see nx.h). When the NxTerm is the top window and keyboard input is
* received on the top window, that window callback should be directed to
* this function. This function will buffer the keyboard data and makE
* it available to the NxTerm as stdin.
*
* If CONFIG_NXTERM_NXKBDIN is not selected, then the NxTerm will
* receive its input from stdin (/dev/console). This works great but
* cannot be shared between different windows. Chaos will ensue if you
* try to support multiple NxTerm windows without CONFIG_NXTERM_NXKBDIN
*
* 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
*
****************************************************************************/
void nxterm_kbdin(NXTERM handle, FAR const uint8_t *buffer, uint8_t buflen)
{
FAR struct nxterm_state_s *priv;
ssize_t nwritten;
int nexthead;
char ch;
int ret;
ginfo("buflen=%" PRId8 "\n", buflen);
DEBUGASSERT(handle);
priv = (FAR struct nxterm_state_s *)handle;
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
gerr("ERROR: nxmutex_lock failed\n");
return;
}
* 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.
*/
for (nwritten = 0; nwritten < buflen; nwritten++)
{
ch = buffer[nwritten];
* buffer
*/
nexthead = priv->head + 1;
if (nexthead >= CONFIG_NXTERM_KBDBUFSIZE)
{
nexthead = 0;
}
if (nexthead == priv->tail)
{
* the buffer.
*/
gerr("ERROR: Keyboard data overrun\n");
break;
}
priv->rxbuffer[priv->head] = ch;
priv->head = nexthead;
}
if (nwritten > 0)
{
int i;
nxterm_pollnotify(priv, POLLIN);
for (i = 0; i < priv->nwaiters; i++)
{
* available
*/
nxsem_post(&priv->waitsem);
}
}
nxmutex_unlock(&priv->lock);
}
#endif