RetroArch/dynamic.c

422 lines
12 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"
#include "compat/posix_string.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
#include "console/rarch_console.h"
2012-03-11 23:37:44 +00:00
#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-07-07 14:28:01 +00:00
RARCH_LOG("Loading dynamic libretro 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
2012-07-07 14:28:01 +00:00
// Try to verify that -lretro 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-07-07 14:28:01 +00:00
RARCH_ERR("Serious problem. RetroArch wants to load libretro dyamically, but it is already linked.\n");
RARCH_ERR("This could happen if other modules RetroArch depends on link against libretro directly.\n");
2012-04-21 21:25:32 +00:00
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)
{
RARCH_ERR("RetroArch is built for dynamic libretro, but libretro_path is not set. Cannot continue.\n");
rarch_fail(1, "init_libretro_sym()");
}
#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:
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");
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;
}
2012-05-22 18:14:07 +00:00
case RETRO_ENVIRONMENT_SHUTDOWN:
RARCH_LOG("Environ SHUTDOWN.\n");
2012-05-22 18:14:07 +00:00
g_extern.system.shutdown = true;
break;
case RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL:
g_extern.system.performance_level = *(const unsigned*)data;
RARCH_LOG("Environ PERFORMANCE_LEVEL: %u.\n", g_extern.system.performance_level);
break;
case RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY:
*(const char **)data = *g_settings.system_directory ? g_settings.system_directory : NULL;
RARCH_LOG("Environ SYSTEM_DIRECTORY: \"%s\".\n", g_settings.system_directory);
break;
2012-06-16 13:07:31 +00:00
case RETRO_ENVIRONMENT_SET_PIXEL_FORMAT:
{
enum retro_pixel_format pix_fmt = *(const enum retro_pixel_format*)data;
bool rgb32 = false;
switch (pix_fmt)
{
case RETRO_PIXEL_FORMAT_0RGB1555:
rgb32 = false;
RARCH_LOG("Environ SET_PIXEL_FORMAT: 0RGB1555.\n");
break;
#ifndef RARCH_CONSOLE
case RETRO_PIXEL_FORMAT_XRGB8888:
rgb32 = true;
RARCH_LOG("Environ SET_PIXEL_FORMAT: XRGB8888.\n");
break;
#endif
default:
return false;
}
g_extern.system.rgb32 = rgb32;
break;
}
case RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS:
{
memset(g_extern.system.input_desc_btn, 0, sizeof(g_extern.system.input_desc_btn));
const struct retro_input_descriptor *desc = (const struct retro_input_descriptor*)data;
for (; desc->description; desc++)
{
if (desc->port >= MAX_PLAYERS)
continue;
if (desc->device != RETRO_DEVICE_JOYPAD) // Ignore all others for now.
continue;
if (desc->id >= RARCH_FIRST_ANALOG_BIND)
continue;
g_extern.system.input_desc_btn[desc->port][desc->id] = desc->description;
}
static const char *libretro_btn_desc[] = {
"B (bottom)", "Y (left)", "Select", "Start",
"D-Pad Up", "D-Pad Down", "D-Pad Left", "D-Pad Right",
"A (right)", "X (up)",
"L", "R", "L2", "R2", "L3", "R3",
};
RARCH_LOG("Environ SET_INPUT_DESCRIPTORS:\n");
for (unsigned p = 0; p < MAX_PLAYERS; p++)
{
for (unsigned id = 0; id < RARCH_FIRST_ANALOG_BIND; id++)
{
const char *desc = g_extern.system.input_desc_btn[p][id];
if (desc)
{
RARCH_LOG("\tRetroPad, Player %u, Button \"%s\" => \"%s\"\n",
p + 1, libretro_btn_desc[id], desc);
}
}
}
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)
{
char *save;
// Split up environment variables beforehand.
if (g_extern.system.environment_split &&
strtok_r(g_extern.system.environment_split, ";", &save))
while (strtok_r(NULL, ";", &save));
2011-12-02 01:48:04 +00:00
}