2012-04-21 21:13:50 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 00:50:59 +00:00
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2015-01-07 17:06:50 +00:00
|
|
|
* Copyright (C) 2011-2015 - Daniel De Matteis
|
2011-05-25 13:15:20 +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
|
2011-05-25 13:15:20 +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;
|
2011-05-25 13:15:20 +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.
|
2011-05-25 13:15:20 +00:00
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-01-12 15:00:13 +00:00
|
|
|
#include "video_state_tracker.h"
|
2011-05-25 13:15:20 +00:00
|
|
|
#include <stdlib.h>
|
2014-10-21 05:58:58 +00:00
|
|
|
#include <compat/strl.h>
|
2014-10-25 23:15:41 +00:00
|
|
|
#include "../general.h"
|
2014-01-08 16:31:14 +00:00
|
|
|
#include "../input/input_common.h"
|
2011-06-06 15:44:05 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_PYTHON
|
2015-01-12 15:00:13 +00:00
|
|
|
#include "video_state_python.h"
|
2011-06-06 15:44:05 +00:00
|
|
|
#endif
|
2011-05-25 13:15:20 +00:00
|
|
|
|
2012-04-07 10:17:40 +00:00
|
|
|
struct state_tracker_internal
|
2011-05-25 13:15:20 +00:00
|
|
|
{
|
|
|
|
char id[64];
|
|
|
|
|
2011-06-11 18:02:17 +00:00
|
|
|
bool is_input;
|
|
|
|
const uint16_t *input_ptr;
|
2011-05-25 13:15:20 +00:00
|
|
|
const uint8_t *ptr;
|
2011-06-06 15:44:05 +00:00
|
|
|
#ifdef HAVE_PYTHON
|
|
|
|
py_state_t *py;
|
|
|
|
#endif
|
|
|
|
|
2011-05-25 13:15:20 +00:00
|
|
|
uint32_t addr;
|
2012-02-12 17:05:33 +00:00
|
|
|
uint16_t mask;
|
|
|
|
|
|
|
|
uint16_t equal;
|
2011-05-25 13:15:20 +00:00
|
|
|
|
2012-04-07 10:17:40 +00:00
|
|
|
enum state_tracker_type type;
|
2011-05-25 13:15:20 +00:00
|
|
|
|
2011-06-11 18:02:17 +00:00
|
|
|
uint32_t prev[2];
|
2011-05-25 13:15:20 +00:00
|
|
|
int frame_count;
|
2011-06-07 19:07:00 +00:00
|
|
|
int frame_count_prev;
|
2011-06-11 18:02:17 +00:00
|
|
|
uint32_t old_value;
|
2011-06-06 19:32:10 +00:00
|
|
|
int transition_count;
|
2011-05-25 13:15:20 +00:00
|
|
|
};
|
|
|
|
|
2012-04-07 10:17:40 +00:00
|
|
|
struct state_tracker
|
2011-05-25 13:15:20 +00:00
|
|
|
{
|
2012-04-07 10:17:40 +00:00
|
|
|
struct state_tracker_internal *info;
|
2011-05-25 13:15:20 +00:00
|
|
|
unsigned info_elem;
|
2011-06-06 15:44:05 +00:00
|
|
|
|
2011-06-11 18:02:17 +00:00
|
|
|
uint16_t input_state[2];
|
|
|
|
|
2011-06-06 15:44:05 +00:00
|
|
|
#ifdef HAVE_PYTHON
|
|
|
|
py_state_t *py;
|
|
|
|
#endif
|
2011-05-25 13:15:20 +00:00
|
|
|
};
|
|
|
|
|
2015-01-11 21:39:50 +00:00
|
|
|
/**
|
|
|
|
* state_tracker_init:
|
|
|
|
* @info : State tracker info handle.
|
|
|
|
*
|
|
|
|
* Creates and initializes graphics state tracker.
|
|
|
|
*
|
|
|
|
* Returns: new state tracker handle if successful, otherwise NULL.
|
|
|
|
**/
|
2012-04-07 10:17:40 +00:00
|
|
|
state_tracker_t* state_tracker_init(const struct state_tracker_info *info)
|
2011-05-25 13:15:20 +00:00
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
unsigned i;
|
2012-04-07 10:17:40 +00:00
|
|
|
state_tracker_t *tracker = (state_tracker_t*)calloc(1, sizeof(*tracker));
|
2011-05-25 13:15:20 +00:00
|
|
|
if (!tracker)
|
|
|
|
return NULL;
|
|
|
|
|
2011-06-06 15:44:05 +00:00
|
|
|
#ifdef HAVE_PYTHON
|
|
|
|
if (info->script)
|
|
|
|
{
|
2014-09-08 15:57:18 +00:00
|
|
|
tracker->py = py_state_new(info->script, info->script_is_file,
|
|
|
|
info->script_class ? info->script_class : "GameAware");
|
|
|
|
|
2011-06-06 15:44:05 +00:00
|
|
|
if (!tracker->py)
|
|
|
|
{
|
|
|
|
free(tracker);
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_ERR("Failed to init Python script.\n");
|
2011-06-06 15:44:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-08 15:57:18 +00:00
|
|
|
tracker->info = (struct state_tracker_internal*)
|
|
|
|
calloc(info->info_elem, sizeof(struct state_tracker_internal));
|
2014-09-12 23:27:16 +00:00
|
|
|
|
|
|
|
if (!tracker->info)
|
|
|
|
{
|
2014-09-13 23:49:24 +00:00
|
|
|
RARCH_ERR("Allocation of state tracker info failed.\n");
|
2014-09-12 23:27:16 +00:00
|
|
|
free(tracker);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-25 13:15:20 +00:00
|
|
|
tracker->info_elem = info->info_elem;
|
|
|
|
|
2013-10-22 13:08:17 +00:00
|
|
|
for (i = 0; i < info->info_elem; i++)
|
2011-05-25 13:15:20 +00:00
|
|
|
{
|
2015-01-07 21:18:03 +00:00
|
|
|
/* If we don't have a valid pointer. */
|
|
|
|
static const uint8_t empty = 0;
|
|
|
|
|
2014-09-08 15:57:18 +00:00
|
|
|
strlcpy(tracker->info[i].id, info->info[i].id,
|
|
|
|
sizeof(tracker->info[i].id));
|
2012-02-12 17:05:33 +00:00
|
|
|
tracker->info[i].addr = info->info[i].addr;
|
|
|
|
tracker->info[i].type = info->info[i].type;
|
2014-09-08 15:57:18 +00:00
|
|
|
tracker->info[i].mask = (info->info[i].mask == 0)
|
|
|
|
? 0xffff : info->info[i].mask;
|
2012-02-12 17:05:33 +00:00
|
|
|
tracker->info[i].equal = info->info[i].equal;
|
2011-05-25 13:15:20 +00:00
|
|
|
|
2011-06-06 15:44:05 +00:00
|
|
|
#ifdef HAVE_PYTHON
|
2012-04-21 21:25:32 +00:00
|
|
|
if (info->info[i].type == RARCH_STATE_PYTHON)
|
2012-11-18 20:23:34 +00:00
|
|
|
{
|
|
|
|
if (!tracker->py)
|
|
|
|
{
|
|
|
|
free(tracker->info);
|
|
|
|
free(tracker);
|
|
|
|
RARCH_ERR("Python semantic was requested, but Python tracker is not loaded.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-06 15:44:05 +00:00
|
|
|
tracker->info[i].py = tracker->py;
|
2012-11-18 20:23:34 +00:00
|
|
|
}
|
2011-06-06 15:44:05 +00:00
|
|
|
#endif
|
|
|
|
|
2011-05-25 13:15:20 +00:00
|
|
|
switch (info->info[i].ram_type)
|
|
|
|
{
|
2012-04-21 21:25:32 +00:00
|
|
|
case RARCH_STATE_WRAM:
|
2011-10-26 17:51:10 +00:00
|
|
|
tracker->info[i].ptr = info->wram ? info->wram : ∅
|
2011-05-25 13:15:20 +00:00
|
|
|
break;
|
2012-04-21 21:25:32 +00:00
|
|
|
case RARCH_STATE_INPUT_SLOT1:
|
2011-06-11 18:02:17 +00:00
|
|
|
tracker->info[i].input_ptr = &tracker->input_state[0];
|
|
|
|
tracker->info[i].is_input = true;
|
|
|
|
break;
|
2012-04-21 21:25:32 +00:00
|
|
|
case RARCH_STATE_INPUT_SLOT2:
|
2011-06-11 18:02:17 +00:00
|
|
|
tracker->info[i].input_ptr = &tracker->input_state[1];
|
|
|
|
tracker->info[i].is_input = true;
|
|
|
|
break;
|
2011-05-25 13:15:20 +00:00
|
|
|
|
|
|
|
default:
|
2011-10-26 17:51:10 +00:00
|
|
|
tracker->info[i].ptr = ∅
|
2011-05-25 13:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tracker;
|
|
|
|
}
|
|
|
|
|
2015-01-11 21:39:50 +00:00
|
|
|
/**
|
|
|
|
* state_tracker_free:
|
|
|
|
* @tracker : State tracker handle.
|
|
|
|
*
|
|
|
|
* Frees a state tracker handle.
|
|
|
|
**/
|
2012-04-07 10:17:40 +00:00
|
|
|
void state_tracker_free(state_tracker_t *tracker)
|
2011-05-25 13:15:20 +00:00
|
|
|
{
|
2014-09-12 23:27:16 +00:00
|
|
|
if (tracker)
|
|
|
|
{
|
|
|
|
free(tracker->info);
|
2011-06-06 15:44:05 +00:00
|
|
|
#ifdef HAVE_PYTHON
|
2014-09-12 23:27:16 +00:00
|
|
|
py_state_free(tracker->py);
|
2011-06-06 15:44:05 +00:00
|
|
|
#endif
|
2014-09-12 23:27:16 +00:00
|
|
|
}
|
|
|
|
|
2011-05-25 13:15:20 +00:00
|
|
|
free(tracker);
|
|
|
|
}
|
|
|
|
|
2015-01-12 01:15:35 +00:00
|
|
|
static inline uint16_t state_tracker_fetch(
|
|
|
|
const struct state_tracker_internal *info)
|
2012-02-12 17:05:33 +00:00
|
|
|
{
|
2015-01-07 21:18:03 +00:00
|
|
|
uint16_t val = info->ptr[info->addr];
|
|
|
|
|
2012-02-12 17:05:33 +00:00
|
|
|
if (info->is_input)
|
|
|
|
val = *info->input_ptr;
|
|
|
|
|
|
|
|
val &= info->mask;
|
|
|
|
|
|
|
|
if (info->equal && val != info->equal)
|
|
|
|
val = 0;
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
2011-05-29 21:58:04 +00:00
|
|
|
|
2015-01-12 01:15:35 +00:00
|
|
|
static void state_tracker_update_element(
|
2012-04-07 10:17:40 +00:00
|
|
|
struct state_tracker_uniform *uniform,
|
|
|
|
struct state_tracker_internal *info,
|
2011-05-25 13:15:20 +00:00
|
|
|
unsigned frame_count)
|
|
|
|
{
|
|
|
|
uniform->id = info->id;
|
|
|
|
|
|
|
|
switch (info->type)
|
|
|
|
{
|
2012-04-21 21:25:32 +00:00
|
|
|
case RARCH_STATE_CAPTURE:
|
2015-01-12 01:15:35 +00:00
|
|
|
uniform->value = state_tracker_fetch(info);
|
2011-05-25 13:15:20 +00:00
|
|
|
break;
|
|
|
|
|
2012-04-21 21:25:32 +00:00
|
|
|
case RARCH_STATE_CAPTURE_PREV:
|
2015-01-12 01:15:35 +00:00
|
|
|
if (info->prev[0] != state_tracker_fetch(info))
|
2011-05-26 14:23:11 +00:00
|
|
|
{
|
|
|
|
info->prev[1] = info->prev[0];
|
2015-01-12 01:15:35 +00:00
|
|
|
info->prev[0] = state_tracker_fetch(info);
|
2011-05-26 14:23:11 +00:00
|
|
|
}
|
|
|
|
uniform->value = info->prev[1];
|
|
|
|
break;
|
|
|
|
|
2012-04-21 21:25:32 +00:00
|
|
|
case RARCH_STATE_TRANSITION:
|
2015-01-12 01:15:35 +00:00
|
|
|
if (info->old_value != state_tracker_fetch(info))
|
2011-05-25 13:15:20 +00:00
|
|
|
{
|
2015-01-12 01:15:35 +00:00
|
|
|
info->old_value = state_tracker_fetch(info);
|
2011-05-25 13:15:20 +00:00
|
|
|
info->frame_count = frame_count;
|
|
|
|
}
|
2011-05-26 14:23:11 +00:00
|
|
|
uniform->value = info->frame_count;
|
2011-05-25 13:15:20 +00:00
|
|
|
break;
|
|
|
|
|
2012-04-21 21:25:32 +00:00
|
|
|
case RARCH_STATE_TRANSITION_COUNT:
|
2015-01-12 01:15:35 +00:00
|
|
|
if (info->old_value != state_tracker_fetch(info))
|
2011-06-06 19:32:10 +00:00
|
|
|
{
|
2015-01-12 01:15:35 +00:00
|
|
|
info->old_value = state_tracker_fetch(info);
|
2011-06-06 19:32:10 +00:00
|
|
|
info->transition_count++;
|
|
|
|
}
|
|
|
|
uniform->value = info->transition_count;
|
|
|
|
break;
|
|
|
|
|
2012-04-21 21:25:32 +00:00
|
|
|
case RARCH_STATE_TRANSITION_PREV:
|
2015-01-12 01:15:35 +00:00
|
|
|
if (info->old_value != state_tracker_fetch(info))
|
2011-05-25 13:15:20 +00:00
|
|
|
{
|
2015-01-12 01:15:35 +00:00
|
|
|
info->old_value = state_tracker_fetch(info);
|
2011-06-07 19:07:00 +00:00
|
|
|
info->frame_count_prev = info->frame_count;
|
2011-05-26 14:23:11 +00:00
|
|
|
info->frame_count = frame_count;
|
2011-05-25 13:15:20 +00:00
|
|
|
}
|
2011-06-07 19:07:00 +00:00
|
|
|
uniform->value = info->frame_count_prev;
|
2011-05-26 14:23:11 +00:00
|
|
|
break;
|
|
|
|
|
2011-06-06 15:44:05 +00:00
|
|
|
#ifdef HAVE_PYTHON
|
2012-04-21 21:25:32 +00:00
|
|
|
case RARCH_STATE_PYTHON:
|
2011-06-06 15:44:05 +00:00
|
|
|
uniform->value = py_state_get(info->py, info->id, frame_count);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2011-05-25 13:15:20 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-11 21:39:50 +00:00
|
|
|
/**
|
2015-01-12 01:15:35 +00:00
|
|
|
* state_tracker_update_input:
|
2015-01-11 21:39:50 +00:00
|
|
|
* @tracker : State tracker handle.
|
|
|
|
*
|
|
|
|
* Updates 16-bit input in same format as libretro API itself.
|
|
|
|
**/
|
2015-01-12 01:15:35 +00:00
|
|
|
static void state_tracker_update_input(state_tracker_t *tracker)
|
2011-06-11 18:02:17 +00:00
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
unsigned i;
|
2015-01-07 21:18:03 +00:00
|
|
|
uint16_t state[2] = {0};
|
2011-06-11 18:02:17 +00:00
|
|
|
|
|
|
|
static const unsigned buttons[] = {
|
2012-04-07 10:17:40 +00:00
|
|
|
RETRO_DEVICE_ID_JOYPAD_R,
|
|
|
|
RETRO_DEVICE_ID_JOYPAD_L,
|
|
|
|
RETRO_DEVICE_ID_JOYPAD_X,
|
|
|
|
RETRO_DEVICE_ID_JOYPAD_A,
|
|
|
|
RETRO_DEVICE_ID_JOYPAD_RIGHT,
|
|
|
|
RETRO_DEVICE_ID_JOYPAD_LEFT,
|
|
|
|
RETRO_DEVICE_ID_JOYPAD_DOWN,
|
|
|
|
RETRO_DEVICE_ID_JOYPAD_UP,
|
|
|
|
RETRO_DEVICE_ID_JOYPAD_START,
|
|
|
|
RETRO_DEVICE_ID_JOYPAD_SELECT,
|
|
|
|
RETRO_DEVICE_ID_JOYPAD_Y,
|
|
|
|
RETRO_DEVICE_ID_JOYPAD_B,
|
2011-06-11 18:02:17 +00:00
|
|
|
};
|
|
|
|
|
2014-09-08 15:57:18 +00:00
|
|
|
/* Only bind for up to two players for now. */
|
2012-07-07 15:19:32 +00:00
|
|
|
static const struct retro_keybind *binds[2] = {
|
2011-06-11 18:02:17 +00:00
|
|
|
g_settings.input.binds[0],
|
|
|
|
g_settings.input.binds[1],
|
|
|
|
};
|
|
|
|
|
2015-01-07 21:18:03 +00:00
|
|
|
if (!driver.input)
|
|
|
|
return;
|
|
|
|
|
2014-01-08 16:31:14 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
2014-09-08 15:57:18 +00:00
|
|
|
input_push_analog_dpad(g_settings.input.binds[i],
|
|
|
|
g_settings.input.analog_dpad_mode[i]);
|
2015-01-05 00:58:00 +00:00
|
|
|
for (i = 0; i < MAX_USERS; i++)
|
2014-09-08 15:57:18 +00:00
|
|
|
input_push_analog_dpad(g_settings.input.autoconf_binds[i],
|
|
|
|
g_settings.input.analog_dpad_mode[i]);
|
2014-01-08 16:31:14 +00:00
|
|
|
|
2014-07-10 22:26:50 +00:00
|
|
|
if (!driver.block_libretro_input)
|
2011-06-11 18:02:17 +00:00
|
|
|
{
|
2014-07-10 22:26:50 +00:00
|
|
|
for (i = 4; i < 16; i++)
|
|
|
|
{
|
2014-09-08 15:57:18 +00:00
|
|
|
state[0] |= (driver.input->input_state(
|
|
|
|
driver.input_data, binds, 0,
|
|
|
|
RETRO_DEVICE_JOYPAD, 0, buttons[i - 4]) ? 1 : 0) << i;
|
|
|
|
state[1] |= (driver.input->input_state(
|
|
|
|
driver.input_data, binds, 1,
|
|
|
|
RETRO_DEVICE_JOYPAD, 0, buttons[i - 4]) ? 1 : 0) << i;
|
2014-07-10 22:26:50 +00:00
|
|
|
}
|
2011-06-11 18:02:17 +00:00
|
|
|
}
|
|
|
|
|
2014-01-08 16:31:14 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
input_pop_analog_dpad(g_settings.input.binds[i]);
|
2015-01-05 00:58:00 +00:00
|
|
|
for (i = 0; i < MAX_USERS; i++)
|
2014-01-08 16:47:07 +00:00
|
|
|
input_pop_analog_dpad(g_settings.input.autoconf_binds[i]);
|
2014-01-08 16:31:14 +00:00
|
|
|
|
2013-10-22 13:08:17 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
2011-06-11 18:02:17 +00:00
|
|
|
tracker->input_state[i] = state[i];
|
|
|
|
}
|
|
|
|
|
2015-01-11 21:39:50 +00:00
|
|
|
/**
|
2015-01-12 01:15:35 +00:00
|
|
|
* state_tracker_get_uniform:
|
2015-01-11 21:39:50 +00:00
|
|
|
* @tracker : State tracker handle.
|
|
|
|
* @uniforms : State tracker uniforms.
|
|
|
|
* @elem : Amount of uniform elements.
|
|
|
|
* @frame_count : Frame count.
|
|
|
|
*
|
2015-01-12 01:15:35 +00:00
|
|
|
* Calls state_tracker_update_input(), and updates each uniform
|
2015-01-11 21:39:50 +00:00
|
|
|
* element accordingly.
|
|
|
|
*
|
|
|
|
* Returns: Amount of state elements (either equal to @elem
|
|
|
|
* or equal to @tracker->info_eleme).
|
|
|
|
**/
|
2015-01-12 01:15:35 +00:00
|
|
|
unsigned state_tracker_get_uniform(state_tracker_t *tracker,
|
2014-09-08 15:57:18 +00:00
|
|
|
struct state_tracker_uniform *uniforms,
|
|
|
|
unsigned elem, unsigned frame_count)
|
2011-05-25 13:15:20 +00:00
|
|
|
{
|
2015-01-09 00:23:58 +00:00
|
|
|
unsigned i, elems = elem;
|
|
|
|
|
|
|
|
if (tracker->info_elem < elem)
|
|
|
|
elems = tracker->info_elem;
|
2011-05-25 13:15:20 +00:00
|
|
|
|
2015-01-12 01:15:35 +00:00
|
|
|
state_tracker_update_input(tracker);
|
2011-06-11 18:02:17 +00:00
|
|
|
|
2013-10-22 13:08:17 +00:00
|
|
|
for (i = 0; i < elems; i++)
|
2015-01-12 01:15:35 +00:00
|
|
|
state_tracker_update_element(
|
|
|
|
&uniforms[i], &tracker->info[i], frame_count);
|
2011-05-25 13:15:20 +00:00
|
|
|
|
|
|
|
return elems;
|
|
|
|
}
|