OSDN Git Service

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