OSDN Git Service

Added lamexp_init_process() function, which can be used to setup a QProcess object.
[lamexp/LameXP.git] / src / Global.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #pragma once
24
25 //Target version
26 #include "Targetver.h"
27
28 //inlcude C standard library
29 #define _CRT_RAND_S
30 #include <stdio.h>
31 #include <tchar.h>
32
33 //Visual Leaks Detector
34 #include <vld.h>
35
36 //Declarations
37 class QString;
38 class QStringList;
39 class QDate;
40 class QTime;
41 class QIcon;
42 class QWidget;
43 class QProcess;
44 class LockedFile;
45 enum QtMsgType;
46
47 //Types definitions
48 typedef struct
49 {
50         int family;
51         int model;
52         int stepping;
53         int count;
54         bool x64;
55         bool mmx;
56         bool sse;
57         bool sse2;
58         bool sse3;
59         bool ssse3;
60         char vendor[0x40];
61         char brand[0x40];
62         bool intel;
63 }
64 lamexp_cpu_t;
65
66 //Known folders
67 typedef enum
68 {
69         lamexp_folder_localappdata = 0,
70         lamexp_folder_programfiles = 2,
71         lamexp_folder_systemfolder = 3
72 }
73 lamexp_known_folder_t;
74
75 //LameXP user-defined events
76 typedef enum
77 {
78         lamexp_event = 1000,           /*QEvent::User*/
79         lamexp_event_queryendsession = lamexp_event + 666,
80         lamexp_event_endsession      = lamexp_event + 667
81 }
82 lamexp_event_t;
83
84 //OS version number
85 typedef struct _lamexp_os_version_t
86 {
87         unsigned int versionMajor;
88         unsigned int versionMinor;
89         bool overrideFlag;
90
91         //comparision operators
92         inline bool operator== (const _lamexp_os_version_t &rhs) const { return (versionMajor == rhs.versionMajor) && (versionMinor == rhs.versionMinor); }
93         inline bool operator!= (const _lamexp_os_version_t &rhs) const { return (versionMajor != rhs.versionMajor) || (versionMinor != rhs.versionMinor); }
94         inline bool operator>  (const _lamexp_os_version_t &rhs) const { return (versionMajor > rhs.versionMajor) || ((versionMajor == rhs.versionMajor) && (versionMinor >  rhs.versionMinor)); }
95         inline bool operator>= (const _lamexp_os_version_t &rhs) const { return (versionMajor > rhs.versionMajor) || ((versionMajor == rhs.versionMajor) && (versionMinor >= rhs.versionMinor)); }
96         inline bool operator<  (const _lamexp_os_version_t &rhs) const { return (versionMajor < rhs.versionMajor) || ((versionMajor == rhs.versionMajor) && (versionMinor <  rhs.versionMinor)); }
97         inline bool operator<= (const _lamexp_os_version_t &rhs) const { return (versionMajor < rhs.versionMajor) || ((versionMajor == rhs.versionMajor) && (versionMinor <= rhs.versionMinor)); }
98 }
99 lamexp_os_version_t;
100
101 //Known Windows versions
102 extern const lamexp_os_version_t lamexp_winver_win2k;
103 extern const lamexp_os_version_t lamexp_winver_winxp;
104 extern const lamexp_os_version_t lamexp_winver_xpx64;
105 extern const lamexp_os_version_t lamexp_winver_vista;
106 extern const lamexp_os_version_t lamexp_winver_win70;
107 extern const lamexp_os_version_t lamexp_winver_win80;
108 extern const lamexp_os_version_t lamexp_winver_win81;
109
110 //Beep types
111 typedef enum
112 {
113         lamexp_beep_info = 0,
114         lamexp_beep_warning = 1,
115         lamexp_beep_error = 2
116 }
117 lamexp_beep_t;
118
119 //Network connection types
120 typedef enum
121 {
122         lamexp_network_err = 0, /*unknown*/
123         lamexp_network_non = 1, /*not connected*/
124         lamexp_network_yes = 2  /*connected*/
125 }
126 lamexp_network_t;
127
128 //LameXP version info
129 unsigned int lamexp_version_major(void);
130 unsigned int lamexp_version_minor(void);
131 unsigned int lamexp_version_build(void);
132 unsigned int lamexp_version_confg(void);
133 const QDate &lamexp_version_date(void);
134 const char *lamexp_version_time(void);
135 const char *lamexp_version_release(void);
136 bool lamexp_version_demo(void);
137 const char *lamexp_version_compiler(void);
138 const char *lamexp_version_arch(void);
139 QDate lamexp_version_expires(void);
140 unsigned int lamexp_toolver_neroaac(void);
141 unsigned int lamexp_toolver_fhgaacenc(void);
142 unsigned int lamexp_toolver_qaacenc(void);
143 unsigned int lamexp_toolver_coreaudio(void);
144 const char *lamexp_website_url(void);
145 const char *lamexp_mulders_url(void);
146 const char *lamexp_support_url(void);
147 const lamexp_os_version_t &lamexp_get_os_version(void);
148 bool lamexp_detect_wine(void);
149
150 //Public functions
151 void lamexp_init_console(const QStringList &argv);
152 bool lamexp_init_qt(int argc, char* argv[]);
153 int lamexp_init_ipc(void);
154 void lamexp_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t);
155 void lamexp_message_handler(QtMsgType type, const char *msg);
156 void lamexp_register_tool(const QString &toolName, LockedFile *file, unsigned int version = 0, const QString *tag = NULL);
157 bool lamexp_check_tool(const QString &toolName);
158 const QString lamexp_lookup_tool(const QString &toolName);
159 unsigned int lamexp_tool_version(const QString &toolName, QString *tag = NULL);
160 void lamexp_finalization(void);
161 QString lamexp_rand_str(const bool bLong = false);
162 const QString &lamexp_temp_folder2(void);
163 void lamexp_ipc_read(unsigned int *command, char* message, size_t buffSize);
164 void lamexp_ipc_send(unsigned int command, const char* message);
165 lamexp_cpu_t lamexp_detect_cpu_features(const QStringList &argv);
166 bool lamexp_portable_mode(void);
167 bool lamexp_shutdown_computer(const QString &message, const unsigned long timeout = 30, const bool forceShutdown = true, const bool hibernate = false);
168 bool lamexp_is_hibernation_supported(void);
169 QIcon lamexp_app_icon(const QDate *date = NULL, const QTime *time = NULL);
170 const QStringList &lamexp_arguments(void);
171
172 //Translation support
173 QStringList lamexp_query_translations(void);
174 bool lamexp_translation_register(const QString &langId, const QString &qmFile, const QString &langName, unsigned int &systemId, unsigned int &country);
175 QString lamexp_translation_name(const QString &language);
176 unsigned int lamexp_translation_sysid(const QString &langId);
177 unsigned int lamexp_translation_country(const QString &langId);
178 bool lamexp_install_translator_from_file(const QString &qmFile);
179 bool lamexp_install_translator(const QString &language);
180 QStringList lamexp_available_codepages(bool noAliases = true);
181 extern const char* LAMEXP_DEFAULT_LANGID;
182 extern const char* LAMEXP_DEFAULT_TRANSLATION;
183
184 //Auxiliary functions
185 bool lamexp_clean_folder(const QString &folderPath);
186 const QString lamexp_version2string(const QString &pattern, unsigned int version, const QString &defaultText, const QString *tag = NULL);
187 const QString &lamexp_known_folder(lamexp_known_folder_t folder_id);
188 unsigned __int64 lamexp_free_diskspace(const QString &path, bool *ok = NULL);
189 bool lamexp_remove_file(const QString &filename);
190 bool lamexp_themes_enabled(void);
191 void lamexp_blink_window(QWidget *poWindow, unsigned int count = 10, unsigned int delay = 150);
192 const QString lamexp_clean_filename(const QString &str);
193 const QString lamexp_clean_filepath(const QString &str);
194 void lamexp_seed_rand(void);
195 unsigned int lamexp_rand(void);
196 QDate lamexp_current_date_safe(void);
197 void lamexp_sleep(const unsigned int delay);
198 bool lamexp_beep(int beepType);
199 bool lamexp_play_sound(const unsigned short uiSoundIdx, const bool bAsync, const wchar_t *alias = NULL);
200 bool lamexp_play_sound_file(const QString &library, const unsigned short uiSoundIdx, const bool bAsync);
201 bool lamexp_exec_shell(const QWidget *win, const QString &url, const bool explore = false);
202 bool lamexp_exec_shell(const QWidget *win, const QString &url, const QString &parameters, const QString &directory, const bool explore = false);
203 __int64 lamexp_perfcounter_frequ(void);
204 __int64 lamexp_perfcounter_value(void);
205 bool lamexp_append_sysmenu(const QWidget *win, const unsigned int identifier, const QString &text);
206 bool lamexp_update_sysmenu(const QWidget *win, const unsigned int identifier, const QString &text);
207 bool lamexp_check_sysmenu_msg(void *message, const unsigned int identifier);
208 bool lamexp_enable_close_button(const QWidget *win, const bool bEnable = true);
209 bool lamexp_check_escape_state(void);
210 bool lamexp_change_process_priority(const int priority);
211 bool lamexp_change_process_priority(const QProcess *proc, const int priority);
212 bool lamexp_change_process_priority(void *hProcess, const int priority);
213 bool lamexp_bring_to_front(const QWidget *win);
214 bool lamexp_bring_process_to_front(const unsigned long pid);
215 int lamexp_network_status(void);
216 unsigned long lamexp_process_id(const QProcess *proc);
217 unsigned __int64 lamexp_current_file_time(void);
218 void lamexp_natural_string_sort(QStringList &list, const bool bIgnoreCase);
219 bool lamexp_open_media_file(const QString &mediaFilePath);
220 QString lamexp_path_to_short(const QString &longPath);
221 void lamexp_init_process(QProcess &process, const QString &wokringDir);
222 void lamexp_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage = NULL);
223
224
225 //Debug-only functions
226 unsigned long lamexp_dbg_private_bytes(void);
227
228 //Helper macros
229 #define LAMEXP_DELETE(PTR) do { if(PTR) { delete PTR; PTR = NULL; } } while(0)
230 #define LAMEXP_DELETE_ARRAY(PTR) do { if(PTR) { delete [] PTR; PTR = NULL; } } while(0)
231 #define LAMEXP_SAFE_FREE(PTR) do { if(PTR) { free((void*) PTR); PTR = NULL; } } while(0)
232 #define LAMEXP_CLOSE(HANDLE) do { if(HANDLE != NULL && HANDLE != INVALID_HANDLE_VALUE) { CloseHandle(HANDLE); HANDLE = NULL; } } while(0)
233 #define QWCHAR(STR) (reinterpret_cast<const wchar_t*>((STR).utf16()))
234 #define WCHAR2QSTR(STR) (QString::fromUtf16(reinterpret_cast<const unsigned short*>((STR))))
235 #define QUTF8(STR) ((STR).toUtf8().constData())
236 #define LAMEXP_BOOL2STR(X) ((X) ? "1" : "0")
237 #define LAMEXP_MAKE_STRING_EX(X) #X
238 #define LAMEXP_MAKE_STRING(X) LAMEXP_MAKE_STRING_EX(X)
239 #define LAMEXP_COMPILER_WARNING(TXT) __pragma(message(__FILE__ "(" LAMEXP_MAKE_STRING(__LINE__) ") : warning: " TXT))
240 #define NOBR(STR) (QString("<nobr>%1</nobr>").arg((STR)).replace("-", "&minus;"))
241
242 //Check for debug build
243 #if defined(_DEBUG) && defined(QT_DEBUG) && !defined(NDEBUG) && !defined(QT_NO_DEBUG)
244         #define LAMEXP_DEBUG (1)
245 #elif defined(NDEBUG) && defined(QT_NO_DEBUG) && !defined(_DEBUG) && !defined(QT_DEBUG)
246         #define LAMEXP_DEBUG (0)
247 #else
248         #error Inconsistent debug defines detected!
249 #endif
250
251 //Memory check
252 #if LAMEXP_DEBUG
253         #define LAMEXP_MEMORY_CHECK(FUNC, RETV,  ...) do \
254         { \
255                 SIZE_T _privateBytesBefore = lamexp_dbg_private_bytes(); \
256                 RETV = FUNC(__VA_ARGS__); \
257                 SIZE_T _privateBytesLeak = (lamexp_dbg_private_bytes() - _privateBytesBefore) / 1024; \
258                 if(_privateBytesLeak > 0) { \
259                         char _buffer[128]; \
260                         _snprintf_s(_buffer, 128, _TRUNCATE, "Memory leak: Lost %u KiloBytes of PrivateUsage memory.\n", _privateBytesLeak); \
261                         OutputDebugStringA("----------\n"); \
262                         OutputDebugStringA(_buffer); \
263                         OutputDebugStringA("----------\n"); \
264                 } \
265         } \
266         while(0)
267 #else
268         #define LAMEXP_MEMORY_CHECK(FUNC, RETV,  ...) do \
269         { \
270                 RETV = __noop(__VA_ARGS__); \
271         } \
272         while(0)
273 #endif
274
275 //Check for CPU-compatibility options
276 #if !defined(_M_X64) && defined(_MSC_VER) && defined(_M_IX86_FP)
277         #if (_M_IX86_FP != 0)
278                 #error We should not enabled SSE or SSE2 in release builds!
279         #endif
280 #endif
281
282 //Helper macro for throwing exceptions
283 #define THROW(MESSAGE) do \
284 { \
285         throw std::runtime_error((MESSAGE)); \
286 } \
287 while(0)
288 #define THROW_FMT(FORMAT, ...) do \
289 { \
290         char _error_msg[512]; \
291         _snprintf_s(_error_msg, 512, _TRUNCATE, (FORMAT), __VA_ARGS__); \
292         throw std::runtime_error(_error_msg); \
293 } \
294 while(0)