Merge pull request #2261 from M374LX/improve-main

main(): improve error handling and clean up code
This commit is contained in:
Colin Wallace
2015-08-15 10:51:08 -07:00

View File

@@ -69,10 +69,10 @@
#include "DataFile.h"
#include "Song.h"
static inline QString baseName( const QString & _file )
static inline QString baseName( const QString & file )
{
return( QFileInfo( _file ).absolutePath() + "/" +
QFileInfo( _file ).completeBaseName() );
return QFileInfo( file ).absolutePath() + "/" +
QFileInfo( file ).completeBaseName();
}
@@ -86,13 +86,13 @@ static std::string getCurrentYear()
inline void loadTranslation( const QString & _tname,
const QString & _dir = ConfigManager::inst()->localeDir() )
inline void loadTranslation( const QString & tname,
const QString & dir = ConfigManager::inst()->localeDir() )
{
QTranslator * t = new QTranslator( QCoreApplication::instance() );
QString name = _tname + ".qm";
QString name = tname + ".qm";
t->load( name, _dir );
t->load( name, dir );
QCoreApplication::instance()->installTranslator( t );
}
@@ -100,7 +100,7 @@ inline void loadTranslation( const QString & _tname,
void printVersion(char *executableName)
void printVersion( char *executableName )
{
printf( "LMMS %s\n(%s %s, Qt %s, %s)\n\n"
"Copyright (c) 2004-%s LMMS developers.\n\n"
@@ -120,36 +120,36 @@ void printHelp()
{
printf( "LMMS %s\n"
"Copyright (c) 2004-%s LMMS developers.\n\n"
"usage: lmms [ -r <project file> ] [ options ]\n"
"Usage: lmms [ -r <project file> ] [ options ]\n"
" [ -u <in> <out> ]\n"
" [ -d <in> ]\n"
" [ -h ]\n"
" [ <file to load> ]\n\n"
"-r, --render <project file> render given project file\n"
"-o, --output <file> render into <file>\n"
"-f, --output-format <format> specify format of render-output where\n"
" format is either 'wav' or 'ogg'.\n"
"-s, --samplerate <samplerate> specify output samplerate in Hz\n"
" range: 44100 (default) to 192000\n"
"-b, --bitrate <bitrate> specify output bitrate in kHz\n"
" default: 160.\n"
"-i, --interpolation <method> specify interpolation method\n"
" possible values:\n"
"-r, --render <project file> Render given project file\n"
"-o, --output <file> Render into <file>\n"
"-f, --output-format <format> Specify format of render-output where\n"
" Format is either 'wav' or 'ogg'.\n"
"-s, --samplerate <samplerate> Specify output samplerate in Hz\n"
" Range: 44100 (default) to 192000\n"
"-b, --bitrate <bitrate> Specify output bitrate in KBit/s\n"
" Default: 160.\n"
"-i, --interpolation <method> Specify interpolation method\n"
" Possible values:\n"
" - linear\n"
" - sincfastest (default)\n"
" - sincmedium\n"
" - sincbest\n"
"-x, --oversampling <value> specify oversampling\n"
" possible values: 1, 2, 4, 8\n"
" default: 2\n"
"-x, --oversampling <value> Specify oversampling\n"
" Possible values: 1, 2, 4, 8\n"
" Default: 2\n"
"-a, --float 32bit float bit depth\n"
"-l, --loop-mode render as a loop\n"
"-u, --upgrade <in> [out] upgrade file <in> and save as <out>\n"
" standard out is used if no output file is specifed\n"
"-d, --dump <in> dump XML of compressed file <in>\n"
"-v, --version show version information and exit.\n"
" --allowroot bypass root user startup check (use with caution).\n"
"-h, --help show this usage information and exit.\n\n",
"-l, --loop-mode Render as a loop\n"
"-u, --upgrade <in> [out] Upgrade file <in> and save as <out>\n"
" Standard out is used if no output file is specifed\n"
"-d, --dump <in> Dump XML of compressed file <in>\n"
"-v, --version Show version information and exit.\n"
" --allowroot Bypass root user startup check (use with caution).\n"
"-h, --help Show this usage information and exit.\n\n",
LMMS_VERSION, getCurrentYear().c_str() );
}
@@ -167,27 +167,29 @@ int main( int argc, char * * argv )
disable_denormals();
bool core_only = false;
bool coreOnly = false;
bool fullscreen = true;
bool exit_after_import = false;
bool allow_root = false;
bool render_loop = false;
QString file_to_load, file_to_save, file_to_import, render_out, profilerOutputFile;
bool exitAfterImport = false;
bool allowRoot = false;
bool renderLoop = false;
QString fileToLoad, fileToImport, renderOut, profilerOutputFile;
// first of two command-line parsing stages
for( int i = 1; i < argc; ++i )
{
if( argc > i && ( ( QString( argv[i] ) == "--render" ||
QString( argv[i] ) == "-r" ) ||
( QString( argv[i] ) == "--help" ||
QString( argv[i] ) == "-h" ) ) )
QString arg = argv[i];
if( arg == "--help" || arg == "-h" ||
arg == "--version" || arg == "-v" ||
arg == "--render" || arg == "-r" )
{
core_only = true;
coreOnly = true;
}
else if( argc > i && ( QString( argv[i] ) == "--allowroot" ) )
else if( arg == "--allowroot" )
{
allow_root = true;
allowRoot = true;
}
else if( argc > i && QString( argv[i] ) == "-geometry" )
else if( arg == "-geometry" )
{
// option -geometry is filtered by Qt later,
// so we need to check its presence now to
@@ -198,14 +200,14 @@ int main( int argc, char * * argv )
}
#ifndef LMMS_BUILD_WIN32
if ( ( getuid() == 0 || geteuid() == 0 ) && !allow_root )
if ( ( getuid() == 0 || geteuid() == 0 ) && !allowRoot )
{
printf("LMMS cannot be run as root.\nUse \"--allowroot\" to override.\n\n");
return(EXIT_FAILURE);
printf( "LMMS cannot be run as root.\nUse \"--allowroot\" to override.\n\n" );
return EXIT_FAILURE;
}
#endif
QCoreApplication * app = core_only ?
QCoreApplication * app = coreOnly ?
new QCoreApplication( argc, argv ) :
new QApplication( argc, argv ) ;
@@ -214,80 +216,125 @@ int main( int argc, char * * argv )
ProjectRenderer::Depth_16Bit );
ProjectRenderer::ExportFileFormats eff = ProjectRenderer::WaveFile;
// second of two command-line parsing stages
for( int i = 1; i < argc; ++i )
{
if( QString( argv[i] ) == "--version" ||
QString( argv[i] ) == "-v" )
QString arg = argv[i];
if( arg == "--version" || arg == "-v" )
{
printVersion(argv[0]);
return( EXIT_SUCCESS );
printVersion( argv[0] );
return EXIT_SUCCESS;
}
else if( argc > i && ( QString( argv[i] ) == "--help" ||
QString( argv[i] ) == "-h" ) )
else if( arg == "--help" || arg == "-h" )
{
printHelp();
return( EXIT_SUCCESS );
return EXIT_SUCCESS;
}
else if( argc > i+1 && ( QString( argv[i] ) == "--upgrade" ||
QString( argv[i] ) == "-u" ) )
else if( arg == "--upgrade" || arg == "-u" )
{
DataFile dataFile( QString::fromLocal8Bit( argv[i + 1] ) );
if (argc > i+2)
++i;
if( i == argc )
{
dataFile.writeFile( QString::fromLocal8Bit( argv[i + 2] ) );
printf( "\nNo input file specified.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[0] );
return EXIT_FAILURE;
}
else
DataFile dataFile( QString::fromLocal8Bit( argv[i] ) );
if( argc > i+1 ) // output file specified
{
dataFile.writeFile( QString::fromLocal8Bit( argv[i+1] ) );
}
else // no output file specified; use stdout
{
QTextStream ts( stdout );
dataFile.write( ts );
fflush( stdout );
}
return( EXIT_SUCCESS );
return EXIT_SUCCESS;
}
else if( argc > i && QString( argv[i] ) == "--allowroot" )
else if( arg == "--allowroot" )
{
// Ignore, processed earlier
#ifdef LMMS_BUILD_WIN32
if ( allow_root )
if( allowRoot )
{
printf( "\nOption \"--allowroot\" will be ignored on this platform.\n\n" );
}
#endif
}
else if( argc > i && ( QString( argv[i] ) == "--dump" ||
QString( argv[i] ) == "-d" ) )
else if( arg == "--dump" || arg == "-d" )
{
QFile f( QString::fromLocal8Bit( argv[i + 1] ) );
++i;
if( i == argc )
{
printf( "\nNo input file specified.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[0] );
return EXIT_FAILURE;
}
QFile f( QString::fromLocal8Bit( argv[i] ) );
f.open( QIODevice::ReadOnly );
QString d = qUncompress( f.readAll() );
printf( "%s\n", d.toUtf8().constData() );
return( EXIT_SUCCESS );
return EXIT_SUCCESS;
}
else if( argc > i && ( QString( argv[i] ) == "--render" ||
QString( argv[i] ) == "-r" ) )
else if( arg == "--render" || arg == "-r" )
{
file_to_load = QString::fromLocal8Bit( argv[i + 1] );
render_out = baseName( file_to_load ) + ".";
++i;
if( i == argc )
{
printf( "\nNo input file specified.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[0] );
return EXIT_FAILURE;
}
fileToLoad = QString::fromLocal8Bit( argv[i] );
renderOut = baseName( fileToLoad ) + ".";
}
else if( argc > i && ( QString( argv[i] ) == "--loop-mode" ||
QString( argv[i] ) == "-l" ) )
else if( arg == "--loop-mode" || arg == "-l" )
{
render_loop = true;
renderLoop = true;
}
else if( argc > i && ( QString( argv[i] ) == "--output" ||
QString( argv[i] ) == "-o" ) )
else if( arg == "--output" || arg == "-o" )
{
render_out = baseName( QString::fromLocal8Bit( argv[i + 1] ) ) + ".";
++i;
if( i == argc )
{
printf( "\nNo output file specified.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[0] );
return EXIT_FAILURE;
}
renderOut = baseName( QString::fromLocal8Bit( argv[i] ) ) + ".";
}
else if( argc > i &&
( QString( argv[i] ) == "--format" ||
QString( argv[i] ) == "-f" ) )
else if( arg == "--format" || arg == "-f" )
{
const QString ext = QString( argv[i + 1] );
++i;
if( i == argc )
{
printf( "\nNo output format specified.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[0] );
return EXIT_FAILURE;
}
const QString ext = QString( argv[i] );
if( ext == "wav" )
{
eff = ProjectRenderer::WaveFile;
@@ -301,16 +348,23 @@ int main( int argc, char * * argv )
else
{
printf( "\nInvalid output format %s.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[i + 1], argv[0] );
return( EXIT_FAILURE );
"Try \"%s --help\" for more information.\n\n", argv[i], argv[0] );
return EXIT_FAILURE;
}
++i;
}
else if( argc > i &&
( QString( argv[i] ) == "--samplerate" ||
QString( argv[i] ) == "-s" ) )
else if( arg == "--samplerate" || arg == "-s" )
{
sample_rate_t sr = QString( argv[i + 1] ).toUInt();
++i;
if( i == argc )
{
printf( "\nNo samplerate specified.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[0] );
return EXIT_FAILURE;
}
sample_rate_t sr = QString( argv[i] ).toUInt();
if( sr >= 44100 && sr <= 192000 )
{
os.samplerate = sr;
@@ -318,16 +372,24 @@ int main( int argc, char * * argv )
else
{
printf( "\nInvalid samplerate %s.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[i + 1], argv[0] );
return( EXIT_FAILURE );
"Try \"%s --help\" for more information.\n\n", argv[i], argv[0] );
return EXIT_FAILURE;
}
++i;
}
else if( argc > i &&
( QString( argv[i] ) == "--bitrate" ||
QString( argv[i] ) == "-b" ) )
else if( arg == "--bitrate" || arg == "-b" )
{
int br = QString( argv[i + 1] ).toUInt();
++i;
if( i == argc )
{
printf( "\nNo bitrate specified.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[0] );
return EXIT_FAILURE;
}
int br = QString( argv[i] ).toUInt();
if( br >= 64 && br <= 384 )
{
os.bitrate = br;
@@ -335,22 +397,28 @@ int main( int argc, char * * argv )
else
{
printf( "\nInvalid bitrate %s.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[i + 1], argv[0] );
return( EXIT_FAILURE );
"Try \"%s --help\" for more information.\n\n", argv[i], argv[0] );
return EXIT_FAILURE;
}
++i;
}
else if ( argc > i &&
( QString( argv[i] ) =="--float" ||
QString( argv[i] ) == "-a" ) )
else if( arg =="--float" || arg == "-a" )
{
os.depth = ProjectRenderer::Depth_32Bit;
}
else if( argc > i &&
( QString( argv[i] ) == "--interpolation" ||
QString( argv[i] ) == "-i" ) )
else if( arg == "--interpolation" || arg == "-i" )
{
const QString ip = QString( argv[i + 1] );
++i;
if( i == argc )
{
printf( "\nNo interpolation method specified.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[0] );
return EXIT_FAILURE;
}
const QString ip = QString( argv[i] );
if( ip == "linear" )
{
qs.interpolation = Mixer::qualitySettings::Interpolation_Linear;
@@ -370,16 +438,24 @@ int main( int argc, char * * argv )
else
{
printf( "\nInvalid interpolation method %s.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[i + 1], argv[0] );
return( EXIT_FAILURE );
"Try \"%s --help\" for more information.\n\n", argv[i], argv[0] );
return EXIT_FAILURE;
}
++i;
}
else if( argc > i &&
( QString( argv[i] ) == "--oversampling" ||
QString( argv[i] ) == "-x" ) )
else if( arg == "--oversampling" || arg == "-x" )
{
int o = QString( argv[i + 1] ).toUInt();
++i;
if( i == argc )
{
printf( "\nNo oversampling specified.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[0] );
return EXIT_FAILURE;
}
int o = QString( argv[i] ).toUInt();
switch( o )
{
case 1:
@@ -396,26 +472,44 @@ int main( int argc, char * * argv )
break;
default:
printf( "\nInvalid oversampling %s.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[i + 1], argv[0] );
return( EXIT_FAILURE );
"Try \"%s --help\" for more information.\n\n", argv[i], argv[0] );
return EXIT_FAILURE;
}
++i;
}
else if( argc > i &&
( QString( argv[i] ) == "--import" ) )
else if( arg == "--import" )
{
file_to_import = QString::fromLocal8Bit( argv[i+1] );
++i;
// exit after import? (only for debugging)
if( argc > i && QString( argv[i+1] ) == "-e" )
if( i == argc )
{
exit_after_import = true;
printf( "\nNo file specified for importing.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[0] );
return EXIT_FAILURE;
}
fileToImport = QString::fromLocal8Bit( argv[i] );
// exit after import? (only for debugging)
if( QString( argv[i + 1] ) == "-e" )
{
exitAfterImport = true;
++i;
}
}
else if( argc > i && ( QString( argv[i] ) == "--profile" || QString( argv[i] ) == "-p" ) )
else if( arg == "--profile" || arg == "-p" )
{
profilerOutputFile = QString::fromLocal8Bit( argv[i+1] );
++i;
if( i == argc )
{
printf( "\nNo profile specified.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[0] );
return EXIT_FAILURE;
}
profilerOutputFile = QString::fromLocal8Bit( argv[1] );
}
else
{
@@ -423,9 +517,9 @@ int main( int argc, char * * argv )
{
printf( "\nInvalid option %s.\n\n"
"Try \"%s --help\" for more information.\n\n", argv[i], argv[0] );
return( EXIT_FAILURE );
return EXIT_FAILURE;
}
file_to_load = QString::fromLocal8Bit( argv[i] );
fileToLoad = QString::fromLocal8Bit( argv[i] );
}
}
@@ -475,7 +569,39 @@ int main( int argc, char * * argv )
}
#endif
if( render_out.isEmpty() )
// if we have an output file for rendering, just render the song
// without starting the GUI
if( !renderOut.isEmpty() )
{
Engine::init( true );
printf( "Loading project...\n" );
Engine::getSong()->loadProject( fileToLoad );
printf( "Done\n" );
Engine::getSong()->setExportLoop( renderLoop );
// create renderer
QString extension = ( eff == ProjectRenderer::WaveFile ) ? "wav" : "ogg";
ProjectRenderer * r = new ProjectRenderer( qs, os, eff, renderOut + extension );
QCoreApplication::instance()->connect( r,
SIGNAL( finished() ), SLOT( quit() ) );
// timer for progress-updates
QTimer * t = new QTimer( r );
r->connect( t, SIGNAL( timeout() ),
SLOT( updateConsoleProgress() ) );
t->start( 200 );
if( profilerOutputFile.isEmpty() == false )
{
Engine::mixer()->profiler().setOutputFile( profilerOutputFile );
}
// start now!
r->startProcessing();
}
else // otherwise, start the GUI
{
new GuiApplication();
@@ -492,32 +618,33 @@ int main( int argc, char * * argv )
"Do you want to recover the project of this session?" ),
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )
{
file_to_load = recoveryFile;
fileToLoad = recoveryFile;
}
// we try to load given file
if( !file_to_load.isEmpty() )
if( !fileToLoad.isEmpty() )
{
gui->mainWindow()->show();
if( fullscreen )
{
gui->mainWindow()->showMaximized();
}
if( file_to_load == recoveryFile )
if( fileToLoad == recoveryFile )
{
Engine::getSong()->createNewProjectFromTemplate( file_to_load );
Engine::getSong()->createNewProjectFromTemplate( fileToLoad );
}
else
{
Engine::getSong()->loadProject( file_to_load );
Engine::getSong()->loadProject( fileToLoad );
}
}
else if( !file_to_import.isEmpty() )
else if( !fileToImport.isEmpty() )
{
ImportFilter::import( file_to_import, Engine::getSong() );
if( exit_after_import )
ImportFilter::import( fileToImport, Engine::getSong() );
if( exitAfterImport )
{
return 0;
return EXIT_SUCCESS;
}
gui->mainWindow()->show();
@@ -538,38 +665,6 @@ int main( int argc, char * * argv )
gui->mainWindow()->showMaximized();
}
}
}
else
{
// we're going to render our song
Engine::init( true );
printf( "loading project...\n" );
Engine::getSong()->loadProject( file_to_load );
printf( "done\n" );
Engine::getSong()->setExportLoop(render_loop);
// create renderer
QString extension = ( eff == ProjectRenderer::WaveFile ) ? "wav" : "ogg";
ProjectRenderer * r = new ProjectRenderer( qs, os, eff, render_out + extension );
QCoreApplication::instance()->connect( r,
SIGNAL( finished() ), SLOT( quit() ) );
// timer for progress-updates
QTimer * t = new QTimer( r );
r->connect( t, SIGNAL( timeout() ),
SLOT( updateConsoleProgress() ) );
t->start( 200 );
if( profilerOutputFile.isEmpty() == false )
{
Engine::mixer()->profiler().setOutputFile( profilerOutputFile );
}
// start now!
r->startProcessing();
}
const int ret = app->exec();
@@ -578,5 +673,5 @@ int main( int argc, char * * argv )
// cleanup memory managers
MemoryManager::cleanup();
return( ret );
return ret;
}