//MUtils
#include <MUtils/Global.h>
#include <MUtils/OSSupport.h>
+#include <MUtils/Terminal.h>
//CRT includes
#include <cstdio>
#define _LAMEXP_MAKE_STR(STR) #STR
#define LAMEXP_MAKE_STR(STR) _LAMEXP_MAKE_STR(STR)
-//String helper
-#define CLEAN_OUTPUT_STRING(STR) do \
-{ \
- const char CTRL_CHARS[3] = { '\r', '\n', '\t' }; \
- for(size_t i = 0; i < 3; i++) \
- { \
- while(char *pos = strchr((STR), CTRL_CHARS[i])) *pos = char(0x20); \
- } \
-} \
-while(0)
-
-//String helper
-#define TRIM_LEFT(STR) do \
-{ \
- const char WHITE_SPACE[4] = { char(0x20), '\r', '\n', '\t' }; \
- for(size_t i = 0; i < 4; i++) \
- { \
- while(*(STR) == WHITE_SPACE[i]) (STR)++; \
- } \
-} \
-while(0)
-
///////////////////////////////////////////////////////////////////////////////
// GLOBAL VARS
///////////////////////////////////////////////////////////////////////////////
-//Console attached flag
-static bool g_lamexp_console_attached = false;
-
-//Fatal exit flags
-static volatile bool g_lamexp_fatal_flag = true;
-
-//CLI Arguments
-static struct
-{
- QStringList *list;
- QReadWriteLock lock;
-}
-g_lamexp_argv;
-
//Wine detection
static struct
{
//Main thread ID
static const DWORD g_main_thread_id = GetCurrentThreadId();
-//Log file
-static FILE *g_lamexp_log_file = NULL;
-
//Localization
const char* LAMEXP_DEFAULT_LANGID = "en";
const char* LAMEXP_DEFAULT_TRANSLATION = "LameXP_EN.qm";
-//GURU MEDITATION
-static const char *GURU_MEDITATION = "\n\nGURU MEDITATION !!!\n\n";
-
///////////////////////////////////////////////////////////////////////////////
// GLOBAL FUNCTIONS
///////////////////////////////////////////////////////////////////////////////
}
/*
- * Change console text color
- */
-static void lamexp_console_color(FILE* file, WORD attributes)
-{
- const HANDLE hConsole = (HANDLE)(_get_osfhandle(_fileno(file)));
- if((hConsole != NULL) && (hConsole != INVALID_HANDLE_VALUE))
- {
- SetConsoleTextAttribute(hConsole, attributes);
- }
-}
-
-/*
- * Output logging message to console
- */
-static void lamexp_write_console(const int type, const char *msg)
-{
- __try
- {
- if(_isatty(_fileno(stderr)))
- {
- UINT oldOutputCP = GetConsoleOutputCP();
- if(oldOutputCP != CP_UTF8) SetConsoleOutputCP(CP_UTF8);
-
- switch(type)
- {
- case QtCriticalMsg:
- case QtFatalMsg:
- lamexp_console_color(stderr, FOREGROUND_RED | FOREGROUND_INTENSITY);
- fprintf(stderr, GURU_MEDITATION);
- fprintf(stderr, "%s\n", msg);
- fflush(stderr);
- break;
- case QtWarningMsg:
- lamexp_console_color(stderr, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
- fprintf(stderr, "%s\n", msg);
- fflush(stderr);
- break;
- default:
- lamexp_console_color(stderr, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
- fprintf(stderr, "%s\n", msg);
- fflush(stderr);
- break;
- }
-
- lamexp_console_color(stderr, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
- if(oldOutputCP != CP_UTF8) SetConsoleOutputCP(oldOutputCP);
- }
- }
- __except(1)
- {
- /*ignore any exception that might occur here!*/
- }
-}
-
-/*
- * Output logging message to debugger
- */
-static void lamexp_write_dbg_out(const int type, const char *msg)
-{
- const char *FORMAT = "[LameXP][%c] %s\n";
-
- __try
- {
- char buffer[512];
- const char* input = msg;
- TRIM_LEFT(input);
-
- switch(type)
- {
- case QtCriticalMsg:
- case QtFatalMsg:
- _snprintf_s(buffer, 512, _TRUNCATE, FORMAT, 'C', input);
- break;
- case QtWarningMsg:
- _snprintf_s(buffer, 512, _TRUNCATE, FORMAT, 'W', input);
- break;
- default:
- _snprintf_s(buffer, 512, _TRUNCATE, FORMAT, 'I', input);
- break;
- }
-
- char *temp = &buffer[0];
- CLEAN_OUTPUT_STRING(temp);
- OutputDebugStringA(temp);
- }
- __except(1)
- {
- /*ignore any exception that might occur here!*/
- }
-}
-
-/*
- * Output logging message to logfile
- */
-static void lamexp_write_logfile(const int type, const char *msg)
-{
- const char *FORMAT = "[%c][%04u] %s\r\n";
-
- __try
- {
- if(g_lamexp_log_file)
- {
- char buffer[512];
- strncpy_s(buffer, 512, msg, _TRUNCATE);
-
- char *temp = &buffer[0];
- TRIM_LEFT(temp);
- CLEAN_OUTPUT_STRING(temp);
-
- const unsigned int timestamp = static_cast<unsigned int>(_time64(NULL) % 3600I64);
-
- switch(type)
- {
- case QtCriticalMsg:
- case QtFatalMsg:
- fprintf(g_lamexp_log_file, FORMAT, 'C', timestamp, temp);
- break;
- case QtWarningMsg:
- fprintf(g_lamexp_log_file, FORMAT, 'W', timestamp, temp);
- break;
- default:
- fprintf(g_lamexp_log_file, FORMAT, 'I', timestamp, temp);
- break;
- }
-
- fflush(g_lamexp_log_file);
- }
- }
- __except(1)
- {
- /*ignore any exception that might occur here!*/
- }
-}
-
-/*
* Qt message handler
*/
void lamexp_message_handler(QtMsgType type, const char *msg)
return;
}
- //CSLocker lock(g_lamexp_message_lock); FIXME !!!! FIXME !!!! FIXME !!!! FIXME !!!!
-
- if(g_lamexp_log_file)
- {
- lamexp_write_logfile(type, msg);
- }
-
- if(g_lamexp_console_attached)
- {
- lamexp_write_console(type, msg);
- }
- else
- {
- lamexp_write_dbg_out(type, msg);
- }
+ MUtils::Terminal::write(type, msg);
if((type == QtCriticalMsg) || (type == QtFatalMsg))
{
- //lock.forceUnlock();
MUtils::OS::fatal_exit(MUTILS_WCHR(QString::fromUtf8(msg)));
}
}
}
/*
- * Initialize the console
- */
-void lamexp_init_console(const QStringList &argv)
-{
- bool enableConsole = (MUTILS_DEBUG) || ((VER_LAMEXP_CONSOLE_ENABLED) && lamexp_version_demo());
-
- if(_environ)
- {
- wchar_t *logfile = NULL;
- size_t logfile_len = 0;
- if(!_wdupenv_s(&logfile, &logfile_len, L"LAMEXP_LOGFILE"))
- {
- if(logfile && (logfile_len > 0))
- {
- FILE *temp = NULL;
- if(!_wfopen_s(&temp, logfile, L"wb"))
- {
- fprintf(temp, "%c%c%c", char(0xEF), char(0xBB), char(0xBF));
- g_lamexp_log_file = temp;
- }
- free(logfile);
- }
- }
- }
-
- if(!MUTILS_DEBUG)
- {
- for(int i = 0; i < argv.count(); i++)
- {
- if(!argv.at(i).compare("--console", Qt::CaseInsensitive))
- {
- enableConsole = true;
- }
- else if(!argv.at(i).compare("--no-console", Qt::CaseInsensitive))
- {
- enableConsole = false;
- }
- }
- }
-
- if(enableConsole)
- {
- if(!g_lamexp_console_attached)
- {
- if(AllocConsole() != FALSE)
- {
- SetConsoleCtrlHandler(NULL, TRUE);
- SetConsoleTitle(L"LameXP - Audio Encoder Front-End | Debug Console");
- SetConsoleOutputCP(CP_UTF8);
- g_lamexp_console_attached = true;
- }
- }
-
- if(g_lamexp_console_attached)
- {
- //-------------------------------------------------------------------
- //See: http://support.microsoft.com/default.aspx?scid=kb;en-us;105305
- //-------------------------------------------------------------------
- const int flags = _O_WRONLY | _O_U8TEXT;
- int hCrtStdOut = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), flags);
- int hCrtStdErr = _open_osfhandle((intptr_t) GetStdHandle(STD_ERROR_HANDLE), flags);
- FILE *hfStdOut = (hCrtStdOut >= 0) ? _fdopen(hCrtStdOut, "wb") : NULL;
- FILE *hfStdErr = (hCrtStdErr >= 0) ? _fdopen(hCrtStdErr, "wb") : NULL;
- if(hfStdOut) { *stdout = *hfStdOut; std::cout.rdbuf(new std::filebuf(hfStdOut)); }
- if(hfStdErr) { *stderr = *hfStdErr; std::cerr.rdbuf(new std::filebuf(hfStdErr)); }
- }
-
- HWND hwndConsole = GetConsoleWindow();
-
- if((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
- {
- HMENU hMenu = GetSystemMenu(hwndConsole, 0);
- EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
- RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
-
- SetWindowPos(hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
- SetWindowLong(hwndConsole, GWL_STYLE, GetWindowLong(hwndConsole, GWL_STYLE) & (~WS_MAXIMIZEBOX) & (~WS_MINIMIZEBOX));
- SetWindowPos(hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
- }
- }
-}
-
-/*
* Check for debugger (detect routine)
*/
static __forceinline bool lamexp_check_for_debugger(void)
{
static bool qt_initialized = false;
typedef BOOL (WINAPI *SetDllDirectoryProc)(WCHAR *lpPathName);
- const QStringList &arguments = lamexp_arguments();
+ const QStringList &arguments = MUtils::OS::arguments();
//Don't initialized again, if done already
if(qt_initialized)
}
//Update console icon, if a console is attached
+#if 0 //FIXME !!!! FIXME !!!! FIXME !!!! FIXME !!!! FIXME !!!! FIXME !!!! FIXME !!!! FIXME !!!!
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
if(g_lamexp_console_attached && (!lamexp_detect_wine()))
{
}
}
#endif
+#endif
//Done
qt_initialized = true;
return true;
}
-const QStringList &lamexp_arguments(void)
-{
- QReadLocker readLock(&g_lamexp_argv.lock);
-
- if(!g_lamexp_argv.list)
- {
- readLock.unlock();
- QWriteLocker writeLock(&g_lamexp_argv.lock);
-
- g_lamexp_argv.list = new QStringList;
-
- int nArgs = 0;
- LPWSTR *szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
-
- if(NULL != szArglist)
- {
- for(int i = 0; i < nArgs; i++)
- {
- (*g_lamexp_argv.list) << MUTILS_QSTR(szArglist[i]);
- }
- LocalFree(szArglist);
- }
- else
- {
- qWarning("CommandLineToArgvW() has failed !!!");
- }
- }
-
- return (*g_lamexp_argv.list);
-}
-
/*
* Check if visual themes are enabled (WinXP and later)
*/
}
//Zero *before* constructors are called
- LAMEXP_ZERO_MEMORY(g_lamexp_argv);
LAMEXP_ZERO_MEMORY(g_lamexp_wine);
LAMEXP_ZERO_MEMORY(g_lamexp_themes_enabled);
LAMEXP_ZERO_MEMORY(g_lamexp_dwmapi);
g_lamexp_dwmapi.dwmEnableBlurBehindWindow = NULL;
MUTILS_DELETE(g_lamexp_dwmapi.dwmapi_dll);
- //Free STDOUT and STDERR buffers
- if(g_lamexp_console_attached)
- {
- if(std::filebuf *tmp = dynamic_cast<std::filebuf*>(std::cout.rdbuf()))
- {
- std::cout.rdbuf(NULL);
- MUTILS_DELETE(tmp);
- }
- if(std::filebuf *tmp = dynamic_cast<std::filebuf*>(std::cerr.rdbuf()))
- {
- std::cerr.rdbuf(NULL);
- MUTILS_DELETE(tmp);
- }
- }
-
- //Close log file
- if(g_lamexp_log_file)
- {
- fclose(g_lamexp_log_file);
- g_lamexp_log_file = NULL;
- }
-
//Clear sound cache
MUTILS_DELETE(g_lamexp_sounds.sound_db);
-
- //Free CLI Arguments
- MUTILS_DELETE(g_lamexp_argv.list);
}
#include <MUtils/OSSupport.h>
#include <MUtils/Version.h>
#include <MUtils/CPUFeatures.h>
+#include <MUtils/Terminal.h>
//Qt includes
#include <QApplication>
bool bAccepted = true;
//Get CLI arguments
- const QStringList &arguments = lamexp_arguments();
+ const QStringList &arguments = MUtils::OS::arguments();
//Init console
- lamexp_init_console(arguments);
+ MUtils::Terminal::setup(arguments, MUTILS_DEBUG || lamexp_version_demo());
//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());
+ 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(MUTILS_DEBUG)
{
{
qDebug("argv[%d]=%s", i, MUTILS_UTF8(arguments.at(i)));
}
- qDebug("");
+ qDebug(" ");
//Detect CPU capabilities
- const MUtils::CPUFetaures::cpu_info_t cpuFeatures = MUtils::CPUFetaures::detect(lamexp_arguments());
+ const MUtils::CPUFetaures::cpu_info_t cpuFeatures = MUtils::CPUFetaures::detect(MUtils::OS::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);
if(lamexp_version_demo())
{
const QDate currentDate = MUtils::OS::current_date();
- if(currentDate.addDays(1) < MUtils::Version::build_date())
+ if(currentDate.addDays(1) < MUtils::Version::app_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());
+ 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());
}
qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(lamexp_version_expires().toString(Qt::ISODate)).toLatin1().constData());
}