2008-09-17 13:18:22 +03:00
|
|
|
#define VCMI_DLL
|
2007-08-15 18:13:11 +03:00
|
|
|
#include "stdafx.h"
|
2007-08-17 20:42:21 +03:00
|
|
|
#include "CConsoleHandler.h"
|
2008-09-17 13:18:22 +03:00
|
|
|
#include "boost/function.hpp"
|
2008-09-18 23:24:53 +03:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
#include <windows.h>
|
|
|
|
HANDLE handleIn;
|
|
|
|
HANDLE handleOut;
|
2008-06-21 16:27:52 +03:00
|
|
|
#endif
|
2008-09-18 23:24:53 +03:00
|
|
|
WORD defColor;
|
2008-06-21 16:27:52 +03:00
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CConsoleHandler.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-09-17 13:18:22 +03:00
|
|
|
void CConsoleHandler::setColor(int level)
|
|
|
|
{
|
|
|
|
WORD color;
|
|
|
|
switch(level)
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
color = defColor;
|
|
|
|
break;
|
|
|
|
case 0:
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
color = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
color = "\x1b[1;40;32m";
|
|
|
|
#endif
|
2008-09-17 13:18:22 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_INTENSITY;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
color = "\x1b[1;40;31m";
|
|
|
|
#endif
|
2008-09-17 13:18:22 +03:00
|
|
|
break;
|
|
|
|
case 2:
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
color = "\x1b[1;40;35m";
|
|
|
|
#endif
|
2008-09-17 13:18:22 +03:00
|
|
|
break;
|
|
|
|
case 3:
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
color = "\x1b[1;40;32m";
|
|
|
|
#endif
|
2008-09-17 13:18:22 +03:00
|
|
|
break;
|
|
|
|
case 4:
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
color = "\x1b[1;40;39m";
|
|
|
|
#endif
|
2008-09-17 13:18:22 +03:00
|
|
|
break;
|
|
|
|
case 5:
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
color = "\x1b[0;40;39m";
|
|
|
|
#endif
|
2008-09-17 13:18:22 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
color = defColor;
|
|
|
|
break;
|
|
|
|
}
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
SetConsoleTextAttribute(handleOut,color);
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
std::cout << color;
|
2008-09-17 13:18:22 +03:00
|
|
|
#endif
|
|
|
|
}
|
2008-06-21 16:27:52 +03:00
|
|
|
|
2008-09-17 13:18:22 +03:00
|
|
|
int CConsoleHandler::run()
|
2007-08-15 18:13:11 +03:00
|
|
|
{
|
2008-10-26 22:58:34 +02:00
|
|
|
char buffer[5000];
|
2007-08-15 18:13:11 +03:00
|
|
|
while(true)
|
|
|
|
{
|
2008-10-26 22:58:34 +02:00
|
|
|
std::cin.getline(buffer, 5000);
|
2008-09-17 13:18:22 +03:00
|
|
|
if(cb && *cb)
|
|
|
|
(*cb)(buffer);
|
2007-08-15 18:13:11 +03:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2008-09-17 13:18:22 +03:00
|
|
|
CConsoleHandler::CConsoleHandler()
|
2007-08-15 18:13:11 +03:00
|
|
|
{
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
handleIn = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
handleOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
|
|
|
GetConsoleScreenBufferInfo(handleOut,&csbi);
|
|
|
|
defColor = csbi.wAttributes;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
defColor = "\x1b[0m";
|
2008-09-17 13:18:22 +03:00
|
|
|
#endif
|
|
|
|
cb = new boost::function<void(const std::string &)>;
|
2007-08-15 18:13:11 +03:00
|
|
|
}
|
2008-09-17 13:18:22 +03:00
|
|
|
CConsoleHandler::~CConsoleHandler()
|
|
|
|
{
|
|
|
|
delete cb;
|
|
|
|
}
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
void CConsoleHandler::killConsole(pthread_t hThread)
|
|
|
|
#else
|
2008-09-17 13:18:22 +03:00
|
|
|
void CConsoleHandler::killConsole(void *hThread)
|
|
|
|
#endif
|
2008-09-18 23:24:53 +03:00
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog3 << "Killing console... ";
|
2008-09-18 23:24:53 +03:00
|
|
|
_kill_thread(hThread,0);
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog3 << "done!\n";
|
2008-09-18 23:24:53 +03:00
|
|
|
}
|