2012-01-28 16:40:14 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Simple x264 Launcher
|
2017-01-07 18:48:20 +01:00
|
|
|
// Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
|
2012-01-28 16:40:14 +01:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-02-02 15:54:39 +01:00
|
|
|
//x264 includes
|
2012-01-28 16:40:14 +01:00
|
|
|
#include "global.h"
|
2014-02-02 15:54:39 +01:00
|
|
|
#include "targetver.h"
|
2012-01-28 16:40:14 +01:00
|
|
|
|
2013-11-03 17:08:02 +01:00
|
|
|
//Windows includes
|
|
|
|
#define NOMINMAX
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <MMSystem.h>
|
|
|
|
#include <ShellAPI.h>
|
2013-11-29 18:09:38 +01:00
|
|
|
#include <Objbase.h>
|
2013-12-09 22:19:32 +01:00
|
|
|
#include <Psapi.h>
|
|
|
|
#include <SensAPI.h>
|
2013-11-03 17:08:02 +01:00
|
|
|
|
2013-11-03 18:34:20 +01:00
|
|
|
//C++ includes
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
//VLD
|
|
|
|
#include <vld.h>
|
|
|
|
|
2014-02-01 20:21:24 +01:00
|
|
|
//Version
|
|
|
|
#define ENABLE_X264_VERSION_INCLUDE
|
|
|
|
#include "version.h"
|
|
|
|
#undef ENABLE_X264_VERSION_INCLUDE
|
|
|
|
|
2015-02-02 22:11:06 +01:00
|
|
|
//MUtils includes
|
|
|
|
#include <MUtils/Global.h>
|
|
|
|
|
2012-01-28 16:40:14 +01:00
|
|
|
//Qt includes
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QUuid>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QDate>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QPlastiqueStyle>
|
|
|
|
#include <QImageReader>
|
|
|
|
#include <QSharedMemory>
|
|
|
|
#include <QSysInfo>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QSystemSemaphore>
|
2012-02-10 18:40:28 +01:00
|
|
|
#include <QDesktopServices>
|
2012-01-28 16:40:14 +01:00
|
|
|
#include <QMutex>
|
|
|
|
#include <QTextCodec>
|
|
|
|
#include <QLibrary>
|
|
|
|
#include <QRegExp>
|
|
|
|
#include <QResource>
|
|
|
|
#include <QTranslator>
|
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QLibraryInfo>
|
|
|
|
#include <QEvent>
|
2013-11-03 17:08:02 +01:00
|
|
|
#include <QReadLocker>
|
|
|
|
#include <QWriteLocker>
|
2013-11-03 18:34:20 +01:00
|
|
|
#include <QProcess>
|
2012-01-28 16:40:14 +01:00
|
|
|
|
|
|
|
//CRT includes
|
|
|
|
#include <fstream>
|
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <intrin.h>
|
2013-11-03 17:08:02 +01:00
|
|
|
#include <process.h>
|
2012-01-28 16:40:14 +01:00
|
|
|
|
|
|
|
//Debug only includes
|
|
|
|
#if X264_DEBUG
|
|
|
|
#include <Psapi.h>
|
|
|
|
#endif
|
|
|
|
|
2013-12-09 22:19:32 +01:00
|
|
|
//Global types
|
|
|
|
typedef HRESULT (WINAPI *SHGetKnownFolderPath_t)(const GUID &rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
|
|
|
|
typedef HRESULT (WINAPI *SHGetFolderPath_t)(HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath);
|
|
|
|
|
2012-01-28 16:40:14 +01:00
|
|
|
//Const
|
|
|
|
static const char *g_x264_months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
|
|
|
static const char *g_x264_imageformats[] = {"png", "jpg", "gif", "ico", "svg", NULL};
|
|
|
|
|
|
|
|
//Build version
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
unsigned int ver_major;
|
|
|
|
unsigned int ver_minor;
|
2012-02-08 00:29:47 +01:00
|
|
|
unsigned int ver_patch;
|
2012-02-11 00:19:24 +01:00
|
|
|
unsigned int ver_build;
|
2012-01-28 16:40:14 +01:00
|
|
|
}
|
|
|
|
g_x264_version =
|
|
|
|
{
|
2012-02-08 00:29:47 +01:00
|
|
|
(VER_X264_MAJOR),
|
|
|
|
(VER_X264_MINOR),
|
|
|
|
(VER_X264_PATCH),
|
2012-02-11 00:19:24 +01:00
|
|
|
(VER_X264_BUILD),
|
2012-01-28 16:40:14 +01:00
|
|
|
};
|
|
|
|
|
2015-02-08 21:14:21 +01:00
|
|
|
//Portable mode
|
|
|
|
static QReadWriteLock g_portableModeLock;
|
|
|
|
static bool g_portableModeData = false;
|
|
|
|
static bool g_portableModeInit = false;
|
2013-11-03 17:08:02 +01:00
|
|
|
|
2015-02-08 21:14:21 +01:00
|
|
|
//Data path
|
|
|
|
static QString g_dataPathData;
|
|
|
|
static QReadWriteLock g_dataPathLock;
|
2013-11-03 17:08:02 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// MACROS
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//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)
|
|
|
|
|
2014-02-02 15:54:39 +01:00
|
|
|
//Check for CLI flag
|
|
|
|
static inline bool _CHECK_FLAG(const int argc, char **argv, const char *flag)
|
|
|
|
{
|
|
|
|
for(int i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if(_stricmp(argv[i], flag) == 0) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_FLAG(FLAG) _CHECK_FLAG(argc, argv, "--" FLAG)
|
2013-11-03 17:08:02 +01:00
|
|
|
#define X264_ZERO_MEMORY(X) SecureZeroMemory(&X, sizeof(X))
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// GLOBAL FUNCTIONS
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-01-28 16:40:14 +01:00
|
|
|
/*
|
|
|
|
* Version info
|
|
|
|
*/
|
|
|
|
unsigned int x264_version_major(void)
|
|
|
|
{
|
|
|
|
return g_x264_version.ver_major;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int x264_version_minor(void)
|
|
|
|
{
|
2012-02-11 00:19:24 +01:00
|
|
|
return (g_x264_version.ver_minor * 10) + (g_x264_version.ver_patch % 10);
|
2012-01-28 16:40:14 +01:00
|
|
|
}
|
|
|
|
|
2012-02-11 00:19:24 +01:00
|
|
|
unsigned int x264_version_build(void)
|
2012-02-08 00:29:47 +01:00
|
|
|
{
|
2012-02-11 00:19:24 +01:00
|
|
|
return g_x264_version.ver_build;
|
2012-02-08 00:29:47 +01:00
|
|
|
}
|
|
|
|
|
2012-02-10 01:58:45 +01:00
|
|
|
/*
|
|
|
|
* Check for portable mode
|
|
|
|
*/
|
2015-02-08 21:14:21 +01:00
|
|
|
bool x264_is_portable(void)
|
2012-02-10 01:58:45 +01:00
|
|
|
{
|
2015-02-08 21:14:21 +01:00
|
|
|
QReadLocker readLock(&g_portableModeLock);
|
2012-02-10 01:58:45 +01:00
|
|
|
|
2015-02-08 21:14:21 +01:00
|
|
|
if(g_portableModeInit)
|
2012-02-10 01:58:45 +01:00
|
|
|
{
|
2015-02-08 21:14:21 +01:00
|
|
|
return g_portableModeData;
|
2012-02-10 01:58:45 +01:00
|
|
|
}
|
2013-11-03 17:08:02 +01:00
|
|
|
|
|
|
|
readLock.unlock();
|
2015-02-08 21:14:21 +01:00
|
|
|
QWriteLocker writeLock(&g_portableModeLock);
|
2012-02-10 01:58:45 +01:00
|
|
|
|
2015-02-08 21:14:21 +01:00
|
|
|
if(!g_portableModeInit)
|
2013-11-03 17:08:02 +01:00
|
|
|
{
|
|
|
|
if(VER_X264_PORTABLE_EDITION)
|
|
|
|
{
|
|
|
|
qWarning("Simple x264 Launcher portable edition!\n");
|
2015-02-08 21:14:21 +01:00
|
|
|
g_portableModeData = true;
|
2013-11-03 17:08:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QString baseName = QFileInfo(QApplication::applicationFilePath()).completeBaseName();
|
|
|
|
int idx1 = baseName.indexOf("x264", 0, Qt::CaseInsensitive);
|
|
|
|
int idx2 = baseName.lastIndexOf("portable", -1, Qt::CaseInsensitive);
|
2015-02-08 21:14:21 +01:00
|
|
|
g_portableModeData = (idx1 >= 0) && (idx2 >= 0) && (idx1 < idx2);
|
2013-11-03 17:08:02 +01:00
|
|
|
}
|
2015-02-08 21:14:21 +01:00
|
|
|
g_portableModeInit = true;
|
2013-11-03 17:08:02 +01:00
|
|
|
}
|
|
|
|
|
2015-02-08 21:14:21 +01:00
|
|
|
return g_portableModeData;
|
2012-02-10 01:58:45 +01:00
|
|
|
}
|
|
|
|
|
2012-02-10 18:40:28 +01:00
|
|
|
/*
|
|
|
|
* Get data path (i.e. path to store config files)
|
|
|
|
*/
|
|
|
|
const QString &x264_data_path(void)
|
|
|
|
{
|
2015-02-08 21:14:21 +01:00
|
|
|
QReadLocker readLock(&g_dataPathLock);
|
|
|
|
|
|
|
|
if(!g_dataPathData.isEmpty())
|
|
|
|
{
|
|
|
|
return g_dataPathData;
|
|
|
|
}
|
2012-02-10 18:40:28 +01:00
|
|
|
|
2015-02-08 21:14:21 +01:00
|
|
|
readLock.unlock();
|
|
|
|
QWriteLocker writeLock(&g_dataPathLock);
|
|
|
|
|
|
|
|
if(g_dataPathData.isEmpty())
|
2012-02-10 18:40:28 +01:00
|
|
|
{
|
2015-02-08 21:14:21 +01:00
|
|
|
g_dataPathData = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
|
|
|
|
if(g_dataPathData.isEmpty() || x264_is_portable())
|
2012-02-10 18:40:28 +01:00
|
|
|
{
|
2015-02-08 21:14:21 +01:00
|
|
|
g_dataPathData = QApplication::applicationDirPath();
|
2012-02-10 18:40:28 +01:00
|
|
|
}
|
2015-02-08 21:14:21 +01:00
|
|
|
if(!QDir(g_dataPathData).mkpath("."))
|
2012-02-10 18:40:28 +01:00
|
|
|
{
|
2015-02-08 21:14:21 +01:00
|
|
|
qWarning("Data directory could not be created:\n%s\n", g_dataPathData.toUtf8().constData());
|
|
|
|
g_dataPathData = QDir::currentPath();
|
2012-02-10 18:40:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 21:14:21 +01:00
|
|
|
return g_dataPathData;
|
2012-02-10 18:40:28 +01:00
|
|
|
}
|
|
|
|
|
2012-01-28 16:40:14 +01:00
|
|
|
/*
|
2015-02-08 21:14:21 +01:00
|
|
|
* Is pre-release version?
|
2012-01-28 16:40:14 +01:00
|
|
|
*/
|
2012-02-02 22:53:40 +01:00
|
|
|
bool x264_is_prerelease(void)
|
|
|
|
{
|
2012-02-04 19:09:27 +01:00
|
|
|
return (VER_X264_PRE_RELEASE);
|
2012-02-02 22:53:40 +01:00
|
|
|
}
|
|
|
|
|
2012-01-28 16:40:14 +01:00
|
|
|
/*
|
2015-02-02 22:11:06 +01:00
|
|
|
* Convert path to short/ANSI path
|
2012-01-28 16:40:14 +01:00
|
|
|
*/
|
2015-02-02 22:11:06 +01:00
|
|
|
QString x264_path2ansi(const QString &longPath, bool makeLowercase)
|
2013-11-29 18:09:38 +01:00
|
|
|
{
|
2015-02-02 22:11:06 +01:00
|
|
|
QString shortPath = longPath;
|
2013-12-09 22:19:32 +01:00
|
|
|
|
2015-02-02 22:11:06 +01:00
|
|
|
const QString longPathNative = QDir::toNativeSeparators(longPath);
|
|
|
|
DWORD buffSize = GetShortPathNameW(MUTILS_WCHR(longPathNative), NULL, NULL);
|
2013-11-29 18:09:38 +01:00
|
|
|
|
2015-02-02 22:11:06 +01:00
|
|
|
if(buffSize > 0)
|
2013-11-29 18:09:38 +01:00
|
|
|
{
|
2015-02-02 22:11:06 +01:00
|
|
|
wchar_t *buffer = (wchar_t*) _malloca(sizeof(wchar_t) * buffSize);
|
|
|
|
DWORD result = GetShortPathNameW(MUTILS_WCHR(longPathNative), buffer, buffSize);
|
2013-11-29 18:09:38 +01:00
|
|
|
|
2015-02-02 22:11:06 +01:00
|
|
|
if((result > 0) && (result < buffSize))
|
2013-11-29 18:09:38 +01:00
|
|
|
{
|
2015-02-02 22:11:06 +01:00
|
|
|
shortPath = QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast<const unsigned short*>(buffer), result));
|
2013-11-29 18:09:38 +01:00
|
|
|
|
2015-02-02 22:11:06 +01:00
|
|
|
if(makeLowercase)
|
2013-11-29 18:09:38 +01:00
|
|
|
{
|
2015-02-02 22:11:06 +01:00
|
|
|
QFileInfo info(shortPath);
|
|
|
|
shortPath = QString("%1/%2").arg(info.absolutePath(), info.fileName().toLower());
|
2013-11-29 18:09:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 22:11:06 +01:00
|
|
|
_freea(buffer);
|
|
|
|
buffer = NULL;
|
2013-11-03 18:34:20 +01:00
|
|
|
}
|
|
|
|
|
2015-02-02 22:11:06 +01:00
|
|
|
return shortPath;
|
2013-11-03 18:34:20 +01:00
|
|
|
}
|
|
|
|
|
2013-11-10 23:20:24 +01:00
|
|
|
/*
|
|
|
|
* Inform the system that it is in use, thereby preventing the system from entering sleep
|
|
|
|
*/
|
|
|
|
bool x264_set_thread_execution_state(const bool systemRequired)
|
|
|
|
{
|
|
|
|
EXECUTION_STATE state = NULL;
|
|
|
|
if(systemRequired)
|
|
|
|
{
|
|
|
|
state = SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state = SetThreadExecutionState(ES_CONTINUOUS);
|
|
|
|
}
|
|
|
|
return (state != NULL);
|
|
|
|
}
|