OSDN Git Service

Happy new year 2020!
[x264-launcher/x264-launcher.git] / src / main.cpp
index db85c6f..796689a 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // Simple x264 Launcher
-// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2020 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // 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
 // http://www.gnu.org/licenses/gpl-2.0.txt
 ///////////////////////////////////////////////////////////////////////////////
 
+//Internal
 #include "global.h"
 #include "win_main.h"
+#include "cli.h"
 #include "ipc.h"
-#include "taskbar7.h"
+#include "thread_ipc_send.h"
+
+//MUtils
+#include <MUtils/Startup.h>
+#include <MUtils/OSSupport.h>
+#include <MUtils/CPUFeatures.h>
+#include <MUtils/IPCChannel.h>
+#include <MUtils/Version.h>
 
 //Qt includes
 #include <QApplication>
 #define WIN32_LEAN_AND_MEAN
 #include <Windows.h>
 
-//Forward declaration
-void handleMultipleInstances(QStringList args, IPC *ipc);
-
 ///////////////////////////////////////////////////////////////////////////////
-// Main function
+// Helper functions
 ///////////////////////////////////////////////////////////////////////////////
 
-static int x264_main(int argc, char* argv[])
+static void x264_print_logo(void)
 {
-       //Init console
-       x264_init_console(argc, argv);
-
        //Print version info
        qDebug("Simple x264 Launcher v%u.%02u.%u - use 64-Bit x264 with 32-Bit Avisynth", x264_version_major(), x264_version_minor(), x264_version_build());
-       qDebug("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.", qMax(x264_version_date().year(),QDate::currentDate().year()));
-       qDebug("Built on %s at %s with %s for Win-%s.\n", x264_version_date().toString(Qt::ISODate).toLatin1().constData(), x264_version_time(), x264_version_compiler(), x264_version_arch());
-       
+       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()));
+       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());
+
        //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");
 
+       //Print library version
+       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)));
+
        //Print warning, if this is a "debug" build
-       if(X264_DEBUG)
+       if(MUTILS_DEBUG)
        {
                qWarning("---------------------------------------------------------");
                qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
                qWarning("---------------------------------------------------------\n"); 
        }
+}
 
