1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2020 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 ///////////////////////////////////////////////////////////////////////////////
24 #include "targetver.h"
28 #define WIN32_LEAN_AND_MEAN
46 #define ENABLE_X264_VERSION_INCLUDE
48 #undef ENABLE_X264_VERSION_INCLUDE
51 #include <MUtils/Global.h>
54 #include <QApplication>
55 #include <QMessageBox>
61 #include <QPlastiqueStyle>
62 #include <QImageReader>
63 #include <QSharedMemory>
65 #include <QStringList>
66 #include <QSystemSemaphore>
67 #include <QDesktopServices>
73 #include <QTranslator>
76 #include <QLibraryInfo>
78 #include <QReadLocker>
79 #include <QWriteLocker>
95 typedef HRESULT (WINAPI
*SHGetKnownFolderPath_t
)(const GUID
&rfid
, DWORD dwFlags
, HANDLE hToken
, PWSTR
*ppszPath
);
96 typedef HRESULT (WINAPI
*SHGetFolderPath_t
)(HWND hwndOwner
, int nFolder
, HANDLE hToken
, DWORD dwFlags
, LPWSTR pszPath
);
99 static const char *g_x264_months
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
100 static const char *g_x264_imageformats
[] = {"png", "jpg", "gif", "ico", "svg", NULL
};
105 unsigned int ver_major
;
106 unsigned int ver_minor
;
107 unsigned int ver_patch
;
108 unsigned int ver_build
;
119 static QReadWriteLock g_portableModeLock
;
120 static bool g_portableModeData
= false;
121 static bool g_portableModeInit
= false;
124 static QString g_dataPathData
;
125 static QReadWriteLock g_dataPathLock
;
127 ///////////////////////////////////////////////////////////////////////////////
129 ///////////////////////////////////////////////////////////////////////////////
132 #define CLEAN_OUTPUT_STRING(STR) do \
134 const char CTRL_CHARS[3] = { '\r', '\n', '\t' }; \
135 for(size_t i = 0; i < 3; i++) \
137 while(char *pos = strchr((STR), CTRL_CHARS[i])) *pos = char(0x20); \
143 #define TRIM_LEFT(STR) do \
145 const char WHITE_SPACE[4] = { char(0x20), '\r', '\n', '\t' }; \
146 for(size_t i = 0; i < 4; i++) \
148 while(*(STR) == WHITE_SPACE[i]) (STR)++; \
154 static inline bool _CHECK_FLAG(const int argc
, char **argv
, const char *flag
)
156 for(int i
= 1; i
< argc
; i
++)
158 if(_stricmp(argv
[i
], flag
) == 0) return true;
163 #define CHECK_FLAG(FLAG) _CHECK_FLAG(argc, argv, "--" FLAG)
164 #define X264_ZERO_MEMORY(X) SecureZeroMemory(&X, sizeof(X))
166 ///////////////////////////////////////////////////////////////////////////////
168 ///////////////////////////////////////////////////////////////////////////////
173 unsigned int x264_version_major(void)
175 return g_x264_version
.ver_major
;
178 unsigned int x264_version_minor(void)
180 return (g_x264_version
.ver_minor
* 10) + (g_x264_version
.ver_patch
% 10);
183 unsigned int x264_version_build(void)
185 return g_x264_version
.ver_build
;
189 * Check for portable mode
191 bool x264_is_portable(void)
193 QReadLocker
readLock(&g_portableModeLock
);
195 if(g_portableModeInit
)
197 return g_portableModeData
;
201 QWriteLocker
writeLock(&g_portableModeLock
);
203 if(!g_portableModeInit
)
205 if(VER_X264_PORTABLE_EDITION
)
207 qWarning("Simple x264 Launcher portable edition!\n");
208 g_portableModeData
= true;
212 QString baseName
= QFileInfo(QApplication::applicationFilePath()).completeBaseName();
213 int idx1
= baseName
.indexOf("x264", 0, Qt::CaseInsensitive
);
214 int idx2
= baseName
.lastIndexOf("portable", -1, Qt::CaseInsensitive
);
215 g_portableModeData
= (idx1
>= 0) && (idx2
>= 0) && (idx1
< idx2
);
217 g_portableModeInit
= true;
220 return g_portableModeData
;
224 * Get data path (i.e. path to store config files)
226 const QString
&x264_data_path(void)
228 QReadLocker
readLock(&g_dataPathLock
);
230 if(!g_dataPathData
.isEmpty())
232 return g_dataPathData
;
236 QWriteLocker
writeLock(&g_dataPathLock
);
238 if(g_dataPathData
.isEmpty())
240 g_dataPathData
= QDesktopServices::storageLocation(QDesktopServices::DataLocation
);
241 if(g_dataPathData
.isEmpty() || x264_is_portable())
243 g_dataPathData
= QApplication::applicationDirPath();
245 if(!QDir(g_dataPathData
).mkpath("."))
247 qWarning("Data directory could not be created:\n%s\n", g_dataPathData
.toUtf8().constData());
248 g_dataPathData
= QDir::currentPath();
252 return g_dataPathData
;
256 * Is pre-release version?
258 bool x264_is_prerelease(void)
260 return (VER_X264_PRE_RELEASE
);
264 * Convert path to short/ANSI path
266 QString
x264_path2ansi(const QString
&longPath
, bool makeLowercase
)
268 QString shortPath
= longPath
;
270 const QString longPathNative
= QDir::toNativeSeparators(longPath
);
271 DWORD buffSize
= GetShortPathNameW(MUTILS_WCHR(longPathNative
), NULL
, NULL
);
275 wchar_t *buffer
= (wchar_t*) _malloca(sizeof(wchar_t) * buffSize
);
276 DWORD result
= GetShortPathNameW(MUTILS_WCHR(longPathNative
), buffer
, buffSize
);
278 if((result
> 0) && (result
< buffSize
))
280 shortPath
= QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast<const unsigned short*>(buffer
), result
));
284 QFileInfo
info(shortPath
);
285 shortPath
= QString("%1/%2").arg(info
.absolutePath(), info
.fileName().toLower());
297 * Inform the system that it is in use, thereby preventing the system from entering sleep
299 bool x264_set_thread_execution_state(const bool systemRequired
)
301 EXECUTION_STATE state
= NULL
;
304 state
= SetThreadExecutionState(ES_CONTINUOUS
| ES_SYSTEM_REQUIRED
| ES_AWAYMODE_REQUIRED
);
308 state
= SetThreadExecutionState(ES_CONTINUOUS
);
310 return (state
!= NULL
);