OSDN Git Service

[UI][Qt] Fix FTBFSs basically, now enable to work for FM77AV40EX.
[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 #ifdef SUPPORT_TCHAR_TYPE
20  #include <tchar.h>
21 #endif
22 #include <stdio.h>
23 #include <errno.h>
24
25 #if defined(_MSC_VER)
26 #include <windows.h>
27 #include <windowsx.h>
28 #include <mmsystem.h>
29 #include <process.h>
30 # define __CSP_COMPILER_MS_C
31 #endif
32
33 #if defined(_USE_QT)
34 # if defined(_USE_QT5)
35 #  include <QString>
36 #  include <QFile>
37 #  include <QtEndian>
38 # else
39 #  include <QtCore/QString>
40 #  include <QtCore/QFile>
41 # endif
42 #endif
43
44
45 #if defined(__GNUC__)
46 #include "common_gcc.h"
47 #elif defined(_MSC_VER)
48 #include "common_msc.h"
49 #endif
50 // variable scope of 'for' loop for Microsoft Visual C++ 6.0
51 #if defined(_MSC_VER) && (_MSC_VER == 1200)
52 #define for if(0);else for
53 #endif
54
55 // disable warnings  for microsoft visual c++ 2005 or later
56 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
57 #pragma warning( disable : 4819 )
58 //#pragma warning( disable : 4995 )
59 #pragma warning( disable : 4996 )
60 #endif
61
62 // endian
63 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
64         #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
65                 #if __BYTE_ORDER == __LITTLE_ENDIAN
66                         #define __LITTLE_ENDIAN__
67                 #elif __BYTE_ORDER == __BIG_ENDIAN
68                         #define __BIG_ENDIAN__
69                 #endif
70         #elif defined(SDL_BYTEORDER) && (defined(SDL_LIL_ENDIAN) || defined(SDL_BIG_ENDIAN))
71                 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
72                         #define __LITTLE_ENDIAN__
73                 #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
74                         #define __BIG_ENDIAN__
75                 #endif
76         #elif defined(WORDS_LITTLEENDIAN)
77                 #define __LITTLE_ENDIAN__
78         #elif defined(WORDS_BIGENDIAN)
79                 #define __BIG_ENDIAN__
80         #endif
81 #endif
82 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
83         // Microsoft Visual C++
84         #define __LITTLE_ENDIAN__
85 #endif
86
87
88 // type definition
89 #ifndef uint8
90 typedef unsigned char uint8;
91 #endif
92 #ifndef uint16
93 typedef unsigned short uint16;
94 #endif
95 #ifndef uint32
96 typedef unsigned int uint32;
97 #endif
98 #ifndef uint64
99 #ifdef _MSC_VER
100 typedef unsigned __int64 uint64;
101 #else
102 //typedef unsigned long long uint64;
103 #endif
104 #endif
105 #ifndef int8
106 typedef signed char int8;
107 #endif
108 #ifndef int16
109 typedef signed short int16;
110 #endif
111 #ifndef int32
112 typedef signed int int32;
113 #endif
114 #ifndef int64
115 #ifdef _MSC_VER
116 typedef signed __int64 int64;
117 #else
118 //typedef signed long long int64;
119 #endif
120 #endif
121
122 #if defined(__LITTLE_ENDIAN__)
123 static inline uint32_t EndianToLittle_DWORD(uint32_t x)
124 {
125    return x;
126 }
127
128 static inline uint16_t EndianToLittle_WORD(uint16_t x)
129 {
130    return x;
131 }
132 #else // BIG_ENDIAN
133 static inline uint32_t EndianToLittle_DWORD(uint32_t x)
134 {
135    uint32_t y;
136    y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
137        ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);
138    return y;
139 }
140
141 static inline uint16_t EndianToLittle_WORD(uint16_t x)
142 {
143    uint16_t y;
144    y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
145    return y;
146 }
147 #endif
148
149 typedef union {
150         struct {
151 #ifdef __BIG_ENDIAN__
152                 uint8 h3, h2, h, l;
153 #else
154                 uint8 l, h, h2, h3;
155 #endif
156         } b;
157         struct {
158 #ifdef __BIG_ENDIAN__
159                 int8 h3, h2, h, l;
160 #else
161                 int8 l, h, h2, h3;
162 #endif
163         } sb;
164         struct {
165 #ifdef __BIG_ENDIAN__
166                 uint16 h, l;
167 #else
168                 uint16 l, h;
169 #endif
170         } w;
171         struct {
172 #ifdef __BIG_ENDIAN__
173                 int16 h, l;
174 #else
175                 int16 l, h;
176 #endif
177         } sw;
178         uint32 d;
179         int32 sd;
180         inline void read_2bytes_le_from(uint8 *t)
181         {
182                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
183         }
184         inline void write_2bytes_le_to(uint8 *t)
185         {
186                 t[0] = b.l; t[1] = b.h;
187         }
188         inline void read_2bytes_be_from(uint8 *t)
189         {
190                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
191         }
192         inline void write_2bytes_be_to(uint8 *t)
193         {
194                 t[0] = b.h; t[1] = b.l;
195         }
196         inline void read_4bytes_le_from(uint8 *t)
197         {
198                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
199         }
200         inline void write_4bytes_le_to(uint8 *t)
201         {
202                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
203         }
204         inline void read_4bytes_be_from(uint8 *t)
205         {
206                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
207         }
208         inline void write_4bytes_be_to(uint8 *t)
209         {
210                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
211         }
212
213 } pair;
214
215 // max/min from WinDef.h
216 #ifndef max
217         #define MAX_MACRO_NOT_DEFINED
218         int max(int a, int b);
219         unsigned int max(unsigned int a, unsigned int b);
220 #endif
221 #ifndef min
222         #define MIN_MACRO_NOT_DEFINED
223         int min(int a, int b);
224         unsigned int min(unsigned int a, unsigned int b);
225 #endif
226
227 // _TCHAR
228 #ifndef SUPPORT_TCHAR_TYPE
229         #ifndef _tfopen
230                 #define _tfopen fopen
231         #endif
232         #ifndef _tcscmp
233                 #define _tcscmp strcmp
234         #endif
235         #ifndef _tcscpy
236                 #define _tcscpy strcpy
237         #endif
238         #ifndef _tcsicmp
239                 #define _tcsicmp stricmp
240         #endif
241         #ifndef _tcslen
242                 #define _tcslen strlen
243         #endif
244         #ifndef _tcsncat
245                 #define _tcsncat strncat
246         #endif
247         #ifndef _tcsncpy
248                 #define _tcsncpy strncpy
249         #endif
250         #ifndef _tcsncicmp
251                 #define _tcsncicmp strnicmp
252         #endif
253         #ifndef _tcsstr
254                 #define _tcsstr strstr
255         #endif
256         #ifndef _tcstok
257                 #define _tcstok strtok
258         #endif
259         #ifndef _tcstol
260                 #define _tcstol strtol
261         #endif
262         #ifndef _tcstoul
263                 #define _tcstoul strtoul
264         #endif
265         #ifndef _stprintf
266                 #define _stprintf sprintf
267         #endif
268         #ifndef _vstprintf
269                 #define _vstprintf vsprintf
270         #endif
271         #ifndef _taccess
272                 #define _taccess access
273         #endif
274         #ifndef _tremove
275                 #define _tremove remove
276         #endif
277         #define __T(x) x
278         #define _T(x) __T(x)
279         #define _TEXT(x) __T(x)
280 #endif
281
282 // secture functions
283 #ifndef SUPPORT_SECURE_FUNCTIONS
284         #ifndef errno_t
285                 typedef int errno_t;
286         #endif
287 //      errno_t my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode);
288         errno_t my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
289         errno_t my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
290         _TCHAR *my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
291         int my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
292         int my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
293 #else
294 //      #define my_tfopen_s _tfopen_s
295         #define my_strcpy_s strcpy_s
296         #define my_tcscpy_s _tcscpy_s
297         #define my_tcstok_s _tcstok_s
298         #define my_stprintf_s _stprintf_s
299         #define my_vstprintf_s _vstprintf_s
300 #endif
301
302 #if!defined(_MSC_VER) && !defined(CSP_OS_WINDOWS)
303         BOOL MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName);
304         DWORD MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPCTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName);
305         UINT MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName);
306 #else
307         #define MyWritePrivateProfileString WritePrivateProfileString
308         #define MyGetPrivateProfileString GetPrivateProfileString
309         #define MyGetPrivateProfileInt GetPrivateProfileInt
310 #endif
311
312 // rgb color
313 #define _RGB888
314
315 #if defined(_RGB555) || defined(_RGB565)
316         typedef uint16 scrntype;
317         scrntype RGB_COLOR(uint r, uint g, uint b);
318         scrntype RGBA_COLOR(uint r, uint g, uint b, uint a);
319         uint8 R_OF_COLOR(scrntype c);
320         uint8 G_OF_COLOR(scrntype c);
321         uint8 B_OF_COLOR(scrntype c);
322         uint8 A_OF_COLOR(scrntype c);
323 #elif defined(__BIG_ENDIAN__)
324 # if defined(_RGB888) 
325         typedef uint32 scrntype;
326         #define RGB_COLOR(r, g, b)      (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8))
327         #define RGBA_COLOR(r, g, b, a)  (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8) | ((uint32)(a) << 0))   
328         #define R_OF_COLOR(c)           (((c) >> 24) & 0xff)
329         #define G_OF_COLOR(c)           (((c) >> 16) & 0xff)
330         #define B_OF_COLOR(c)           (((c) >> 8 ) & 0xff)
331         #define A_OF_COLOR(c)           (((c) >> 0 ) & 0xff)
332 # endif
333
334 #else // LITTLE ENDIAN
335
336 # if defined(_RGB888)
337         typedef uint32 scrntype;
338         #define RGB_COLOR(r, g, b)      (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0) | (uint32)0xff << 24)
339         #define RGBA_COLOR(r, g, b, a)  (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0) | ((uint32)(a) << 24))   
340         #define R_OF_COLOR(c)           (((c) >> 24) & 0xff)
341         #define G_OF_COLOR(c)           (((c) >> 16) & 0xff)
342         #define B_OF_COLOR(c)           (((c) >> 8 ) & 0xff)
343         #define A_OF_COLOR(c)           (((c) >> 0 ) & 0xff)
344 # endif
345
346 #endif  // ENDIAN
347
348
349 // _TCHAR
350 #ifndef SUPPORT_TCHAR_TYPE
351         typedef char _TCHAR;
352 //#define _T(s) (s)
353         #define _tfopen fopen
354         #define _tcscmp strcmp
355         #define _tcscpy strcpy
356         #define _tcsicmp stricmp
357         #define _tcslen strlen
358         #define _tcsncat strncat
359         #define _tcsncpy strncpy
360         #define _tcsncicmp strnicmp
361         #define _tcsstr strstr
362         #define _tcstok strtok
363         #define _tcstol strtol
364         #define _tcstoul strtoul
365         #define _stprintf sprintf
366         #define _vstprintf vsprintf
367 #endif
368
369 #if !defined(SUPPORT_SECURE_FUNCTIONS) || defined(CSP_OS_GCC_WINDOWS) || defined(CSP_OS_GCC_CYGWIN)
370 # ifndef errno_t
371 typedef int errno_t;
372 # endif
373 #  define _strcpy_s _tcscpy_s
374 # if defined(CSP_OS_GCC_WINDOWS) || defined(CSP_OS_GCC_CYGWIN)
375 #  define strcpy_s _tcscpy_s
376 #  define tcscpy_s _tcscpy_s
377 #  define tcstok_s _tcstok_s
378 #  define stprintf_s _stprintf_s
379 #  define vstprintf_s _vstprintf_s
380 #  define vsprintf_s _vsprintf_s
381 # endif
382 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
383 _TCHAR *_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
384 int _stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
385 static inline int _vsprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...)
386 {
387         va_list ap;
388         va_start(ap, format);
389         int result = vsprintf(buffer, format, ap);
390         va_end(ap);
391         return result;
392 }
393 int _vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
394 #else
395 # define _strcpy_s _tcscpy_s
396 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
397 #endif
398
399 // secture functions
400
401 // wav file header
402 #pragma pack(1)
403 typedef struct {
404         char id[4];
405         uint32 size;
406 } wav_chunk_t;
407 #pragma pack()
408
409 #pragma pack(1)
410 typedef struct {
411         wav_chunk_t riff_chunk;
412         char wave[4];
413         wav_chunk_t fmt_chunk;
414         uint16 format_id;
415         uint16 channels;
416         uint32 sample_rate;
417         uint32 data_speed;
418         uint16 block_size;
419         uint16 sample_bits;
420 } wav_header_t;
421 #pragma pack()
422
423 // misc
424 #ifdef __cplusplus
425 bool check_file_extension(const _TCHAR* file_path, const _TCHAR* ext);
426 _TCHAR *get_file_path_without_extensiton(const _TCHAR* file_path);
427 uint32 getcrc32(uint8 data[], int size);
428
429
430 #define array_length(array) (sizeof(array) / sizeof(array[0]))
431
432 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
433 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
434 #define TO_BCD_LO(v)    ((v) % 10)
435 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)
436
437 #define LEAP_YEAR(y)    (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
438
439 typedef struct cur_time_s {
440         int year, month, day, day_of_week, hour, minute, second;
441         bool initialized;
442         cur_time_s()
443         {
444                 initialized = false;
445         }
446         void increment();
447         void update_year();
448         void update_day_of_week();
449         void save_state(void *f);
450         bool load_state(void *f);
451 } cur_time_t;
452 #endif
453
454 #endif