1
0
Fork 0
forked from nuttx/nuttx-update

sim/win/hostuart: only read key event from console

This commit will detect input events and filter out events(mouse/window/etc) other than key

Signed-off-by: chao an <anchao@xiaomi.com>
This commit is contained in:
chao an 2023-04-11 12:13:46 +08:00 committed by Xiang Xiao
parent 8b43096e28
commit 49c863f238

View file

@ -25,6 +25,12 @@
#include <stdbool.h>
#include <windows.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NUM_INPUT 16
/****************************************************************************
* Private Data
****************************************************************************/
@ -88,38 +94,6 @@ int host_uart_puts(int fd, const char *buf, size_t size)
return ret == 0 ? -EIO : nwritten;
}
/****************************************************************************
* Name: host_uart_getc
****************************************************************************/
int host_uart_gets(int fd, char *buf, size_t size)
{
DWORD nread;
int ret;
ret = ReadConsole(g_stdin_handle, buf, size, &nread, 0);
return ret == 0 ? -EIO : nread;
}
/****************************************************************************
* Name: host_uart_getcflag
****************************************************************************/
int host_uart_getcflag(int fd, unsigned int *cflag)
{
return -ENOSYS;
}
/****************************************************************************
* Name: host_uart_setcflag
****************************************************************************/
int host_uart_setcflag(int fd, unsigned int cflag)
{
return -ENOSYS;
}
/****************************************************************************
* Name: host_uart_checkin
****************************************************************************/
@ -144,3 +118,57 @@ bool host_uart_checkout(int fd)
{
return true;
}
/****************************************************************************
* Name: host_uart_getc
****************************************************************************/
int host_uart_gets(int fd, char *buf, size_t size)
{
INPUT_RECORD input[NUM_INPUT];
char *pos = buf;
DWORD ninput;
int i;
while (size > 0 && host_uart_checkin(fd))
{
ninput = size > NUM_INPUT ? NUM_INPUT : size;
if (ReadConsoleInput(g_stdin_handle,
(void *)&input, ninput, &ninput) <= 0 ||
ninput == 0)
{
break;
}
for (i = 0; i < ninput; i++)
{
if (input[i].EventType == KEY_EVENT &&
input[i].Event.KeyEvent.bKeyDown &&
input[i].Event.KeyEvent.uChar.AsciiChar != 0)
{
*pos++ = input[i].Event.KeyEvent.uChar.AsciiChar;
size--;
}
}
}
return pos == buf ? -EIO : pos - buf;
}
/****************************************************************************
* Name: host_uart_getcflag
****************************************************************************/
int host_uart_getcflag(int fd, unsigned int *cflag)
{
return -ENOSYS;
}
/****************************************************************************
* Name: host_uart_setcflag
****************************************************************************/
int host_uart_setcflag(int fd, unsigned int cflag)
{
return -ENOSYS;
}