This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 97752385b3f drivers/input: add mouse GRAB ioctl
97752385b3f is described below
commit 97752385b3f0364eefa2f23578d5029e52dd61bc
Author: rongyichang <[email protected]>
AuthorDate: Sun Jan 5 08:52:43 2025 +0800
drivers/input: add mouse GRAB ioctl
Add MSIOC_GRAB so an open mouse handle can receive subsequent
reports exclusively.
Signed-off-by: rongyichang <[email protected]>
---
drivers/input/mouse_upper.c | 110 +++++++++++++++++++++++++++++++++++---------
include/nuttx/input/mouse.h | 3 +-
2 files changed, 90 insertions(+), 23 deletions(-)
diff --git a/drivers/input/mouse_upper.c b/drivers/input/mouse_upper.c
index 25332332245..c9bd2793ca1 100644
--- a/drivers/input/mouse_upper.c
+++ b/drivers/input/mouse_upper.c
@@ -58,6 +58,7 @@ struct mouse_upperhalf_s
mutex_t lock; /* Manages exclusive access to this
structure */
struct list_node head; /* Opened file buffer chain header node
*/
FAR struct mouse_lowerhalf_s *lower; /* A pointer of lower half instance */
+ FAR struct mouse_openpriv_s *grab; /* A pointer of grab file */
};
/****************************************************************************
@@ -166,6 +167,11 @@ static int mouse_close(FAR struct file *filep)
return ret;
}
+ if (upper->grab == openpriv)
+ {
+ upper->grab = NULL;
+ }
+
if (lower->close && list_is_singular(&openpriv->node))
{
ret = lower->close(lower);
@@ -258,9 +264,10 @@ static ssize_t mouse_write(FAR struct file *filep, FAR
const char *buffer,
static int mouse_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
- FAR struct inode *inode = filep->f_inode;
- FAR struct mouse_upperhalf_s *upper = inode->i_private;
- FAR struct mouse_lowerhalf_s *lower = upper->lower;
+ FAR struct inode *inode = filep->f_inode;
+ FAR struct mouse_upperhalf_s *upper = inode->i_private;
+ FAR struct mouse_lowerhalf_s *lower = upper->lower;
+ FAR struct mouse_openpriv_s *openpriv = filep->f_priv;
int ret;
ret = nxmutex_lock(&upper->lock);
@@ -269,13 +276,49 @@ static int mouse_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
return ret;
}
- if (lower->control)
- {
- ret = lower->control(lower, cmd, arg);
- }
- else
+ switch (cmd)
{
- ret = -ENOTTY;
+ case MSIOC_GRAB:
+ {
+ int enable = (int)arg;
+
+ ret = OK;
+ if (enable)
+ {
+ if (upper->grab != NULL)
+ {
+ ret = -EBUSY;
+ }
+ else
+ {
+ upper->grab = openpriv;
+ }
+ }
+ else
+ {
+ if (upper->grab != openpriv)
+ {
+ ret = -EINVAL;
+ }
+ else
+ {
+ upper->grab = NULL;
+ }
+ }
+ }
+ break;
+ default:
+ {
+ if (lower->control)
+ {
+ ret = lower->control(lower, cmd, arg);
+ }
+ else
+ {
+ ret = -ENOTTY;
+ }
+ }
+ break;
}
nxmutex_unlock(&upper->lock);
@@ -330,6 +373,34 @@ errout:
return ret;
}
+/****************************************************************************
+ * Name: mouse_event_notify
+ ****************************************************************************/
+
+static void mouse_event_notify(FAR struct mouse_openpriv_s *openpriv,
+ FAR const struct mouse_report_s *sample)
+{
+ int semcount;
+
+ nxmutex_lock(&openpriv->lock);
+
+ circbuf_overwrite(&openpriv->circbuf, sample,
+ sizeof(struct mouse_report_s));
+
+ nxsem_get_value(&openpriv->waitsem, &semcount);
+ if (semcount < 1)
+ {
+ nxsem_post(&openpriv->waitsem);
+ }
+
+ if (openpriv->fds && openpriv->fds->fd >= 0)
+ {
+ poll_notify(&openpriv->fds, 1, POLLIN);
+ }
+
+ nxmutex_unlock(&openpriv->lock);
+}
+
/****************************************************************************
* Public Function
****************************************************************************/
@@ -342,27 +413,22 @@ void mouse_event(FAR void *priv, FAR const struct
mouse_report_s *sample)
{
FAR struct mouse_upperhalf_s *upper = priv;
FAR struct mouse_openpriv_s *openpriv;
- int semcount;
if (nxmutex_lock(&upper->lock) < 0)
{
return;
}
- list_for_every_entry(&upper->head, openpriv, struct mouse_openpriv_s, node)
+ if (upper->grab)
{
- circbuf_overwrite(&openpriv->circbuf, sample,
- sizeof(struct mouse_report_s));
-
- nxsem_get_value(&openpriv->waitsem, &semcount);
- if (semcount < 1)
- {
- nxsem_post(&openpriv->waitsem);
- }
-
- if (openpriv->fds && openpriv->fds->fd >= 0)
+ mouse_event_notify(upper->grab, sample);
+ }
+ else
+ {
+ list_for_every_entry(&upper->head, openpriv,
+ struct mouse_openpriv_s, node)
{
- poll_notify(&openpriv->fds, 1, POLLIN);
+ mouse_event_notify(openpriv, sample);
}
}
diff --git a/include/nuttx/input/mouse.h b/include/nuttx/input/mouse.h
index 86552ab8524..98ae6a1f55e 100644
--- a/include/nuttx/input/mouse.h
+++ b/include/nuttx/input/mouse.h
@@ -56,9 +56,10 @@
/* Common mouse IOCTL commands */
#define MSIOC_VENDOR _MSIOC(0x0001) /* Vendor-specific commands */
+#define MSIOC_GRAB _MSIOC(0x0002) /* Grab/Release mouse events */
#define MSC_FIRST 0x0001 /* First common command */
-#define MSC_NCMDS 1 /* One common commands */
+#define MSC_NCMDS 2 /* Two common commands */
/* Vendor-specific command structure
*