Updated changelog.
[LameXP.git] / src / Main.cpp
blob6fbbea4ed26e98ec8fcc129b59913092950fb43d
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2023 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GENERAL PUBLIC LICENSE as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 //LameXP includes
24 #include "Global.h"
25 #include "Dialog_SplashScreen.h"
26 #include "Dialog_MainWindow.h"
27 #include "Dialog_Processing.h"
28 #include "Thread_Initialization.h"
29 #include "Thread_MessageProducer.h"
30 #include "Model_Settings.h"
31 #include "Model_FileList.h"
32 #include "Model_AudioFile.h"
33 #include "Encoder_Abstract.h"
34 #include "ShellIntegration.h"
36 //MUitls
37 #include <MUtils/Global.h>
38 #include <MUtils/OSSupport.h>
39 #include <MUtils/Version.h>
40 #include <MUtils/CPUFeatures.h>
41 #include <MUtils/Terminal.h>
42 #include <MUtils/Startup.h>
43 #include <MUtils/IPCChannel.h>
45 //Qt includes
46 #include <QMutex>
47 #include <QApplication>
48 #include <QMessageBox>
49 #include <QDate>
50 #include <QDir>
52 //VLD
53 #ifdef _MSC_VER
54 #include <vld.h>
55 #endif
59 ///////////////////////////////////////////////////////////////////////////////
60 // Helper functions
61 ///////////////////////////////////////////////////////////////////////////////
63 static void lamexp_print_logo(void)
65 //Print version info
66 qDebug("LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d)", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build());
67 qDebug("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.", qMax(MUtils::Version::app_build_date().year(), MUtils::OS::current_date().year()));
68 qDebug("Built on %s at %s with %s for Win-%s.\n", MUTILS_UTF8(MUtils::Version::app_build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::app_build_time().toString(Qt::ISODate)), MUtils::Version::compiler_version(), MUtils::Version::compiler_arch());
70 //print license info
71 qDebug("This program is free software: you can redistribute it and/or modify");
72 qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
73 qDebug("Note that this program is distributed with ABSOLUTELY NO WARRANTY.\n");
75 //Print library version
76 qDebug("This application is powerd by MUtils library v%u.%02u (%s, %s).\n", MUtils::Version::lib_version_major(), MUtils::Version::lib_version_minor(), MUTILS_UTF8(MUtils::Version::lib_build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::lib_build_time().toString(Qt::ISODate)));
78 //Print warning, if this is a "debug" build
79 if(MUTILS_DEBUG)
81 qWarning("---------------------------------------------------------");
82 qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
83 qWarning("---------------------------------------------------------\n");
87 static int lamexp_initialize_ipc(MUtils::IPCChannel *const ipcChannel)
89 int iResult = 0;
91 if((iResult = ipcChannel->initialize()) != MUtils::IPCChannel::RET_SUCCESS_MASTER)
93 if(iResult == MUtils::IPCChannel::RET_SUCCESS_SLAVE)
95 qDebug("LameXP is already running, connecting to running instance...");
96 QScopedPointer<MessageProducerThread> messageProducerThread(new MessageProducerThread(ipcChannel));
97 messageProducerThread->start();
98 if(!messageProducerThread->wait(30000))
100 qWarning("MessageProducer thread has encountered timeout -> going to kill!");
101 messageProducerThread->terminate();
102 messageProducerThread->wait();
103 MUtils::OS::system_message_err(L"LameXP", L"LameXP is already running, but the running instance doesn't respond!");
104 return -1;
106 return 0;
108 else
110 qFatal("The IPC initialization has failed!");
111 return -1;
115 return 1;
118 static void initialize_lamexp(const MUtils::OS::ArgumentMap &arguments, const MUtils::CPUFetaures::cpu_info_t &cpuFeatures, SettingsModel *const settingsModel)
120 QScopedPointer<InitializationThread> poInitializationThread(new InitializationThread(cpuFeatures));
121 if (!arguments.contains("no-splash"))
123 SplashScreen::showSplash(poInitializationThread.data());
124 settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
126 else
128 poInitializationThread->runSyncronized(); /*no splash*/
132 static int lamexp_main_loop(const MUtils::OS::ArgumentMap &arguments, const MUtils::CPUFetaures::cpu_info_t &cpuFeatures, MUtils::IPCChannel *const ipcChannel, int &iShutdown)
134 int iResult = -1;
135 bool bAccepted = true;
137 //Create models
138 QScopedPointer<FileListModel> fileListModel(new FileListModel() );
139 QScopedPointer<AudioFileModel_MetaInfo> metaInfoModel(new AudioFileModel_MetaInfo());
140 QScopedPointer<SettingsModel> settingsModel(new SettingsModel() );
142 //Show splash screen
143 initialize_lamexp(arguments, cpuFeatures, settingsModel.data());
145 //Validate settings
146 settingsModel->validate();
148 //Create main window
149 QScopedPointer<MainWindow> poMainWindow(new MainWindow(ipcChannel, fileListModel.data(), metaInfoModel.data(), settingsModel.data()));
151 //Main application loop
152 while(bAccepted && (iShutdown <= SHUTDOWN_FLAG_NONE))
154 //Show main window
155 poMainWindow->show();
156 iResult = qApp->exec();
157 bAccepted = poMainWindow->isAccepted();
159 //Sync settings
160 settingsModel->syncNow();
162 //Show processing dialog
163 if(bAccepted && (fileListModel->rowCount() > 0))
165 ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel.data(), metaInfoModel.data(), settingsModel.data());
166 processingDialog->exec();
167 iShutdown = processingDialog->getShutdownFlag();
168 MUTILS_DELETE(processingDialog);
172 return iResult;
175 ///////////////////////////////////////////////////////////////////////////////
176 // Main function
177 ///////////////////////////////////////////////////////////////////////////////
179 #pragma warning(disable: 4456)
181 static int lamexp_main(int &argc, char **argv)
183 int iResult = -1;
184 int iShutdown = SHUTDOWN_FLAG_NONE;
186 //Print logo
187 lamexp_print_logo();
189 //Get CLI arguments
190 const MUtils::OS::ArgumentMap &arguments = MUtils::OS::arguments();
192 //Enumerate CLI arguments
193 if(!arguments.isEmpty())
195 qDebug("Command-Line Arguments:");
196 foreach(const QString &key, arguments.uniqueKeys())
198 foreach(const QString &val, arguments.values(key))
200 if(!val.isEmpty())
202 qDebug("--%s = \"%s\"", MUTILS_UTF8(key), MUTILS_UTF8(val));
203 continue;
205 qDebug("--%s", MUTILS_UTF8(key));
208 qDebug(" ");
211 //Uninstall?
212 if(arguments.contains("uninstall"))
214 qWarning("Un-install: Removing LameXP shell integration...");
215 ShellIntegration::remove(false);
216 return EXIT_SUCCESS;
219 //Detect CPU capabilities
220 const MUtils::CPUFetaures::cpu_info_t cpuFeatures = MUtils::CPUFetaures::detect();
221 qDebug(" CPU vendor id : %s (Intel=%s)", cpuFeatures.idstr, MUTILS_BOOL2STR(cpuFeatures.vendor & MUtils::CPUFetaures::VENDOR_INTEL));
222 qDebug("CPU brand string : %s", cpuFeatures.brand);
223 qDebug(" CPU signature : Family=%d, Model=%d, Stepping=%d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
224 qDebug("CPU architecture : %s", cpuFeatures.x64 ? "x64 (64-Bit)" : "x86 (32-Bit)");
225 qDebug("CPU capabilities : CMOV=%s, MMX=%s, SSE=%s, SSE2=%s, SSE3=%s, SSSE3=%s", MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_CMOV), 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));
226 qDebug("CPU capabilities : SSE4.1=%s, SSE4.2=%s, AVX=%s, AVX2=%s, FMA3=%s, LZCNT=%s", MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE41), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE42), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_AVX), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_AVX2), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_FMA3), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_LZCNT));
227 qDebug(" Number of CPU's : %d\n", cpuFeatures.count);
229 //Initialize Qt
230 QScopedPointer<QApplication> application(MUtils::Startup::create_qt(argc, argv, QLatin1String("LameXP - Audio Encoder Front-End")));
231 if(application.isNull())
233 return EXIT_FAILURE;
236 //Initialize application
237 application->setWindowIcon(lamexp_app_icon());
238 application->setApplicationVersion(QString().sprintf("%d.%02d.%04d", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build()));
240 //Initialize resources
241 lamexp_initialize_resources();
243 //Check for expiration
244 if(lamexp_version_test())
246 const QDate currentDate = MUtils::OS::current_date();
247 if(currentDate.addDays(1) < MUtils::Version::app_build_date())
249 qFatal("System's date (%s) is before LameXP build date (%s). Huh?", currentDate.toString(Qt::ISODate).toLatin1().constData(), MUtils::Version::app_build_date().toString(Qt::ISODate).toLatin1().constData());
251 qWarning(QString("Note: This test (pre-release) version of LameXP will expire at %1.\n").arg(lamexp_version_expires().toString(Qt::ISODate)).toLatin1().constData());
254 //Initialize IPC
255 QScopedPointer<MUtils::IPCChannel> ipcChannel(new MUtils::IPCChannel("lamexp-v4", lamexp_version_build(), "instance"));
256 if((iResult = lamexp_initialize_ipc(ipcChannel.data())) < 1)
258 return (iResult == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
261 //Kill application?
262 if(arguments.contains("kill") || arguments.contains("force-kill"))
264 return EXIT_SUCCESS;
267 //Self-test
268 if(MUTILS_DEBUG)
270 InitializationThread::selfTest();
273 //Main application loop
274 iResult = lamexp_main_loop(arguments, cpuFeatures, ipcChannel.data(), iShutdown);
276 //Final clean-up
277 qDebug("Shutting down, please wait...\n");
279 //Shotdown computer
280 if(iShutdown > SHUTDOWN_FLAG_NONE)
282 if(!MUtils::OS::shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == SHUTDOWN_FLAG_HIBERNATE)))
284 QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
288 //Terminate
289 return iResult;
292 ///////////////////////////////////////////////////////////////////////////////
293 // Applicaton entry point
294 ///////////////////////////////////////////////////////////////////////////////
296 int main(int argc, char* argv[])
298 return MUtils::Startup::startup(argc, argv, lamexp_main, "LameXP", lamexp_version_test());