1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
25 #include <QApplication>
26 #include <QMessageBox>
32 #include <QPlastiqueStyle>
33 #include <QImageReader>
34 #include <QSharedMemory>
36 #include <QStringList>
40 #include "LockedFile.h"
52 ///////////////////////////////////////////////////////////////////////////////
54 ///////////////////////////////////////////////////////////////////////////////
57 static const unsigned int g_lamexp_version_major
= VER_LAMEXP_MAJOR
;
58 static const unsigned int g_lamexp_version_minor
= VER_LAMEXP_MINOR
;
59 static const unsigned int g_lamexp_version_build
= VER_LAMEXP_BUILD
;
60 static const char *g_lamexp_version_release
= VER_LAMEXP_SUFFIX_STR
;
63 static QDate g_lamexp_version_date
;
64 static const char *g_lamexp_months
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
65 static const char *g_lamexp_version_raw_date
= __DATE__
;
68 static QString g_lamexp_temp_folder
;
71 static QMap
<QString
, LockedFile
*> g_lamexp_tool_registry
;
74 static const char *g_lamexp_sharedmem_uuid
= "{21A68A42-6923-43bb-9CF6-64BF151942EE}";
75 static QSharedMemory
*g_lamexp_sharedmem_ptr
= NULL
;
77 ///////////////////////////////////////////////////////////////////////////////
79 ///////////////////////////////////////////////////////////////////////////////
84 unsigned int lamexp_version_major(void) { return g_lamexp_version_major
; }
85 unsigned int lamexp_version_minor(void) { return g_lamexp_version_minor
; }
86 unsigned int lamexp_version_build(void) { return g_lamexp_version_build
; }
87 const char *lamexp_version_release(void) { return g_lamexp_version_release
; }
89 bool lamexp_version_demo(void)
91 return !(strstr(g_lamexp_version_release
, "Final") || strstr(g_lamexp_version_release
, "Hotfix"));
97 const QDate
&lamexp_version_date(void)
99 if(!g_lamexp_version_date
.isValid())
104 char *this_token
= NULL
;
105 char *next_token
= NULL
;
107 strcpy_s(temp
, 32, g_lamexp_version_raw_date
);
108 this_token
= strtok_s(temp
, " ", &next_token
);
110 for(int i
= 0; i
< 3; i
++)
115 for(int j
= 0; j
< 12; j
++)
117 if(!_strcmpi(this_token
, g_lamexp_months
[j
]))
125 date
[i
] = atoi(this_token
);
127 this_token
= strtok_s(NULL
, " ", &next_token
);
131 if(date
[0] >= 0 && date
[1] >= 0 && date
[2] >= 0)
133 g_lamexp_version_date
= QDate(date
[2], date
[0], date
[1]);
137 return g_lamexp_version_date
;
141 * Initialize the console
143 void lamexp_init_console(int argc
, char* argv
[])
145 for(int i
= 0; i
< argc
; i
++)
147 if(lamexp_version_demo() || !_stricmp(argv
[i
], "--console"))
151 //See: http://support.microsoft.com/default.aspx?scid=kb;en-us;105305
152 int hCrtStdOut
= _open_osfhandle((long) GetStdHandle(STD_OUTPUT_HANDLE
), _O_TEXT
);
153 int hCrtStdErr
= _open_osfhandle((long) GetStdHandle(STD_ERROR_HANDLE
), _O_TEXT
);
154 FILE *hfStdOut
= _fdopen(hCrtStdOut
, "w");
155 FILE *hfStderr
= _fdopen(hCrtStdErr
, "w");
158 setvbuf(stdout
, NULL
, _IONBF
, 0);
159 setvbuf(stderr
, NULL
, _IONBF
, 0);
162 HMENU hMenu
= GetSystemMenu(GetConsoleWindow(), 0);
163 EnableMenuItem(hMenu
, SC_CLOSE
, MF_BYCOMMAND
| MF_GRAYED
);
164 RemoveMenu(hMenu
, SC_CLOSE
, MF_BYCOMMAND
);
166 SetConsoleCtrlHandler(NULL
, TRUE
);
167 SetConsoleTitle(L
"LameXP - Audio Encoder Front-End | Debug Console");
168 SetConsoleOutputCP(CP_UTF8
);
176 * Initialize Qt framework
178 bool lamexp_init_qt(int argc
, char* argv
[])
180 static bool qt_initialized
= false;
182 //Don't initialized again, if done already
189 qDebug("Using Qt Framework v%s, compiled with Qt v%s", qVersion(), QT_VERSION_STR
);
190 QT_REQUIRE_VERSION(argc
, argv
, QT_VERSION_STR
);
192 //Check the Windows version
193 switch(QSysInfo::WindowsVersion
)
195 case QSysInfo::WV_2000
:
196 qDebug("Running on Windows 2000 (not offically supported!).\n");
198 case QSysInfo::WV_XP
:
199 qDebug("Running on Windows XP.\n\n");
201 case QSysInfo::WV_2003
:
202 qDebug("Running on Windows Server 2003 or Windows XP Professional x64 Edition.\n");
204 case QSysInfo::WV_VISTA
:
205 qDebug("Running on Windows Vista or Windows Server 200.8\n");
207 case QSysInfo::WV_WINDOWS7
:
208 qDebug("Running on Windows 7 or Windows Server 2008 R2.\n");
211 qFatal("Unsupported OS, only Windows 2000 or later is supported!");
215 //Create Qt application instance and setup version info
216 QApplication
*application
= new QApplication(argc
, argv
);
217 application
->setApplicationName("LameXP - Audio Encoder Front-End");
218 application
->setApplicationVersion(QString().sprintf("%d.%02d.%04d", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build()));
219 application
->setOrganizationName("LoRd_MuldeR");
220 application
->setOrganizationDomain("mulder.dummwiedeutsch.de");
221 application
->setWindowIcon(QIcon(":/MainIcon.png"));
223 //Load plugins from application directory
224 QCoreApplication::setLibraryPaths(QStringList() << QApplication::applicationDirPath());
225 qDebug("Library Path:\n%s\n", QApplication::libraryPaths().first().toUtf8().constData());
227 //Check for supported image formats
228 QList
<QByteArray
> supportedFormats
= QImageReader::supportedImageFormats();
229 if(!(supportedFormats
.contains("png") && supportedFormats
.contains("gif") && supportedFormats
.contains("ico") && supportedFormats
.contains("svg")))
231 qFatal("Qt initialization error: At least one image format plugin is missing!");
235 //Change application look
236 QApplication::setStyle(new QPlastiqueStyle());
239 qt_initialized
= true;
244 * Check for running instances of LameXP
246 bool lamexp_check_instances(void)
248 if(g_lamexp_sharedmem_ptr
)
253 QSharedMemory
*sharedMemory
= new QSharedMemory(g_lamexp_sharedmem_uuid
, NULL
);
255 if(!sharedMemory
->create(1048576))
257 if(sharedMemory
->error() == QSharedMemory::AlreadyExists
)
259 LAMEXP_DELETE(sharedMemory
);
260 qWarning("Another instance of LameXP is already running on this computer!");
261 QMessageBox::warning(NULL
, "LameXP", "LameXP is already running. Please use the running instance!");
266 QString errorMessage
= sharedMemory
->errorString();
267 LAMEXP_DELETE(sharedMemory
);
268 qFatal("Failed to create shared memory: %s", errorMessage
.toUtf8().constData());
273 g_lamexp_sharedmem_ptr
= sharedMemory
;
278 * Get LameXP temp folder
280 const QString
&lamexp_temp_folder(void)
282 if(g_lamexp_temp_folder
.isEmpty())
284 QDir
tempFolder(QDir::tempPath());
285 QString uuid
= QUuid::createUuid().toString();
286 tempFolder
.mkdir(uuid
);
288 if(tempFolder
.cd(uuid
))
290 g_lamexp_temp_folder
= tempFolder
.absolutePath();
294 g_lamexp_temp_folder
= QDir::tempPath();
298 return g_lamexp_temp_folder
;
304 bool lamexp_clean_folder(const QString folderPath
)
306 QDir
tempFolder(folderPath
);
307 QFileInfoList entryList
= tempFolder
.entryInfoList();
309 for(int i
= 0; i
< entryList
.count(); i
++)
311 if(entryList
.at(i
).fileName().compare(".") == 0 || entryList
.at(i
).fileName().compare("..") == 0)
316 if(entryList
.at(i
).isDir())
318 lamexp_clean_folder(entryList
.at(i
).absoluteFilePath());
322 QFile::remove(entryList
.at(i
).absoluteFilePath());
326 tempFolder
.rmdir(".");
327 return !tempFolder
.exists();
331 * Finalization function (final clean-up)
333 void lamexp_finalization(void)
336 if(!g_lamexp_tool_registry
.isEmpty())
338 QStringList keys
= g_lamexp_tool_registry
.keys();
339 for(int i
= 0; i
< keys
.count(); i
++)
341 LAMEXP_DELETE(g_lamexp_tool_registry
[keys
.at(i
)]);
343 g_lamexp_tool_registry
.clear();
346 //Delete temporary files
347 if(!g_lamexp_temp_folder
.isEmpty())
349 for(int i
= 0; i
< 100; i
++)
351 if(lamexp_clean_folder(g_lamexp_temp_folder
)) break;
354 g_lamexp_temp_folder
.clear();
357 //Destroy Qt application object
358 QApplication
*application
= dynamic_cast<QApplication
*>(QApplication::instance());
359 LAMEXP_DELETE(application
);
361 //Detach from shared memory
362 if(g_lamexp_sharedmem_ptr
) g_lamexp_sharedmem_ptr
->detach();
363 LAMEXP_DELETE(g_lamexp_sharedmem_ptr
);
369 void lamexp_register_tool(const QString
&toolName
, LockedFile
*file
)
371 if(g_lamexp_tool_registry
.contains(toolName
))
373 throw "lamexp_register_tool: Tool is already registered!";
376 g_lamexp_tool_registry
.insert(toolName
, file
);
382 const QString
lamexp_lookup_tool(const QString
&toolName
)
384 if(g_lamexp_tool_registry
.contains(toolName
))
386 return g_lamexp_tool_registry
.value(toolName
)->filePath();
395 * Get number private bytes [debug only]
397 SIZE_T
lamexp_dbg_private_bytes(void)
400 PROCESS_MEMORY_COUNTERS_EX memoryCounters
;
401 memoryCounters
.cb
= sizeof(PROCESS_MEMORY_COUNTERS_EX
);
402 GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS
) &memoryCounters
, sizeof(PROCESS_MEMORY_COUNTERS_EX
));
403 return memoryCounters
.PrivateUsage
;
405 throw "Cannot call this function in a non-debug build!";