2010-11-06 23:04:47 +01:00
///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
2013-02-08 23:50:51 +01:00
// Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
2010-11-06 23:04:47 +01:00
//
// 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.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// http://www.gnu.org/licenses/gpl-2.0.txt
///////////////////////////////////////////////////////////////////////////////
//LameXP includes
# include "Global.h"
# include "Dialog_SplashScreen.h"
# include "Dialog_MainWindow.h"
2010-11-17 19:35:50 +01:00
# include "Dialog_Processing.h"
2010-11-06 23:04:47 +01:00
# include "Thread_Initialization.h"
2010-11-09 22:06:11 +01:00
# include "Thread_MessageProducer.h"
2010-11-12 21:02:14 +01:00
# include "Model_Settings.h"
2010-11-19 21:11:54 +01:00
# include "Model_FileList.h"
# include "Model_AudioFile.h"
2010-11-20 02:14:22 +01:00
# include "Encoder_Abstract.h"
2011-11-07 17:13:41 +01:00
# include "WinSevenTaskbar.h"
2010-11-06 23:04:47 +01:00
//Qt includes
# include <QApplication>
# include <QMessageBox>
# include <QDate>
2010-11-19 13:31:45 +01:00
# include <QMutex>
2010-11-20 02:14:22 +01:00
# include <QDir>
2010-11-06 23:04:47 +01:00
///////////////////////////////////////////////////////////////////////////////
// Main function
///////////////////////////////////////////////////////////////////////////////
2011-06-22 01:10:53 +02:00
static int lamexp_main ( int argc , char * argv [ ] )
2010-11-06 23:04:47 +01:00
{
2010-11-09 22:06:11 +01:00
int iResult = - 1 ;
2011-10-22 01:13:28 +02:00
int iShutdown = shutdownFlag_None ;
2010-11-19 21:11:54 +01:00
bool bAccepted = true ;
2012-06-21 18:31:36 +02:00
2013-04-16 21:54:01 +02:00
//Increase "main" thread priority
SetThreadPriority ( GetCurrentThread ( ) , THREAD_PRIORITY_HIGHEST ) ;
2012-06-22 15:49:56 +02:00
//Get CLI arguments
const QStringList & arguments = lamexp_arguments ( ) ;
2010-11-07 23:06:30 +01:00
//Init console
2012-06-22 15:49:56 +02:00
lamexp_init_console ( arguments ) ;
2010-11-22 21:45:00 +01:00
2010-11-06 23:04:47 +01:00
//Print version info
2011-07-26 22:17:14 +02:00
qDebug ( " LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d) " , lamexp_version_major ( ) , lamexp_version_minor ( ) , lamexp_version_release ( ) , lamexp_version_build ( ) ) ;
2013-01-12 01:53:43 +01:00
qDebug ( " Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved. " , qMax ( lamexp_version_date ( ) . year ( ) , lamexp_current_date_safe ( ) . year ( ) ) ) ;
2011-07-26 22:17:14 +02:00
qDebug ( " Built on %s at %s with %s for Win-%s. \n " , lamexp_version_date ( ) . toString ( Qt : : ISODate ) . toLatin1 ( ) . constData ( ) , lamexp_version_time ( ) , lamexp_version_compiler ( ) , lamexp_version_arch ( ) ) ;
2010-11-06 23:04:47 +01:00
//print license info
qDebug ( " This program is free software: you can redistribute it and/or modify " ) ;
2010-11-08 00:24:54 +01:00
qDebug ( " it under the terms of the GNU General Public License <http://www.gnu.org/>. " ) ;
2011-07-26 22:17:14 +02:00
qDebug ( " Note that this program is distributed with ABSOLUTELY NO WARRANTY. \n " ) ;
2010-11-13 02:11:15 +01:00
2010-11-06 23:04:47 +01:00
//Print warning, if this is a "debug" build
2011-04-14 01:40:11 +02:00
if ( LAMEXP_DEBUG )
{
qWarning ( " --------------------------------------------------------- " ) ;
qWarning ( " DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!! " ) ;
qWarning ( " --------------------------------------------------------- \n " ) ;
}
2010-11-09 22:06:11 +01:00
2012-06-21 18:31:36 +02:00
//Enumerate CLI arguments
qDebug ( " Command-Line Arguments: " ) ;
2012-06-22 15:49:56 +02:00
for ( int i = 0 ; i < arguments . count ( ) ; i + + )
2012-06-21 18:31:36 +02:00
{
2012-06-22 15:49:56 +02:00
qDebug ( " argv[%d]=%s " , i , arguments . at ( i ) . toUtf8 ( ) . constData ( ) ) ;
2012-06-21 18:31:36 +02:00
}
qDebug ( " " ) ;
2010-11-15 04:42:06 +01:00
//Detect CPU capabilities
2012-06-23 18:18:57 +02:00
lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features ( arguments ) ;
2012-07-29 19:09:15 +02:00
qDebug ( " CPU vendor id : %s (Intel: %s) " , cpuFeatures . vendor , LAMEXP_BOOL2STR ( cpuFeatures . intel ) ) ;
2010-11-20 22:14:10 +01:00
qDebug ( " CPU brand string : %s " , cpuFeatures . brand ) ;
qDebug ( " CPU signature : Family: %d, Model: %d, Stepping: %d " , cpuFeatures . family , cpuFeatures . model , cpuFeatures . stepping ) ;
2012-07-29 19:09:15 +02:00
qDebug ( " CPU capabilities : MMX: %s, SSE: %s, SSE2: %s, SSE3: %s, SSSE3: %s, x64: %s " , LAMEXP_BOOL2STR ( cpuFeatures . mmx ) , LAMEXP_BOOL2STR ( cpuFeatures . sse ) , LAMEXP_BOOL2STR ( cpuFeatures . sse2 ) , LAMEXP_BOOL2STR ( cpuFeatures . sse3 ) , LAMEXP_BOOL2STR ( cpuFeatures . ssse3 ) , LAMEXP_BOOL2STR ( cpuFeatures . x64 ) ) ;
2010-11-24 21:00:59 +01:00
qDebug ( " Number of CPU's : %d \n " , cpuFeatures . count ) ;
2012-12-10 23:35:14 +01:00
2010-11-06 23:04:47 +01:00
//Initialize Qt
2010-12-27 01:50:22 +01:00
if ( ! lamexp_init_qt ( argc , argv ) )
{
return - 1 ;
}
2010-11-21 21:51:22 +01:00
2010-11-06 23:04:47 +01:00
//Check for expiration
if ( lamexp_version_demo ( ) )
{
2013-01-12 01:53:43 +01:00
const QDate currentDate = lamexp_current_date_safe ( ) ;
if ( currentDate . addDays ( 1 ) < lamexp_version_date ( ) )
2011-12-25 22:29:04 +01:00
{
2013-01-12 01:53:43 +01:00
qFatal ( " System's date (%s) is before LameXP build date (%s). Huh? " , currentDate . toString ( Qt : : ISODate ) . toLatin1 ( ) . constData ( ) , lamexp_version_date ( ) . toString ( Qt : : ISODate ) . toLatin1 ( ) . constData ( ) ) ;
2011-12-25 22:29:04 +01:00
}
2011-01-20 22:06:59 +01:00
qWarning ( QString ( " Note: This demo (pre-release) version of LameXP will expire at %1. \n " ) . arg ( lamexp_version_expires ( ) . toString ( Qt : : ISODate ) ) . toLatin1 ( ) . constData ( ) ) ;
2010-11-06 23:04:47 +01:00
}
2010-11-08 21:47:35 +01:00
//Check for multiple instances of LameXP
2010-11-09 22:06:11 +01:00
if ( ( iResult = lamexp_init_ipc ( ) ) ! = 0 )
2010-11-08 21:47:35 +01:00
{
qDebug ( " LameXP is already running, connecting to running instance... " ) ;
2010-11-09 22:06:11 +01:00
if ( iResult = = 1 )
{
MessageProducerThread * messageProducerThread = new MessageProducerThread ( ) ;
messageProducerThread - > start ( ) ;
if ( ! messageProducerThread - > wait ( 30000 ) )
{
messageProducerThread - > terminate ( ) ;
QMessageBox messageBox ( QMessageBox : : Critical , " LameXP " , " LameXP is already running, but the running instance doesn't respond! " , QMessageBox : : NoButton , NULL , Qt : : Dialog | Qt : : MSWindowsFixedSizeDialogHint | Qt : : WindowStaysOnTopHint ) ;
messageBox . exec ( ) ;
messageProducerThread - > wait ( ) ;
LAMEXP_DELETE ( messageProducerThread ) ;
return - 1 ;
}
LAMEXP_DELETE ( messageProducerThread ) ;
}
2010-11-08 21:47:35 +01:00
return 0 ;
}
2010-11-09 22:06:11 +01:00
//Kill application?
for ( int i = 0 ; i < argc ; i + + )
2010-11-08 21:47:35 +01:00
{
2012-06-23 18:18:57 +02:00
if ( ! arguments [ i ] . compare ( " --kill " , Qt : : CaseInsensitive ) | | ! arguments [ i ] . compare ( " --force-kill " , Qt : : CaseInsensitive ) )
2010-11-09 22:06:11 +01:00
{
return 0 ;
}
2010-11-08 21:47:35 +01:00
}
2010-11-06 23:04:47 +01:00
2011-10-19 00:24:03 +02:00
//Self-test
if ( LAMEXP_DEBUG )
{
InitializationThread : : selfTest ( ) ;
}
2011-11-07 17:13:41 +01:00
//Taskbar init
WinSevenTaskbar : : init ( ) ;
2010-11-19 21:11:54 +01:00
//Create models
FileListModel * fileListModel = new FileListModel ( ) ;
AudioFileModel * metaInfo = new AudioFileModel ( ) ;
SettingsModel * settingsModel = new SettingsModel ( ) ;
2012-12-08 00:21:58 +01:00
2010-11-06 23:04:47 +01:00
//Show splash screen
2011-04-11 02:53:29 +02:00
InitializationThread * poInitializationThread = new InitializationThread ( & cpuFeatures ) ;
2010-11-06 23:04:47 +01:00
SplashScreen : : showSplash ( poInitializationThread ) ;
2011-06-24 18:17:04 +02:00
settingsModel - > slowStartup ( poInitializationThread - > getSlowIndicator ( ) ) ;
2010-11-06 23:04:47 +01:00
LAMEXP_DELETE ( poInitializationThread ) ;
2010-12-03 23:01:17 +01:00
//Validate settings
settingsModel - > validate ( ) ;
2010-11-20 22:14:10 +01:00
//Create main window
MainWindow * poMainWindow = new MainWindow ( fileListModel , metaInfo , settingsModel ) ;
//Main application loop
2011-12-29 14:42:20 +01:00
while ( bAccepted & & ( iShutdown < = shutdownFlag_None ) )
2010-11-19 21:11:54 +01:00
{
2010-11-20 22:14:10 +01:00
//Show main window
2010-11-19 21:11:54 +01:00
poMainWindow - > show ( ) ;
iResult = QApplication : : instance ( ) - > exec ( ) ;
bAccepted = poMainWindow - > isAccepted ( ) ;
//Show processing dialog
2011-12-29 14:42:20 +01:00
if ( bAccepted & & ( fileListModel - > rowCount ( ) > 0 ) )
2010-11-19 21:11:54 +01:00
{
2010-11-20 19:16:04 +01:00
ProcessingDialog * processingDialog = new ProcessingDialog ( fileListModel , metaInfo , settingsModel ) ;
2010-11-19 21:11:54 +01:00
processingDialog - > exec ( ) ;
2011-10-22 01:13:28 +02:00
iShutdown = processingDialog - > getShutdownFlag ( ) ;
2010-11-19 21:11:54 +01:00
LAMEXP_DELETE ( processingDialog ) ;
}
}
2010-11-06 23:04:47 +01:00
2010-11-19 21:11:54 +01:00
//Free models
2010-11-20 22:14:10 +01:00
LAMEXP_DELETE ( poMainWindow ) ;
2010-11-19 21:11:54 +01:00
LAMEXP_DELETE ( fileListModel ) ;
LAMEXP_DELETE ( metaInfo ) ;
LAMEXP_DELETE ( settingsModel ) ;
2011-02-07 22:01:06 +01:00
2011-11-07 17:13:41 +01:00
//Taskbar un-init
WinSevenTaskbar : : uninit ( ) ;
2010-11-06 23:04:47 +01:00
//Final clean-up
qDebug ( " Shutting down, please wait... \n " ) ;
2010-11-12 21:02:14 +01:00
2011-03-18 01:12:17 +01:00
//Shotdown computer
2011-10-22 01:13:28 +02:00
if ( iShutdown > shutdownFlag_None )
2011-03-18 01:12:17 +01:00
{
2011-10-22 01:13:28 +02:00
if ( ! lamexp_shutdown_computer ( QApplication : : applicationFilePath ( ) , 12 , true , ( iShutdown = = shutdownFlag_Hibernate ) ) )
2011-03-18 01:12:17 +01:00
{
QMessageBox messageBox ( QMessageBox : : Critical , " LameXP " , " Sorry, LameXP was unable to shutdown your computer! " , QMessageBox : : NoButton , NULL , Qt : : Dialog | Qt : : MSWindowsFixedSizeDialogHint | Qt : : WindowStaysOnTopHint ) ;
}
}
2010-11-06 23:04:47 +01:00
//Terminate
return iResult ;
}
///////////////////////////////////////////////////////////////////////////////
// Applicaton entry point
///////////////////////////////////////////////////////////////////////////////
2011-06-22 01:10:53 +02:00
static int _main ( int argc , char * argv [ ] )
2010-11-06 23:04:47 +01:00
{
2011-04-16 21:21:08 +02:00
if ( LAMEXP_DEBUG )
2010-11-06 23:04:47 +01:00
{
2011-03-08 01:12:40 +01:00
int iResult = - 1 ;
2010-11-06 23:04:47 +01:00
qInstallMsgHandler ( lamexp_message_handler ) ;
2012-04-10 01:25:34 +02:00
iResult = lamexp_main ( argc , argv ) ;
2010-11-06 23:04:47 +01:00
lamexp_finalization ( ) ;
return iResult ;
}
2011-04-16 21:21:08 +02:00
else
2010-11-06 23:04:47 +01:00
{
2011-04-17 15:53:28 +02:00
int iResult = - 1 ;
2011-04-16 21:21:08 +02:00
try
{
qInstallMsgHandler ( lamexp_message_handler ) ;
iResult = lamexp_main ( argc , argv ) ;
lamexp_finalization ( ) ;
}
catch ( char * error )
{
fflush ( stdout ) ;
fflush ( stderr ) ;
2011-08-25 03:44:46 +02:00
fprintf ( stderr , " \n GURU MEDITATION !!! \n \n Exception error message: %s \n " , error ) ;
2012-12-11 22:54:00 +01:00
lamexp_fatal_exit ( L " Unhandeled C++ exception error, application will exit! " ) ;
2011-04-16 21:21:08 +02:00
}
catch ( int error )
{
fflush ( stdout ) ;
fflush ( stderr ) ;
2011-08-25 03:44:46 +02:00
fprintf ( stderr , " \n GURU MEDITATION !!! \n \n Exception error code: 0x%X \n " , error ) ;
2012-12-11 22:54:00 +01:00
lamexp_fatal_exit ( L " Unhandeled C++ exception error, application will exit! " ) ;
2011-04-16 21:21:08 +02:00
}
catch ( . . . )
{
fflush ( stdout ) ;
fflush ( stderr ) ;
fprintf ( stderr , " \n GURU MEDITATION !!! \n " ) ;
2012-12-11 22:54:00 +01:00
lamexp_fatal_exit ( L " Unhandeled C++ exception error, application will exit! " ) ;
2011-04-16 21:21:08 +02:00
}
2011-04-17 15:53:28 +02:00
return iResult ;
2010-11-06 23:04:47 +01:00
}
}
2011-03-04 23:40:09 +01:00
2011-06-22 01:10:53 +02:00
int main ( int argc , char * argv [ ] )
2011-03-04 23:40:09 +01:00
{
2011-06-22 01:10:53 +02:00
if ( LAMEXP_DEBUG )
{
2012-04-10 01:25:34 +02:00
int exit_code = - 1 ;
LAMEXP_MEMORY_CHECK ( _main , exit_code , argc , argv ) ;
return exit_code ;
2011-06-22 01:10:53 +02:00
}
else
2011-03-04 23:40:09 +01:00
{
2011-06-22 01:10:53 +02:00
__try
{
SetUnhandledExceptionFilter ( lamexp_exception_handler ) ;
2011-07-26 22:17:14 +02:00
_set_invalid_parameter_handler ( lamexp_invalid_param_handler ) ;
2011-06-22 01:10:53 +02:00
return _main ( argc , argv ) ;
}
__except ( 1 )
{
fflush ( stdout ) ;
fflush ( stderr ) ;
2011-08-25 03:44:46 +02:00
fprintf ( stderr , " \n GURU MEDITATION !!! \n \n Unhandeled structured exception error! [code: 0x%X] \n " , GetExceptionCode ( ) ) ;
2012-12-11 22:54:00 +01:00
lamexp_fatal_exit ( L " Unhandeled structured exception error, application will exit! " ) ;
2011-06-22 01:10:53 +02:00
}
2011-03-04 23:40:09 +01:00
}
2011-03-05 01:56:00 +01:00
}