OSDN Git Service

Updated RHash and UPX to the latest versions.
[x264-launcher/x264-launcher.git] / src / global.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2016 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 //x264 includes
23 #include "global.h"
24 #include "targetver.h"
25
26 //Windows includes
27 #define NOMINMAX
28 #define WIN32_LEAN_AND_MEAN
29 #include <Windows.h>
30 #include <MMSystem.h>
31 #include <ShellAPI.h>
32 #include <Objbase.h>
33 #include <Psapi.h>
34 #include <SensAPI.h>
35
36 //C++ includes
37 #include <stdio.h>
38 #include <string.h>
39 #include <iostream>
40 #include <time.h>
41
42 //VLD
43 #include <vld.h>
44
45 //Version
46 #define ENABLE_X264_VERSION_INCLUDE
47 #include "version.h"
48 #undef  ENABLE_X264_VERSION_INCLUDE
49
50 //MUtils includes
51 #include <MUtils/Global.h>
52
53 //Qt includes
54 #include <QApplication>
55 #include <QMessageBox>
56 #include <QDir>
57 #include <QUuid>
58 #include <QMap>
59 #include <QDate>
60 #include <QIcon>
61 #include <QPlastiqueStyle>
62 #include <QImageReader>
63 #include <QSharedMemory>
64 #include <QSysInfo>
65 #include <QStringList>
66 #include <QSystemSemaphore>
67 #include <QDesktopServices>
68 #include <QMutex>
69 #include <QTextCodec>
70 #include <QLibrary>
71 #include <QRegExp>
72 #include <QResource>
73 #include <QTranslator>
74 #include <QEventLoop>
75 #include <QTimer>
76 #include <QLibraryInfo>
77 #include <QEvent>
78 #include <QReadLocker>
79 #include <QWriteLocker>
80 #include <QProcess>
81
82 //CRT includes
83 #include <fstream>
84 #include <io.h>
85 #include <fcntl.h>
86 #include <intrin.h>
87 #include <process.h>
88
89 //Debug only includes
90 #if X264_DEBUG
91 #include <Psapi.h>
92 #endif
93
94 //Global types
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);
97
98 //Const
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};
101
102 //Build version
103 static const struct
104 {
105         unsigned int ver_major;
106         unsigned int ver_minor;
107         unsigned int ver_patch;
108         unsigned int ver_build;
109 }
110 g_x264_version =
111 {
112         (VER_X264_MAJOR),
113         (VER_X264_MINOR),
114         (VER_X264_PATCH),
115         (VER_X264_BUILD),
116 };
117
118 //Portable mode
119 static QReadWriteLock g_portableModeLock;
120 static bool           g_portableModeData = false;
121 static bool           g_portableModeInit = false;
122
123 //Data path
124 static QString        g_dataPathData;
125 static QReadWriteLock g_dataPathLock;
126
127 ///////////////////////////////////////////////////////////////////////////////
128 // MACROS
129 ///////////////////////////////////////////////////////////////////////////////
130
131 //String helper
132 #define CLEAN_OUTPUT_STRING(STR) do \
133 { \
134         const char CTRL_CHARS[3] = { '\r', '\n', '\t' }; \
135         for(size_t i = 0; i < 3; i++) \
136         { \
137                 while(char *pos = strchr((STR), CTRL_CHARS[i])) *pos = char(0x20); \
138         } \
139 } \
140 while(0)
141
142 //String helper
143 #define TRIM_LEFT(STR) do \
144 { \
145         const char WHITE_SPACE[4] = { char(0x20), '\r', '\n', '\t' }; \
146         for(size_t i = 0; i < 4; i++) \
147         { \
148                 while(*(STR) == WHITE_SPACE[i]) (STR)++; \
149         } \
150 } \
151 while(0)
152
153 //Check for CLI flag
154 static inline bool _CHECK_FLAG(const int argc, char **argv, const char *flag)
155 {
156         for(int i = 1; i < argc; i++)
157         {
158                 if(_stricmp(argv[i], flag) == 0) return true;
159         }
160         return false;
161 }
162
163 #define CHECK_FLAG(FLAG) _CHECK_FLAG(argc, argv, "--" FLAG)
164 #define X264_ZERO_MEMORY(X) SecureZeroMemory(&X, sizeof(X))
165
166 ///////////////////////////////////////////////////////////////////////////////
167 // GLOBAL FUNCTIONS
168 ///////////////////////////////////////////////////////////////////////////////
169
170 /*
171  * Version info
172  */
173 unsigned int x264_version_major(void)
174 {
175         return g_x264_version.ver_major;
176 }
177
178 unsigned int x264_version_minor(void)
179 {
180         return (g_x264_version.ver_minor * 10) + (g_x264_version.ver_patch % 10);
181 }
182
183 unsigned int x264_version_build(void)
184 {
185         return g_x264_version.ver_build;
186 }
187
188 /*
189  * Check for portable mode
190  */
191 bool x264_is_portable(void)
192 {
193         QReadLocker readLock(&g_portableModeLock);
194
195         if(g_portableModeInit)
196         {
197                 return g_portableModeData;
198         }
199         
200         readLock.unlock();
201         QWriteLocker writeLock(&g_portableModeLock);
202
203         if(!g_portableModeInit)
204         {
205                 if(VER_X264_PORTABLE_EDITION)
206                 {
207                         qWarning("Simple x264 Launcher portable edition!\n");
208                         g_portableModeData = true;
209                 }
210                 else
211                 {
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);
216                 }
217                 g_portableModeInit = true;
218         }
219         
220         return g_portableModeData;
221 }
222
223 /*
224  * Get data path (i.e. path to store config files)
225  */
226 const QString &x264_data_path(void)
227 {
228         QReadLocker readLock(&g_dataPathLock);
229
230         if(!g_dataPathData.isEmpty())
231         {
232                 return g_dataPathData;
233         }
234         
235         readLock.unlock();
236         QWriteLocker writeLock(&g_dataPathLock);
237         
238         if(g_dataPathData.isEmpty())
239         {
240                 g_dataPathData = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
241                 if(g_dataPathData.isEmpty() || x264_is_portable())
242                 {
243                         g_dataPathData = QApplication::applicationDirPath();
244                 }
245                 if(!QDir(g_dataPathData).mkpath("."))
246                 {
247                         qWarning("Data directory could not be created:\n%s\n", g_dataPathData.toUtf8().constData());
248                         g_dataPathData = QDir::currentPath();
249                 }
250         }
251         
252         return g_dataPathData;
253 }
254
255 /*
256  * Is pre-release version?
257  */
258 bool x264_is_prerelease(void)
259 {
260         return (VER_X264_PRE_RELEASE);
261 }
262
263 /*
264  * Convert path to short/ANSI path
265  */
266 QString x264_path2ansi(const QString &longPath, bool makeLowercase)
267 {
268         QString shortPath = longPath;
269         
270         const QString longPathNative = QDir::toNativeSeparators(longPath);
271         DWORD buffSize = GetShortPathNameW(MUTILS_WCHR(longPathNative), NULL, NULL);
272         
273         if(buffSize > 0)
274         {
275                 wchar_t *buffer = (wchar_t*) _malloca(sizeof(wchar_t) * buffSize);
276                 DWORD result = GetShortPathNameW(MUTILS_WCHR(longPathNative), buffer, buffSize);
277
278                 if((result > 0) && (result < buffSize))
279                 {
280                         shortPath = QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast<const unsigned short*>(buffer), result));
281
282                         if(makeLowercase)
283                         {
284                                 QFileInfo info(shortPath);
285                                 shortPath = QString("%1/%2").arg(info.absolutePath(), info.fileName().toLower());
286                         }
287                 }
288
289                 _freea(buffer);
290                 buffer = NULL;
291         }
292
293         return shortPath;
294 }
295
296 /*
297  * Inform the system that it is in use, thereby preventing the system from entering sleep
298  */
299 bool x264_set_thread_execution_state(const bool systemRequired)
300 {
301         EXECUTION_STATE state = NULL;
302         if(systemRequired)
303         {
304                 state = SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED);
305         }
306         else
307         {
308                 state = SetThreadExecutionState(ES_CONTINUOUS);
309         }
310         return (state != NULL);
311 }