]> git.tdb.fi Git - libs/core.git/blob - source/io/windows/console.cpp
Implement controls for file descriptor inheritance
[libs/core.git] / source / io / windows / console.cpp
1 #include <msp/core/systemerror.h>
2 #include "console.h"
3 #include "handle_private.h"
4
5 using namespace std;
6
7 namespace {
8
9 DWORD stream_to_sys(Msp::IO::Console::Stream stream)
10 {
11         switch(stream)
12         {
13         case Msp::IO::Console::CIN: return STD_INPUT_HANDLE;
14         case Msp::IO::Console::COUT: return STD_OUTPUT_HANDLE;
15         case Msp::IO::Console::CERR: return STD_ERROR_HANDLE;
16         default: throw invalid_argument("stream_to_sys");
17         }
18 }
19
20 } // namespace
21
22
23 namespace Msp {
24 namespace IO {
25
26 void Console::platform_init()
27 {
28         *handle = GetStdHandle(stream_to_sys(stream));
29 }
30
31 Console::~Console()
32 { }
33
34 void Console::set_local_echo(bool e)
35 {
36         check_access(M_READ);
37
38         DWORD m;
39         GetConsoleMode(*handle, &m);
40         SetConsoleMode(*handle, (m&~ENABLE_ECHO_INPUT) | (e?ENABLE_ECHO_INPUT:0));
41 }
42
43 void Console::set_line_buffer(bool l)
44 {
45         check_access(M_READ);
46
47         DWORD m;
48         if(!GetConsoleMode(*handle, &m))
49                 throw system_error("GetConsoleMode");
50         if(!SetConsoleMode(*handle, (m&~ENABLE_LINE_INPUT) | (l?ENABLE_LINE_INPUT:0)))
51                 throw system_error("SetConsoleMode");
52 }
53
54 void Console::get_size(unsigned &rows, unsigned &cols)
55 {
56         check_access(M_WRITE);
57
58         CONSOLE_SCREEN_BUFFER_INFO sbi;
59         if(!GetConsoleScreenBufferInfo(*handle, &sbi))
60                 throw system_error("GetConsoleScreenBufferInfo");
61         // Right/bottom coords are inclusive
62         rows = sbi.srWindow.Bottom+1-sbi.srWindow.Top;
63         cols = sbi.srWindow.Right+1-sbi.srWindow.Left;
64 }
65
66 void Console::redirect(Base &other)
67 {
68         SetStdHandle(stream_to_sys(stream), *other.get_handle(mode));
69 }
70
71 } // namespace IO
72 } // namespace Msp