Update of /cvsroot/wpdev/wolfpack
In directory sc8-pr-cvs1:/tmp/cvs-serv7549
Modified Files:
console.cpp console.h wolf.dsp wolfpack.cpp
Added Files:
console_linux.cpp console_win.cpp
Log Message:
Trying out new console code.
--- NEW FILE: console_linux.cpp ---
#include "console.h"
class cConsoleThread : public QThread
{
protected:
virtual void run()
{
try
{
while( serverState < SHUTDOWN )
{
char c = getch();
if( c > 0 && serverState == RUNNING )
{
Console::instance()->handleCommand( QChar( c ) );
}
else
{
Sleep( 100 );
}
}
}
// If there is any error: Quit.
// It's better to have no console input
// than a deadlocking server.
catch( ... )
{
}
}
};
cConsoleThread *thread = 0;
void cConsole::start()
{
thread = new cConsoleThread;
thread->start();
}
void cConsole::poll()
{
// Normally we would check if there is a command in the command queue and execute it
}
void cConsole::stop()
{
thread->wait();
delete thread;
}
void cConsole::setConsoleTitle( const QString& data )
{
#if defined(Q_OS_WIN32)
SetConsoleTitle( data.latin1() );
#endif
}
//=========================================================================================
// Change the console Color
void cConsole::ChangeColor( WPC_ColorKeys Color )
{
#if defined(Q_OS_UNIX)
QString cb = "\e[0m";
switch( Color )
{
case WPC_GREEN: cb = "\e[1;32m";
break;
case WPC_RED: cb = "\e[1;31m";
break;
case WPC_YELLOW:cb = "\e[1;33m";
break;
case WPC_NORMAL:cb = "\e[0m";
break;
case WPC_WHITE: cb = "\e[1;37m";
break;
default: cb = "\e[0m";
}
send( cb );
#elif defined(Q_OS_WIN32)
HANDLE ConsoleHandle = GetStdHandle( STD_OUTPUT_HANDLE );
UI16 ColorKey = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE;
switch( Color )
{
case WPC_GREEN:
ColorKey = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
case WPC_RED:
ColorKey = FOREGROUND_RED | FOREGROUND_INTENSITY;
break;
case WPC_YELLOW:
ColorKey = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
break;
case WPC_NORMAL:
ColorKey = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE;
break;
case WPC_WHITE:
ColorKey = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
break;
default:
break;
}
SetConsoleTextAttribute( ConsoleHandle, ColorKey );
#endif
}
//========================================================================================
// Send a message to the console
void cConsole::send(const QString &sMessage)
{
if( outputstrm != NULL )
{
(*outputstrm) << sMessage.latin1();
flush( *outputstrm );
}
if( sMessage.contains( "\n" ) )
{
#if defined(Q_OS_UNIX) && 0
sMessage.replace("\e[0m", "");
sMessage.replace("\e[1;32m", "");
sMessage.replace("\e[1;31m", "");
sMessage.replace("\e[1;33m", "");
sMessage.replace("\e[1;37m", "");
#endif
incompleteLine_.append( sMessage ); // Split by \n
QStringList lines = QStringList::split( "\n", incompleteLine_, true );
// Insert all except the last element
for( int i = 0; i < lines.count()-1; ++i )
linebuffer_.push_back( lines[i] );
incompleteLine_ = lines[ lines.count() - 1 ];
}
else
{
incompleteLine_.append( sMessage );
}
}
--- NEW FILE: console_win.cpp ---
// System Includes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <richedit.h>
#include <qthread.h>
// Wolfpack Includes
#include "console.h"
#include "resource.h"
#include "globals.h"
/*
This file includes the Windows GUI implementation of our Console.
*/
#define WOLFPACK_CLASS "wolfpack_class"
extern int main( int argc, char **argv );
// Variables important for this GUI implementation
HWND logWindow = 0; // Log Window
HWND inputWindow = 0; // Input Textfield
HWND mainWindow = 0; // Main Window
HINSTANCE appInstance = 0; // Application Instance
HFONT font = 0; // The font we'll use
unsigned int inputHeight = 0; // For measuring the height of the input field
/*
Directly taken from MSDN
*/
static QString getErrorString()
{
LPVOID lpMsgBuf;
if( !FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 0, GetLastError(), MAKELANGID( LANG_NEUTRAL , SUBLANG_DEFAULT ), (LPTSTR)&lpMsgBuf, 0, 0 ) )
return QString( "Unknown Error" );
QString result( (char*)lpMsgBuf );
// Free the buffer.
LocalFree( lpMsgBuf );
return result;
}
LRESULT CALLBACK wpWindowProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
{
CHARFORMAT cf;
LOGFONT lfont;
switch( msg )
{
case WM_CREATE:
// Create Richedit Box
logWindow = CreateWindow( RICHEDIT_CLASS, 0, ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY | WS_CHILD | WS_VISIBLE | WS_VSCROLL, 0, 0, 10, 10, hwnd, 0, appInstance, 0 );
if( logWindow == 0 )
{
MessageBox( 0, QString( "Couldn't create the logwindow: " + getErrorString() ).latin1(), "Wolfpack", MB_OK|MB_ICONERROR );
DestroyWindow( hwnd );
return TRUE;
}
// Set up the fonts we need
ZeroMemory( &lfont, sizeof( LOGFONT ) );
qstrcpy( lfont.lfFaceName, "Courier" );
font = CreateFontIndirect( &lfont );
// Set the font of our logwindow
SendMessage( logWindow, WM_SETFONT, (WPARAM)font, 0 );
SendMessage( logWindow, EM_SETBKGNDCOLOR, 0, (LPARAM)RGB(0,0,0) );
// Default Charformat
ZeroMemory( &cf, sizeof( CHARFORMAT ) );
cf.cbSize = sizeof( CHARFORMAT );
cf.dwMask = CFM_COLOR;
cf.crTextColor = RGB( 0xAF,0xAF,0xAF );
SendMessage( logWindow, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&cf );
SendMessage( logWindow, EM_AUTOURLDETECT, 1, 0 );
SendMessage( logWindow, EM_SETEVENTMASK, 0, ENM_LINK|ENM_MOUSEEVENTS|ENM_KEYEVENTS );
// Create InputWindow
inputWindow = CreateWindow( "EDIT", 0, ES_LEFT|ES_AUTOHSCROLL|WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP, 0, 0, 10, 10, hwnd, 0, appInstance, 0 );
return 0;
case WM_SIZE:
if( logWindow && inputWindow && wparam != SIZE_MINIMIZED && wparam != SIZE_MAXHIDE )
{
unsigned int width = LOWORD( lparam );
unsigned int height = HIWORD( lparam );
// Measure the Height of our Input Field
if( !inputHeight )
{
HFONT font = (HFONT)SendMessage( mainWindow, WM_GETFONT, 0, 0 );
if( !font )
font = (HFONT)GetStockObject( SYSTEM_FONT );
LOGFONT logfont;
if( GetObject( font, sizeof( LOGFONT ), &logfont ) == sizeof( LOGFONT ) )
inputHeight = logfont.lfHeight + 4;
}
MoveWindow( logWindow, 0, 0, width, height - inputHeight, TRUE );
MoveWindow( inputWindow, 0, height - inputHeight, width, inputHeight, TRUE );
}
return 0;
case WM_DESTROY:
keeprun = 0;
return 0;
default:
return DefWindowProc( hwnd, msg, wparam, lparam );
}
}
class cGuiThread : public QThread
{
protected:
virtual void run()
{
char **argv = (char**)malloc( 1 * sizeof( char* ) );
argv[0] = "wolfpack.exe";
int argc = 1;
main( argc, argv );
free( argv[0] );
free( argv );
PostQuitMessage( 0 );
}
};
cGuiThread *guiThread = 0;
int WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
appInstance = hInstance;
// Try to load riched20.dll
HMODULE hRiched = LoadLibrary( "riched20.dll" );
if( hRiched == 0 )
{
MessageBox( 0, "The riched20.dll library could not be found on your system.\nPlease install Microsoft Internet Explorer 4.0 or later.", "Riched missing", MB_OK|MB_ICONERROR );
return 1;
}
// Create the WindowClass
WNDCLASS wpClass;
ZeroMemory( &wpClass, sizeof( WNDCLASS ) );
wpClass.hInstance = hInstance;
wpClass.lpfnWndProc = wpWindowProc;
wpClass.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE( IDI_ICON2 ) );
wpClass.hbrBackground = GetSysColorBrush( COLOR_BTNFACE );
wpClass.lpszClassName = WOLFPACK_CLASS;
wpClass.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
if( !RegisterClass( &wpClass ) )
{
MessageBox( 0, "Couldn't register Window Class.", "Window Class", MB_OK|MB_ICONERROR );
return 1;
}
// Create the Window itself
mainWindow = CreateWindow( WOLFPACK_CLASS, "Wolfpack", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, NULL, 0, hInstance, NULL );
if( mainWindow == 0 )
{
MessageBox( 0, QString( "Couldn't create the window: " + getErrorString() ).latin1(), "Wolfpack", MB_OK|MB_ICONERROR );
return 1;
}
ShowWindow( mainWindow, SW_NORMAL );
guiThread = new cGuiThread;
guiThread->start();
MSG msg;
while( GetMessage( &msg, mainWindow, 0, 0 ) > 0 )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
keeprun = 0; // We quit, so let's quit the server too
guiThread->wait();
delete guiThread;
return 0;
}
void cConsole::start()
{
}
void cConsole::poll()
{
}
void cConsole::stop()
{
}
void cConsole::send(const QString &sMessage)
{
// process \b properly
SendMessage( logWindow, EM_REPLACESEL, FALSE, (LPARAM)sMessage.latin1() );
}
void cConsole::ChangeColor( WPC_ColorKeys color )
{
CHARFORMAT cf;
ZeroMemory( &cf, sizeof( CHARFORMAT ) );
cf.cbSize = sizeof( CHARFORMAT );
cf.dwMask = CFM_COLOR;
switch( color )
{
case WPC_GREEN:
cf.crTextColor = RGB( 0x00,0xFF,0x00 );
break;
case WPC_RED:
cf.crTextColor = RGB( 0xFF,0x00,0x00 );
break;
case WPC_YELLOW:
cf.crTextColor = RGB( 0x00,0xFF,0xFF );
break;
case WPC_NORMAL:
cf.crTextColor = RGB( 0xAF,0xAF,0xAF );
break;
case WPC_WHITE:
cf.crTextColor = RGB( 0xFF,0xFF,0xFF );
break;
};
SendMessage( logWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf );
}
void cConsole::setConsoleTitle( const QString& data )
{
SetWindowText( mainWindow, data.latin1() );
}
Index: console.cpp
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/console.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** console.cpp 8 Sep 2003 11:46:24 -0000 1.1
--- console.cpp 8 Sep 2003 23:38:49 -0000 1.2
***************
*** 71,75 ****
bEnabled = true;
// do nothing at the moment
! setStreams( &cin, &cout, &cerr, &cout );
}
--- 71,75 ----
bEnabled = true;
// do nothing at the moment
! setStreams( &cin, &cout );
}
***************
*** 86,129 ****
}
! void cConsole::setStreams(istream *in, ostream *out, ostream *error, ostream *log)
{
inputstrm = in;
outputstrm = out;
- errorstrm = error;
- logstrm = log;
- }
-
- //========================================================================================
- // Send a message to the console
- void cConsole::send(const QString &sMessage)
- {
- if( outputstrm != NULL )
- {
- (*outputstrm) << sMessage.latin1();
- flush( *outputstrm );
- }
-
- if( sMessage.contains( "\n" ) )
- {
- #if defined(Q_OS_UNIX) && 0
- sMessage.replace("\e[0m", "");
- sMessage.replace("\e[1;32m", "");
- sMessage.replace("\e[1;31m", "");
- sMessage.replace("\e[1;33m", "");
- sMessage.replace("\e[1;37m", "");
- #endif
- incompleteLine_.append( sMessage ); // Split by \n
- QStringList lines = QStringList::split( "\n", incompleteLine_, true );
-
- // Insert all except the last element
- for( int i = 0; i < lines.count()-1; ++i )
- linebuffer_.push_back( lines[i] );
-
- incompleteLine_ = lines[ lines.count() - 1 ];
- }
- else
- {
- incompleteLine_.append( sMessage );
- }
}
--- 86,93 ----
}
! void cConsole::setStreams(istream *in, ostream *out)
{
inputstrm = in;
outputstrm = out;
}
***************
*** 140,151 ****
}
- //========================================================================================
- // Send a message to the console
- void cConsole::error(const QString& sMessage)
- {
- if (errorstrm != NULL)
- (*errorstrm) << sMessage.latin1();
- }
-
//=========================================================================================
// Get input from the console
--- 104,107 ----
***************
*** 211,276 ****
}
- //=========================================================================================
- // Change the console Color
- void cConsole::ChangeColor( WPC_ColorKeys Color )
- {
- #if defined(Q_OS_UNIX)
- QString cb = "\e[0m";
- switch( Color )
- {
- case WPC_GREEN: cb = "\e[1;32m";
- break;
- case WPC_RED: cb = "\e[1;31m";
- break;
- case WPC_YELLOW:cb = "\e[1;33m";
- break;
- case WPC_NORMAL:cb = "\e[0m";
- break;
- case WPC_WHITE: cb = "\e[1;37m";
- break;
- default: cb = "\e[0m";
-
- }
- send( cb );
- #elif defined(Q_OS_WIN32)
- HANDLE ConsoleHandle = GetStdHandle( STD_OUTPUT_HANDLE );
- UI16 ColorKey = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE;
-
- switch( Color )
- {
- case WPC_GREEN:
- ColorKey = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
- break;
-
- case WPC_RED:
- ColorKey = FOREGROUND_RED | FOREGROUND_INTENSITY;
- break;
-
- case WPC_YELLOW:
- ColorKey = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
- break;
-
- case WPC_NORMAL:
- ColorKey = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE;
- break;
-
- case WPC_WHITE:
- ColorKey = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
- break;
-
- default:
- break;
- }
-
- SetConsoleTextAttribute( ConsoleHandle, ColorKey );
- #endif
- }
-
- void cConsole::setConsoleTitle( const QString& data )
- {
- #if defined(Q_OS_WIN32)
- SetConsoleTitle( data.latin1() );
- #endif
- }
bool cConsole::handleCommand( const QString &command, bool silentFail )
--- 167,170 ----
***************
*** 352,400 ****
return true;
- }
-
- class cConsoleThread : public QThread
- {
- protected:
- virtual void run()
- {
- try
- {
- while( serverState < SHUTDOWN )
- {
- char c = getch();
-
- if( c > 0 && serverState == RUNNING )
- {
- Console::instance()->send( QChar( c ) );
- }
- else
- {
- Sleep( 100 );
- }
- }
- }
- catch( ... )
- {
- }
- }
- };
-
- cConsoleThread *thread = 0;
-
- void cConsole::start()
- {
- thread = new cConsoleThread;
- thread->start();
- }
-
- void cConsole::poll()
- {
- // Normally we would check if there is a command in the command queue and execute it
- }
-
- void cConsole::stop()
- {
- thread->wait();
- delete thread;
}
--- 246,248 ----
Index: console.h
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/console.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** console.h 8 Sep 2003 11:46:24 -0000 1.1
--- console.h 8 Sep 2003 23:38:49 -0000 1.2
***************
*** 72,87 ****
void enabled(bool);
! void setStreams( std::istream *in, std::ostream *out, std::ostream *error, std::ostream *log );
// Send a message to the console
! void send(const QString &sMessage);
!
// Log a message
void log( UINT8 logLevel, const QString &message );
- // Flag an error
- void error(const QString&);
-
// Get input from the console
UI08 getkey(void);
--- 72,83 ----
void enabled(bool);
! void setStreams( std::istream *in, std::ostream *out );
// Send a message to the console
! virtual void send(const QString &sMessage);
// Log a message
void log( UINT8 logLevel, const QString &message );
// Get input from the console
UI08 getkey(void);
***************
*** 95,113 ****
void ProgressSkip( void );
! void ChangeColor( WPC_ColorKeys Color );
! void setConsoleTitle( const QString& data );
QStringList linebuffer() const { return linebuffer_; }
bool handleCommand( const QString &command, bool silentFail = false );
! void start();
! void poll();
! void stop();
private:
std::istream *inputstrm;
std::ostream *outputstrm;
- std::ostream *errorstrm;
- std::ostream *logstrm;
bool bEnabled;
};
--- 91,107 ----
void ProgressSkip( void );
! virtual void ChangeColor( WPC_ColorKeys Color );
! virtual void setConsoleTitle( const QString& data );
QStringList linebuffer() const { return linebuffer_; }
bool handleCommand( const QString &command, bool silentFail = false );
! virtual void start();
! virtual void poll();
! virtual void stop();
private:
std::istream *inputstrm;
std::ostream *outputstrm;
bool bEnabled;
};
Index: wolf.dsp
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/wolf.dsp,v
retrieving revision 1.219
retrieving revision 1.220
diff -C2 -d -r1.219 -r1.220
*** wolf.dsp 8 Sep 2003 10:58:47 -0000 1.219
--- wolf.dsp 8 Sep 2003 23:38:49 -0000 1.220
***************
*** 52,56 ****
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
! # ADD LINK32 kernel32.lib user32.lib advapi32.lib ws2_32.lib $(QTDIR)\lib\qt-mt312.lib libmysql.lib /nologo /subsystem:console /map /machine:I386 /nodefaultlib:"libcmt MSVCRTD" /out:"D:\wolfpack\wolfpack.exe" /libpath:"lib\ZThread\lib" /libpath:"lib\Python\lib" /libpath:"lib\bugreport\lib" /libpath:"flatstore\Release" /opt:ref /opt:nowin98
# SUBTRACT LINK32 /pdb:none
--- 52,56 ----
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
! # ADD LINK32 kernel32.lib user32.lib gdi32.lib advapi32.lib ws2_32.lib $(QTDIR)\lib\qt-mt312.lib libmysql.lib /nologo /subsystem:console /map /machine:I386 /nodefaultlib:"libcmt MSVCRTD" /out:"..\wolfpack.exe" /libpath:"lib\ZThread\lib" /libpath:"lib\Python\lib" /libpath:"lib\bugreport\lib" /libpath:"flatstore\Release" /opt:ref /opt:nowin98
# SUBTRACT LINK32 /pdb:none
***************
*** 77,81 ****
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
! # ADD LINK32 kernel32.lib user32.lib advapi32.lib ws2_32.lib $(QTDIR)\lib\qt-mt312.lib /nologo /version:12.9 /subsystem:console /map /debug /machine:I386 /out:"..\Wolfpack.exe" /pdbtype:sept /libpath:"lib\bugreport\lib" /libpath:"flatstore\Debug"
# SUBTRACT LINK32 /pdb:none
--- 77,81 ----
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
! # ADD LINK32 kernel32.lib user32.lib gdi32.lib advapi32.lib ws2_32.lib $(QTDIR)\lib\qt-mt312.lib /nologo /version:12.9 /subsystem:windows /map /debug /machine:I386 /out:"D:\Wolfpack\wolfpack.exe" /pdbtype:sept /libpath:"lib\bugreport\lib" /libpath:"flatstore\Debug"
# SUBTRACT LINK32 /pdb:none
***************
*** 143,146 ****
--- 143,150 ----
# Begin Source File
+ SOURCE=.\console_win.cpp
+ # End Source File
+ # Begin Source File
+
SOURCE=.\contextmenu.cpp
# End Source File
***************
*** 453,456 ****
--- 457,464 ----
SOURCE=.\console.h
+ # End Source File
+ # Begin Source File
+
+ SOURCE=.\console_win.h
# End Source File
# Begin Source File
Index: wolfpack.cpp
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/wolfpack.cpp,v
retrieving revision 1.458
retrieving revision 1.459
diff -C2 -d -r1.458 -r1.459
*** wolfpack.cpp 8 Sep 2003 20:50:22 -0000 1.458
--- wolfpack.cpp 8 Sep 2003 23:38:49 -0000 1.459
***************
*** 229,233 ****
}
else
! Console::instance()->error( QString("The specified python script [%1] doesn't exist.").arg(param) );
}
--- 229,233 ----
}
else
! Console::instance()->send( QString("The specified python script [%1] doesn't exist.").arg(param) );
}
***************
*** 330,335 ****
return 0;*/
- Console::instance()->start(); // Startup Console
-
Console::instance()->send( QString( "\n%1 %2 %3 \n\n" ).arg( wp_version.productstring.c_str() ).arg( wp_version.betareleasestring.c_str() ).arg( wp_version.verstring.c_str() ) );
--- 330,333 ----
***************
*** 545,548 ****
--- 543,548 ----
serverState = RUNNING;
+
+ Console::instance()->start(); // Startup Console
QWaitCondition niceLevel;
|