OSDN Git Service

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