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