LameXP/src/Main.cpp

278 lines
9.4 KiB
C++
Raw Normal View History

2010-11-06 23:04:47 +01:00
///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
2014-01-01 17:05:52 +01:00
// Copyright (C) 2004-2014 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
2013-10-23 20:56:57 +02:00
// (at your option) any later version, but always including the *additional*
// restrictions defined in the "License.txt" file.
2010-11-06 23:04:47 +01:00
//
// 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"
#include "Dialog_Processing.h"
2010-11-06 23:04:47 +01:00
#include "Thread_Initialization.h"
#include "Thread_MessageProducer.h"
#include "Model_Settings.h"
#include "Model_FileList.h"
#include "Model_AudioFile.h"
#include "Encoder_Abstract.h"
#include "WinSevenTaskbar.h"
2010-11-06 23:04:47 +01:00
//MUitls
#include <MUtils/Global.h>
#include <MUtils/OSSupport.h>
#include <MUtils/Version.h>
#include <MUtils/CPUFeatures.h>
2010-11-06 23:04:47 +01:00
//Qt includes
#include <QApplication>
#include <QMessageBox>
#include <QDate>
#include <QMutex>
#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
{
int iResult = -1;
int iShutdown = shutdownFlag_None;
bool bAccepted = true;
//Get CLI arguments
const QStringList &arguments = lamexp_arguments();
//Init console
lamexp_init_console(arguments);
2010-11-06 23:04:47 +01:00
//Print version info
qDebug("LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d)", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build());
qDebug("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.", qMax(MUtils::Version::build_date().year(), MUtils::OS::current_date().year()));
qDebug("Built on %s at %s with %s for Win-%s.\n", MUTILS_UTF8(MUtils::Version::build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::build_time().toString(Qt::ISODate)), MUtils::Version::compiler_version(), MUtils::Version::compiler_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");
qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
qDebug("Note that this program is distributed with ABSOLUTELY NO WARRANTY.\n");
2010-11-06 23:04:47 +01:00
//Print warning, if this is a "debug" build
if(MUTILS_DEBUG)
2011-04-14 01:40:11 +02:00
{
qWarning("---------------------------------------------------------");
qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
qWarning("---------------------------------------------------------\n");
2011-04-14 01:40:11 +02:00
}
//Enumerate CLI arguments
qDebug("Command-Line Arguments:");
for(int i = 0; i < arguments.count(); i++)
{
qDebug("argv[%d]=%s", i, MUTILS_UTF8(arguments.at(i)));
}
qDebug("");
//Detect CPU capabilities
const MUtils::CPUFetaures::cpu_info_t cpuFeatures = MUtils::CPUFetaures::detect(lamexp_arguments());
qDebug(" CPU vendor id : %s (Intel=%s)", cpuFeatures.vendor, MUTILS_BOOL2STR(cpuFeatures.intel));
qDebug("CPU brand string : %s", cpuFeatures.brand);
qDebug(" CPU signature : Family=%d Model=%d Stepping=%d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
qDebug("CPU capabilities : MMX=%s SSE=%s SSE2=%s SSE3=%s SSSE3=%s SSE4=%s SSE4.2=%s x64=%s", MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_MMX), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE2), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE3), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSSE3), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE4), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE42), MUTILS_BOOL2STR(cpuFeatures.x64));
qDebug(" Number of CPU's : %d\n", cpuFeatures.count);
2010-11-06 23:04:47 +01:00
//Initialize Qt
if(!lamexp_init_qt(argc, argv))
{
return -1;
}
2010-11-06 23:04:47 +01:00
//Check for expiration
if(lamexp_version_demo())
{
const QDate currentDate = MUtils::OS::current_date();
if(currentDate.addDays(1) < MUtils::Version::build_date())
{
qFatal("System's date (%s) is before LameXP build date (%s). Huh?", currentDate.toString(Qt::ISODate).toLatin1().constData(), MUtils::Version::build_date().toString(Qt::ISODate).toLatin1().constData());
}
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
}
//Check for multiple instances of LameXP
if((iResult = lamexp_init_ipc()) != 0)
{
qDebug("LameXP is already running, connecting to running instance...");
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();
MUTILS_DELETE(messageProducerThread);
return -1;
}
MUTILS_DELETE(messageProducerThread);
}
return 0;
}
//Kill application?
for(int i = 0; i < argc; i++)
{
if(!arguments[i].compare("--kill", Qt::CaseInsensitive) || !arguments[i].compare("--force-kill", Qt::CaseInsensitive))
{
return 0;
}
}
2010-11-06 23:04:47 +01:00
//Self-test
if(MUTILS_DEBUG)
{
InitializationThread::selfTest();
}
//Taskbar init
WinSevenTaskbar::init();
//Create models
FileListModel *fileListModel = new FileListModel();
AudioFileModel_MetaInfo *metaInfo = new AudioFileModel_MetaInfo();
SettingsModel *settingsModel = new SettingsModel();
2010-11-06 23:04:47 +01:00
//Show splash screen
InitializationThread *poInitializationThread = new InitializationThread(cpuFeatures);
2010-11-06 23:04:47 +01:00
SplashScreen::showSplash(poInitializationThread);
settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
MUTILS_DELETE(poInitializationThread);
2010-11-06 23:04:47 +01:00
2010-12-03 23:01:17 +01:00
//Validate settings
settingsModel->validate();
//Create main window
MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
//Main application loop
while(bAccepted && (iShutdown <= shutdownFlag_None))
{
//Show main window
poMainWindow->show();
iResult = QApplication::instance()->exec();
bAccepted = poMainWindow->isAccepted();
//Sync settings
settingsModel->syncNow();
//Show processing dialog
if(bAccepted && (fileListModel->rowCount() > 0))
{
ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
processingDialog->exec();
iShutdown = processingDialog->getShutdownFlag();
MUTILS_DELETE(processingDialog);
}
}
2010-11-06 23:04:47 +01:00
//Free models
MUTILS_DELETE(poMainWindow);
MUTILS_DELETE(fileListModel);
MUTILS_DELETE(metaInfo);
MUTILS_DELETE(settingsModel);
//Taskbar un-init
WinSevenTaskbar::uninit();
2010-11-06 23:04:47 +01:00
//Final clean-up
qDebug("Shutting down, please wait...\n");
//Shotdown computer
if(iShutdown > shutdownFlag_None)
{
if(!lamexp_shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == shutdownFlag_Hibernate)))
{
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
{
if(MUTILS_DEBUG)
2010-11-06 23:04:47 +01:00
{
int iResult = -1;
2010-11-06 23:04:47 +01:00
qInstallMsgHandler(lamexp_message_handler);
iResult = lamexp_main(argc, argv);
2010-11-06 23:04:47 +01:00
lamexp_finalization();
return iResult;
}
else
2010-11-06 23:04:47 +01:00
{
int iResult = -1;
try
{
qInstallMsgHandler(lamexp_message_handler);
iResult = lamexp_main(argc, argv);
lamexp_finalization();
}
catch(const std::exception &error)
{
PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
}
catch(...)
{
PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
}
return iResult;
2010-11-06 23:04:47 +01:00
}
}
2011-06-22 01:10:53 +02:00
int main(int argc, char* argv[])
{
if(MUTILS_DEBUG)
2011-06-22 01:10:53 +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-06-22 01:10:53 +02:00
__try
{
lamexp_init_error_handlers();
2011-06-22 01:10:53 +02:00
return _main(argc, argv);
}
__except(1)
{
PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnhandeled structured exception error!\n");
MUtils::OS::fatal_exit(L"Unhandeled structured exception error, application will exit!");
2011-06-22 01:10:53 +02:00
}
}
}