2013-07-06 04:37:13 +00:00
|
|
|
/*
|
2015-10-22 05:13:26 +00:00
|
|
|
* Apple // emulator for *ix
|
|
|
|
*
|
|
|
|
* This software package is subject to the GNU General Public License
|
|
|
|
* version 3 or later (your choice) as published by the Free Software
|
|
|
|
* Foundation.
|
2013-06-11 07:08:15 +00:00
|
|
|
*
|
|
|
|
* Copyright 1994 Alexander Jean-Claude Bottema
|
|
|
|
* Copyright 1995 Stephen Lee
|
|
|
|
* Copyright 1997, 1998 Aaron Culliney
|
|
|
|
* Copyright 1998, 1999, 2000 Michael Deutschmann
|
2015-10-22 05:13:26 +00:00
|
|
|
* Copyright 2013-2015 Aaron Culliney
|
2013-06-11 07:08:15 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-01-23 04:42:34 +00:00
|
|
|
#include "common.h"
|
2013-06-11 07:08:15 +00:00
|
|
|
|
2014-01-26 06:10:33 +00:00
|
|
|
bool do_logging = true; // also controlled by NDEBUG
|
|
|
|
FILE *error_log = NULL;
|
2015-02-18 00:16:34 +00:00
|
|
|
int sound_volume = 2;
|
|
|
|
color_mode_t color_mode = COLOR;
|
2015-02-23 19:19:41 +00:00
|
|
|
const char *data_dir = NULL;
|
2015-09-11 07:00:04 +00:00
|
|
|
char **argv = NULL;
|
|
|
|
int argc = 0;
|
2015-09-26 22:20:54 +00:00
|
|
|
CrashHandler_s *crashHandler = NULL;
|
2015-02-18 00:16:34 +00:00
|
|
|
|
2015-04-18 05:12:13 +00:00
|
|
|
__attribute__((constructor(CTOR_PRIORITY_FIRST)))
|
2015-04-18 04:40:56 +00:00
|
|
|
static void _init_common() {
|
2014-06-14 18:17:34 +00:00
|
|
|
error_log = stderr;
|
2015-02-23 19:19:41 +00:00
|
|
|
#if defined(CONFIG_DATADIR)
|
2015-06-02 05:25:29 +00:00
|
|
|
data_dir = strdup(CONFIG_DATADIR PATH_SEPARATOR PACKAGE_NAME);
|
2015-02-23 19:19:41 +00:00
|
|
|
#elif defined(ANDROID)
|
2015-09-11 07:00:04 +00:00
|
|
|
// data_dir is set up in JNI
|
2015-03-10 03:44:31 +00:00
|
|
|
#elif !defined(__APPLE__)
|
2015-09-11 07:00:04 +00:00
|
|
|
# error "Specify a CONFIG_DATADIR and PACKAGE_NAME"
|
2014-01-23 04:42:34 +00:00
|
|
|
#endif
|
2013-06-11 07:08:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 19:59:26 +00:00
|
|
|
static void _shutdown_threads(void) {
|
2015-09-11 07:00:04 +00:00
|
|
|
#if !TESTING
|
2015-09-12 22:33:22 +00:00
|
|
|
# if defined(__linux__) && !defined(ANDROID)
|
2015-05-31 19:59:26 +00:00
|
|
|
LOG("Emulator waiting for other threads to clean up...");
|
|
|
|
do {
|
|
|
|
DIR *dir = opendir("/proc/self/task");
|
|
|
|
if (!dir) {
|
|
|
|
ERRLOG("Cannot open /proc/self/task !");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int thread_count = 0;
|
|
|
|
struct dirent *d = NULL;
|
|
|
|
while ((d = readdir(dir)) != NULL) {
|
|
|
|
if (strncmp(".", d->d_name, 2) == 0) {
|
|
|
|
// ignore
|
|
|
|
} else if (strncmp("..", d->d_name, 3) == 0) {
|
|
|
|
// ignore
|
|
|
|
} else {
|
|
|
|
++thread_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
assert(thread_count >= 1 && "there must at least be one thread =P");
|
|
|
|
if (thread_count == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct timespec ts = { .tv_sec=0, .tv_nsec=33333333 };
|
|
|
|
nanosleep(&ts, NULL); // 30Hz framerate
|
|
|
|
} while (1);
|
2015-09-12 22:33:22 +00:00
|
|
|
# elif defined(__APPLE__)
|
|
|
|
# error TODO FIXME ... verify leaks-n-things with instruments on Darwin
|
2015-09-11 07:00:04 +00:00
|
|
|
# endif
|
2015-05-31 19:59:26 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-09-11 07:00:04 +00:00
|
|
|
void emulator_start(void) {
|
2015-02-16 02:33:35 +00:00
|
|
|
#ifdef INTERFACE_CLASSIC
|
2015-09-11 07:00:04 +00:00
|
|
|
load_settings(); // user prefs
|
2014-11-22 22:28:38 +00:00
|
|
|
c_keys_set_key(kF8); // show credits before emulation start
|
2015-02-16 02:33:35 +00:00
|
|
|
#endif
|
2015-09-13 21:24:17 +00:00
|
|
|
video_init();
|
2015-09-06 21:03:02 +00:00
|
|
|
timing_startCPU();
|
2014-09-07 03:04:32 +00:00
|
|
|
video_main_loop();
|
2015-09-11 07:00:04 +00:00
|
|
|
}
|
2015-05-31 19:59:26 +00:00
|
|
|
|
2015-09-11 07:00:04 +00:00
|
|
|
void emulator_shutdown(void) {
|
|
|
|
video_shutdown();
|
|
|
|
timing_stopCPU();
|
|
|
|
_shutdown_threads();
|
|
|
|
}
|
2015-05-31 19:59:26 +00:00
|
|
|
|
2015-09-11 07:00:04 +00:00
|
|
|
#if !TESTING && !defined(__APPLE__) && !defined(ANDROID)
|
|
|
|
int main(int _argc, char **_argv) {
|
|
|
|
argc = _argc;
|
|
|
|
argv = _argv;
|
2015-05-31 19:59:26 +00:00
|
|
|
|
2015-09-11 07:00:04 +00:00
|
|
|
emulator_start();
|
2015-09-12 22:33:22 +00:00
|
|
|
|
|
|
|
// main loop ...
|
|
|
|
|
2015-09-11 07:00:04 +00:00
|
|
|
emulator_shutdown();
|
2015-05-31 19:59:26 +00:00
|
|
|
|
2015-09-12 22:33:22 +00:00
|
|
|
LOG("Emulator exit ...");
|
|
|
|
|
2015-05-31 19:59:26 +00:00
|
|
|
return 0;
|
2013-06-11 07:08:15 +00:00
|
|
|
}
|
2015-02-18 04:28:23 +00:00
|
|
|
#endif
|
2014-01-25 22:13:38 +00:00
|
|
|
|