RetroArch/dynamic.c

346 lines
9.1 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2012-01-08 00:08:18 +00:00
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
2010-12-30 12:54:49 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2010-12-30 12:54:49 +00:00
* 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.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2010-12-30 12:54:49 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2010-12-30 12:54:49 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "dynamic.h"
#include "general.h"
2012-03-16 22:26:57 +00:00
#include "compat/strl.h"
2010-12-30 12:54:49 +00:00
#include <string.h>
2011-01-07 16:59:53 +00:00
2012-04-21 21:25:32 +00:00
#ifdef RARCH_CONSOLE
2012-03-11 23:37:44 +00:00
#include "console/console_ext.h"
#endif
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
2011-12-24 12:46:12 +00:00
#include "boolean.h"
2012-04-07 09:55:37 +00:00
#include "libretro.h"
2010-12-30 12:54:49 +00:00
#ifdef NEED_DYNAMIC
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
2011-12-01 20:54:42 +00:00
#endif
2011-03-07 18:12:14 +00:00
2012-04-05 09:47:43 +00:00
#ifdef HAVE_DYNAMIC
#define SYM(x) do { \
2012-04-07 09:55:37 +00:00
function_t func = dylib_proc(lib_handle, #x); \
2012-04-05 09:47:43 +00:00
memcpy(&p##x, &func, sizeof(func)); \
2012-04-21 21:25:32 +00:00
if (p##x == NULL) { RARCH_ERR("Failed to load symbol: \"%s\"\n", #x); rarch_fail(1, "init_libretro_sym()"); } \
2011-10-15 10:56:48 +00:00
} while (0)
2010-12-30 12:54:49 +00:00
2011-03-07 18:12:14 +00:00
static dylib_t lib_handle = NULL;
2012-04-05 09:47:43 +00:00
#else
#define SYM(x) p##x = x
2011-01-19 12:25:18 +00:00
#endif
2010-12-30 12:54:49 +00:00
2012-04-05 09:47:43 +00:00
void (*pretro_init)(void);
void (*pretro_deinit)(void);
2010-12-30 12:54:49 +00:00
2012-04-05 09:47:43 +00:00
unsigned (*pretro_api_version)(void);
2010-12-30 12:54:49 +00:00
2012-04-05 09:47:43 +00:00
void (*pretro_get_system_info)(struct retro_system_info*);
void (*pretro_get_system_av_info)(struct retro_system_av_info*);
2010-12-30 12:54:49 +00:00
2012-04-05 09:47:43 +00:00
void (*pretro_set_environment)(retro_environment_t);
void (*pretro_set_video_refresh)(retro_video_refresh_t);
void (*pretro_set_audio_sample)(retro_audio_sample_t);
void (*pretro_set_audio_sample_batch)(retro_audio_sample_batch_t);
void (*pretro_set_input_poll)(retro_input_poll_t);
void (*pretro_set_input_state)(retro_input_state_t);
2011-04-17 11:30:59 +00:00
2012-04-09 22:37:09 +00:00
void (*pretro_set_controller_port_device)(unsigned, unsigned);
2010-12-30 12:54:49 +00:00
2012-04-05 09:47:43 +00:00
void (*pretro_reset)(void);
void (*pretro_run)(void);
2012-04-05 09:47:43 +00:00
size_t (*pretro_serialize_size)(void);
bool (*pretro_serialize)(void*, size_t);
bool (*pretro_unserialize)(const void*, size_t);
2010-12-30 12:54:49 +00:00
2012-04-05 09:47:43 +00:00
void (*pretro_cheat_reset)(void);
void (*pretro_cheat_set)(unsigned, bool, const char*);
2012-04-07 09:55:37 +00:00
bool (*pretro_load_game)(const struct retro_game_info*);
bool (*pretro_load_game_special)(unsigned, const struct retro_game_info*, size_t);
2010-12-30 12:54:49 +00:00
2012-04-05 09:47:43 +00:00
void (*pretro_unload_game)(void);
2010-12-30 12:54:49 +00:00
2012-04-05 09:47:43 +00:00
unsigned (*pretro_get_region)(void);
2010-12-30 12:54:49 +00:00
2012-04-05 09:47:43 +00:00
void *(*pretro_get_memory_data)(unsigned);
size_t (*pretro_get_memory_size)(unsigned);
2010-12-30 12:54:49 +00:00
static void set_environment(void);
2011-12-02 01:48:04 +00:00
static void set_environment_defaults(void);
2012-04-05 09:47:43 +00:00
static void load_symbols(void)
2010-12-30 12:54:49 +00:00
{
2012-04-05 09:47:43 +00:00
#ifdef HAVE_DYNAMIC
2012-04-21 21:25:32 +00:00
RARCH_LOG("Loading dynamic libsnes from: \"%s\"\n", g_settings.libretro);
2012-04-07 10:17:40 +00:00
lib_handle = dylib_load(g_settings.libretro);
2010-12-30 12:54:49 +00:00
if (!lib_handle)
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to open dynamic library: \"%s\"\n", g_settings.libretro);
rarch_fail(1, "load_dynamic()");
2010-12-30 12:54:49 +00:00
}
2012-04-05 09:47:43 +00:00
#endif
2010-12-30 12:54:49 +00:00
2012-04-05 09:47:43 +00:00
SYM(retro_init);
SYM(retro_deinit);
2010-12-30 12:54:49 +00:00
2012-04-05 09:47:43 +00:00
SYM(retro_api_version);
SYM(retro_get_system_info);
SYM(retro_get_system_av_info);
SYM(retro_set_environment);
SYM(retro_set_video_refresh);
SYM(retro_set_audio_sample);
SYM(retro_set_audio_sample_batch);
SYM(retro_set_input_poll);
SYM(retro_set_input_state);
SYM(retro_set_controller_port_device);
SYM(retro_reset);
SYM(retro_run);
SYM(retro_serialize_size);
SYM(retro_serialize);
SYM(retro_unserialize);
SYM(retro_cheat_reset);
SYM(retro_cheat_set);
SYM(retro_load_game);
SYM(retro_load_game_special);
SYM(retro_unload_game);
SYM(retro_get_region);
SYM(retro_get_memory_data);
SYM(retro_get_memory_size);
2010-12-30 12:54:49 +00:00
}
2012-04-07 10:17:40 +00:00
void init_libretro_sym(void)
2010-12-30 12:54:49 +00:00
{
// Guarantee that we can do "dirty" casting.
// Every OS that this program supports should pass this ...
2012-04-21 21:25:32 +00:00
rarch_assert(sizeof(void*) == sizeof(void (*)(void)));
#ifdef HAVE_DYNAMIC
// Try to verify that -lsnes was not linked in from other modules
// since loading it dynamically and with -l will fail hard.
2012-04-05 09:47:43 +00:00
function_t sym = dylib_proc(NULL, "retro_init");
if (sym)
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Serious problem. RetroArch wants to load libsnes dyamically, but it is already linked.\n");
RARCH_ERR("This could happen if other modules RetroArch depends on link against libsnes directly.\n");
RARCH_ERR("Proceeding could cause a crash. Aborting ...\n");
rarch_fail(1, "init_libretro_sym()");
}
2012-04-07 10:17:40 +00:00
if (!*g_settings.libretro)
{
#if defined(_WIN32)
2012-04-07 10:17:40 +00:00
const char *libretro_path = "retro.dll";
#elif defined(__APPLE__)
2012-04-07 10:17:40 +00:00
const char *libretro_path = "libretro.dylib";
#else
2012-04-07 10:17:40 +00:00
const char *libretro_path = "libretro.so";
#endif
2012-04-07 10:17:40 +00:00
strlcpy(g_settings.libretro, libretro_path, sizeof(g_settings.libretro));
}
#endif
2012-04-05 09:47:43 +00:00
load_symbols();
2011-12-02 01:48:04 +00:00
set_environment_defaults();
set_environment();
2010-12-30 12:54:49 +00:00
}
2012-04-07 10:17:40 +00:00
void uninit_libretro_sym(void)
2010-12-30 12:54:49 +00:00
{
2011-12-04 16:54:00 +00:00
#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
}
#ifdef NEED_DYNAMIC
2011-03-07 18:12:14 +00:00
// Platform independent dylib loading.
dylib_t dylib_load(const char *path)
{
2011-01-19 12:25:18 +00:00
#ifdef _WIN32
dylib_t lib = LoadLibrary(path);
if (!lib)
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to load library, error code: 0x%x\n", (unsigned)GetLastError());
return lib;
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
2011-12-24 12:46:12 +00:00
function_t sym = (function_t)GetProcAddress(lib ? (HMODULE)lib : GetModuleHandle(NULL), proc);
2011-03-07 18:12:14 +00:00
#else
void *ptr_sym = NULL;
if (lib)
ptr_sym = dlsym(lib, proc);
else
{
void *handle = dlopen(NULL, RTLD_LAZY);
if (handle)
{
ptr_sym = dlsym(handle, proc);
dlclose(handle);
}
}
2011-11-02 18:34:08 +00:00
// Dirty hack to workaround the non-legality of (void*) -> fn-pointer casts.
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
return sym;
2011-03-07 18:12:14 +00:00
}
void dylib_close(dylib_t lib)
{
#ifdef _WIN32
2011-12-24 12:46:12 +00:00
FreeLibrary((HMODULE)lib);
2011-03-07 18:12:14 +00:00
#else
dlclose(lib);
2010-12-30 12:54:49 +00:00
#endif
}
2012-01-02 14:42:53 +00:00
#endif
2011-03-07 18:12:14 +00:00
static bool environment_cb(unsigned cmd, void *data)
{
switch (cmd)
{
2012-04-07 09:55:37 +00:00
case RETRO_ENVIRONMENT_GET_OVERSCAN:
2011-11-09 21:18:48 +00:00
*(bool*)data = !g_settings.video.crop_overscan;
2012-04-21 21:25:32 +00:00
RARCH_LOG("Environ GET_OVERSCAN: %u\n", (unsigned)!g_settings.video.crop_overscan);
2011-11-09 21:18:48 +00:00
break;
2012-04-07 09:55:37 +00:00
case RETRO_ENVIRONMENT_GET_CAN_DUPE:
#ifdef HAVE_FFMPEG
2011-11-22 16:27:02 +00:00
*(bool*)data = true;
2012-04-21 21:25:32 +00:00
RARCH_LOG("Environ GET_CAN_DUPE: true\n");
#else
*(bool*)data = false;
2012-04-21 21:25:32 +00:00
RARCH_LOG("Environ GET_CAN_DUPE: false\n");
#endif
2011-12-02 18:12:47 +00:00
break;
2012-04-07 09:55:37 +00:00
case RETRO_ENVIRONMENT_GET_VARIABLE:
{
2012-04-07 09:55:37 +00:00
struct retro_variable *var = (struct retro_variable*)data;
if (var->key)
{
// Split string has '\0' delimiters so we have to find the position in original string,
// then pass the corresponding offset into the split string.
const char *key = strstr(g_extern.system.environment, var->key);
size_t key_len = strlen(var->key);
if (key && key[key_len] == '=')
{
ptrdiff_t offset = key - g_extern.system.environment;
var->value = &g_extern.system.environment_split[offset + key_len + 1];
}
else
var->value = NULL;
}
else
var->value = g_extern.system.environment;
2012-04-21 21:25:32 +00:00
RARCH_LOG("Environ GET_VARIABLE: %s=%s\n",
var->key ? var->key : "null",
var->value ? var->value : "null");
break;
}
2012-04-07 09:55:37 +00:00
case RETRO_ENVIRONMENT_SET_VARIABLES:
2012-02-06 15:13:29 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_LOG("Environ SET_VARIABLES:\n");
RARCH_LOG("=======================\n");
2012-04-07 09:55:37 +00:00
const struct retro_variable *vars = (const struct retro_variable*)data;
2012-02-06 15:13:29 +00:00
while (vars->key)
{
2012-04-21 21:25:32 +00:00
RARCH_LOG("\t%s :: %s\n",
2012-02-06 15:13:29 +00:00
vars->key,
vars->value ? vars->value : "N/A");
vars++;
}
2012-04-21 21:25:32 +00:00
RARCH_LOG("=======================\n");
2012-02-06 15:13:29 +00:00
break;
}
2012-04-07 09:55:37 +00:00
case RETRO_ENVIRONMENT_SET_MESSAGE:
2012-03-11 23:37:44 +00:00
{
2012-04-07 09:55:37 +00:00
const struct retro_message *msg = (const struct retro_message*)data;
2012-04-21 21:25:32 +00:00
RARCH_LOG("Environ SET_MESSAGE: %s\n", msg->msg);
2012-03-11 23:37:44 +00:00
if (g_extern.msg_queue)
msg_queue_push(g_extern.msg_queue, msg->msg, 1, msg->frames);
break;
}
2012-04-07 09:55:37 +00:00
case RETRO_ENVIRONMENT_SET_ROTATION:
2012-03-30 17:09:34 +00:00
{
unsigned rotation = *(const unsigned*)data;
2012-04-21 21:25:32 +00:00
RARCH_LOG("Environ SET_ROTATION: %u\n", rotation);
2012-04-01 14:12:04 +00:00
if (!g_settings.video.allow_rotate)
break;
2012-04-01 17:20:37 +00:00
g_extern.system.rotation = rotation;
if (driver.video && driver.video->set_rotation)
{
if (driver.video_data)
video_set_rotation_func(rotation);
}
2012-03-30 17:09:34 +00:00
else
return false;
break;
}
default:
2012-04-21 21:25:32 +00:00
RARCH_LOG("Environ UNSUPPORTED (#%u).\n", cmd);
return false;
}
return true;
}
static void set_environment(void)
{
2012-04-05 09:47:43 +00:00
pretro_set_environment(environment_cb);
}
2011-10-27 22:34:42 +00:00
2011-12-02 01:48:04 +00:00
// Assume SNES as defaults.
static void set_environment_defaults(void)
{
// Split up environment variables beforehand.
if (g_extern.system.environment_split && strtok(g_extern.system.environment_split, ";"))
while (strtok(NULL, ";"));
2011-12-02 01:48:04 +00:00
}