OSDN Git Service

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