diff mbox series

chardev/char-win-stdio.c: restore old console mode

Message ID ME3P282MB25482B87915BEBE85CCE7E898CAF2@ME3P282MB2548.AUSP282.PROD.OUTLOOK.COM
State New
Headers show
Series chardev/char-win-stdio.c: restore old console mode | expand

Commit Message

songziming July 21, 2024, 6:36 p.m. UTC
If I use `-serial stdio` on Windows, after QEMU exits, the terminal
could not handle arrow keys and tab any more. Because stdio backend
on Windows sets console mode to virtual terminal input when starts,
but does not restore the old mode when finalize.

This small patch saves the old console mode and set it back.

Signed-off-by: Ziming Song <s.ziming@hotmail.com>

---
 chardev/char-win-stdio.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Marc-André Lureau July 22, 2024, 7:09 a.m. UTC | #1
Hi

On Mon, Jul 22, 2024 at 12:01 AM songziming <s.ziming@hotmail.com> wrote:

> If I use `-serial stdio` on Windows, after QEMU exits, the terminal
> could not handle arrow keys and tab any more. Because stdio backend
> on Windows sets console mode to virtual terminal input when starts,
> but does not restore the old mode when finalize.
>
> This small patch saves the old console mode and set it back.
>

Thanks, we had a similar patch from Irina Ryapolova, but it didn't save the
old mode and she didn't update it.


> Signed-off-by: Ziming Song <s.ziming@hotmail.com>
>

> ---
>  chardev/char-win-stdio.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/chardev/char-win-stdio.c b/chardev/char-win-stdio.c
> index 1a18999..5b24893 100644
> --- a/chardev/char-win-stdio.c
> +++ b/chardev/char-win-stdio.c
> @@ -33,6 +33,7 @@
>  struct WinStdioChardev {
>      Chardev parent;
>      HANDLE  hStdIn;
> +    DWORD   dwOldMode;
>      HANDLE  hInputReadyEvent;
>      HANDLE  hInputDoneEvent;
>      HANDLE  hInputThread;
> @@ -159,6 +160,7 @@ static void qemu_chr_open_stdio(Chardev *chr,
>      }
>
>      is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
> +    stdio->dwOldMode = dwMode;
>
>      if (is_console) {
>          if (qemu_add_wait_object(stdio->hStdIn,
> @@ -220,6 +222,7 @@ err1:
>  static void char_win_stdio_finalize(Object *obj)
>  {
>      WinStdioChardev *stdio = WIN_STDIO_CHARDEV(obj);
> +    SetConsoleMode(stdio->hStdIn, stdio->dwOldMode);
>
>
It should not reset if the open callback was not called successfully.

You can check if it's the case by adding a if (stdio->hStdIn !=
INVALID_HANDLE_VALUE) condition.

     if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
>          CloseHandle(stdio->hInputReadyEvent);
> --
> 2.45.1.windows.1
>
>
>
diff mbox series

Patch

diff --git a/chardev/char-win-stdio.c b/chardev/char-win-stdio.c
index 1a18999..5b24893 100644
--- a/chardev/char-win-stdio.c
+++ b/chardev/char-win-stdio.c
@@ -33,6 +33,7 @@ 
 struct WinStdioChardev {
     Chardev parent;
     HANDLE  hStdIn;
+    DWORD   dwOldMode;
     HANDLE  hInputReadyEvent;
     HANDLE  hInputDoneEvent;
     HANDLE  hInputThread;
@@ -159,6 +160,7 @@  static void qemu_chr_open_stdio(Chardev *chr,
     }
 
     is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
+    stdio->dwOldMode = dwMode;
 
     if (is_console) {
         if (qemu_add_wait_object(stdio->hStdIn,
@@ -220,6 +222,7 @@  err1:
 static void char_win_stdio_finalize(Object *obj)
 {
     WinStdioChardev *stdio = WIN_STDIO_CHARDEV(obj);
+    SetConsoleMode(stdio->hStdIn, stdio->dwOldMode);
 
     if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
         CloseHandle(stdio->hInputReadyEvent);