OSDN Git Service

Merge pull request #1 from lordmulder/master
[lamexp/LameXP.git] / src / Global.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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 #define _CRT_RAND_S
26 #include <cstdlib>
27
28 //Forward declarations
29 class QString;
30 class QStringList;
31 class QDate;
32 class QTime;
33 class QIcon;
34 class QWidget;
35 class QProcess;
36 class QColor;
37 class LockedFile;
38 enum QtMsgType;
39
40 //Variables
41 extern const char* LAMEXP_DEFAULT_LANGID;
42 extern const char* LAMEXP_DEFAULT_TRANSLATION;
43
44 ///////////////////////////////////////////////////////////////////////////////
45 // TYPE DEFINITIONS
46 ///////////////////////////////////////////////////////////////////////////////
47
48 //CPU features
49 typedef struct
50 {
51         int family;
52         int model;
53         int stepping;
54         int count;
55         bool x64;
56         bool mmx;
57         bool sse;
58         bool sse2;
59         bool sse3;
60         bool ssse3;
61         char vendor[0x40];
62         char brand[0x40];
63         bool intel;
64 }
65 lamexp_cpu_t;
66
67 //Known folders
68 typedef enum
69 {
70         lamexp_folder_localappdata = 0,
71         lamexp_folder_programfiles = 2,
72         lamexp_folder_systemfolder = 3,
73         lamexp_folder_systroot_dir = 4
74 }
75 lamexp_known_folder_t;
76
77 //LameXP user-defined events
78 typedef enum
79 {
80         lamexp_event = 1000,           /*QEvent::User*/
81         lamexp_event_queryendsession = lamexp_event + 666,
82         lamexp_event_endsession      = lamexp_event + 667
83 }
84 lamexp_event_t;
85
86 //OS version number
87 typedef struct _lamexp_os_version_t
88 {
89         unsigned int versionMajor;
90         unsigned int versionMinor;
91         bool overrideFlag;
92
93         //comparision operators
94         inline bool operator== (const _lamexp_os_version_t &rhs) const { return (versionMajor == rhs.versionMajor) && (versionMinor == rhs.versionMinor); }
95         inline bool operator!= (const _lamexp_os_version_t &rhs) const { return (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         inline bool operator<  (const _lamexp_os_version_t &rhs) const { return (versionMajor < rhs.versionMajor) || ((versionMajor == rhs.versionMajor) && (versionMinor <  rhs.versionMinor)); }
99         inline bool operator<= (const _lamexp_os_version_t &rhs) const { return (versionMajor < rhs.versionMajor) || ((versionMajor == rhs.versionMajor) && (versionMinor <= rhs.versionMinor)); }
100 }
101 lamexp_os_version_t;
102
103 //Known Windows versions
104 extern const lamexp_os_version_t lamexp_winver_win2k;
105 extern const lamexp_os_version_t lamexp_winver_winxp;
106 extern const lamexp_os_version_t lamexp_winver_xpx64;
107 extern const lamexp_os_version_t lamexp_winver_vista;
108 extern const lamexp_os_version_t lamexp_winver_win70;
109 extern const lamexp_os_version_t lamexp_winver_win80;
110 extern const lamexp_os_version_t lamexp_winver_win81;
111
112 //Beep types
113 typedef enum
114 {
115         lamexp_beep_info = 0,
116         lamexp_beep_warning = 1,
117         lamexp_beep_error = 2
118 }
119 lamexp_beep_t;
120
121 //Network connection types
122 typedef enum
123 {
124         lamexp_network_err = 0, /*unknown*/
125         lamexp_network_non = 1, /*not connected*/
126         lamexp_network_yes = 2  /*connected*/
127 }
128 lamexp_network_t;
129
130 //System color types
131 typedef enum
132 {
133         lamexp_syscolor_text = 0,
134         lamexp_syscolor_background = 1,
135         lamexp_syscolor_caption = 2
136 }
137 lamexp_syscolor_t;
138
139 //Icon type
140 class lamexp_icon_t;
141
142 ///////////////////////////////////////////////////////////////////////////////
143 // GLOBAL FUNCTIONS
144 ///////////////////////////////////////////////////////////////////////////////
145
146 const QIcon &lamexp_app_icon(void);
147 bool lamexp_append_sysmenu(const QWidget *win, const unsigned int identifier, const QString &text);
148 const QStringList &lamexp_arguments(void);
149 QStringList lamexp_available_codepages(bool noAliases = true);
150 bool lamexp_beep(int beepType);
151 void lamexp_blink_window(QWidget *poWindow, unsigned int count = 10, unsigned int delay = 150);
152 bool lamexp_block_window_move(void *message);
153 bool lamexp_bring_process_to_front(const unsigned long pid);
154 bool lamexp_bring_to_front(const QWidget *win);
155 bool lamexp_broadcast(int eventType, bool onlyToVisible);
156 bool lamexp_change_process_priority(const int priority);
157 bool lamexp_change_process_priority(void *hProcess, const int priority);
158 bool lamexp_change_process_priority(const QProcess *proc, const int priority);
159 bool lamexp_check_escape_state(void);
160 bool lamexp_check_sysmenu_msg(void *message, const unsigned int identifier);
161 bool lamexp_check_tool(const QString &toolName);
162 const QString lamexp_clean_filename(const QString &str);
163 const QString lamexp_clean_filepath(const QString &str);
164 bool lamexp_clean_folder(const QString &folderPath);
165 QDate lamexp_current_date_safe(void);
166 unsigned __int64 lamexp_current_file_time(void);
167 void lamexp_dbg_dbg_output_string(const char* format, ...);
168 unsigned long lamexp_dbg_private_bytes(void);
169 lamexp_cpu_t lamexp_detect_cpu_features(const QStringList &argv);
170 bool lamexp_detect_wine(void);
171 bool lamexp_enable_close_button(const QWidget *win, const bool bEnable = true);
172 bool lamexp_exec_shell(const QWidget *win, const QString &url, const bool explore = false);
173 bool lamexp_exec_shell(const QWidget *win, const QString &url, const QString &parameters, const QString &directory, const bool explore = false);
174 void lamexp_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage = NULL);
175 void lamexp_finalization(void);
176 unsigned __int64 lamexp_free_diskspace(const QString &path, bool *ok = NULL);
177 void lamexp_free_window_icon(lamexp_icon_t *icon);
178 const lamexp_os_version_t &lamexp_get_os_version(void);
179 void lamexp_init_console(const QStringList &argv);
180 void lamexp_init_error_handlers(void);
181 int lamexp_init_ipc(void);
182 void lamexp_init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir = true);
183 bool lamexp_init_qt(int argc, char* argv[]);
184 bool lamexp_install_translator(const QString &language);
185 bool lamexp_install_translator_from_file(const QString &qmFile);
186 void lamexp_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t);
187 void lamexp_ipc_read(unsigned int *command, char* message, size_t buffSize);
188 void lamexp_ipc_send(unsigned int command, const char* message);
189 bool lamexp_is_executable(const QString &path);
190 bool lamexp_is_hibernation_supported(void);
191 const QString &lamexp_known_folder(lamexp_known_folder_t folder_id);
192 const QString lamexp_lookup_tool(const QString &toolName);
193 void lamexp_message_handler(QtMsgType type, const char *msg);
194 const char *lamexp_mulders_url(void);
195 void lamexp_natural_string_sort(QStringList &list, const bool bIgnoreCase);
196 int lamexp_network_status(void);
197 bool lamexp_open_media_file(const QString &mediaFilePath);
198 QString lamexp_path_to_short(const QString &longPath);
199 __int64 lamexp_perfcounter_frequ(void);
200 __int64 lamexp_perfcounter_value(void);
201 bool lamexp_play_sound(const QString &name, const bool bAsync);
202 bool lamexp_play_sound_file(const QString &library, const unsigned short uiSoundIdx, const bool bAsync);
203 bool lamexp_play_sound_alias(const QString &alias, const bool bAsync);
204 bool lamexp_portable_mode(void);
205 unsigned long lamexp_process_id(const QProcess *proc);
206 QStringList lamexp_query_translations(void);
207 unsigned int lamexp_rand(void);
208 QString lamexp_rand_str(const bool bLong = false);
209 void lamexp_register_tool(const QString &toolName, LockedFile *file, unsigned int version = 0, const QString *tag = NULL);
210 bool lamexp_remove_file(const QString &filename);
211 void lamexp_seed_rand(void);
212 lamexp_icon_t *lamexp_set_window_icon(QWidget *window, const QIcon &icon, const bool bIsBigIcon);
213 bool lamexp_sheet_of_glass(QWidget *window);
214 bool lamexp_sheet_of_glass_update(QWidget *window);
215 bool lamexp_shutdown_computer(const QString &message, const unsigned long timeout = 30, const bool forceShutdown = true, const bool hibernate = false);
216 void lamexp_sleep(const unsigned int delay);
217 QColor lamexp_system_color(const int color_id);
218 int lamexp_system_message(const wchar_t *text, int beepType);
219 const char *lamexp_support_url(void);
220 const QString &lamexp_temp_folder2(void);
221 bool lamexp_themes_enabled(void);
222 unsigned int lamexp_tool_version(const QString &toolName, QString *tag = NULL);
223 unsigned int lamexp_toolver_coreaudio(void);
224 unsigned int lamexp_toolver_fhgaacenc(void);
225 unsigned int lamexp_toolver_neroaac(void);
226 unsigned int lamexp_toolver_qaacenc(void);
227 unsigned int lamexp_translation_country(const QString &langId);
228 bool lamexp_translation_init(void);
229 QString lamexp_translation_name(const QString &language);
230 bool lamexp_translation_register(const QString &langId, const QString &qmFile, const QString &langName, unsigned int &systemId, unsigned int &country);
231 unsigned int lamexp_translation_sysid(const QString &langId);
232 bool lamexp_update_sysmenu(const QWidget *win, const unsigned int identifier, const QString &text);
233 bool lamexp_user_is_admin(void);
234 const QString lamexp_version2string(const QString &pattern, unsigned int version, const QString &defaultText, const QString *tag = NULL);
235 const char *lamexp_version_arch(void);
236 unsigned int lamexp_version_build(void);
237 const char *lamexp_version_compiler(void);
238 unsigned int lamexp_version_confg(void);
239 const QDate &lamexp_version_date(void);
240 bool lamexp_version_demo(void);
241 QDate lamexp_version_expires(void);
242 unsigned int lamexp_version_major(void);
243 unsigned int lamexp_version_minor(void);
244 const char *lamexp_version_release(void);
245 const char *lamexp_version_time(void);
246 const char *lamexp_website_url(void);
247
248 ///////////////////////////////////////////////////////////////////////////////
249 // HELPER MACROS
250 ///////////////////////////////////////////////////////////////////////////////
251
252 #define LAMEXP_BOOL2STR(X) ((X) ? "1" : "0")
253 #define LAMEXP_COMPILER_WARNING(TXT) __pragma(message(__FILE__ "(" LAMEXP_MAKE_STRING(__LINE__) ") : warning: " TXT))
254 #define LAMEXP_CLOSE(HANDLE) do { if(HANDLE != NULL && HANDLE != INVALID_HANDLE_VALUE) { CloseHandle(HANDLE); HANDLE = NULL; } } while(0)
255 #define LAMEXP_DELETE(PTR) do { if(PTR) { delete PTR; PTR = NULL; } } while(0)
256 #define LAMEXP_DELETE_ARRAY(PTR) do { if(PTR) { delete [] PTR; PTR = NULL; } } while(0)
257 #define LAMEXP_MAKE_STRING_EX(X) #X
258 #define LAMEXP_MAKE_STRING(X) LAMEXP_MAKE_STRING_EX(X)
259 #define LAMEXP_SAFE_FREE(PTR) do { if(PTR) { free((void*) PTR); PTR = NULL; } } while(0)
260 #define LAMEXP_ZERO_MEMORY(X) memset(&(X), 0, sizeof((X)))
261 #define NOBR(STR) (QString("<nobr>%1</nobr>").arg((STR)).replace("-", "&minus;"))
262 #define QUTF8(STR) ((STR).toUtf8().constData())
263 #define QWCHAR(STR) (reinterpret_cast<const wchar_t*>((STR).utf16()))
264 #define WCHAR2QSTR(STR) (QString::fromUtf16(reinterpret_cast<const unsigned short*>((STR))))
265
266 //Check for debug build
267 #if defined(_DEBUG) && defined(QT_DEBUG) && !defined(NDEBUG) && !defined(QT_NO_DEBUG)
268         #define LAMEXP_DEBUG (1)
269 #elif defined(NDEBUG) && defined(QT_NO_DEBUG) && !defined(_DEBUG) && !defined(QT_DEBUG)
270         #define LAMEXP_DEBUG (0)
271 #else
272         #error Inconsistent debug defines detected!
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)
295
296 //Memory check
297 #if LAMEXP_DEBUG
298         #define LAMEXP_MEMORY_CHECK(FUNC, RETV,  ...) do \
299         { \
300                 size_t _privateBytesBefore = lamexp_dbg_private_bytes(); \
301                 RETV = FUNC(__VA_ARGS__); \
302                 size_t _privateBytesLeak = (lamexp_dbg_private_bytes() - _privateBytesBefore) / 1024; \
303                 if(_privateBytesLeak > 0) { \
304                         lamexp_dbg_dbg_output_string("\nMemory leak: Lost %u KiloBytes of PrivateUsage memory!\n\n", _privateBytesLeak); \
305                 } \
306         } \
307         while(0)
308 #else
309         #define LAMEXP_MEMORY_CHECK(FUNC, RETV,  ...) do \
310         { \
311                 RETV = __noop(__VA_ARGS__); \
312         } \
313         while(0)
314 #endif