OSDN Git Service

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