OSDN Git Service

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