2011-06-22 06:25:39 -06:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
2011-07-15 11:58:17 -06:00
|
|
|
Copyright (c) 2011 Petr Mrázek <peterix@gmail.com>
|
2011-06-22 06:25:39 -06:00
|
|
|
|
2011-07-15 11:58:17 -06:00
|
|
|
A thread-safe logging console with a line editor for windows.
|
2011-06-22 06:25:39 -06:00
|
|
|
|
2011-07-15 11:58:17 -06:00
|
|
|
Based on linenoise win32 port,
|
|
|
|
copyright 2010, Jon Griffiths <jon_p_griffiths at yahoo dot com>.
|
2011-07-14 03:15:23 -06:00
|
|
|
All rights reserved.
|
|
|
|
Based on linenoise, copyright 2010, Salvatore Sanfilippo <antirez at gmail dot com>.
|
|
|
|
The original linenoise can be found at: http://github.com/antirez/linenoise
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
to endorse or promote products derived from this software without
|
|
|
|
specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-06-22 06:25:39 -06:00
|
|
|
#include <windows.h>
|
|
|
|
#include <conio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2011-07-16 00:00:13 -06:00
|
|
|
#include <process.h>
|
2011-06-22 06:25:39 -06:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <istream>
|
|
|
|
#include <string>
|
|
|
|
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Console.h"
|
|
|
|
#include "Hooks.h"
|
2011-06-22 06:25:39 -06:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <sstream>
|
2011-07-13 20:05:27 -06:00
|
|
|
#include <deque>
|
2011-06-22 06:25:39 -06:00
|
|
|
using namespace DFHack;
|
|
|
|
|
2011-07-27 06:22:37 -06:00
|
|
|
#include "tinythread.h"
|
|
|
|
using namespace tthread;
|
|
|
|
|
2011-06-22 06:25:39 -06:00
|
|
|
// FIXME: maybe make configurable with an ini option?
|
2017-12-22 05:42:59 -07:00
|
|
|
#define MAX_CONSOLE_LINES 999
|
2011-06-22 06:25:39 -06:00
|
|
|
|
2011-07-13 20:05:27 -06:00
|
|
|
namespace DFHack
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
class Private
|
2011-07-13 20:05:27 -06:00
|
|
|
{
|
|
|
|
public:
|
2012-03-10 02:29:33 -07:00
|
|
|
Private()
|
2011-07-13 20:05:27 -06:00
|
|
|
{
|
|
|
|
dfout_C = 0;
|
|
|
|
rawmode = 0;
|
|
|
|
console_in = 0;
|
|
|
|
console_out = 0;
|
|
|
|
ConsoleWindow = 0;
|
|
|
|
default_attributes = 0;
|
2011-07-15 11:58:17 -06:00
|
|
|
state = con_unclaimed;
|
2012-03-10 02:29:33 -07:00
|
|
|
in_batch = false;
|
2011-07-15 11:58:17 -06:00
|
|
|
raw_cursor = 0;
|
2011-07-13 20:05:27 -06:00
|
|
|
};
|
2011-07-15 16:15:20 -06:00
|
|
|
virtual ~Private()
|
|
|
|
{
|
|
|
|
//sync();
|
|
|
|
}
|
|
|
|
public:
|
2012-03-10 02:29:33 -07:00
|
|
|
void print(const char *data)
|
2011-07-15 11:58:17 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
fputs(data, dfout_C);
|
2011-07-15 11:58:17 -06:00
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
|
|
|
|
void print_text(color_ostream::color_value clr, const std::string &chunk)
|
2011-07-15 11:58:17 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
if(!in_batch && state == con_lineedit)
|
2011-07-15 11:58:17 -06:00
|
|
|
{
|
|
|
|
clearline();
|
2012-03-10 02:29:33 -07:00
|
|
|
|
|
|
|
color(clr);
|
|
|
|
print(chunk.c_str());
|
|
|
|
|
|
|
|
reset_color();
|
2011-07-15 11:58:17 -06:00
|
|
|
prompt_refresh();
|
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
color(clr);
|
|
|
|
print(chunk.c_str());
|
|
|
|
}
|
2011-07-15 11:58:17 -06:00
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
|
|
|
|
void begin_batch()
|
2011-07-15 11:58:17 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
assert(!in_batch);
|
|
|
|
|
|
|
|
in_batch = true;
|
|
|
|
|
|
|
|
if (state == con_lineedit)
|
2011-07-15 11:58:17 -06:00
|
|
|
{
|
|
|
|
clearline();
|
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void end_batch()
|
|
|
|
{
|
|
|
|
assert(in_batch);
|
|
|
|
|
|
|
|
flush();
|
|
|
|
|
|
|
|
in_batch = false;
|
|
|
|
|
|
|
|
if (state == con_lineedit)
|
2011-07-15 11:58:17 -06:00
|
|
|
{
|
|
|
|
reset_color();
|
2012-03-10 02:29:33 -07:00
|
|
|
prompt_refresh();
|
2011-07-15 11:58:17 -06:00
|
|
|
}
|
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
|
|
|
|
void flush()
|
2011-07-15 11:58:17 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
fflush(dfout_C);
|
2011-07-15 11:58:17 -06:00
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
|
2011-07-15 11:58:17 -06:00
|
|
|
int get_columns(void)
|
|
|
|
{
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO inf = { 0 };
|
|
|
|
GetConsoleScreenBufferInfo(console_out, &inf);
|
|
|
|
return (size_t)inf.dwSize.X;
|
|
|
|
}
|
|
|
|
int get_rows(void)
|
|
|
|
{
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO inf = { 0 };
|
|
|
|
GetConsoleScreenBufferInfo(console_out, &inf);
|
|
|
|
return (size_t)inf.dwSize.Y;
|
|
|
|
}
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
system("cls");
|
|
|
|
}
|
|
|
|
void clearline()
|
|
|
|
{
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO inf = { 0 };
|
|
|
|
GetConsoleScreenBufferInfo(console_out, &inf);
|
|
|
|
// Blank to EOL
|
|
|
|
char* tmp = (char*)malloc(inf.dwSize.X);
|
|
|
|
memset(tmp, ' ', inf.dwSize.X);
|
2017-12-22 05:42:59 -07:00
|
|
|
blankout(tmp, inf.dwSize.X, 0, inf.dwCursorPosition.Y);
|
2011-07-15 11:58:17 -06:00
|
|
|
free(tmp);
|
|
|
|
COORD coord = {0, inf.dwCursorPosition.Y}; // Windows uses 0-based coordinates
|
|
|
|
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
|
|
|
|
}
|
|
|
|
void gotoxy(int x, int y)
|
|
|
|
{
|
2017-08-07 13:13:22 -06:00
|
|
|
COORD coord = {(SHORT)(x-1), (SHORT)(y-1)}; // Windows uses 0-based coordinates
|
2011-07-15 11:58:17 -06:00
|
|
|
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
|
|
|
|
}
|
|
|
|
|
|
|
|
void color(int index)
|
|
|
|
{
|
|
|
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
2012-08-18 23:21:25 -06:00
|
|
|
SetConsoleTextAttribute(hConsole, index == COLOR_RESET ? default_attributes : index);
|
2011-07-15 11:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset_color( void )
|
|
|
|
{
|
|
|
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
SetConsoleTextAttribute(hConsole, default_attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cursor(bool enable)
|
|
|
|
{
|
|
|
|
if(enable)
|
|
|
|
{
|
|
|
|
HANDLE hConsoleOutput;
|
|
|
|
CONSOLE_CURSOR_INFO structCursorInfo;
|
|
|
|
hConsoleOutput = GetStdHandle( STD_OUTPUT_HANDLE );
|
|
|
|
GetConsoleCursorInfo( hConsoleOutput, &structCursorInfo ); // Get current cursor size
|
|
|
|
structCursorInfo.bVisible = TRUE;
|
|
|
|
SetConsoleCursorInfo( hConsoleOutput, &structCursorInfo );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HANDLE hConsoleOutput;
|
|
|
|
CONSOLE_CURSOR_INFO structCursorInfo;
|
|
|
|
hConsoleOutput = GetStdHandle( STD_OUTPUT_HANDLE );
|
|
|
|
GetConsoleCursorInfo( hConsoleOutput, &structCursorInfo ); // Get current cursor size
|
|
|
|
structCursorInfo.bVisible = FALSE;
|
|
|
|
SetConsoleCursorInfo( hConsoleOutput, &structCursorInfo );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 05:42:59 -07:00
|
|
|
void blankout(const char* str, size_t len, int x, int y)
|
|
|
|
{
|
|
|
|
COORD pos = { (SHORT)x, (SHORT)y };
|
|
|
|
DWORD count = 0;
|
|
|
|
WriteConsoleOutputCharacterA(console_out, str, len, pos, &count);
|
|
|
|
}
|
|
|
|
|
2011-07-13 20:05:27 -06:00
|
|
|
void output(const char* str, size_t len, int x, int y)
|
|
|
|
{
|
|
|
|
COORD pos = { (SHORT)x, (SHORT)y };
|
|
|
|
DWORD count = 0;
|
2017-12-14 04:45:35 -07:00
|
|
|
CONSOLE_SCREEN_BUFFER_INFO inf = { 0 };
|
|
|
|
GetConsoleScreenBufferInfo(console_out, &inf);
|
|
|
|
SetConsoleCursorPosition(console_out, pos);
|
|
|
|
WriteConsoleA(console_out, str, len, &count, NULL);
|
2017-12-14 04:46:52 -07:00
|
|
|
}
|
2011-07-13 20:05:27 -06:00
|
|
|
|
2011-07-15 11:58:17 -06:00
|
|
|
void prompt_refresh()
|
|
|
|
{
|
|
|
|
size_t cols = get_columns();
|
|
|
|
size_t plen = prompt.size();
|
|
|
|
const char * buf = raw_buffer.c_str();
|
|
|
|
size_t len = raw_buffer.size();
|
2012-04-17 15:23:45 -06:00
|
|
|
int cooked_cursor = raw_cursor;
|
2011-07-15 11:58:17 -06:00
|
|
|
|
2012-04-17 15:23:45 -06:00
|
|
|
while ((plen + cooked_cursor) >= cols)
|
2011-07-15 11:58:17 -06:00
|
|
|
{
|
|
|
|
buf++;
|
|
|
|
len--;
|
2012-04-17 15:23:45 -06:00
|
|
|
cooked_cursor--;
|
2011-07-15 11:58:17 -06:00
|
|
|
}
|
|
|
|
while (plen + len > cols)
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO inf = { 0 };
|
|
|
|
GetConsoleScreenBufferInfo(console_out, &inf);
|
|
|
|
output(prompt.c_str(), plen, 0, inf.dwCursorPosition.Y);
|
|
|
|
output(buf, len, plen, inf.dwCursorPosition.Y);
|
|
|
|
if (plen + len < (size_t)inf.dwSize.X)
|
|
|
|
{
|
|
|
|
// Blank to EOL
|
|
|
|
char* tmp = (char*)malloc(inf.dwSize.X - (plen + len));
|
|
|
|
memset(tmp, ' ', inf.dwSize.X - (plen + len));
|
2017-12-22 05:42:59 -07:00
|
|
|
blankout(tmp, inf.dwSize.X - (plen + len), len + plen, inf.dwCursorPosition.Y);
|
2011-07-15 11:58:17 -06:00
|
|
|
free(tmp);
|
|
|
|
}
|
2012-04-17 15:23:45 -06:00
|
|
|
inf.dwCursorPosition.X = (SHORT)(cooked_cursor + plen);
|
2011-07-15 11:58:17 -06:00
|
|
|
SetConsoleCursorPosition(console_out, inf.dwCursorPosition);
|
|
|
|
}
|
|
|
|
|
2012-03-10 02:29:33 -07:00
|
|
|
int prompt_loop(recursive_mutex * lock, CommandHistory & history)
|
2011-07-15 11:58:17 -06:00
|
|
|
{
|
|
|
|
raw_buffer.clear(); // make sure the buffer is empty!
|
|
|
|
size_t plen = prompt.size();
|
|
|
|
raw_cursor = 0;
|
|
|
|
int history_index = 0;
|
|
|
|
// The latest history entry is always our current buffer, that
|
|
|
|
// initially is just an empty string.
|
|
|
|
const std::string empty;
|
2011-08-13 06:42:09 -06:00
|
|
|
history.add(empty);
|
2011-07-15 11:58:17 -06:00
|
|
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO inf = { 0 };
|
|
|
|
GetConsoleScreenBufferInfo(console_out, &inf);
|
|
|
|
size_t cols = inf.dwSize.X;
|
|
|
|
output(prompt.c_str(), plen, 0, inf.dwCursorPosition.Y);
|
|
|
|
inf.dwCursorPosition.X = (SHORT)plen;
|
|
|
|
SetConsoleCursorPosition(console_out, inf.dwCursorPosition);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
INPUT_RECORD rec;
|
|
|
|
DWORD count;
|
2011-07-27 06:22:37 -06:00
|
|
|
lock->unlock();
|
2018-07-07 15:50:43 -06:00
|
|
|
if (ReadConsoleInputA(console_in, &rec, 1, &count) == 0) {
|
2018-06-12 08:53:43 -06:00
|
|
|
lock->lock();
|
2018-07-04 06:21:25 -06:00
|
|
|
return Console::SHUTDOWN;
|
2018-06-12 08:53:43 -06:00
|
|
|
}
|
2011-07-27 06:22:37 -06:00
|
|
|
lock->lock();
|
2011-07-15 11:58:17 -06:00
|
|
|
if (rec.EventType != KEY_EVENT || !rec.Event.KeyEvent.bKeyDown)
|
|
|
|
continue;
|
|
|
|
switch (rec.Event.KeyEvent.wVirtualKeyCode)
|
|
|
|
{
|
|
|
|
case VK_RETURN: // enter
|
2011-08-13 06:42:09 -06:00
|
|
|
history.remove();
|
2011-07-15 11:58:17 -06:00
|
|
|
return raw_buffer.size();
|
|
|
|
case VK_BACK: // backspace
|
|
|
|
if (raw_cursor > 0 && raw_buffer.size() > 0)
|
|
|
|
{
|
|
|
|
raw_buffer.erase(raw_cursor-1,1);
|
|
|
|
raw_cursor--;
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VK_LEFT: // left arrow
|
|
|
|
if (raw_cursor > 0)
|
|
|
|
{
|
|
|
|
raw_cursor--;
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VK_RIGHT: // right arrow
|
|
|
|
if (raw_cursor != raw_buffer.size())
|
|
|
|
{
|
|
|
|
raw_cursor++;
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VK_UP:
|
|
|
|
case VK_DOWN:
|
|
|
|
// up and down arrow: history
|
|
|
|
if (history.size() > 1)
|
|
|
|
{
|
|
|
|
// Update the current history entry before to
|
|
|
|
// overwrite it with tne next one.
|
|
|
|
history[history_index] = raw_buffer;
|
|
|
|
// Show the new entry
|
|
|
|
history_index += (rec.Event.KeyEvent.wVirtualKeyCode == VK_UP) ? 1 : -1;
|
|
|
|
if (history_index < 0)
|
|
|
|
{
|
|
|
|
history_index = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (history_index >= history.size())
|
|
|
|
{
|
|
|
|
history_index = history.size()-1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
raw_buffer = history[history_index];
|
|
|
|
raw_cursor = raw_buffer.size();
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VK_DELETE:
|
|
|
|
// delete
|
|
|
|
if (raw_buffer.size() > 0 && raw_cursor < raw_buffer.size())
|
|
|
|
{
|
|
|
|
raw_buffer.erase(raw_cursor,1);
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VK_HOME:
|
|
|
|
raw_cursor = 0;
|
|
|
|
prompt_refresh();
|
|
|
|
break;
|
|
|
|
case VK_END:
|
|
|
|
raw_cursor = raw_buffer.size();
|
|
|
|
prompt_refresh();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (rec.Event.KeyEvent.uChar.AsciiChar < ' ' ||
|
|
|
|
rec.Event.KeyEvent.uChar.AsciiChar > '~')
|
|
|
|
continue;
|
|
|
|
if (raw_buffer.size() == raw_cursor)
|
|
|
|
raw_buffer.append(1,rec.Event.KeyEvent.uChar.AsciiChar);
|
|
|
|
else
|
|
|
|
raw_buffer.insert(raw_cursor,1,rec.Event.KeyEvent.uChar.AsciiChar);
|
|
|
|
raw_cursor++;
|
|
|
|
prompt_refresh();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
int lineedit(const std::string & prompt, std::string & output, recursive_mutex * lock, CommandHistory & ch)
|
2011-07-15 11:58:17 -06:00
|
|
|
{
|
2020-12-03 20:04:00 -07:00
|
|
|
if(state == con_lineedit)
|
|
|
|
return Console::FAILURE;
|
2011-07-15 11:58:17 -06:00
|
|
|
output.clear();
|
2012-03-10 02:29:33 -07:00
|
|
|
reset_color();
|
2011-07-15 11:58:17 -06:00
|
|
|
int count;
|
|
|
|
state = con_lineedit;
|
|
|
|
this->prompt = prompt;
|
2011-08-13 06:42:09 -06:00
|
|
|
count = prompt_loop(lock, ch);
|
2018-07-04 06:21:25 -06:00
|
|
|
if(count > Console::FAILURE)
|
2011-07-15 11:58:17 -06:00
|
|
|
output = raw_buffer;
|
|
|
|
state = con_unclaimed;
|
|
|
|
print("\n");
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2011-07-13 20:05:27 -06:00
|
|
|
FILE * dfout_C;
|
2011-07-27 06:22:37 -06:00
|
|
|
int rawmode;
|
2011-07-13 20:05:27 -06:00
|
|
|
HANDLE console_in;
|
|
|
|
HANDLE console_out;
|
|
|
|
HWND ConsoleWindow;
|
2011-11-04 02:08:29 -06:00
|
|
|
HWND MainWindow;
|
2011-07-13 20:05:27 -06:00
|
|
|
WORD default_attributes;
|
2011-07-15 11:58:17 -06:00
|
|
|
// current state
|
|
|
|
enum console_state
|
|
|
|
{
|
|
|
|
con_unclaimed,
|
|
|
|
con_lineedit
|
|
|
|
} state;
|
2012-03-10 02:29:33 -07:00
|
|
|
bool in_batch;
|
2011-07-15 11:58:17 -06:00
|
|
|
std::string prompt; // current prompt string
|
|
|
|
std::string raw_buffer; // current raw mode buffer
|
|
|
|
int raw_cursor; // cursor position in the buffer
|
2011-07-13 20:05:27 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-10 02:29:33 -07:00
|
|
|
Console::Console()
|
2011-07-13 20:05:27 -06:00
|
|
|
{
|
2011-07-14 00:02:29 -06:00
|
|
|
d = 0;
|
2011-07-17 03:06:45 -06:00
|
|
|
wlock = 0;
|
|
|
|
inited = false;
|
2011-07-13 20:05:27 -06:00
|
|
|
}
|
2011-06-22 06:25:39 -06:00
|
|
|
|
2011-07-13 20:05:27 -06:00
|
|
|
Console::~Console()
|
|
|
|
{
|
|
|
|
}
|
2011-11-04 02:08:29 -06:00
|
|
|
/*
|
|
|
|
// DOESN'T WORK - locks up DF!
|
|
|
|
void ForceForegroundWindow(HWND window)
|
|
|
|
{
|
|
|
|
DWORD nForeThread, nAppThread;
|
|
|
|
|
|
|
|
nForeThread = GetWindowThreadProcessId(GetForegroundWindow(), 0);
|
|
|
|
nAppThread = ::GetWindowThreadProcessId(window,0);
|
2011-06-22 06:25:39 -06:00
|
|
|
|
2011-11-04 02:08:29 -06:00
|
|
|
if(nForeThread != nAppThread)
|
|
|
|
{
|
|
|
|
AttachThreadInput(nForeThread, nAppThread, true);
|
|
|
|
BringWindowToTop(window);
|
|
|
|
ShowWindow(window,3);
|
|
|
|
AttachThreadInput(nForeThread, nAppThread, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BringWindowToTop(window);
|
|
|
|
ShowWindow(window,3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2011-07-31 22:30:29 -06:00
|
|
|
bool Console::init(bool)
|
2011-06-22 06:25:39 -06:00
|
|
|
{
|
2011-07-14 00:02:29 -06:00
|
|
|
d = new Private();
|
2011-06-22 06:25:39 -06:00
|
|
|
int hConHandle;
|
2017-08-07 13:13:22 -06:00
|
|
|
intptr_t lStdHandle;
|
2011-06-22 06:25:39 -06:00
|
|
|
CONSOLE_SCREEN_BUFFER_INFO coninfo;
|
|
|
|
FILE *fp;
|
|
|
|
DWORD oldMode, newMode;
|
2011-11-04 02:08:29 -06:00
|
|
|
DWORD dwTheardId;
|
|
|
|
|
|
|
|
HWND h = ::GetTopWindow(0 );
|
|
|
|
while ( h )
|
|
|
|
{
|
|
|
|
DWORD pid;
|
|
|
|
dwTheardId = ::GetWindowThreadProcessId( h,&pid);
|
|
|
|
if ( pid == GetCurrentProcessId() )
|
|
|
|
{
|
|
|
|
// here h is the handle to the window
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
h = ::GetNextWindow( h , GW_HWNDNEXT);
|
|
|
|
}
|
|
|
|
d->MainWindow = h;
|
2011-06-22 06:25:39 -06:00
|
|
|
|
|
|
|
// Allocate a console!
|
|
|
|
AllocConsole();
|
2011-07-13 20:05:27 -06:00
|
|
|
d->ConsoleWindow = GetConsoleWindow();
|
2012-03-10 02:29:33 -07:00
|
|
|
wlock = new recursive_mutex();
|
2011-07-13 20:05:27 -06:00
|
|
|
HMENU hm = GetSystemMenu(d->ConsoleWindow,false);
|
2011-06-22 10:04:22 -06:00
|
|
|
DeleteMenu(hm, SC_CLOSE, MF_BYCOMMAND);
|
2011-06-22 06:25:39 -06:00
|
|
|
|
|
|
|
// set the screen buffer to be big enough to let us scroll text
|
|
|
|
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
|
2011-07-13 20:05:27 -06:00
|
|
|
d->default_attributes = coninfo.wAttributes;
|
2011-06-22 06:25:39 -06:00
|
|
|
coninfo.dwSize.Y = MAX_CONSOLE_LINES; // How many lines do you want to have in the console buffer
|
|
|
|
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
|
|
|
|
|
|
|
|
// redirect unbuffered STDOUT to the console
|
2011-07-13 20:05:27 -06:00
|
|
|
d->console_out = GetStdHandle(STD_OUTPUT_HANDLE);
|
2017-08-07 13:13:22 -06:00
|
|
|
lStdHandle = (intptr_t)d->console_out;
|
2011-06-22 06:25:39 -06:00
|
|
|
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
|
2011-07-13 20:05:27 -06:00
|
|
|
d->dfout_C = _fdopen( hConHandle, "w" );
|
|
|
|
setvbuf( d->dfout_C, NULL, _IONBF, 0 );
|
2011-06-22 06:25:39 -06:00
|
|
|
|
|
|
|
// redirect unbuffered STDIN to the console
|
2011-07-13 20:05:27 -06:00
|
|
|
d->console_in = GetStdHandle(STD_INPUT_HANDLE);
|
2017-08-07 13:13:22 -06:00
|
|
|
lStdHandle = (intptr_t)d->console_in;
|
2011-06-22 06:25:39 -06:00
|
|
|
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
|
|
|
|
fp = _fdopen( hConHandle, "r" );
|
|
|
|
*stdin = *fp;
|
|
|
|
setvbuf( stdin, NULL, _IONBF, 0 );
|
|
|
|
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),&oldMode);
|
|
|
|
newMode = oldMode | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
|
|
|
|
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),newMode);
|
2011-07-16 00:00:13 -06:00
|
|
|
SetConsoleCtrlHandler(NULL,true);
|
2011-06-22 06:25:39 -06:00
|
|
|
std::ios::sync_with_stdio();
|
|
|
|
|
|
|
|
// make our own weird streams so our IO isn't redirected
|
2011-07-13 20:05:27 -06:00
|
|
|
std::cin.tie(this);
|
2011-06-22 06:25:39 -06:00
|
|
|
clear();
|
2011-07-17 03:06:45 -06:00
|
|
|
inited = true;
|
2011-11-04 02:08:29 -06:00
|
|
|
// DOESN'T WORK - locks up DF!
|
|
|
|
// ForceForegroundWindow(d->MainWindow);
|
2023-05-03 15:34:48 -06:00
|
|
|
hide();
|
2011-07-13 20:05:27 -06:00
|
|
|
return true;
|
2011-06-22 06:25:39 -06:00
|
|
|
}
|
2011-07-15 11:58:17 -06:00
|
|
|
// FIXME: looks awfully empty, doesn't it?
|
2011-07-13 20:05:27 -06:00
|
|
|
bool Console::shutdown(void)
|
2011-06-22 06:25:39 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-06-22 06:25:39 -06:00
|
|
|
FreeConsole();
|
2011-07-17 03:06:45 -06:00
|
|
|
inited = false;
|
2011-07-13 20:05:27 -06:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
|
|
|
|
void Console::begin_batch()
|
2011-07-13 20:05:27 -06:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
//color_ostream::begin_batch();
|
2012-03-10 02:29:33 -07:00
|
|
|
|
|
|
|
wlock->lock();
|
|
|
|
|
|
|
|
if (inited)
|
|
|
|
d->begin_batch();
|
2011-07-15 11:58:17 -06:00
|
|
|
}
|
|
|
|
|
2012-03-10 02:29:33 -07:00
|
|
|
void Console::end_batch()
|
2011-07-15 11:58:17 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
if (inited)
|
|
|
|
d->end_batch();
|
|
|
|
|
|
|
|
wlock->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::flush_proxy()
|
|
|
|
{
|
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
|
|
|
if (inited)
|
|
|
|
d->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::add_text(color_value color, const std::string &text)
|
|
|
|
{
|
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
|
|
|
if (inited)
|
|
|
|
d->print_text(color, text);
|
2011-07-13 20:05:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int Console::get_columns(void)
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-07-17 03:06:45 -06:00
|
|
|
int ret = -1;
|
|
|
|
if(inited)
|
|
|
|
ret = d->get_columns();
|
|
|
|
return ret;
|
2011-07-13 20:05:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int Console::get_rows(void)
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-07-17 03:06:45 -06:00
|
|
|
int ret = -1;
|
|
|
|
if(inited)
|
|
|
|
ret = d->get_rows();
|
|
|
|
return ret;
|
2011-06-22 06:25:39 -06:00
|
|
|
}
|
2011-06-24 21:35:29 -06:00
|
|
|
|
2011-06-22 06:25:39 -06:00
|
|
|
void Console::clear()
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-07-17 03:06:45 -06:00
|
|
|
if(inited)
|
|
|
|
d->clear();
|
2011-06-22 06:25:39 -06:00
|
|
|
}
|
2011-06-24 21:35:29 -06:00
|
|
|
|
2011-06-22 06:25:39 -06:00
|
|
|
void Console::gotoxy(int x, int y)
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-07-17 03:06:45 -06:00
|
|
|
if(inited)
|
|
|
|
d->gotoxy(x,y);
|
2011-06-22 06:25:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void Console::cursor(bool enable)
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-07-17 03:06:45 -06:00
|
|
|
if(inited)
|
|
|
|
d->cursor(enable);
|
2011-06-24 21:35:29 -06:00
|
|
|
}
|
|
|
|
|
2011-08-13 06:42:09 -06:00
|
|
|
int Console::lineedit(const std::string & prompt, std::string & output, CommandHistory & ch)
|
2011-07-13 20:05:27 -06:00
|
|
|
{
|
2011-07-27 06:22:37 -06:00
|
|
|
wlock->lock();
|
2018-07-04 06:21:25 -06:00
|
|
|
int ret = Console::SHUTDOWN;
|
2011-07-17 03:06:45 -06:00
|
|
|
if(inited)
|
2011-08-13 06:42:09 -06:00
|
|
|
ret = d->lineedit(prompt,output,wlock,ch);
|
2011-07-27 06:22:37 -06:00
|
|
|
wlock->unlock();
|
2011-07-15 11:58:17 -06:00
|
|
|
return ret;
|
2011-07-13 20:05:27 -06:00
|
|
|
}
|
|
|
|
|
2011-07-15 11:58:17 -06:00
|
|
|
void Console::msleep (unsigned int msec)
|
2011-07-13 20:05:27 -06:00
|
|
|
{
|
2011-07-15 11:58:17 -06:00
|
|
|
Sleep(msec);
|
2011-07-13 20:05:27 -06:00
|
|
|
}
|
2014-05-26 09:29:27 -06:00
|
|
|
|
2015-02-08 07:30:40 -07:00
|
|
|
bool Console::hide()
|
2014-05-26 09:29:27 -06:00
|
|
|
{
|
2015-02-08 07:30:40 -07:00
|
|
|
ShowWindow( GetConsoleWindow(), SW_HIDE );
|
|
|
|
return true;
|
2014-05-26 09:29:27 -06:00
|
|
|
}
|
|
|
|
|
2015-02-08 07:30:40 -07:00
|
|
|
bool Console::show()
|
2014-05-26 09:29:27 -06:00
|
|
|
{
|
|
|
|
ShowWindow( GetConsoleWindow(), SW_RESTORE );
|
2015-02-08 07:30:40 -07:00
|
|
|
return true;
|
|
|
|
}
|