-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.cpp
More file actions
94 lines (77 loc) · 2.04 KB
/
console.cpp
File metadata and controls
94 lines (77 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "CAE/Console.hpp"
namespace cae
{
void Console::create()
{
#ifdef _WIN32
if (!GetConsoleWindow())
{
AllocConsole();
}
consoleHwnd_ = GetConsoleWindow();
FILE* fp;
freopen_s(&fp, "CONOUT$", "w", stdout);
freopen_s(&fp, "CONOUT$", "w", stderr);
freopen_s(&fp, "CONIN$", "r", stdin);
std::ios::sync_with_stdio(true);
SetConsoleTitleA("CAE Console");
utl::Logger::addSink([](const std::string& msg, utl::LogLevel) {
std::cout << msg << std::endl;
});
#endif
}
void Console::startInput()
{
running_ = true;
inputThread_ = std::thread(&Console::consoleLoop, this);
}
void Console::stop()
{
running_ = false;
if (inputThread_.joinable())
inputThread_.join();
}
void Console::consoleLoop()
{
std::string line;
while (running_)
{
std::cout << "> " << std::flush;
if (!std::getline(std::cin, line))
break;
handleCommand(line);
}
}
void Console::handleCommand(const std::string& cmd)
{
if (cmd == "exit")
utl::Logger::log("Exit command received", utl::LogLevel::INFO);
else
utl::Logger::log("Unknown command: " + cmd, utl::LogLevel::WARNING);
}
void Console::attachTo(const NativeWindowHandle& handle)
{
#ifdef _WIN32
HWND app = static_cast<HWND>(handle.window);
if (!app || !consoleHwnd_) return;
RECT r;
GetWindowRect(app, &r);
MoveWindow(
consoleHwnd_,
r.right,
r.top,
500,
r.bottom - r.top,
TRUE
);
#endif
}
void Console::syncWith(const NativeWindowHandle& handle)
{
#ifdef _WIN32
HWND app = static_cast<HWND>(handle.window);
if (!app || !consoleHwnd_) return;
ShowWindow(consoleHwnd_, IsIconic(app) ? SW_MINIMIZE : SW_SHOW);
#endif
}
} // namespace cae