OSDN Git Service

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