2013-07-07 14:01:21 -07:00
|
|
|
/*
|
2013-10-05 23:13:24 -07:00
|
|
|
* Apple // emulator for *nix
|
2013-07-07 14:01:21 -07:00
|
|
|
*
|
|
|
|
* This software package is subject to the GNU General Public License
|
|
|
|
* version 2 or later (your choice) as published by the Free Software
|
|
|
|
* Foundation.
|
|
|
|
*
|
|
|
|
* THERE ARE NO WARRANTIES WHATSOEVER.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _COMMON_H_
|
|
|
|
#define _COMMON_H_
|
|
|
|
|
2013-10-22 21:18:03 -07:00
|
|
|
#if defined(__GNUC__) && !defined(_GNU_SOURCE)
|
2013-10-06 21:01:00 -07:00
|
|
|
# define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2014-01-22 20:42:34 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-07 14:01:21 -07:00
|
|
|
#include <stdlib.h>
|
2014-01-22 20:42:34 -08:00
|
|
|
#include <stdio.h>
|
2013-07-07 14:01:21 -07:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2013-10-05 23:13:24 -07:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <pthread.h>
|
2013-10-06 21:01:00 -07:00
|
|
|
#include <ctype.h>
|
2013-11-30 20:00:34 -08:00
|
|
|
#include <dirent.h>
|
2014-01-22 20:42:34 -08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include <zlib.h>
|
2013-07-07 14:01:21 -07:00
|
|
|
|
2013-11-24 12:19:54 -08:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2013-11-30 20:00:34 -08:00
|
|
|
#include <sys/stat.h>
|
2013-11-24 12:19:54 -08:00
|
|
|
|
2014-01-22 20:42:34 -08:00
|
|
|
#include "misc.h"
|
|
|
|
#include "timing.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "video/video.h"
|
|
|
|
#include "disk.h"
|
|
|
|
#include "interface.h"
|
|
|
|
#include "keys.h"
|
|
|
|
#include "joystick.h"
|
|
|
|
#include "glue.h"
|
|
|
|
#include "prefs.h"
|
|
|
|
#include "uthash.h"
|
|
|
|
#include "zlib-helpers.h"
|
|
|
|
|
2015-01-16 21:10:45 -08:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include "darwin-shim.h"
|
|
|
|
#import <CoreFoundation/CoreFoundation.h>
|
|
|
|
#endif
|
2014-09-16 20:49:57 -07:00
|
|
|
|
|
|
|
#if VIDEO_OPENGL
|
2014-10-07 21:26:50 -07:00
|
|
|
#include "video_util/glUtil.h"
|
2014-11-29 13:31:21 -08:00
|
|
|
#define CRASH_APP_ON_LOAD_BECAUSE_YAY_GJ_APPLE 0
|
|
|
|
// 2014/11/29 -- Yay GJ Apple! ... you would think that early app lifecycle calls to glGetError() would not segfault on Macs
|
|
|
|
extern bool safe_to_do_opengl_logging;
|
|
|
|
static inline GLenum safeGLGetError(void) {
|
|
|
|
if (safe_to_do_opengl_logging) {
|
|
|
|
return glGetError();
|
|
|
|
}
|
|
|
|
return (GLenum)0;
|
|
|
|
}
|
2014-09-16 20:49:57 -07:00
|
|
|
#else
|
2014-09-27 11:03:51 -07:00
|
|
|
#define GLenum int
|
2014-12-05 22:31:12 -08:00
|
|
|
#define safeGLGetError() 0
|
2014-09-16 20:49:57 -07:00
|
|
|
#define glGetError() 0
|
|
|
|
#endif
|
|
|
|
|
2014-01-22 20:42:34 -08:00
|
|
|
#ifdef DEBUGGER
|
|
|
|
#include "meta/debug.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AUDIO_ENABLED
|
|
|
|
#include "audio/soundcore.h"
|
|
|
|
#include "audio/speaker.h"
|
|
|
|
#include "audio/mockingboard.h"
|
|
|
|
#endif
|
|
|
|
|
2014-03-30 10:39:45 -07:00
|
|
|
#if !defined(MIN)
|
|
|
|
#define MIN(a,b) (((a) <= (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(MAX)
|
|
|
|
#define MAX(a,b) (((a) >= (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
2014-01-25 22:10:33 -08:00
|
|
|
extern bool do_logging;
|
2014-06-14 11:17:34 -07:00
|
|
|
|
2015-02-15 18:35:32 -08:00
|
|
|
#ifdef ANDROID
|
|
|
|
static const char *log_end = "";
|
|
|
|
# include <android/log.h>
|
2015-02-15 20:08:01 -08:00
|
|
|
# warning TODO ... FOSS crash reporting?
|
|
|
|
# define QUIT_FUNCTION(x) exit(x)
|
2015-02-15 18:35:32 -08:00
|
|
|
# define _LOG_CMD(str) __android_log_print(ANDROID_LOG_ERROR, "apple2ix", "%s", str)
|
|
|
|
#else
|
|
|
|
extern FILE *error_log;
|
|
|
|
static const char *log_end = "\n";
|
|
|
|
# define QUIT_FUNCTION(x) exit(x)
|
|
|
|
# define _LOG_CMD(str) fprintf(error_log, "%s", str)
|
|
|
|
#endif
|
2014-09-16 20:49:57 -07:00
|
|
|
|
2014-06-14 11:17:34 -07:00
|
|
|
#define _LOG(...) \
|
2015-02-15 18:35:32 -08:00
|
|
|
do { \
|
|
|
|
int _err = errno; \
|
|
|
|
errno = 0; \
|
|
|
|
\
|
|
|
|
char *syserr_str = NULL; \
|
|
|
|
char *glerr_str = NULL; \
|
|
|
|
if (_err) { \
|
|
|
|
asprintf(&syserr_str, " (syserr:%s)", strerror(_err)); \
|
|
|
|
} \
|
|
|
|
if (_glerr) { \
|
|
|
|
asprintf(&glerr_str, " (glerr:%04X)", _glerr); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
char *buf0 = NULL; \
|
|
|
|
asprintf(&buf0, __VA_ARGS__); \
|
|
|
|
\
|
|
|
|
char *buf = NULL; \
|
|
|
|
asprintf(&buf, "%s:%d -%s%s %s%s", __FILE__, __LINE__, syserr_str ? : "", glerr_str ? : "", buf0, log_end); \
|
|
|
|
\
|
|
|
|
_LOG_CMD(buf); \
|
|
|
|
\
|
|
|
|
free(buf0); \
|
|
|
|
free(buf); \
|
|
|
|
if (syserr_str) { \
|
|
|
|
free(syserr_str); \
|
|
|
|
} \
|
|
|
|
if (glerr_str) { \
|
|
|
|
free(glerr_str); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2014-06-14 11:17:34 -07:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
2014-09-16 20:49:57 -07:00
|
|
|
#define LOG(...) \
|
2014-06-14 11:17:34 -07:00
|
|
|
if (do_logging) { \
|
2014-09-16 20:49:57 -07:00
|
|
|
errno = 0; \
|
2014-09-27 11:03:51 -07:00
|
|
|
GLenum _glerr = 0; \
|
2014-06-14 11:17:34 -07:00
|
|
|
_LOG(__VA_ARGS__); \
|
2014-10-25 11:38:28 -07:00
|
|
|
} //
|
2013-07-07 14:01:21 -07:00
|
|
|
|
2014-09-16 20:49:57 -07:00
|
|
|
#define ERRLOG(...) \
|
2014-01-25 22:10:33 -08:00
|
|
|
if (do_logging) { \
|
2014-11-29 13:31:21 -08:00
|
|
|
GLenum _glerr = safeGLGetError(); \
|
2014-06-14 11:17:34 -07:00
|
|
|
_LOG(__VA_ARGS__); \
|
2014-11-29 13:31:21 -08:00
|
|
|
while ( (_glerr = safeGLGetError()) ) { \
|
2014-09-27 11:03:51 -07:00
|
|
|
_LOG(__VA_ARGS__); \
|
|
|
|
} \
|
2014-10-25 11:38:28 -07:00
|
|
|
} //
|
2014-06-14 11:17:34 -07:00
|
|
|
|
2014-09-16 20:49:57 -07:00
|
|
|
#define GL_ERRLOG(...) \
|
2014-06-14 11:17:34 -07:00
|
|
|
if (do_logging) { \
|
2014-09-27 11:03:51 -07:00
|
|
|
GLenum _glerr = 0; \
|
2014-11-29 13:31:21 -08:00
|
|
|
while ( (_glerr = safeGLGetError()) ) { \
|
2014-09-16 20:49:57 -07:00
|
|
|
_LOG(__VA_ARGS__); \
|
|
|
|
} \
|
2014-10-25 11:38:28 -07:00
|
|
|
} //
|
2013-09-12 20:47:00 -07:00
|
|
|
|
2014-09-16 20:49:57 -07:00
|
|
|
#define ERRQUIT(...) \
|
|
|
|
do { \
|
2014-11-29 13:31:21 -08:00
|
|
|
GLenum _glerr = safeGLGetError(); \
|
2014-09-16 20:49:57 -07:00
|
|
|
_LOG(__VA_ARGS__); \
|
2014-11-29 13:31:21 -08:00
|
|
|
while ( (_glerr = safeGLGetError()) ) { \
|
2014-09-27 11:03:51 -07:00
|
|
|
_LOG(__VA_ARGS__); \
|
|
|
|
} \
|
2014-09-16 20:49:57 -07:00
|
|
|
QUIT_FUNCTION(1); \
|
2014-10-25 11:38:28 -07:00
|
|
|
} while (0)
|
2014-09-16 20:49:57 -07:00
|
|
|
|
|
|
|
#define GL_ERRQUIT(...) \
|
|
|
|
do { \
|
2014-09-27 11:03:51 -07:00
|
|
|
GLenum _glerr = 0; \
|
2014-11-29 13:31:21 -08:00
|
|
|
while ( (_glerr = safeGLGetError()) ) { \
|
2014-09-27 11:03:51 -07:00
|
|
|
_LOG(__VA_ARGS__); \
|
2014-09-16 20:49:57 -07:00
|
|
|
QUIT_FUNCTION(_glerr); \
|
|
|
|
} \
|
2014-10-25 11:38:28 -07:00
|
|
|
} while (0)
|
2014-09-16 20:49:57 -07:00
|
|
|
|
2013-07-07 14:01:21 -07:00
|
|
|
#else // NDEBUG
|
|
|
|
|
2013-10-05 23:13:24 -07:00
|
|
|
#define ERRLOG(...) \
|
2014-10-25 11:38:28 -07:00
|
|
|
do { } while (0)
|
2013-07-07 14:01:21 -07:00
|
|
|
|
2014-06-14 11:17:34 -07:00
|
|
|
#define ERRQUIT(...) \
|
2014-10-25 11:38:28 -07:00
|
|
|
do { } while (0)
|
2013-07-07 14:01:21 -07:00
|
|
|
|
2013-10-05 23:13:24 -07:00
|
|
|
#define LOG(...) \
|
2014-10-25 11:38:28 -07:00
|
|
|
do { } while (0)
|
|
|
|
|
|
|
|
#define GL_ERRLOG(...) \
|
|
|
|
do { } while (0)
|
|
|
|
|
|
|
|
#define GL_ERRQUIT(...) \
|
|
|
|
do { } while (0)
|
2014-06-14 11:17:34 -07:00
|
|
|
|
2014-09-16 20:49:57 -07:00
|
|
|
#endif // NDEBUG
|
2014-06-14 11:17:34 -07:00
|
|
|
|
|
|
|
#define RELEASE_ERRLOG(...) \
|
|
|
|
do { \
|
2014-10-25 11:38:28 -07:00
|
|
|
GLenum _glerr = 0; \
|
2014-06-14 11:17:34 -07:00
|
|
|
_LOG(__VA_ARGS__); \
|
2014-10-25 11:38:28 -07:00
|
|
|
} while (0)
|
2014-06-14 11:17:34 -07:00
|
|
|
|
|
|
|
#define RELEASE_LOG(...) \
|
2013-10-05 23:13:24 -07:00
|
|
|
do { \
|
2014-11-29 13:31:21 -08:00
|
|
|
GLenum _glerr = safeGLGetError(); \
|
2013-10-05 23:13:24 -07:00
|
|
|
errno = 0; \
|
2014-06-14 11:17:34 -07:00
|
|
|
_LOG(__VA_ARGS__); \
|
2014-10-25 11:38:28 -07:00
|
|
|
} while (0)
|
2013-07-07 14:01:21 -07:00
|
|
|
|
2014-09-06 19:47:59 -07:00
|
|
|
#define FREE(x) \
|
2013-10-05 23:13:24 -07:00
|
|
|
do { \
|
2014-09-06 19:47:59 -07:00
|
|
|
free((x)); \
|
|
|
|
(x) = NULL; \
|
2014-10-25 11:38:28 -07:00
|
|
|
} while (0)
|
2013-07-07 14:01:21 -07:00
|
|
|
|
2014-09-27 11:03:51 -07:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#define CFRELEASE(x) \
|
|
|
|
do { \
|
|
|
|
CFRelease((x)); \
|
|
|
|
(x) = NULL; \
|
2014-10-25 11:38:28 -07:00
|
|
|
} while (0)
|
2014-09-27 11:03:51 -07:00
|
|
|
#endif
|
|
|
|
|
2015-01-23 23:34:03 -08:00
|
|
|
// branch prediction
|
|
|
|
#define LIKELY(x) __builtin_expect((x), true)
|
|
|
|
#define UNLIKELY(x) __builtin_expect((x), false)
|
|
|
|
|
2013-07-07 14:01:21 -07:00
|
|
|
#endif // whole file
|