cleanup console state after console app exiting

This commit is contained in:
Vadim Lopatin 2016-10-10 09:14:51 +03:00
parent c5e2636157
commit a210978161
7 changed files with 115 additions and 3 deletions

View File

@ -259,6 +259,7 @@
<VersionIds>
<VersionIds>
<String>USE_CONSOLE</String>
<String>EmbedStandardResources</String>
</VersionIds>
</VersionIds>
<ObjectsDirectory>obj/DebugConsole</ObjectsDirectory>
@ -268,6 +269,22 @@
-Jviews/res/i18n
-Jviews/res/shaders
-Jviews/res/mdpi
-Jviews/res/hdpi</ExtraCompilerArguments>
<UnittestMode>false</UnittestMode>
<OutputName>libdlangui-monod-linux</OutputName>
<Target>StaticLibrary</Target>
<Externalconsole>true</Externalconsole>
<DebugLevel>0</DebugLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugConsole|AnyCPU' ">
<OutputPath>bin\DebugConsole</OutputPath>
<ObjectsDirectory>obj/DebugConsole</ObjectsDirectory>
<LinkinThirdPartyLibraries>false</LinkinThirdPartyLibraries>
<ExtraCompilerArguments>-Jviews
-Jviews/res
-Jviews/res/i18n
-Jviews/res/shaders
-Jviews/res/mdpi
-Jviews/res/hdpi</ExtraCompilerArguments>
<UnittestMode>false</UnittestMode>
<OutputName>libdlangui-monod-linux</OutputName>

View File

@ -265,6 +265,22 @@
</VersionIds>
</VersionIds>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugConsole|AnyCPU' ">
<OutputPath>bin\DebugConsole</OutputPath>
<ObjectsDirectory>obj/DebugConsole</ObjectsDirectory>
<LinkinThirdPartyLibraries>false</LinkinThirdPartyLibraries>
<ExtraCompilerArguments>-Jviews
-Jviews/res
-Jviews/res/hdpi
-Jviews/res/mdpi
-Jviews/res/i18n
</ExtraCompilerArguments>
<UnittestMode>false</UnittestMode>
<OutputName>dmledit-monod-linux</OutputName>
<Target>Executable</Target>
<Externalconsole>true</Externalconsole>
<DebugLevel>0</DebugLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="src\dmledit.d" />
</ItemGroup>

View File

@ -258,6 +258,22 @@
-Jviews/res/hdpi
-Jviews/res/mdpi
-Jviews/res/i18n
</ExtraCompilerArguments>
<UnittestMode>false</UnittestMode>
<OutputName>example1-monod-linux</OutputName>
<Target>Executable</Target>
<Externalconsole>true</Externalconsole>
<DebugLevel>0</DebugLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugConsole|AnyCPU' ">
<OutputPath>bin\DebugConsole</OutputPath>
<ObjectsDirectory>obj/DebugConsole</ObjectsDirectory>
<LinkinThirdPartyLibraries>false</LinkinThirdPartyLibraries>
<ExtraCompilerArguments>-Jviews
-Jviews/res
-Jviews/res/hdpi
-Jviews/res/mdpi
-Jviews/res/i18n
</ExtraCompilerArguments>
<UnittestMode>false</UnittestMode>
<OutputName>example1-monod-linux</OutputName>

View File

@ -202,6 +202,16 @@
<Externalconsole>true</Externalconsole>
<DebugLevel>0</DebugLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugConsole|AnyCPU' ">
<OutputPath>bin\DebugConsole</OutputPath>
<ObjectsDirectory>obj/DebugConsole</ObjectsDirectory>
<LinkinThirdPartyLibraries>false</LinkinThirdPartyLibraries>
<UnittestMode>false</UnittestMode>
<OutputName>helloworld-monod-linux</OutputName>
<Target>Executable</Target>
<Externalconsole>true</Externalconsole>
<DebugLevel>0</DebugLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="src\helloworld.d" />
</ItemGroup>

View File

@ -256,6 +256,22 @@
-Jviews/res/hdpi
-Jviews/res/mdpi
-Jviews/res/i18n
</ExtraCompilerArguments>
<UnittestMode>false</UnittestMode>
<OutputName>tetris-monod-linux</OutputName>
<Target>Executable</Target>
<Externalconsole>true</Externalconsole>
<DebugLevel>0</DebugLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugConsole|AnyCPU' ">
<OutputPath>bin\DebugConsole</OutputPath>
<ObjectsDirectory>obj/DebugConsole</ObjectsDirectory>
<LinkinThirdPartyLibraries>false</LinkinThirdPartyLibraries>
<ExtraCompilerArguments>-Jviews
-Jviews/res
-Jviews/res/hdpi
-Jviews/res/mdpi
-Jviews/res/i18n
</ExtraCompilerArguments>
<UnittestMode>false</UnittestMode>
<OutputName>tetris-monod-linux</OutputName>

View File

@ -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");

View File

@ -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;