OSDN Git Service

[Qt][VM] Fix FTBFSs with MinGW.
[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 #endif
30
31 #if defined(_USE_QT)
32 #include <SDL.h>
33 #include <stdarg.h>
34 #include "qt_input.h"
35 # if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
36 #  include <tchar.h>
37 # endif
38 # if defined(_USE_QT5)
39 #  include <QString>
40 #  include <QFile>
41 #  include <QtEndian>
42 # else
43 #  include <QtCore/QString>
44 #  include <QtCore/QFile>
45 # endif
46 #endif
47
48
49
50 #if defined(_USE_AGAR) || defined(_USE_SDL) || defined(_USE_QT)
51
52 #  ifndef uint8
53    typedef uint8_t uint8;
54 #  endif
55 #  ifndef int8
56    typedef int8_t int8;
57 #  endif
58 #  ifndef uint16
59    typedef uint16_t uint16;
60 #  endif
61 #  ifndef int16
62    typedef int16_t int16;
63 #  endif
64 #  ifndef uint32
65    typedef uint32_t uint32;
66 #  endif
67 #  ifndef int32
68    typedef int32_t int32;
69 #  endif
70 #  ifndef uint64
71    typedef uint64_t uint64;
72 #  endif
73 #  ifndef int64
74    typedef int64_t int64;
75 #  endif
76 #  ifndef BOOL
77    typedef int BOOL;
78 #  endif
79 # if !defined(Q_OS_CYGWIN) && !defined(Q_OS_WIN)
80 #  ifndef BYTE
81    typedef uint8_t BYTE;
82 #  endif
83 #  ifndef WORD
84    typedef uint16_t WORD;
85 #  endif
86 #  ifndef DWORD
87    typedef uint32_t DWORD;
88 #  endif
89 #  ifndef QWORD
90    typedef uint64_t QWORD;
91 #  endif
92 # endif
93 # if !defined(Q_OS_CYGWIN)
94 #  ifndef UINT8
95    typedef uint8_t UINT8;
96 #  endif
97 #  ifndef UINT16
98    typedef uint16_t UINT16;
99 #  endif
100 #  ifndef UINT32
101    typedef uint32_t UINT32;
102 #  endif
103 #  ifndef UINT64
104    typedef uint64_t UINT64;
105 #  endif
106
107 #  ifndef INT8
108    typedef int8_t INT8;
109 #  endif
110 #  ifndef INT16
111    typedef int16_t INT16;
112 #  endif
113 #  ifndef INT32
114    typedef int32_t INT32;
115 #  endif
116 #  ifndef INT64
117    typedef int64_t INT64;
118 #  endif
119
120 # if !defined(Q_OS_CYGWIN) && !defined(Q_OS_WIN)
121 static inline void _stprintf(char *s, const char *fmt, ...) {
122    va_list args;
123    
124    va_start(args, fmt);
125    sprintf(s, fmt, args);
126    va_end(args);
127 }
128 # endif
129 # define stricmp(a,b) strcmp(a,b)
130 # define strnicmp(a,b,n) strncmp(a,b,n)
131
132
133 #    define __T(x)      x
134  
135 #  define _T(x)       __T(x)
136 #  define _TEXT(x)    __T(x)
137
138     typedef char    _TCHAR;
139
140 # if !defined(Q_OS_CYGWIN) && !defined(Q_OS_WIN)
141 #  ifndef LPCTSTR
142     typedef _TCHAR* LPCTSTR;
143 #  endif
144 static inline char *_tcsncpy(_TCHAR *d, _TCHAR *s, int n) {
145    return strncpy((char *)d, (char *)s, n);
146 }
147
148 static inline char *_tcsncat(_TCHAR *d, _TCHAR *s, int n) {
149    return strncat((char *)d, (char *)s, n);
150 }
151 # endif
152
153 # endif
154 #endif
155
156 #if defined(_USE_QT)
157 # if defined(Q_OS_CYGWIN)
158 # define stricmp(a,b) strcmp(a,b)
159 # define strnicmp(a,b,n) strncmp(a,b,n)
160 # endif
161 # if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
162
163    typedef char    _TCHAR;
164 # endif
165 static int DeleteFile(_TCHAR *path) 
166 {
167 #ifdef _USE_QT
168        QString fpath = (char *)path;
169        QFile tfp(fpath);
170        if(tfp.remove(fpath)) return (int)true;
171        return 0;
172 #else   
173    return AG_FileDelete((const char *)path);
174 #endif
175 }
176 #include <algorithm>
177
178 # ifndef Q_OS_WIN
179 #  define _N(x) _T(x)
180 # endif
181 #endif
182
183 #if defined(_USE_QT)
184 # if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
185 #  include <tchar.h>
186 # endif
187
188 #undef __LITTLE_ENDIAN___
189 #undef __BIG_ENDIAN___
190
191 # if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
192 #  define __LITTLE_ENDIAN__
193 static inline uint32_t EndianToLittle_DWORD(uint32_t x)
194 {
195    return x;
196 }
197
198 static inline uint16_t EndianToLittle_WORD(uint16_t x)
199 {
200    return x;
201 }
202 # else // BIG_ENDIAN
203 #  define __BIG_ENDIAN__
204 static inline uint32_t EndianToLittle_DWORD(uint32_t x)
205 {
206    uint32_t y;
207    y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
208        ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);
209    return y;
210 }
211
212 static inline uint16_t EndianToLittle_WORD(uint16_t x)
213 {
214    uint16_t y;
215    y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
216    return y;
217 }
218 # endif
219 # if defined(_USE_QT) && !defined(Q_OS_WIN)
220 #  define ZeroMemory(p,s) memset(p,0x00,s)
221 #  define CopyMemory(t,f,s) memcpy(t,f,s)
222 # endif
223
224 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
225 # if SDL_BYTEORDER == SDL_LIL_ENDIAN
226 #  define __LITTLE_ENDIAN__
227 # else
228 #  define __BIG_ENDIAN__
229 # endif
230 #endif
231
232
233 #else
234 #include <tchar.h>
235
236 // variable scope of 'for' loop for Microsoft Visual C++ 6.0
237
238 #if defined(_MSC_VER) && (_MSC_VER == 1200)
239 #define for if(0);else for
240 #endif
241
242 // disable warnings  for microsoft visual c++ 2005 or later
243 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
244 #pragma warning( disable : 4819 )
245 //#pragma warning( disable : 4995 )
246 #pragma warning( disable : 4996 )
247 #endif
248
249 // endian
250 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
251         #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
252                 #if __BYTE_ORDER == __LITTLE_ENDIAN
253                         #define __LITTLE_ENDIAN__
254                 #elif __BYTE_ORDER == __BIG_ENDIAN
255                         #define __BIG_ENDIAN__
256                 #endif
257         #elif defined(WORDS_LITTLEENDIAN)
258                 #define __LITTLE_ENDIAN__
259         #elif defined(WORDS_BIGENDIAN)
260                 #define __BIG_ENDIAN__
261         #endif
262 #endif
263 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
264         // Microsoft Visual C++
265         #define __LITTLE_ENDIAN__
266 #endif
267
268
269 // type definition
270 #ifndef uint8
271 typedef unsigned char uint8;
272 #endif
273 #ifndef uint16
274 typedef unsigned short uint16;
275 #endif
276 #ifndef uint32
277 typedef unsigned int uint32;
278 #endif
279 #ifndef uint64
280 #ifdef _MSC_VER
281 typedef unsigned __int64 uint64;
282 #else
283 typedef unsigned long long uint64;
284 #endif
285 #endif
286 #ifndef int8
287 typedef signed char int8;
288 #endif
289 #ifndef int16
290 typedef signed short int16;
291 #endif
292 #ifndef int32
293 typedef signed int int32;
294 #endif
295 #ifndef int64
296 #ifdef _MSC_VER
297 typedef signed __int64 int64;
298 #else
299 typedef signed long long int64;
300 #endif
301 #endif
302
303 static inline DWORD EndianToLittle_DWORD(DWORD x)
304 {
305    return x;
306 }
307
308 static inline WORD EndianToLittle_WORD(WORD x)
309 {
310    return x;
311 }
312
313
314 #endif
315
316 typedef union {
317         struct {
318 #ifdef __BIG_ENDIAN__
319                 uint8 h3, h2, h, l;
320 #else
321                 uint8 l, h, h2, h3;
322 #endif
323         } b;
324         struct {
325 #ifdef __BIG_ENDIAN__
326                 int8 h3, h2, h, l;
327 #else
328                 int8 l, h, h2, h3;
329 #endif
330         } sb;
331         struct {
332 #ifdef __BIG_ENDIAN__
333                 uint16 h, l;
334 #else
335                 uint16 l, h;
336 #endif
337         } w;
338         struct {
339 #ifdef __BIG_ENDIAN__
340                 int16 h, l;
341 #else
342                 int16 l, h;
343 #endif
344         } sw;
345         uint32 d;
346         int32 sd;
347         inline void read_2bytes_le_from(uint8 *t)
348         {
349                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
350         }
351         inline void write_2bytes_le_to(uint8 *t)
352         {
353                 t[0] = b.l; t[1] = b.h;
354         }
355         inline void read_2bytes_be_from(uint8 *t)
356         {
357                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
358         }
359         inline void write_2bytes_be_to(uint8 *t)
360         {
361                 t[0] = b.h; t[1] = b.l;
362         }
363         inline void read_4bytes_le_from(uint8 *t)
364         {
365                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
366         }
367         inline void write_4bytes_le_to(uint8 *t)
368         {
369                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
370         }
371         inline void read_4bytes_be_from(uint8 *t)
372         {
373                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
374         }
375         inline void write_4bytes_be_to(uint8 *t)
376         {
377                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
378         }
379
380 } pair;
381
382 // max/min from WinDef.h
383 #if !defined(MSC_VER)
384 //#ifndef max
385 static inline int max(int a, int b) {
386         return (((a) > (b)) ? (a) : (b));
387 }
388 static inline uint32_t max(uint32_t a, uint32_t b) {
389         return (((a) > (b)) ? (a) : (b));
390 }
391
392 static inline int min(int a, int b) {
393         return (((a) < (b)) ? (a) : (b));
394 }
395 static inline uint32_t min(uint32_t a, uint32_t b) {
396         return (((a) < (b)) ? (a) : (b));
397 }
398 //#endif
399 #endif
400 // rgb color
401 #if !defined(_USE_QT)
402 #define _RGB888
403 #else
404 #define _RGBA888
405 #endif
406
407 #if defined(__BIG_ENDIAN__)
408 # if defined(_RGB555)
409 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) >>4) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(r) & 0xf8) << 8))
410 typedef uint16 scrntype;
411 # elif defined(_RGB565)
412 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) >>3) | (uint16)(((uint16)(g) & 0xfc) << 2) | (uint16)(((uint16)(r) & 0xf8) << 8))
413 typedef uint16 scrntype;
414 # elif defined(_RGB888) 
415 #  define RGB_COLOR(r, g, b) (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8))
416 typedef uint32 scrntype;
417 # elif defined(_RGBA888)
418 //#   define RGB_COLOR(r, g, b) (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8)) | ((uint32)0xff << 0)
419 #   define RGB_COLOR(r, g, b) (((uint32)(b) << 24) | ((uint32)(g) << 16) | ((uint32)(r) << 8)) | ((uint32)0xff << 0)
420 typedef uint32 scrntype;
421 # endif
422
423 #else // LITTLE ENDIAN
424
425 # if defined(_RGB555)
426 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) << 7) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(r) & 0xf8) >> 3))
427 typedef uint16 scrntype;
428 # elif defined(_RGB565)
429 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) << 8) | (uint16)(((uint16)(g) & 0xfc) << 3) | (uint16)(((uint16)(r) & 0xf8) >> 3))
430 typedef uint16 scrntype;
431 # elif defined(_RGB888)
432 #  define RGB_COLOR(r, g, b) (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0))
433 typedef uint32 scrntype;
434 # elif defined(_RGBA888)
435 //#   define RGB_COLOR(r, g, b) (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0)) | ((uint32)0xff << 24)
436 #   define RGB_COLOR(r, g, b) (((uint32)(b) << 16) | ((uint32)(g) << 8) | ((uint32)(r) << 0)) | ((uint32)0xff << 24)
437 typedef uint32 scrntype;
438 # endif
439
440 #endif  // ENDIAN
441
442
443 // _TCHAR
444 #ifndef SUPPORT_TCHAR_TYPE
445 typedef char _TCHAR;
446 //#define _T(s) (s)
447 #define _tfopen fopen
448 #define _tcscmp strcmp
449 #define _tcscpy strcpy
450 # if !defined(_tcsicmp)
451 # define _tcsicmp stricmp
452 # endif
453 #define _tcslen strlen
454 #define _tcsncat strncat
455 #define _tcsncpy strncpy
456 # if !defined(_tcsncicmp)
457 #define _tcsncicmp strnicmp
458 # endif
459 #define _tcsstr strstr
460 #define _tcstok strtok
461 #define _tcstol strtol
462 #define _tcstoul strtoul
463 #define _stprintf sprintf
464 #define _vstprintf vsprintf
465 #endif
466
467 #if !defined(_MSC_VER)
468 #include <errno.h>
469 typedef int errno_t;
470 #endif
471 // secture functions
472 #if !defined(SUPPORT_SECURE_FUNCTIONS) || defined(Q_OS_WIN)
473 # ifndef errno_t
474 typedef int errno_t;
475 # endif
476 #  define _strcpy_s _tcscpy_s
477 # if defined(Q_OS_WIN)
478 #  define strcpy_s _tcscpy_s
479 #  define tcscpy_s _tcscpy_s
480 #  define tcstok_s _tcstok_s
481 #  define stprintf_s _stprintf_s
482 #  define vstprintf_s _vstprintf_s
483 #  define vsprintf_s _vsprintf_s
484 # endif
485 //# if !defined(Q_OS_WIN) //&& !defined(Q_OS_CYGWIN)
486 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
487 _TCHAR *_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
488 //# endif
489 int _stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
490 int _vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
491 int _vsprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
492 #else
493 # define _strcpy_s _tcscpy_s
494 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
495 #endif
496
497 // wav file header
498 #pragma pack(1)
499 typedef struct {
500         char id[4];
501         uint32 size;
502 } wav_chunk_t;
503 #pragma pack()
504
505 #pragma pack(1)
506 typedef struct {
507         wav_chunk_t riff_chunk;
508         char wave[4];
509         wav_chunk_t fmt_chunk;
510         uint16 format_id;
511         uint16 channels;
512         uint32 sample_rate;
513         uint32 data_speed;
514         uint16 block_size;
515         uint16 sample_bits;
516 } wav_header_t;
517 #pragma pack()
518
519
520 // misc
521 #ifdef __cplusplus
522 bool check_file_extension(const _TCHAR* file_path, const _TCHAR* ext);
523 _TCHAR *get_file_path_without_extensiton(const _TCHAR* file_path);
524 uint32 getcrc32(uint8 data[], int size);
525
526
527 #define array_length(array) (sizeof(array) / sizeof(array[0]))
528
529 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
530 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
531 #define TO_BCD_LO(v)    ((v) % 10)
532 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)
533
534 #define LEAP_YEAR(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
535
536 typedef struct cur_time_s {
537         int year, month, day, day_of_week, hour, minute, second;
538         bool initialized;
539         cur_time_s()
540         {
541                 initialized = false;
542         }
543         void increment();
544         void update_year();
545         void update_day_of_week();
546         void save_state(void *f);
547         bool load_state(void *f);
548 } cur_time_t;
549 #endif
550
551 //#endif
552
553 #endif