jamulus/src/main.cpp

301 lines
8.9 KiB
C++
Raw Normal View History

/******************************************************************************\
* Copyright (c) 2004-2008
*
* Author(s):
* Volker Fischer
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
\******************************************************************************/
#include <qapplication.h>
2008-07-13 20:43:40 +02:00
#include <qmessagebox.h>
#include <iostream>
#include "global.h"
#include "llconclientdlg.h"
2006-01-28 12:29:22 +01:00
#include "llconserverdlg.h"
#include "settings.h"
/* Implementation *************************************************************/
2008-01-22 20:58:53 +01:00
// these pointers are only used for the post-event routine
QApplication* pApp = NULL;
QDialog* pMainWindow = NULL;
int main ( int argc, char** argv )
{
std::string strArgument;
double rDbleArgument;
/* check if server or client application shall be started */
bool bIsClient = true;
bool bUseGUI = true;
bool bUseServerLogging = false;
quint16 iPortNumber = LLCON_PORT_NUMBER;
std::string strIniFileName = "";
2007-05-06 14:27:41 +02:00
/* QT docu: argv()[0] is the program name, argv()[1] is the first
argument and argv()[argc()-1] is the last argument.
Start with first argument, therefore "i = 1" */
for ( int i = 1; i < argc; i++ )
{
/* Server mode flag ------------------------------------------------------- */
2008-01-27 11:05:15 +01:00
if ( GetFlagArgument ( argc, argv, i, "-s", "--server" ) )
{
bIsClient = false;
cerr << "server mode chosen" << std::endl;
continue;
}
/* Use GUI flag ----------------------------------------------------------- */
2008-01-27 11:05:15 +01:00
if ( GetFlagArgument ( argc, argv, i, "-n", "--nogui" ) )
{
bUseGUI = false;
cerr << "no GUI mode chosen" << std::endl;
continue;
}
/* Use logging flag ------------------------------------------------------- */
2008-01-27 11:05:15 +01:00
if ( GetFlagArgument ( argc, argv, i, "-l", "--log" ) )
{
bUseServerLogging = true;
cerr << "logging enabled" << std::endl;
continue;
}
/* Port number ------------------------------------------------------------ */
if ( GetNumericArgument ( argc, argv, i, "-p", "--port",
0, 65535, rDbleArgument ) )
{
iPortNumber = static_cast<quint16> ( rDbleArgument );
cerr << "selected port number: " << iPortNumber << std::endl;
continue;
}
/* Initialization file ---------------------------------------------------- */
2008-01-27 11:05:15 +01:00
if ( GetStringArgument ( argc, argv, i, "-i", "--inifile", strArgument ) )
{
strIniFileName = strArgument;
continue;
}
/* Help (usage) flag ------------------------------------------------------ */
if ( ( !strcmp ( argv[i], "--help" ) ) ||
( !strcmp ( argv[i], "-h" ) ) || ( !strcmp ( argv[i], "-?" ) ) )
{
const std::string strHelp = UsageArguments(argv);
cerr << strHelp;
exit ( 1 );
}
/* Unknown option --------------------------------------------------------- */
cerr << argv[0] << ": ";
cerr << "Unknown option '" << argv[i] << "' -- use '--help' for help"
<< endl;
exit ( 1 );
}
// Application object
QApplication app ( argc, argv, bUseGUI );
2008-01-20 19:07:13 +01:00
// init resources
extern int qInitResources();
qInitResources();
2008-07-13 20:43:40 +02:00
try
{
2008-07-13 20:43:40 +02:00
if ( bIsClient )
{
// client
// actual client object
CClient Client;
2006-01-28 12:29:22 +01:00
2008-07-13 20:43:40 +02:00
// load settings from init-file
CSettings Settings ( &Client );
// TODO use QString
2008-07-13 20:43:40 +02:00
Settings.Load ( strIniFileName.c_str() );
2006-01-28 12:29:22 +01:00
2008-07-13 20:43:40 +02:00
// GUI object
CLlconClientDlg ClientDlg ( &Client, 0 );
2008-07-13 20:43:40 +02:00
// set main window
pMainWindow = &ClientDlg;
pApp = &app; // Needed for post-event routine
2008-07-13 20:43:40 +02:00
// show dialog
ClientDlg.show();
app.exec();
2006-01-28 12:29:22 +01:00
2008-07-13 20:43:40 +02:00
// save settings to init-file
// TODO use QString
2008-07-13 20:43:40 +02:00
Settings.Save ( strIniFileName.c_str() );
}
else
{
// server
// actual server object
CServer Server ( bUseServerLogging, iPortNumber );
2008-07-13 20:43:40 +02:00
if ( bUseGUI )
{
// GUI object for the server
CLlconServerDlg ServerDlg ( &Server, 0 );
// set main window
pMainWindow = &ServerDlg;
pApp = &app; // needed for post-event routine
// show dialog
ServerDlg.show();
app.exec();
}
else
{
// only start application without using the GUI
qDebug() << CAboutDlg::GetVersionAndNameStr ( false );
app.exec();
}
}
}
2008-07-13 20:43:40 +02:00
catch ( CGenErr generr )
{
// show generic error
if ( bUseGUI )
{
QMessageBox::critical ( 0, APP_NAME, generr.GetErrorText(), "Quit", 0 );
}
else
{
qDebug() << generr.GetErrorText();
}
}
return 0;
}
/******************************************************************************\
* Command Line Argument Parsing *
\******************************************************************************/
std::string UsageArguments ( char **argv )
{
return
"Usage: " + std::string ( argv[0] ) + " [option] [argument]\n"
"Recognized options:\n"
" -s, --server start server\n"
" -n, --nogui disable GUI (only avaiable for server)\n"
" -l, --log enable logging\n"
" -i, --inifile initialization file name (only available for client)\n"
" -h, -?, --help this help text\n"
"Example: " + std::string ( argv[0] ) + " -l -inifile myinifile.ini\n";
}
bool GetFlagArgument ( int, char **argv, int &i,
std::string strShortOpt, std::string strLongOpt )
{
if ( ( !strShortOpt.compare ( argv[i] ) ) || ( !strLongOpt.compare ( argv[i] ) ) )
{
return true;
}
else
{
return false;
}
}
bool GetStringArgument ( int argc, char **argv, int &i,
std::string strShortOpt, std::string strLongOpt,
std::string & strArg )
{
if ( ( !strShortOpt.compare ( argv[i] ) ) || ( !strLongOpt.compare ( argv[i] ) ) )
{
if ( ++i >= argc )
{
cerr << argv[0] << ": ";
cerr << "'" << strLongOpt << "' needs a string argument" << endl;
exit ( 1 );
}
strArg = argv[i];
return true;
}
else
{
return false;
}
}
bool GetNumericArgument ( int argc, char **argv, int &i,
std::string strShortOpt, std::string strLongOpt,
double rRangeStart, double rRangeStop,
double & rValue)
{
if ( ( !strShortOpt.compare ( argv[i] ) ) || ( !strLongOpt.compare ( argv[i] ) ) )
{
if ( ++i >= argc )
{
cerr << argv[0] << ": ";
cerr << "'" << strLongOpt << "' needs a numeric argument between "
<< rRangeStart << " and " << rRangeStop << endl;
exit ( 1 );
}
char *p;
rValue = strtod ( argv[i], &p );
if ( *p || rValue < rRangeStart || rValue > rRangeStop )
{
cerr << argv[0] << ": ";
cerr << "'" << strLongOpt << "' needs a numeric argument between "
<< rRangeStart << " and " << rRangeStop << endl;
exit ( 1 );
}
return true;
}
else
{
return false;
}
}
/******************************************************************************\
* Window Message System *
\******************************************************************************/
void PostWinMessage ( const _MESSAGE_IDENT MessID, const int iMessageParam,
const int iChanNum )
{
// first check if application is initialized
if ( pApp != NULL )
{
CLlconEvent* LlconEv =
new CLlconEvent ( MessID, iMessageParam, iChanNum );
// Qt will delete the event object when done
2008-01-22 20:58:53 +01:00
QCoreApplication::postEvent ( pMainWindow, LlconEv );
}
}