-       //Get CLI arguments
-       const QStringList &arguments = x264_arguments();
-       
-       //Detect CPU capabilities
-       const x264_cpu_t cpuFeatures = x264_detect_cpu_features(arguments);
-       qDebug("   CPU vendor id  :  %s (Intel: %s)", cpuFeatures.vendor, X264_BOOL(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, MMXEXT=%s, SSE=%s, SSE2=%s, SSE3=%s, SSSE3=%s, X64=%s", X264_BOOL(cpuFeatures.mmx), X264_BOOL(cpuFeatures.mmx2), X264_BOOL(cpuFeatures.sse), X264_BOOL(cpuFeatures.sse2), X264_BOOL(cpuFeatures.sse3), X264_BOOL(cpuFeatures.ssse3), X264_BOOL(cpuFeatures.x64));
-       qDebug(" Number of CPU's  :  %d\n", cpuFeatures.count);
+static int x264_initialize_ipc(MUtils::IPCChannel *const ipcChannel)
+{
+               int iResult = 0;
 
-       //Initialize the IPC handler class
-       bool firstInstance = false;
-       IPC *ipc = new IPC();
-       if(!ipc->initialize(firstInstance))
+       if((iResult = ipcChannel->initialize()) != MUtils::IPCChannel::RET_SUCCESS_MASTER)
        {
-               if(!firstInstance)
+               if(iResult == MUtils::IPCChannel::RET_SUCCESS_SLAVE)
                {
-                       handleMultipleInstances(arguments, ipc);
-                       X264_DELETE(ipc);
+                       qDebug("Simple x264 Launcher is already running, connecting to running instance...");
+                       QScopedPointer<IPCThread_Send> messageProducerThread(new IPCThread_Send(ipcChannel));
+                       messageProducerThread->start();
+                       if(!messageProducerThread->wait(30000))
+                       {
+                               qWarning("MessageProducer thread has encountered timeout -> going to kill!");
+                               messageProducerThread->terminate();
+                               messageProducerThread->wait();
+                               MUtils::OS::system_message_err(L"Simple x264 Launcher", L"Simple x264 Launcher is already running, but the running instance doesn't respond!");
+                               return -1;
+                       }
                        return 0;
                }
-       }
-       else
-       {
-               qWarning("IPC initialization has failed!");
-       }
-
-       //Initialize Qt
-       if(!x264_init_qt(argc, argv))
-       {
-               return -1;
-       }
-       
-       //Running in portable mode?
-       if(x264_portable())
-       {
-               qDebug("Application is running in portable mode!\n");
-       }
-
-       //Taskbar init
-       WinSevenTaskbar::init();
-
-       //Set style
-       if(!qApp->arguments().contains("--no-style", Qt::CaseInsensitive))
-       {
-               qApp->setStyle(new QPlastiqueStyle());
+               else
+               {
+                       qFatal("The IPC initialization has failed!");
+                       return -1;
+               }
        }
 
-       //Create Main Window
-       MainWindow *mainWin = new MainWindow(&cpuFeatures);
-       mainWin->show();
-
-       //Run application
-       int ret = qApp->exec();
-
-       //Taskbar uninit
-       WinSevenTaskbar::init();
-       
-       X264_DELETE(mainWin);
-       return ret;
+       return 1;
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-// Multi-instance handler
+// Main function
 ///////////////////////////////////////////////////////////////////////////////
 
-void handleMultipleInstances(QStringList args, IPC *ipc)
+static int simple_x264_main(int &argc, char **argv)
 {
-       bool commandSent = false;
+       int iResult = -1;
+
+       //Print logo
+       x264_print_logo();
 
-       //Process all command-line arguments
-       while(!args.isEmpty())
+       //Get CLI arguments
+       const MUtils::OS::ArgumentMap &arguments = MUtils::OS::arguments();
+
+       //Enumerate CLI arguments
+       if(!arguments.isEmpty())
        {
-               const QString current = args.takeFirst();
-               if((current.compare("--add", Qt::CaseInsensitive) == 0) || (current.compare("--add-file", Qt::CaseInsensitive) == 0))
-               {
-                       if(!args.isEmpty())
-                       {
-                               commandSent = true;
-                               if(!ipc->sendAsync(IPC::IPC_OPCODE_ADD_FILE, QStringList() << args.takeFirst()))
-                               {
-                                       break;
-                               }
-                       }
-                       else
-                       {
-                               qWarning("Argument for '--add-file' is missing!");
-                       }
-               }
-               else if(current.compare("--add-job", Qt::CaseInsensitive) == 0)
+               qDebug("Command-Line Arguments:");
+               foreach(const QString &key, arguments.uniqueKeys())
                {
-                       if(args.size() >= 3)
+                       foreach(const QString &val, arguments.values(key))
                        {
-                               commandSent = true;
-                               if(!ipc->sendAsync(IPC::IPC_OPCODE_ADD_JOB, QStringList() << args.takeFirst() << args.takeFirst() << args.takeFirst()))
+                               if(!val.isEmpty())
                                {
-                                       break;
+                                       qDebug("--%s = \"%s\"", MUTILS_UTF8(key), MUTILS_UTF8(val));
+                                       continue;
                                }
-                       }
-                       else
-                       {
-                               qWarning("Argument(s) for '--add-job' are missing!");
-                               args.clear();
+                               qDebug("--%s", MUTILS_UTF8(key));
                        }
                }
+               qDebug(" ");
        }
 
-       //If no argument has been sent yet, send a ping!
-       if(!commandSent)
+       //Detect CPU capabilities
+       const MUtils::CPUFetaures::cpu_info_t cpuFeatures = MUtils::CPUFetaures::detect();
+       qDebug("   CPU vendor id  :  %s (Intel=%s)", cpuFeatures.idstr, MUTILS_BOOL2STR(cpuFeatures.vendor & MUtils::CPUFetaures::VENDOR_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 architecture  :  %s", cpuFeatures.x64 ? "x64 (64-Bit)" : "x86 (32-Bit)");
+       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));
+       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));
+       qDebug(" Number of CPU's  :  %d\n", cpuFeatures.count);
+
+       //Initialize Qt
+       QScopedPointer<QApplication> application(MUtils::Startup::create_qt(argc, argv, QLatin1String("Simple x264 Launcher"), QLatin1String("LoRd_MuldeR"), QLatin1String("muldersoft.com"), false));
+       if(application.isNull())
        {
-               ipc->sendAsync(IPC::IPC_OPCODE_PING, QStringList());
+               return EXIT_FAILURE;
        }
-}
 
-///////////////////////////////////////////////////////////////////////////////
-// Applicaton entry point
-///////////////////////////////////////////////////////////////////////////////
+       //Initialize application
+       application->setWindowIcon(QIcon(":/icons/movie.ico"));
+       application->setApplicationVersion(QString().sprintf("%d.%02d.%04d", x264_version_major(), x264_version_minor(), x264_version_build())); 
 
-LONG WINAPI x264_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionInfo);
-void x264_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t);
+       //Initialize the IPC handler class
+       QScopedPointer<MUtils::IPCChannel> ipcChannel(new MUtils::IPCChannel("simple-x264-launcher", x264_version_build(), "instance"));
+       if((iResult = x264_initialize_ipc(ipcChannel.data())) < 1)
+       {
+               return (iResult == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+       }
 
-static int _main(int argc, char* argv[])
-{
-       if(X264_DEBUG)
+       //Running in portable mode?
+       if(x264_is_portable())
        {
-               int iResult = -1;
-               qInstallMsgHandler(x264_message_handler);
-               X264_MEMORY_CHECK(x264_main, iResult, argc, argv);
-               x264_finalization();
-               return iResult;
+               qDebug("Application is running in portable mode!\n");
        }
-       else
+
+       //Set style
+       if(!arguments.contains(CLI_PARAM_NO_GUI_STYLE))
        {
-               int iResult = -1;
-               try
-               {
-                       qInstallMsgHandler(x264_message_handler);
-                       iResult = x264_main(argc, argv);
-                       x264_finalization();
-               }
-               catch(char *error)
-               {
-                       fflush(stdout);
-                       fflush(stderr);
-                       fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error message: %s\n", error);
-                       x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
-               }
-               catch(int error)
-               {
-                       fflush(stdout);
-                       fflush(stderr);
-                       fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error code: 0x%X\n", error);
-                       x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
-               }
-               catch(...)
-               {
-                       fflush(stdout);
-                       fflush(stderr);
-                       fprintf(stderr, "\nGURU MEDITATION !!!\n");
-                       x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
-               }
-               return iResult;
+               qApp->setStyle(new QPlastiqueStyle());
        }
+
+       //Create Main Window
+       QScopedPointer<MainWindow> mainWindow(new MainWindow(cpuFeatures, ipcChannel.data()));
+       mainWindow->show();
+
+       //Run application
+       int ret = qApp->exec();
+       
+       //Exit program
+       return ret;
 }
 
+///////////////////////////////////////////////////////////////////////////////
+// Applicaton entry point
+///////////////////////////////////////////////////////////////////////////////
+
 int main(int argc, char* argv[])
 {
-       if(X264_DEBUG)
-       {
-               return _main(argc, argv);
-       }
-       else
-       {
-               __try
-               {
-                       SetUnhandledExceptionFilter(x264_exception_handler);
-                       _set_invalid_parameter_handler(x264_invalid_param_handler);
-                       return _main(argc, argv);
-               }
-               __except(1)
-               {
-                       fflush(stdout);
-                       fflush(stderr);
-                       fprintf(stderr, "\nGURU MEDITATION !!!\n\nUnhandeled structured exception error! [code: 0x%X]\n", GetExceptionCode());
-                       x264_fatal_exit(L"Unhandeled structured exception error, application will exit!");
-               }
-       }
+       return MUtils::Startup::startup(argc, argv, simple_x264_main, "Simple x264 Launcher", x264_is_prerelease());
 }