diff mbox series

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

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

Commit Message

songziming July 22, 2024, 9:52 a.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>
---

Changes in V2: only reset mode when console handle is valid

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

Comments

Michael Tokarev July 24, 2024, 4:47 a.m. UTC | #1
22.07.2024 12:52, songziming 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.

Is this a stable@ material?

Thanks,

/mjt
Marc-André Lureau July 24, 2024, 10:25 a.m. UTC | #2
Hi

On Wed, Jul 24, 2024 at 8:48 AM Michael Tokarev <mjt@tls.msk.ru> wrote:

> 22.07.2024 12:52, songziming 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.
>
> Is this a stable@ material?
>

It should be safe, but I don't think it deserves a stable backport: it
always had that behaviour before.
Michael Tokarev July 24, 2024, 4 p.m. UTC | #3
24.07.2024 13:25, Marc-André Lureau wrote:
> Hi
> 
> On Wed, Jul 24, 2024 at 8:48 AM Michael Tokarev <mjt@tls.msk.ru <mailto:mjt@tls.msk.ru>> wrote:
> 
>     22.07.2024 12:52, songziming 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.
> 
>     Is this a stable@ material?
> 
> 
> It should be safe, but I don't think it deserves a stable backport: it always had that behaviour before.

Well. "It was always wrong" does not mean it shouldn't be fixed, I'd say :)
In my view, a bug is a bug and it's okay to fix it even if it was here since
the day one.

Thanks,

/mjt
diff mbox series

Patch

diff --git a/chardev/char-win-stdio.c b/chardev/char-win-stdio.c
index 1a18999..13325ca 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,
@@ -221,6 +223,9 @@  static void char_win_stdio_finalize(Object *obj)
 {
     WinStdioChardev *stdio = WIN_STDIO_CHARDEV(obj);
 
+    if (stdio->hStdIn != INVALID_HANDLE_VALUE) {
+        SetConsoleMode(stdio->hStdIn, stdio->dwOldMode);
+    }
     if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
         CloseHandle(stdio->hInputReadyEvent);
     }