RetroArch/input/input_overlay.c

1085 lines
27 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2015-01-07 16:46:50 +00:00
* Copyright (C) 2011-2015 - Daniel De Matteis
*
* RetroArch 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.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "input_overlay.h"
#include "../driver.h"
#include "../general.h"
2014-10-21 05:58:58 +00:00
#include <compat/posix_string.h>
2012-12-22 12:27:00 +00:00
#include "input_common.h"
2014-10-21 22:23:06 +00:00
#include <file/file_path.h>
#include <file/config_file.h>
#include <clamping.h>
2012-12-22 12:27:00 +00:00
#include <stddef.h>
#include <math.h>
2015-01-18 08:25:48 +00:00
/**
* input_overlay_scale:
* @ol : Overlay handle.
* @scale : Scaling factor.
*
* Scales overlay and all its associated descriptors
* by a given scaling factor (@scale).
**/
static void input_overlay_scale(struct overlay *ol, float scale)
2013-02-03 22:35:55 +00:00
{
size_t i;
2015-01-14 22:15:24 +00:00
2015-02-15 06:01:32 +00:00
if (!ol)
return;
2015-01-18 08:25:48 +00:00
if (ol->block_scale)
scale = 1.0f;
2015-01-18 08:25:48 +00:00
ol->scale = scale;
ol->mod_w = ol->w * scale;
ol->mod_h = ol->h * scale;
ol->mod_x = ol->center_x +
(ol->x - ol->center_x) * scale;
ol->mod_y = ol->center_y +
(ol->y - ol->center_y) * scale;
2015-01-18 08:25:48 +00:00
for (i = 0; i < ol->size; i++)
2013-02-03 22:35:55 +00:00
{
2015-01-14 22:15:24 +00:00
float scale_w, scale_h, adj_center_x, adj_center_y;
2015-01-18 08:25:48 +00:00
struct overlay_desc *desc = &ol->descs[i];
2013-10-21 12:26:42 +00:00
2015-01-18 08:25:48 +00:00
if (!desc)
continue;
scale_w = ol->mod_w * desc->range_x;
scale_h = ol->mod_h * desc->range_y;
2013-10-21 12:26:42 +00:00
desc->mod_w = 2.0f * scale_w;
desc->mod_h = 2.0f * scale_h;
2015-01-18 08:25:48 +00:00
adj_center_x = ol->mod_x + desc->x * ol->mod_w;
adj_center_y = ol->mod_y + desc->y * ol->mod_h;
2013-10-21 12:26:42 +00:00
desc->mod_x = adj_center_x - scale_w;
desc->mod_y = adj_center_y - scale_h;
2013-02-03 22:35:55 +00:00
}
}
static void input_overlay_set_vertex_geom(input_overlay_t *ol)
{
size_t i;
2015-02-15 06:01:32 +00:00
if (!ol)
return;
if (ol->active->image.pixels)
ol->iface->vertex_geom(ol->iface_data, 0,
2014-09-02 14:50:28 +00:00
ol->active->mod_x, ol->active->mod_y,
ol->active->mod_w, ol->active->mod_h);
for (i = 0; i < ol->active->size; i++)
2013-02-03 22:35:55 +00:00
{
struct overlay_desc *desc = &ol->active->descs[i];
if (!desc)
continue;
2015-01-14 22:15:24 +00:00
if (!desc->image.pixels)
continue;
ol->iface->vertex_geom(ol->iface_data, desc->image_index,
desc->mod_x, desc->mod_y, desc->mod_w, desc->mod_h);
2013-02-03 22:35:55 +00:00
}
}
2015-01-09 23:59:05 +00:00
/**
* input_overlay_set_scale_factor:
* @ol : Overlay handle.
* @scale : Factor of scale to apply.
*
* Scales the overlay by a factor of scale.
**/
2013-02-03 22:35:55 +00:00
void input_overlay_set_scale_factor(input_overlay_t *ol, float scale)
{
size_t i;
2014-09-28 15:52:15 +00:00
if (!ol)
return;
for (i = 0; i < ol->size; i++)
2013-02-03 22:35:55 +00:00
input_overlay_scale(&ol->overlays[i], scale);
input_overlay_set_vertex_geom(ol);
2013-02-03 22:35:55 +00:00
}
2012-12-22 12:27:00 +00:00
static void input_overlay_free_overlay(struct overlay *overlay)
{
size_t i;
if (!overlay)
return;
2014-05-28 19:14:11 +00:00
for (i = 0; i < overlay->size; i++)
2014-06-17 15:44:48 +00:00
texture_image_free(&overlay->descs[i].image);
free(overlay->load_images);
2012-12-22 12:27:00 +00:00
free(overlay->descs);
2014-06-17 15:44:48 +00:00
texture_image_free(&overlay->image);
2012-12-22 12:27:00 +00:00
}
static void input_overlay_free_overlays(input_overlay_t *ol)
{
size_t i;
if (!ol)
return;
for (i = 0; i < ol->size; i++)
2012-12-22 12:27:00 +00:00
input_overlay_free_overlay(&ol->overlays[i]);
2012-12-22 12:27:00 +00:00
free(ol->overlays);
}
2014-09-02 14:50:28 +00:00
static bool input_overlay_load_desc(input_overlay_t *ol,
struct overlay_desc *desc,
2014-10-20 18:31:00 +00:00
unsigned ol_idx, unsigned desc_idx,
2013-10-21 10:42:47 +00:00
unsigned width, unsigned height,
bool normalized, float alpha_mod, float range_mod)
2012-12-22 12:27:00 +00:00
{
bool ret = true, by_pixel;
char overlay_desc_key[64], overlay_desc_image_key[64], conf_key[64],
overlay_desc_normalized_key[64], image_path[PATH_MAX_LENGTH];
char overlay[256], *save, *key;
float width_mod, height_mod;
struct string_list *list = NULL;
const char *x = NULL;
const char *y = NULL;
const char *box = NULL;
config_file_t *conf = config_file_new(ol->overlay_path);
if (!conf)
{
ret = false;
goto end;
}
2014-09-02 14:50:28 +00:00
snprintf(overlay_desc_key, sizeof(overlay_desc_key),
2014-10-20 18:31:00 +00:00
"overlay%u_desc%u", ol_idx, desc_idx);
2012-12-22 12:27:00 +00:00
snprintf(overlay_desc_image_key, sizeof(overlay_desc_image_key),
2014-10-20 18:31:00 +00:00
"overlay%u_desc%u_overlay", ol_idx, desc_idx);
2014-09-02 14:50:28 +00:00
if (config_get_path(conf, overlay_desc_image_key,
image_path, sizeof(image_path)))
{
char path[PATH_MAX_LENGTH];
struct texture_image img = {0};
2014-09-02 14:50:28 +00:00
fill_pathname_resolve_relative(path, ol->overlay_path,
image_path, sizeof(path));
2014-06-17 15:44:48 +00:00
if (texture_image_load(&img, path))
2014-05-28 19:14:11 +00:00
desc->image = img;
}
snprintf(overlay_desc_normalized_key, sizeof(overlay_desc_normalized_key),
2014-10-20 18:31:00 +00:00
"overlay%u_desc%u_normalized", ol_idx, desc_idx);
config_get_bool(conf, overlay_desc_normalized_key, &normalized);
by_pixel = !normalized;
if (by_pixel && (width == 0 || height == 0))
{
RARCH_ERR("[Overlay]: Base overlay is not set and not using normalized coordinates.\n");
return false;
}
2012-12-22 12:27:00 +00:00
if (!config_get_array(conf, overlay_desc_key, overlay, sizeof(overlay)))
{
RARCH_ERR("[Overlay]: Didn't find key: %s.\n", overlay_desc_key);
2012-12-22 12:27:00 +00:00
return false;
}
2012-12-22 12:27:00 +00:00
list = string_split(overlay, ", ");
2015-01-14 22:15:24 +00:00
2012-12-22 12:27:00 +00:00
if (!list)
{
RARCH_ERR("[Overlay]: Failed to split overlay desc.\n");
2012-12-22 12:27:00 +00:00
return false;
}
2012-12-22 12:27:00 +00:00
if (list->size < 6)
{
string_list_free(list);
RARCH_ERR("[Overlay]: Overlay desc is invalid. Requires at least 6 tokens.\n");
2012-12-22 12:27:00 +00:00
return false;
}
x = list->elems[1].data;
y = list->elems[2].data;
box = list->elems[3].data;
2012-12-22 12:27:00 +00:00
key = list->elems[0].data;
desc->key_mask = 0;
2015-01-14 22:15:24 +00:00
if (!strcmp(key, "analog_left"))
2013-09-05 15:38:00 +00:00
desc->type = OVERLAY_TYPE_ANALOG_LEFT;
2015-01-14 22:15:24 +00:00
else if (!strcmp(key, "analog_right"))
2013-09-05 15:38:00 +00:00
desc->type = OVERLAY_TYPE_ANALOG_RIGHT;
else if (strstr(key, "retrok_") == key)
{
desc->type = OVERLAY_TYPE_KEYBOARD;
desc->key_mask = input_translate_str_to_rk(key + 7);
}
2013-09-05 15:38:00 +00:00
else
2013-02-23 21:57:39 +00:00
{
const char *tmp;
2013-09-05 15:38:00 +00:00
desc->type = OVERLAY_TYPE_BUTTONS;
for (tmp = strtok_r(key, "|", &save); tmp; tmp = strtok_r(NULL, "|", &save))
{
if (strcmp(tmp, "nul") != 0)
desc->key_mask |= UINT64_C(1) << input_translate_str_to_bind_id(tmp);
}
2013-09-05 15:38:00 +00:00
if (desc->key_mask & (UINT64_C(1) << RARCH_OVERLAY_NEXT))
{
char overlay_target_key[64];
2014-09-02 14:50:28 +00:00
snprintf(overlay_target_key, sizeof(overlay_target_key),
2014-10-20 18:31:00 +00:00
"overlay%u_desc%u_next_target", ol_idx, desc_idx);
2014-09-02 14:50:28 +00:00
config_get_array(conf, overlay_target_key,
desc->next_index_name, sizeof(desc->next_index_name));
2013-09-05 15:38:00 +00:00
}
2013-02-23 21:57:39 +00:00
}
width_mod = by_pixel ? (1.0f / width) : 1.0f;
height_mod = by_pixel ? (1.0f / height) : 1.0f;
desc->x = (float)strtod(x, NULL) * width_mod;
desc->y = (float)strtod(y, NULL) * height_mod;
2012-12-22 12:27:00 +00:00
if (!strcmp(box, "radial"))
desc->hitbox = OVERLAY_HITBOX_RADIAL;
else if (!strcmp(box, "rect"))
desc->hitbox = OVERLAY_HITBOX_RECT;
else
{
RARCH_ERR("[Overlay]: Hitbox type (%s) is invalid. Use \"radial\" or \"rect\".\n", box);
2012-12-22 12:27:00 +00:00
ret = false;
goto end;
}
2014-09-02 14:50:28 +00:00
if (
desc->type == OVERLAY_TYPE_ANALOG_LEFT ||
desc->type == OVERLAY_TYPE_ANALOG_RIGHT)
2013-09-05 15:38:00 +00:00
{
if (desc->hitbox != OVERLAY_HITBOX_RADIAL)
{
RARCH_ERR("[Overlay]: Analog hitbox type must be \"radial\".\n");
ret = false;
goto end;
}
char overlay_analog_saturate_key[64];
2014-09-02 14:50:28 +00:00
snprintf(overlay_analog_saturate_key, sizeof(overlay_analog_saturate_key),
2014-10-20 18:31:00 +00:00
"overlay%u_desc%u_saturate_pct", ol_idx, desc_idx);
2014-09-02 14:50:28 +00:00
if (!config_get_float(conf, overlay_analog_saturate_key,
&desc->analog_saturate_pct))
desc->analog_saturate_pct = 1.0f;
2013-09-05 15:38:00 +00:00
}
desc->range_x = (float)strtod(list->elems[4].data, NULL) * width_mod;
desc->range_y = (float)strtod(list->elems[5].data, NULL) * height_mod;
desc->mod_x = desc->x - desc->range_x;
desc->mod_w = 2.0f * desc->range_x;
desc->mod_y = desc->y - desc->range_y;
desc->mod_h = 2.0f * desc->range_y;
2012-12-22 12:27:00 +00:00
2014-09-02 14:50:28 +00:00
snprintf(conf_key, sizeof(conf_key),
2014-10-20 18:31:00 +00:00
"overlay%u_desc%u_alpha_mod", ol_idx, desc_idx);
2013-10-21 10:42:47 +00:00
desc->alpha_mod = alpha_mod;
config_get_float(conf, conf_key, &desc->alpha_mod);
2014-09-02 14:50:28 +00:00
snprintf(conf_key, sizeof(conf_key),
2014-10-20 18:31:00 +00:00
"overlay%u_desc%u_range_mod", ol_idx, desc_idx);
2013-10-21 10:42:47 +00:00
desc->range_mod = range_mod;
config_get_float(conf, conf_key, &desc->range_mod);
2014-09-02 14:50:28 +00:00
snprintf(conf_key, sizeof(conf_key),
2014-10-20 18:31:00 +00:00
"overlay%u_desc%u_movable", ol_idx, desc_idx);
desc->movable = false;
desc->delta_x = 0.0f;
desc->delta_y = 0.0f;
config_get_bool(conf, conf_key, &desc->movable);
2013-10-21 10:42:47 +00:00
desc->range_x_mod = desc->range_x;
desc->range_y_mod = desc->range_y;
2012-12-22 12:27:00 +00:00
end:
if (conf)
config_file_free(conf);
2012-12-22 12:27:00 +00:00
if (list)
string_list_free(list);
return ret;
}
static bool input_overlay_load_overlay_image(input_overlay_t *ol,
const char *config_path,
2014-10-20 18:31:00 +00:00
struct overlay *overlay, unsigned idx)
2012-12-22 12:27:00 +00:00
{
if (overlay->config.paths.path[0] != '\0')
{
2015-02-23 08:26:35 +00:00
char overlay_resolved_path[PATH_MAX_LENGTH];
struct texture_image img = {0};
fill_pathname_resolve_relative(overlay_resolved_path, config_path,
overlay->config.paths.path, sizeof(overlay_resolved_path));
2015-02-23 06:34:14 +00:00
if (!texture_image_load(&img, overlay_resolved_path))
{
2014-09-02 14:50:28 +00:00
RARCH_ERR("[Overlay]: Failed to load image: %s.\n",
overlay_resolved_path);
ol->loading_status = OVERLAY_IMAGE_TRANSFER_ERROR;
return false;
}
2015-02-23 06:34:14 +00:00
overlay->image = img;
2015-02-23 08:28:15 +00:00
return true;
}
2012-12-22 12:27:00 +00:00
return false;
}
static bool input_overlay_load_overlay(input_overlay_t *ol,
const char *config_path,
struct overlay *overlay, unsigned idx)
{
size_t i;
char conf_key[64], overlay_full_screen_key[64], overlay_descs_key[64];
unsigned descs = 0;
bool normalized = false;
float alpha_mod = 1.0f;
float range_mod = 1.0f;
config_file_t *conf = config_file_new(ol->overlay_path);
if (!conf)
return false;
2013-01-11 15:23:04 +00:00
snprintf(overlay_full_screen_key, sizeof(overlay_full_screen_key),
2014-10-20 18:31:00 +00:00
"overlay%u_full_screen", idx);
2013-01-11 15:23:04 +00:00
overlay->full_screen = false;
config_get_bool(conf, overlay_full_screen_key, &overlay->full_screen);
2014-10-20 18:31:00 +00:00
snprintf(overlay_descs_key, sizeof(overlay_descs_key), "overlay%u_descs", idx);
2012-12-22 12:27:00 +00:00
if (!config_get_uint(conf, overlay_descs_key, &descs))
{
2014-09-02 14:50:28 +00:00
RARCH_ERR("[Overlay]: Failed to read number of descs from config key: %s.\n",
overlay_descs_key);
goto error;
}
2012-12-22 12:27:00 +00:00
overlay->descs = (struct overlay_desc*)calloc(descs, sizeof(*overlay->descs));
2014-09-02 14:50:28 +00:00
2012-12-22 12:27:00 +00:00
if (!overlay->descs)
{
RARCH_ERR("[Overlay]: Failed to allocate descs.\n");
goto error;
}
2012-12-22 12:27:00 +00:00
overlay->size = descs;
2013-10-21 10:42:47 +00:00
snprintf(conf_key, sizeof(conf_key),
2014-10-20 18:31:00 +00:00
"overlay%u_normalized", idx);
2013-10-21 10:42:47 +00:00
config_get_bool(conf, conf_key, &normalized);
2014-10-20 18:31:00 +00:00
snprintf(conf_key, sizeof(conf_key), "overlay%u_alpha_mod", idx);
2013-10-21 10:42:47 +00:00
config_get_float(conf, conf_key, &alpha_mod);
2014-10-20 18:31:00 +00:00
snprintf(conf_key, sizeof(conf_key), "overlay%u_range_mod", idx);
2013-10-21 10:42:47 +00:00
config_get_float(conf, conf_key, &range_mod);
for (i = 0; i < overlay->size; i++)
2012-12-22 12:27:00 +00:00
{
if (!input_overlay_load_desc(ol, &overlay->descs[i], idx, i,
2013-10-21 10:42:47 +00:00
overlay->image.width, overlay->image.height,
normalized, alpha_mod, range_mod))
{
2014-09-02 14:50:28 +00:00
RARCH_ERR("[Overlay]: Failed to load overlay descs for overlay #%u.\n",
(unsigned)i);
goto error;
}
2012-12-22 12:27:00 +00:00
}
2014-09-02 14:50:28 +00:00
/* Precache load image array for simplicity. */
overlay->load_images = (struct texture_image*)
calloc(1 + overlay->size, sizeof(struct texture_image));
if (!overlay->load_images)
{
RARCH_ERR("[Overlay]: Failed to allocate load_images.\n");
goto error;
}
if (overlay->image.pixels)
overlay->load_images[overlay->load_images_size++] = overlay->image;
for (i = 0; i < overlay->size; i++)
{
2015-01-14 22:15:24 +00:00
if (!overlay->descs[i].image.pixels)
continue;
overlay->descs[i].image_index = overlay->load_images_size;
overlay->load_images[overlay->load_images_size++] = overlay->descs[i].image;
}
2013-02-03 22:35:55 +00:00
2014-09-02 14:50:28 +00:00
/* Assume for now that scaling center is in the middle.
* TODO: Make this configurable. */
2013-02-03 22:35:55 +00:00
overlay->block_scale = false;
overlay->center_x = overlay->x + 0.5f * overlay->w;
overlay->center_y = overlay->y + 0.5f * overlay->h;
config_file_free(conf);
2012-12-22 12:27:00 +00:00
return true;
error:
config_file_free(conf);
return false;
2012-12-22 12:27:00 +00:00
}
2014-09-02 14:50:28 +00:00
static ssize_t input_overlay_find_index(const struct overlay *ol,
const char *name, size_t size)
2013-02-23 21:57:39 +00:00
{
size_t i;
2015-02-15 06:01:32 +00:00
if (!ol)
return -1;
for (i = 0; i < size; i++)
2013-02-23 21:57:39 +00:00
{
2015-01-14 22:15:24 +00:00
if (!strcmp(ol[i].name, name))
2013-02-23 21:57:39 +00:00
return i;
}
return -1;
}
2014-09-02 14:50:28 +00:00
static bool input_overlay_resolve_targets(struct overlay *ol,
2014-10-20 18:31:00 +00:00
size_t idx, size_t size)
2013-02-23 21:57:39 +00:00
{
size_t i;
2015-02-15 06:01:32 +00:00
struct overlay *current = NULL;
if (!ol)
return false;
current = (struct overlay*)&ol[idx];
2013-02-23 21:57:39 +00:00
for (i = 0; i < current->size; i++)
2013-02-23 21:57:39 +00:00
{
const char *next = current->descs[i].next_index_name;
2013-02-23 21:57:39 +00:00
if (*next)
{
ssize_t next_idx = input_overlay_find_index(ol, next, size);
if (next_idx < 0)
2013-02-23 21:57:39 +00:00
{
2014-09-02 14:50:28 +00:00
RARCH_ERR("[Overlay]: Couldn't find overlay called: \"%s\".\n",
next);
2013-02-23 21:57:39 +00:00
return false;
}
current->descs[i].next_index = next_idx;
2013-02-23 21:57:39 +00:00
}
else
2014-10-20 18:31:00 +00:00
current->descs[i].next_index = (idx + 1) % size;
2013-02-23 21:57:39 +00:00
}
return true;
}
bool input_overlay_load_overlays_resolve_iterate(input_overlay_t *ol)
{
bool not_done = true;
if (!ol)
return false;
not_done = ol->pos < ol->size;
if (!not_done)
{
ol->state = OVERLAY_STATUS_DEFERRED_DONE;
return true;
}
if (!input_overlay_resolve_targets(ol->overlays, ol->pos, ol->size))
{
RARCH_ERR("[Overlay]: Failed to resolve next targets.\n");
goto error;
}
ol->pos += 1;
return true;
error:
ol->state = OVERLAY_STATUS_DEFERRED_ERROR;
return false;
}
bool input_overlay_load_overlays_iterate(input_overlay_t *ol)
{
bool not_done = true;
if (!ol)
return false;
not_done = ol->pos < ol->size;
if (!not_done)
{
ol->pos = 0;
ol->state = OVERLAY_STATUS_DEFERRED_LOADING_RESOLVE;
return true;
}
switch (ol->loading_status)
{
case OVERLAY_IMAGE_TRANSFER_NONE:
if (!input_overlay_load_overlay_image(ol,
ol->overlay_path, &ol->overlays[ol->pos], ol->pos))
ol->loading_status = OVERLAY_IMAGE_TRANSFER_DONE;
ol->loading_status = OVERLAY_IMAGE_TRANSFER_BUSY;
2015-02-23 06:34:14 +00:00
break;
case OVERLAY_IMAGE_TRANSFER_BUSY:
2015-02-23 06:30:57 +00:00
ol->loading_status = OVERLAY_IMAGE_TRANSFER_DONE;
break;
case OVERLAY_IMAGE_TRANSFER_DONE:
if (!input_overlay_load_overlay(ol,
ol->overlay_path, &ol->overlays[ol->pos], ol->pos))
{
RARCH_ERR("[Overlay]: Failed to load overlay #%u.\n", (unsigned)ol->pos);
goto error;
}
ol->pos += 1;
ol->loading_status = OVERLAY_IMAGE_TRANSFER_NONE;
break;
case OVERLAY_IMAGE_TRANSFER_ERROR:
goto error;
}
return true;
error:
ol->state = OVERLAY_STATUS_DEFERRED_ERROR;
return false;
}
bool input_overlay_load_overlays(input_overlay_t *ol)
2012-12-22 12:27:00 +00:00
{
unsigned i;
config_file_t *conf = NULL;
if (!ol)
return false;
conf = config_file_new(ol->overlay_path);
if (!conf)
2012-12-22 12:27:00 +00:00
{
RARCH_ERR("Failed to load config file: %s.\n", ol->overlay_path);
2012-12-22 12:27:00 +00:00
return false;
}
if (!config_get_uint(conf, "overlays", &ol->config.overlays.size))
2012-12-22 12:27:00 +00:00
{
RARCH_ERR("overlays variable not defined in config.\n");
goto error;
2012-12-22 12:27:00 +00:00
}
if (!ol->config.overlays.size)
goto error;
2012-12-22 12:27:00 +00:00
ol->overlays = (struct overlay*)calloc(
ol->config.overlays.size, sizeof(*ol->overlays));
2012-12-22 12:27:00 +00:00
if (!ol->overlays)
goto error;
2012-12-22 12:27:00 +00:00
ol->size = ol->config.overlays.size;
ol->pos = 0;
2012-12-22 12:27:00 +00:00
for (i = 0; i < ol->size; i++)
{
struct overlay *overlay = &ol->overlays[i];
if (!overlay)
continue;
snprintf(overlay->config.paths.key, sizeof(overlay->config.paths.key),
"overlay%u_overlay", i);
config_get_path(conf, overlay->config.paths.key,
overlay->config.paths.path, sizeof(overlay->config.paths.path));
snprintf(overlay->config.names.key, sizeof(overlay->config.names.key),
"overlay%u_name", i);
config_get_array(conf, overlay->config.names.key,
overlay->name, sizeof(overlay->name));
/* By default, we stretch the overlay out in full. */
overlay->x = overlay->y = 0.0f;
overlay->w = overlay->h = 1.0f;
snprintf(overlay->config.rect.key, sizeof(overlay->config.rect.key),
"overlay%u_rect", i);
if (config_get_array(conf, overlay->config.rect.key,
overlay->config.rect.array, sizeof(overlay->config.rect.array)))
{
struct string_list *list = string_split(overlay->config.rect.array, ", ");
if (!list || list->size < 4)
{
RARCH_ERR("[Overlay]: Failed to split rect \"%s\" into at least four tokens.\n",
overlay->config.rect.array);
string_list_free(list);
goto error;
}
overlay->x = (float)strtod(list->elems[0].data, NULL);
overlay->y = (float)strtod(list->elems[1].data, NULL);
overlay->w = (float)strtod(list->elems[2].data, NULL);
overlay->h = (float)strtod(list->elems[3].data, NULL);
string_list_free(list);
}
}
2015-02-23 05:37:27 +00:00
ol->state = OVERLAY_STATUS_DEFERRED_LOADING;
2013-02-23 21:57:39 +00:00
config_file_free(conf);
return true;
error:
config_file_free(conf);
ol->state = OVERLAY_STATUS_DEFERRED_ERROR;
return false;
2012-12-22 12:27:00 +00:00
}
static void input_overlay_load_active(input_overlay_t *ol,
float opacity)
{
if (!ol)
return;
2015-01-14 22:15:24 +00:00
2014-09-02 14:50:28 +00:00
ol->iface->load(ol->iface_data, ol->active->load_images,
ol->active->load_images_size);
input_overlay_set_alpha_mod(ol, opacity);
input_overlay_set_vertex_geom(ol);
ol->iface->full_screen(ol->iface_data, ol->active->full_screen);
}
bool input_overlay_new_done(input_overlay_t *ol)
2015-02-21 05:47:04 +00:00
{
if (!ol)
return false;
ol->active = &ol->overlays[0];
input_overlay_load_active(ol, ol->deferred.opacity);
input_overlay_enable(ol, ol->deferred.enable);
2015-02-21 05:47:04 +00:00
input_overlay_set_alpha_mod(ol, ol->deferred.opacity);
input_overlay_set_scale_factor(ol, ol->deferred.scale_factor);
2015-02-21 05:47:04 +00:00
ol->next_index = (ol->index + 1) % ol->size;
ol->state = OVERLAY_STATUS_ALIVE;
2015-02-21 05:47:04 +00:00
return true;
}
2015-01-09 23:59:05 +00:00
/**
* input_overlay_new:
2015-01-10 00:03:05 +00:00
* @path : Path to overlay file.
* @enable : Enable the overlay after initializing it?
2015-01-09 23:59:05 +00:00
*
* Creates and initializes an overlay handle.
*
* Returns: Overlay handle on success, otherwise NULL.
**/
input_overlay_t *input_overlay_new(const char *path, bool enable,
float opacity, float scale_factor)
{
input_overlay_t *ol = (input_overlay_t*)calloc(1, sizeof(*ol));
2012-12-20 19:23:53 +00:00
if (!ol)
goto error;
2015-01-09 23:59:05 +00:00
ol->overlay_path = strdup(path);
if (!ol->overlay_path)
{
free(ol);
return NULL;
}
if (!driver.video->overlay_interface)
{
RARCH_ERR("Overlay interface is not present in video driver.\n");
goto error;
}
2014-08-15 18:51:59 +00:00
if (driver.video && driver.video->overlay_interface)
driver.video->overlay_interface(driver.video_data, &ol->iface);
ol->iface_data = driver.video_data;
if (!ol->iface)
goto error;
ol->state = OVERLAY_STATUS_DEFERRED_LOAD;
ol->deferred.enable = enable;
ol->deferred.opacity = opacity;
ol->deferred.scale_factor = scale_factor;
2013-01-29 20:53:03 +00:00
return ol;
error:
input_overlay_free(ol);
return NULL;
}
2015-01-09 23:59:05 +00:00
/**
* input_overlay_enable:
* @ol : Overlay handle.
* @enable : Enable or disable the overlay
*
* Enable or disable the overlay.
**/
2012-12-20 14:37:04 +00:00
void input_overlay_enable(input_overlay_t *ol, bool enable)
{
if (!ol)
return;
2012-12-20 14:37:04 +00:00
ol->enable = enable;
ol->iface->enable(ol->iface_data, enable);
}
2015-01-09 23:59:05 +00:00
/**
* inside_hitbox:
* @desc : Overlay descriptor handle.
* @x : X coordinate value.
* @y : Y coordinate value.
*
* Check whether the given @x and @y coordinates of the overlay
* descriptor @desc is inside the overlay descriptor's hitbox.
*
* Returns: true (1) if X, Y coordinates are inside a hitbox, otherwise false (0).
**/
2012-12-22 12:27:00 +00:00
static bool inside_hitbox(const struct overlay_desc *desc, float x, float y)
{
2015-02-15 06:01:32 +00:00
if (!desc)
return false;
2012-12-22 12:27:00 +00:00
switch (desc->hitbox)
{
case OVERLAY_HITBOX_RADIAL:
{
2014-09-02 14:50:28 +00:00
/* Ellipsis. */
2015-01-14 22:15:24 +00:00
float x_dist = (x - desc->x) / desc->range_x_mod;
float y_dist = (y - desc->y) / desc->range_y_mod;
2012-12-22 12:27:00 +00:00
float sq_dist = x_dist * x_dist + y_dist * y_dist;
2015-01-14 22:15:24 +00:00
return (sq_dist <= 1.0f);
2012-12-22 12:27:00 +00:00
}
2012-12-22 12:27:00 +00:00
case OVERLAY_HITBOX_RECT:
2013-10-21 10:42:47 +00:00
return (fabs(x - desc->x) <= desc->range_x_mod) &&
(fabs(y - desc->y) <= desc->range_y_mod);
2012-12-22 12:27:00 +00:00
}
2014-09-15 05:03:54 +00:00
return false;
2012-12-22 12:27:00 +00:00
}
2015-01-09 23:59:05 +00:00
/**
* input_overlay_poll:
* @ol : Overlay handle.
* @out : Polled output data.
* @norm_x : Normalized X coordinate.
* @norm_y : Normalized Y coordinate.
*
* Polls input overlay.
*
* @norm_x and @norm_y are the result of
* input_translate_coord_viewport().
**/
2014-09-02 14:50:28 +00:00
void input_overlay_poll(input_overlay_t *ol, input_overlay_state_t *out,
int16_t norm_x, int16_t norm_y)
{
size_t i;
float x, y;
2013-09-05 22:19:07 +00:00
memset(out, 0, sizeof(*out));
2012-12-20 14:37:04 +00:00
if (!ol->enable)
{
ol->blocked = false;
2013-09-05 22:19:07 +00:00
return;
}
2012-12-20 14:37:04 +00:00
2014-09-02 14:50:28 +00:00
/* norm_x and norm_y is in [-0x7fff, 0x7fff] range,
* like RETRO_DEVICE_POINTER. */
x = (float)(norm_x + 0x7fff) / 0xffff;
y = (float)(norm_y + 0x7fff) / 0xffff;
2013-05-17 22:18:24 +00:00
x -= ol->active->mod_x;
y -= ol->active->mod_y;
x /= ol->active->mod_w;
y /= ol->active->mod_h;
2012-12-22 15:09:15 +00:00
for (i = 0; i < ol->active->size; i++)
{
2015-01-18 08:25:48 +00:00
float x_dist, y_dist;
2013-10-21 10:42:47 +00:00
struct overlay_desc *desc = &ol->active->descs[i];
2015-02-15 06:01:32 +00:00
if (!desc)
continue;
2013-10-21 10:42:47 +00:00
if (!inside_hitbox(desc, x, y))
2013-09-05 15:38:00 +00:00
continue;
desc->updated = true;
2015-01-18 08:25:48 +00:00
x_dist = x - desc->x;
y_dist = y - desc->y;
2013-10-21 10:42:47 +00:00
if (desc->type == OVERLAY_TYPE_BUTTONS)
2013-02-23 21:57:39 +00:00
{
2013-10-21 10:42:47 +00:00
uint64_t mask = desc->key_mask;
2013-09-05 22:19:07 +00:00
out->buttons |= mask;
2013-02-23 21:57:39 +00:00
if (mask & (UINT64_C(1) << RARCH_OVERLAY_NEXT))
2013-10-21 10:42:47 +00:00
ol->next_index = desc->next_index;
2013-02-23 21:57:39 +00:00
}
else if (desc->type == OVERLAY_TYPE_KEYBOARD)
{
if (desc->key_mask < RETROK_LAST)
OVERLAY_SET_KEY(out, desc->key_mask);
}
2013-09-05 15:38:00 +00:00
else
{
2015-01-18 08:25:48 +00:00
float x_val = x_dist / desc->range_x;
float y_val = y_dist / desc->range_y;
float x_val_sat = x_val / desc->analog_saturate_pct;
float y_val_sat = y_val / desc->analog_saturate_pct;
2013-10-21 10:42:47 +00:00
unsigned int base = (desc->type == OVERLAY_TYPE_ANALOG_RIGHT) ? 2 : 0;
2015-01-14 22:15:24 +00:00
out->analog[base + 0] = clamp_float(x_val_sat, -1.0f, 1.0f) * 32767.0f;
out->analog[base + 1] = clamp_float(y_val_sat, -1.0f, 1.0f) * 32767.0f;
}
if (desc->movable)
{
desc->delta_x = clamp_float(x_dist, -desc->range_x, desc->range_x)
2014-09-02 14:50:28 +00:00
* ol->active->mod_w;
desc->delta_y = clamp_float(y_dist, -desc->range_y, desc->range_y)
2014-09-02 14:50:28 +00:00
* ol->active->mod_h;
2013-09-05 15:38:00 +00:00
}
}
2013-09-05 22:19:07 +00:00
if (!out->buttons)
ol->blocked = false;
else if (ol->blocked)
2013-09-05 22:19:07 +00:00
memset(out, 0, sizeof(*out));
}
2015-01-18 08:25:48 +00:00
/**
* input_overlay_update_desc_geom:
* @ol : overlay handle.
* @desc : overlay descriptors handle.
*
* Update input overlay descriptors' vertex geometry.
**/
2014-09-02 14:50:28 +00:00
static void input_overlay_update_desc_geom(input_overlay_t *ol,
struct overlay_desc *desc)
{
2015-02-15 06:01:32 +00:00
if (!desc || !desc->image.pixels)
2015-01-18 08:25:48 +00:00
return;
if (!desc->movable)
return;
2015-01-18 08:25:48 +00:00
ol->iface->vertex_geom(ol->iface_data, desc->image_index,
desc->mod_x + desc->delta_x, desc->mod_y + desc->delta_y,
desc->mod_w, desc->mod_h);
desc->delta_x = 0.0f;
desc->delta_y = 0.0f;
}
2015-01-09 23:59:05 +00:00
/**
* input_overlay_post_poll:
* @ol : overlay handle
*
* Called after all the input_overlay_poll() calls to
* update the range modifiers for pressed/unpressed regions
* and alpha mods.
**/
void input_overlay_post_poll(input_overlay_t *ol, float opacity)
{
size_t i;
2015-02-15 05:57:02 +00:00
if (!ol)
return;
input_overlay_set_alpha_mod(ol, opacity);
for (i = 0; i < ol->active->size; i++)
{
struct overlay_desc *desc = &ol->active->descs[i];
if (!desc)
continue;
2015-01-14 22:15:24 +00:00
desc->range_x_mod = desc->range_x;
desc->range_y_mod = desc->range_y;
if (desc->updated)
{
2014-09-02 14:50:28 +00:00
/* If pressed this frame, change the hitbox. */
2015-01-14 22:15:24 +00:00
desc->range_x_mod *= desc->range_mod;
desc->range_y_mod *= desc->range_mod;
if (desc->image.pixels)
ol->iface->set_alpha(ol->iface_data, desc->image_index,
desc->alpha_mod * opacity);
}
input_overlay_update_desc_geom(ol, desc);
desc->updated = false;
}
}
2015-01-09 23:59:05 +00:00
/**
* input_overlay_poll_clear:
* @ol : overlay handle
*
* Call when there is nothing to poll. Allows overlay to
* clear certain state.
**/
void input_overlay_poll_clear(input_overlay_t *ol, float opacity)
{
size_t i;
2015-02-15 06:01:32 +00:00
if (!ol)
return;
ol->blocked = false;
2015-01-29 21:54:42 +00:00
input_overlay_set_alpha_mod(ol, opacity);
2013-10-21 10:42:47 +00:00
for (i = 0; i < ol->active->size; i++)
2013-10-21 10:42:47 +00:00
{
struct overlay_desc *desc = &ol->active->descs[i];
if (!desc)
continue;
2013-10-21 10:42:47 +00:00
desc->range_x_mod = desc->range_x;
desc->range_y_mod = desc->range_y;
desc->updated = false;
desc->delta_x = 0.0f;
desc->delta_y = 0.0f;
input_overlay_update_desc_geom(ol, desc);
2013-10-21 10:42:47 +00:00
}
}
2015-01-09 23:59:05 +00:00
/**
* input_overlay_next:
* @ol : Overlay handle.
*
* Switch to the next available overlay
* screen.
**/
void input_overlay_next(input_overlay_t *ol, float opacity)
{
2014-10-03 12:53:04 +00:00
if (!ol)
return;
2013-02-23 21:57:39 +00:00
ol->index = ol->next_index;
2012-12-22 12:27:00 +00:00
ol->active = &ol->overlays[ol->index];
input_overlay_load_active(ol, opacity);
ol->blocked = true;
ol->next_index = (ol->index + 1) % ol->size;
2013-01-11 15:23:04 +00:00
}
2015-01-09 23:59:05 +00:00
/**
* input_overlay_full_screen:
* @ol : Overlay handle.
*
* Checks if the overlay is fullscreen.
*
* Returns: true (1) if overlay is fullscreen, otherwise false (0).
**/
2013-01-11 15:23:04 +00:00
bool input_overlay_full_screen(input_overlay_t *ol)
{
if (!ol)
return false;
2013-01-11 15:23:04 +00:00
return ol->active->full_screen;
}
2015-01-09 23:59:05 +00:00
/**
* input_overlay_free:
* @ol : Overlay handle.
*
* Frees overlay handle.
**/
void input_overlay_free(input_overlay_t *ol)
{
if (!ol)
return;
2012-12-22 12:27:00 +00:00
input_overlay_free_overlays(ol);
if (ol->iface)
ol->iface->enable(ol->iface_data, false);
free(ol->overlay_path);
free(ol);
}
2015-01-09 23:59:05 +00:00
/**
* input_overlay_set_alpha_mod:
* @ol : Overlay handle.
* @mod : New modulating factor to apply.
*
* Sets a modulating factor for alpha channel. Default is 1.0.
* The alpha factor is applied for all overlays.
**/
2013-01-29 20:51:15 +00:00
void input_overlay_set_alpha_mod(input_overlay_t *ol, float mod)
{
unsigned i;
2014-09-28 15:52:15 +00:00
if (!ol)
return;
for (i = 0; i < ol->active->load_images_size; i++)
ol->iface->set_alpha(ol->iface_data, i, mod);
2013-01-29 20:51:15 +00:00
}