OSDN Git Service

[UI][Qt] Add save screen "Capture Screen" feature.
[csp-qt/common_source_project-fm7.git] / source / src / common.h
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.18 -
6
7         [ common header ]
8 */
9
10 #ifndef _COMMON_H_
11 #define _COMMON_H_
12
13 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
14 #define SUPPORT_TCHAR_TYPE
15 #endif
16 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
17 #define SUPPORT_SECURE_FUNCTIONS
18 #endif
19 #ifdef SUPPORT_TCHAR_TYPE
20  #include <tchar.h>
21 #endif
22 #include <stdio.h>
23
24 #if defined(_MSC_VER)
25 #include <windows.h>
26 #include <windowsx.h>
27 #include <mmsystem.h>
28 #include <process.h>
29 # define __CSP_COMPILER_MS_C
30 #endif
31
32 #if defined(_USE_QT)
33 # if defined(_USE_QT5)
34 #  include <QString>
35 #  include <QFile>
36 #  include <QtEndian>
37 # else
38 #  include <QtCore/QString>
39 #  include <QtCore/QFile>
40 # endif
41 #endif
42
43
44 #if defined(__GNUC__)
45 #include "common_gcc.h"
46 #else
47 #include <tchar.h>
48 // variable scope of 'for' loop for Microsoft Visual C++ 6.0
49 #if defined(_MSC_VER) && (_MSC_VER == 1200)
50 #define for if(0);else for
51 #endif
52
53 // disable warnings  for microsoft visual c++ 2005 or later
54 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
55 #pragma warning( disable : 4819 )
56 //#pragma warning( disable : 4995 )
57 #pragma warning( disable : 4996 )
58 #endif
59
60 // endian
61 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
62         #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
63                 #if __BYTE_ORDER == __LITTLE_ENDIAN
64                         #define __LITTLE_ENDIAN__
65                 #elif __BYTE_ORDER == __BIG_ENDIAN
66                         #define __BIG_ENDIAN__
67                 #endif
68         #elif defined(WORDS_LITTLEENDIAN)
69                 #define __LITTLE_ENDIAN__
70         #elif defined(WORDS_BIGENDIAN)
71                 #define __BIG_ENDIAN__
72         #endif
73 #endif
74 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
75         // Microsoft Visual C++
76         #define __LITTLE_ENDIAN__
77 #endif
78
79
80 // type definition
81 #ifndef uint8
82 typedef unsigned char uint8;
83 #endif
84 #ifndef uint16
85 typedef unsigned short uint16;
86 #endif
87 #ifndef uint32
88 typedef unsigned int uint32;
89 #endif
90 #ifndef uint64
91 #ifdef _MSC_VER
92 typedef unsigned __int64 uint64;
93 #else
94 typedef unsigned long long uint64;
95 #endif
96 #endif
97 #ifndef int8
98 typedef signed char int8;
99 #endif
100 #ifndef int16
101 typedef signed short int16;
102 #endif
103 #ifndef int32
104 typedef signed int int32;
105 #endif
106 #ifndef int64
107 #ifdef _MSC_VER
108 typedef signed __int64 int64;
109 #else
110 typedef signed long long int64;
111 #endif
112 #endif
113
114 static inline DWORD EndianToLittle_DWORD(DWORD x)
115 {
116    return x;
117 }
118
119 static inline WORD EndianToLittle_WORD(WORD x)
120 {
121    return x;
122 }
123
124
125 #endif
126
127 typedef union {
128         struct {
129 #ifdef __BIG_ENDIAN__
130                 uint8 h3, h2, h, l;
131 #else
132                 uint8 l, h, h2, h3;
133 #endif
134         } b;
135         struct {
136 #ifdef __BIG_ENDIAN__
137                 int8 h3, h2, h, l;
138 #else
139                 int8 l, h, h2, h3;
140 #endif
141         } sb;
142         struct {
143 #ifdef __BIG_ENDIAN__
144                 uint16 h, l;
145 #else
146                 uint16 l, h;
147 #endif
148         } w;
149         struct {
150 #ifdef __BIG_ENDIAN__
151                 int16 h, l;
152 #else
153                 int16 l, h;
154 #endif
155         } sw;
156         uint32 d;
157         int32 sd;
158         inline void read_2bytes_le_from(uint8 *t)
159         {
160                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
161         }
162         inline void write_2bytes_le_to(uint8 *t)
163         {
164                 t[0] = b.l; t[1] = b.h;
165         }
166         inline void read_2bytes_be_from(uint8 *t)
167         {
168                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
169         }
170         inline void write_2bytes_be_to(uint8 *t)
171         {
172                 t[0] = b.h; t[1] = b.l;
173         }
174         inline void read_4bytes_le_from(uint8 *t)
175         {
176                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
177         }
178         inline void write_4bytes_le_to(uint8 *t)
179         {
180                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
181         }
182         inline void read_4bytes_be_from(uint8 *t)
183         {
184                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
185         }
186         inline void write_4bytes_be_to(uint8 *t)
187         {
188                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
189         }
190
191 } pair;
192
193 // max/min from WinDef.h
194 // rgb color
195 #if !defined(_USE_QT)
196 #define _RGB888
197 #else
198 #define _RGBA888
199 #endif
200
201 #if defined(__BIG_ENDIAN__)
202 # if defined(_RGB555)
203 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) >>4) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(r) & 0xf8) << 8))
204 typedef uint16 scrntype;
205 # elif defined(_RGB565)
206 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) >>3) | (uint16)(((uint16)(g) & 0xfc) << 2) | (uint16)(((uint16)(r) & 0xf8) << 8))
207 typedef uint16 scrntype;
208 # elif defined(_RGB888) 
209 #  define RGB_COLOR(r, g, b) (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8))
210 typedef uint32 scrntype;
211 # elif defined(_RGBA888)
212 #   define RGB_COLOR(r, g, b) (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8)) | ((uint32)0xff << 0)
213 //#   define RGB_COLOR(r, g, b) (((uint32)(b) << 24) | ((uint32)(g) << 16) | ((uint32)(r) << 8)) | ((uint32)0xff << 0)
214 typedef uint32 scrntype;
215 # endif
216
217 #else // LITTLE ENDIAN
218
219 # if defined(_RGB555)
220 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) << 7) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(r) & 0xf8) >> 3))
221 typedef uint16 scrntype;
222 # elif defined(_RGB565)
223 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) << 8) | (uint16)(((uint16)(g) & 0xfc) << 3) | (uint16)(((uint16)(r) & 0xf8) >> 3))
224 typedef uint16 scrntype;
225 # elif defined(_RGB888)
226 #  define RGB_COLOR(r, g, b) (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0))
227 typedef uint32 scrntype;
228 # elif defined(_RGBA888)
229 #   define RGB_COLOR(r, g, b) (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0)) | ((uint32)0xff << 24)
230 //#   define RGB_COLOR(r, g, b) (((uint32)(b) << 16) | ((uint32)(g) << 8) | ((uint32)(r) << 0)) | ((uint32)0xff << 24)
231 typedef uint32 scrntype;
232 # endif
233
234 #endif  // ENDIAN
235
236
237 // _TCHAR
238 #ifndef SUPPORT_TCHAR_TYPE
239 typedef char _TCHAR;
240 //#define _T(s) (s)
241 #define _tfopen fopen
242 #define _tcscmp strcmp
243 #define _tcscpy strcpy
244 # if !defined(_tcsicmp)
245 # define _tcsicmp stricmp
246 # endif
247 #define _tcslen strlen
248 #define _tcsncat strncat
249 #define _tcsncpy strncpy
250 # if !defined(_tcsncicmp)
251 #define _tcsncicmp strnicmp
252 # endif
253 #define _tcsstr strstr
254 #define _tcstok strtok
255 #define _tcstol strtol
256 #define _tcstoul strtoul
257 #define _stprintf sprintf
258 #define _vstprintf vsprintf
259 #endif
260
261 #if !defined(SUPPORT_SECURE_FUNCTIONS) || defined(CSP_OS_GCC_WINDOWS) || defined(CSP_OS_GCC_CYGWIN)
262 # ifndef errno_t
263 typedef int errno_t;
264 # endif
265 #  define _strcpy_s _tcscpy_s
266 # if defined(CSP_OS_GCC_WINDOWS) || defined(CSP_OS_GCC_CYGWIN)
267 #  define strcpy_s _tcscpy_s
268 #  define tcscpy_s _tcscpy_s
269 #  define tcstok_s _tcstok_s
270 #  define stprintf_s _stprintf_s
271 #  define vstprintf_s _vstprintf_s
272 #  define vsprintf_s _vsprintf_s
273 # endif
274 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
275 _TCHAR *_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
276 int _stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
277 static inline int _vsprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...)
278 {
279         va_list ap;
280         va_start(ap, format);
281         int result = vsprintf(buffer, format, ap);
282         va_end(ap);
283         return result;
284 }
285 int _vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
286 #else
287 # define _strcpy_s _tcscpy_s
288 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
289 #endif
290
291 // secture functions
292
293 // wav file header
294 #pragma pack(1)
295 typedef struct {
296         char id[4];
297         uint32 size;
298 } wav_chunk_t;
299 #pragma pack()
300
301 #pragma pack(1)
302 typedef struct {
303         wav_chunk_t riff_chunk;
304         char wave[4];
305         wav_chunk_t fmt_chunk;
306         uint16 format_id;
307         uint16 channels;
308         uint32 sample_rate;
309         uint32 data_speed;
310         uint16 block_size;
311         uint16 sample_bits;
312 } wav_header_t;
313 #pragma pack()
314
315
316 // misc
317 #ifdef __cplusplus
318 bool check_file_extension(const _TCHAR* file_path, const _TCHAR* ext);
319 _TCHAR *get_file_path_without_extensiton(const _TCHAR* file_path);
320 uint32 getcrc32(uint8 data[], int size);
321
322
323 #define array_length(array) (sizeof(array) / sizeof(array[0]))
324
325 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
326 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
327 #define TO_BCD_LO(v)    ((v) % 10)
328 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)
329
330 #define LEAP_YEAR(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
331
332 typedef struct cur_time_s {
333         int year, month, day, day_of_week, hour, minute, second;
334         bool initialized;
335         cur_time_s()
336         {
337                 initialized = false;
338         }
339         void increment();
340         void update_year();
341         void update_day_of_week();
342         void save_state(void *f);
343         bool load_state(void *f);
344 } cur_time_t;
345 #endif
346
347 #endif