bug-ncurses
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/1] EXP_WIN32_DRIVER: Fix console restore on exit.


From: Daniel Starke
Subject: [PATCH 1/1] EXP_WIN32_DRIVER: Fix console restore on exit.
Date: Mon, 24 Mar 2025 21:17:39 +0100

Using the experimental WIN32 driver does currently not restore the scroll
buffer and console correctly on exit as no such function is implemented.
Furthermore, the console mode is left in VT100 mode and other settings like
mouse are not reverted.

Fix this by:
a) setting the actually given values in _nc_console_setmode() instead of always
   applying the defaults and
b) calling the fixed restore_original_screen() function when exiting ncurses.

Note that mvcur() is not available in restore_original_screen() when it is
called anymore. Use the direct function SetConsoleCursorPosition() instead
here.

Signed-off-by: Daniel Starke <daniel-email@gmx.net>
---
 include/nc_win32.h           |  1 +
 ncurses/tinfo/lib_win32con.c | 36 +++++++++++++++++-------------------
 ncurses/tinfo/tinfo_driver.c |  8 ++++++++
 3 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/include/nc_win32.h b/include/nc_win32.h
index edfb84f0..ae90d642 100644
--- a/include/nc_win32.h
+++ b/include/nc_win32.h
@@ -109,6 +109,7 @@ extern NCURSES_EXPORT(int)    _nc_console_testmouse(const 
SCREEN *,HANDLE,int EV
 extern NCURSES_EXPORT(int)    _nc_console_keyok(int keycode,int flag);
 extern NCURSES_EXPORT(bool)   _nc_console_keyExist(int keycode);
 extern NCURSES_EXPORT(bool)   _nc_console_checkinit(bool initFlag, bool 
assumeTermInfo);
+extern NCURSES_EXPORT(bool)   _nc_console_restore(void);
 extern NCURSES_EXPORT(int)    _nc_console_vt_supported(void);

 #ifdef _NC_CHECK_MINTTY
diff --git a/ncurses/tinfo/lib_win32con.c b/ncurses/tinfo/lib_win32con.c
index 34630af0..ea66bf3a 100644
--- a/ncurses/tinfo/lib_win32con.c
+++ b/ncurses/tinfo/lib_win32con.c
@@ -259,36 +259,20 @@ _nc_console_setmode(HANDLE hdl, const TTY * arg)
        T(("lib_win32con:_nc_console_setmode %s", _nc_trace_ttymode(arg)));
        if (hdl == WINCONSOLE.inp) {
            dwFlag = arg->dwFlagIn | ENABLE_MOUSE_INPUT | VT_FLAG_IN;
-           if (WINCONSOLE.isTermInfoConsole)
-               dwFlag |= (VT_FLAG_IN);
-           else
-               dwFlag &= (DWORD) ~ (VT_FLAG_IN);
            TRCTTYIN(dwFlag);
            SetConsoleMode(hdl, dwFlag);

            alt = OutHandle();
            dwFlag = arg->dwFlagOut;
-           if (WINCONSOLE.isTermInfoConsole)
-               dwFlag |= (VT_FLAG_OUT);
-           else
-               dwFlag |= (VT_FLAG_OUT);
            TRCTTYOUT(dwFlag);
            SetConsoleMode(alt, dwFlag);
        } else {
            dwFlag = arg->dwFlagOut;
-           if (WINCONSOLE.isTermInfoConsole)
-               dwFlag |= (VT_FLAG_OUT);
-           else
-               dwFlag |= (VT_FLAG_OUT);
            TRCTTYOUT(dwFlag);
            SetConsoleMode(hdl, dwFlag);

            alt = WINCONSOLE.inp;
            dwFlag = arg->dwFlagIn | ENABLE_MOUSE_INPUT;
-           if (WINCONSOLE.isTermInfoConsole)
-               dwFlag |= (VT_FLAG_IN);
-           else
-               dwFlag &= (DWORD) ~ (VT_FLAG_IN);
            TRCTTYIN(dwFlag);
            SetConsoleMode(alt, dwFlag);
            T(("effective mode set %s", _nc_trace_ttymode(&TRCTTY)));
@@ -404,7 +388,6 @@ save_original_screen(void)
     return result;
 }

-#if 0
 static bool
 restore_original_screen(void)
 {
@@ -426,7 +409,7 @@ restore_original_screen(void)
                     bufferCoord,
                     &save_region)) {
        result = TRUE;
-       mvcur(-1, -1, LINES - 2, 0);
+       SetConsoleCursorPosition(WINCONSOLE.hdl, 
WINCONSOLE.save_SBI.dwCursorPosition);
        T(("... restore original screen contents ok %dx%d (%d,%d - %d,%d)",
           WINCONSOLE.save_size.Y,
           WINCONSOLE.save_size.X,
@@ -439,7 +422,6 @@ restore_original_screen(void)
     }
     return result;
 }
-#endif

 static bool
 read_screen_data(void)
@@ -1248,4 +1230,20 @@ _nc_console_checkinit(bool initFlag, bool assumeTermInfo)
     returnBool(res);
 }

+NCURSES_EXPORT(bool)
+_nc_console_restore(void)
+{
+    bool res = FALSE;
+    if (WINCONSOLE.hdl != INVALID_HANDLE_VALUE) {
+       res = TRUE;
+       if (!WINCONSOLE.buffered) {
+           _nc_console_set_scrollback(TRUE, &WINCONSOLE.save_SBI);
+           if (!restore_original_screen())
+               res = FALSE;
+       }
+       SetConsoleCursorInfo(WINCONSOLE.hdl, &WINCONSOLE.save_CI);
+    }
+    returnBool(res);
+}
+
 #endif // _NC_WINDOWS
diff --git a/ncurses/tinfo/tinfo_driver.c b/ncurses/tinfo/tinfo_driver.c
index 3bdd8124..f71d6842 100644
--- a/ncurses/tinfo/tinfo_driver.c
+++ b/ncurses/tinfo/tinfo_driver.c
@@ -628,6 +628,10 @@ drv_mode(TERMINAL_CONTROL_BLOCK * TCB, int progFlag, int 
defFlag)
                    if (sp->_keypad_on)
                        _nc_keypad(sp, TRUE);
                }
+#if defined(EXP_WIN32_DRIVER)
+               if (!WINCONSOLE.buffered)
+                   _nc_console_set_scrollback(FALSE, &WINCONSOLE.SBI);
+#endif
                code = OK;
            }
        }
@@ -656,6 +660,10 @@ drv_mode(TERMINAL_CONTROL_BLOCK * TCB, int progFlag, int 
defFlag)
                NCURSES_SP_NAME(_nc_flush) (sp);
            }
            code = drv_sgmode(TCB, TRUE, &(_term->Ottyb));
+#if defined(EXP_WIN32_DRIVER)
+           if (!_nc_console_restore())
+               code = ERR;
+#endif
        }
     }
     return (code);
--
2.39.5




reply via email to

[Prev in Thread] Current Thread [Next in Thread]