OSDN Git Service

Move IPC sender to separate thread + implement kill IPC command + improve deployment...
[lamexp/LameXP.git] / src / Main.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 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.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 //LameXP includes
23 #include "Global.h"
24 #include "Dialog_SplashScreen.h"
25 #include "Dialog_MainWindow.h"
26 #include "Thread_Initialization.h"
27 #include "Thread_MessageProducer.h"
28
29 //Qt includes
30 #include <QApplication>
31 #include <QMessageBox>
32 #include <QDate>
33
34 ///////////////////////////////////////////////////////////////////////////////
35 // Main function
36 ///////////////////////////////////////////////////////////////////////////////
37
38 int lamexp_main(int argc, char* argv[])
39 {
40         int iResult = -1;
41         
42         //Init console
43         lamexp_init_console(argc, argv);
44         
45         //Print version info
46         qDebug("LameXP - Audio Encoder Front-End");
47         qDebug("Version %d.%02d %s, Build %d [%s], MSVC compiler v%02d.%02d", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build(), lamexp_version_date().toString(Qt::ISODate).toLatin1().constData(), _MSC_VER / 100, _MSC_VER % 100);
48         qDebug("Copyright (C) 2004-%04d LoRd_MuldeR <MuldeR2@GMX.de>\n", max(lamexp_version_date().year(),QDate::currentDate().year()));
49         
50         //print license info
51         qDebug("This program is free software: you can redistribute it and/or modify");
52         qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
53         qDebug("This program comes with ABSOLUTELY NO WARRANTY.\n");
54         
55         //Print warning, if this is a "debug" build
56         LAMEXP_CHECK_DEBUG_BUILD;
57         
58         //Initialize Qt
59         lamexp_init_qt(argc, argv);
60         
61         //Check for expiration
62         if(lamexp_version_demo())
63         {
64                 QDate expireDate = lamexp_version_date().addDays(14);
65                 qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(expireDate.toString(Qt::ISODate)).toLatin1().constData());
66                 if(QDate::currentDate() >= expireDate)
67                 {
68                         qWarning("Expired !!!");
69                         QMessageBox::warning(NULL, "LameXP - Expired", QString("This demo (pre-release) version of LameXP has expired at %1.\nLameXP is free software and release versions won't expire.").arg(expireDate.toString()), "Exit Program");
70                         return 0;
71                 }
72         }
73
74         //Check for multiple instances of LameXP
75         if((iResult = lamexp_init_ipc()) != 0)
76         {
77                 qDebug("LameXP is already running, connecting to running instance...");
78                 if(iResult == 1)
79                 {
80                         MessageProducerThread *messageProducerThread = new MessageProducerThread();
81                         messageProducerThread->start();
82                         if(!messageProducerThread->wait(30000))
83                         {
84                                 messageProducerThread->terminate();
85                                 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);
86                                 messageBox.exec();
87                                 messageProducerThread->wait();
88                                 LAMEXP_DELETE(messageProducerThread);
89                                 return -1;
90                         }
91                         LAMEXP_DELETE(messageProducerThread);
92                 }
93                 return 0;
94         }
95
96         //Kill application?
97         for(int i = 0; i < argc; i++)
98         {
99                 if(!_stricmp("--kill", argv[i]) || !_stricmp("--force-kill", argv[i]))
100                 {
101                         return 0;
102                 }
103         }
104         
105         //Show splash screen
106         InitializationThread *poInitializationThread = new InitializationThread();
107         SplashScreen::showSplash(poInitializationThread);
108         LAMEXP_DELETE(poInitializationThread);
109
110         //Show main window
111         MainWindow *poMainWindow = new MainWindow();
112         poMainWindow->show();
113         iResult = QApplication::instance()->exec();
114         LAMEXP_DELETE(poMainWindow);
115         
116         //Final clean-up
117         qDebug("Shutting down, please wait...\n");
118         
119         //Terminate
120         return iResult;
121 }
122
123 ///////////////////////////////////////////////////////////////////////////////
124 // Message Handler
125 ///////////////////////////////////////////////////////////////////////////////
126
127 static void lamexp_message_handler(QtMsgType type, const char *msg)
128 {
129         static HANDLE hConsole = NULL;
130         
131         if(!hConsole)
132         {
133                 hConsole = CreateFile(L"CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
134         }
135
136         CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
137         GetConsoleScreenBufferInfo(hConsole, &bufferInfo);
138
139         switch(type)
140         {
141         case QtCriticalMsg:
142         case QtFatalMsg:
143                 fflush(stdout);
144                 fflush(stderr);
145                 SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
146                 fprintf(stderr, "\nCRITICAL ERROR !!!\n%s\n\n", msg);
147                 MessageBoxA(NULL, msg, "LameXP - CRITICAL ERROR", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL);
148                 break;
149         case QtWarningMsg:
150                 SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
151                 fprintf(stderr, "%s\n", msg);
152                 fflush(stderr);
153                 break;
154         default:
155                 SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
156                 fprintf(stderr, "%s\n", msg);
157                 fflush(stderr);
158                 break;
159         }
160
161         SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
162
163         if(type == QtCriticalMsg || type == QtFatalMsg)
164         {
165                 FatalAppExit(0, L"The application has encountered a critical error and will exit now!");
166                 TerminateProcess(GetCurrentProcess(), -1);
167         }
168  }
169
170
171 ///////////////////////////////////////////////////////////////////////////////
172 // Applicaton entry point
173 ///////////////////////////////////////////////////////////////////////////////
174
175 int main(int argc, char* argv[])
176 {
177         try
178         {
179                 int iResult;
180                 qInstallMsgHandler(lamexp_message_handler);
181                 LAMEXP_MEMORY_CHECK(iResult = lamexp_main(argc, argv));
182                 lamexp_finalization();
183                 return iResult;
184         }
185         catch(char *error)
186         {
187                 fflush(stdout);
188                 fflush(stderr);
189                 fprintf(stderr, "\nEXCEPTION ERROR: %s\n", error);
190                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
191                 TerminateProcess(GetCurrentProcess(), -1);
192         }
193         catch(int error)
194         {
195                 fflush(stdout);
196                 fflush(stderr);
197                 fprintf(stderr, "\nEXCEPTION ERROR: Error code 0x%X\n", error);
198                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
199                 TerminateProcess(GetCurrentProcess(), -1);
200         }
201         catch(...)
202         {
203                 fflush(stdout);
204                 fflush(stderr);
205                 fprintf(stderr, "\nEXCEPTION ERROR !!!\n");
206                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
207                 TerminateProcess(GetCurrentProcess(), -1);
208         }
209 }