RetroArch/dynamic.c

268 lines
8.0 KiB
C
Raw Normal View History

2011-01-17 19:54:58 +00:00
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
2011-01-23 19:29:28 +00:00
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
2010-12-30 12:54:49 +00:00
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "dynamic.h"
#include "general.h"
#include "strl.h"
2010-12-30 12:54:49 +00:00
#include <string.h>
#include <assert.h>
2011-01-07 16:59:53 +00:00
#ifdef HAVE_CONFIG_H
2010-12-30 12:54:49 +00:00
#include "config.h"
2011-01-07 16:59:53 +00:00
#endif
#include <libsnes.hpp>
2010-12-30 12:54:49 +00:00
2011-01-19 12:25:18 +00:00
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
2011-03-07 18:12:14 +00:00
#endif
#ifdef HAVE_DYNAMIC
#define DLSYM(lib, x) dylib_proc(lib, #x)
#define SYM(type, x) do { \
p##x = (type)DLSYM(lib_handle, x); \
2010-12-30 12:54:49 +00:00
if (p##x == NULL) { SSNES_ERR("Failed to load symbol: \"%s\"\n", #x); exit(1); } \
2011-10-15 10:56:48 +00:00
} while (0)
2010-12-30 12:54:49 +00:00
#define OPT_SYM(type, x) do { \
p##x = (type)DLSYM(lib_handle, x); \
2011-10-15 10:56:48 +00:00
} while (0)
2011-03-17 00:25:44 +00:00
2011-03-07 18:12:14 +00:00
static dylib_t lib_handle = NULL;
2011-01-19 12:25:18 +00:00
#endif
2010-12-30 12:54:49 +00:00
void (*psnes_init)(void);
void (*psnes_set_video_refresh)(snes_video_refresh_t);
void (*psnes_set_audio_sample)(snes_audio_sample_t);
void (*psnes_set_input_poll)(snes_input_poll_t);
void (*psnes_set_input_state)(snes_input_state_t);
void (*psnes_reset)(void);
2010-12-30 12:54:49 +00:00
void (*psnes_run)(void);
2011-04-17 11:30:59 +00:00
void (*psnes_cheat_reset)(void);
void (*psnes_cheat_set)(unsigned, bool, const char*);
2011-03-17 00:25:44 +00:00
const char *(*psnes_library_id)(void) = NULL;
2010-12-30 12:54:49 +00:00
unsigned (*psnes_library_revision_minor)(void);
unsigned (*psnes_library_revision_major)(void);
bool (*psnes_load_cartridge_normal)(const char*, const uint8_t*, unsigned);
bool (*psnes_load_cartridge_super_game_boy)(
const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned);
bool (*psnes_load_cartridge_bsx)(
const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned);
bool (*psnes_load_cartridge_bsx_slotted)(
const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned);
bool (*psnes_load_cartridge_sufami_turbo)(
const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned);
2011-01-10 06:58:11 +00:00
void (*psnes_set_controller_port_device)(bool, unsigned);
2010-12-30 12:54:49 +00:00
bool (*psnes_get_region)(void);
2010-12-30 12:54:49 +00:00
unsigned (*psnes_serialize_size)(void);
bool (*psnes_serialize)(uint8_t*, unsigned);
bool (*psnes_unserialize)(const uint8_t*, unsigned);
void (*psnes_set_cartridge_basename)(const char*);
uint8_t* (*psnes_get_memory_data)(unsigned);
unsigned (*psnes_get_memory_size)(unsigned);
void (*psnes_unload_cartridge)(void);
void (*psnes_term)(void);
#ifdef HAVE_DYNAMIC
2010-12-30 12:54:49 +00:00
static void load_dynamic(void)
{
SSNES_LOG("Loading dynamic libsnes from: \"%s\"\n", g_settings.libsnes);
2011-03-07 18:12:14 +00:00
lib_handle = dylib_load(g_settings.libsnes);
2010-12-30 12:54:49 +00:00
if (!lib_handle)
{
SSNES_ERR("Failed to open dynamic library: \"%s\"\n", g_settings.libsnes);
exit(1);
}
SYM(void (*)(void), snes_init);
SYM(void (*)(snes_video_refresh_t), snes_set_video_refresh);
SYM(void (*)(snes_audio_sample_t), snes_set_audio_sample);
SYM(void (*)(snes_input_poll_t), snes_set_input_poll);
SYM(void (*)(snes_input_state_t), snes_set_input_state);
SYM(const char *(*)(void), snes_library_id);
SYM(unsigned (*)(void), snes_library_revision_minor);
SYM(unsigned (*)(void), snes_library_revision_major);
SYM(void (*)(void), snes_cheat_reset);
SYM(void (*)(unsigned, bool, const char*), snes_cheat_set);
SYM(void (*)(void), snes_reset);
SYM(void (*)(void), snes_run);
SYM(bool (*)(void), snes_get_region);
SYM(bool (*)(const char*, const uint8_t*, unsigned), snes_load_cartridge_normal);
SYM(bool (*)(const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned), snes_load_cartridge_super_game_boy);
SYM(bool (*)(const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned), snes_load_cartridge_bsx);
SYM(bool (*)(const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned), snes_load_cartridge_bsx_slotted);
SYM(bool (*)(const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned), snes_load_cartridge_sufami_turbo);
SYM(void (*)(bool, unsigned), snes_set_controller_port_device);
SYM(unsigned (*)(void), snes_serialize_size);
SYM(bool (*)(uint8_t*, unsigned), snes_serialize);
SYM(bool (*)(const uint8_t*, unsigned), snes_unserialize);
SYM(void (*)(const char*), snes_set_cartridge_basename);
SYM(uint8_t *(*)(unsigned), snes_get_memory_data);
SYM(unsigned (*)(unsigned), snes_get_memory_size);
SYM(void (*)(void), snes_unload_cartridge);
SYM(void (*)(void), snes_term);
2010-12-30 12:54:49 +00:00
}
#endif
#define SSYM(x) do { \
p##x = x; \
2011-10-15 10:56:48 +00:00
} while (0)
2010-12-30 12:54:49 +00:00
#ifndef HAVE_DYNAMIC
2010-12-30 12:54:49 +00:00
static void set_statics(void)
{
SSYM(snes_init);
SSYM(snes_set_video_refresh);
SSYM(snes_set_audio_sample);
SSYM(snes_set_input_poll);
SSYM(snes_set_input_state);
SSYM(snes_library_revision_minor);
SSYM(snes_library_revision_major);
SSYM(snes_library_id);
2011-04-17 11:30:59 +00:00
SSYM(snes_cheat_reset);
SSYM(snes_cheat_set);
SSYM(snes_reset);
2010-12-30 12:54:49 +00:00
SSYM(snes_run);
SSYM(snes_get_region);
2010-12-30 12:54:49 +00:00
SSYM(snes_load_cartridge_normal);
SSYM(snes_load_cartridge_super_game_boy);
SSYM(snes_load_cartridge_bsx);
SSYM(snes_load_cartridge_bsx_slotted);
SSYM(snes_load_cartridge_sufami_turbo);
2011-01-10 06:58:11 +00:00
SSYM(snes_set_controller_port_device);
2010-12-30 12:54:49 +00:00
SSYM(snes_serialize_size);
SSYM(snes_serialize);
SSYM(snes_unserialize);
SSYM(snes_set_cartridge_basename);
SSYM(snes_get_memory_data);
SSYM(snes_get_memory_size);
SSYM(snes_unload_cartridge);
SSYM(snes_term);
}
#endif
2010-12-30 12:54:49 +00:00
void init_dlsym(void)
{
// Guarantee that we can do "dirty" casting. Every OS that this program supports should pass this ...
assert(sizeof(void*) == sizeof(void (*)(void)));
#ifdef HAVE_DYNAMIC
#ifndef _WIN32 // Try to verify that -lsnes was not linked in from other modules
// since loading it dynamically and with -l will fail hard.
void *lib = dlopen(NULL, RTLD_LAZY);
if (lib)
{
void *sym = dlsym(lib, "snes_init");
if (sym)
{
SSNES_ERR("Serious problem! SSNES wants to load libsnes dyamically, but it is already linked!\n");
SSNES_ERR("This could happen if other modules SSNES depends on link against libsnes directly.\n");
SSNES_ERR("Proceeding could cause a crash! Aborting ...\n");
dlclose(lib);
exit(1);
}
dlclose(lib);
}
#endif
if (!*g_settings.libsnes)
{
#if defined(_WIN32)
strlcpy(g_settings.libsnes, "snes.dll", sizeof(g_settings.libsnes));
#elif defined(__APPLE__)
strlcpy(g_settings.libsnes, "libsnes.dylib", sizeof(g_settings.libsnes));
#else
strlcpy(g_settings.libsnes, "libsnes.so", sizeof(g_settings.libsnes));
#endif
}
load_dynamic();
#else
set_statics();
#endif
2010-12-30 12:54:49 +00:00
}
void uninit_dlsym(void)
{
#ifdef HAVE_DYNAMIC
2010-12-30 12:54:49 +00:00
if (lib_handle)
2011-03-07 18:12:14 +00:00
dylib_close(lib_handle);
#endif
}
// Platform independent dylib loading.
dylib_t dylib_load(const char *path)
{
2011-01-19 12:25:18 +00:00
#ifdef _WIN32
2011-03-07 18:12:14 +00:00
return LoadLibrary(path);
2011-01-19 12:25:18 +00:00
#else
2011-03-07 18:12:14 +00:00
return dlopen(path, RTLD_LAZY);
2011-01-19 12:25:18 +00:00
#endif
2011-03-07 18:12:14 +00:00
}
function_t dylib_proc(dylib_t lib, const char *proc)
2011-03-07 18:12:14 +00:00
{
#ifdef _WIN32
function_t sym = (function_t)GetProcAddress(lib, proc);
2011-03-07 18:12:14 +00:00
#else
// Dirty hack to workaround the non-legality of void* -> fn-pointer casts.
void *ptr_sym = dlsym(lib, proc);
function_t sym;
memcpy(&sym, &ptr_sym, sizeof(void*));
2011-03-07 18:12:14 +00:00
#endif
2011-03-07 18:56:40 +00:00
if (!sym)
SSNES_WARN("Failed to load symbol \"%s\"\n", proc);
return sym;
2011-03-07 18:12:14 +00:00
}
void dylib_close(dylib_t lib)
{
#ifdef _WIN32
FreeLibrary(lib);
#else
dlclose(lib);
2010-12-30 12:54:49 +00:00
#endif
}
2011-03-07 18:12:14 +00:00