This is an automated email from the ASF dual-hosted git repository. xiaoxiang 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 49c863f238 sim/win/hostuart: only read key event from console 49c863f238 is described below commit 49c863f238930de5f5b12fe8901d06e9335c7202 Author: chao an <anc...@xiaomi.com> AuthorDate: Tue Apr 11 12:13:46 2023 +0800 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 <anc...@xiaomi.com> --- arch/sim/src/sim/win/sim_hostuart.c | 78 +++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/arch/sim/src/sim/win/sim_hostuart.c b/arch/sim/src/sim/win/sim_hostuart.c index 2dad21907a..430a2aa18d 100644 --- a/arch/sim/src/sim/win/sim_hostuart.c +++ b/arch/sim/src/sim/win/sim_hostuart.c @@ -25,6 +25,12 @@ #include <stdbool.h> #include <windows.h> +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define NUM_INPUT 16 + /**************************************************************************** * Private Data ****************************************************************************/ @@ -89,58 +95,80 @@ int host_uart_puts(int fd, const char *buf, size_t size) } /**************************************************************************** - * Name: host_uart_getc + * Name: host_uart_checkin ****************************************************************************/ -int host_uart_gets(int fd, char *buf, size_t size) +bool host_uart_checkin(int fd) { - DWORD nread; - int ret; + DWORD size; - ret = ReadConsole(g_stdin_handle, buf, size, &nread, 0); + if (GetNumberOfConsoleInputEvents(g_stdin_handle, &size) && size > 1) + { + return true; + } - return ret == 0 ? -EIO : nread; + return false; } /**************************************************************************** - * Name: host_uart_getcflag + * Name: host_uart_checkout ****************************************************************************/ -int host_uart_getcflag(int fd, unsigned int *cflag) +bool host_uart_checkout(int fd) { - return -ENOSYS; + return true; } /**************************************************************************** - * Name: host_uart_setcflag + * Name: host_uart_getc ****************************************************************************/ -int host_uart_setcflag(int fd, unsigned int cflag) +int host_uart_gets(int fd, char *buf, size_t size) { - return -ENOSYS; + 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_checkin + * Name: host_uart_getcflag ****************************************************************************/ -bool host_uart_checkin(int fd) +int host_uart_getcflag(int fd, unsigned int *cflag) { - DWORD size; - - if (GetNumberOfConsoleInputEvents(g_stdin_handle, &size) && size > 1) - { - return true; - } - - return false; + return -ENOSYS; } /**************************************************************************** - * Name: host_uart_checkout + * Name: host_uart_setcflag ****************************************************************************/ -bool host_uart_checkout(int fd) +int host_uart_setcflag(int fd, unsigned int cflag) { - return true; + return -ENOSYS; }