jamulus/src/main.cpp

276 lines
7.8 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>
#include <iostream>
#include "global.h"
#include "llconclientdlg.h"
2006-01-28 12:29:22 +01:00
#include "llconserverdlg.h"
#include "settings.h"
/* Implementation *************************************************************/
/* This pointer is only used for the post-event routine */
2006-01-28 12:29:22 +01:00
QApplication* pApp = NULL;
int main ( int argc, char** argv )
{
std::string strArgument;
/* check if server or client application shall be started */
bool bIsClient = true;
bool bUseGUI = true;
bool bUseServerLogging = false;
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 ------------------------------------------------------- */
if ( GetFlagArgument ( argc, argv, i, "-s", "--server" ) == TRUE )
{
bIsClient = false;
cerr << "server ";
continue;
}
/* Use GUI flag ----------------------------------------------------------- */
if ( GetFlagArgument ( argc, argv, i, "-n", "--nogui" ) == TRUE )
{
bUseGUI = false;
cerr << "nogui ";
continue;
}
/* Use logging flag ------------------------------------------------------- */
if ( GetFlagArgument ( argc, argv, i, "-l", "--log" ) == TRUE )
{
bUseServerLogging = true;
cerr << "logging ";
continue;
}
/* Initialization file ---------------------------------------------------- */
if ( GetStringArgument ( argc, argv, i, "-i", "--inifile", strArgument ) == TRUE )
{
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();
if ( bIsClient )
{
// client
// actual client object
CClient Client;
2006-01-28 12:29:22 +01:00
// load settings from init-file
CSettings Settings ( &Client );
Settings.Load ( strIniFileName );
2006-01-28 12:29:22 +01:00
// GUI object
CLlconClientDlg ClientDlg ( &Client, 0 );
// set main window
app.setActiveWindow ( &ClientDlg );
pApp = &app; // Needed for post-event routine
// show dialog
2007-05-06 14:27:41 +02:00
ClientDlg.show();
app.exec();
2006-01-28 12:29:22 +01:00
// save settings to init-file
Settings.Save ( strIniFileName );
}
else
{
// server
// actual server object
2007-05-06 14:27:41 +02:00
CServer Server ( bUseServerLogging );
if ( bUseGUI )
{
// GUI object for the server
CLlconServerDlg ServerDlg ( &Server, 0 );
// set main window
app.setActiveWindow ( &ServerDlg );
pApp = &app; // needed for post-event routine
// show dialog
2007-05-06 14:27:41 +02:00
ServerDlg.show();
app.exec();
}
else
{
// only start application without using the GUI
qDebug() << CAboutDlg::GetVersionAndNameStr ( false );
2007-05-06 14:27:41 +02:00
app.exec();
}
}
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 );
// TODO QT4
// Qt will delete the event object when done
// QThread::postEvent ( pApp->mainWidget(), LlconEv );
}
}