From a210978161878fb2b19f5194e72d3fdbea648b24 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Mon, 10 Oct 2016 09:14:51 +0300 Subject: [PATCH] cleanup console state after console app exiting --- dlangui-monod-linux.dproj | 17 +++++++++ examples/dmledit/dmledit-monod-linux.dproj | 16 +++++++++ examples/example1/example1-monod-linux.dproj | 16 +++++++++ .../helloworld/helloworld-monod-linux.dproj | 10 ++++++ examples/tetris/tetris-monod-linux.dproj | 16 +++++++++ src/dlangui/platforms/console/consoleapp.d | 7 ++++ src/dlangui/platforms/console/dconsole.d | 36 +++++++++++++++++-- 7 files changed, 115 insertions(+), 3 deletions(-) diff --git a/dlangui-monod-linux.dproj b/dlangui-monod-linux.dproj index f57f780b..57f959d9 100644 --- a/dlangui-monod-linux.dproj +++ b/dlangui-monod-linux.dproj @@ -259,6 +259,7 @@ USE_CONSOLE + EmbedStandardResources obj/DebugConsole @@ -268,6 +269,22 @@ -Jviews/res/i18n -Jviews/res/shaders -Jviews/res/mdpi +-Jviews/res/hdpi + false + libdlangui-monod-linux + StaticLibrary + true + 0 + + + bin\DebugConsole + obj/DebugConsole + false + -Jviews +-Jviews/res +-Jviews/res/i18n +-Jviews/res/shaders +-Jviews/res/mdpi -Jviews/res/hdpi false libdlangui-monod-linux diff --git a/examples/dmledit/dmledit-monod-linux.dproj b/examples/dmledit/dmledit-monod-linux.dproj index 71a953af..91296430 100644 --- a/examples/dmledit/dmledit-monod-linux.dproj +++ b/examples/dmledit/dmledit-monod-linux.dproj @@ -265,6 +265,22 @@ + + bin\DebugConsole + obj/DebugConsole + false + -Jviews +-Jviews/res +-Jviews/res/hdpi +-Jviews/res/mdpi +-Jviews/res/i18n + + false + dmledit-monod-linux + Executable + true + 0 + diff --git a/examples/example1/example1-monod-linux.dproj b/examples/example1/example1-monod-linux.dproj index 00ad762e..a9a8bd92 100644 --- a/examples/example1/example1-monod-linux.dproj +++ b/examples/example1/example1-monod-linux.dproj @@ -258,6 +258,22 @@ -Jviews/res/hdpi -Jviews/res/mdpi -Jviews/res/i18n + + false + example1-monod-linux + Executable + true + 0 + + + bin\DebugConsole + obj/DebugConsole + false + -Jviews +-Jviews/res +-Jviews/res/hdpi +-Jviews/res/mdpi +-Jviews/res/i18n false example1-monod-linux diff --git a/examples/helloworld/helloworld-monod-linux.dproj b/examples/helloworld/helloworld-monod-linux.dproj index 763faf9f..a60110a4 100644 --- a/examples/helloworld/helloworld-monod-linux.dproj +++ b/examples/helloworld/helloworld-monod-linux.dproj @@ -202,6 +202,16 @@ true 0 + + bin\DebugConsole + obj/DebugConsole + false + false + helloworld-monod-linux + Executable + true + 0 + diff --git a/examples/tetris/tetris-monod-linux.dproj b/examples/tetris/tetris-monod-linux.dproj index 2b675a37..fb02e3b7 100644 --- a/examples/tetris/tetris-monod-linux.dproj +++ b/examples/tetris/tetris-monod-linux.dproj @@ -256,6 +256,22 @@ -Jviews/res/hdpi -Jviews/res/mdpi -Jviews/res/i18n + + false + tetris-monod-linux + Executable + true + 0 + + + bin\DebugConsole + obj/DebugConsole + false + -Jviews +-Jviews/res +-Jviews/res/hdpi +-Jviews/res/mdpi +-Jviews/res/i18n false tetris-monod-linux diff --git a/src/dlangui/platforms/console/consoleapp.d b/src/dlangui/platforms/console/consoleapp.d index 5cba1658..d8ea0355 100644 --- a/src/dlangui/platforms/console/consoleapp.d +++ b/src/dlangui/platforms/console/consoleapp.d @@ -71,6 +71,9 @@ class ConsolePlatform : Platform { _console.setCursorType(ConsoleCursorType.Invisible); _drawBuf = new ConsoleDrawBuf(_console); } + ~this() { + destroy(_drawBuf); + } ConsoleWindow[] _windowList; @@ -238,6 +241,8 @@ class ConsoleDrawBuf : DrawBuf { } ~this() { + Log.d("Calling console.uninit"); + _console.uninit(); } /// returns current width @@ -435,6 +440,8 @@ extern(C) int DLANGUImain(string[] args) { Log.e("UIAppMain exception: ", e); } + Platform.setInstance(null); + releaseResourcesOnAppExit(); Log.d("Exiting main"); diff --git a/src/dlangui/platforms/console/dconsole.d b/src/dlangui/platforms/console/dconsole.d index c318db3e..0d40512d 100644 --- a/src/dlangui/platforms/console/dconsole.d +++ b/src/dlangui/platforms/console/dconsole.d @@ -284,6 +284,36 @@ class Console { } } + version (Windows) { + DWORD savedStdinMode; + DWORD savedStdoutMode; + } else { + import core.sys.posix.termios; + import core.sys.posix.fcntl; + import core.sys.posix.sys.ioctl; + termios savedStdinState; + } + + void uninit() { + version (Windows) { + SetConsoleMode(_hstdin, savedStdinMode); + SetConsoleMode(_hstdout, savedStdoutMode); + } else { + import core.sys.posix.unistd; + tcsetattr(STDIN_FILENO, TCSANOW, &savedStdinState); + // reset terminal state + rawWrite("\033c"); + // reset attributes + rawWrite("\x1b[0m"); + // clear screen + rawWrite("\033[2J"); + // normal cursor + rawWrite("\x1b[?25h"); + // set auto wrapping mode + rawWrite("\x1b[?7h"); + } + } + bool init() { version(Windows) { _hstdin = GetStdHandle(STD_INPUT_HANDLE); @@ -311,6 +341,7 @@ class Console { immutable DWORD ENABLE_LVB_GRID_WORLDWIDE = 0x0010; DWORD mode = 0; GetConsoleMode(_hstdin, &mode); + savedStdinMode = mode; mode = mode & ~ENABLE_ECHO_INPUT; mode = mode & ~ENABLE_LINE_INPUT; mode = mode & ~ENABLE_QUICK_EDIT_MODE; @@ -319,6 +350,7 @@ class Console { mode |= ENABLE_WINDOW_INPUT; SetConsoleMode(_hstdin, mode); GetConsoleMode(_hstdout, &mode); + savedStdoutMode = mode; mode = mode & ~ENABLE_PROCESSED_OUTPUT; mode = mode & ~ENABLE_WRAP_AT_EOL_OUTPUT; mode = mode & ~ENABLE_VIRTUAL_TERMINAL_PROCESSING; @@ -336,9 +368,6 @@ class Console { //writeln("csbi=", csbi); } else { import core.sys.posix.unistd; - import core.sys.posix.fcntl; - import core.sys.posix.termios; - import core.sys.posix.sys.ioctl; if (!isatty(1)) return false; setSignalHandlers(); @@ -346,6 +375,7 @@ class Console { termios ttystate; //get the terminal state tcgetattr(STDIN_FILENO, &ttystate); + savedStdinState = ttystate; //turn off canonical mode ttystate.c_lflag &= ~ICANON; ttystate.c_lflag &= ~ECHO;