OSDN Git Service

[BUILD][CMAKE][Qt][MINGW] Fix FTBFSs with MinGW32's GCC, Windows.
[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
20 // secure functions need tchar type
21 #ifndef SUPPORT_TCHAR_TYPE
22 #undef SUPPORT_SECURE_FUNCTIONS
23 #endif
24
25 #ifdef SUPPORT_TCHAR_TYPE
26 #include <tchar.h>
27 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32
33 // LOG COMMAND
34 #define EMU_LOG_CPU1        0x00000001
35 #define EMU_LOG_CPU2        0x00000002
36 #define EMU_LOG_CPU3        0x00000004
37 #define EMU_LOG_CPU4        0x00000008
38 #define EMU_LOG_FLOPPY      0x00000010
39 #define EMU_LOG_CMT         0x00000020
40 #define EMU_LOG_QD          0x00000040
41 #define EMU_LOG_CART        0x00000080
42 #define EMU_LOG_BINARY      0x00000100
43 #define EMU_LOG_LASERDISC   0x00000200
44 #define EMU_LOG_DISPLAY     0x00001000
45 #define EMU_LOG_SOUND       0x00002000
46 #define EMU_LOG_KEYBOARD    0x00004000
47 #define EMU_LOG_IO          0x00008000
48 #define EMU_LOG_MEMORY      0x00010000
49 #define EMU_LOG_USR1        0x00020000
50 #define EMU_LOG_USR2        0x00040000
51 #define EMU_LOG_USR3        0x00080000
52 #define EMU_LOG_USR4        0x00100000
53 #define EMU_LOG_USR5        0x00200000
54 #define EMU_LOG_USR6        0x00400000
55 #define EMU_LOG_USR7        0x00800000
56 #define EMU_LOG_USR8        0x01000000
57 #define EMU_LOG_USR9        0x02000000
58 #define EMU_LOG_USR10       0x04000000
59 #define EMU_LOG_GUI         0x08000000
60 #define EMU_LOG_DEBUG       0x10000000
61 #define EMU_LOG_INFO        0x20000000
62 #define EMU_LOG_WARNING     0x40000000
63 #define EMU_LOG_GENERAL     0x80000000
64
65 #ifdef _MSC_VER
66 #include <windows.h>
67 #include <windowsx.h>
68 #include <mmsystem.h>
69 #include <process.h>
70 # define __CSP_COMPILER_MS_C
71 #endif
72
73 #if defined(_USE_QT)
74 # if defined(_USE_QT5)
75 #  include <QtEndian>
76 # endif
77 # include <sys/param.h>
78 # ifndef _MAX_PATH
79 #  define _MAX_PATH 2048
80 # endif
81 #endif
82
83
84 #if defined(__GNUC__)
85 #include "common_gcc.h"
86 #elif defined(_MSC_VER)
87 //#include "common_msc.h"
88 #endif
89 #ifdef _WIN32
90         #ifdef _MSC_VER
91                 #if _MSC_VER == 1200
92                         // variable scope of 'for' loop for Microsoft Visual C++ 6.0
93                         #define for if(0);else for
94                 #endif
95                 #if _MSC_VER >= 1200
96                         #define SUPPORT_TCHAR_TYPE
97                 #endif
98                 #if _MSC_VER >= 1400
99                         #define SUPPORT_SECURE_FUNCTIONS
100                         // disable warnings for Microsoft Visual C++ 2005 or later
101                         #pragma warning( disable : 4819 )
102                         //#pragma warning( disable : 4995 )
103                         #pragma warning( disable : 4996 )
104                 #endif
105         #else
106                 // Windows, but not VC++
107                 #define SUPPORT_TCHAR_TYPE
108 //              #define SUPPORT_SECURE_FUNCTIONS
109         #endif
110  #endif
111
112 // disable warnings  for microsoft visual c++ 2005 or later
113 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
114 #pragma warning( disable : 4819 )
115 //#pragma warning( disable : 4995 )
116 #pragma warning( disable : 4996 )
117 #endif
118
119 // endian
120 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
121         #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
122                 #if __BYTE_ORDER == __LITTLE_ENDIAN
123                         #define __LITTLE_ENDIAN__
124                 #elif __BYTE_ORDER == __BIG_ENDIAN
125                         #define __BIG_ENDIAN__
126                 #endif
127         #elif defined(SDL_BYTEORDER) && (defined(SDL_LIL_ENDIAN) || defined(SDL_BIG_ENDIAN))
128                 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
129                         #define __LITTLE_ENDIAN__
130                 #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
131                         #define __BIG_ENDIAN__
132                 #endif
133         #elif defined(WORDS_LITTLEENDIAN)
134                 #define __LITTLE_ENDIAN__
135         #elif defined(WORDS_BIGENDIAN)
136                 #define __BIG_ENDIAN__
137         #endif
138 #endif
139 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
140         // may be Microsoft Visual C++
141         #define __LITTLE_ENDIAN__
142 #endif
143
144
145 // type definition
146 #if !defined(_MSC_VER) && !defined(CSP_OS_GCC_WINDOWS) && !defined(CSP_OS_GCC_CYGWIN)
147         #ifndef boolean
148                 typedef bool boolean;
149         #endif
150         #ifndef byte
151                 typedef unsigned char byte;
152         #endif
153 #endif
154 #ifndef uchar
155         typedef unsigned char uchar;
156 #endif
157 #ifndef uint8
158  typedef unsigned char uint8;
159 #endif
160 #ifndef uint16
161  typedef unsigned short uint16;
162 #endif
163 #ifndef uint32
164  typedef unsigned int uint32;
165 #endif
166 #ifndef uint64
167  #ifdef _MSC_VER
168  typedef unsigned __int64 uint64;
169  #else
170 //typedef unsigned long long uint64;
171  #endif
172 #endif
173 #ifndef int8
174  typedef signed char int8;
175 #endif
176 #ifndef int16
177  typedef signed short int16;
178 #endif
179 #ifndef int32
180  typedef signed int int32;
181 #endif
182 #ifndef int64
183  #ifdef _MSC_VER
184   typedef signed __int64 int64;
185  #else
186  //typedef signed long long int64;
187  #endif
188 #endif
189 #ifndef SUPPORT_TCHAR_TYPE
190         #ifndef _TCHAR
191                 typedef char _TCHAR;
192         #endif
193 #endif
194 #if defined(_MSC_VER) 
195 # define CSP_OS_VISUALC
196 # define CSP_OS_WINDOWS
197 typedef int errno_t;
198 typedef uint8_t uint8;
199 typedef int8_t int8;
200 typedef uint16_t uint16;
201 typedef int16_t int16;
202 typedef uint32_t uint32;
203 typedef int32_t int32;
204 typedef uint64_t uint64;
205 typedef int64_t int64;
206 typedef int BOOL;
207
208 typedef uint8_t BYTE;
209 typedef uint16_t WORD;
210 typedef uint32_t DWORD;
211 typedef uint64_t QWORD;
212 //# if !defined(Q_OS_CYGWIN)
213 typedef uint8_t UINT8;
214 typedef uint16_t UINT16;
215 typedef uint32_t UINT32;
216 typedef uint64_t UINT64;
217 typedef int8_t INT8;
218 typedef int16_t INT16;
219 typedef int32_t INT32;
220 typedef int64_t INT64;
221 #endif
222
223 #ifndef _WIN32
224         #ifndef LPTSTR
225                 typedef _TCHAR* LPTSTR;
226         #endif
227         #ifndef LPCTSTR // This with const causes FTBFS with GCC/LLVM . 
228                 typedef _TCHAR* LPCTSTR;
229         #endif
230 #endif
231
232 #if defined(__LITTLE_ENDIAN__)
233 static inline uint32_t EndianToLittle_DWORD(uint32_t x)
234 {
235    return x;
236 }
237
238 static inline uint16_t EndianToLittle_WORD(uint16_t x)
239 {
240    return x;
241 }
242 #else // BIG_ENDIAN
243 static inline uint32_t EndianToLittle_DWORD(uint32_t x)
244 {
245    uint32_t y;
246    y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
247        ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);
248    return y;
249 }
250
251 static inline uint16_t EndianToLittle_WORD(uint16_t x)
252 {
253    uint16_t y;
254    y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
255    return y;
256 }
257 #endif
258
259 typedef union {
260         struct {
261 #ifdef __BIG_ENDIAN__
262                 uint8 h3, h2, h, l;
263 #else
264                 uint8 l, h, h2, h3;
265 #endif
266         } b;
267         struct {
268 #ifdef __BIG_ENDIAN__
269                 int8 h3, h2, h, l;
270 #else
271                 int8 l, h, h2, h3;
272 #endif
273         } sb;
274         struct {
275 #ifdef __BIG_ENDIAN__
276                 uint16 h, l;
277 #else
278                 uint16 l, h;
279 #endif
280         } w;
281         struct {
282 #ifdef __BIG_ENDIAN__
283                 int16 h, l;
284 #else
285                 int16 l, h;
286 #endif
287         } sw;
288         uint32 d;
289         int32 sd;
290         inline void read_2bytes_le_from(uint8 *t)
291         {
292                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
293         }
294         inline void write_2bytes_le_to(uint8 *t)
295         {
296                 t[0] = b.l; t[1] = b.h;
297         }
298         inline void read_2bytes_be_from(uint8 *t)
299         {
300                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
301         }
302         inline void write_2bytes_be_to(uint8 *t)
303         {
304                 t[0] = b.h; t[1] = b.l;
305         }
306         inline void read_4bytes_le_from(uint8 *t)
307         {
308                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
309         }
310         inline void write_4bytes_le_to(uint8 *t)
311         {
312                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
313         }
314         inline void read_4bytes_be_from(uint8 *t)
315         {
316                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
317         }
318         inline void write_4bytes_be_to(uint8 *t)
319         {
320                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
321         }
322
323 } pair;
324
325 // max/min from WinDef.h
326 #ifndef _MSC_VER
327         #undef max
328         #undef min
329         #define MAX_MACRO_NOT_DEFINED
330         int max(int a, int b);
331         unsigned int max(unsigned int a, unsigned int b);
332         #define MIN_MACRO_NOT_DEFINED
333         int min(int a, int b);
334         unsigned int min(unsigned int a, unsigned int b);
335 #endif
336
337 //#if defined(__GNUC__) || defined(__CYGWIN__) || defined(Q_OS_CYGWIN)
338 //      #define stricmp(a,b) strcasecmp(a,b)
339 //      #define strnicmp(a,b,n) strncasecmp(a,b,n)
340 //#endif
341
342 // _TCHAR
343 #ifndef SUPPORT_TCHAR_TYPE
344         #ifndef _tfopen
345                 #define _tfopen fopen
346         #endif
347         #ifndef _tcscmp
348                 #define _tcscmp strcmp
349         #endif
350         #ifndef _tcscpy
351                 #define _tcscpy strcpy
352         #endif
353         #ifndef _tcsicmp
354                 #define _tcsicmp stricmp
355         #endif
356         #ifndef _tcslen
357                 #define _tcslen strlen
358         #endif
359         #ifndef _tcsncat
360                 #define _tcsncat strncat
361         #endif
362         #ifndef _tcsncpy
363                 #define _tcsncpy strncpy
364         #endif
365         #ifndef _tcsncicmp
366                 #define _tcsncicmp strnicmp
367         #endif
368         #ifndef _tcschr
369                 #define _tcschr strchr
370         #endif
371         #ifndef _tcsrchr
372                 #define _tcsrchr strrchr
373         #endif
374         #ifndef _tcsstr
375                 #define _tcsstr strstr
376         #endif
377         #ifndef _tcstok
378                 #define _tcstok strtok
379         #endif
380         #ifndef _tcstol
381                 #define _tcstol strtol
382         #endif
383         #ifndef _tcstoul
384                 #define _tcstoul strtoul
385         #endif
386         #ifndef _stprintf
387                 #define _stprintf sprintf
388         #endif
389         #ifndef _vstprintf
390                 #define _vstprintf vsprintf
391         #endif
392         #ifndef _taccess
393                 #define _taccess access
394         #endif
395         #ifndef _tremove
396                 #define _tremove remove
397         #endif
398         #ifndef _trename
399                 #define _trename rename
400         #endif
401         #define __T(x) x
402         #define _T(x) __T(x)
403         #define _TEXT(x) __T(x)
404 #endif
405
406 #ifndef SUPPORT_SECURE_FUNCTIONS
407         #ifndef errno_t
408                 typedef int errno_t;
409         #endif
410 //      errno_t my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode);
411         errno_t my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
412         errno_t my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
413         errno_t my_strncpy_s(char *strDestination, size_t numberOfElements, const char *strSource, size_t count);
414         errno_t my_tcsncpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource, size_t count);
415         char *my_strtok_s(char *strToken, const char *strDelimit, char **context);
416         _TCHAR *my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
417         #define my_fprintf_s fprintf
418         int my_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
419         int my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
420         int my_vsprintf_s(char *buffer, size_t numberOfElements, const char *format, va_list argptr);
421         int my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
422 #else
423 //      #define my_tfopen_s _tfopen_s
424         #define my_strcpy_s strcpy_s
425         #define my_tcscpy_s _tcscpy_s
426         #define my_strncpy_s strncpy_s
427         #define my_tcsncpy_s _tcsncpy_s
428         #define my_strtok_s strtok_s
429         #define my_tcstok_s _tcstok_s
430         #define my_fprintf_s fprintf_s
431         #define my_sprintf_s sprintf_s
432         #define my_stprintf_s _stprintf_s
433         #define my_vsprintf_s vsprintf_s
434         #define my_vstprintf_s _vstprintf_s
435 #endif
436
437 // win32 aoi
438 #ifndef _WIN32
439         BOOL MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName);
440         DWORD MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPCTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName);
441         UINT MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName);
442         // used only in winmain and win32 osd class
443 //      #define ZeroMemory(p,s) memset(p,0x00,s)
444 //      #define CopyMemory(t,f,s) memcpy(t,f,s)
445 #else
446         #define MyWritePrivateProfileString WritePrivateProfileString
447         #define MyGetPrivateProfileString GetPrivateProfileString
448         #define MyGetPrivateProfileInt GetPrivateProfileInt
449 #endif
450
451
452 // rgb color
453 #define _RGB888
454
455 #if defined(_RGB555) || defined(_RGB565)
456         typedef uint16 scrntype;
457         scrntype RGB_COLOR(uint r, uint g, uint b);
458         scrntype RGBA_COLOR(uint r, uint g, uint b, uint a);
459         uint8 R_OF_COLOR(scrntype c);
460         uint8 G_OF_COLOR(scrntype c);
461         uint8 B_OF_COLOR(scrntype c);
462         uint8 A_OF_COLOR(scrntype c);
463 #elif defined(__BIG_ENDIAN__)
464 # if defined(_RGB888) 
465         typedef uint32 scrntype;
466         #define RGB_COLOR(r, g, b)      (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8))
467         #define RGBA_COLOR(r, g, b, a)  (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8) | ((uint32)(a) << 0))   
468         #define R_OF_COLOR(c)           (((c) >> 24) & 0xff)
469         #define G_OF_COLOR(c)           (((c) >> 16) & 0xff)
470         #define B_OF_COLOR(c)           (((c) >> 8 ) & 0xff)
471         #define A_OF_COLOR(c)           (((c) >> 0 ) & 0xff)
472 # endif
473
474 #else // LITTLE ENDIAN
475
476 # if defined(_RGB888)
477         typedef uint32 scrntype;
478         #define RGB_COLOR(r, g, b)      (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0) | (uint32)0xff << 24)
479         #define RGBA_COLOR(r, g, b, a)  (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0) | ((uint32)(a) << 24))   
480         #define R_OF_COLOR(c)           (((c) >> 24) & 0xff)
481         #define G_OF_COLOR(c)           (((c) >> 16) & 0xff)
482         #define B_OF_COLOR(c)           (((c) >> 8 ) & 0xff)
483         #define A_OF_COLOR(c)           (((c) >> 0 ) & 0xff)
484 # endif
485
486 #endif  // ENDIAN
487
488
489 // string
490 #if defined(__GNUC__) || defined(__CYGWIN__) || defined(Q_OS_CYGWIN)
491         #define stricmp(a,b) strcasecmp(a,b)
492         #define strnicmp(a,b,n) strncasecmp(a,b,n)
493         #define _strnicmp(a,b,n) strnicmp(a,b,n)
494 #endif
495
496 // _TCHAR
497 #ifndef SUPPORT_TCHAR_TYPE
498         typedef char _TCHAR;
499 //#define _T(s) (s)
500         #define _tfopen fopen
501         #define _tcscmp strcmp
502         #define _tcscpy strcpy
503         #define _tcsicmp stricmp
504         #define _tcslen strlen
505         #define _tcsncat strncat
506         #define _tcsncpy strncpy
507         #define _tcsncicmp strnicmp
508         #define _tcsstr strstr
509         #define _tcstok strtok
510         #define _tcstol strtol
511         #define _tcstoul strtoul
512         #define _stprintf sprintf
513         #define _vstprintf vsprintf
514 #endif
515
516 #if !defined(SUPPORT_SECURE_FUNCTIONS) || defined(CSP_OS_GCC_WINDOWS) || defined(CSP_OS_GCC_CYGWIN)
517 # ifndef errno_t
518 typedef int errno_t;
519 # endif
520 #  define _strcpy_s _tcscpy_s
521 # if defined(CSP_OS_GCC_WINDOWS) || defined(CSP_OS_GCC_CYGWIN)
522 #  define strcpy_s _tcscpy_s
523 #  define tcscpy_s _tcscpy_s
524 #  define tcstok_s _tcstok_s
525 #  define stprintf_s _stprintf_s
526 #  define vstprintf_s _vstprintf_s
527 #  define vsprintf_s _vsprintf_s
528 # endif
529 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
530 _TCHAR *_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
531 int _stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
532 static inline int _vsprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...)
533 {
534         va_list ap;
535         va_start(ap, format);
536         int result = vsprintf(buffer, format, ap);
537         va_end(ap);
538         return result;
539 }
540 int _vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
541 #else
542 # define _strcpy_s _tcscpy_s
543 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
544 #endif
545
546 // secture functions
547
548 // wav file header
549 #pragma pack(1)
550 typedef struct {
551         char id[4];
552         uint32 size;
553 } wav_chunk_t;
554 #pragma pack()
555
556 #pragma pack(1)
557 typedef struct {
558         wav_chunk_t riff_chunk;
559         char wave[4];
560         wav_chunk_t fmt_chunk;
561         uint16 format_id;
562         uint16 channels;
563         uint32 sample_rate;
564         uint32 data_speed;
565         uint16 block_size;
566         uint16 sample_bits;
567 } wav_header_t;
568 #pragma pack()
569
570 // misc
571 const _TCHAR *application_path();
572 const _TCHAR *create_local_path(const _TCHAR* format, ...);
573 void create_local_path(_TCHAR *file_path, int length, const _TCHAR* format, ...);
574 const _TCHAR *create_date_file_path(const _TCHAR *extension);
575 void create_date_file_path(_TCHAR *file_path, int length, const _TCHAR *extension);
576 bool check_file_extension(const _TCHAR* file_path, const _TCHAR* ext);
577 _TCHAR *get_file_path_without_extensiton(const _TCHAR* file_path);
578
579 uint32 getcrc32(uint8 data[], int size);
580
581
582 #define array_length(array) (sizeof(array) / sizeof(array[0]))
583
584 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
585 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
586 #define TO_BCD_LO(v)    ((v) % 10)
587 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)
588
589 #define LEAP_YEAR(y)    (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
590
591 typedef struct cur_time_s {
592         int year, month, day, day_of_week, hour, minute, second;
593         bool initialized;
594         cur_time_s()
595         {
596                 initialized = false;
597         }
598         void increment();
599         void update_year();
600         void update_day_of_week();
601         void save_state(void *f);
602         bool load_state(void *f);
603 } cur_time_t;
604
605 void get_host_time(cur_time_t* cur_time);
606
607 #endif