OSDN Git Service

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