/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2021 - Daniel De Matteis
* Copyright (C) 2014-2017 - Jean-André Santoni
* Copyright (C) 2016-2019 - Andrés Suárez
* Copyright (C) 2016-2019 - Brad Parker
*
* 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 .
*/
#if defined(HAVE_CONFIG_H)
#include "../config.h"
#endif
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "menu_str.h"
#ifdef WIIU
#include
#endif
#ifdef HAVE_ACCESSIBILITY
#include "../accessibility.h"
#endif
#ifdef HAVE_NETWORKING
#include "../network/netplay/netplay.h"
#endif
#include "../audio/audio_driver.h"
#include "menu_driver.h"
#include "menu_bind_trigger.h"
#include "menu_dirwalk.h"
#include "menu_cbs.h"
#include "../driver.h"
#include "../file_path_special.h"
#include "../list_special.h"
#include "../msg_hash_lbl_str.h"
#include "../paths.h"
#include "../tasks/task_content.h"
#include "../tasks/task_powerstate.h"
#include "../tasks/tasks_internal.h"
#include "../verbosity.h"
#include "../frontend/frontend_driver.h"
#ifdef HAVE_LANGEXTRA
/* This file has a UTF8 BOM, we assume HAVE_LANGEXTRA
* is only enabled for compilers that can support this. */
#include "../input/input_osk_utf8_pages.h"
#endif
#ifdef HAVE_CHEEVOS
#include "../cheevos/cheevos_menu.h"
#endif
#include "../gfx/gfx_animation.h"
#include "../input/input_driver.h"
#include "../input/input_osk.h"
#include "../input/input_remapping.h"
#ifdef HAVE_OPENXR
#include "../input/common/input_openxr.h"
#endif
#include "../performance_counters.h"
#include "../version.h"
#include "../os/lakka/cpufreq/cpufreq.h"
#ifdef HAVE_LIBNX
#include
#include "../os/libnx/switch_performance_profiles.h"
#endif
#ifdef HAVE_MIST
#include "../steam/steam.h"
#endif
#ifdef HAVE_CORE_SIGNATURES
#include "../core_trust_runtime.h"
#endif
#ifdef HAVE_COCOATOUCH
#include "../ui/drivers/cocoa/apple_platform.h"
#include
#endif
typedef struct menu_input_ctx_bind
{
char *s;
size_t len;
} menu_input_ctx_bind_t;
/* Force a helper out of line even though it has a single call site.
* Follows the RXML_NOINLINE precedent in
* libretro-common/formats/xml/rxml.c. Under -Os the compiler already
* optimises for size and the outlining only adds call overhead, so it
* is disabled there. */
#if defined(__OPTIMIZE_SIZE__)
#define MENU_NOINLINE
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
#define MENU_NOINLINE __attribute__((noinline))
#elif defined(_MSC_VER)
#define MENU_NOINLINE __declspec(noinline)
#else
#define MENU_NOINLINE
#endif
#ifdef HAVE_LIBNX
#define LIBNX_SWKBD_LIMIT 500 /* enforced by HOS */
/* TODO/FIXME - public global variable */
extern u32 __nx_applet_type;
void libnx_apply_overclock(void);
#endif
/* Accelerated and latched navigation buttons */
#define NAVIGATION_BUTTONS 12
struct key_desc key_descriptors[RARCH_MAX_KEYS] =
{
{RETROK_FIRST, "Unmapped"},
{RETROK_BACKSPACE, "Backspace"},
{RETROK_TAB, "Tab"},
{RETROK_CLEAR, "Clear"},
{RETROK_RETURN, "Return"},
{RETROK_PAUSE, "Pause"},
{RETROK_ESCAPE, "Escape"},
{RETROK_SPACE, "Space"},
{RETROK_EXCLAIM, "!"},
{RETROK_QUOTEDBL, "\""},
{RETROK_HASH, "#"},
{RETROK_DOLLAR, "$"},
{RETROK_AMPERSAND, "&"},
{RETROK_QUOTE, "\'"},
{RETROK_LEFTPAREN, "("},
{RETROK_RIGHTPAREN, ")"},
{RETROK_ASTERISK, "*"},
{RETROK_PLUS, "+"},
{RETROK_COMMA, ","},
{RETROK_MINUS, "-"},
{RETROK_PERIOD, "."},
{RETROK_SLASH, "/"},
{RETROK_0, "0"},
{RETROK_1, "1"},
{RETROK_2, "2"},
{RETROK_3, "3"},
{RETROK_4, "4"},
{RETROK_5, "5"},
{RETROK_6, "6"},
{RETROK_7, "7"},
{RETROK_8, "8"},
{RETROK_9, "9"},
{RETROK_COLON, ":"},
{RETROK_SEMICOLON, ";"},
{RETROK_LESS, "<"},
{RETROK_EQUALS, "="},
{RETROK_GREATER, ">"},
{RETROK_QUESTION, "?"},
{RETROK_AT, "@"},
{RETROK_LEFTBRACKET, "["},
{RETROK_BACKSLASH, "\\"},
{RETROK_RIGHTBRACKET, "]"},
{RETROK_CARET, "^"},
{RETROK_UNDERSCORE, "_"},
{RETROK_BACKQUOTE, "`"},
{RETROK_a, "A"},
{RETROK_b, "B"},
{RETROK_c, "C"},
{RETROK_d, "D"},
{RETROK_e, "E"},
{RETROK_f, "F"},
{RETROK_g, "G"},
{RETROK_h, "H"},
{RETROK_i, "I"},
{RETROK_j, "J"},
{RETROK_k, "K"},
{RETROK_l, "L"},
{RETROK_m, "M"},
{RETROK_n, "N"},
{RETROK_o, "O"},
{RETROK_p, "P"},
{RETROK_q, "Q"},
{RETROK_r, "R"},
{RETROK_s, "S"},
{RETROK_t, "T"},
{RETROK_u, "U"},
{RETROK_v, "V"},
{RETROK_w, "W"},
{RETROK_x, "X"},
{RETROK_y, "Y"},
{RETROK_z, "Z"},
{RETROK_DELETE, "Delete"},
{RETROK_KP0, "Numpad 0"},
{RETROK_KP1, "Numpad 1"},
{RETROK_KP2, "Numpad 2"},
{RETROK_KP3, "Numpad 3"},
{RETROK_KP4, "Numpad 4"},
{RETROK_KP5, "Numpad 5"},
{RETROK_KP6, "Numpad 6"},
{RETROK_KP7, "Numpad 7"},
{RETROK_KP8, "Numpad 8"},
{RETROK_KP9, "Numpad 9"},
{RETROK_KP_PERIOD, "Numpad ."},
{RETROK_KP_DIVIDE, "Numpad /"},
{RETROK_KP_MULTIPLY, "Numpad *"},
{RETROK_KP_MINUS, "Numpad -"},
{RETROK_KP_PLUS, "Numpad +"},
{RETROK_KP_ENTER, "Numpad Enter"},
{RETROK_KP_EQUALS, "Numpad ="},
{RETROK_UP, "Up"},
{RETROK_DOWN, "Down"},
{RETROK_RIGHT, "Right"},
{RETROK_LEFT, "Left"},
{RETROK_INSERT, "Insert"},
{RETROK_HOME, "Home"},
{RETROK_END, "End"},
{RETROK_PAGEUP, "Page Up"},
{RETROK_PAGEDOWN, "Page Down"},
{RETROK_F1, "F1"},
{RETROK_F2, "F2"},
{RETROK_F3, "F3"},
{RETROK_F4, "F4"},
{RETROK_F5, "F5"},
{RETROK_F6, "F6"},
{RETROK_F7, "F7"},
{RETROK_F8, "F8"},
{RETROK_F9, "F9"},
{RETROK_F10, "F10"},
{RETROK_F11, "F11"},
{RETROK_F12, "F12"},
{RETROK_F13, "F13"},
{RETROK_F14, "F14"},
{RETROK_F15, "F15"},
{RETROK_NUMLOCK, "Num Lock"},
{RETROK_CAPSLOCK, "Caps Lock"},
{RETROK_SCROLLOCK, "Scroll Lock"},
{RETROK_RSHIFT, "Right Shift"},
{RETROK_LSHIFT, "Left Shift"},
{RETROK_RCTRL, "Right Control"},
{RETROK_LCTRL, "Left Control"},
{RETROK_RALT, "Right Alt"},
{RETROK_LALT, "Left Alt"},
{RETROK_RMETA, "Right Meta"},
{RETROK_LMETA, "Left Meta"},
{RETROK_RSUPER, "Right Super"},
{RETROK_LSUPER, "Left Super"},
{RETROK_MODE, "Mode"},
{RETROK_COMPOSE, "Compose"},
{RETROK_HELP, "Help"},
{RETROK_PRINT, "Print"},
{RETROK_SYSREQ, "Sys Req"},
{RETROK_BREAK, "Break"},
{RETROK_MENU, "Menu"},
{RETROK_POWER, "Power"},
{RETROK_EURO, {-30, -126, -84, 0}}, /* " " */
{RETROK_UNDO, "Undo"},
{RETROK_OEM_102, "OEM-102"},
{RETROK_BROWSER_BACK, "Back"},
{RETROK_BROWSER_FORWARD, "Forward"},
{RETROK_BROWSER_REFRESH, "Refresh"},
{RETROK_BROWSER_STOP, "Stop"},
{RETROK_BROWSER_SEARCH, "Search"},
{RETROK_BROWSER_FAVORITES, "Favorites"},
{RETROK_BROWSER_HOME, "Home Page"},
{RETROK_VOLUME_MUTE, "Mute"},
{RETROK_VOLUME_DOWN, "Volume Up"},
{RETROK_VOLUME_UP, "Volume Down"},
{RETROK_MEDIA_NEXT, "Next Track"},
{RETROK_MEDIA_PREV, "Previous Track"},
{RETROK_MEDIA_STOP, "Media Stop"},
{RETROK_MEDIA_PLAY_PAUSE, "Media Play"},
{RETROK_LAUNCH_MAIL, "Launch Email"},
{RETROK_LAUNCH_MEDIA, "Launch Media"},
{RETROK_LAUNCH_APP1, "Launch App1"},
{RETROK_LAUNCH_APP2, "Launch App2"}
};
static void *null_menu_init(void **userdata, bool video_is_threaded)
{
menu_handle_t *menu = (menu_handle_t*)calloc(1, sizeof(*menu));
if (!menu)
return NULL;
return menu;
}
static int null_menu_list_bind_init(menu_file_list_cbs_t *cbs,
const char *path, const char *label, unsigned type, size_t idx) { return 0; }
static menu_ctx_driver_t menu_ctx_null = {
NULL, /* set_texture */
NULL, /* render_messagebox */
NULL, /* render */
NULL, /* frame */
null_menu_init,
NULL, /* free */
NULL, /* context_reset */
NULL, /* context_destroy */
NULL, /* populate_entries */
NULL, /* toggle */
NULL, /* navigation_clear */
NULL, /* navigation_decrement */
NULL, /* navigation_increment */
NULL, /* navigation_set */
NULL, /* navigation_set_last */
NULL, /* navigation_descend_alphabet */
NULL, /* navigation_ascend_alphabet */
NULL, /* lists_init */
NULL, /* list_insert */
NULL, /* list_prepend */
NULL, /* list_delete */
NULL, /* list_clear */
NULL, /* list_cache */
NULL, /* list_push */
NULL, /* list_get_selection */
NULL, /* list_get_size */
NULL, /* list_get_entry */
NULL, /* list_set_selection */
null_menu_list_bind_init,
NULL, /* load_image */
"null",
NULL, /* environ */
NULL, /* update_thumbnail_path */
NULL, /* update_thumbnail_image */
NULL, /* refresh_thumbnail_image */
NULL, /* set_thumbnail_content */
NULL, /* osk_ptr_at_pos */
NULL, /* osk_pointer_over_textbox */
NULL, /* update_savestate_thumbnail_path */
NULL, /* update_savestate_thumbnail_image */
NULL, /* pointer_down */
NULL, /* pointer_up */
NULL /* entry_action */
};
/* Menu drivers */
const menu_ctx_driver_t *menu_ctx_drivers[] = {
#if defined(HAVE_MATERIALUI)
&menu_ctx_mui,
#endif
#if defined(HAVE_OZONE)
&menu_ctx_ozone,
#endif
#if defined(HAVE_RGUI)
&menu_ctx_rgui,
#endif
#if defined(HAVE_XMB)
&menu_ctx_xmb,
#endif
&menu_ctx_null,
NULL
};
static struct menu_state menu_driver_state = { 0 };
static bool menu_pointer_outside_press;
static char *menu_media_launch_path;
static bool menu_media_launch_browsing;
static bool menu_media_launch_activate;
static unsigned menu_media_launch_serial;
static retro_atomic_int_t menu_media_startup = RETRO_ATOMIC_INT_INITIALIZER(0);
/* menu_file_list_cbs_t is fixed-size and allocated once per appended
* entry, so a playlist of any size used to cost one malloc() and one
* free() per row on top of the two strdup()s file_list_append() already
* does. A pool turns that into a free-list pop, and turns a rebuild of
* a list of the same size into no calls into libc at all.
*
* One pool serves every menu list. It is lazily grown, so a build
* without HAVE_MENU reaching this file costs nothing, and it is torn
* down in RARCH_MENU_CTL_DEINIT after menu_list_free() has released
* every entry back to it. */
static mempool_t menu_cbs_pool;
static bool menu_cbs_pool_ready = false;
static menu_file_list_cbs_t *menu_cbs_alloc(void)
{
if (!menu_cbs_pool_ready)
{
/* 256 blocks is ~34 KiB for the first chunk, which covers an
* ordinary settings page outright; chunks double from there, so
* a 100k-row playlist still reaches libc a couple of dozen
* times rather than 100k. */
mempool_init(&menu_cbs_pool, sizeof(menu_file_list_cbs_t), 256);
menu_cbs_pool_ready = true;
}
{
menu_file_list_cbs_t *cbs =
(menu_file_list_cbs_t*)mempool_alloc(&menu_cbs_pool);
/* Pool blocks retain their previous contents. */
if (cbs)
cbs->file_extension_state = MENU_FILE_BROWSER_EXTENSION_STATE_FULL;
return cbs;
}
}
static void menu_cbs_pool_deinit(void)
{
if (!menu_cbs_pool_ready)
return;
mempool_deinit(&menu_cbs_pool);
menu_cbs_pool_ready = false;
}
struct menu_state *menu_state_get_ptr(void)
{
return &menu_driver_state;
}
static void menu_dialog_press(struct menu_state *menu_st, int idx);
#ifdef HAVE_CORE_SIGNATURES
static const char *menu_core_warning_text(void);
#endif
static bool menu_should_pop_stack(const char *label)
{
/* > Info box */
if (string_is_equal(label, MENU_ENUM_LABEL_INFO_SCREEN_STR))
return true;
/* > Help box */
if (string_starts_with_size(label, "help", STRLEN_CONST("help")))
if (
string_is_equal(label, MENU_ENUM_LABEL_HELP_STR)
|| string_is_equal(label, msg_hash_to_str(MENU_ENUM_LABEL_CHEEVOS_DESCRIPTION)))
return true;
if (
string_is_equal(label, msg_hash_to_str(MENU_ENUM_LABEL_CHEEVOS_DESCRIPTION)))
return true;
return false;
}
/**
* menu_file_browser_stem_length:
*
* Length of @path up to (not including) the dot that starts its
* extension, or strlen(@path) when there is no extension. A sole
* leading dot in the basename (".gitignore") is part of the name.
**/
size_t menu_file_browser_stem_length(const char *path)
{
const char *name = path_basename(path);
const char *ext = path_get_extension(path);
return (*ext && ext > name + 1)
? (size_t)(ext - path - 1) : strlen(path);
}
/* Rewrites the drawn label only; the entry path is left alone. */
static void menu_file_browser_format_display_name(const char *path,
uint8_t state, char *s, size_t len)
{
size_t _len;
size_t stem_len;
if ( !path || !*path || !len
|| state == MENU_FILE_BROWSER_EXTENSION_STATE_FULL)
return;
stem_len = menu_file_browser_stem_length(path);
_len = (stem_len < len) ? stem_len : len - 1;
strlcpy(s, path, _len + 1);
if (state != MENU_FILE_BROWSER_EXTENSION_STATE_HINT)
return;
_len += strlcpy(s + _len, " (", len - _len);
if (_len >= len)
return;
_len += strlcpy(s + _len, path[stem_len]
? path + stem_len + 1
: msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NO_EXTENSION),
len - _len);
if (_len >= len)
return;
strlcpy(s + _len, ")", len - _len);
}
size_t menu_entries_restorable_selection(const file_list_t *list,
size_t selection)
{
size_t i;
const char *loading;
if (!list || selection < list->size)
return selection;
loading = msg_hash_to_str(MSG_LOADING);
for (i = 0; i < list->size; i++)
{
if ( ( list->list[i].type == MENU_SETTING_NO_ITEM
&& string_is_equal(list->list[i].path, loading))
|| string_is_equal(list->list[i].label,
MENU_ENUM_LABEL_EXPLORE_INITIALISING_LIST_STR))
return selection;
}
return list->size ? list->size - 1 : 0;
}
void menu_entry_get(menu_entry_t *entry, size_t stack_idx,
size_t i, void *userdata, bool use_representation)
{
bool path_enabled;
char newpath[NAME_MAX_LENGTH];
const char *path = NULL;
const char *entry_label = NULL;
menu_file_list_cbs_t *cbs = NULL;
struct menu_state *menu_st = &menu_driver_state;
file_list_t *selection_buf = MENU_ENTRIES_GET_SELECTION_BUF_PTR_INTERNAL(menu_st, stack_idx);
file_list_t *list = (userdata) ? (file_list_t*)userdata : selection_buf;
uint8_t entry_flags = entry->flags;
newpath[0] = '\0';
/* i is usually selection_ptr, which can run past a list rebuilt
* shorter before the selection is re-clamped (cf. #18797). */
if (!list || i >= list->size)
return;
path_enabled = (entry_flags & MENU_ENTRY_FLAG_PATH_ENABLED) ? true : false;
path = list->list[i].path;
entry_label = list->list[i].label;
entry->type = list->list[i].type;
entry->entry_idx = list->list[i].entry_idx;
entry->setting_type = 0;
cbs = (menu_file_list_cbs_t*)list->list[i].actiondata;
entry->idx = (unsigned)i;
if ( (entry_flags & MENU_ENTRY_FLAG_LABEL_ENABLED)
&& entry_label && *entry_label)
strlcpy(entry->label, entry_label, sizeof(entry->label));
if (cbs)
{
const char *label = NULL;
file_list_t *menu_stack = MENU_LIST_GET(menu_st->entries.list, 0);
entry->enum_idx = cbs->enum_idx;
if (cbs->setting && cbs->setting->type)
entry->setting_type = cbs->setting->type;
/* Exceptions without cbs->setting->type */
if (!entry->setting_type)
{
switch (entry->type)
{
case MENU_SETTING_ACTION_CORE_LOCK:
case MENU_SETTING_ACTION_CORE_SIGNATURE_CHECK:
entry->setting_type = ST_BOOL;
break;
default:
break;
}
}
if (cbs->checked)
entry->flags |= MENU_ENTRY_FLAG_CHECKED;
if (menu_stack && menu_stack->size)
label = menu_stack->list[menu_stack->size - 1].label;
if ( (entry_flags & MENU_ENTRY_FLAG_RICH_LABEL_ENABLED)
&& cbs->action_label)
{
cbs->action_label(list,
entry->type, (unsigned)i,
label, path,
entry->rich_label,
sizeof(entry->rich_label));
menu_file_browser_format_display_name(path,
cbs->file_extension_state, entry->rich_label,
sizeof(entry->rich_label));
if (!path_enabled && !*entry->rich_label)
path_enabled = true;
}
if ( (path_enabled
|| (entry_flags & MENU_ENTRY_FLAG_VALUE_ENABLED))
&& cbs->action_get_value
&& use_representation)
{
cbs->action_get_value(list,
&entry->spacing, entry->type,
(unsigned)i, label,
entry->value,
(entry_flags & MENU_ENTRY_FLAG_VALUE_ENABLED)
? sizeof(entry->value)
: 0,
path,
newpath,
path_enabled ? sizeof(newpath) : 0);
if (*entry->value)
{
if (entry->enum_idx == MENU_ENUM_LABEL_CHEEVOS_PASSWORD)
{
size_t j;
size_t _len = strlcpy(entry->password_value, entry->value,
sizeof(entry->password_value));
for (j = 0; j < _len; j++)
entry->password_value[j] = '*';
}
}
}
if ( cbs->action_sublabel
&& (entry_flags & MENU_ENTRY_FLAG_SUBLABEL_ENABLED))
cbs->action_sublabel(list,
entry->type, (unsigned)i,
label, path,
entry->sublabel,
sizeof(entry->sublabel));
}
/* Inspect core options and set entries with only 2 options as
* boolean for accurate graphical switch icons */
if ( entry->type >= MENU_SETTINGS_CORE_OPTION_START
&& entry->type < MENU_SETTINGS_CHEEVOS_START)
{
struct core_option *option = NULL;
core_option_manager_t *coreopts = NULL;
size_t option_index = entry->type - MENU_SETTINGS_CORE_OPTION_START;
retroarch_ctl(RARCH_CTL_CORE_OPTIONS_LIST_GET, &coreopts);
if (coreopts)
option = (struct core_option*)&coreopts->opts[option_index];
if (option && option->vals && option->vals->size == 2)
entry->setting_type = ST_BOOL;
}
if (path_enabled)
{
if (path && *path && !use_representation)
strlcpy(entry->path, path, sizeof(entry->path));
else if (
cbs
&& cbs->setting
&& cbs->setting->enum_value_idx != MSG_UNKNOWN
&& !(cbs->setting->flags
& SD_FLAG_DONT_USE_ENUM_IDX_REPRESENTATION))
strlcpy(entry->path,
msg_hash_to_str(cbs->setting->enum_value_idx),
sizeof(entry->path));
else
if (*newpath)
strlcpy(entry->path, newpath, sizeof(entry->path));
}
}
static menu_search_terms_t *menu_entries_search_get_terms_internal(void)
{
struct menu_state *menu_st = &menu_driver_state;
file_list_t *list = MENU_LIST_GET(menu_st->entries.list, 0);
if (list && (list->size >= 1))
{
menu_file_list_cbs_t *cbs = NULL;
if ((cbs = (menu_file_list_cbs_t*)list->list[list->size - 1].actiondata))
return cbs->search;
}
return NULL;
}
/* As above, but allocates the block on first use. Only
* menu_entries_search_push() needs this; every other caller is happy
* to see NULL and treat it as "no search active". */
static menu_search_terms_t *menu_entries_search_get_terms_alloc(void)
{
struct menu_state *menu_st = &menu_driver_state;
file_list_t *list = MENU_LIST_GET(menu_st->entries.list, 0);
if (list && (list->size >= 1))
{
menu_file_list_cbs_t *cbs = NULL;
if ((cbs = (menu_file_list_cbs_t*)list->list[list->size - 1].actiondata))
{
if (!cbs->search)
cbs->search = (menu_search_terms_t*)
calloc(1, sizeof(*cbs->search));
return cbs->search;
}
}
return NULL;
}
/* file_list_t::actiondata_free hook for every list the menu owns.
* menu_file_list_cbs_t owns a further allocation now, so it can no
* longer be torn down with a plain free(); routing it through the
* hook means the seven call sites that can destroy a menu list --
* menu_list_free_list(), menu_entries_clear(), the two append paths,
* menu_driver_list_free(), xmb_list_free() and ozone_list_free() --
* do not each have to know that. */
void menu_entries_cbs_free(void *actiondata)
{
menu_file_list_cbs_t *cbs = (menu_file_list_cbs_t*)actiondata;
if (!cbs)
return;
if (cbs->search)
free(cbs->search);
cbs->search = NULL;
mempool_free(&menu_cbs_pool, cbs);
}
/* Searches current menu list for specified 'needle'
* string. If string is found, returns true and sets
* 'idx' to the matching list entry index. */
bool menu_entries_list_search(const char *needle, size_t *idx)
{
size_t i;
struct menu_state *menu_st = &menu_driver_state;
menu_list_t *menu_list = menu_st->entries.list;
file_list_t *list = MENU_LIST_GET_SELECTION(menu_list, (unsigned)0);
bool match_found = false;
bool char_search = false;
char needle_char = 0;
if ( !list
|| (!needle || !*needle)
|| !idx)
return false;
/* Check if we are searching for a single
* Latin alphabet character */
char_search = ((needle[1] == '\0') && (ISALPHA(needle[0])));
if (char_search)
needle_char = TOLOWER(needle[0]);
for (i = 0; i < list->size; i++)
{
const char *entry_label = NULL;
menu_entry_t entry;
/* Note the we have to get the actual menu
* entry here, since we need the exact label
* that is currently displayed by the menu
* driver */
MENU_ENTRY_INITIALIZE(entry);
entry.flags |= MENU_ENTRY_FLAG_PATH_ENABLED
| MENU_ENTRY_FLAG_LABEL_ENABLED
| MENU_ENTRY_FLAG_RICH_LABEL_ENABLED;
menu_entry_get(&entry, 0, i, NULL, true);
/* When using the file browser, one or more
* 'utility' entries will be added to the top
* of the list (e.g. 'Parent Directory'). These
* have no bearing on the actual content of the
* list, and should be excluded from the search */
if ( (entry.type == FILE_TYPE_SCAN_DIRECTORY)
|| (entry.type == FILE_TYPE_MANUAL_SCAN_DIRECTORY)
|| (entry.type == FILE_TYPE_USE_DIRECTORY)
|| (entry.type == FILE_TYPE_PARENT_DIRECTORY))
continue;
/* Get displayed entry label */
if (*entry.rich_label)
entry_label = entry.rich_label;
else
entry_label = entry.path;
if (!entry_label || !*entry_label)
continue;
/* If we are performing a single character
* search, jump to the first entry whose
* first character matches */
if (char_search)
{
if (needle_char == TOLOWER(entry_label[0]))
{
*idx = i;
match_found = true;
break;
}
}
/* Otherwise perform an exhaustive string
* comparison */
else
{
const char *found_str = (const char *)compat_strcasestr(entry_label, needle);
/* Found a match with the first characters
* of the label -> best possible match,
* so quit immediately */
if (found_str == entry_label)
{
*idx = i;
match_found = true;
break;
}
/* Found a mid-string match; this is a valid
* result, but keep searching for the best
* possible match */
else if (found_str)
{
*idx = i;
match_found = true;
}
}
}
return match_found;
}
/* Display the date and time - time_mode will influence how
* the time representation will look like.
* */
size_t menu_display_timedate(gfx_display_ctx_datetime_t *datetime,
char *s, size_t len)
{
/* Storage container for current menu datetime
* representation string */
static char datetime_cache[NAME_MAX_LENGTH];
static retro_time_t datetime_last_time_us;
struct menu_state *menu_st = &menu_driver_state;
/* Trigger an update, if required */
if ( menu_st->current_time_us - datetime_last_time_us >=
DATETIME_CHECK_INTERVAL
|| datetime_last_time_us == 0)
{
time_t time_;
struct tm tm_;
bool has_am_pm = false;
const char *format_str = "";
datetime_last_time_us = menu_st->current_time_us;
/* Get current time */
time(&time_);
rtime_localtime(&time_, &tm_);
/* Format string representation */
switch (datetime->time_mode)
{
case MENU_TIMEDATE_STYLE_YMD_HMS: /* YYYY-MM-DD HH:MM:SS */
/* Using switch statements to set the format
* string is verbose, but has far less performance
* impact than setting the date separator dynamically
* (i.e. no snprintf() or character replacement...) */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%Y/%m/%d %H:%M:%S";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%Y.%m.%d %H:%M:%S";
break;
default:
format_str = "%Y-%m-%d %H:%M:%S";
break;
}
break;
case MENU_TIMEDATE_STYLE_YMD_HM: /* YYYY-MM-DD HH:MM */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%Y/%m/%d %H:%M";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%Y.%m.%d %H:%M";
break;
default:
format_str = "%Y-%m-%d %H:%M";
break;
}
break;
case MENU_TIMEDATE_STYLE_YMD: /* YYYY-MM-DD */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%Y/%m/%d";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%Y.%m.%d";
break;
default:
format_str = "%Y-%m-%d";
break;
}
break;
case MENU_TIMEDATE_STYLE_YM: /* YYYY-MM */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%Y/%m";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%Y.%m";
break;
default:
format_str = "%Y-%m";
break;
}
break;
case MENU_TIMEDATE_STYLE_MDYYYY_HMS: /* MM-DD-YYYY HH:MM:SS */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%m/%d/%Y %H:%M:%S";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%m.%d.%Y %H:%M:%S";
break;
default:
format_str = "%m-%d-%Y %H:%M:%S";
break;
}
break;
case MENU_TIMEDATE_STYLE_MDYYYY_HM: /* MM-DD-YYYY HH:MM */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%m/%d/%Y %H:%M";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%m.%d.%Y %H:%M";
break;
default:
format_str = "%m-%d-%Y %H:%M";
break;
}
break;
case MENU_TIMEDATE_STYLE_MD_HM: /* MM-DD HH:MM */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%m/%d %H:%M";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%m.%d %H:%M";
break;
default:
format_str = "%m-%d %H:%M";
break;
}
break;
case MENU_TIMEDATE_STYLE_MDYYYY: /* MM-DD-YYYY */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%m/%d/%Y";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%m.%d.%Y";
break;
default:
format_str = "%m-%d-%Y";
break;
}
break;
case MENU_TIMEDATE_STYLE_MD: /* MM-DD */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%m/%d";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%m.%d";
break;
default:
format_str = "%m-%d";
break;
}
break;
case MENU_TIMEDATE_STYLE_DDMMYYYY_HMS: /* DD-MM-YYYY HH:MM:SS */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%d/%m/%Y %H:%M:%S";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%d.%m.%Y %H:%M:%S";
break;
default:
format_str = "%d-%m-%Y %H:%M:%S";
break;
}
break;
case MENU_TIMEDATE_STYLE_DDMMYYYY_HM: /* DD-MM-YYYY HH:MM */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%d/%m/%Y %H:%M";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%d.%m.%Y %H:%M";
break;
default:
format_str = "%d-%m-%Y %H:%M";
break;
}
break;
case MENU_TIMEDATE_STYLE_DDMM_HM: /* DD-MM HH:MM */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%d/%m %H:%M";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%d.%m %H:%M";
break;
default:
format_str = "%d-%m %H:%M";
break;
}
break;
case MENU_TIMEDATE_STYLE_DDMMYYYY: /* DD-MM-YYYY */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%d/%m/%Y";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%d.%m.%Y";
break;
default:
format_str = "%d-%m-%Y";
break;
}
break;
case MENU_TIMEDATE_STYLE_DDMM: /* DD-MM */
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%d/%m";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%d.%m";
break;
default:
format_str = "%d-%m";
break;
}
break;
case MENU_TIMEDATE_STYLE_HMS: /* HH:MM:SS */
format_str = "%H:%M:%S";
break;
case MENU_TIMEDATE_STYLE_HM: /* HH:MM */
format_str = "%H:%M";
break;
case MENU_TIMEDATE_STYLE_YMD_HMS_AMPM: /* YYYY-MM-DD HH:MM:SS (AM/PM) */
has_am_pm = true;
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%Y/%m/%d %I:%M:%S %p";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%Y.%m.%d %I:%M:%S %p";
break;
default:
format_str = "%Y-%m-%d %I:%M:%S %p";
break;
}
break;
case MENU_TIMEDATE_STYLE_YMD_HM_AMPM: /* YYYY-MM-DD HH:MM (AM/PM) */
has_am_pm = true;
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%Y/%m/%d %I:%M %p";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%Y.%m.%d %I:%M %p";
break;
default:
format_str = "%Y-%m-%d %I:%M %p";
break;
}
break;
case MENU_TIMEDATE_STYLE_MDYYYY_HMS_AMPM: /* MM-DD-YYYY HH:MM:SS (AM/PM) */
has_am_pm = true;
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%m/%d/%Y %I:%M:%S %p";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%m.%d.%Y %I:%M:%S %p";
break;
default:
format_str = "%m-%d-%Y %I:%M:%S %p";
break;
}
break;
case MENU_TIMEDATE_STYLE_MDYYYY_HM_AMPM: /* MM-DD-YYYY HH:MM (AM/PM) */
has_am_pm = true;
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%m/%d/%Y %I:%M %p";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%m.%d.%Y %I:%M %p";
break;
default:
format_str = "%m-%d-%Y %I:%M %p";
break;
}
break;
case MENU_TIMEDATE_STYLE_MD_HM_AMPM: /* MM-DD HH:MM (AM/PM) */
has_am_pm = true;
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%m/%d %I:%M %p";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%m.%d %I:%M %p";
break;
default:
format_str = "%m-%d %I:%M %p";
break;
}
break;
case MENU_TIMEDATE_STYLE_DDMMYYYY_HMS_AMPM: /* DD-MM-YYYY HH:MM:SS (AM/PM) */
has_am_pm = true;
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%d/%m/%Y %I:%M:%S %p";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%d.%m.%Y %I:%M:%S %p";
break;
default:
format_str = "%d-%m-%Y %I:%M:%S %p";
break;
}
break;
case MENU_TIMEDATE_STYLE_DDMMYYYY_HM_AMPM: /* DD-MM-YYYY HH:MM (AM/PM) */
has_am_pm = true;
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%d/%m/%Y %I:%M %p";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%d.%m.%Y %I:%M %p";
break;
default:
format_str = "%d-%m-%Y %I:%M %p";
break;
}
break;
case MENU_TIMEDATE_STYLE_DDMM_HM_AMPM: /* DD-MM HH:MM (AM/PM) */
has_am_pm = true;
switch (datetime->date_separator)
{
case MENU_TIMEDATE_DATE_SEPARATOR_SLASH:
format_str = "%d/%m %I:%M %p";
break;
case MENU_TIMEDATE_DATE_SEPARATOR_PERIOD:
format_str = "%d.%m %I:%M %p";
break;
default:
format_str = "%d-%m %I:%M %p";
break;
}
break;
case MENU_TIMEDATE_STYLE_HMS_AMPM: /* HH:MM:SS (AM/PM) */
has_am_pm = true;
format_str = "%I:%M:%S %p";
break;
case MENU_TIMEDATE_STYLE_HM_AMPM: /* HH:MM (AM/PM) */
has_am_pm = true;
format_str = "%I:%M %p";
break;
}
if (has_am_pm)
strftime_am_pm(datetime_cache, sizeof(datetime_cache),
format_str, &tm_);
else
strftime(datetime_cache, sizeof(datetime_cache),
format_str, &tm_);
}
/* Copy cached datetime string to input
* menu_display_ctx_datetime_t struct */
return strlcpy(s, datetime_cache, len);
}
/* Display current (battery) power state */
size_t menu_display_powerstate(gfx_display_ctx_powerstate_t *powerstate,
char *s, size_t len)
{
static retro_time_t powerstate_last_time_us;
int percent = 0;
struct menu_state *menu_st = &menu_driver_state;
enum frontend_powerstate state = FRONTEND_POWERSTATE_NONE;
/* Trigger an update, if required */
if ( menu_st->current_time_us - powerstate_last_time_us >=
POWERSTATE_CHECK_INTERVAL
|| powerstate_last_time_us == 0)
{
powerstate_last_time_us = menu_st->current_time_us;
task_push_get_powerstate();
}
/* Get last recorded state */
state = get_last_powerstate(&percent);
/* Populate gfx_display_ctx_powerstate_t */
powerstate->battery_enabled = (state != FRONTEND_POWERSTATE_NONE) &&
(state != FRONTEND_POWERSTATE_NO_SOURCE);
powerstate->percent = 0;
powerstate->charging = false;
if (powerstate->battery_enabled)
{
if (state == FRONTEND_POWERSTATE_CHARGING)
powerstate->charging = true;
if (percent > 0)
powerstate->percent = (unsigned)percent;
return snprintf(s, len, "%u%%", powerstate->percent);
}
return 0;
}
/* Sets title to what the name of the current menu should be. */
size_t menu_entries_get_title(char *s, size_t len)
{
struct menu_state *menu_st = &menu_driver_state;
menu_handle_t *menu = menu_st->driver_data;
const file_list_t *list = menu_st->entries.list
? MENU_LIST_GET(menu_st->entries.list, 0)
: NULL;
menu_file_list_cbs_t *cbs = list
? (menu_file_list_cbs_t*)list->list[list->size - 1].actiondata
: NULL;
if (cbs && cbs->action_get_title)
{
const char *label = (list->size) ? list->list[list->size - 1].label : NULL;
/* Show playlist entry instead of "Quick Menu" */
if (string_is_equal(label, MENU_ENUM_LABEL_DEFERRED_RPL_ENTRY_ACTIONS_STR))
{
playlist_t *playlist = playlist_get_cached();
if (playlist)
{
const struct playlist_entry *entry = NULL;
playlist_get_index(playlist, menu->rpl_entry_selection_ptr, &entry);
if (entry)
{
char entry_path[NAME_MAX_LENGTH];
strlcpy(entry_path, entry->path, sizeof(entry_path));
path_remove_extension(entry_path);
return strlcpy(s,
(entry->label && *entry->label) ? entry->label : path_basename(entry_path),
len);
}
}
}
else
{
const char *path = NULL;
unsigned menu_type = 0;
if ( string_is_equal(label, MENU_ENUM_LABEL_CONTENT_SETTINGS_STR)
&& !path_is_empty(RARCH_PATH_CONTENT))
{
char content_label[NAME_MAX_LENGTH];
strlcpy(content_label, path_get(RARCH_PATH_CONTENT), sizeof(content_label));
path_remove_extension(content_label);
return strlcpy(s, path_basename(content_label), len);
}
if (list->size)
{
path = list->list[list->size - 1].path;
menu_type = list->list[list->size - 1].type;
}
cbs->action_get_title(path, label, menu_type, s, len);
}
}
return 0;
}
/* Used to close an active message box (help or info)
* TODO/FIXME: The way that message boxes are handled
* is complete garbage. generic_menu_iterate() and
* message boxes in general need a total rewrite.
* I consider this current 'close_messagebox' a hack,
* but at least it prevents undefined/dangerous
* behaviour... */
static void menu_input_pointer_close_messagebox(struct menu_state *menu_st)
{
const file_list_t *list = MENU_LIST_GET(menu_st->entries.list, 0);
const char *label = list->list[list->size - 1].label;
#ifdef HAVE_AUDIOMIXER
/* Determine whether this is a help or info
* message box */
if (list && list->size)
{
/* Play sound for closing the info box */
settings_t *settings = config_get_ptr();
bool audio_enable_menu = settings->bools.audio_enable_menu;
bool audio_enable_menu_notice = settings->bools.audio_enable_menu_notice;
if (audio_enable_menu && audio_enable_menu_notice)
audio_driver_mixer_play_menu_sound(AUDIO_MIXER_SYSTEM_SLOT_NOTICE_BACK);
}
#endif
/* Pop stack, if required */
if (menu_should_pop_stack(label))
{
size_t selection = menu_st->selection_ptr;
size_t new_selection = selection;
menu_entries_pop_stack(&new_selection, 0, 0);
menu_st->selection_ptr = selection;
}
}
static float menu_input_get_dpi(
menu_handle_t *menu,
gfx_display_t *p_disp,
unsigned video_dims)
{
static unsigned last_video_dims = 0;
static float dpi = 0.0f;
static bool dpi_cached = false;
/* Regardless of menu driver, need 'actual' screen DPI
* Note: DPI is a fixed hardware property. To minimise performance
* overheads we therefore only call video_context_driver_get_metrics()
* on first run, or when the current video resolution changes */
if ( (!dpi_cached)
|| (video_dims != last_video_dims))
{
gfx_ctx_metrics_t mets;
/* Note: If video_context_driver_get_metrics() fails,
* we don't know what happened to dpi - so ensure it
* is reset to a sane value */
mets.type = DISPLAY_METRIC_DPI;
mets.value = &dpi;
if (!video_context_driver_get_metrics(&mets))
#ifdef VITA
dpi = 220.0f;
#else
dpi = 0.0f;
#endif
dpi_cached = true;
last_video_dims = video_dims;
}
/* RGUI uses a framebuffer texture, which means we
* operate in menu space, not screen space.
* DPI in a traditional sense is therefore meaningless,
* so generate a substitute value based upon framebuffer
* dimensions */
if (dpi > 0.0f)
{
bool menu_has_fb =
menu->driver_ctx
&& menu->driver_ctx->set_texture;
/* Read framebuffer info? */
if (menu_has_fb)
{
unsigned fb_height = VIDEO_SCALE_H(p_disp->framebuf_dims);
/* Rationale for current 'DPI' determination method:
* - Divide screen height by DPI, to get number of vertical
* '1 inch' squares
* - Divide RGUI framebuffer height by number of vertical
* '1 inch' squares to get number of menu space pixels
* per inch
* This is crude, but should be sufficient... */
return ((float)fb_height / (float)VIDEO_SCALE_H(video_dims)) * dpi;
}
}
return dpi;
}
MENU_NOINLINE static bool input_event_osk_show_symbol_pages(
menu_handle_t *menu)
{
#if defined(HAVE_LANGEXTRA)
#if defined(HAVE_RGUI)
bool menu_has_fb = (menu &&
menu->driver_ctx &&
menu->driver_ctx->set_texture);
unsigned language = *msg_hash_get_uint(MSG_HASH_USER_LANGUAGE);
return (!menu_has_fb)
|| ((language == RETRO_LANGUAGE_JAPANESE)
|| (language == RETRO_LANGUAGE_KOREAN)
|| (language == RETRO_LANGUAGE_CHINESE_SIMPLIFIED)
|| (language == RETRO_LANGUAGE_CHINESE_TRADITIONAL));
#else /* HAVE_RGUI */
return true;
#endif /* HAVE_RGUI */
#else /* HAVE_LANGEXTRA */
return false;
#endif /* HAVE_LANGEXTRA */
}
static void menu_driver_list_free(
const menu_ctx_driver_t *menu_driver_ctx,
menu_ctx_list_t *list)
{
if (menu_driver_ctx)
if (menu_driver_ctx->list_free)
menu_driver_ctx->list_free(
list->list, list->idx, list->list_size);
if (list->list)
{
file_list_free_userdata (list->list, list->idx);
file_list_free_actiondata(list->list, list->idx);
}
}
static void menu_list_free_list(
const menu_ctx_driver_t *menu_driver_ctx,
file_list_t *list)
{
unsigned i;
for (i = 0; i < list->size; i++)
{
menu_ctx_list_t list_info;
list_info.list = list;
list_info.idx = i;
list_info.list_size = list->size;
menu_driver_list_free(menu_driver_ctx, &list_info);
}
file_list_free(list);
}
static void menu_list_pop_stack(const menu_ctx_driver_t *menu_driver_ctx,
void *menu_userdata, menu_list_t *list,
size_t idx, size_t *directory_ptr)
{
file_list_t *menu_list = MENU_LIST_GET(list, (unsigned)idx);
if (menu_list)
{
if (menu_list->size != 0)
{
menu_ctx_list_t list_info;
list_info.list = menu_list;
list_info.idx = menu_list->size - 1;
list_info.list_size = menu_list->size - 1;
menu_driver_list_free(menu_driver_ctx, &list_info);
}
file_list_pop(menu_list, directory_ptr);
if ( menu_driver_ctx &&
menu_driver_ctx->list_set_selection)
menu_driver_ctx->list_set_selection(menu_userdata,
menu_list);
}
}
static int menu_list_flush_stack_type(const char *needle, const char *label,
unsigned type, unsigned final_type)
{
return needle ? !string_is_equal(needle, label) : (type != final_type);
}
static void menu_list_flush_stack(
const menu_ctx_driver_t *menu_driver_ctx,
void *menu_userdata, struct menu_state *menu_st,
menu_list_t *list,
size_t idx, const char *needle, unsigned final_type)
{
const char *label = NULL;
unsigned type = 0;
file_list_t *menu_list = MENU_LIST_GET(list, (unsigned)idx);
menu_st->flags |= MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
menu_contentless_cores_flush_runtime();
if (menu_list && menu_list->size)
{
label = menu_list->list[menu_list->size - 1].label;
type = menu_list->list[menu_list->size - 1].type;
}
while (menu_list_flush_stack_type(
needle, label, type, final_type) != 0)
{
size_t new_selection_ptr = menu_st->selection_ptr;
bool wont_pop_stack = (MENU_LIST_GET_STACK_SIZE(list, idx) <= 1);
if (wont_pop_stack)
break;
if (menu_driver_ctx->list_cache)
menu_driver_ctx->list_cache(menu_userdata,
MENU_LIST_PLAIN, 0);
menu_list_pop_stack(menu_driver_ctx,
menu_userdata,
list, idx, &new_selection_ptr);
menu_st->flags |= MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
menu_st->selection_ptr = new_selection_ptr;
menu_list = MENU_LIST_GET(list, (unsigned)idx);
if (menu_list && menu_list->size)
{
label = menu_list->list[menu_list->size - 1].label;
type = menu_list->list[menu_list->size - 1].type;
}
}
}
static void menu_list_free(const menu_ctx_driver_t *menu_driver_ctx,
menu_list_t *menu_list)
{
if (!menu_list)
return;
if (menu_list->menu_stack)
{
unsigned i;
for (i = 0; i < menu_list->menu_stack_size; i++)
{
if (!menu_list->menu_stack[i])
continue;
menu_list_free_list(menu_driver_ctx,
menu_list->menu_stack[i]);
menu_list->menu_stack[i] = NULL;
}
free(menu_list->menu_stack);
}
if (menu_list->selection_buf)
{
unsigned i;
for (i = 0; i < menu_list->selection_buf_size; i++)
{
if (!menu_list->selection_buf[i])
continue;
menu_list_free_list(menu_driver_ctx,
menu_list->selection_buf[i]);
menu_list->selection_buf[i] = NULL;
}
free(menu_list->selection_buf);
}
free(menu_list);
}
static menu_list_t *menu_list_new(const menu_ctx_driver_t *menu_driver_ctx)
{
unsigned i;
menu_list_t *list = (menu_list_t*)malloc(sizeof(*list));
if (!list)
return NULL;
list->menu_stack_size = 1;
list->selection_buf_size = 1;
list->selection_buf = NULL;
list->menu_stack = (file_list_t**)
calloc(list->menu_stack_size, sizeof(*list->menu_stack));
if (!list->menu_stack)
goto error;
list->selection_buf = (file_list_t**)
calloc(list->selection_buf_size, sizeof(*list->selection_buf));
if (!list->selection_buf)
goto error;
for (i = 0; i < list->menu_stack_size; i++)
{
list->menu_stack[i] = (file_list_t*)
malloc(sizeof(*list->menu_stack[i]));
/* NULL-check before the three field writes below NULL-deref
* on OOM. menu_list_free tolerates a partially-populated
* menu_stack (has 'if (!menu_list->menu_stack[i]) continue'
* guards), and the array itself was calloc'd above so
* unallocated slots past 'i' are already NULL. */
if (!list->menu_stack[i])
goto error;
/* Every field, named. file_list_t gains one now and again --
* userdata_free was the last -- and anything left out here holds
* whatever malloc() returned, which for a destructor hook is an
* uninitialised function pointer called on the first teardown
* that reaches it. Add new fields to both loops. */
list->menu_stack[i]->list = NULL;
list->menu_stack[i]->capacity = 0;
list->menu_stack[i]->size = 0;
list->menu_stack[i]->userdata_free = NULL;
list->menu_stack[i]->actiondata_free = menu_entries_cbs_free;
}
for (i = 0; i < list->selection_buf_size; i++)
{
list->selection_buf[i] = (file_list_t*)
malloc(sizeof(*list->selection_buf[i]));
/* Same pattern as the menu_stack loop above. */
if (!list->selection_buf[i])
goto error;
/* Every field, named. file_list_t gains one now and again --
* userdata_free was the last -- and anything left out here holds
* whatever malloc() returned, which for a destructor hook is an
* uninitialised function pointer called on the first teardown
* that reaches it. Add new fields to both loops. */
list->selection_buf[i]->list = NULL;
list->selection_buf[i]->capacity = 0;
list->selection_buf[i]->size = 0;
list->selection_buf[i]->userdata_free = NULL;
list->selection_buf[i]->actiondata_free = menu_entries_cbs_free;
}
return list;
error:
menu_list_free(menu_driver_ctx, list);
return NULL;
}
static int menu_input_key_bind_set_mode_common(struct menu_state *menu_st,
struct menu_bind_state *binds, enum menu_input_binds_ctl_state state,
rarch_setting_t *setting, settings_t *settings)
{
switch (state)
{
case MENU_INPUT_BINDS_CTL_BIND_SINGLE:
{
unsigned bind_type;
menu_displaylist_info_t info;
menu_list_t *menu_list = menu_st->entries.list;
file_list_t *menu_stack = menu_list ? MENU_LIST_GET(menu_list, (unsigned)0) : NULL;
size_t selection = menu_st->selection_ptr;
menu_displaylist_info_init(&info);
bind_type = setting->bind_type;
binds->order = 0;
binds->begin = bind_type;
binds->last = bind_type;
binds->out_user = setting->index_offset;
binds->out_id = bind_type - MENU_SETTINGS_BIND_BEGIN;
binds->buffer = *input_config_bind(
binds->out_user, binds->out_id);
binds->user = setting->index_offset;
info.list = menu_stack;
info.type = MENU_SETTINGS_CUSTOM_BIND_KEYBOARD;
info.directory_ptr = selection;
info.enum_idx = MENU_ENUM_LABEL_CUSTOM_BIND;
info.label = strdup(MENU_ENUM_LABEL_CUSTOM_BIND_STR);
if (menu_displaylist_ctl(DISPLAYLIST_INFO, &info, settings))
menu_displaylist_process(&info);
menu_displaylist_info_free(&info);
}
break;
case MENU_INPUT_BINDS_CTL_BIND_ALL:
{
menu_displaylist_info_t info;
menu_list_t *menu_list = menu_st->entries.list;
file_list_t *menu_stack = menu_list ? MENU_LIST_GET(menu_list, (unsigned)0) : NULL;
size_t selection = menu_st->selection_ptr;
menu_displaylist_info_init(&info);
binds->order = 0;
binds->begin = MENU_SETTINGS_BIND_BEGIN
+ input_config_bind_order[0];
binds->last = MENU_SETTINGS_BIND_LAST;
binds->out_user = setting->index_offset;
binds->out_id = input_config_bind_order[0];
binds->buffer = *input_config_bind(
binds->out_user, binds->out_id);
/* whose binds these are: the keyboard's capture asks for
* the next one by user and number */
binds->user = setting->index_offset;
info.list = menu_stack;
info.type = MENU_SETTINGS_CUSTOM_BIND_KEYBOARD;
info.directory_ptr = selection;
info.enum_idx = MENU_ENUM_LABEL_CUSTOM_BIND_ALL;
info.label = strdup(MENU_ENUM_LABEL_CUSTOM_BIND_ALL_STR);
if (menu_displaylist_ctl(DISPLAYLIST_INFO, &info, settings))
menu_displaylist_process(&info);
menu_displaylist_info_free(&info);
}
break;
default:
case MENU_INPUT_BINDS_CTL_BIND_NONE:
break;
}
return 0;
}
/* Whether the control being bound is L2 or R2. */
static bool menu_bind_is_trigger(const struct menu_bind_state *state)
{
unsigned id = state->begin - MENU_SETTINGS_BIND_BEGIN;
return id == RETRO_DEVICE_ID_JOYPAD_L2 || id == RETRO_DEVICE_ID_JOYPAD_R2;
}
/* The lowest bit of @now not in @before (NULL: none), or @count bits
* past the end for none. */
static unsigned menu_bind_first_bit(const uint32_t *now,
const uint32_t *before, unsigned words)
{
unsigned w;
for (w = 0; w < words; w++)
{
uint32_t bits = before ? (now[w] & ~before[w]) : now[w];
if (bits)
{
unsigned b = w * 32;
while (!(bits & 1))
{
bits >>= 1;
b++;
}
return b;
}
}
return words * 32;
}
static bool menu_input_key_bind_poll_find_hold_pad(
struct menu_bind_state *new_state,
struct retro_keybind *output,
unsigned p)
{
unsigned a, b, h;
const struct menu_bind_state_port *n =
&new_state->state[new_state->now][p];
b = menu_bind_first_bit(n->keys, NULL, ARRAY_SIZE(n->keys));
if (b < RETROK_LAST)
{
RETRO_KEYBIND_SET_KEY(output, (enum retro_key)b);
new_state->from_keys = true;
return true;
}
for (b = 0; b < MENU_MAX_MBUTTONS; b++)
{
bool found = BIT32_GET(n->mouse_buttons, b);
if (!found)
continue;
switch (b)
{
case RETRO_DEVICE_ID_MOUSE_LEFT:
case RETRO_DEVICE_ID_MOUSE_RIGHT:
case RETRO_DEVICE_ID_MOUSE_MIDDLE:
case RETRO_DEVICE_ID_MOUSE_BUTTON_4:
case RETRO_DEVICE_ID_MOUSE_BUTTON_5:
case RETRO_DEVICE_ID_MOUSE_WHEELUP:
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP:
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN:
RETRO_KEYBIND_SET_MBUTTON(output, b);
new_state->from_keys = true;
return true;
}
}
b = menu_bind_first_bit(n->buttons.data, NULL,
ARRAY_SIZE(n->buttons.data));
if (b < MENU_MAX_BUTTONS)
{
/* L2 and R2: a trigger that is a button and an axis is bound
* to its axis, which is analog */
if (menu_bind_is_trigger(new_state))
{
uint32_t joyaxis = menu_bind_trigger_axis(n->axes,
new_state->axis_state[p].rested_axes, MENU_MAX_AXES);
if (joyaxis != AXIS_NONE)
{
output->joyaxis = joyaxis;
output->joykey = NO_BTN;
return true;
}
}
output->joykey = b;
output->joyaxis = AXIS_NONE;
return true;
}
/* Axes are a bit tricky ... */
for (a = 0; a < MENU_MAX_AXES; a++)
{
if ( abs(n->axes[a]) >= 20000
&& n->axes[a] != new_state->axis_state[p].rested_axes[a])
{
/* Take care of case where axis rests on +/- 0x7fff
* (e.g. 360 controller on Linux) */
output->joyaxis = n->axes[a] > 0 ? AXIS_POS(a) : AXIS_NEG(a);
output->joykey = NO_BTN;
return true;
}
}
for (h = 0; h < MENU_MAX_HATS; h++)
{
uint16_t trigged = n->hats[h];
uint16_t sane_trigger = 0;
if (trigged & HAT_UP_MASK)
sane_trigger = HAT_UP_MASK;
else if (trigged & HAT_DOWN_MASK)
sane_trigger = HAT_DOWN_MASK;
else if (trigged & HAT_LEFT_MASK)
sane_trigger = HAT_LEFT_MASK;
else if (trigged & HAT_RIGHT_MASK)
sane_trigger = HAT_RIGHT_MASK;
if (sane_trigger)
{
output->joykey = HAT_MAP(h, sane_trigger);
output->joyaxis = AXIS_NONE;
return true;
}
}
return false;
}
static bool menu_input_key_bind_poll_find_hold(
struct menu_bind_state *new_state,
struct retro_keybind *output)
{
if (new_state)
{
unsigned i;
/* only the slots polled can have anything held */
for (i = 0; i < MAX_USERS; i++)
{
if ( (i == new_state->port || ((new_state->listen >> i) & 1))
&& menu_input_key_bind_poll_find_hold_pad(new_state, output, i))
{
new_state->from = i;
return true;
}
}
}
return false;
}
static bool menu_input_key_bind_poll_find_trigger_pad(
struct menu_bind_state *new_state,
struct retro_keybind *output,
unsigned p)
{
unsigned a, b, h;
const struct menu_bind_state_port *n =
&new_state->state[new_state->now][p];
const struct menu_bind_state_port *o =
&new_state->state[!new_state->now][p];
b = menu_bind_first_bit(n->keys, o->keys, ARRAY_SIZE(n->keys));
if (b < RETROK_LAST)
{
RETRO_KEYBIND_SET_KEY(output, (enum retro_key)b);
new_state->from_keys = true;
return true;
}
for (b = 0; b < MENU_MAX_MBUTTONS; b++)
{
bool found = BIT32_GET(n->mouse_buttons, b) && !BIT32_GET(o->mouse_buttons, b);
if (!found)
continue;
switch (b)
{
case RETRO_DEVICE_ID_MOUSE_LEFT:
case RETRO_DEVICE_ID_MOUSE_RIGHT:
case RETRO_DEVICE_ID_MOUSE_MIDDLE:
case RETRO_DEVICE_ID_MOUSE_BUTTON_4:
case RETRO_DEVICE_ID_MOUSE_BUTTON_5:
case RETRO_DEVICE_ID_MOUSE_WHEELUP:
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP:
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN:
RETRO_KEYBIND_SET_MBUTTON(output, b);
new_state->from_keys = true;
return true;
}
}
b = menu_bind_first_bit(n->buttons.data, o->buttons.data,
ARRAY_SIZE(n->buttons.data));
if (b < MENU_MAX_BUTTONS)
{
/* L2 and R2: a trigger that is a button and an axis is bound
* to its axis, which is analog */
if (menu_bind_is_trigger(new_state))
{
uint32_t joyaxis = menu_bind_trigger_axis(n->axes,
new_state->axis_state[p].rested_axes, MENU_MAX_AXES);
if (joyaxis != AXIS_NONE)
{
unsigned axis = (AXIS_NEG_GET(joyaxis) < MENU_MAX_AXES)
? AXIS_NEG_GET(joyaxis) : AXIS_POS_GET(joyaxis);
output->joyaxis = joyaxis;
output->joykey = NO_BTN;
/* the rest of this pull is not the next bind's press */
new_state->axis_state[p].trigger_pulled |= (1U << axis);
return true;
}
}
output->joykey = b;
output->joyaxis = AXIS_NONE;
return true;
}
/* Axes are a bit tricky ... */
for (a = 0; a < MENU_MAX_AXES; a++)
{
int locked_distance = abs(n->axes[a] -
new_state->axis_state[p].locked_axes[a]);
int rested_distance = abs(n->axes[a] -
new_state->axis_state[p].rested_axes[a]);
/* a trigger bound at the start of its pull: nothing more of
* that pull, until it is back at rest */
if (new_state->axis_state[p].trigger_pulled & (1U << a))
{
if (rested_distance < MENU_BIND_TRIGGER_MOVED)
new_state->axis_state[p].trigger_pulled &= ~(1U << a);
continue;
}
if ( (abs(n->axes[a]) >= 20000)
&& (locked_distance >= 20000)
&& (rested_distance >= 20000))
{
/* Take care of case where axis rests on +/- 0x7fff
* (e.g. 360 controller on Linux) */
output->joyaxis = (n->axes[a] > 0) ? AXIS_POS(a) : AXIS_NEG(a);
output->joykey = NO_BTN;
/* Lock the current axis */
new_state->axis_state[p].locked_axes[a] = n->axes[a] > 0
? 0x7fff : -0x7fff;
return true;
}
if (locked_distance >= 20000) /* Unlock the axis. */
new_state->axis_state[p].locked_axes[a] = 0;
}
for (h = 0; h < MENU_MAX_HATS; h++)
{
uint16_t trigged = n->hats[h] & (~o->hats[h]);
uint16_t sane_trigger = 0;
if (trigged & HAT_UP_MASK)
sane_trigger = HAT_UP_MASK;
else if (trigged & HAT_DOWN_MASK)
sane_trigger = HAT_DOWN_MASK;
else if (trigged & HAT_LEFT_MASK)
sane_trigger = HAT_LEFT_MASK;
else if (trigged & HAT_RIGHT_MASK)
sane_trigger = HAT_RIGHT_MASK;
if (sane_trigger)
{
output->joykey = HAT_MAP(h, sane_trigger);
output->joyaxis = AXIS_NONE;
return true;
}
}
return false;
}
static bool menu_input_key_bind_poll_find_trigger(
struct menu_bind_state *new_state,
struct retro_keybind *output)
{
if (new_state)
{
unsigned i;
/* only the slots polled can have anything pressed */
for (i = 0; i < MAX_USERS; i++)
{
if ( (i == new_state->port || ((new_state->listen >> i) & 1))
&& menu_input_key_bind_poll_find_trigger_pad(
new_state, output, i))
{
new_state->from = i;
return true;
}
}
}
return false;
}
/* Where each of the controller's axes rests, read as the capture of a
* bind starts. */
static void menu_input_key_bind_poll_bind_get_rested_axes(
struct menu_bind_state *state)
{
unsigned s;
input_driver_capture_pad(state->port, false,
NULL, 0,
state->axis_state[state->port].rested_axes, MENU_MAX_AXES,
NULL, 0);
for (s = 0; s < MAX_USERS; s++)
if ((state->listen >> s) & 1)
input_driver_capture_pad(s, false, NULL, 0,
state->axis_state[s].rested_axes, MENU_MAX_AXES, NULL, 0);
}
MENU_NOINLINE static void input_event_osk_iterate(void *osk_grid, enum osk_type osk_idx)
{
#ifndef HAVE_LANGEXTRA
/* If HAVE_LANGEXTRA is not defined, define some ASCII-friendly pages. */
static const char *uppercase_grid[] = {
"1","2","3","4","5","6","7","8","9","0","Bksp",
"Q","W","E","R","T","Y","U","I","O","P","Enter",
"A","S","D","F","G","H","J","K","L","+","Lower",
"Z","X","C","V","B","N","M"," ","_","/","Next"};
static const char *lowercase_grid[] = {
"1","2","3","4","5","6","7","8","9","0","Bksp",
"q","w","e","r","t","y","u","i","o","p","Enter",
"a","s","d","f","g","h","j","k","l","@","Upper",
"z","x","c","v","b","n","m"," ","-",".","Next"};
static const char *symbols_page1_grid[] = {
"1","2","3","4","5","6","7","8","9","0","Bksp",
"!","\"","#","$","%","&","'","*","(",")","Enter",
"+",",","-","~","/",":",";","=","<",">","Lower",
"?","@","[","\\","]","^","_","|","{","}","Next"};
#endif
switch (osk_idx)
{
#ifdef HAVE_LANGEXTRA
case OSK_HIRAGANA_PAGE1:
memcpy(osk_grid,
hiragana_page1_grid,
sizeof(hiragana_page1_grid));
break;
case OSK_HIRAGANA_PAGE2:
memcpy(osk_grid,
hiragana_page2_grid,
sizeof(hiragana_page2_grid));
break;
case OSK_KATAKANA_PAGE1:
memcpy(osk_grid,
katakana_page1_grid,
sizeof(katakana_page1_grid));
break;
case OSK_KATAKANA_PAGE2:
memcpy(osk_grid,
katakana_page2_grid,
sizeof(katakana_page2_grid));
break;
case OSK_KOREAN_PAGE1:
memcpy(osk_grid,
korean_page1_grid,
sizeof(korean_page1_grid));
break;
case OSK_CHINESE_INDEX:
input_osk_chinese_grid((char**)osk_grid, osk_idx);
break;
#endif
case OSK_SYMBOLS_PAGE1:
memcpy(osk_grid,
symbols_page1_grid,
sizeof(uppercase_grid));
break;
case OSK_UPPERCASE_LATIN:
memcpy(osk_grid,
uppercase_grid,
sizeof(uppercase_grid));
break;
case OSK_LOWERCASE_LATIN:
default:
#ifdef HAVE_LANGEXTRA
/* the Chinese pages of characters: laid out from the table */
if (osk_idx > OSK_CHINESE_INDEX && osk_idx < OSK_TYPE_LAST)
{
input_osk_chinese_grid((char**)osk_grid, osk_idx);
break;
}
#endif
memcpy(osk_grid,
lowercase_grid,
sizeof(lowercase_grid));
break;
}
}
/* The mouse has not been seen to move yet. */
#define MENU_MOUSE_POS_NONE VIDEO_POS_PACK(-0x7fff, -0x7fff)
/* A window point in a framebuffer menu's own pixels, clamped: RGUI
* uses a framebuffer texture + custom viewports, which means we have to
* convert from screen space to menu space. */
static void menu_input_fb_point(const gfx_display_t *p_disp,
const struct video_viewport *vp, float wx, float wy,
int *x, int *y)
{
unsigned fb_width = VIDEO_SCALE_W(p_disp->framebuf_dims);
unsigned fb_height = VIDEO_SCALE_H(p_disp->framebuf_dims);
*x = (int16_t)(((wx - (float)VIDEO_POS_X(vp->pos))
/ (float)VIDEO_SCALE_W(vp->dims)) * (float)fb_width);
if (*x < 0)
*x = 0;
else if (*x >= (int)fb_width)
*x = (fb_width - 1);
*y = (int16_t)(((wy - (float)VIDEO_POS_Y(vp->pos))
/ (float)VIDEO_SCALE_H(vp->dims)) * (float)fb_height);
if (*y < 0)
*y = 0;
else if (*y >= (int)fb_height)
*y = (fb_height - 1);
}
/* A point in the UI's coordinates to RGUI's framebuffer while views are
* presented: the driver fits that to the UI, while the viewport it
* reports is the whole window. False, with nothing written, when the
* fit is empty. */
static bool menu_input_views_fb_point(const gfx_display_t *p_disp,
const video_views_layout_t *layout, float ui_x, float ui_y,
int *x, int *y)
{
struct video_viewport vp = {0};
/* Drivers keep the aspect once content has loaded. A window that
* shows no screen has no ui_dims: the UI is the whole window. */
vp.full_dims = layout->num_placements
? layout->ui_dims : video_driver_get_output_dims();
video_driver_update_viewport(&vp, false, true, true);
if (!VIDEO_SCALE_W(vp.dims) || !VIDEO_SCALE_H(vp.dims))
return false;
menu_input_fb_point(p_disp, &vp, ui_x, ui_y, x, y);
return true;
}
/* A window point to where the menu has it while views are presented. */
static void menu_input_views_point(
gfx_display_t *p_disp,
const video_views_layout_t *layout,
bool menu_has_fb,
int *x, int *y)
{
int ui_x, ui_y;
video_views_ui_point(layout, *x, *y, &ui_x, &ui_y);
if ( !menu_has_fb
|| !menu_input_views_fb_point(p_disp, layout,
(float)ui_x, (float)ui_y, x, y))
{
*x = ui_x;
*y = ui_y;
}
}
/* A point in the menu's own window, which its UI layer of @width x
* @height fills, to RGUI's framebuffer as the driver fits it there */
static void menu_input_window_fb_point(const gfx_display_t *p_disp,
unsigned width, unsigned height, int *x, int *y)
{
struct video_viewport vp = {0};
vp.full_dims = VIDEO_SCALE_PACK(width, height);
video_driver_update_viewport(&vp, false, true, true);
if (VIDEO_SCALE_W(vp.dims) && VIDEO_SCALE_H(vp.dims))
menu_input_fb_point(p_disp, &vp, (float)*x, (float)*y, x, y);
}
MENU_NOINLINE static void menu_input_get_mouse_hw_state(
gfx_display_t *p_disp,
menu_handle_t *menu,
bool menu_mouse_enable,
bool input_overlay_enable,
bool overlay_active,
menu_input_pointer_hw_state_t *hw_state)
{
struct menu_state *menu_st = &menu_driver_state;
/* where the mouse last was: one word, one compare */
static uint32_t last_pos = MENU_MOUSE_POS_NONE;
uint32_t now_pos = 0;
int x = 0;
int y = 0;
bool ignore_position = false;
bool is_select_pressed = false;
bool is_cancel_pressed = false;
static bool last_select_pressed = false;
static bool last_cancel_pressed = false;
bool menu_has_fb =
(menu &&
menu->driver_ctx &&
menu->driver_ctx->set_texture);
const input_pointer_view_t *view = input_driver_pointer_view();
bool state_inited = (view->flags & INPUT_PTR_VIEW_VALID) != 0;
const video_views_layout_t *layout = video_driver_get_views_layout();
#ifdef HAVE_OVERLAY
/* Menu pointer controls are ignored when overlays are enabled. */
if (overlay_active)
menu_mouse_enable = false;
#endif
/* Ignore initial mouse position also after reinit */
if (menu_st->input_pointer_hw_state.flags & MENU_INP_PTR_FLG_RESET)
{
menu_st->input_pointer_hw_state.flags &= ~MENU_INP_PTR_FLG_RESET;
last_pos = MENU_MOUSE_POS_NONE;
}
/* Easiest to set inactive by default, and toggle
* when input is detected */
hw_state->pos = 0;
hw_state->flags = 0;
if (!menu_mouse_enable)
{
/* 'hw_state->flags' has just been zeroed, but the button
* edge detectors below are not reached on this path. Reset
* them to match, otherwise they retain the state from the
* last enabled frame and report a spurious change - and so
* a spurious MENU_INP_PTR_FLG_ACTIVE - once mouse input is
* re-enabled (e.g. when an overlay is dismissed) */
last_select_pressed = false;
last_cancel_pressed = false;
return;
}
/* X/Y position */
if (state_inited)
{
now_pos = view->mouse_pos;
x = VIDEO_POS_X(now_pos);
y = VIDEO_POS_Y(now_pos);
if (now_pos != last_pos)
hw_state->flags |= MENU_INP_PTR_FLG_ACTIVE;
}
/* Start reading mouse position after moving it once */
if (last_pos == MENU_MOUSE_POS_NONE)
ignore_position = true;
last_pos = now_pos;
/* > X/Y position adjustment */
if (layout)
menu_input_views_point(p_disp, layout, menu_has_fb, &x, &y);
else if (menu_has_fb)
{
struct video_viewport vp = {0};
video_driver_get_viewport_info(&vp);
menu_input_fb_point(p_disp, &vp, (float)x, (float)y, &x, &y);
}
hw_state->pos = VIDEO_POS_PACK(x, y);
if (state_inited)
{
/* Select (LMB)
* Note that releasing select also counts as activity */
if (view->flags & INPUT_PTR_VIEW_MOUSE_LEFT)
hw_state->flags |= MENU_INP_PTR_FLG_PRESS_SELECT;
/* Cancel (RMB)
* Note that releasing cancel also counts as activity */
if (view->flags & INPUT_PTR_VIEW_MOUSE_RIGHT)
hw_state->flags |= MENU_INP_PTR_FLG_PRESS_CANCEL;
/* Up (mouse wheel up) */
if (view->flags & INPUT_PTR_VIEW_WHEEL_UP)
hw_state->flags |= (MENU_INP_PTR_FLG_PRESS_UP
| MENU_INP_PTR_FLG_ACTIVE
);
/* Down (mouse wheel down) */
if (view->flags & INPUT_PTR_VIEW_WHEEL_DOWN)
hw_state->flags |= (MENU_INP_PTR_FLG_PRESS_DOWN
| MENU_INP_PTR_FLG_ACTIVE
);
/* Left (mouse wheel horizontal left) */
if (view->flags & INPUT_PTR_VIEW_HWHEEL_DOWN)
hw_state->flags |= (MENU_INP_PTR_FLG_PRESS_LEFT
| MENU_INP_PTR_FLG_ACTIVE
);
/* Right (mouse wheel horizontal right) */
if (view->flags & INPUT_PTR_VIEW_HWHEEL_UP)
hw_state->flags |= (MENU_INP_PTR_FLG_PRESS_RIGHT
| MENU_INP_PTR_FLG_ACTIVE
);
}
is_select_pressed = ((hw_state->flags & MENU_INP_PTR_FLG_PRESS_SELECT) > 0);
is_cancel_pressed = ((hw_state->flags & MENU_INP_PTR_FLG_PRESS_CANCEL) > 0);
if (is_select_pressed || (is_select_pressed != last_select_pressed))
hw_state->flags |= MENU_INP_PTR_FLG_ACTIVE;
if (is_cancel_pressed || (is_cancel_pressed != last_cancel_pressed))
hw_state->flags |= MENU_INP_PTR_FLG_ACTIVE;
last_select_pressed = (hw_state->flags & MENU_INP_PTR_FLG_PRESS_SELECT) > 0;
last_cancel_pressed = (hw_state->flags & MENU_INP_PTR_FLG_PRESS_CANCEL) > 0;
if (ignore_position)
hw_state->flags &= ~MENU_INP_PTR_FLG_ACTIVE;
}
MENU_NOINLINE static void menu_input_get_touchscreen_hw_state(
gfx_display_t *p_disp,
menu_handle_t *menu,
bool overlay_active,
bool pointer_enabled,
unsigned input_touch_scale,
menu_input_pointer_hw_state_t *hw_state)
{
unsigned fb_width, fb_height;
int pointer_x = 0;
int pointer_y = 0;
const input_pointer_view_t *view = input_driver_pointer_view();
bool state_inited = (view->flags & INPUT_PTR_VIEW_VALID) != 0;
/* The menu's own window, which it fills: no views to map through */
unsigned window =
video_driver_get_ui_layer_window(VIDEO_WINDOW_UI_MENU);
const video_views_layout_t *layout = window
? NULL : video_driver_get_views_layout();
/* Is a background texture set for the current menu driver?
* Checks if the menu framebuffer is set.
* This would usually only return true
* for framebuffer-based menu drivers, like RGUI. */
bool menu_has_fb =
(menu && menu->driver_ctx && menu->driver_ctx->set_texture);
/* With views the window's point is mapped here: the pointer
* device's is in the core's packed frame. */
int pointer_device =
(menu_has_fb && !layout && !window) ?
RETRO_DEVICE_POINTER : RARCH_DEVICE_POINTER_SCREEN;
static uint32_t last_pos = 0;
uint32_t now_pos;
int x = 0;
int y = 0;
static bool last_select_pressed = false;
static bool last_cancel_pressed = false;
/* Easiest to set inactive by default, and toggle
* when input is detected */
/* Touch screens don't have mouse wheels,
* so up/down/left/right are always disabled */
hw_state->flags &= ~(MENU_INP_PTR_FLG_ACTIVE
| MENU_INP_PTR_FLG_PRESS_UP
| MENU_INP_PTR_FLG_PRESS_DOWN
| MENU_INP_PTR_FLG_PRESS_LEFT
| MENU_INP_PTR_FLG_PRESS_RIGHT
);
#ifdef HAVE_OVERLAY
/* Menu pointer controls are ignored when overlays are enabled,
* unless the menu has a window of its own: the overlay is the main
* window's */
if (overlay_active && !window)
pointer_enabled = false;
#endif
/* If touchscreen is disabled, ignore all input */
if (!pointer_enabled)
{
hw_state->pos = 0;
hw_state->flags &= ~(MENU_INP_PTR_FLG_PRESS_SELECT
| MENU_INP_PTR_FLG_PRESS_CANCEL);
/* Keep the edge detectors in step with the cleared flags,
* otherwise they retain the state from the last enabled
* frame and report a spurious change once touch input is
* re-enabled (e.g. when an overlay is dismissed) */
last_select_pressed = false;
last_cancel_pressed = false;
return;
}
/* TODO/FIXME - this should only be used for framebuffer-based
* menu drivers like RGUI. Touchscreen input as a whole should
* NOT be dependent on this */
fb_width = VIDEO_SCALE_W(p_disp->framebuf_dims);
fb_height = VIDEO_SCALE_H(p_disp->framebuf_dims);
if (window)
{
unsigned dims = video_driver_get_ui_layer_dims(
VIDEO_WINDOW_UI_MENU);
fb_width = VIDEO_SCALE_W(dims);
fb_height = VIDEO_SCALE_H(dims);
}
else if (layout)
{
/* The window: the canvas, unless that is offscreen. One that
* shows no screen has neither. */
unsigned dims = layout->offscreen
? layout->ui_dims : layout->canvas_dims;
if (!layout->num_placements)
dims = video_driver_get_output_dims();
fb_width = VIDEO_SCALE_W(dims);
fb_height = VIDEO_SCALE_H(dims);
}
/* X pos */
if (state_inited)
pointer_x = VIDEO_POS_X(
(pointer_device == RETRO_DEVICE_POINTER)
? view->ptr_pos : view->scr_pos);
x = ((pointer_x + 0x7fff) * (int)fb_width) / 0xFFFF;
x *= input_touch_scale;
/* Y pos */
if (state_inited)
pointer_y = VIDEO_POS_Y(
(pointer_device == RETRO_DEVICE_POINTER)
? view->ptr_pos : view->scr_pos);
y = ((pointer_y + 0x7fff) * (int)fb_height) / 0xFFFF;
y *= input_touch_scale;
if (layout)
menu_input_views_point(p_disp, layout, menu_has_fb, &x, &y);
else if (window && menu_has_fb)
menu_input_window_fb_point(p_disp, fb_width, fb_height, &x, &y);
hw_state->pos = VIDEO_POS_PACK(x, y);
/* Whether it moved, both axes in one compare.
* > An annoyance - we get different starting positions
* depending upon whether pointer_device is
* RETRO_DEVICE_POINTER or RARCH_DEVICE_POINTER_SCREEN,
* so different 'activity' checks are required to prevent
* false positives on first run */
now_pos = (pointer_device == RARCH_DEVICE_POINTER_SCREEN)
? hw_state->pos : VIDEO_POS_PACK(pointer_x, pointer_y);
if (now_pos != last_pos)
hw_state->flags |= MENU_INP_PTR_FLG_ACTIVE;
last_pos = now_pos;
/* Select (touch screen contact)
* Note that releasing select also counts as activity */
if (state_inited)
{
if (view->flags & ((pointer_device == RETRO_DEVICE_POINTER)
? INPUT_PTR_VIEW_PTR_PRESSED : INPUT_PTR_VIEW_SCR_PRESSED))
hw_state->flags |= MENU_INP_PTR_FLG_PRESS_SELECT;
else
hw_state->flags &= ~MENU_INP_PTR_FLG_PRESS_SELECT;
}
{
bool select_pressed = ((hw_state->flags & MENU_INP_PTR_FLG_PRESS_SELECT) > 0);
if (select_pressed || (select_pressed != last_select_pressed))
hw_state->flags |= MENU_INP_PTR_FLG_ACTIVE;
last_select_pressed = select_pressed;
}
/* Cancel (touch screen 'back' - don't know what is this, but whatever...)
* Note that releasing cancel also counts as activity */
if (state_inited)
{
if (view->flags & ((pointer_device == RETRO_DEVICE_POINTER)
? INPUT_PTR_VIEW_PTR_BACK : INPUT_PTR_VIEW_SCR_BACK))
hw_state->flags |= MENU_INP_PTR_FLG_PRESS_CANCEL;
else
hw_state->flags &= ~MENU_INP_PTR_FLG_PRESS_CANCEL;
}
{
bool cancel_pressed = ((hw_state->flags & MENU_INP_PTR_FLG_PRESS_CANCEL) > 0);
if (cancel_pressed || (cancel_pressed != last_cancel_pressed))
hw_state->flags |= MENU_INP_PTR_FLG_ACTIVE;
last_cancel_pressed = cancel_pressed;
}
}
#ifdef HAVE_OPENXR
/* The laser drives the menu's pointer: since it last read on the menu
* quad, no mouse or touch has moved. */
static bool menu_input_headset_drives = false;
/* The headset's laser on the menu quad, read as a mouse. The quad shows
* the UI, where a framebuffer menu (RGUI) fills the viewport it is
* fitted to. A press that leaves the quad is released where it left.
* True while the laser is on the quad, and once more for that release. */
MENU_NOINLINE static bool menu_input_get_headset_hw_state(
gfx_display_t *p_disp,
menu_handle_t *menu,
menu_input_pointer_hw_state_t *hw_state)
{
float u, v;
bool pressed, changed;
static int16_t last_x = -1;
static int16_t last_y = -1;
static bool last_pressed = false;
static size_t last_selection = (size_t)-1;
int x = 0;
int y = 0;
struct menu_state *menu_st = &menu_driver_state;
unsigned fb_width = VIDEO_SCALE_W(p_disp->framebuf_dims);
unsigned fb_height = VIDEO_SCALE_H(p_disp->framebuf_dims);
bool menu_has_fb = menu && menu->driver_ctx
&& menu->driver_ctx->set_texture;
hw_state->pos = 0;
hw_state->flags = 0;
if (!input_openxr_menu_pointer(&u, &v, &pressed))
{
bool release = last_pressed;
if (release)
{
hw_state->pos = VIDEO_POS_PACK(last_x, last_y);
hw_state->flags = MENU_INP_PTR_FLG_ACTIVE;
}
last_x = -1;
last_y = -1;
last_pressed = false;
last_selection = (size_t)-1;
return release;
}
if (menu_has_fb)
{
const video_views_layout_t *layout = video_driver_get_views_layout();
/* With views the quad is the UI, one eye's when that is drawn
* per eye; without, the whole window. */
if (layout)
menu_input_views_fb_point(p_disp, layout,
u * (float)VIDEO_SCALE_W(layout->ui_dims),
v * (float)VIDEO_SCALE_H(layout->ui_dims),
&x, &y);
else
{
struct video_viewport vp = {0};
video_driver_get_viewport_info(&vp);
if (VIDEO_SCALE_W(vp.dims) && VIDEO_SCALE_H(vp.dims))
menu_input_fb_point(p_disp, &vp,
u * (float)VIDEO_SCALE_W(vp.full_dims),
v * (float)VIDEO_SCALE_H(vp.full_dims),
&x, &y);
}
}
else
{
x = (int16_t)(u * (float)fb_width);
y = (int16_t)(v * (float)fb_height);
if (fb_width && x >= (int)fb_width)
x = (int)fb_width - 1;
if (fb_height && y >= (int)fb_height)
y = (int)fb_height - 1;
}
hw_state->pos = VIDEO_POS_PACK(x, y);
changed = x != last_x || y != last_y
|| pressed != last_pressed;
if (pressed)
hw_state->flags |= MENU_INP_PTR_FLG_PRESS_SELECT;
/* Active while pressed too, as the mouse is: menu_event() then
* leaves the press to the pointer, so a click acts once, at
* pointer-up. */
if (changed || pressed)
hw_state->flags |= MENU_INP_PTR_FLG_ACTIVE;
if (changed || menu_st->selection_ptr != last_selection)
RARCH_DBG("[Menu] Headset pointer x=%d y=%d pressed=%d selection=%u.\n",
x, y, pressed ? 1 : 0,
(unsigned)menu_st->selection_ptr);
last_x = x;
last_y = y;
last_pressed = pressed;
last_selection = menu_st->selection_ptr;
return true;
}
#endif
static void menu_entries_settings_deinit(struct menu_state *menu_st)
{
menu_setting_free(menu_st->entries.list_settings);
if (menu_st->entries.list_settings)
free(menu_st->entries.list_settings);
menu_st->entries.list_settings = NULL;
}
static bool menu_driver_displaylist_push_internal(
const char *label,
menu_displaylist_info_t *info,
settings_t *settings)
{
if (string_is_equal(label, MENU_ENUM_LABEL_HISTORY_TAB_STR))
return menu_displaylist_ctl(DISPLAYLIST_HISTORY, info, settings);
else if (string_is_equal(label, MENU_ENUM_LABEL_FAVORITES_TAB_STR))
return menu_displaylist_ctl(DISPLAYLIST_FAVORITES, info, settings);
else if (string_is_equal(label, MENU_ENUM_LABEL_SETTINGS_TAB_STR))
return menu_displaylist_ctl(DISPLAYLIST_SETTINGS_ALL, info, settings);
#ifdef HAVE_CHEATS
else if (string_is_equal(label, MENU_ENUM_LABEL_CHEAT_SEARCH_SETTINGS_STR))
return menu_displaylist_ctl(DISPLAYLIST_CHEAT_SEARCH_SETTINGS_LIST, info, settings);
#endif
else if (string_is_equal(label, MENU_ENUM_LABEL_MUSIC_TAB_STR))
{
filebrowser_clear_type();
info->type = 42;
if (info->exts && *info->exts)
free(info->exts);
if (info->label && *info->label)
free(info->label);
info->exts = strldup("lpl", sizeof("lpl"));
info->label = strdup(MENU_ENUM_LABEL_PLAYLISTS_TAB_STR);
menu_entries_clear(info->list);
return menu_displaylist_ctl(DISPLAYLIST_MUSIC_HISTORY, info, settings);
}
else if (string_is_equal(label, MENU_ENUM_LABEL_VIDEO_TAB_STR))
{
filebrowser_clear_type();
info->type = 42;
if (info->exts && *info->exts)
free(info->exts);
if (info->label && *info->label)
free(info->label);
info->exts = strldup("lpl", sizeof("lpl"));
info->label = strdup(MENU_ENUM_LABEL_PLAYLISTS_TAB_STR);
menu_entries_clear(info->list);
return menu_displaylist_ctl(DISPLAYLIST_VIDEO_HISTORY, info, settings);
}
else if (string_is_equal(label, MENU_ENUM_LABEL_IMAGES_TAB_STR))
{
filebrowser_clear_type();
info->type = 42;
if (info->exts && *info->exts)
free(info->exts);
if (info->label && *info->label)
free(info->label);
info->exts = strldup("lpl", sizeof("lpl"));
info->label = strdup(MENU_ENUM_LABEL_PLAYLISTS_TAB_STR);
menu_entries_clear(info->list);
return menu_displaylist_ctl(DISPLAYLIST_IMAGES_HISTORY, info, settings);
}
else if (string_is_equal(label, MENU_ENUM_LABEL_PLAYLISTS_TAB_STR))
{
const char *dir_playlist = settings->paths.directory_playlist;
filebrowser_clear_type();
info->type = 42;
if (info->exts && *info->exts)
free(info->exts);
if (info->label && *info->label)
free(info->label);
info->exts = strldup("lpl", sizeof("lpl"));
info->label = strdup(MENU_ENUM_LABEL_PLAYLISTS_TAB_STR);
if (!dir_playlist || !*dir_playlist)
{
menu_entries_clear(info->list);
info->flags |= MD_FLAG_NEED_REFRESH
| MD_FLAG_NEED_PUSH_NO_PLAYLIST_ENTRIES
| MD_FLAG_NEED_PUSH;
return true;
}
if (info->path && *info->path)
free(info->path);
info->path = strdup(dir_playlist);
return menu_displaylist_ctl(
DISPLAYLIST_DATABASE_PLAYLISTS, info, settings);
}
else if (string_is_equal(label, MENU_ENUM_LABEL_ADD_TAB_STR))
return menu_displaylist_ctl(DISPLAYLIST_SCAN_DIRECTORY_LIST, info, settings);
#if defined(HAVE_LIBRETRODB)
else if (string_is_equal(label, MENU_ENUM_LABEL_EXPLORE_TAB_STR))
return menu_displaylist_ctl(DISPLAYLIST_EXPLORE, info, settings);
#endif
else if (string_is_equal(label, MENU_ENUM_LABEL_CONTENTLESS_CORES_TAB_STR))
return menu_displaylist_ctl(DISPLAYLIST_CONTENTLESS_CORES, info, settings);
else if (string_is_equal(label, MENU_ENUM_LABEL_NETPLAY_TAB_STR))
return menu_displaylist_ctl(DISPLAYLIST_NETPLAY_ROOM_LIST, info, settings);
else if (string_is_equal(label, MENU_ENUM_LABEL_HORIZONTAL_MENU_STR))
return menu_displaylist_ctl(DISPLAYLIST_HORIZONTAL, info, settings);
return false;
}
static bool menu_playlist_within_budget(void *ud)
{
return task_nbio_slice_within_budget(ud, 0, 0);
}
static bool menu_driver_displaylist_push(
struct menu_state *menu_st,
settings_t *settings,
file_list_t *entry_list,
file_list_t *entry_stack)
{
menu_displaylist_info_t info;
const char *path = NULL;
const char *label = NULL;
unsigned type = 0;
bool ret = false;
enum msg_hash_enums enum_idx = MSG_UNKNOWN;
menu_list_t *menu_list = menu_st->entries.list;
file_list_t *list = menu_list
? MENU_LIST_GET(menu_list, 0) : NULL;
menu_file_list_cbs_t *cbs = NULL;
menu_displaylist_info_init(&info);
if (list && list->size)
{
cbs = (menu_file_list_cbs_t*)
list->list[list->size - 1].actiondata;
path = list->list[list->size - 1].path;
label = list->list[list->size - 1].label;
type = list->list[list->size - 1].type;
info.directory_ptr = list->list[list->size - 1].directory_ptr;
}
if (cbs)
enum_idx = cbs->enum_idx;
info.list = entry_list;
info.type = type;
info.enum_idx = enum_idx;
if (path && *path)
info.path = strdup(path);
if (label && *label)
info.label = strdup(label);
if (!info.list)
goto error;
if (menu_driver_displaylist_push_internal(label, &info, settings))
{
ret = menu_displaylist_process(&info);
goto end;
}
/* push_internal is free to tear the menu down and build it again:
* a driver switch, a language change or a video reinit all reach
* RARCH_MENU_CTL_DEINIT, which menu_list_free()s every
* menu_stack[i] before menu_list_new() allocates replacements. The
* stack captured on entry is dangling in that case, so re-derive it
* before reading an entry back out. Matches the re-fetch in
* generic_menu_entry_action(). */
menu_list = menu_st->entries.list;
list = menu_list ? MENU_LIST_GET(menu_list, 0) : NULL;
cbs = (list && list->size)
? (menu_file_list_cbs_t*)list->list[list->size - 1].actiondata
: NULL;
if (cbs && cbs->action_deferred_push)
if (cbs->action_deferred_push(&info) != 0)
goto error;
ret = true;
end:
menu_displaylist_info_free(&info);
return ret;
error:
menu_displaylist_info_free(&info);
return false;
}
/* This sets up all the callback functions for a menu entry.
*
* OK : When we press the 'OK' button on an entry.
* Cancel : When we press the 'Cancel' button on an entry.
* Scan : When we press the 'Scan' button on an entry.
* Start : When we press the 'Start' button on an entry.
* Select : When we press the 'Select' button on an entry.
* Info : When we press the 'Info' button on an entry.
* Left : when we press 'Left' on the D-pad while this entry is selected.
* Right : when we press 'Right' on the D-pad while this entry is selected.
* Deferred push : When pressing an entry results in spawning a new list, it waits until the next
* frame to push this onto the stack. This function callback will then be invoked.
* Get value: Each entry has associated 'text', which we call the value. This function callback
* lets us render that text.
* Title: Each entry can have a custom 'title'.
* Label: Each entry has a label name. This function callback lets us render that label text.
* Sublabel: each entry has a sublabel, which consists of one or more lines of additional information.
* This function callback lets us render that text.
*/
static void menu_cbs_init(
struct menu_state *menu_st,
const menu_ctx_driver_t *menu_driver_ctx,
file_list_t *list,
menu_file_list_cbs_t *cbs,
const char *path,
const char *label,
size_t lbl_len,
unsigned type, size_t idx)
{
size_t menu_lbl_len;
const char *menu_lbl = NULL;
file_list_t *menu_list = MENU_LIST_GET(menu_st->entries.list, 0);
#ifdef DEBUG_LOG
menu_file_list_cbs_t *menu_cbs = (menu_file_list_cbs_t*)
menu_list->list[list->size - 1].actiondata;
enum msg_hash_enums enum_idx = menu_cbs ? menu_cbs->enum_idx : MSG_UNKNOWN;
#endif
if (menu_list && menu_list->size)
menu_lbl = menu_list->list[menu_list->size - 1].label;
if (!label || !menu_lbl)
return;
menu_lbl_len = strlen(menu_lbl);
#ifdef DEBUG_LOG
if (cbs && cbs->enum_idx != MSG_UNKNOWN)
RARCH_LOG("\t\t\tenum_idx %d [%s]\n", cbs->enum_idx, msg_hash_to_str(cbs->enum_idx));
#endif
/* It will try to find a corresponding callback function inside
* menu_cbs_ok.c, then map this callback to the entry. */
menu_cbs_init_bind_ok(cbs, path, label, lbl_len, type, idx, menu_lbl, menu_lbl_len);
/* It will try to find a corresponding callback function inside
* menu_cbs_cancel.c, then map this callback to the entry. */
menu_cbs_init_bind_cancel(cbs, path, label, lbl_len, type, idx, menu_lbl, menu_lbl_len);
/* It will try to find a corresponding callback function inside
* menu_cbs_scan.c, then map this callback to the entry. */
menu_cbs_init_bind_scan(cbs, path, label, type, idx);
/* It will try to find a corresponding callback function inside
* menu_cbs_start.c, then map this callback to the entry. */
menu_cbs_init_bind_start(cbs, path, label, type, idx);
/* It will try to find a corresponding callback function inside
* menu_cbs_drag.c, then map this callback to the entry. */
menu_cbs_init_bind_drag(cbs, path, label, type, idx);
/* It will try to find a corresponding callback function inside
* menu_cbs_drop.c, then map this callback to the entry. */
menu_cbs_init_bind_drop(cbs, path, label, type, idx);
/* It will try to find a corresponding callback function inside
* menu_cbs_select.c, then map this callback to the entry. */
menu_cbs_init_bind_select(cbs, path, label, type, idx);
/* It will try to find a corresponding callback function inside
* menu_cbs_info.c, then map this callback to the entry. */
menu_cbs_init_bind_info(cbs, path, label, type, idx);
/* It will try to find a corresponding callback function inside
* menu_cbs_left.c, then map this callback to the entry. */
menu_cbs_init_bind_left(cbs, path, label, lbl_len, type, idx, menu_lbl, menu_lbl_len);
/* It will try to find a corresponding callback function inside
* menu_cbs_right.c, then map this callback to the entry. */
menu_cbs_init_bind_right(cbs, path, label, lbl_len, type, idx, menu_lbl, menu_lbl_len);
/* It will try to find a corresponding callback function inside
* menu_cbs_deferred_push.c, then map this callback to the entry. */
menu_cbs_init_bind_deferred_push(cbs, path, label, type, idx);
/* It will try to find a corresponding callback function inside
* menu_cbs_get_string_representation.c, then map this callback to the entry. */
menu_cbs_init_bind_get_string_representation(cbs, path, label, lbl_len, type, idx);
/* It will try to find a corresponding callback function inside
* menu_cbs_title.c, then map this callback to the entry. */
menu_cbs_init_bind_title(cbs, path, label, type, idx);
/* It will try to find a corresponding callback function inside
* menu_cbs_label.c, then map this callback to the entry. */
menu_cbs_init_bind_label(cbs, path, label, type, idx);
/* It will try to find a corresponding callback function inside
* menu_cbs_sublabel.c, then map this callback to the entry. */
menu_cbs_init_bind_sublabel(cbs, path, label, lbl_len, type, idx);
if (menu_driver_ctx && menu_driver_ctx->bind_init)
menu_driver_ctx->bind_init(cbs, path, label, type, idx);
}
#if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_SLANG) || defined(HAVE_HLSL)
static void menu_driver_set_last_shader_path_int(
const char *shader_path,
enum rarch_shader_type *type,
char *shader_dir, size_t dir_len,
char *shader_file, size_t file_len)
{
const char *file_name = NULL;
if ( !type
|| !shader_dir
|| (dir_len < 1)
|| !shader_file
|| (file_len < 1))
return;
/* Reset existing cache */
*type = RARCH_SHADER_NONE;
shader_dir[0] = '\0';
shader_file[0] = '\0';
/* If path is empty, do nothing */
if (!shader_path || !*shader_path)
return;
/* Get shader type */
/* If type is invalid, do nothing */
if ((*type = video_shader_parse_type(shader_path)) == RARCH_SHADER_NONE)
return;
/* Cache parent directory */
fill_pathname_parent_dir(shader_dir, shader_path, dir_len);
/* If parent directory is empty, then file name
* is only valid if 'shader_path' refers to an
* existing file in the root of the file system */
if ( (!shader_dir || !*shader_dir)
&& !path_is_valid(shader_path))
return;
/* Cache file name */
file_name = path_basename_nocompression(shader_path);
if (file_name && *file_name)
strlcpy(shader_file, file_name, file_len);
}
void menu_driver_set_last_shader_preset_path(const char *path)
{
menu_handle_t *menu = menu_driver_state.driver_data;
if (menu)
menu_driver_set_last_shader_path_int(
path,
&menu->last_shader_selection.preset_type,
menu->last_shader_selection.preset_dir,
sizeof(menu->last_shader_selection.preset_dir),
menu->last_shader_selection.preset_file_name,
sizeof(menu->last_shader_selection.preset_file_name));
}
void menu_driver_set_last_shader_pass_path(const char *path)
{
menu_handle_t *menu = menu_driver_state.driver_data;
if (menu)
menu_driver_set_last_shader_path_int(
path,
&menu->last_shader_selection.pass_type,
menu->last_shader_selection.pass_dir,
sizeof(menu->last_shader_selection.pass_dir),
menu->last_shader_selection.pass_file_name,
sizeof(menu->last_shader_selection.pass_file_name));
}
enum rarch_shader_type menu_driver_get_last_shader_preset_type(void)
{
menu_handle_t *menu = menu_driver_state.driver_data;
if (!menu)
return RARCH_SHADER_NONE;
return menu->last_shader_selection.preset_type;
}
static void menu_driver_get_last_shader_path_int(
bool remember_last_dir,
const char *video_shader_dir,
enum rarch_shader_type type,
const char *shader_dir,
const char *shader_file_name,
const char **dir_out,
const char **file_name_out)
{
gfx_ctx_flags_t flags;
/* File name is NULL by default */
if (file_name_out)
*file_name_out = NULL;
flags.flags = 0;
video_context_driver_get_flags(&flags);
/* If any of the following are true:
* - Directory caching is disabled
* - No directory has been cached
* - Cached directory is invalid
* - Last selected shader is incompatible with
* the current video driver
* ...use default settings */
if ( (!remember_last_dir)
|| (type == RARCH_SHADER_NONE)
|| (!shader_dir || !*shader_dir)
|| !path_is_directory(shader_dir)
|| !BIT32_GET(flags.flags, video_shader_type_to_flag(type)))
{
if (dir_out)
*dir_out = video_shader_dir;
return;
}
/* Assign last set directory */
if (dir_out)
*dir_out = shader_dir;
/* Assign file name */
if ( file_name_out
&& (shader_file_name && *shader_file_name))
*file_name_out = shader_file_name;
}
void menu_driver_get_last_shader_preset_path(
const char **directory, const char **file_name)
{
settings_t *settings = config_get_ptr();
menu_handle_t *menu = menu_driver_state.driver_data;
enum rarch_shader_type type = RARCH_SHADER_NONE;
const char *shader_dir = NULL;
const char *shader_file_name = NULL;
if (menu)
{
type = menu->last_shader_selection.preset_type;
shader_dir = menu->last_shader_selection.preset_dir;
shader_file_name = menu->last_shader_selection.preset_file_name;
}
menu_driver_get_last_shader_path_int(
settings->bools.video_shader_remember_last_dir,
settings->paths.directory_video_shader,
type, shader_dir, shader_file_name,
directory, file_name);
}
void menu_driver_get_last_shader_pass_path(
const char **directory, const char **file_name)
{
menu_handle_t *menu = menu_driver_state.driver_data;
settings_t *settings = config_get_ptr();
enum rarch_shader_type type = RARCH_SHADER_NONE;
const char *shader_dir = NULL;
const char *shader_file_name = NULL;
if (menu)
{
type = menu->last_shader_selection.pass_type;
shader_dir = menu->last_shader_selection.pass_dir;
shader_file_name = menu->last_shader_selection.pass_file_name;
}
menu_driver_get_last_shader_path_int(
settings->bools.video_shader_remember_last_dir,
settings->paths.directory_video_shader,
type, shader_dir, shader_file_name,
directory, file_name);
}
static int menu_shader_manager_clear_num_passes_internal(
struct video_shader *shader, bool apply_changes)
{
if (shader)
{
struct menu_state *menu_st = &menu_driver_state;
shader->passes = 0;
menu_st->flags |= MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
video_shader_resolve_parameters(shader);
shader->flags |= SHDR_FLAG_MODIFIED;
if (apply_changes)
command_event(CMD_EVENT_SHADERS_APPLY_CHANGES, NULL);
}
return 0;
}
int menu_shader_manager_clear_num_passes(struct video_shader *shader)
{
return menu_shader_manager_clear_num_passes_internal(shader, true);
}
int menu_shader_manager_clear_parameter(struct video_shader *shader,
unsigned i)
{
struct video_shader_parameter *param = shader ?
&shader->parameters[i] : NULL;
if (param)
{
video_shader_param_set(param, MIN(MAX(param->minimum,
param->initial), param->maximum));
shader->flags |= SHDR_FLAG_MODIFIED;
}
return 0;
}
int menu_shader_manager_clear_pass_filter(struct video_shader *shader,
unsigned i)
{
struct video_shader_pass *shader_pass = shader ?
&shader->pass[i] : NULL;
if (!shader_pass)
return -1;
shader_pass->filter = RARCH_FILTER_UNSPEC;
shader->flags |= SHDR_FLAG_MODIFIED;
return 0;
}
void menu_shader_manager_clear_pass_scale(struct video_shader *shader,
unsigned i)
{
struct video_shader_pass *shader_pass = shader ?
&shader->pass[i] : NULL;
if (!shader_pass)
return;
shader_pass->fbo.scale_x = 0;
shader_pass->fbo.scale_y = 0;
shader_pass->fbo.flags &= ~FBO_SCALE_FLAG_VALID;
shader->flags |= SHDR_FLAG_MODIFIED;
}
void menu_shader_manager_clear_pass_path(struct video_shader *shader,
unsigned i)
{
struct video_shader_pass
*shader_pass = shader
? &shader->pass[i]
: NULL;
if (shader_pass)
*shader_pass->source.path = '\0';
if (shader)
shader->flags |= SHDR_FLAG_MODIFIED;
}
/**
* menu_shader_manager_get_type:
* @shader : shader handle
*
* Gets type of shader.
*
* Returns: type of shader.
**/
static enum rarch_shader_type menu_shader_manager_get_type(
const struct video_shader *shader)
{
enum rarch_shader_type type = RARCH_SHADER_NONE;
/* All shader types must be the same, or we cannot use it. */
if (shader)
{
type = video_shader_parse_type(shader->path);
if (shader->passes)
{
size_t i = 0;
if (type == RARCH_SHADER_NONE)
{
type = video_shader_parse_type(shader->pass[0].source.path);
i = 1;
}
for (; i < shader->passes; i++)
{
enum rarch_shader_type pass_type =
video_shader_parse_type(shader->pass[i].source.path);
switch (pass_type)
{
case RARCH_SHADER_CG:
case RARCH_SHADER_GLSL:
case RARCH_SHADER_SLANG:
if (type != pass_type)
return RARCH_SHADER_NONE;
break;
default:
break;
}
}
}
}
return type;
}
/**
* menu_shader_manager_apply_changes:
*
* Apply shader state changes.
**/
void menu_shader_manager_apply_changes(
struct video_shader *shader,
const char *dir_video_shader,
const char *dir_menu_config)
{
enum rarch_shader_type type = RARCH_SHADER_NONE;
settings_t *settings = config_get_ptr();
if (!shader)
return;
type = menu_shader_manager_get_type(shader);
/* Allow cold start from hotkey */
if ( type == RARCH_SHADER_NONE
&& settings->bools.video_shader_enable
&& !(shader->flags & SHDR_FLAG_DISABLED))
{
const char *preset = video_shader_get_current_shader_preset();
enum rarch_shader_type type = video_shader_parse_type(preset);
video_shader_apply_shader(settings, type, preset, false);
return;
}
/* Temporary state does not save anything */
if (shader->flags & SHDR_FLAG_TEMPORARY)
return;
if ( shader->passes
&& type != RARCH_SHADER_NONE
&& !(shader->flags & SHDR_FLAG_DISABLED))
{
menu_shader_manager_save_preset(shader, NULL,
dir_video_shader, dir_menu_config, true);
return;
}
menu_shader_manager_set_preset(NULL, type, NULL, true);
}
static bool menu_shader_manager_save_preset_internal(
bool save_reference,
const struct video_shader *shader,
const char *basename,
const char *dir_video_shader,
bool apply,
const char **target_dirs,
size_t num_target_dirs)
{
size_t _len;
char fullname[NAME_MAX_LENGTH];
char buffer[DIR_MAX_LENGTH];
bool ret = false;
enum rarch_shader_type type = RARCH_SHADER_NONE;
char *preset_path = NULL;
size_t i = 0;
if (!shader || !shader->passes)
return false;
if ((type = menu_shader_manager_get_type(shader)) == RARCH_SHADER_NONE)
return false;
if (basename && *basename)
_len = strlcpy(fullname, basename, sizeof(fullname));
else
_len = strlcpy_lit(fullname, "retroarch", sizeof(fullname));
strlcpy(fullname + _len,
video_shader_get_preset_extension(type),
sizeof(fullname) - _len);
if (path_is_absolute(fullname))
{
preset_path = fullname;
if ((ret = video_shader_write_preset(preset_path, shader, save_reference)))
RARCH_LOG("[Shaders] Saved shader preset to \"%s\".\n", preset_path);
else
RARCH_ERR("[Shaders] Failed writing shader preset to \"%s\".\n", preset_path);
}
else
{
char basedir[DIR_MAX_LENGTH];
for (i = 0; i < num_target_dirs; i++)
{
if (!target_dirs[i] || !*target_dirs[i])
continue;
fill_pathname_join(buffer, target_dirs[i],
fullname, sizeof(buffer));
fill_pathname_basedir(basedir, buffer, sizeof(basedir));
if (!path_is_directory(basedir) && !(ret = path_mkdir(basedir)))
{
RARCH_WARN("[Shaders] Failed to create preset directory \"%s\".\n", basedir);
continue;
}
preset_path = buffer;
if ((ret = video_shader_write_preset(preset_path,
shader, save_reference)))
{
RARCH_LOG("[Shaders] Saved shader preset to \"%s\".\n", preset_path);
break;
}
else
RARCH_WARN("[Shaders] Failed writing shader preset to \"%s\".\n", preset_path);
}
if (!ret)
RARCH_ERR("[Shaders] Failed to write shader preset. Make sure shader directory "
"and/or config directory are writable.\n");
}
if (ret && apply)
menu_shader_manager_set_preset(NULL, type, preset_path, true);
return ret;
}
/**
* menu_shader_manager_save_preset:
* @shader : shader to save
* @type : type of shader preset which determines save path
* @basename : basename of preset
* @apply : immediately set preset after saving
*
* Save a shader preset to disk.
**/
bool menu_shader_manager_save_preset(const struct video_shader *shader,
const char *basename,
const char *dir_video_shader,
const char *dir_menu_config,
bool apply)
{
char config_directory[DIR_MAX_LENGTH];
const char *preset_dirs[3] = {0};
bool preset_save_ref_enable = config_get_ptr()->
bools.video_shader_preset_save_reference_enable;
if (path_is_empty(RARCH_PATH_CONFIG))
config_directory[0] = '\0';
else
fill_pathname_basedir(config_directory,
path_get(RARCH_PATH_CONFIG),
sizeof(config_directory));
preset_dirs[0] = dir_video_shader;
preset_dirs[1] = dir_menu_config;
preset_dirs[2] = config_directory;
return menu_shader_manager_save_preset_internal(
preset_save_ref_enable,
shader, basename,
dir_video_shader,
apply,
preset_dirs,
ARRAY_SIZE(preset_dirs));
}
static bool menu_shader_manager_operate_auto_preset(
enum auto_shader_operation op,
const struct video_shader *shader,
const char *dir_video_shader,
const char *dir_menu_config,
enum auto_shader_type type, bool apply)
{
char file[PATH_MAX_LENGTH];
char old_presets_directory[DIR_MAX_LENGTH];
char config_directory[DIR_MAX_LENGTH];
bool video_shader_preset_save_reference_enable = config_get_ptr()->
bools.video_shader_preset_save_reference_enable;
struct retro_system_info *sysinfo = &runloop_state_get_ptr()->system.info;
static enum rarch_shader_type shader_types[] =
{
RARCH_SHADER_GLSL, RARCH_SHADER_SLANG, RARCH_SHADER_CG
};
static enum display_flags shader_types_flags[] =
{
GFX_CTX_FLAGS_SHADERS_GLSL, GFX_CTX_FLAGS_SHADERS_SLANG, GFX_CTX_FLAGS_SHADERS_CG
};
const char *core_name = sysinfo ? sysinfo->library_name : NULL;
const char *rarch_path_basename = path_get(RARCH_PATH_BASENAME);
const char *auto_preset_dirs[3] = {0};
bool has_content = rarch_path_basename && *rarch_path_basename;
if (type != SHADER_PRESET_GLOBAL && (!core_name || !*core_name))
return false;
if ( !has_content
&& ((type == SHADER_PRESET_GAME)
|| (type == SHADER_PRESET_PARENT)))
return false;
if (path_is_empty(RARCH_PATH_CONFIG))
config_directory[0] = '\0';
else
fill_pathname_basedir(
config_directory,
path_get(RARCH_PATH_CONFIG),
sizeof(config_directory));
/* We are only including this directory for compatibility purposes with
* versions 1.8.7 and older. */
if (op != AUTO_SHADER_OP_SAVE && dir_video_shader && *dir_video_shader)
fill_pathname_join_special(
old_presets_directory,
dir_video_shader,
"presets",
sizeof(old_presets_directory));
else
old_presets_directory[0] = '\0';
auto_preset_dirs[0] = dir_menu_config;
auto_preset_dirs[1] = config_directory;
auto_preset_dirs[2] = old_presets_directory;
switch (type)
{
case SHADER_PRESET_GLOBAL:
strlcpy_lit(file, "global", sizeof(file));
break;
case SHADER_PRESET_CORE:
fill_pathname_join_special(file, core_name, core_name, sizeof(file));
break;
case SHADER_PRESET_PARENT:
{
char tmp_dir[DIR_MAX_LENGTH];
fill_pathname_parent_dir_name(tmp_dir,
rarch_path_basename, sizeof(tmp_dir));
fill_pathname_join_special(file, core_name, tmp_dir, sizeof(file));
}
break;
case SHADER_PRESET_GAME:
{
const char *game_name = path_basename(rarch_path_basename);
if (!game_name || !*game_name)
return false;
fill_pathname_join_special(file, core_name, game_name, sizeof(file));
break;
}
case SHADER_PRESET_CURRENT:
{
const char *current = video_shader_get_current_shader_preset();
if (!current || !*current)
return false;
strlcpy(file, current, sizeof(file));
path_remove_extension(file);
}
break;
default:
return false;
}
switch (op)
{
case AUTO_SHADER_OP_SAVE:
return menu_shader_manager_save_preset_internal(
video_shader_preset_save_reference_enable,
shader, file,
dir_video_shader,
apply,
auto_preset_dirs,
ARRAY_SIZE(auto_preset_dirs));
case AUTO_SHADER_OP_REMOVE:
{
/* remove all supported auto-shaders of given type */
char *end;
size_t i, j, m;
char preset_path[PATH_MAX_LENGTH];
gfx_ctx_flags_t flags;
/* n = amount of relevant shader presets found
* m = amount of successfully deleted shader presets */
size_t n = m = 0;
flags.flags = 0;
video_context_driver_get_flags(&flags);
for (i = 0; i < ARRAY_SIZE(auto_preset_dirs); i++)
{
size_t _len2;
if (!auto_preset_dirs[i] || !*auto_preset_dirs[i])
continue;
_len2 = fill_pathname_join(preset_path,
auto_preset_dirs[i], file, sizeof(preset_path));
end = preset_path + _len2;
for (j = 0; j < ARRAY_SIZE(shader_types); j++)
{
if (!(BIT32_GET(flags.flags, shader_types_flags[j])))
continue;
strlcpy(end, video_shader_get_preset_extension(shader_types[j]),
sizeof(preset_path) - (end - preset_path));
if (path_is_valid(preset_path))
{
n++;
if (!filestream_delete(preset_path))
{
m++;
RARCH_LOG("[Shaders] Deleted shader preset from \"%s\".\n", preset_path);
}
else
RARCH_WARN("[Shaders] Failed to remove shader preset at \"%s\".\n", preset_path);
}
}
}
return n == m;
}
case AUTO_SHADER_OP_EXISTS:
{
/* test if any supported auto-shaders of given type exists */
char *end;
size_t i, j;
gfx_ctx_flags_t flags;
char preset_path[PATH_MAX_LENGTH];
flags.flags = 0;
video_context_driver_get_flags(&flags);
for (i = 0; i < ARRAY_SIZE(auto_preset_dirs); i++)
{
size_t _len2;
if (!auto_preset_dirs[i] || !*auto_preset_dirs[i])
continue;
_len2 = fill_pathname_join(preset_path,
auto_preset_dirs[i], file, sizeof(preset_path));
end = preset_path + _len2;
for (j = 0; j < ARRAY_SIZE(shader_types); j++)
{
if (!(BIT32_GET(flags.flags, shader_types_flags[j])))
continue;
strlcpy(end, video_shader_get_preset_extension(shader_types[j]),
sizeof(preset_path) - (end - preset_path));
if (path_is_valid(preset_path))
return true;
}
}
}
break;
}
return false;
}
/**
* menu_shader_manager_remove_auto_preset:
* @type : type of shader preset to delete
*
* Deletes an auto-shader.
**/
bool menu_shader_manager_remove_auto_preset(
enum auto_shader_type type,
const char *dir_video_shader,
const char *dir_menu_config)
{
return menu_shader_manager_operate_auto_preset(
AUTO_SHADER_OP_REMOVE, NULL,
dir_video_shader,
dir_menu_config,
type, false);
}
/**
* menu_shader_manager_auto_preset_exists:
* @type : type of shader preset
*
* Tests if an auto-shader of the given type exists.
**/
bool menu_shader_manager_auto_preset_exists(
enum auto_shader_type type,
const char *dir_video_shader,
const char *dir_menu_config)
{
return menu_shader_manager_operate_auto_preset(
AUTO_SHADER_OP_EXISTS, NULL,
dir_video_shader,
dir_menu_config,
type, false);
}
/**
* menu_shader_manager_save_auto_preset:
* @shader : shader to save
* @type : type of shader preset which determines save path
* @apply : immediately set preset after saving
*
* Save a shader as an auto-shader to it's appropriate path:
* SHADER_PRESET_GLOBAL: /global
* SHADER_PRESET_CORE: //
* SHADER_PRESET_PARENT: //
* SHADER_PRESET_GAME: //
* Needs to be consistent with video_shader_load_auto_shader_preset()
* Auto-shaders will be saved as a reference if possible
**/
bool menu_shader_manager_save_auto_preset(
const struct video_shader *shader,
enum auto_shader_type type,
const char *dir_video_shader,
const char *dir_menu_config,
bool apply)
{
return menu_shader_manager_operate_auto_preset(
AUTO_SHADER_OP_SAVE, shader,
dir_video_shader,
dir_menu_config,
type, apply);
}
#endif
/* "Find a Button by Pressing It", in a port's controls: the menu waits
* for a button or a stick of that port's controller and puts the
* selection on its entry. The devices are read for it here, while the screen
* is up, and nowhere else. */
#define MENU_REMAP_FIND_SCREEN "input_remap_find_listen"
#define MENU_REMAP_FIND_TIMEOUT_US 5000000
static struct
{
retro_time_t deadline;
unsigned port;
int found; /* the button pressed or the stick's
* direction pushed, or -1 */
bool released; /* every one has been seen let go */
} menu_remap_find;
void menu_input_remap_find_begin(unsigned port)
{
menu_displaylist_info_t info;
struct menu_state *menu_st = &menu_driver_state;
menu_list_t *menu_list = menu_st->entries.list;
file_list_t *menu_stack = menu_list ? MENU_LIST_GET(menu_list, (unsigned)0) : NULL;
if (!menu_stack)
return;
menu_remap_find.port = port;
menu_remap_find.found = -1;
menu_remap_find.released = false;
menu_remap_find.deadline = cpu_features_get_time_usec()
+ MENU_REMAP_FIND_TIMEOUT_US;
menu_displaylist_info_init(&info);
info.list = menu_stack;
info.type = MENU_SETTING_ACTION;
info.directory_ptr = menu_st->selection_ptr;
info.enum_idx = MENU_ENUM_LABEL_INPUT_REMAP_FIND;
info.label = strdup(MENU_REMAP_FIND_SCREEN);
if (menu_displaylist_ctl(DISPLAYLIST_INFO, &info, config_get_ptr()))
menu_displaylist_process(&info);
menu_displaylist_info_free(&info);
}
/* A combination's buttons set by holding them (input_entry_capture_start()).
* While it runs the menu is in the state "find a button by pressing it"
* puts it in - the list faded behind a message, and no input taken by
* the menu, OK and Back being pressed for the combination like any
* other button - for as long as it runs, whatever list is up. Nothing
* is pushed onto the menu's stack for it: a list pushed from inside a
* row's action came up as a file browser, and stayed. */
static unsigned menu_entry_capture_number;
bool menu_input_entry_capture_begin(unsigned number)
{
if (!input_entry_capture_start(number))
return false;
menu_entry_capture_number = number;
return true;
}
/* One pass while it is up; true when it is over. */
static bool menu_input_entry_capture_iterate(char *s, size_t len)
{
unsigned left;
if (!input_entry_capture_running())
return true;
left = input_entry_capture_seconds_left();
if (left)
snprintf(s, len, "Combination %u: hold its buttons together, then"
" let go. (%u)", menu_entry_capture_number, left);
else
snprintf(s, len, "Combination %u: let go of all of them to set"
" it.", menu_entry_capture_number);
return false;
}
/* One pass while the screen is up. Returns true when it is over: a
* button was pressed and let go again, or the time ran out. The first
* press counts only once everything has been seen released - the
* button that opened this screen is still down when it comes up - and
* the screen stays until the button found is let go, so that it does
* not act on the list it lands on. */
static bool menu_input_remap_find_iterate(char *s, size_t len,
retro_time_t current_time)
{
uint32_t held = input_driver_user_controls_bound(menu_remap_find.port);
if (menu_remap_find.found >= 0)
return held == 0;
if (!menu_remap_find.released)
{
if (!held)
menu_remap_find.released = true;
}
else if (held)
{
unsigned id;
/* a button before a stick, when both answer */
for (id = 0; id < RARCH_ANALOG_BIND_LIST_END; id++)
if (held & (1U << id))
break;
menu_remap_find.found = (int)id;
return false;
}
if (current_time >= menu_remap_find.deadline)
return true;
snprintf(s, len, msg_hash_to_str(MSG_INPUT_REMAP_FIND_PRESS),
menu_remap_find.port + 1,
(unsigned)((menu_remap_find.deadline - current_time) / 1000000) + 1);
return false;
}
static enum action_iterate_type action_iterate_type(const char *label, struct menu_state *menu_st)
{
/* a combination being set by holding its buttons: over any list */
if (input_entry_capture_running())
return ITERATE_TYPE_ENTRY_CAPTURE;
if ( (menu_st->dialog_st.confirm_msg && menu_st->dialog_st.confirm_cmd)
|| menu_st->dialog_st.button_count)
return ITERATE_TYPE_CONFIRM;
if (!strcmp(label, "info_screen"))
return ITERATE_TYPE_INFO;
if (string_starts_with_size(label, "help", STRLEN_CONST("help")))
if (!strcmp(label, "help"))
return ITERATE_TYPE_HELP;
if (!strcmp(label, "cheevos_description"))
return ITERATE_TYPE_HELP;
if (string_starts_with_size(label, "custom_bind", STRLEN_CONST("custom_bind")))
if ( !strcmp(label, "custom_bind")
|| !strcmp(label, "custom_bind_all")
|| !strcmp(label, "custom_bind_defaults"))
return ITERATE_TYPE_BIND;
if (!strcmp(label, MENU_REMAP_FIND_SCREEN))
return ITERATE_TYPE_REMAP_FIND;
return ITERATE_TYPE_DEFAULT;
}
/* Returns true if search filter is enabled
* for the specified menu list */
bool menu_driver_search_filter_enabled(const char *label, unsigned type)
{
bool filter_enabled = false;
/* > Check for playlists */
filter_enabled = (type == MENU_SETTING_HORIZONTAL_MENU)
|| (type == MENU_HISTORY_TAB)
|| (type == MENU_FAVORITES_TAB)
|| (type == MENU_IMAGES_TAB)
|| (type == MENU_MUSIC_TAB)
|| (type == MENU_VIDEO_TAB)
|| (type == FILE_TYPE_PLAYLIST_COLLECTION);
if (!filter_enabled && label && *label)
filter_enabled = string_is_equal(label, MENU_ENUM_LABEL_LOAD_CONTENT_HISTORY_STR)
|| string_is_equal(label, MENU_ENUM_LABEL_DEFERRED_FAVORITES_LIST_STR)
|| string_is_equal(label, MENU_ENUM_LABEL_DEFERRED_IMAGES_LIST_STR)
|| string_is_equal(label, MENU_ENUM_LABEL_DEFERRED_MUSIC_LIST_STR)
|| string_is_equal(label, MENU_ENUM_LABEL_DEFERRED_VIDEO_LIST_STR)
/* > Core updater */
|| string_is_equal(label, MENU_ENUM_LABEL_DEFERRED_CORE_UPDATER_LIST_STR)
/* > File browser (Load Content) */
|| string_is_equal(label, MENU_ENUM_LABEL_FAVORITES_STR)
/* > Shader presets/passes */
|| string_is_equal(label, MENU_ENUM_LABEL_VIDEO_SHADER_PRESET_STR)
|| string_is_equal(label, MENU_ENUM_LABEL_VIDEO_SHADER_PRESET_PREPEND_STR)
|| string_is_equal(label, MENU_ENUM_LABEL_VIDEO_SHADER_PRESET_APPEND_STR)
|| string_is_equal(label, MENU_ENUM_LABEL_VIDEO_SHADER_PASS_STR)
/* > Cheat files */
|| string_is_equal(label, MENU_ENUM_LABEL_CHEAT_FILE_LOAD_STR)
|| string_is_equal(label, MENU_ENUM_LABEL_CHEAT_FILE_LOAD_APPEND_STR)
/* > Cheats */
|| string_is_equal(label, MENU_ENUM_LABEL_CORE_CHEAT_OPTIONS_STR)
/* > Overlays */
|| string_is_equal(label, MENU_ENUM_LABEL_INPUT_OVERLAY_STR)
/* > Manage Cores */
|| string_is_equal(label, MENU_ENUM_LABEL_DEFERRED_CORE_MANAGER_LIST_STR);
return filter_enabled;
}
static void menu_input_key_bind_poll_bind_state(
unsigned joy_idx,
struct menu_bind_state *state,
bool timed_out)
{
unsigned b;
bool state_inited = input_driver_has_device_state();
unsigned port = state->port;
struct menu_bind_state_port *ports;
/* last frame's stays as it is, to be compared with */
state->now = !state->now;
ports = state->state[state->now];
for (b = 0; b < MAX_USERS; b++)
if (b == port || ((state->listen >> b) & 1))
memset(&ports[b], 0, sizeof(ports[b]));
if (state_inited)
{
/* Poll mouse (on the relevant port)
*
* Check if key was being pressed by
* the mouse of the user being bound
*
* NOTE: We start iterating on 2 (RETRO_DEVICE_ID_MOUSE_LEFT),
* because we want to skip the axes
*/
for (b = 2; b < MENU_MAX_MBUTTONS; b++)
{
if (input_driver_bind_capture_state(
joy_idx,
state->user,
RETRO_DEVICE_MOUSE, 0, b))
BIT32_SET(ports[port].mouse_buttons, b);
}
input_driver_bind_capture_keys(joy_idx, ports[port].keys);
}
state->skip = timed_out;
/* the controller itself: polled, then read as it has it */
input_driver_capture_pad(port, true,
&ports[port].buttons, MENU_MAX_BUTTONS,
ports[port].axes, MENU_MAX_AXES,
ports[port].hats, MENU_MAX_HATS);
/* and for a hotkey, the controllers sharing its core port */
for (b = 0; b < MAX_USERS; b++)
if (b != port && ((state->listen >> b) & 1))
input_driver_capture_pad(b, false,
&ports[b].buttons, MENU_MAX_BUTTONS,
ports[b].axes, MENU_MAX_AXES,
ports[b].hats, MENU_MAX_HATS);
}
MENU_NOINLINE static int menu_dialog_iterate(
menu_dialog_t *p_dialog,
settings_t *settings,
char *s, size_t len,
retro_time_t current_time
)
{
switch (p_dialog->current_type)
{
case MENU_DIALOG_WELCOME:
{
static rarch_timer_t timer;
if (!timer.timer_begin)
{
timer.timeout_us = 3 * 1000000;
timer.current = cpu_features_get_time_usec();
timer.timeout_end = timer.current + timer.timeout_us;
timer.timer_begin = true;
timer.timer_end = false;
}
timer.current = current_time;
timer.timeout_us = (timer.timeout_end = timer.current);
msg_hash_get_help_enum(
MENU_ENUM_LABEL_WELCOME_TO_RETROARCH,
s, len);
if (!timer.timer_end && (timer.timeout_us <= 0))
{
timer.timer_end = true;
timer.timer_begin = false;
timer.timeout_end = 0;
p_dialog->current_type = MENU_DIALOG_NONE;
return 1;
}
}
break;
#ifdef HAVE_CHEEVOS
case MENU_DIALOG_HELP_CHEEVOS_DESCRIPTION:
if (!rcheevos_menu_get_help_text(p_dialog->current_id, s, len))
return 1;
break;
#endif
case MENU_DIALOG_HELP_EXTRACT:
{
bool bundle_finished = settings->bools.bundle_finished;
msg_hash_get_help_enum(
MENU_ENUM_LABEL_VALUE_EXTRACTING_PLEASE_WAIT,
s, len);
if (bundle_finished)
{
configuration_set_bool(settings,
settings->bools.bundle_finished, false);
p_dialog->current_type = MENU_DIALOG_NONE;
return 1;
}
}
break;
case MENU_DIALOG_QUIT_CONFIRM:
case MENU_DIALOG_INFORMATION:
case MENU_DIALOG_QUESTION:
case MENU_DIALOG_WARNING:
case MENU_DIALOG_ERROR:
msg_hash_get_help_enum(MSG_UNKNOWN,
s, len);
break;
case MENU_DIALOG_NONE:
default:
break;
}
return 0;
}
static bool menu_entries_init(
struct menu_state *menu_st,
const menu_ctx_driver_t *menu_driver_ctx)
{
if (!(menu_st->entries.list = (menu_list_t*)menu_list_new(menu_driver_ctx)))
return false;
if (!(menu_st->entries.list_settings = menu_setting_new()))
return false;
return true;
}
static void generic_menu_init_list(struct menu_state *menu_st,
settings_t *settings)
{
menu_displaylist_info_t info;
menu_list_t *menu_list = menu_st->entries.list;
file_list_t *menu_stack = NULL;
file_list_t *selection_buf = NULL;
if (menu_list)
{
menu_stack = MENU_LIST_GET(menu_list, (unsigned)0);
selection_buf = MENU_LIST_GET_SELECTION(menu_list, (unsigned)0);
}
menu_displaylist_info_init(&info);
info.label = strdup(
MENU_ENUM_LABEL_MAIN_MENU_STR);
info.enum_idx = MENU_ENUM_LABEL_MAIN_MENU;
menu_entries_append(menu_stack,
info.path,
info.label,
MENU_ENUM_LABEL_MAIN_MENU,
info.type, info.flags, 0, NULL);
info.list = selection_buf;
if (menu_displaylist_ctl(DISPLAYLIST_MAIN_MENU, &info, settings))
menu_displaylist_process(&info);
menu_displaylist_info_free(&info);
}
/* This function gets called at first startup on Android/iOS
* when we need to extract the APK contents/zip file. This
* file contains assets which then get extracted to the
* user's asset directories. */
static void bundle_decompressed(retro_task_t *task,
void *task_data,
void *user_data, const char *err)
{
settings_t *settings = config_get_ptr();
decompress_task_data_t *dec = (decompress_task_data_t*)task_data;
if (err)
RARCH_ERR("[Bundle] %s", err);
if (dec)
{
if (!err)
command_event(CMD_EVENT_REINIT, NULL);
/* delete bundle? */
free(dec->source_file);
free(dec);
}
configuration_set_uint(settings,
settings->uints.bundle_assets_extract_last_version,
settings->uints.bundle_assets_extract_version_current);
configuration_set_bool(settings, settings->bools.bundle_finished, true);
command_event(CMD_EVENT_MENU_SAVE_CURRENT_CONFIG, NULL);
}
#if defined(HAVE_ONLINE_UPDATER) && defined(HAVE_NETWORKING) && defined(HAVE_COMPRESSION) && defined(HAVE_CONFIGFILE)
enum menu_first_run_dl
{
MENU_FIRST_RUN_DL_ASSETS = (1 << 0),
MENU_FIRST_RUN_DL_AUTOCONFIG = (1 << 1),
MENU_FIRST_RUN_DL_CORE_INFO = (1 << 2),
MENU_FIRST_RUN_DL_DATABASES = (1 << 3),
MENU_FIRST_RUN_DL_OVERLAYS = (1 << 4)
};
/* A directory that is configured but missing or empty */
static bool menu_first_run_dir_missing(const char *dir)
{
return *dir && (!path_is_directory(dir) || path_is_empty_directory(dir));
}
#ifdef HAVE_UPDATE_ASSETS
/* A UWP install holds an assets directory with only an empty
* wallpapers folder in it, so a look at the top level alone calls
* it complete. Where Ozone is built in, its own folder is the one
* to ask: when that has files the parent has them too, so the
* parent needs no look of its own. */
static bool menu_first_run_assets_missing(const char *dir)
{
#ifdef HAVE_OZONE
char ozone[PATH_MAX_LENGTH];
if (!*dir)
return false;
fill_pathname_join_special(ozone, dir, "ozone", sizeof(ozone));
return menu_first_run_dir_missing(ozone);
#else
return menu_first_run_dir_missing(dir);
#endif
}
#endif
/* On a first run the directory usually does not exist yet, so ask the
* nearest existing parent. An unknown answer (-1) does not hold the
* download back; only a known shortfall does. */
static bool menu_first_run_has_space(const char *dir, int64_t need)
{
char tmp[PATH_MAX_LENGTH];
strlcpy(tmp, dir, sizeof(tmp));
while (*tmp && !path_is_directory(tmp))
path_parent_dir(tmp, strlen(tmp));
if (*tmp)
{
int64_t avail = path_get_free_space(tmp);
if (avail >= 0 && avail < need)
return false;
}
return true;
}
/* Which bundles are missing; scanned once, at menu init */
static uint8_t menu_first_run_scan(settings_t *settings)
{
uint8_t mask = 0;
#ifdef HAVE_UPDATE_ASSETS
if ( menu_first_run_assets_missing(settings->paths.directory_assets)
&& menu_first_run_has_space(settings->paths.directory_assets,
ASSETS_ZIP_PLUS_DECOMPRESSION_SIZE))
mask |= MENU_FIRST_RUN_DL_ASSETS;
#endif
if (menu_first_run_dir_missing(settings->paths.directory_autoconfig))
mask |= MENU_FIRST_RUN_DL_AUTOCONFIG;
#ifdef HAVE_UPDATE_CORE_INFO
if (menu_first_run_dir_missing(settings->paths.path_libretro_info))
mask |= MENU_FIRST_RUN_DL_CORE_INFO;
#endif
#ifdef HAVE_LIBRETRODB
if ( menu_first_run_dir_missing(settings->paths.path_content_database)
&& menu_first_run_has_space(settings->paths.path_content_database,
DATABASE_RDB_ZIP_PLUS_DECOMPRESSION_SIZE))
mask |= MENU_FIRST_RUN_DL_DATABASES;
#endif
#if defined(RARCH_MOBILE) && defined(HAVE_OVERLAY)
if (menu_first_run_dir_missing(settings->paths.directory_overlay))
mask |= MENU_FIRST_RUN_DL_OVERLAYS;
#endif
return mask;
}
/* Confirmed: push the downloads found at init, through the same path
* as the Online Updater entries (streamed straight to disk) */
void menu_first_run_download_start(void)
{
uint8_t mask = menu_driver_state.dialog_st.first_run_downloads;
menu_driver_state.dialog_st.first_run_downloads = 0;
if (mask & MENU_FIRST_RUN_DL_ASSETS)
action_ok_download_bundle(MENU_ENUM_LABEL_CB_UPDATE_ASSETS);
if (mask & MENU_FIRST_RUN_DL_AUTOCONFIG)
action_ok_download_bundle(MENU_ENUM_LABEL_CB_UPDATE_AUTOCONFIG_PROFILES);
if (mask & MENU_FIRST_RUN_DL_CORE_INFO)
action_ok_download_bundle(MENU_ENUM_LABEL_CB_UPDATE_CORE_INFO_FILES);
if (mask & MENU_FIRST_RUN_DL_DATABASES)
action_ok_download_bundle(MENU_ENUM_LABEL_CB_UPDATE_DATABASES);
if (mask & MENU_FIRST_RUN_DL_OVERLAYS)
action_ok_download_bundle(MENU_ENUM_LABEL_CB_UPDATE_OVERLAYS);
}
#endif
static bool rarch_menu_init(
struct menu_state *menu_st,
menu_dialog_t *p_dialog,
const menu_ctx_driver_t *menu_driver_ctx,
menu_input_t *menu_input,
menu_input_pointer_hw_state_t *pointer_hw_state,
settings_t *settings
)
{
#ifdef HAVE_CONFIGFILE
bool menu_show_start_screen = settings->bools.menu_show_start_screen;
bool config_save_on_exit = settings->bools.config_save_on_exit;
#endif
/* thumbnail initialization */
if (!(menu_st->thumbnail_path_data = gfx_thumbnail_path_init()))
return false;
/* Ensure that menu pointer input is correctly
* initialised */
memset(menu_input, 0, sizeof(menu_input_t));
memset(pointer_hw_state, 0, sizeof(menu_input_pointer_hw_state_t));
if (!menu_entries_init(menu_st, menu_driver_ctx))
{
menu_entries_settings_deinit(menu_st);
if (menu_st->entries.list)
menu_list_free(menu_driver_ctx, menu_st->entries.list);
menu_st->entries.list = NULL;
return false;
}
#ifdef HAVE_CONFIGFILE
if (menu_show_start_screen)
{
/* We don't want the welcome dialog screen to show up
* again after the first startup, so we save to config
* file immediately. */
p_dialog->current_type = MENU_DIALOG_WELCOME;
configuration_set_bool(settings,
settings->bools.menu_show_start_screen, false);
if (config_save_on_exit)
command_event(CMD_EVENT_MENU_SAVE_CURRENT_CONFIG, NULL);
}
#endif
#ifdef HAVE_COMPRESSION
if ( settings->bools.bundle_assets_extract_enable
&& *settings->paths.bundle_assets_src
&& *settings->paths.bundle_assets_dst
&& (settings->uints.bundle_assets_extract_version_current
!= settings->uints.bundle_assets_extract_last_version)
)
{
p_dialog->current_type = MENU_DIALOG_HELP_EXTRACT;
task_push_decompress(
settings->paths.bundle_assets_src,
settings->paths.bundle_assets_dst,
NULL,
settings->paths.bundle_assets_dst_subdir,
NULL,
bundle_decompressed,
NULL,
NULL,
false);
/* Support only 1 version - setting this would prevent
* the assets from being extracted every time */
configuration_set_int(settings,
settings->uints.bundle_assets_extract_last_version, 1);
}
#if defined(HAVE_ONLINE_UPDATER) && defined(HAVE_NETWORKING) && defined(HAVE_CONFIGFILE)
/* First run with nothing bundled: offer what is missing */
else if (menu_show_start_screen)
{
p_dialog->first_run_downloads = menu_first_run_scan(settings);
if (p_dialog->first_run_downloads)
menu_dialog_confirm_set(menu_st,
MSG_FIRST_RUN_DOWNLOAD_PROMPT,
CMD_EVENT_MENU_FIRST_RUN_DOWNLOAD);
}
#endif
#endif
return true;
}
MENU_NOINLINE static void menu_input_set_pointer_visibility(
menu_input_pointer_hw_state_t *pointer_hw_state,
menu_input_t *menu_input,
retro_time_t current_time)
{
static bool cursor_shown = false;
static bool cursor_hidden = false;
static retro_time_t end_time = 0;
struct menu_state *menu_st = &menu_driver_state;
#ifdef HAVE_OPENXR
/* The laser's dot is its cursor. */
if (menu_input_headset_drives)
{
if (!cursor_hidden)
{
if (menu_st->driver_ctx->environ_cb)
menu_st->driver_ctx->environ_cb(MENU_ENVIRON_DISABLE_MOUSE_CURSOR,
NULL, menu_st->userdata);
cursor_shown = false;
cursor_hidden = true;
}
/* So a mouse taking over shows it at once. */
end_time = 0;
return;
}
#endif
/* Ensure that mouse cursor is hidden when not in use */
if ( (menu_input->pointer.type == MENU_POINTER_MOUSE)
&& ((pointer_hw_state->flags & MENU_INP_PTR_FLG_ACTIVE) > 0))
{
/* Show cursor */
if ((current_time > end_time) && !cursor_shown)
{
if (menu_st->driver_ctx->environ_cb)
menu_st->driver_ctx->environ_cb(MENU_ENVIRON_ENABLE_MOUSE_CURSOR,
NULL, menu_st->userdata);
cursor_shown = true;
cursor_hidden = false;
}
end_time = current_time + MENU_INPUT_HIDE_CURSOR_DELAY;
}
else
{
/* Hide cursor */
if ((current_time > end_time) && !cursor_hidden)
{
if (menu_st->driver_ctx->environ_cb)
menu_st->driver_ctx->environ_cb(MENU_ENVIRON_DISABLE_MOUSE_CURSOR,
NULL, menu_st->userdata);
cursor_shown = false;
cursor_hidden = true;
}
}
}
void menu_entries_build_scroll_indices(
struct menu_state *menu_st,
file_list_t *list)
{
bool current_is_dir = false;
size_t i = 0;
const char *path = list->list[0].alt
? list->list[0].alt
: list->list[0].path;
int ret = path ? TOLOWER((int)*path) : 0;
int current = ELEM_GET_FIRST_CHAR(ret);
unsigned type = list->list[0].type;
menu_st->scroll.index_list[0] = 0;
menu_st->scroll.index_size = 1;
if (type == FILE_TYPE_DIRECTORY)
current_is_dir = true;
for (i = 1; i < list->size; i++)
{
int first;
bool is_dir = false;
unsigned idx = (unsigned)i;
path = list->list[i].alt
? list->list[i].alt
: list->list[i].path;
ret = path ? TOLOWER((int)*path) : 0;
first = ELEM_GET_FIRST_CHAR(ret);
type = list->list[idx].type;
if (type == FILE_TYPE_DIRECTORY)
is_dir = true;
if ((current_is_dir && !is_dir) || (first != current))
{
/* Add scroll index */
menu_st->scroll.index_list[menu_st->scroll.index_size] = i;
if (!((menu_st->scroll.index_size + 1) >= SCROLL_INDEX_SIZE))
menu_st->scroll.index_size++;
}
current = first;
current_is_dir = is_dir;
}
/* Add scroll index */
menu_st->scroll.index_list[menu_st->scroll.index_size] = list->size - 1;
if (!((menu_st->scroll.index_size + 1) >= SCROLL_INDEX_SIZE))
menu_st->scroll.index_size++;
}
void menu_display_common_image_upload(void *data, void *user_data, unsigned type)
{
struct texture_image *img = (struct texture_image*)data;
struct menu_state *menu_st = &menu_driver_state;
const menu_ctx_driver_t *menu_driver_ctx = menu_st->driver_ctx;
void *menu_userdata = menu_st->userdata;
if ( menu_driver_ctx
&& menu_driver_ctx->load_image)
menu_driver_ctx->load_image(menu_userdata,
img, (enum menu_image_type)type);
image_texture_free(img);
free(img);
free(user_data);
}
static enum menu_driver_id_type menu_driver_set_id(
const char *driver_name)
{
if (driver_name && *driver_name)
{
#ifdef HAVE_RGUI
if (!strcmp(driver_name, "rgui"))
return MENU_DRIVER_ID_RGUI;
#endif
#ifdef HAVE_OZONE
if (!strcmp(driver_name, "ozone"))
return MENU_DRIVER_ID_OZONE;
#endif
#ifdef HAVE_MATERIALUI
if (!strcmp(driver_name, "glui"))
return MENU_DRIVER_ID_GLUI;
#endif
#ifdef HAVE_XMB
if (!strcmp(driver_name, "xmb"))
return MENU_DRIVER_ID_XMB;
#endif
}
return MENU_DRIVER_ID_UNKNOWN;
}
static bool menu_entries_search_push(const char *search_term)
{
size_t i;
char search_term_clipped[MENU_SEARCH_FILTER_MAX_LENGTH];
menu_search_terms_t *search = menu_entries_search_get_terms_alloc();
/* Sanity check + verify whether we have reached
* the maximum number of allowed search terms */
if ( !search
|| (!search_term || !*search_term)
|| (search->size >= MENU_SEARCH_FILTER_MAX_TERMS))
return false;
/* Check whether search term already exists
* > Note that we clip the input search term
* to MENU_SEARCH_FILTER_MAX_LENGTH characters
* *before* comparing existing entries */
strlcpy(search_term_clipped, search_term,
sizeof(search_term_clipped));
for (i = 0; i < search->size; i++)
{
if (string_is_equal(search_term_clipped,
search->terms[i]))
return false;
}
/* Add search term */
strlcpy(search->terms[search->size], search_term_clipped,
sizeof(search->terms[search->size]));
search->size++;
return true;
}
bool menu_entries_search_pop(void)
{
menu_search_terms_t *search = menu_entries_search_get_terms_internal();
/* Do nothing if list of search terms is empty */
if ( !search
|| (search->size == 0))
return false;
/* Remove last item from the list */
search->size--;
search->terms[search->size][0] = '\0';
return true;
}
menu_search_terms_t *menu_entries_search_get_terms(void)
{
menu_search_terms_t *search = menu_entries_search_get_terms_internal();
if ( !search
|| (search->size == 0))
return NULL;
return search;
}
void menu_entries_search_append_terms_string(char *s, size_t len)
{
menu_search_terms_t *search = menu_entries_search_get_terms_internal();
if ( search
&& (search->size > 0)
&& s)
{
size_t i;
size_t _len = strlen(s);
/* If buffer is already 'full', nothing
* further can be added */
if (_len >= len)
return;
for (i = 0; i < search->size; i++)
{
/* strlcpy() reports the length it was handed rather than the
* length it wrote, so an append that truncates would carry
* _len past len and leave the next len - _len wrapping to a
* very large size_t. Eight terms of MENU_SEARCH_FILTER_MAX_LENGTH
* do not fit a title, so this is reached by typing. */
size_t tlen = strlen(search->terms[i]);
if (_len + 3 >= len)
break;
_len += strlcpy_lit(s + _len, " > ", len - _len);
if (_len + tlen >= len)
{
strlcpy(s + _len, search->terms[i], len - _len);
break;
}
_len += strlcpy(s + _len, search->terms[i], len - _len);
}
}
}
#ifdef HAVE_ACCESSIBILITY
static size_t get_current_menu_value(
struct menu_state *menu_st, char *s, size_t len)
{
menu_entry_t entry;
MENU_ENTRY_INITIALIZE(entry);
entry.flags |= MENU_ENTRY_FLAG_VALUE_ENABLED;
menu_entry_get(&entry, 0, menu_st->selection_ptr, NULL, true);
if (entry.enum_idx == MENU_ENUM_LABEL_CHEEVOS_PASSWORD)
return strlcpy(s, entry.password_value, len);
return strlcpy(s, entry.value, len);
}
static size_t menu_driver_get_current_menu_label(struct menu_state *menu_st,
char *s, size_t len)
{
menu_entry_t entry;
MENU_ENTRY_INITIALIZE(entry);
entry.flags |= MENU_ENTRY_FLAG_PATH_ENABLED
| MENU_ENTRY_FLAG_LABEL_ENABLED
| MENU_ENTRY_FLAG_RICH_LABEL_ENABLED
| MENU_ENTRY_FLAG_VALUE_ENABLED
| MENU_ENTRY_FLAG_SUBLABEL_ENABLED;
menu_entry_get(&entry, 0, menu_st->selection_ptr, NULL, true);
if (*entry.rich_label)
return strlcpy(s, entry.rich_label, len);
return strlcpy(s, entry.path, len);
}
#endif
static size_t menu_driver_get_current_menu_sublabel(
struct menu_state *menu_st, char *s, size_t len)
{
menu_entry_t entry;
MENU_ENTRY_INITIALIZE(entry);
entry.flags |= MENU_ENTRY_FLAG_SUBLABEL_ENABLED;
menu_entry_get(&entry, 0, menu_st->selection_ptr, NULL, true);
return strlcpy(s, entry.sublabel, len);
}
void menu_entries_get_last_stack(const char **path, const char **label,
unsigned *file_type, enum msg_hash_enums *enum_idx, size_t *entry_idx)
{
file_list_t *list = NULL;
struct menu_state *menu_st = &menu_driver_state;
if (!menu_st->entries.list)
return;
list = MENU_LIST_GET(menu_st->entries.list, 0);
if (list && list->size)
{
if (path)
*path = list->list[list->size - 1].path;
if (label)
*label = list->list[list->size - 1].label;
if (file_type)
*file_type = list->list[list->size - 1].type;
if (entry_idx)
*entry_idx = list->list[list->size - 1].entry_idx;
}
if (enum_idx)
{
menu_file_list_cbs_t *cbs = (menu_file_list_cbs_t*)
list->list[list->size - 1].actiondata;
if (cbs)
*enum_idx = cbs->enum_idx;
}
}
int menu_driver_deferred_push_content_list(file_list_t *list)
{
settings_t *settings = config_get_ptr();
struct menu_state *menu_st = &menu_driver_state;
menu_list_t *menu_list = menu_st->entries.list;
file_list_t *selection_buf = MENU_LIST_GET_SELECTION(menu_list, (unsigned)0);
menu_st->selection_ptr = 0;
menu_st->contentless_core_ptr = 0;
menu_contentless_cores_flush_runtime();
if (!menu_driver_displaylist_push(
menu_st,
settings,
list,
selection_buf))
return -1;
return 0;
}
retro_time_t menu_driver_get_current_time(void)
{
struct menu_state *menu_st = &menu_driver_state;
return menu_st->current_time_us;
}
void menu_driver_set_pending_selection(const char *pending_selection)
{
struct menu_state *menu_st = &menu_driver_state;
char *selection = menu_st->pending_selection;
/* Reset existing cache */
selection[0] = '\0';
if (pending_selection && *pending_selection)
strlcpy(selection, pending_selection,
sizeof(menu_st->pending_selection));
}
static void menu_input_search_cb(void *userdata, const char *str)
{
const char *label = NULL;
unsigned type = MENU_SETTINGS_NONE;
struct menu_state *menu_st = &menu_driver_state;
const file_list_t *list = MENU_LIST_GET(menu_st->entries.list, 0);
if (!str || !*str)
goto end;
/* Determine whether we are currently
* viewing a menu list with 'search
* filter' support */
if (list && list->size)
{
label = list->list[list->size - 1].label;
type = list->list[list->size - 1].type;
}
/* Do not apply search filter if string
* consists of a single Latin alphabet
* character */
if ( ((str[1] != '\0') || (!ISALPHA(str[0])))
&& menu_driver_search_filter_enabled(label, type))
{
/* Add search term */
if (menu_entries_search_push(str))
{
/* Reset navigation pointer */
menu_st->selection_ptr = 0;
if (menu_st->driver_ctx->navigation_set)
menu_st->driver_ctx->navigation_set(menu_st->userdata, false);
/* Refresh menu */
menu_st->flags |= MENU_ST_FLAG_PREVENT_POPULATE
| MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
}
}
/* Perform a regular search: jump to the
* first matching entry */
else
{
size_t idx = 0;
if (menu_entries_list_search(str, &idx))
{
menu_st->selection_ptr = idx;
if (menu_st->driver_ctx->navigation_set)
menu_st->driver_ctx->navigation_set(menu_st->userdata, true);
}
}
end:
menu_input_dialog_end();
}
int menu_entry_action(menu_entry_t *entry, size_t i, enum menu_action action)
{
struct menu_state *menu_st = &menu_driver_state;
if ( menu_st->driver_ctx
&& menu_st->driver_ctx->entry_action)
return menu_st->driver_ctx->entry_action(
menu_st->userdata, entry, i, action);
return -1;
}
static struct item_file *menu_entry_drag_drop_item(size_t i,
const struct string_list *payload)
{
menu_list_t *menu_list = menu_driver_state.entries.list;
file_list_t *selection_buf = menu_list
? MENU_LIST_GET_SELECTION(menu_list, (unsigned)0) : NULL;
if ( !selection_buf
|| i >= selection_buf->size
|| !payload
|| !payload->size
|| !selection_buf->list[i].actiondata)
return NULL;
return &selection_buf->list[i];
}
int menu_entry_drag(size_t i, const struct string_list *payload)
{
struct item_file *item = menu_entry_drag_drop_item(i, payload);
menu_file_list_cbs_t *cbs = item
? (menu_file_list_cbs_t*)item->actiondata : NULL;
if (!cbs || !cbs->action_drag)
return -1;
return cbs->action_drag(item->path, item->label, item->type,
i, item->entry_idx, payload);
}
int menu_entry_drop(size_t i, const struct string_list *payload)
{
struct item_file *item = menu_entry_drag_drop_item(i, payload);
menu_file_list_cbs_t *cbs = item
? (menu_file_list_cbs_t*)item->actiondata : NULL;
if (!cbs || !cbs->action_drop)
return -1;
return cbs->action_drop(item->path, item->label, item->type,
i, item->entry_idx, payload);
}
#ifdef RETRO_ATOMIC_HAS_PTR
/* Platform drop callbacks may run on the video thread; the menu takes
* the payload on the main thread in menu_driver_iterate(). */
static retro_atomic_ptr_t menu_drop_pending;
static retro_atomic_int_t menu_drop_accept;
#endif
bool menu_driver_drop(struct string_list *payload)
{
#ifdef RETRO_ATOMIC_HAS_PTR
if ( payload
&& payload->size
&& retro_atomic_load_acquire_int(&menu_drop_accept))
{
string_list_free((struct string_list*)
retro_atomic_exchange_ptr(&menu_drop_pending, payload));
return true;
}
#endif
string_list_free(payload);
return false;
}
bool menu_driver_drop_uri_list(char *list)
{
union string_list_elem_attr attr;
struct string_list *files = string_list_new();
char *line = list;
if (!files)
return false;
attr.i = 0;
while (line && *line)
{
char *next = strchr(line, '\n');
if (next)
*next++ = '\0';
line[strcspn(line, "\r")] = '\0';
if ( *line != '#'
&& string_starts_with_size(line, "file://",
STRLEN_CONST("file://")))
{
/* Skip the authority, normally empty or "localhost". */
char *path = strchr(line + STRLEN_CONST("file://"), '/');
if ( path
&& string_percent_decode(path, strlen(path) + 1, path) > 0
&& !string_list_append(files, path, attr))
{
string_list_free(files);
return false;
}
}
line = next;
}
return menu_driver_drop(files);
}
static void menu_driver_drop_accept(bool accept)
{
#ifdef RETRO_ATOMIC_HAS_PTR
retro_atomic_store_release_int(&menu_drop_accept, accept ? 1 : 0);
if (!accept)
string_list_free((struct string_list*)
retro_atomic_exchange_ptr(&menu_drop_pending, NULL));
#endif
}
bool menu_entries_append(
file_list_t *list,
const char *path,
const char *label,
enum msg_hash_enums enum_idx,
unsigned type,
size_t directory_ptr,
size_t entry_idx,
rarch_setting_t *setting)
{
menu_ctx_list_t list_info;
size_t idx, lbl_len;
const char *menu_path = NULL;
menu_file_list_cbs_t *cbs = NULL;
struct menu_state *menu_st = &menu_driver_state;
const file_list_t *mlist = MENU_LIST_GET(menu_st->entries.list, 0);
if (!list || !label)
return false;
if (!(cbs = menu_cbs_alloc()))
return false;
/* Bail out on a failed append rather than carrying on with
* idx = list->size - 1, which underflows to SIZE_MAX when the
* list is still empty. */
if (!file_list_append(list, path, label, type, directory_ptr, entry_idx))
{
mempool_free(&menu_cbs_pool, cbs);
return false;
}
if (mlist && mlist->size)
menu_path = mlist->list[mlist->size - 1].path;
idx = list->size - 1;
/* The menu path is handed to list_insert() by pointer rather than
* copied. It is the same string for every entry of a list, so the
* strdup()/free() pair this replaces ran once per appended entry --
* tens of thousands of times for a MAME or FBNeo playlist -- to
* hand each driver a private copy none of them needed.
*
* Checked against all three hooks that take it: xmb_list_insert()
* and ozone_list_insert() strdup() their own copy into the node,
* materialui_list_insert() only compares and atoi()s it, and rgui
* has no list_insert at all. None of them appends to or frees a
* menu list, so nothing can reallocate mlist->list underneath the
* pointer while they hold it. */
list_info.fullpath = (menu_path && *menu_path) ? menu_path : NULL;
list_info.list = list;
list_info.path = path;
list_info.label = label;
list_info.idx = idx;
list_info.entry_type = type;
if ( menu_st->driver_ctx &&
menu_st->driver_ctx->list_insert)
menu_st->driver_ctx->list_insert(
menu_st->userdata,
list_info.list,
list_info.path,
list_info.fullpath,
list_info.label,
list_info.idx,
list_info.entry_type);
file_list_free_actiondata(list, idx);
/* The cbs comes from a pool, so it cannot be released with the bare
* free() that file_list_free_actiondata() falls back to. Installing
* the destructor here rather than at each list's construction is
* what makes that impossible to get wrong: any list that receives a
* cbs learns how to release one in the same statement, including a
* stack-local file_list_t that menu_displaylist_build_list() is
* handed directly. */
list->actiondata_free = menu_entries_cbs_free;
cbs->enum_idx = enum_idx;
cbs->checked = false;
cbs->setting = setting;
cbs->action_iterate = NULL;
cbs->action_deferred_push = NULL;
cbs->action_select = NULL;
cbs->action_get_title = NULL;
cbs->action_ok = NULL;
cbs->action_cancel = NULL;
cbs->action_scan = NULL;
cbs->action_start = NULL;
cbs->action_drag = NULL;
cbs->action_drop = NULL;
cbs->action_info = NULL;
cbs->action_left = NULL;
cbs->action_right = NULL;
cbs->action_label = NULL;
cbs->action_sublabel = NULL;
cbs->action_get_value = NULL;
cbs->search = NULL;
list->list[idx].actiondata = cbs;
if (!cbs->setting && enum_idx != MSG_UNKNOWN)
{
if ( enum_idx != MENU_ENUM_LABEL_PLAYLIST_ENTRY
&& enum_idx != MENU_ENUM_LABEL_PLAYLIST_COLLECTION_ENTRY
&& enum_idx != MENU_ENUM_LABEL_EXPLORE_ITEM
&& enum_idx != MENU_ENUM_LABEL_CONTENTLESS_CORE
&& enum_idx != MENU_ENUM_LABEL_RDB_ENTRY)
cbs->setting = menu_setting_find_enum(enum_idx);
}
lbl_len = strlen(label);
menu_cbs_init(menu_st,
menu_st->driver_ctx,
list, cbs, path, label, lbl_len, type, idx);
return true;
}
void menu_entries_prepend(file_list_t *list,
const char *path, const char *label,
enum msg_hash_enums enum_idx,
unsigned type, size_t directory_ptr, size_t entry_idx)
{
size_t lbl_len;
menu_ctx_list_t list_info;
size_t idx = 0;
const char *menu_path = NULL;
menu_file_list_cbs_t *cbs = NULL;
struct menu_state *menu_st = &menu_driver_state;
const file_list_t *mlist = MENU_LIST_GET(menu_st->entries.list, 0);
if (!list || !label)
return;
if (!(cbs = menu_cbs_alloc()))
return;
if (!file_list_insert(list, path, label, type, directory_ptr, entry_idx, 0))
{
mempool_free(&menu_cbs_pool, cbs);
return;
}
if (mlist && mlist->size)
menu_path = mlist->list[mlist->size - 1].path;
/* See menu_entries_append(): handed over by pointer, not copied. */
list_info.fullpath = (menu_path && *menu_path) ? menu_path : NULL;
list_info.list = list;
list_info.path = path;
list_info.label = label;
list_info.idx = idx;
list_info.entry_type = type;
if ( menu_st->driver_ctx &&
menu_st->driver_ctx->list_insert)
menu_st->driver_ctx->list_insert(
menu_st->userdata,
list_info.list,
list_info.path,
list_info.fullpath,
list_info.label,
list_info.idx,
list_info.entry_type);
file_list_free_actiondata(list, idx);
/* See the matching comment in menu_entries_append(). */
list->actiondata_free = menu_entries_cbs_free;
cbs->enum_idx = enum_idx;
cbs->checked = false;
cbs->setting = menu_setting_find_enum(cbs->enum_idx);
cbs->action_iterate = NULL;
cbs->action_deferred_push = NULL;
cbs->action_select = NULL;
cbs->action_get_title = NULL;
cbs->action_ok = NULL;
cbs->action_cancel = NULL;
cbs->action_scan = NULL;
cbs->action_start = NULL;
cbs->action_drag = NULL;
cbs->action_drop = NULL;
cbs->action_info = NULL;
cbs->action_left = NULL;
cbs->action_right = NULL;
cbs->action_label = NULL;
cbs->action_sublabel = NULL;
cbs->action_get_value = NULL;
cbs->search = NULL;
list->list[idx].actiondata = cbs;
lbl_len = strlen(label);
menu_cbs_init(menu_st,
menu_st->driver_ctx,
list, cbs, path, label, lbl_len, type, idx);
}
void menu_entries_flush_stack(const char *needle, unsigned final_type)
{
struct menu_state *menu_st = &menu_driver_state;
menu_list_t *menu_list = menu_st->entries.list;
if (menu_list)
menu_list_flush_stack(
menu_st->driver_ctx,
menu_st->userdata,
menu_st,
menu_list, 0, needle, final_type);
}
void menu_entries_pop_stack(size_t *ptr, size_t idx, bool animate)
{
struct menu_state *menu_st = &menu_driver_state;
const menu_ctx_driver_t *menu_driver_ctx = menu_st->driver_ctx;
menu_list_t *menu_list = menu_st->entries.list;
if (!menu_list)
return;
if (MENU_LIST_GET_STACK_SIZE(menu_list, idx) > 1)
{
if (animate)
{
if (menu_driver_ctx->list_cache)
menu_driver_ctx->list_cache(menu_st->userdata,
MENU_LIST_PLAIN, 0);
}
menu_list_pop_stack(menu_driver_ctx,
menu_st->userdata, menu_list, idx, ptr);
if (animate)
menu_st->flags |= MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
}
}
bool menu_entries_clear(file_list_t *list)
{
size_t i;
struct menu_state *menu_st = &menu_driver_state;
if (!list)
return false;
/* Clear all the menu lists. */
if (menu_st->driver_ctx->list_clear)
menu_st->driver_ctx->list_clear(list);
for (i = 0; i < list->size; i++)
file_list_free_actiondata(list, i);
file_list_clear(list);
return true;
}
/* Function that gets called when we want to load in a
* new menu wallpaper.
*/
void menu_display_handle_wallpaper_upload(
retro_task_t *task,
void *task_data,
void *user_data, const char *err)
{
menu_display_common_image_upload(
(struct texture_image*)task_data,
user_data,
MENU_IMAGE_WALLPAPER);
}
void menu_driver_frame(bool menu_is_alive, video_frame_info_t *video_info)
{
struct menu_state *menu_st = &menu_driver_state;
if (menu_is_alive && menu_st->driver_ctx->frame)
{
gfx_display_t *p_disp = disp_get_ptr();
menu_st->driver_ctx->frame(menu_st->userdata, video_info);
/* Nothing the menu gathered may still be waiting when the frame
* it belongs to is over */
gfx_display_flush_batch(p_disp);
if (menu_driver_media_startup_token())
{
static float black[16] = {
0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
video_driver_state_t *video_st = video_state_get_ptr();
/* Menu drivers restore the content viewport before returning.
* Cover the entire window, including pillar/letterbox areas. */
if (video_st->current_video && video_st->current_video->set_viewport)
video_st->current_video->set_viewport(video_st->data,
video_info->dims, true, false);
gfx_display_draw_quad(p_disp, video_info->userdata,
video_info->dims, 0, 0, video_info->dims,
video_info->dims, black, NULL);
gfx_display_flush_batch(p_disp);
if (video_st->current_video && video_st->current_video->set_viewport)
video_st->current_video->set_viewport(video_st->data,
video_info->dims, false, true);
gfx_display_stats_latch(p_disp);
return;
}
/* The statistics overlay, on top of the menu when it is not
* hidden here; drivers only draw it themselves without a menu. */
if ( video_info->statistics_show
&& !video_info->statistics_hide_in_menu
&& video_info->stat_text_len)
font_driver_render_msg(video_info->userdata,
video_info->stat_text, video_info->stat_text_len,
(const struct font_params*)&video_info->osd_stat_params, NULL);
gfx_display_stats_latch(p_disp);
}
}
/* Teardown function for the menu driver. */
void menu_driver_destroy(
struct menu_state *menu_st)
{
menu_pointer_outside_press = false;
if (!task_content_load_pending() || (menu_driver_media_startup_token() & 1))
menu_driver_media_open(NULL);
menu_st->flags &= ~(MENU_ST_FLAG_PENDING_QUICK_MENU
| MENU_ST_FLAG_PREVENT_POPULATE
| MENU_ST_FLAG_DATA_OWN
| MENU_ST_FLAG_ALIVE);
menu_st->driver_ctx = NULL;
menu_st->userdata = NULL;
input_driver_hold_clear();
}
void menu_input_get_pointer_state(menu_input_pointer_t *copy_target)
{
struct menu_state *menu_st = &menu_driver_state;
menu_input_t *menu_input = &menu_st->input_state;
/* Copy parameters from global menu_input_state
* (i.e. don't pass by reference)
* This is a fast operation */
if (copy_target)
memcpy(copy_target, &menu_input->pointer, sizeof(menu_input_pointer_t));
}
/* Whether the menu is up, for code that has no other business with
* the menu's state. */
bool menu_driver_alive(void)
{
return (menu_driver_state.flags & MENU_ST_FLAG_ALIVE) != 0;
}
/* The label of the text entry that is open. */
const char *menu_input_dialog_get_kb_label(void)
{
return menu_driver_state.input_dialog_kb_label;
}
const char *menu_input_dialog_get_buffer(void)
{
struct menu_state *menu_st = &menu_driver_state;
if (!(*menu_st->input_dialog_keyboard_buffer))
return "";
return *menu_st->input_dialog_keyboard_buffer;
}
/* This callback gets triggered by the keyboard whenever
* we press or release a keyboard key. When a keyboard
* key is being pressed down, 'down' will be true. If it
* is being released, 'down' will be false.
*/
static void menu_input_key_event(bool down, unsigned keycode,
uint32_t character, uint16_t mod)
{
struct menu_state *menu_st = &menu_driver_state;
enum retro_key key = (enum retro_key)keycode;
if (key == RETROK_UNKNOWN)
{
unsigned i;
for (i = 0; i < RETROK_LAST; i++)
menu_st->kb_key_state[i] =
(menu_st->kb_key_state[(enum retro_key)i] & 1) << 1;
}
else
menu_st->kb_key_state[key] =
((menu_st->kb_key_state[key] & 1) << 1) | down;
}
void menu_input_dialog_end(void)
{
struct menu_state *menu_st = &menu_driver_state;
menu_st->input_dialog_kb_idx = 0;
menu_st->input_dialog_kb_text_type = MENU_INPUT_DIALOG_KB_TYPE_TEXT;
menu_st->flags &= ~MENU_ST_FLAG_INP_DLG_KB_DISPLAY;
menu_st->input_dialog_kb_label[0] = '\0';
menu_st->input_dialog_kb_label_setting[0] = '\0';
/* Avoid triggering states on pressing return. */
/* Held back until everything held now is let go. */
input_driver_hold_held_input();
#ifdef HAVE_COCOATOUCH
/* Dismiss iOS/tvOS native keyboard if it's currently open */
if (ios_keyboard_active())
ios_keyboard_end();
#endif
#ifdef ANDROID
/* Dismiss the Android system keyboard if it's currently open */
if (android_keyboard_active())
android_keyboard_end();
#endif
}
#if defined(_MSC_VER)
static const char * msvc_vercode_to_str(const unsigned vercode)
{
switch (vercode)
{
case 1200:
return " msvc6";
case 1300:
return " msvc2002";
case 1310:
return " msvc2003";
case 1400:
return " msvc2005";
case 1500:
return " msvc2008";
case 1600:
return " msvc2010";
case 1700:
return " msvc2012";
case 1800:
return " msvc2013";
case 1900:
return " msvc2015";
default:
if (vercode >= 1910 && vercode < 1920)
return " msvc2017";
else if (vercode >= 1920 && vercode < 1930)
return " msvc2019";
else if (vercode >= 1930 && vercode < 1950)
return " msvc2022";
else if (vercode >= 1950)
return " msvc2026";
break;
}
return "";
}
#endif
/* Sets 's' to the name of the current core
* (shown at the top of the UI). */
void menu_entries_get_core_title(char *s, size_t len)
{
/* A content load in flight swaps the core under the menu over
* several frames - the next core's library opened, the old one
* closed, the next one brought up: the title stays what it was
* until the load is through, and changes once. */
static char held[NAME_MAX_LENGTH + 64];
struct retro_system_info *sysinfo = &runloop_state_get_ptr()->system.info;
const char *core_name =
(sysinfo && (sysinfo->library_name && *sysinfo->library_name))
? sysinfo->library_name
: msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NO_CORE);
const char *core_version =
(sysinfo && sysinfo->library_version)
? sysinfo->library_version
: "";
size_t _len;
if (runloop_is_content_switching() && *held)
{
strlcpy(s, held, len);
return;
}
_len = strlcpy(s, PACKAGE_VERSION, len);
#if defined(_MSC_VER)
_len += strlcpy(s + _len, msvc_vercode_to_str(_MSC_VER), len - _len);
#endif
_len += strlcpy_lit(s + _len, " - ", len - _len);
_len += strlcpy(s + _len, core_name, len - _len);
if (core_version && *core_version)
{
_len += strlcpy_lit(s + _len, " (", len - _len);
_len += strlcpy(s + _len, core_version, len - _len);
strlcpy_lit(s + _len, ")", len - _len);
}
strlcpy(held, s, sizeof(held));
}
static bool menu_driver_init_internal(
struct menu_state *menu_st,
gfx_display_t *p_disp,
settings_t *settings,
bool video_is_threaded)
{
if (menu_st->driver_ctx)
{
const char *ident = menu_st->driver_ctx->ident;
/* ID must be set first, since it is required for
* the proper determination of pixel/DPI scaling
* parameters (and some menu drivers fetch the
* current pixel/dpi scale during 'menu_driver_ctx->init()') */
if (ident)
p_disp->menu_driver_id = menu_driver_set_id(ident);
else
p_disp->menu_driver_id = MENU_DRIVER_ID_UNKNOWN;
if (menu_st->driver_ctx->init)
{
menu_st->driver_data = (menu_handle_t*)
menu_st->driver_ctx->init(&menu_st->userdata,
video_is_threaded);
/* init returns NULL on failure; the check below handles it. */
if (menu_st->driver_data)
{
menu_st->driver_data->userdata = menu_st->userdata;
menu_st->driver_data->driver_ctx = menu_st->driver_ctx;
}
}
}
if (!menu_st->driver_data || !rarch_menu_init(
menu_st,
&menu_st->dialog_st,
menu_st->driver_ctx,
&menu_st->input_state,
&menu_st->input_pointer_hw_state,
settings))
return false;
gfx_display_init();
/* TODO/FIXME - can we get rid of this? Is this needed? */
configuration_set_string(settings,
settings->arrays.menu_driver, menu_st->driver_ctx->ident);
if (menu_st->driver_ctx->lists_init)
{
if (!menu_st->driver_ctx->lists_init(menu_st->driver_data))
return false;
}
else
generic_menu_init_list(menu_st, settings);
/* Set startup page */
if (settings->uints.menu_startup_page != MENU_STARTUP_PAGE_MAIN_MENU)
menu_st->flags |= MENU_ST_FLAG_PENDING_STARTUP_PAGE;
/* Initialise menu screensaver */
menu_st->input_last_time_us = cpu_features_get_time_usec();
menu_st->flags &= ~MENU_ST_FLAG_SCREENSAVER_ACTIVE;
if ( menu_st->driver_ctx->environ_cb
&& (menu_st->driver_ctx->environ_cb(MENU_ENVIRON_DISABLE_SCREENSAVER,
NULL, menu_st->userdata) == 0))
menu_st->flags |= MENU_ST_FLAG_SCREENSAVER_SUPPORTED;
else
menu_st->flags &= ~MENU_ST_FLAG_SCREENSAVER_SUPPORTED;
return true;
}
bool menu_driver_init(bool video_is_threaded)
{
gfx_display_t *p_disp = disp_get_ptr();
settings_t *settings = config_get_ptr();
struct menu_state *menu_st = &menu_driver_state;
command_event(CMD_EVENT_CORE_INFO_INIT, NULL);
/* With a core up, its system info came from its own handle at
* init; the probe would only reopen it and replay its
* retro_set_environment. */
if (!(runloop_state_get_ptr()->current_core.flags
& RETRO_CORE_FLAG_SYMBOLS_INITED))
command_event(CMD_EVENT_LOAD_CORE_PERSIST, NULL);
if ( menu_st->driver_data
|| menu_driver_init_internal(
menu_st, p_disp, settings,
video_is_threaded))
{
if (menu_st->driver_ctx && menu_st->driver_ctx->context_reset)
{
menu_st->driver_ctx->context_reset(menu_st->userdata,
video_is_threaded);
return true;
}
}
/* If driver initialisation failed, must reset
* driver id to 'unknown' */
p_disp->menu_driver_id = MENU_DRIVER_ID_UNKNOWN;
return false;
}
void menu_driver_context_rebuild(void)
{
struct menu_state *menu_st = &menu_driver_state;
if (!menu_st->driver_ctx || !menu_st->userdata)
return;
if (menu_driver_media_startup_token() & 1)
menu_driver_media_open(NULL);
/* context_reset loads every texture into its slot without looking
* at what the slot held, so on its own it leaks the set already
* loaded. Release that set first, as on a video driver swap. */
if (menu_st->driver_ctx->context_destroy)
menu_st->driver_ctx->context_destroy(menu_st->userdata);
if (menu_st->driver_ctx->context_reset)
menu_st->driver_ctx->context_reset(menu_st->userdata,
video_driver_is_threaded());
}
const char *menu_driver_ident(void)
{
struct menu_state *menu_st = &menu_driver_state;
if (menu_st->driver_ctx && menu_st->driver_ctx->ident)
return menu_st->driver_ctx->ident;
return NULL;
}
const menu_ctx_driver_t *menu_driver_find_driver(
settings_t *settings,
const char *prefix,
bool verbosity_enabled)
{
int i = (int)driver_find_index("menu_driver",
settings->arrays.menu_driver);
if (i >= 0)
return (const menu_ctx_driver_t*)menu_ctx_drivers[i];
if (verbosity_enabled)
{
unsigned d;
RARCH_WARN("Couldn't find any %s named \"%s\".\n", prefix,
settings->arrays.menu_driver);
RARCH_LOG_OUTPUT("Available %ss are:\n", prefix);
for (d = 0; menu_ctx_drivers[d]; d++)
{
if (menu_ctx_drivers[d])
{
RARCH_LOG_OUTPUT("\t%s\n", menu_ctx_drivers[d]->ident);
}
}
RARCH_WARN("Going to default to first %s...\n", prefix);
}
return (const menu_ctx_driver_t*)menu_ctx_drivers[0];
}
#ifdef USE_CUSTOM_BIND_KEYBOARD_CB
static bool menu_input_key_bind_custom_bind_keyboard_cb(
void *data, unsigned code)
{
settings_t *settings = config_get_ptr();
struct menu_state *menu_st = &menu_driver_state;
struct menu_bind_state *binds = &menu_st->input_binds;
uint64_t input_bind_hold_us = settings->uints.input_bind_hold * 1000000;
uint64_t input_bind_timeout_us = settings->uints.input_bind_timeout * 1000000;
uint64_t current_usec = cpu_features_get_time_usec();
/* Clear old mapping bit */
input_keyboard_mapping_bits(0, RETRO_KEYBIND_KEY(&binds->buffer));
/* Store key in bind */
RETRO_KEYBIND_SET_KEY(&binds->buffer, (enum retro_key)code);
/* Store new mapping bit */
input_keyboard_mapping_bits(1, RETRO_KEYBIND_KEY(&binds->buffer));
/* Write out the bind */
*input_config_bind_edit(binds->out_user, binds->out_id) = binds->buffer;
/* Next bind: asked for by its number, not found by stepping along
* the user's row. */
binds->begin++;
binds->out_user = binds->user;
binds->out_id = binds->begin - MENU_SETTINGS_BIND_BEGIN;
binds->buffer = *input_config_bind(
binds->out_user, binds->out_id);
binds->timer_hold.timeout_us = input_bind_hold_us;
binds->timer_hold.current = current_usec;
binds->timer_hold.timeout_end = current_usec + input_bind_hold_us;
binds->timer_timeout.timeout_us = input_bind_timeout_us;
binds->timer_timeout.current = current_usec;
binds->timer_timeout.timeout_end = current_usec + input_bind_timeout_us;
return (binds->begin <= binds->last);
}
#endif
void menu_input_plays_as_change(unsigned row, unsigned player)
{
settings_t *settings = config_get_ptr();
struct menu_state *menu_st = &menu_driver_state;
bool slot = row < MAX_INPUT_DEVICES;
/* gone, or back, since the list was made: the list is made again */
if (slot
? !input_config_get_device_name(row)
: input_config_plays_as_entry_taken(row - MAX_INPUT_DEVICES))
{
menu_st->flags &= ~MENU_ST_FLAG_PREVENT_POPULATE;
menu_st->flags |= MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
return;
}
/* the same player: nothing is placed again */
if (player == (slot
? input_config_plays_as_player(row)
: input_config_plays_as_entry_player(row - MAX_INPUT_DEVICES)))
return;
if (slot)
{
if (!input_config_plays_as_set(row, player))
{
const char *msg = msg_hash_to_str(MSG_INPUT_PLAYS_AS_FULL);
runloop_msg_queue_push(msg, strlen(msg), 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_WARNING);
return;
}
input_autoconfigure_reallocate_ports(row);
}
else
input_config_plays_as_set_entry(row - MAX_INPUT_DEVICES, player);
settings->flags |= SETTINGS_FLG_MODIFIED;
/* the rows follow the controllers: the list is made again, with
* the cursor kept on this one */
if (menu_st->driver_data)
menu_st->driver_data->plays_as_row = row + 1;
menu_st->flags &= ~MENU_ST_FLAG_PREVENT_POPULATE;
menu_st->flags |= MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
}
bool menu_input_key_bind_set_mode(
enum menu_input_binds_ctl_state state, void *data)
{
uint64_t current_usec;
unsigned index_offset;
rarch_setting_t *setting = (rarch_setting_t*)data;
struct menu_state *menu_st = &menu_driver_state;
menu_handle_t *menu = menu_st->driver_data;
menu_input_t *menu_input = &menu_st->input_state;
settings_t *settings = config_get_ptr();
struct menu_bind_state *binds = &menu_st->input_binds;
uint64_t input_bind_hold_us = settings->uints.input_bind_hold
* 1000000;
uint64_t input_bind_timeout_us = settings->uints.input_bind_timeout
* 1000000;
if (!setting || !menu)
return false;
if (menu_input_key_bind_set_mode_common(menu_st,
binds, state, setting, settings) == -1)
return false;
index_offset = setting->index_offset;
binds->port = settings->uints.input_joypad_index[
index_offset];
/* a hotkey is bound from user 1's pad or a controller that fires
* the hotkeys, which keeps the press as its own */
binds->listen = (binds->out_id >= RARCH_FIRST_META_KEY)
? input_config_hotkey_listen_slots() : 0;
binds->from = binds->port;
binds->from_keys = false;
/* neither what was held nor what was locked in the capture before */
memset(binds->state, 0, sizeof(binds->state));
memset(binds->axis_state, 0, sizeof(binds->axis_state));
menu_input_key_bind_poll_bind_get_rested_axes(binds);
menu_input_key_bind_poll_bind_state(binds->port, binds, false);
current_usec = cpu_features_get_time_usec();
binds->timer_hold .timeout_us = input_bind_hold_us;
binds->timer_hold .current = current_usec;
binds->timer_hold .timeout_end = current_usec + input_bind_hold_us;
binds->timer_timeout.timeout_us = input_bind_timeout_us;
binds->timer_timeout.current = current_usec;
binds->timer_timeout.timeout_end = current_usec + input_bind_timeout_us;
#ifdef USE_CUSTOM_BIND_KEYBOARD_CB
input_driver_set_keyboard_press_cb(menu_input_key_bind_custom_bind_keyboard_cb, menu);
#endif
/* While waiting for input, we have to block all hotkeys. */
input_driver_set_keyboard_mapping_blocked(true);
/* Wait until keys are released before starting bind timeout. */
input_driver_set_wait_input_release(true);
/* Upon triggering an input bind operation,
* pointer input must be inhibited - otherwise
* attempting to bind mouse buttons will cause
* spurious menu actions */
menu_input->select_inhibit = true;
menu_input->cancel_inhibit = true;
return true;
}
MENU_NOINLINE static bool menu_input_key_bind_iterate(
settings_t *settings,
menu_input_ctx_bind_t *bind,
retro_time_t current_time)
{
char heard[128];
bool timed_out = false;
struct menu_state *menu_st = &menu_driver_state;
struct menu_bind_state *_binds = &menu_st->input_binds;
menu_input_t *menu_input = &menu_st->input_state;
uint64_t input_bind_hold_us = settings->uints.input_bind_hold * 1000000;
uint64_t input_bind_timeout_us = settings->uints.input_bind_timeout * 1000000;
unsigned bind_id = _binds->begin - MENU_SETTINGS_BIND_BEGIN;
/* a hotkey says whose controllers it hears */
heard[0] = '\0';
if (bind_id >= RARCH_FIRST_META_KEY && bind_id < RARCH_BIND_LIST_END)
{
size_t _len = strlcpy(heard, "\n", sizeof(heard));
snprintf(heard + _len, sizeof(heard) - _len,
msg_hash_to_str(MSG_INPUT_BIND_LISTENING_PLAYER),
input_config_user_player(input_config_hotkey_user()) + 1);
}
snprintf(bind->s, bind->len,
"%s..\n(%s %1.1f %s)\n \n%s%s\n \n",
msg_hash_to_str(MSG_INPUT_BIND_PRESS),
msg_hash_to_str(MSG_INPUT_BIND_TIMEOUT),
((float)_binds->timer_timeout.timeout_us / 1000000),
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SECONDS),
input_config_bind_map_get_desc(bind_id),
heard);
/* Tick main timers */
_binds->timer_hold .current = current_time;
_binds->timer_hold .timeout_us = _binds->timer_hold .timeout_end - current_time;
_binds->timer_timeout.current = current_time;
_binds->timer_timeout.timeout_us = _binds->timer_timeout.timeout_end - current_time;
if (_binds->timer_timeout.timeout_us <= 0)
{
input_driver_set_keyboard_mapping_blocked(false);
/* Give up on first timeout */
return true;
}
/* binds.begin is updated in keyboard_press callback. */
if (_binds->begin > _binds->last)
{
/* Avoid new binds triggering things right away. */
/* Held back until everything held now is let go. */
input_driver_hold_held_input();
/* We won't be getting any key events, so just cancel early. */
if (timed_out)
{
input_driver_set_keyboard_press_cb(NULL, NULL);
input_driver_set_keyboard_mapping_blocked(false);
}
return true;
}
{
bool complete = false;
bool skipped = _binds->skip;
unsigned bind_index = _binds->begin - MENU_SETTINGS_BIND_BEGIN;
const struct retro_keybind *old_binds = input_config_bind(_binds->out_user, bind_index);
unsigned old_key = RETRO_KEYBIND_KEY(old_binds);
input_driver_set_keyboard_mapping_blocked(false);
_binds->from = _binds->port;
_binds->from_keys = false;
menu_input_key_bind_poll_bind_state(_binds->port,
_binds, timed_out);
/* Wait until keys and buttons are released */
if (input_driver_waiting_input_release())
{
if (input_bind_hold_us)
{
if (!menu_input_key_bind_poll_find_hold(
_binds, &(_binds->buffer)))
input_driver_set_wait_input_release(false);
}
else
{
if (!menu_input_key_bind_poll_find_trigger(
_binds, &(_binds->buffer)))
input_driver_set_wait_input_release(false);
}
if (!input_driver_waiting_input_release())
{
/* Reset timeout */
_binds->timer_timeout.timeout_us = input_bind_timeout_us;
_binds->timer_timeout.current = current_time;
_binds->timer_timeout.timeout_end = current_time + input_bind_timeout_us;
}
else
snprintf(bind->s, bind->len,
"%s..\n(%s %1.1f %s)\n \n--- %s ---%s\n \n",
msg_hash_to_str(MSG_INPUT_BIND_PRESS),
msg_hash_to_str(MSG_INPUT_BIND_TIMEOUT),
((float)_binds->timer_timeout.timeout_us / 1000000),
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SECONDS),
msg_hash_to_str(MSG_INPUT_BIND_RELEASE),
heard
);
}
else if (input_bind_hold_us)
{
/* Keep resetting bind during the hold period,
* or we'll potentially bind joystick and mouse, etc. */
_binds->buffer = *input_config_bind(
_binds->out_user, _binds->out_id);
if (menu_input_key_bind_poll_find_hold(
_binds, &_binds->buffer))
{
char hold_label[NAME_MAX_LENGTH];
hold_label[0] = '\0';
/* Inhibit timeout */
_binds->timer_timeout.timeout_us = input_bind_timeout_us;
_binds->timer_timeout.current = current_time;
_binds->timer_timeout.timeout_end = current_time + input_bind_timeout_us;
/* Run hold timer */
_binds->timer_hold.current = current_time;
_binds->timer_hold.timeout_us = _binds->timer_hold.timeout_end - current_time;
/* Transient bind under capture: it lives on the stack and has
* no entry in the label arrays, so it carries no labels. */
input_config_get_bind_string(settings, hold_label,
&_binds->buffer, NULL, NULL, NULL, sizeof(hold_label));
snprintf(bind->s, bind->len,
"%s..\n(%s %1.1f %s)\n \n%s\n %s%s",
msg_hash_to_str(MSG_INPUT_BIND_PRESS),
msg_hash_to_str(MSG_INPUT_BIND_HOLD),
((float)_binds->timer_hold.timeout_us / 1000000),
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SECONDS),
input_config_bind_map_get_desc(bind_id),
hold_label,
heard);
/* Hold complete? */
if (_binds->timer_hold.timeout_us <= 0)
complete = true;
}
else
{
/* Reset hold countdown */
_binds->timer_hold.timeout_us = input_bind_hold_us;
_binds->timer_hold.current = current_time;
_binds->timer_hold.timeout_end = current_time + input_bind_hold_us;
}
}
else if ((_binds->skip && !skipped)
|| menu_input_key_bind_poll_find_trigger(
_binds, &(_binds->buffer)))
complete = true;
if (complete)
{
/* Always stop binding when not binding all */
bool stop_binding = _binds->order == 0 && _binds->begin == _binds->last;
/* Update bind */
/* a sharing controller's press goes to its own bind, and that
* of user 1's pad or the hotkey user's does where it has its
* own for the hotkey; keys, mouse buttons and the rest go to
* the set */
if ( !_binds->skip
&& !_binds->from_keys
&& input_config_hotkey_bind_own(_binds->from,
_binds->out_id))
{
/* refused with the controller still connected only when
* the list is full */
if ( !input_config_plays_as_set_hotkey(_binds->from,
_binds->out_id, _binds->buffer.joykey,
_binds->buffer.joyaxis)
&& input_config_get_device_name(_binds->from))
{
const char *msg = msg_hash_to_str(MSG_INPUT_PLAYS_AS_FULL);
runloop_msg_queue_push(msg, strlen(msg), 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT,
MESSAGE_QUEUE_CATEGORY_WARNING);
}
settings->flags |= SETTINGS_FLG_MODIFIED;
}
else
/* written where the bind is kept now, which is asked for
* here: the write is counted as a change when it is made,
* and no address is carried from the frame the capture
* began in */
*input_config_bind_edit(_binds->out_user, _binds->out_id)
= _binds->buffer;
/* Update keyboard mapping bits */
if (RETRO_KEYBIND_KEY(&_binds->buffer))
{
input_keyboard_mapping_bits(0, old_key);
input_keyboard_mapping_bits(1, RETRO_KEYBIND_KEY(&_binds->buffer));
}
/* Avoid new binds triggering things right away. */
/* Held back until everything held now is let go. */
input_driver_hold_held_input();
/* Use human readable order instead */
_binds->order++;
_binds->begin = MENU_SETTINGS_BIND_BEGIN + input_config_bind_order[_binds->order];
if ( _binds->order > ARRAY_SIZE(input_config_bind_order) - 1
|| stop_binding)
{
input_driver_set_keyboard_press_cb(NULL, NULL);
input_driver_set_keyboard_mapping_blocked(false);
return true;
}
input_driver_set_keyboard_mapping_blocked(false);
input_driver_set_wait_input_release(true);
/* Next bind */
_binds->out_user = _binds->user;
_binds->out_id =
input_config_bind_order[_binds->order];
_binds->buffer = *input_config_bind(
_binds->out_user, _binds->out_id);
_binds->timer_hold .timeout_us = input_bind_hold_us;
_binds->timer_hold .current = current_time;
_binds->timer_hold .timeout_end = current_time + input_bind_hold_us;
_binds->timer_timeout.timeout_us = input_bind_timeout_us;
_binds->timer_timeout.current = current_time;
_binds->timer_timeout.timeout_end = current_time + input_bind_timeout_us;
}
}
/* Pointer input must be inhibited on each
* frame that the bind operation is active -
* otherwise attempting to bind mouse buttons
* will cause spurious menu actions */
menu_input->select_inhibit = true;
menu_input->cancel_inhibit = true;
/* Menu screensaver should be inhibited on each
* frame that the bind operation is active */
menu_st->input_last_time_us = menu_st->current_time_us;
return false;
}
/* input_osk_native_active() is true when a platform-native text-entry
* panel currently owns the keyboard line. The built-in on-screen
* keyboard must not append in that case: both paths write into
* the frontend's line of text, and input_driver_osk_press() calls
* input_keyboard_line_append(), which can realloc the buffer out from
* under state the native path is holding. Every backend answers
* through that one function; see input/input_osk.h. */
enum menu_input_dialog_kb_text_type menu_input_dialog_get_kb_text_type(void)
{
return menu_driver_state.input_dialog_kb_text_type;
}
bool menu_input_dialog_get_display_kb(void)
{
struct menu_state *menu_st = &menu_driver_state;
#ifdef HAVE_LIBNX
SwkbdConfig kbd;
Result rc;
/* Indicates that we are "typing" from the swkbd
* result to RetroArch with repeated calls to input_keyboard_event
* This prevents input_keyboard_event from calling back
* menu_input_dialog_get_display_kb, looping indefinintely */
static bool typing = false;
if (typing)
return false;
/* swkbd only works on "real" titles */
if ( __nx_applet_type != AppletType_Application
&& __nx_applet_type != AppletType_SystemApplication)
return ((menu_st->flags & MENU_ST_FLAG_INP_DLG_KB_DISPLAY) > 0);
if (!(menu_st->flags & MENU_ST_FLAG_INP_DLG_KB_DISPLAY))
return false;
rc = swkbdCreate(&kbd, 0);
if (R_SUCCEEDED(rc))
{
unsigned i;
char buf[LIBNX_SWKBD_LIMIT] = {'\0'};
swkbdConfigMakePresetDefault(&kbd);
swkbdConfigSetGuideText(&kbd,
menu_st->input_dialog_kb_label);
rc = swkbdShow(&kbd, buf, sizeof(buf));
swkbdClose(&kbd);
/* RetroArch uses key-by-key input
so we need to simulate it */
typing = true;
for (i = 0; i < LIBNX_SWKBD_LIMIT; i++)
{
/* In case a previous "Enter" press closed the keyboard */
if (!(menu_st->flags & MENU_ST_FLAG_INP_DLG_KB_DISPLAY))
break;
if (buf[i] == '\n' || buf[i] == '\0')
input_keyboard_event(true, '\n', '\n', 0, RETRO_DEVICE_KEYBOARD);
else
{
const char *word = &buf[i];
/* input_keyboard_line_append expects a null-terminated
string, so just make one (yes, the touch keyboard is
a list of "NULL-terminated characters") */
char oldchar = buf[i+1];
buf[i+1] = '\0';
input_driver_keyboard_line_type(word, strlen(word));
buf[i+1] = oldchar;
}
}
/* fail-safe */
if (menu_st->flags & MENU_ST_FLAG_INP_DLG_KB_DISPLAY)
input_keyboard_event(true, '\n', '\n', 0, RETRO_DEVICE_KEYBOARD);
typing = false;
libnx_apply_overclock();
return false;
}
libnx_apply_overclock();
#endif /* HAVE_LIBNX */
return ((menu_st->flags & MENU_ST_FLAG_INP_DLG_KB_DISPLAY) > 0);
}
/* Menu action buttons that need to be processed on key up
* when toggle combos are active */
static unsigned input_combo_type_onkeyup_lut[INPUT_COMBO_LAST] =
{
/* INPUT_COMBO_NONE */ 0,
/* INPUT_COMBO_DOWN_Y_L_R */ (1 << RETRO_DEVICE_ID_JOYPAD_Y) | (1 << RETRO_DEVICE_ID_JOYPAD_L) | (1 << RETRO_DEVICE_ID_JOYPAD_R),
/* INPUT_COMBO_L3_R3 */ (1 << RETRO_DEVICE_ID_JOYPAD_L3) | (1 << RETRO_DEVICE_ID_JOYPAD_R3),
/* INPUT_COMBO_L1_R1_START_SELECT */ (1 << RETRO_DEVICE_ID_JOYPAD_L) | (1 << RETRO_DEVICE_ID_JOYPAD_R) | (1 << RETRO_DEVICE_ID_JOYPAD_START) | (1 << RETRO_DEVICE_ID_JOYPAD_SELECT),
/* INPUT_COMBO_START_SELECT */ (1 << RETRO_DEVICE_ID_JOYPAD_START) | (1 << RETRO_DEVICE_ID_JOYPAD_SELECT),
/* INPUT_COMBO_L3_R */ (1 << RETRO_DEVICE_ID_JOYPAD_L3) | (1 << RETRO_DEVICE_ID_JOYPAD_R),
/* INPUT_COMBO_L_R */ (1 << RETRO_DEVICE_ID_JOYPAD_L) | (1 << RETRO_DEVICE_ID_JOYPAD_R),
/* INPUT_COMBO_HOLD_START */ (1 << RETRO_DEVICE_ID_JOYPAD_START),
/* INPUT_COMBO_HOLD_SELECT */ (1 << RETRO_DEVICE_ID_JOYPAD_SELECT),
/* INPUT_COMBO_DOWN_SELECT */ (1 << RETRO_DEVICE_ID_JOYPAD_SELECT),
/* INPUT_COMBO_L2_R2 */ (1 << RETRO_DEVICE_ID_JOYPAD_L2) | (1 << RETRO_DEVICE_ID_JOYPAD_R2)
};
#define MENU_ACTION_RET(id, action) \
{ \
if (onkeyup & (1 << id)) \
{ \
if (BIT256_GET_PTR(p_input, id)) \
keydown[id] = true; \
else if (keydown[id]) \
{ \
keydown[id] = false; \
ret = action; \
} \
} \
else \
{ \
if (BIT256_GET_PTR(p_trigger_input, id)) \
ret = action; \
} \
} \
/* Set as the menu opens or a pointer press is dropped: menu_event()
* starts its own button memory afresh. Without it, an OK still down
* when the menu last closed (by its hotkey, say) is remembered as down,
* and the menu opening later sees it let go - and an entry that acts on
* release, Resume first of all, acts on the menu's first frame and
* shuts it again. A dropped press would otherwise act as it lifts. */
static bool menu_event_starts_afresh;
/* Set as the menu opens, if it was not up: a press held then is dropped
* as one held across a window change is */
static bool menu_event_opens;
/* A held direction, once Menu Scroll Delay (the user's) has passed:
* the list moves 20 entries a second to start with - what the menu gave
* at 60 Hz - and faster as the hold goes on, by real time and not by
* frames; the steps it moves in are as fine as the display's frames
* allow. Left, Right and the other buttons repeat 20 times a second.
* MENU_REPEAT_INTERVAL_US is that repeat's period and the acceleration's
* step. */
#define MENU_REPEAT_PER_SECOND 20
#define MENU_REPEAT_INTERVAL_US (1000000 / MENU_REPEAT_PER_SECOND)
unsigned menu_event(
settings_t *settings,
input_bits_t *p_input,
input_bits_t *p_trigger_input,
bool display_kb)
{
int i;
/* the mouse or the touchscreen is in use this frame */
bool pointer_active = false;
/* Used for key repeat */
/* The auto-repeat of a held direction, on a clock of real time: the
* first repeat comes the scroll delay after the press, the next ones
* every MENU_REPEAT_INTERVAL_US after that, on a schedule that
* carries over what a frame overshoots - so the rate is the same at
* any refresh rate. */
static retro_time_t hold_start_us = 0;
static retro_time_t next_repeat_us = 0;
/* Up/Down's motion: entries due, in millionths, and when it was
* last worked out */
static retro_time_t list_carry = 0;
static retro_time_t list_last_us = 0;
static bool list_started = false;
static bool holding = false;
static unsigned ok_old = 0;
static uint8_t switch_old = 0;
static size_t ok_enum_idx = 0;
static bool keydown[RARCH_FIRST_CUSTOM_BIND] = {false};
static bool navigation_reset_delay = true;
unsigned ret = MENU_ACTION_NOOP;
uint8_t switch_current = 0;
uint8_t switch_trigger = 0;
struct menu_state *menu_st = &menu_driver_state;
menu_input_t *menu_input = &menu_st->input_state;
gfx_display_t *p_disp = disp_get_ptr();
menu_input_pointer_hw_state_t *pointer_hw_state = &menu_st->input_pointer_hw_state;
menu_handle_t *menu = menu_st->driver_data;
bool menu_mouse_enable = settings->bools.menu_mouse_enable;
bool menu_pointer_enable = settings->bools.menu_pointer_enable;
bool swap_ok_cancel_btns = settings->bools.input_menu_swap_ok_cancel_buttons;
bool swap_scroll_btns = settings->bools.input_menu_swap_scroll_buttons;
bool menu_scroll_fast = settings->bools.menu_scroll_fast;
bool pointer_enabled = settings->bools.menu_pointer_enable;
unsigned input_touch_scale = settings->uints.input_touch_scale;
unsigned menu_scroll_delay = settings->uints.menu_scroll_delay;
#ifdef HAVE_OVERLAY
bool input_overlay_enable = settings->bools.input_overlay_enable;
/* An overlay takes the menu's pointer only if it can use it: its
* page has a desc that does something when pressed, or its own
* pointer (mouse/lightgun) mode is on. A page of "nul" buttons (an
* LED or decoration overlay) leaves the mouse to the menu. */
bool overlay_active = input_overlay_enable
&& ( input_driver_overlay_takes_input()
|| ( input_driver_overlay_active_page()
&& settings->bools.input_overlay_pointer_enable));
#else
bool input_overlay_enable = false;
bool overlay_active = false;
#endif
unsigned menu_ok_btn = swap_ok_cancel_btns ?
RETRO_DEVICE_ID_JOYPAD_B : RETRO_DEVICE_ID_JOYPAD_A;
unsigned menu_cancel_btn = swap_ok_cancel_btns ?
RETRO_DEVICE_ID_JOYPAD_A : RETRO_DEVICE_ID_JOYPAD_B;
unsigned ok_current =
BIT256_GET_PTR(p_input, menu_ok_btn)
|| ( (pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_SELECT)
&& !menu_driver_state.dialog_st.confirm_cmd
&& !menu_driver_state.dialog_st.button_count);
unsigned ok_trigger = ok_current & ~ok_old;
unsigned ok_trigger_release = !ok_current && ok_old;
static unsigned navigation_initial = 0;
unsigned navigation_current = 0;
unsigned navigation_buttons[NAVIGATION_BUTTONS] =
{
RETRO_DEVICE_ID_JOYPAD_UP,
RETRO_DEVICE_ID_JOYPAD_DOWN,
RETRO_DEVICE_ID_JOYPAD_LEFT,
RETRO_DEVICE_ID_JOYPAD_RIGHT,
RETRO_DEVICE_ID_JOYPAD_SELECT,
RETRO_DEVICE_ID_JOYPAD_START,
RETRO_DEVICE_ID_JOYPAD_L,
RETRO_DEVICE_ID_JOYPAD_R,
RETRO_DEVICE_ID_JOYPAD_L2,
RETRO_DEVICE_ID_JOYPAD_R2,
RETRO_DEVICE_ID_JOYPAD_X,
RETRO_DEVICE_ID_JOYPAD_Y
};
if (menu_event_starts_afresh)
{
menu_event_starts_afresh = false;
ok_old = ok_current;
ok_trigger = 0;
ok_trigger_release = 0;
ok_enum_idx = 0;
}
/* Check if all menu input is blocked
* > 'ok_old' must be updated before returning, otherwise the
* button state is frozen for the duration of the block and a
* phantom trigger/release edge is generated against a stale
* 'ok_enum_idx' once input is unblocked */
if (menu_st->flags & MENU_ST_FLAG_BLOCK_ALL_INPUT)
{
ok_old = ok_current;
switch_old = BIT256_GET_PTR(p_input, RETRO_DEVICE_ID_JOYPAD_LEFT)
| BIT256_GET_PTR(p_input, RETRO_DEVICE_ID_JOYPAD_RIGHT);
/* The block ends the hold: a hold that spans it would otherwise
* resume with a repeat already due, at the acceleration it had,
* and the selection would jump several places as the menu
* unblocks. The next press starts from the initial delay. */
holding = false;
navigation_initial = 0;
menu_st->scroll.acceleration = 0;
menu_st->scroll.steps = 0;
menu_st->action_press_time = 0;
return MENU_ACTION_NOOP;
}
/* Clear OK if dragged */
if (menu_input->pointer.flags & MENU_INP_PTR_FLG_DRAGGED)
ok_current = ok_trigger = ok_trigger_release = 0;
ok_old = ok_current;
/* Get pointer (mouse + touchscreen) input
* Note: Must be done regardless of menu screensaver
* state */
/* > If pointer input is disabled, do nothing */
if (!menu_mouse_enable && !menu_pointer_enable)
menu_input->pointer.type = MENU_POINTER_DISABLED;
#ifdef HAVE_COCOATOUCH
/* > Also disable when keyboard dialog is active to prevent touch events
* from iOS keyboard (e.g., Return button) from being interpreted as
* menu input that could reopen the keyboard */
else if (menu_st->flags & MENU_ST_FLAG_INP_DLG_KB_DISPLAY)
menu_input->pointer.type = MENU_POINTER_DISABLED;
#endif
else
{
static unsigned last_window = 0;
unsigned window =
video_driver_get_ui_layer_window(VIDEO_WINDOW_UI_MENU);
menu_input_pointer_hw_state_t mouse_hw_state = {0};
menu_input_pointer_hw_state_t touchscreen_hw_state = {0};
bool was_pressed =
(pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_SELECT) ? true : false;
#ifdef HAVE_OPENXR
menu_input_pointer_hw_state_t headset_hw_state = {0};
bool headset_on = false;
#endif
/* Read mouse */
if (menu_mouse_enable)
menu_input_get_mouse_hw_state(
p_disp,
menu,
menu_mouse_enable,
input_overlay_enable,
overlay_active,
&mouse_hw_state);
/* Read touchscreen
* Note: Could forgo this if mouse is currently active,
* but this is 'cleaner' code... (if performance is a
* concern - and it isn't - user can just disable touch
* screen support) */
if (menu_pointer_enable)
menu_input_get_touchscreen_hw_state(
p_disp,
menu,
overlay_active,
pointer_enabled,
input_touch_scale,
&touchscreen_hw_state);
#ifdef HAVE_OPENXR
headset_on = menu_input_get_headset_hw_state(p_disp, menu,
&headset_hw_state);
#endif
/* Mouse takes precedence */
if (mouse_hw_state.flags & MENU_INP_PTR_FLG_ACTIVE)
menu_input->pointer.type = MENU_POINTER_MOUSE;
else if (touchscreen_hw_state.flags & MENU_INP_PTR_FLG_ACTIVE)
menu_input->pointer.type = MENU_POINTER_TOUCHSCREEN;
/* Copy input from the current device */
#ifdef HAVE_OPENXR
/* The laser, unless the mouse or a touch moves this frame. Off
* the quad, the pointer stays where the laser left it until one
* does: the idle mouse's point would read as a move. */
if ( (mouse_hw_state.flags & MENU_INP_PTR_FLG_ACTIVE)
|| (touchscreen_hw_state.flags & MENU_INP_PTR_FLG_ACTIVE))
menu_input_headset_drives = false;
else if (headset_on)
menu_input_headset_drives = true;
if (menu_input_headset_drives)
{
menu_input->pointer.type = MENU_POINTER_MOUSE;
if (headset_on)
memcpy(pointer_hw_state, &headset_hw_state,
sizeof(menu_input_pointer_hw_state_t));
else
pointer_hw_state->flags = 0;
}
else
#endif
if (menu_input->pointer.type == MENU_POINTER_MOUSE)
memcpy(pointer_hw_state, &mouse_hw_state, sizeof(menu_input_pointer_hw_state_t));
else if (menu_input->pointer.type == MENU_POINTER_TOUCHSCREEN)
memcpy(pointer_hw_state, &touchscreen_hw_state, sizeof(menu_input_pointer_hw_state_t));
/* A press held as the menu changes window, down where it goes,
* down as it opens, or let go while it was closed, is no tap or
* cancel there: dropped until it lifts */
if (window != last_window || menu_event_opens)
{
if ( was_pressed
|| (pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_SELECT))
{
menu_input->select_inhibit = true;
menu_input->pointer.flags &= ~(MENU_INP_PTR_FLG_PRESSED
| MENU_INP_PTR_FLG_DRAGGED);
menu_input->pointer.press_duration = 0;
menu_input->pointer.press_direction = MENU_INPUT_PRESS_DIRECTION_NONE;
menu_input->pointer.delta = 0;
menu_input->pointer.y_accel = 0.0f;
menu_event_starts_afresh = true;
}
if (pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_CANCEL)
menu_input->cancel_inhibit = true;
last_window = window;
menu_event_opens = false;
}
if (pointer_hw_state->flags & MENU_INP_PTR_FLG_ACTIVE)
{
menu_st->input_last_time_us = menu_st->current_time_us;
/* Prevent double trigger when OK/Cancel has mouse binds: the
* pointer acts through the pointer path this frame, not as a
* button too, and a button it is bound to stays held back
* until let go */
#ifdef HAVE_OPENXR
/* This also keeps a press from firing OK here as well as at
* pointer-up. The laser skips it only while it merely points:
* a held hand is never still, and pad input would never
* pass. */
if ( !menu_input_headset_drives
|| was_pressed
|| (pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_SELECT))
#endif
{
pointer_active = true;
input_driver_hold_held_input();
}
}
}
/* Populate menu_input_state
* Note: dx, dy, ptr, y_accel, etc. entries are set elsewhere */
menu_input->pointer.pos = pointer_hw_state->pos;
if (menu_input->select_inhibit || menu_input->cancel_inhibit)
menu_input->pointer.flags &= ~(MENU_INP_PTR_FLG_ACTIVE
| MENU_INP_PTR_FLG_PRESS_SELECT);
else
{
if (pointer_hw_state->flags & MENU_INP_PTR_FLG_ACTIVE)
menu_input->pointer.flags |= (MENU_INP_PTR_FLG_ACTIVE);
else
menu_input->pointer.flags &= ~(MENU_INP_PTR_FLG_ACTIVE);
if (pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_SELECT)
menu_input->pointer.flags |= (MENU_INP_PTR_FLG_PRESSED);
else
menu_input->pointer.flags &= ~(MENU_INP_PTR_FLG_PRESSED);
}
/* If menu screensaver is active, any input
* is intercepted and used to switch it off */
if (menu_st->flags & MENU_ST_FLAG_SCREENSAVER_ACTIVE)
{
/* Check pointer input */
bool input_active = ( (menu_input->pointer.type != MENU_POINTER_DISABLED)
&& ((menu_input->pointer.flags & MENU_INP_PTR_FLG_ACTIVE) > 0));
/* Check regular input */
if (!input_active)
input_active = bits_any_set(p_input->data, ARRAY_SIZE(p_input->data));
if (!input_active)
input_active = bits_any_set(p_trigger_input->data, ARRAY_SIZE(p_trigger_input->data));
/* Disable screensaver if required */
if (input_active)
{
menu_st->flags &= ~MENU_ST_FLAG_SCREENSAVER_ACTIVE;
menu_st->input_last_time_us = menu_st->current_time_us;
if (menu_st->driver_ctx->environ_cb)
menu_st->driver_ctx->environ_cb(MENU_ENVIRON_DISABLE_SCREENSAVER,
NULL, menu_st->userdata);
}
/* Annul received input */
menu_input->pointer.flags &= ~(MENU_INP_PTR_FLG_ACTIVE
| MENU_INP_PTR_FLG_PRESSED);
pointer_hw_state->flags &= ~(MENU_INP_PTR_FLG_PRESS_UP
| MENU_INP_PTR_FLG_PRESS_DOWN
| MENU_INP_PTR_FLG_PRESS_LEFT
| MENU_INP_PTR_FLG_PRESS_RIGHT);
menu_input->select_inhibit = true;
menu_input->cancel_inhibit = true;
switch_old = BIT256_GET_PTR(p_input, RETRO_DEVICE_ID_JOYPAD_LEFT)
| BIT256_GET_PTR(p_input, RETRO_DEVICE_ID_JOYPAD_RIGHT);
/* End the hold, for the same reason as the BLOCK_ALL_INPUT
* path above */
holding = false;
navigation_initial = 0;
menu_st->scroll.acceleration = 0;
menu_st->scroll.steps = 0;
menu_st->action_press_time = 0;
return MENU_ACTION_NOOP;
}
/* Accelerate only navigation buttons */
for (i = 0; i < NAVIGATION_BUTTONS; i++)
{
if (BIT256_GET_PTR(p_input, navigation_buttons[i]))
navigation_current |= (1 << navigation_buttons[i]);
}
/* Ignore Start when both Start and OK are pressed
* by non-unified menu input (Enter) */
if ( BIT256_GET_PTR(p_input, RETRO_DEVICE_ID_JOYPAD_START)
&& BIT256_GET_PTR(p_input, menu_ok_btn))
navigation_current &= ~(1 << RETRO_DEVICE_ID_JOYPAD_START);
/* 0: this frame's Up or Down moves as the acceleration says (a
* press); otherwise it moves this many entries (a repeat) */
menu_st->scroll.steps = 0;
if (navigation_current)
{
retro_time_t now = menu_st->input_time_us;
retro_time_t repeat_from;
navigation_reset_delay = true;
/* Store first direction in order to block "diagonals" */
if (!navigation_initial)
navigation_initial = navigation_current;
if (!holding)
{
/* The press itself acts through the edge; what follows is the
* repeat, from Menu Scroll Delay on */
holding = true;
hold_start_us = now;
list_started = false;
list_carry = 0;
next_repeat_us = now + (retro_time_t)menu_scroll_delay * 1000;
}
repeat_from = hold_start_us
+ (retro_time_t)menu_scroll_delay * 1000;
/* Acceleration and the number speed-up go by how long the
* direction has been repeating, not by frames: one step of
* acceleration per MENU_REPEAT_INTERVAL_US */
if (now >= repeat_from)
{
retro_time_t repeating = now - repeat_from;
unsigned accel = (unsigned)MIN(1 + repeating
/ MENU_REPEAT_INTERVAL_US, 1000);
menu_st->scroll.acceleration = MIN(accel, menu_scroll_fast ? 25U : 5U);
menu_st->action_press_time = repeating;
}
else
{
menu_st->scroll.acceleration = 0;
menu_st->action_press_time = 0;
}
if (now > hold_start_us)
{
uint32_t input_repeat = 0;
/* Left, Right and the other buttons: a repeat each
* MENU_REPEAT_INTERVAL_US, on a schedule that carries over
* what a frame overshoots */
if (now >= next_repeat_us)
{
unsigned due = (unsigned)(1
+ (now - next_repeat_us) / MENU_REPEAT_INTERVAL_US);
next_repeat_us += (retro_time_t)due * MENU_REPEAT_INTERVAL_US;
for (i = 0; i < NAVIGATION_BUTTONS; i++)
BIT32_SET(input_repeat, navigation_buttons[i]);
}
/* Up and Down: the list moves at a speed in entries a second,
* which the acceleration raises, and as often as frames come:
* each frame moves the entries due since the last one, and
* carries the fraction. A faster display moves the same
* distance in more, smaller steps. */
if (now >= repeat_from)
{
retro_time_t speed = MENU_REPEAT_PER_SECOND
* (retro_time_t)((MAX(menu_st->scroll.acceleration, 2) - 2) / 4 + 1);
unsigned moves;
if (!list_started)
{
/* the first repeat is due at repeat_from itself */
list_started = true;
list_carry = 1000000 + (now - repeat_from) * speed;
}
else
list_carry += (now - list_last_us) * speed;
moves = (unsigned)(list_carry / 1000000);
list_carry -= (retro_time_t)moves * 1000000;
BIT32_CLEAR(input_repeat, RETRO_DEVICE_ID_JOYPAD_UP);
BIT32_CLEAR(input_repeat, RETRO_DEVICE_ID_JOYPAD_DOWN);
if (moves)
{
BIT32_SET(input_repeat, RETRO_DEVICE_ID_JOYPAD_UP);
BIT32_SET(input_repeat, RETRO_DEVICE_ID_JOYPAD_DOWN);
menu_st->scroll.steps = moves;
}
}
else
{
BIT32_CLEAR(input_repeat, RETRO_DEVICE_ID_JOYPAD_UP);
BIT32_CLEAR(input_repeat, RETRO_DEVICE_ID_JOYPAD_DOWN);
}
p_trigger_input->data[0] |= p_input->data[0] & input_repeat;
}
list_last_us = now;
}
else
{
holding = false;
menu_st->scroll.acceleration = 0;
menu_st->action_press_time = 0;
/* Buffer for keyboard combo jitter */
if (navigation_reset_delay)
navigation_reset_delay = false;
else
navigation_initial = 0;
}
/* Left/Right edge detection
* > Must be maintained regardless of the on-screen keyboard
* state, otherwise 'switch_old' freezes for the duration of
* the OSK session and a stale edge is emitted on the first
* frame after it closes, bypassing the ST_BOOL debounce */
switch_current = BIT256_GET_PTR(p_input, RETRO_DEVICE_ID_JOYPAD_LEFT)
| BIT256_GET_PTR(p_input, RETRO_DEVICE_ID_JOYPAD_RIGHT);
switch_trigger = switch_current & ~switch_old;
switch_old = switch_current;
if (display_kb)
{
/* Menu navigation stays suppressed for the whole OSK session
* (the trigger clear below), but the built-in keyboard only
* consumes input when no native panel owns the line. */
bool native_kb = input_osk_native_active();
if (!native_kb)
{
bool show_osk_symbols = input_event_osk_show_symbol_pages(menu_st->driver_data);
input_event_osk_iterate(menu_st->osk_grid, menu_st->osk_idx);
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_DOWN))
{
menu_st->input_last_time_us = menu_st->current_time_us;
if (menu_st->osk_ptr < 33)
menu_st->osk_ptr += OSK_CHARS_PER_LINE;
}
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_UP))
{
menu_st->input_last_time_us = menu_st->current_time_us;
if (menu_st->osk_ptr >= OSK_CHARS_PER_LINE)
menu_st->osk_ptr -= OSK_CHARS_PER_LINE;
}
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_RIGHT))
{
menu_st->input_last_time_us = menu_st->current_time_us;
if (menu_st->osk_ptr < 43)
menu_st->osk_ptr += 1;
}
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_LEFT))
{
menu_st->input_last_time_us = menu_st->current_time_us;
if (menu_st->osk_ptr >= 1)
menu_st->osk_ptr -= 1;
}
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_L))
{
menu_st->input_last_time_us = menu_st->current_time_us;
menu_st->osk_idx = input_osk_step(menu_st->osk_idx, -1,
show_osk_symbols);
}
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_R))
{
menu_st->input_last_time_us = menu_st->current_time_us;
menu_st->osk_idx = input_osk_step(menu_st->osk_idx, 1,
show_osk_symbols);
}
if (BIT256_GET_PTR(p_trigger_input, menu_ok_btn))
{
if (menu_st->osk_ptr >= 0)
input_driver_osk_press(
&menu_st->osk_idx,
menu_st->osk_ptr,
show_osk_symbols,
menu_st->osk_grid[menu_st->osk_ptr],
strlen(menu_st->osk_grid[menu_st->osk_ptr]));
}
/* Cancel: Send backspace if buffer is not empty, otherwise close window */
if (BIT256_GET_PTR(p_trigger_input, menu_cancel_btn))
{
if (input_driver_keyboard_line_length())
input_keyboard_event(true, '\x7f', '\x7f', 0, RETRO_DEVICE_KEYBOARD);
else
input_keyboard_event(true, '\n', '\n', 0, RETRO_DEVICE_KEYBOARD);
}
/* Scan: Clear the keyboard input window */
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_Y))
input_driver_keyboard_line_clear();
}
/* Cancel closes outright under a native panel: the panel owns
* the text, so feeding it a backspace here would only desync
* the two buffers. */
else if (BIT256_GET_PTR(p_trigger_input, menu_cancel_btn))
input_keyboard_event(true, '\n', '\n', 0, RETRO_DEVICE_KEYBOARD);
/* Closing the dialog stays available whichever keyboard is up.
* These two end the line through input_keyboard_event() and
* never reach input_event_osk_append(), so the realloc hazard
* the guard above exists for does not apply to them - and
* without them a native panel that emits no Return (webOS) or
* that the user has dismissed leaves the dialog with no way out
* from a pad at all. */
/* Select: Clear and close the keyboard input window */
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_SELECT))
{
input_driver_keyboard_line_clear();
input_keyboard_event(true, '\n', '\n', 0, RETRO_DEVICE_KEYBOARD);
}
/* Start + Search: Send return key to close keyboard input window */
if ( BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_START)
|| BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_X))
input_keyboard_event(true, '\n', '\n', 0, RETRO_DEVICE_KEYBOARD);
BIT256_CLEAR_ALL_PTR(p_trigger_input);
}
else
{
unsigned onkeyup =
input_combo_type_onkeyup_lut[settings->uints.input_menu_toggle_gamepad_combo];
/* Always process Select and Start on release */
onkeyup |= (1 << RETRO_DEVICE_ID_JOYPAD_SELECT)
| (1 << RETRO_DEVICE_ID_JOYPAD_START);
/* Process scroll keys on release when conflicting with menu toggle */
if (runloop_state_get_ptr()->flags & RUNLOOP_FLAG_CORE_RUNNING)
{
int i;
const struct retro_keybind menu_toggle_bind = *input_config_bind(0, RARCH_MENU_TOGGLE);
for (i = RETRO_DEVICE_ID_JOYPAD_L2; i <= RETRO_DEVICE_ID_JOYPAD_R3; i++)
{
if ( (menu_toggle_bind.joykey != NO_BTN && menu_toggle_bind.joykey == input_config_bind(0, i)->joykey)
|| (RETRO_KEYBIND_KEY(&menu_toggle_bind) != RETROK_UNKNOWN && RETRO_KEYBIND_KEY(&menu_toggle_bind) == RETRO_KEYBIND_KEY(input_config_bind(0, i))))
onkeyup |= (1 << i);
}
}
/* Handle OK on release with specific items */
if (ok_current || ok_trigger_release)
{
menu_entry_t entry;
MENU_ENTRY_INITIALIZE(entry);
menu_entry_get(&entry, 0, menu_st->selection_ptr, NULL, true);
/* Due to navigation animations changing current entry between
* keypress, require OK trigger enum match for release action */
if (ok_trigger)
ok_enum_idx = entry.enum_idx;
/* Trigger on release during dialog boxes */
if (*MENU_STATE_TEXT(menu))
{
if (ok_trigger)
ok_enum_idx = MENU_ENUM_LABEL_MESSAGE;
if (ok_enum_idx != MENU_ENUM_LABEL_MESSAGE)
ok_trigger_release = 0;
ok_trigger = ok_trigger_release;
}
/* Single-click playlist entries */
if ( settings->bools.input_menu_singleclick_playlists
&& ( entry.enum_idx == MENU_ENUM_LABEL_RUN
|| entry.enum_idx == MENU_ENUM_LABEL_RESUME_CONTENT)
&& ( ok_enum_idx == MENU_ENUM_LABEL_PLAYLIST_ENTRY
|| ok_enum_idx == MENU_ENUM_LABEL_EXPLORE_ITEM))
ok_trigger = ok_trigger_release;
/* Resume */
if ( ok_enum_idx == MENU_ENUM_LABEL_RESUME_CONTENT
&& ok_enum_idx == entry.enum_idx)
ok_trigger = ok_trigger_release;
/* Save state resume */
if ( settings->bools.menu_savestate_resume
&& ( ok_enum_idx == MENU_ENUM_LABEL_LOAD_STATE
|| ok_enum_idx == MENU_ENUM_LABEL_SAVE_STATE
|| ok_enum_idx == MENU_ENUM_LABEL_UNDO_LOAD_STATE
|| ok_enum_idx == MENU_ENUM_LABEL_UNDO_SAVE_STATE)
&& ok_enum_idx == entry.enum_idx)
ok_trigger = ok_trigger_release;
/* Disc insert resume */
if ( settings->bools.menu_insert_disk_resume
&& ( ok_enum_idx == MENU_SETTING_DROPDOWN_ITEM_DISK_INDEX
|| ok_enum_idx == MENU_ENUM_LABEL_DISK_TRAY_INSERT
|| ok_enum_idx == MENU_ENUM_LABEL_DISK_TRAY_EJECT)
&& ok_enum_idx == entry.enum_idx)
ok_trigger = ok_trigger_release;
}
/* Prevent holding down left/right with boolean settings */
if (switch_current)
{
uint8_t setting_type = 0;
menu_entry_t entry;
MENU_ENTRY_INITIALIZE(entry);
entry.flags = MENU_ENTRY_FLAG_VALUE_ENABLED;
menu_entry_get(&entry, 0, menu_st->selection_ptr, NULL, true);
setting_type = entry.setting_type;
#ifdef HAVE_CHEATS
/* Treat cheat left/right toggle as bools */
if ( !setting_type
&& entry.type >= MENU_SETTINGS_CHEAT_BEGIN
&& entry.type <= MENU_SETTINGS_CHEAT_END)
setting_type = ST_BOOL;
#endif
if (setting_type == ST_BOOL)
{
char value[8];
if (entry.enum_idx == MENU_ENUM_LABEL_CHEEVOS_PASSWORD)
strlcpy(value, entry.password_value, sizeof(value));
else
strlcpy(value, entry.value, sizeof(value));
/* Ignore direction if switch is already in that position */
if ( ( string_is_equal(value, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_ON))
&& BIT256_GET_PTR(p_input, RETRO_DEVICE_ID_JOYPAD_RIGHT))
|| ( string_is_equal(value, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_OFF))
&& BIT256_GET_PTR(p_input, RETRO_DEVICE_ID_JOYPAD_LEFT))
)
switch_trigger = 0;
}
else
/* Always allow repeat direction */
switch_trigger = 1;
}
if ( BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_UP)
&& navigation_initial == (1 << RETRO_DEVICE_ID_JOYPAD_UP))
{
ret = MENU_ACTION_UP;
keydown[RETRO_DEVICE_ID_JOYPAD_UP] = true;
}
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_DOWN)
&& navigation_initial == (1 << RETRO_DEVICE_ID_JOYPAD_DOWN))
{
ret = MENU_ACTION_DOWN;
keydown[RETRO_DEVICE_ID_JOYPAD_DOWN] = true;
}
if ( BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_LEFT)
&& switch_trigger
&& navigation_initial == (1 << RETRO_DEVICE_ID_JOYPAD_LEFT))
{
ret = MENU_ACTION_LEFT;
keydown[RETRO_DEVICE_ID_JOYPAD_LEFT] = true;
}
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_RIGHT)
&& switch_trigger
&& navigation_initial == (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT))
{
ret = MENU_ACTION_RIGHT;
keydown[RETRO_DEVICE_ID_JOYPAD_RIGHT] = true;
}
if (BIT256_GET_PTR(p_trigger_input, RARCH_ANALOG_RIGHT_Y_MINUS))
ret = MENU_ACTION_CYCLE_THUMBNAIL_PRIMARY;
else if (BIT256_GET_PTR(p_trigger_input, RARCH_ANALOG_RIGHT_Y_PLUS))
ret = MENU_ACTION_CYCLE_THUMBNAIL_SECONDARY;
else if (BIT256_GET_PTR(p_trigger_input, RARCH_ANALOG_RIGHT_X_MINUS))
ret = MENU_ACTION_CYCLE_THUMBNAIL_PRIMARY;
else if (BIT256_GET_PTR(p_trigger_input, RARCH_ANALOG_RIGHT_X_PLUS))
ret = MENU_ACTION_CYCLE_THUMBNAIL_SECONDARY;
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_L))
menu_st->scroll.mode = (swap_scroll_btns) ? MENU_SCROLL_START_LETTER : MENU_SCROLL_PAGE;
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_R))
menu_st->scroll.mode = (swap_scroll_btns) ? MENU_SCROLL_START_LETTER : MENU_SCROLL_PAGE;
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_L2))
menu_st->scroll.mode = (swap_scroll_btns) ? MENU_SCROLL_PAGE : MENU_SCROLL_START_LETTER;
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_R2))
menu_st->scroll.mode = (swap_scroll_btns) ? MENU_SCROLL_PAGE : MENU_SCROLL_START_LETTER;
MENU_ACTION_RET(RETRO_DEVICE_ID_JOYPAD_L, MENU_ACTION_SCROLL_UP);
MENU_ACTION_RET(RETRO_DEVICE_ID_JOYPAD_R, MENU_ACTION_SCROLL_DOWN);
MENU_ACTION_RET(RETRO_DEVICE_ID_JOYPAD_L2, MENU_ACTION_SCROLL_UP);
MENU_ACTION_RET(RETRO_DEVICE_ID_JOYPAD_R2, MENU_ACTION_SCROLL_DOWN);
MENU_ACTION_RET(RETRO_DEVICE_ID_JOYPAD_L3, MENU_ACTION_SCROLL_HOME);
MENU_ACTION_RET(RETRO_DEVICE_ID_JOYPAD_R3, MENU_ACTION_SCROLL_END);
if (ret == MENU_ACTION_NOOP && !ok_trigger && !ok_trigger_release)
{
MENU_ACTION_RET(RETRO_DEVICE_ID_JOYPAD_START, MENU_ACTION_START);
MENU_ACTION_RET(RETRO_DEVICE_ID_JOYPAD_SELECT, MENU_ACTION_INFO);
MENU_ACTION_RET(RETRO_DEVICE_ID_JOYPAD_X, MENU_ACTION_SEARCH);
MENU_ACTION_RET(RETRO_DEVICE_ID_JOYPAD_Y, MENU_ACTION_SCAN);
}
else
{
/* Ignore OK release if navigation happened while down */
if (ok_trigger_release)
{
if ( keydown[RETRO_DEVICE_ID_JOYPAD_UP]
|| keydown[RETRO_DEVICE_ID_JOYPAD_DOWN]
|| keydown[RETRO_DEVICE_ID_JOYPAD_LEFT]
|| keydown[RETRO_DEVICE_ID_JOYPAD_RIGHT]
|| keydown[menu_cancel_btn]
)
ok_trigger = 0;
memset(keydown, 0, sizeof(keydown));
}
}
if (ok_trigger)
ret = MENU_ACTION_OK;
else if (BIT256_GET_PTR(p_trigger_input, menu_cancel_btn))
{
ret = MENU_ACTION_CANCEL;
keydown[menu_cancel_btn] = true;
}
if (BIT256_GET_PTR(p_trigger_input, RARCH_MENU_TOGGLE))
ret = MENU_ACTION_TOGGLE;
if (ret != MENU_ACTION_NOOP && !ok_current)
memset(keydown, 0, sizeof(keydown));
/* Prevent simultaneous hotkey actions according to hotkey block delay */
if (input_config_bind(0, RARCH_ENABLE_HOTKEY)->joykey != NO_BTN)
{
if ( input_driver_libretro_input_blocked()
|| !input_driver_hotkey_blocked())
ret = MENU_ACTION_NOOP;
}
if (ret != MENU_ACTION_NOOP)
menu_st->input_last_time_us = menu_st->current_time_us;
}
/* Menu must be alive. What was held as it opened is held back by the
* run loop, button by button, and does not reach here. While the
* pointer is in use it is the pointer that acts, not a button: the
* flag that held input back used to make this frame's action
* nothing, and the mouse's press, wheel and long press are built on
* that. */
if ( !(menu_st->flags & MENU_ST_FLAG_ALIVE)
|| pointer_active)
ret = MENU_ACTION_NOOP;
return ret;
}
/**
* menu_input_wheel_scroll:
*
* Hands a mouse wheel notch to the menu driver as list movement.
* A wheel is not a d-pad, and a driver that can move its list
* under a resting pointer reads far better doing that than
* stepping the selection one entry per notch.
*
* Returns: true when the driver took it, false to fall back to
* MENU_ACTION_UP/MENU_ACTION_DOWN.
**/
static bool menu_input_wheel_scroll(struct menu_state *menu_st,
int notches)
{
if ( menu_st->driver_ctx
&& menu_st->driver_ctx->wheel_scroll)
return menu_st->driver_ctx->wheel_scroll(
menu_st->userdata, notches);
return false;
}
MENU_NOINLINE static int menu_input_post_iterate(
gfx_display_t *p_disp,
struct menu_state *menu_st,
unsigned action,
retro_time_t current_time)
{
menu_entry_t entry;
static retro_time_t start_time = 0;
static uint32_t start_pos = 0; /* VIDEO_POS_PACK */
static uint32_t last_pos = 0;
static uint16_t dx_start_right_max = 0;
static uint16_t dx_start_left_max = 0;
static uint16_t dy_start_up_max = 0;
static uint16_t dy_start_down_max = 0;
static bool last_select_pressed = false;
static uint8_t press_epoch = 0;
static bool last_cancel_pressed = false;
static bool last_left_pressed = false;
static bool last_right_pressed = false;
static retro_time_t last_left_action_time = 0;
static retro_time_t last_right_action_time = 0;
static retro_time_t last_press_direction_time = 0;
static retro_time_t y_accel_decay_time = 0;
static retro_time_t y_accel_decay_carry = 0;
bool attenuate_y_accel = true;
bool osk_active = menu_input_dialog_get_display_kb();
bool messagebox_active = false;
int ret = 0;
menu_input_pointer_hw_state_t *pointer_hw_state = &menu_st->input_pointer_hw_state;
menu_input_t *menu_input = &menu_st->input_state;
menu_handle_t *menu = menu_st->driver_data;
menu_list_t *menu_list = menu_st->entries.list;
file_list_t *selection_buf = menu_list ? MENU_LIST_GET_SELECTION(menu_list, (unsigned)0) : NULL;
size_t selection = menu_st->selection_ptr;
menu_file_list_cbs_t *cbs = selection_buf && selection_buf->size
? (menu_file_list_cbs_t*)selection_buf->list[selection].actiondata
: NULL;
unsigned output_size =
video_driver_get_ui_layer_dims(VIDEO_WINDOW_UI_MENU);
MENU_ENTRY_INITIALIZE(entry);
entry.flags |= MENU_ENTRY_FLAG_PATH_ENABLED
| MENU_ENTRY_FLAG_LABEL_ENABLED;
menu_entry_get(&entry, 0, selection, NULL, false);
/* Check whether a message box is currently
* being shown
* > Note: This ignores input bind dialogs,
* since input binding overrides normal input
* and must be handled separately... */
if (menu)
messagebox_active = BIT64_GET(
menu->state, MENU_STATE_RENDER_MESSAGEBOX)
&& *MENU_STATE_TEXT(menu);
/* If onscreen keyboard is shown and we currently have
* active mouse input, highlight key under mouse cursor */
if ( osk_active
&& (menu_input->pointer.type == MENU_POINTER_MOUSE)
&& ((pointer_hw_state->flags & MENU_INP_PTR_FLG_ACTIVE) > 0))
{
menu_ctx_pointer_t point;
point.x = VIDEO_POS_X(pointer_hw_state->pos);
point.y = VIDEO_POS_Y(pointer_hw_state->pos);
point.ptr = 0;
point.cbs = NULL;
point.entry = NULL;
point.action = 0;
point.gesture = MENU_INPUT_GESTURE_NONE;
point.retcode = 0;
menu_driver_ctl(RARCH_MENU_CTL_OSK_PTR_AT_POS, &point);
if (point.retcode > -1)
menu_st->osk_ptr = point.retcode;
}
/* Select + X/Y position */
if (!menu_input->select_inhibit)
{
if (pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_SELECT)
{
int16_t x = VIDEO_POS_X(pointer_hw_state->pos);
int16_t y = VIDEO_POS_Y(pointer_hw_state->pos);
static float accel0 = 0.0f;
static float accel1 = 0.0f;
/* Transition from select unpressed to select pressed */
if (!last_select_pressed)
{
menu_ctx_pointer_t point;
/* Initialise variables */
start_time = current_time;
start_pos = VIDEO_POS_PACK(x, y);
last_pos = start_pos;
dx_start_right_max = 0;
dx_start_left_max = 0;
dy_start_up_max = 0;
dy_start_down_max = 0;
accel0 = 0.0f;
accel1 = 0.0f;
last_press_direction_time = 0;
press_epoch = menu_st->dialog_st.button_epoch;
/* If we are not currently showing the onscreen
* keyboard or a message box, trigger a 'pointer
* down' event */
if (!osk_active && !messagebox_active)
{
point.x = x;
point.y = y;
/* Note: menu_input->ptr is meaningless here when
* using a touchscreen... */
point.ptr = menu_input->ptr;
point.cbs = cbs;
point.entry = &entry;
point.action = action;
point.gesture = MENU_INPUT_GESTURE_NONE;
menu_driver_ctl(RARCH_MENU_CTL_POINTER_DOWN, &point);
ret = point.retcode;
}
}
else
{
/* Pointer is being held down
* (i.e. for more than one frame) */
float dpi = menu ? menu_input_get_dpi(menu, p_disp,
output_size) : 0.0f;
/* > Update deltas + acceleration & detect press direction
* Note: We only do this if the pointer has moved above
* a certain threshold - this requires dpi info */
if (dpi > 0.0f)
{
uint16_t dpi_threshold_drag =
(uint16_t)((dpi * MENU_INPUT_DPI_THRESHOLD_DRAG) + 0.5f);
int16_t dx_start = x - VIDEO_POS_X(start_pos);
int16_t dy_start = y - VIDEO_POS_Y(start_pos);
uint16_t dx_start_abs = dx_start < 0 ? dx_start * -1 : dx_start;
uint16_t dy_start_abs = dy_start < 0 ? dy_start * -1 : dy_start;
if ( (dx_start_abs > dpi_threshold_drag)
|| (dy_start_abs > dpi_threshold_drag))
{
uint16_t dpi_threshold_press_direction_min =
(uint16_t)((dpi * MENU_INPUT_DPI_THRESHOLD_PRESS_DIRECTION_MIN) + 0.5f);
uint16_t dpi_threshold_press_direction_max =
(uint16_t)((dpi * MENU_INPUT_DPI_THRESHOLD_PRESS_DIRECTION_MAX) + 0.5f);
uint16_t dpi_threshold_press_direction_tangent =
(uint16_t)((dpi * MENU_INPUT_DPI_THRESHOLD_PRESS_DIRECTION_TANGENT) + 0.5f);
enum menu_input_pointer_press_direction
press_direction = MENU_INPUT_PRESS_DIRECTION_NONE;
float press_direction_amplitude = 0.0f;
retro_time_t press_direction_delay = MENU_INPUT_PRESS_DIRECTION_DELAY_MAX;
/* Pointer has moved a sufficient distance to
* trigger a 'dragged' state */
menu_input->pointer.flags |= MENU_INP_PTR_FLG_DRAGGED;
/* Here we diverge:
* > If onscreen keyboard or a message box is
* active, pointer deltas, acceleration and
* press direction must be inhibited
* > If not, input is processed normally */
if (osk_active || messagebox_active)
{
/* Inhibit normal pointer input */
menu_input->pointer.delta = 0;
menu_input->pointer.y_accel = 0.0f;
menu_input->pointer.press_direction = MENU_INPUT_PRESS_DIRECTION_NONE;
accel0 = 0.0f;
accel1 = 0.0f;
attenuate_y_accel = false;
}
else
{
/* Assign current deltas */
menu_input->pointer.delta = VIDEO_POS_PACK(
x - VIDEO_POS_X(last_pos), y - VIDEO_POS_Y(last_pos));
/* Update maximum start->current deltas */
if (dx_start > 0)
dx_start_right_max = (dx_start_abs > dx_start_right_max)
? dx_start_abs : dx_start_right_max;
else
dx_start_left_max = (dx_start_abs > dx_start_left_max)
? dx_start_abs : dx_start_left_max;
if (dy_start > 0)
dy_start_down_max = (dy_start_abs > dy_start_down_max)
? dy_start_abs : dy_start_down_max;
else
dy_start_up_max = (dy_start_abs > dy_start_up_max)
? dy_start_abs : dy_start_up_max;
/* Magic numbers... */
menu_input->pointer.y_accel = (accel0 + accel1 + (float)VIDEO_POS_Y(menu_input->pointer.delta)) / 3.0f;
accel0 = accel1;
accel1 = menu_input->pointer.y_accel;
/* Acceleration decays over time - but if the value
* has been set on this frame, attenuation should
* be skipped */
attenuate_y_accel = false;
/* Check if pointer is being held in a particular
* direction */
menu_input->pointer.press_direction = MENU_INPUT_PRESS_DIRECTION_NONE;
/* > Press directions are actually triggered as a pulse train,
* since a continuous direction prevents fine control in the
* context of menu actions (i.e. it would be the same
* as always holding down a cursor key all the time - too fast
* to control). We therefore apply a low pass filter, with
* a variable frequency based upon the distance the user has
* dragged the pointer */
/* > Horizontal */
if ((dx_start_abs >= dpi_threshold_press_direction_min) &&
(dy_start_abs < dpi_threshold_press_direction_tangent))
{
press_direction = (dx_start > 0)
? MENU_INPUT_PRESS_DIRECTION_RIGHT
: MENU_INPUT_PRESS_DIRECTION_LEFT;
/* Get effective amplitude of press direction offset */
press_direction_amplitude =
(float)(dx_start_abs - dpi_threshold_press_direction_min) /
(float)(dpi_threshold_press_direction_max - dpi_threshold_press_direction_min);
}
/* > Vertical */
else if ( (dy_start_abs >= dpi_threshold_press_direction_min)
&& (dx_start_abs < dpi_threshold_press_direction_tangent))
{
press_direction = (dy_start > 0)
? MENU_INPUT_PRESS_DIRECTION_DOWN
: MENU_INPUT_PRESS_DIRECTION_UP;
/* Get effective amplitude of press direction offset */
press_direction_amplitude =
(float)(dy_start_abs - dpi_threshold_press_direction_min) /
(float)(dpi_threshold_press_direction_max - dpi_threshold_press_direction_min);
}
if (press_direction != MENU_INPUT_PRESS_DIRECTION_NONE)
{
/* > Update low pass filter frequency */
if (press_direction_amplitude > 1.0f)
press_direction_delay = MENU_INPUT_PRESS_DIRECTION_DELAY_MIN;
else
press_direction_delay = MENU_INPUT_PRESS_DIRECTION_DELAY_MIN +
((MENU_INPUT_PRESS_DIRECTION_DELAY_MAX - MENU_INPUT_PRESS_DIRECTION_DELAY_MIN)*
(1.0f - press_direction_amplitude));
/* > Apply low pass filter */
if (current_time - last_press_direction_time > press_direction_delay)
{
menu_input->pointer.press_direction = press_direction;
last_press_direction_time = current_time;
}
}
}
}
else
{
/* Pointer is stationary */
menu_input->pointer.delta = 0;
menu_input->pointer.press_direction = MENU_INPUT_PRESS_DIRECTION_NONE;
/* Standard behaviour (on Android, at least) is to stop
* scrolling when the user touches the screen. We should
* therefore 'reset' y acceleration whenever the pointer
* is stationary - with two caveats:
* - We only disable scrolling if the pointer *remains*
* stationary. If the pointer is held down then
* subsequently moves, normal scrolling should resume
* - Halting the scroll immediately produces a very
* unpleasant 'jerky' user experience. To avoid this,
* we add a small delay between detecting a pointer
* down event and forcing y acceleration to zero
* NOTE: Of course, we must also 'reset' y acceleration
* whenever the onscreen keyboard or a message box is
* shown */
if ( (!(menu_input->pointer.flags & MENU_INP_PTR_FLG_DRAGGED)
&& (menu_input->pointer.press_duration > MENU_INPUT_Y_ACCEL_RESET_DELAY))
|| (osk_active || messagebox_active))
{
menu_input->pointer.y_accel = 0.0f;
accel0 = 0.0f;
accel1 = 0.0f;
attenuate_y_accel = false;
}
}
}
else
{
/* No dpi info - just fallback to zero... */
menu_input->pointer.delta = 0;
menu_input->pointer.y_accel = 0.0f;
menu_input->pointer.press_direction = MENU_INPUT_PRESS_DIRECTION_NONE;
accel0 = 0.0f;
accel1 = 0.0f;
attenuate_y_accel = false;
}
/* > Update remaining variables */
menu_input->pointer.press_duration = current_time - start_time;
last_pos = VIDEO_POS_PACK(x, y);
}
}
else if (last_select_pressed)
{
/* Transition from select pressed to select unpressed */
int16_t x;
int16_t y;
menu_ctx_pointer_t point;
if (menu_input->pointer.flags & MENU_INP_PTR_FLG_DRAGGED)
{
/* Pointer has moved.
* When using a touchscreen, releasing a press
* resets the x/y position - so cannot use
* current hardware x/y values. Instead, use
* previous position from last time that a
* press was active */
x = VIDEO_POS_X(last_pos);
y = VIDEO_POS_Y(last_pos);
}
else
{
/* Pointer is considered stationary,
* so use start position */
x = VIDEO_POS_X(start_pos);
y = VIDEO_POS_Y(start_pos);
}
point.x = x;
point.y = y;
point.ptr = menu_input->ptr;
point.cbs = cbs;
point.entry = &entry;
point.action = action;
point.gesture = MENU_INPUT_GESTURE_NONE;
/* On screen keyboard overrides normal menu input... */
if (osk_active)
{
/* If pointer has been 'dragged', then it counts as
* a miss. Only register 'release' event if pointer
* has remained stationary. A native panel owning the
* line swallows the gesture outright - the enclosing
* branch still runs so it does not fall through to
* normal menu input. */
if ( !input_osk_native_active()
&& !(menu_input->pointer.flags & MENU_INP_PTR_FLG_DRAGGED))
{
if ( menu_st->driver_ctx
&& menu_st->driver_ctx->osk_pointer_over_textbox
&& menu_st->driver_ctx->osk_pointer_over_textbox(
menu_st->userdata, x, y, output_size))
input_driver_set_keyboard_textbox_focus(true);
else
{
menu_driver_ctl(RARCH_MENU_CTL_OSK_PTR_AT_POS, &point);
if (point.retcode > -1)
{
bool textbox_focus = input_driver_keyboard_textbox_focus();
menu_st->osk_ptr = point.retcode;
input_driver_set_keyboard_textbox_focus(false);
if (!textbox_focus)
{
bool show_osk_symbols = input_event_osk_show_symbol_pages(menu_st->driver_data);
input_driver_osk_press(
&menu_st->osk_idx,
point.retcode,
show_osk_symbols,
menu_st->osk_grid[menu_st->osk_ptr],
strlen(menu_st->osk_grid[menu_st->osk_ptr]));
}
}
}
}
}
/* Message boxes override normal menu input...
* > If a message box is shown, any kind of pointer
* gesture should close it
* > If a confirmation box is show, ignore gestures
* outside of Back/OK buttons */
else if (messagebox_active)
{
/* A confirm dialog is answered by any tap: OK confirms,
* anywhere else cancels, so it cannot be left half-closed */
unsigned confirm_cmd = menu_st->dialog_st.confirm_cmd;
bool hover_ok = menu_st->dialog_st.confirm_hover_ok;
int hover = menu_st->dialog_st.button_hover;
/* Named buttons: the one tapped, anywhere else Cancel; a
* press that began on a box since replaced chooses nothing */
if (menu_st->dialog_st.button_count)
{
if (press_epoch == menu_st->dialog_st.button_epoch)
menu_dialog_press(menu_st, (hover >= 0) ? hover : 0);
}
else
{
menu_input_pointer_close_messagebox(menu_st);
if (confirm_cmd)
{
if (hover_ok)
menu_dialog_confirm(menu_st);
else
menu_dialog_confirm_clear(menu_st);
}
}
}
/* Normal menu input */
else
{
/* Detect gesture type. A mouse goes through here as well:
* its left click is the tap. */
if (!(menu_input->pointer.flags & MENU_INP_PTR_FLG_DRAGGED))
{
/* Pointer hasn't moved - check press duration */
if (menu_input->pointer.press_duration
< MENU_INPUT_PRESS_TIME_SHORT)
point.gesture = MENU_INPUT_GESTURE_TAP;
else if (menu_input->pointer.press_duration
< MENU_INPUT_PRESS_TIME_LONG)
point.gesture = MENU_INPUT_GESTURE_SHORT_PRESS;
else
point.gesture = MENU_INPUT_GESTURE_LONG_PRESS;
}
else
{
/* Pointer has moved - check if this is a swipe */
float dpi = menu ? menu_input_get_dpi(menu, p_disp,
output_size) : 0.0f;
if ( (dpi > 0.0f)
&& (menu_input->pointer.press_duration <
MENU_INPUT_SWIPE_TIMEOUT))
{
uint16_t dpi_threshold_swipe =
(uint16_t)((dpi * MENU_INPUT_DPI_THRESHOLD_SWIPE) + 0.5f);
uint16_t dpi_threshold_swipe_tangent =
(uint16_t)((dpi * MENU_INPUT_DPI_THRESHOLD_SWIPE_TANGENT) + 0.5f);
int16_t dx_start = x - VIDEO_POS_X(start_pos);
int16_t dy_start = y - VIDEO_POS_Y(start_pos);
uint16_t dx_start_right_final = 0;
uint16_t dx_start_left_final = 0;
uint16_t dy_start_up_final = 0;
uint16_t dy_start_down_final = 0;
/* Get final deltas */
if (dx_start > 0)
dx_start_right_final = (uint16_t)dx_start;
else
dx_start_left_final = (uint16_t)(dx_start * -1);
if (dy_start > 0)
dy_start_down_final = (uint16_t)dy_start;
else
dy_start_up_final = (uint16_t)(dy_start * -1);
/* Swipe right */
if ( (dx_start_right_final > dpi_threshold_swipe)
&& (dx_start_left_max < dpi_threshold_swipe_tangent)
&& (dy_start_up_max < dpi_threshold_swipe_tangent)
&& (dy_start_down_max < dpi_threshold_swipe_tangent)
)
point.gesture = MENU_INPUT_GESTURE_SWIPE_RIGHT;
/* Swipe left */
else if (
(dx_start_right_max < dpi_threshold_swipe_tangent)
&& (dx_start_left_final > dpi_threshold_swipe)
&& (dy_start_up_max < dpi_threshold_swipe_tangent)
&& (dy_start_down_max < dpi_threshold_swipe_tangent)
)
point.gesture = MENU_INPUT_GESTURE_SWIPE_LEFT;
/* Swipe up */
else if (
(dx_start_right_max < dpi_threshold_swipe_tangent)
&& (dx_start_left_max < dpi_threshold_swipe_tangent)
&& (dy_start_up_final > dpi_threshold_swipe)
&& (dy_start_down_max < dpi_threshold_swipe_tangent)
)
point.gesture = MENU_INPUT_GESTURE_SWIPE_UP;
/* Swipe down */
else if (
(dx_start_right_max < dpi_threshold_swipe_tangent)
&& (dx_start_left_max < dpi_threshold_swipe_tangent)
&& (dy_start_up_max < dpi_threshold_swipe_tangent)
&& (dy_start_down_final > dpi_threshold_swipe)
)
point.gesture = MENU_INPUT_GESTURE_SWIPE_DOWN;
}
}
/* Trigger a 'pointer up' event */
menu_driver_ctl(RARCH_MENU_CTL_POINTER_UP, &point);
ret = point.retcode;
}
/* Reset variables */
start_pos = 0;
last_pos = 0;
dx_start_right_max = 0;
dx_start_left_max = 0;
dy_start_up_max = 0;
dy_start_down_max = 0;
last_press_direction_time = 0;
menu_input->pointer.press_duration = 0;
menu_input->pointer.press_direction = MENU_INPUT_PRESS_DIRECTION_NONE;
menu_input->pointer.delta = 0;
menu_input->pointer.flags &= ~(MENU_INP_PTR_FLG_DRAGGED);
}
}
/* Adjust acceleration
* > If acceleration has not been set on this frame,
* apply normal attenuation
* > Decay steps run on elapsed time, one factor per
* MENU_INPUT_Y_ACCEL_DECAY_PERIOD us with the remainder
* carried, so a flick travels the same distance at
* every refresh rate */
if (attenuate_y_accel)
{
retro_time_t elapsed = (current_time - y_accel_decay_time)
+ y_accel_decay_carry;
/* Cap the step count so a long gap (menu closed,
* dropped frames) costs bounded work; the residual
* acceleration after 64 steps is already negligible */
if (elapsed > (retro_time_t)MENU_INPUT_Y_ACCEL_DECAY_PERIOD * 64)
elapsed = (retro_time_t)MENU_INPUT_Y_ACCEL_DECAY_PERIOD * 64;
while (elapsed >= MENU_INPUT_Y_ACCEL_DECAY_PERIOD)
{
menu_input->pointer.y_accel *= MENU_INPUT_Y_ACCEL_DECAY_FACTOR;
elapsed -= MENU_INPUT_Y_ACCEL_DECAY_PERIOD;
}
y_accel_decay_carry = elapsed;
}
else
y_accel_decay_carry = 0;
y_accel_decay_time = current_time;
/* If select has been released, disable any existing
* select inhibit */
if (!(pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_SELECT))
menu_input->select_inhibit = false;
/* Cancel */
if ( !menu_input->cancel_inhibit
&& (pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_CANCEL)
&& !last_cancel_pressed)
{
/* If currently showing a message box, close it */
if (messagebox_active)
{
if (menu_st->dialog_st.button_count)
menu_dialog_press(menu_st, 0);
else
{
menu_input_pointer_close_messagebox(menu_st);
menu_dialog_confirm_clear(menu_st);
}
}
/* If onscreen keyboard is shown, send a 'backspace' */
else if (osk_active)
input_keyboard_event(true, '\x7f', '\x7f',
0, RETRO_DEVICE_KEYBOARD);
/* ...otherwise, invoke standard MENU_ACTION_CANCEL
* action */
else
{
size_t selection = menu_st->selection_ptr;
ret = menu_entry_action(&entry, selection, MENU_ACTION_CANCEL);
}
}
/* If cancel has been released, disable any existing
* cancel inhibit */
if (!(pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_CANCEL))
menu_input->cancel_inhibit = false;
if (!messagebox_active)
{
/* Up/Down
* > Note 1: These always correspond to a mouse wheel, which
* handles differently from other inputs - i.e. we don't
* want a 'last pressed' check
* > Note 2: If a message box is currently shown, must
* inhibit input */
/* > Up */
if (pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_UP)
{
if (!menu_input_wheel_scroll(menu_st, -1))
{
size_t selection = menu_st->selection_ptr;
ret = menu_entry_action(
&entry, selection, MENU_ACTION_UP);
}
}
/* > Down */
if (pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_DOWN)
{
if (!menu_input_wheel_scroll(menu_st, 1))
{
size_t selection = menu_st->selection_ptr;
ret = menu_entry_action(
&entry, selection, MENU_ACTION_DOWN);
}
}
/* Left/Right
* > Note 1: These also always correspond to a mouse wheel...
* In this case, it's a mouse wheel *tilt* operation, which
* is incredibly annoying because holding a tilt direction
* rapidly toggles the input state. The repeat speed is so
* high that any sort of useable control is impossible - so
* we have to apply a 'low pass' filter by ignoring inputs
* that occur below a certain frequency...
* > Note 2: If a message box is currently shown, must
* inhibit input */
/* > Left */
if ( (pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_LEFT)
&& !last_left_pressed)
{
if (current_time - last_left_action_time
> MENU_INPUT_HORIZ_WHEEL_DELAY)
{
size_t selection = menu_st->selection_ptr;
last_left_action_time = current_time;
ret = menu_entry_action(
&entry, selection, MENU_ACTION_LEFT);
}
}
/* > Right */
if (
(pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_RIGHT)
&& !last_right_pressed)
{
if (current_time - last_right_action_time
> MENU_INPUT_HORIZ_WHEEL_DELAY)
{
size_t selection = menu_st->selection_ptr;
last_right_action_time = current_time;
ret = menu_entry_action(
&entry, selection, MENU_ACTION_RIGHT);
}
}
}
last_select_pressed = ((pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_SELECT) > 0);
last_cancel_pressed = ((pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_CANCEL) > 0);
last_left_pressed = ((pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_LEFT) > 0);
last_right_pressed = ((pointer_hw_state->flags & MENU_INP_PTR_FLG_PRESS_RIGHT) > 0);
return ret;
}
void menu_driver_toggle(
void *curr_video_data,
void *video_driver_data,
menu_handle_t *menu,
menu_input_t *menu_input,
settings_t *settings,
bool menu_driver_alive,
bool overlay_alive,
retro_keyboard_event_t *key_event,
retro_keyboard_event_t *frontend_key_event,
bool on)
{
/* TODO/FIXME - retroarch_main_quit calls menu_driver_toggle -
* we might have to redesign this to avoid EXXC_BAD_ACCESS errors
* on OSX - for now we work around this by checking if the settings
* struct is NULL
*/
video_driver_t *current_video = (video_driver_t*)curr_video_data;
#ifdef HAVE_MICROPHONE
bool menu_pause_libretro = false;
#endif
runloop_state_t *runloop_st = runloop_state_get_ptr();
struct menu_state *menu_st = &menu_driver_state;
bool runloop_shutdown_initiated = (runloop_st->flags &
RUNLOOP_FLAG_SHUTDOWN_INITIATED) ? true : false;
#ifdef HAVE_OVERLAY
bool input_overlay_hide_in_menu = false;
bool input_overlay_enable = false;
#endif
bool video_adaptive_vsync = false;
bool video_scanline_sync = false;
bool video_vsync = false;
bool video_frame_delay_auto = false;
if (settings)
{
#ifdef HAVE_MICROPHONE
#ifdef HAVE_NETWORKING
menu_pause_libretro = settings->bools.menu_pause_libretro
&& netplay_driver_ctl(RARCH_NETPLAY_CTL_ALLOW_PAUSE, NULL);
#else
menu_pause_libretro = settings->bools.menu_pause_libretro;
#endif
#endif
#ifdef HAVE_OVERLAY
input_overlay_hide_in_menu = settings->bools.input_overlay_hide_in_menu;
input_overlay_enable = settings->bools.input_overlay_enable;
#endif
video_adaptive_vsync = settings->bools.video_adaptive_vsync;
video_scanline_sync = settings->bools.video_scanline_sync;
video_vsync = settings->bools.video_vsync;
video_frame_delay_auto = settings->bools.video_frame_delay_auto;
}
if (on)
{
#ifdef HAVE_LAKKA
set_cpu_scaling_signal(CPUSCALING_EVENT_FOCUS_MENU);
#endif
#ifdef HAVE_OVERLAY
/* If an overlay was displayed before the toggle
* and overlays are disabled in menu, need to
* inhibit 'select' input */
if (input_overlay_hide_in_menu)
{
if (input_overlay_enable && overlay_alive)
{
/* Inhibits pointer 'select' and 'cancel' actions
* (until the next time 'select'/'cancel' are released) */
menu_input->select_inhibit = true;
menu_input->cancel_inhibit = true;
}
}
#endif
#ifdef HAVE_OPENXR
/* A laser press the menu closed on ended there: its release in
* the menu opened again is no click. */
if ( menu_input_headset_drives
&& (menu_st->input_pointer_hw_state.flags
& MENU_INP_PTR_FLG_PRESS_SELECT))
{
menu_st->input_pointer_hw_state.flags &=
~MENU_INP_PTR_FLG_PRESS_SELECT;
menu_input->select_inhibit = true;
}
#endif
}
else
{
#ifdef HAVE_LAKKA
set_cpu_scaling_signal(CPUSCALING_EVENT_FOCUS_CORE);
#endif
#ifdef HAVE_OVERLAY
/* Inhibits pointer 'select' and 'cancel' actions
* (until the next time 'select'/'cancel' are released) */
menu_input->select_inhibit = false;
menu_input->cancel_inhibit = false;
#endif
}
if (menu_driver_alive)
{
video_adaptive_vsync = video_adaptive_vsync
&& video_driver_test_all_flags(GFX_CTX_FLAGS_ADAPTIVE_VSYNC);
#ifdef WIIU
/* Enable burn-in protection menu is running */
IMEnableDim();
#endif
menu_st->flags |= MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
/* The window may have changed monitor, or its monitor mode,
* since the menu was last up; Menu Frame Rate's 'Display Rate'
* reads it again */
video_driver_window_output_changed();
/* Menu should always run with swap interval 1 if vsync is on.
* current_video can be NULL when the toggle runs while video is
* torn down or failed to initialize (shutdown paths - see the
* settings workaround above - and display loss on TV boxes). */
if ( video_vsync
&& !video_scanline_sync
&& current_video
&& current_video->set_nonblock_state)
current_video->set_nonblock_state(
video_driver_data,
false,
video_adaptive_vsync,
1);
/* Stop all rumbling before entering the menu. */
command_event(CMD_EVENT_RUMBLE_STOP, NULL);
/* Audio keeps running behind the menu: the runloop feeds the
* device silence while the core is paused, and menu sounds, the
* mixer and thumbnail video playback all mix into that stream.
* Only the microphone is stopped. */
#ifdef HAVE_MICROPHONE
if (menu_pause_libretro)
command_event(CMD_EVENT_MICROPHONE_STOP, NULL);
#endif
/* Override keyboard callback to redirect to menu instead.
* We'll use this later for something ... */
if (key_event && frontend_key_event)
{
*frontend_key_event = *key_event;
*key_event = menu_input_key_event;
runloop_st->frame_time_last = 0;
}
}
else
{
#ifdef WIIU
/* Disable burn-in protection while core is running; this is needed
* because HID inputs don't count for the purpose of Wii U
* power-saving. */
IMDisableDim();
#endif
if (!runloop_shutdown_initiated)
driver_set_nonblock_state();
#ifdef HAVE_MICROPHONE
if (menu_pause_libretro)
command_event(CMD_EVENT_MICROPHONE_START, NULL);
#endif
/* Restore libretro keyboard callback. */
if (key_event && frontend_key_event)
*key_event = *frontend_key_event;
}
/* Ignore frame delay target temporarily */
if (video_frame_delay_auto)
video_state_get_ptr()->frame_delay_pause = true;
}
void retroarch_menu_running(void)
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
video_driver_state_t *video_st = video_state_get_ptr();
settings_t *settings = config_get_ptr();
#ifdef HAVE_OVERLAY
bool input_overlay_hide_in_menu = settings->bools.input_overlay_hide_in_menu;
#endif
#ifdef HAVE_AUDIOMIXER
bool audio_enable_menu = settings->bools.audio_enable_menu;
bool audio_enable_menu_bgm = settings->bools.audio_enable_menu_bgm;
#endif
struct menu_state *menu_st = &menu_driver_state;
menu_handle_t *menu = menu_st->driver_data;
menu_input_t *menu_input = &menu_st->input_state;
menu_event_starts_afresh = true;
if (menu)
{
#ifdef HAVE_NETWORKING
bool menu_pause_libretro = settings->bools.menu_pause_libretro
&& netplay_driver_ctl(RARCH_NETPLAY_CTL_ALLOW_PAUSE, NULL);
#else
bool menu_pause_libretro = settings->bools.menu_pause_libretro;
#endif
/* Ramp core audio down before the driver toggle, which can take long
* enough to matter; here because the hotkey path does not go through
* CMD_EVENT_MENU_TOGGLE. Only when the runloop will in fact stop the
* core: under netplay it keeps running behind the menu. */
if (menu_pause_libretro)
audio_driver_pause_fade(true);
if (menu->driver_ctx && menu->driver_ctx->toggle)
menu->driver_ctx->toggle(menu->userdata, true);
if (!(menu_st->flags & MENU_ST_FLAG_ALIVE))
menu_event_opens = true;
menu_st->flags |= MENU_ST_FLAG_ALIVE;
menu_driver_drop_accept(true);
menu_driver_toggle(
video_st->current_video,
video_st->data,
menu,
menu_input,
settings,
(menu_st->flags & MENU_ST_FLAG_ALIVE) ? true : false,
#ifdef HAVE_OVERLAY
input_driver_overlay_alive(),
#else
false,
#endif
&runloop_st->key_event,
&runloop_st->frontend_key_event,
true);
}
/* Prevent stray input */
input_driver_hold_held_input();
#ifdef HAVE_AUDIOMIXER
if (audio_enable_menu && audio_enable_menu_bgm)
audio_driver_mixer_play_menu_sound_looped(AUDIO_MIXER_SYSTEM_SLOT_BGM);
#endif
/* Ensure that game focus mode is disabled when
* running the menu (note: it is not currently
* possible for game focus to be enabled at this
* point, but must safeguard against future changes) */
if (input_driver_game_focus_enabled())
{
enum input_game_focus_cmd_type game_focus_cmd = GAME_FOCUS_CMD_OFF;
command_event(CMD_EVENT_GAME_FOCUS_TOGGLE, &game_focus_cmd);
}
/* Ensure that menu screensaver is disabled when
* first switching to the menu */
if (menu_st->flags & MENU_ST_FLAG_SCREENSAVER_ACTIVE)
{
menu_st->flags &= ~MENU_ST_FLAG_SCREENSAVER_ACTIVE;
if (menu_st->driver_ctx->environ_cb)
menu_st->driver_ctx->environ_cb(MENU_ENVIRON_DISABLE_SCREENSAVER,
NULL, menu_st->userdata);
}
menu_st->input_last_time_us = cpu_features_get_time_usec();
#ifdef HAVE_OVERLAY
if (input_overlay_hide_in_menu)
command_event(CMD_EVENT_OVERLAY_UNLOAD, NULL);
#endif
}
void retroarch_menu_running_finished(bool quit)
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
video_driver_state_t*video_st = video_state_get_ptr();
settings_t *settings = config_get_ptr();
struct menu_state *menu_st = &menu_driver_state;
menu_handle_t *menu = menu_st->driver_data;
menu_input_t *menu_input = &menu_st->input_state;
/* Only allow toggling menu off with a proper core */
if (runloop_st->current_core_type == CORE_TYPE_DUMMY && !quit)
return;
if (menu)
{
if (menu->driver_ctx && menu->driver_ctx->toggle)
menu->driver_ctx->toggle(menu->userdata, false);
menu_st->flags &= ~MENU_ST_FLAG_ALIVE;
menu_driver_drop_accept(false);
/* Ramp the core's first frames back up. Not when quitting - nothing
* is coming back. Not gated on menu_pause_libretro: the setting can be
* turned off from inside the menu it paused, and a resume with no
* pause behind it does nothing. */
if (!quit)
audio_driver_pause_fade(false);
menu_driver_toggle(
video_st->current_video,
video_st->data,
menu,
menu_input,
settings,
menu_st->flags & MENU_ST_FLAG_ALIVE,
#ifdef HAVE_OVERLAY
input_driver_overlay_alive(),
#else
false,
#endif
&runloop_st->key_event,
&runloop_st->frontend_key_event,
false);
}
/* What is held as the menu closes does nothing more, in the menu or
* in the content, until it is let go */
input_driver_hold_held_input();
if (!quit)
{
/* The menu paces itself through the frame limit; running
* content gets its fast-forward limit back. Paused content keeps
* the display-rate limit the pause set, and unpausing restores it. */
if (!(runloop_st->flags & RUNLOOP_FLAG_PAUSED))
command_event(CMD_EVENT_SET_FRAME_LIMIT, NULL);
#ifdef HAVE_AUDIOMIXER
/* Stop menu background music before we exit the menu */
if ( settings
&& settings->bools.audio_enable_menu
&& settings->bools.audio_enable_menu_bgm
)
audio_driver_mixer_stop_stream(AUDIO_MIXER_SYSTEM_SLOT_BGM);
#endif
/* Enable game focus mode, if required */
if (runloop_st->current_core_type != CORE_TYPE_DUMMY)
{
enum input_auto_game_focus_type auto_game_focus_type = settings
? (enum input_auto_game_focus_type)settings->uints.input_auto_game_focus
: AUTO_GAME_FOCUS_OFF;
if ( (auto_game_focus_type == AUTO_GAME_FOCUS_ON)
|| ((auto_game_focus_type == AUTO_GAME_FOCUS_DETECT)
&& input_driver_game_focus_core_requested()))
{
enum input_game_focus_cmd_type game_focus_cmd = GAME_FOCUS_CMD_ON;
command_event(CMD_EVENT_GAME_FOCUS_TOGGLE, &game_focus_cmd);
}
}
#if HAVE_RUNAHEAD
/* Preemptive Frames isn't run behind the menu,
* so its savestate buffer is out of date. */
if (!settings->bools.menu_pause_libretro)
command_event(CMD_EVENT_PREEMPT_RESET_BUFFER, NULL);
#endif
}
/* Ensure that menu screensaver is disabled when
* switching off the menu */
if (menu_st->flags & MENU_ST_FLAG_SCREENSAVER_ACTIVE)
{
menu_st->flags &= ~MENU_ST_FLAG_SCREENSAVER_ACTIVE;
if (menu_st->driver_ctx->environ_cb)
menu_st->driver_ctx->environ_cb(MENU_ENVIRON_DISABLE_SCREENSAVER,
NULL, menu_st->userdata);
}
if (video_st->poke && video_st->poke->set_texture_enable)
video_st->poke->set_texture_enable(video_st->data,
false, false);
#ifdef HAVE_OVERLAY
if (!quit)
if (settings && settings->bools.input_overlay_hide_in_menu)
input_overlay_init();
#endif
}
#if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_SLANG) || defined(HAVE_HLSL)
static void menu_shader_manager_free(void)
{
video_driver_state_t *video_st = video_state_get_ptr();
if (video_st->menu_driver_shader)
free(video_st->menu_driver_shader);
video_st->menu_driver_shader = NULL;
}
#endif
bool menu_driver_ctl(enum rarch_menu_ctl_state state, void *data)
{
struct menu_state *menu_st = &menu_driver_state;
switch (state)
{
case RARCH_MENU_CTL_SET_PENDING_QUICK_MENU:
{
bool flush_stack = !data ? true : *((bool *)data);
if (flush_stack)
menu_entries_flush_stack(NULL, MENU_SETTINGS);
menu_st->flags |= MENU_ST_FLAG_PENDING_QUICK_MENU;
}
break;
case RARCH_MENU_CTL_DEINIT:
if (menu_media_launch_browsing || (menu_driver_media_startup_token() & 1))
menu_driver_media_open(NULL);
if ( menu_st->driver_ctx
&& menu_st->driver_ctx->context_destroy)
menu_st->driver_ctx->context_destroy(menu_st->userdata);
/* Retired previews must finish before the video device goes,
* including when the menu itself is being reused. */
gfx_thumbnail_anim_worker_deinit();
if (menu_st->flags & MENU_ST_FLAG_DATA_OWN)
return true;
gfx_thumbnail_parked_free();
/* Abandon a pending deferred playlist parse before the
* cache it would install goes away. */
playlist_init_cached_defer_abort();
playlist_free_cached();
/* End any in-flight background directory walk and drop an
* unconsumed listing; late completions become no-ops. */
menu_dirwalk_cancel();
#if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_SLANG) || defined(HAVE_HLSL)
menu_shader_manager_free();
#endif
#ifdef HAVE_NETWORKING
core_updater_list_free_cached();
#endif
#if defined(HAVE_MENU)
#if defined(HAVE_LIBRETRODB)
/* Abandon any in-flight explore menu initialisation and
* database info scan rather than blocking teardown until
* they finish - a full-file database scan is seconds of
* frozen UI on slow storage, and this path runs while the
* user is looking at the menu. Both tasks build into
* their own handles and install only from their main-thread
* callbacks, so the state freed just below is not something
* a worker can still be writing to; the cancels bump a
* generation that makes any completion already in flight
* install nothing. */
menu_explore_cancel_init_task();
menu_explore_free();
menu_dbinfo_cancel_task();
menu_dbinfo_cache_free();
#endif
menu_contentless_cores_free();
#endif
if (menu_st->driver_data)
{
unsigned i;
gfx_display_t *p_disp = disp_get_ptr();
menu_st->scroll.acceleration = 0;
menu_st->selection_ptr = 0;
menu_st->contentless_core_ptr = 0;
menu_st->scroll.index_size = 0;
menu_contentless_cores_flush_runtime();
for (i = 0; i < SCROLL_INDEX_SIZE; i++)
menu_st->scroll.index_list[i] = 0;
memset(&menu_st->input_state, 0, sizeof(menu_input_t));
memset(&menu_st->input_pointer_hw_state, 0, sizeof(menu_input_pointer_hw_state_t));
if ( menu_st->driver_ctx
&& menu_st->driver_ctx->free)
menu_st->driver_ctx->free(menu_st->userdata);
if (menu_st->userdata)
free(menu_st->userdata);
menu_st->userdata = NULL;
p_disp->menu_driver_id = MENU_DRIVER_ID_UNKNOWN;
#ifndef HAVE_DYNAMIC
if (frontend_driver_has_fork())
#endif
{
rarch_system_info_t *sys_info = &runloop_state_get_ptr()->system;
libretro_free_system_info(&sys_info->info);
memset(&sys_info->info, 0, sizeof(struct retro_system_info));
}
menu_entries_settings_deinit(menu_st);
if (menu_st->entries.list)
menu_list_free(menu_st->driver_ctx, menu_st->entries.list);
menu_st->entries.list = NULL;
/* Every list is gone, so every cbs has come back to the
* pool; the chunks behind them go back to libc here. */
menu_cbs_pool_deinit();
menu_driver_drop_accept(false);
/* Same point in teardown: every node has been released, so
* nothing is still sharing a fullpath. */
menu_str_cache_flush();
/* After the lists, not before them. menu_list_free()
* dispatches each driver's list_free hook, and those reach
* gfx_animation_kill_by_tag() -- directly in
* xmb_list_clear(), and again through gfx_thumbnail_reset()
* when a node owns an icon. Freeing the animation state
* first left those calls reading a list that had already
* gone; they did no damage only because RBUF_FREE() leaves
* the pointer NULL and RBUF_LEN() reads NULL as zero, so
* the scan found nothing to kill. That is the tween
* surviving by accident rather than by having been
* cancelled. The same ordering puts gfx_display_free()
* after the texture unloads those hooks perform. */
gfx_animation_deinit();
gfx_display_free();
if (menu_st->thumbnail_path_data)
free(menu_st->thumbnail_path_data);
menu_st->thumbnail_path_data = NULL;
if (menu_st->driver_data->core_buf)
free(menu_st->driver_data->core_buf);
menu_st->driver_data->core_buf = NULL;
menu_st->flags &= ~(MENU_ST_FLAG_ENTRIES_NEED_REFRESH
| MENU_ST_FLAG_ENTRIES_NONBLOCKING_REFRESH);
menu_st->entries.begin = 0;
command_event(CMD_EVENT_HISTORY_DEINIT, NULL);
retroarch_favorites_deinit();
menu_st->dialog_st.pending_push = false;
menu_st->dialog_st.current_id = 0;
menu_st->dialog_st.current_type = MENU_DIALOG_NONE;
free(menu_st->driver_data);
}
menu_st->driver_data = NULL;
break;
case RARCH_MENU_CTL_POINTER_DOWN:
{
menu_ctx_pointer_t *point = (menu_ctx_pointer_t*)data;
unsigned dims = menu_st->driver_ctx
&& menu_st->driver_ctx->set_texture
? disp_get_ptr()->framebuf_dims
: video_driver_get_ui_layer_dims(VIDEO_WINDOW_UI_MENU);
menu_pointer_outside_press = (unsigned)point->x >= VIDEO_SCALE_W(dims)
|| (unsigned)point->y >= VIDEO_SCALE_H(dims);
if (menu_pointer_outside_press)
{
point->retcode = 0;
return false;
}
if (!menu_st->driver_ctx || !menu_st->driver_ctx->pointer_down)
{
point->retcode = 0;
return false;
}
point->retcode = menu_st->driver_ctx->pointer_down(
menu_st->userdata,
point->x, point->y, point->ptr,
point->cbs, point->entry, point->action);
}
break;
case RARCH_MENU_CTL_POINTER_UP:
{
menu_ctx_pointer_t *point = (menu_ctx_pointer_t*)data;
unsigned dims = menu_st->driver_ctx
&& menu_st->driver_ctx->set_texture
? disp_get_ptr()->framebuf_dims
: video_driver_get_ui_layer_dims(VIDEO_WINDOW_UI_MENU);
if (menu_pointer_outside_press)
{
menu_pointer_outside_press = false;
point->retcode = 0;
return false;
}
if ((unsigned)point->x >= VIDEO_SCALE_W(dims)
|| (unsigned)point->y >= VIDEO_SCALE_H(dims))
{
point->retcode = 0;
return false;
}
if (!menu_st->driver_ctx || !menu_st->driver_ctx->pointer_up)
{
point->retcode = 0;
return false;
}
point->retcode = menu_st->driver_ctx->pointer_up(
menu_st->userdata,
point->x, point->y, point->ptr,
point->gesture,
point->cbs, point->entry, point->action);
}
break;
case RARCH_MENU_CTL_OSK_PTR_AT_POS:
{
unsigned output_size =
video_driver_get_ui_layer_dims(VIDEO_WINDOW_UI_MENU);
menu_ctx_pointer_t *point = (menu_ctx_pointer_t*)data;
if (!menu_st->driver_ctx || !menu_st->driver_ctx->osk_ptr_at_pos)
{
point->retcode = 0;
return false;
}
point->retcode = menu_st->driver_ctx->osk_ptr_at_pos(
menu_st->userdata,
point->x, point->y, output_size);
}
break;
case MENU_NAVIGATION_CTL_CLEAR:
{
bool *pending_push = (bool*)data;
/* Always set current selection to first entry */
menu_st->selection_ptr = 0;
/* navigation_set() will be called at the next 'push'.
* If a push is *not* pending, have to do it here
* instead */
if (!(*pending_push))
{
if (menu_st->driver_ctx->navigation_set)
menu_st->driver_ctx->navigation_set(menu_st->userdata, true);
if (menu_st->driver_ctx->navigation_clear)
menu_st->driver_ctx->navigation_clear(
menu_st->userdata, *pending_push);
}
}
break;
default:
case RARCH_MENU_CTL_NONE:
break;
}
return true;
}
#if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_SLANG) || defined(HAVE_HLSL)
struct video_shader *menu_shader_get(void)
{
gfx_ctx_flags_t flags;
flags.flags = 0;
video_context_driver_get_flags(&flags);
if (
BIT32_GET(flags.flags, GFX_CTX_FLAGS_SHADERS_SLANG)
|| BIT32_GET(flags.flags, GFX_CTX_FLAGS_SHADERS_GLSL)
|| BIT32_GET(flags.flags, GFX_CTX_FLAGS_SHADERS_CG)
|| BIT32_GET(flags.flags, GFX_CTX_FLAGS_SHADERS_HLSL))
{
video_driver_state_t *video_st = video_state_get_ptr();
if (video_st)
return video_st->menu_driver_shader;
}
return NULL;
}
/**
* menu_shader_manager_init:
*
* Initializes shader manager.
**/
bool menu_shader_manager_init(void)
{
gfx_ctx_flags_t flags;
video_driver_state_t *video_st = video_state_get_ptr();
enum rarch_shader_type type = RARCH_SHADER_NONE;
bool ret = true;
bool is_preset = false;
const char *path_shader = NULL;
struct video_shader *menu_shader = NULL;
/* We get the shader preset directly from the video driver, so that
* we are in sync with it (it could fail loading an auto-shader)
* If we can't (e.g. get_current_shader is not implemented),
* we'll load video_shader_get_current_shader_preset() like always */
video_shader_ctx_t shader_info = {0};
video_shader_driver_get_current_shader(&shader_info);
if (shader_info.data)
/* Use the path of the originally loaded preset because it could
* have been a preset with a #reference in it to another preset */
path_shader = shader_info.data->loaded_preset_path;
else
path_shader = video_shader_get_current_shader_preset();
menu_shader_manager_free();
if (!(menu_shader = (struct video_shader*)calloc(1, sizeof(*menu_shader))))
{
ret = false;
goto end;
}
if (!path_shader || !*path_shader)
goto end;
type = video_shader_get_type_from_ext(
path_get_extension(path_shader), &is_preset);
flags.flags = 0;
video_context_driver_get_flags(&flags);
if (!BIT32_GET(flags.flags, video_shader_type_to_flag(type)))
{
ret = false;
goto end;
}
if (is_preset)
{
if (!video_shader_load_preset_into_shader(path_shader, menu_shader))
{
ret = false;
goto end;
}
menu_shader->flags &= ~SHDR_FLAG_MODIFIED;
}
else
{
strlcpy(menu_shader->pass[0].source.path, path_shader,
sizeof(menu_shader->pass[0].source.path));
menu_shader->passes = 1;
}
end:
if (!ret)
{
size_t _len;
char msg[NAME_MAX_LENGTH];
_len = snprintf(msg, sizeof(msg), "Could not read shader preset: \"%s\".",
path_basename(path_shader));
runloop_msg_queue_push(msg, _len, 1, 120, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_ERROR);
}
video_st->menu_driver_shader = menu_shader;
command_event(CMD_EVENT_SHADER_PRESET_LOADED, NULL);
return ret;
}
/**
* menu_shader_manager_set_preset:
* @menu_shader : Shader handle to the menu shader.
* @type : Type of shader.
* @preset_path : Preset path to load from.
* @apply : Whether to apply the shader or just update shader information
*
* Sets shader preset.
**/
/* Same bookkeeping as menu_shader_manager_set_preset's success path,
* but the menu shader is copied from the driver's already-loaded
* struct instead of re-parsing the preset chain from disk (two
* chain walks saved per apply). Also removes a latent index
* mismatch: the slang backends rebuild the parameter list by
* reflection, and menu actions index the live struct with positions
* from the menu's own copy - a copy of the driver's struct agrees
* with it by construction, where a fresh parse only happens to. */
bool menu_shader_manager_set_preset_from_live(
struct video_shader *menu_shader,
const struct video_shader *live_shader)
{
struct menu_state *menu_st = &menu_driver_state;
if (!menu_shader || !live_shader)
return false;
video_shader_copy_for_menu(menu_shader, live_shader);
menu_st->flags |= MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
command_event(CMD_EVENT_SHADER_PRESET_LOADED, NULL);
return true;
}
bool menu_shader_manager_set_preset(struct video_shader *menu_shader,
enum rarch_shader_type type, const char *preset_path, bool apply)
{
bool ret = false;
struct menu_state *menu_st = &menu_driver_state;
settings_t *settings = config_get_ptr();
if (apply && !video_shader_apply_shader(settings, type, preset_path, true))
goto clear;
if (!preset_path || !*preset_path)
{
ret = true;
goto clear;
}
/* Load stored Preset into menu on success.
* Used when a preset is directly loaded.
* No point in updating when the Preset was
* created from the menu itself. */
if ( !menu_shader
|| !(video_shader_load_preset_into_shader(preset_path, menu_shader)))
goto end;
ret = true;
end:
menu_st->flags |= MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
command_event(CMD_EVENT_SHADER_PRESET_LOADED, NULL);
return ret;
clear:
/* We don't want to disable shaders entirely here,
* just reset number of passes
* > Note: Disabling shaders at this point would in
* fact be dangerous, since it changes the number of
* entries in the shader options menu which can in
* turn lead to the menu selection pointer going out
* of bounds. This causes undefined behaviour/segfaults */
menu_shader_manager_clear_num_passes_internal(menu_shader, false);
command_event(CMD_EVENT_SHADER_PRESET_LOADED, NULL);
return ret;
}
/**
* menu_shader_manager_append_preset:
* @shader : current shader
* @preset_path : path to the preset to append
* @dir_video_shader : temporary directory
*
* combine current shader with a shader preset on disk
**/
bool menu_shader_manager_append_preset(struct video_shader *shader,
const char* preset_path, const bool prepend)
{
bool ret = false;
const char *dir_video_shader = config_get_ptr()->paths.directory_video_shader;
enum rarch_shader_type type = menu_shader_manager_get_type(shader);
struct menu_state *menu_st = &menu_driver_state;
if (!preset_path || !*preset_path)
{
ret = true;
goto clear;
}
if (!video_shader_combine_preset_and_apply(
type, shader, preset_path, dir_video_shader, prepend, true))
goto clear;
ret = true;
menu_st->flags |= MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
command_event(CMD_EVENT_SHADER_PRESET_LOADED, NULL);
return ret;
clear:
/* We don't want to disable shaders entirely here,
* just reset number of passes
* > Note: Disabling shaders at this point would in
* fact be dangerous, since it changes the number of
* entries in the shader options menu which can in
* turn lead to the menu selection pointer going out
* of bounds. This causes undefined behaviour/segfaults */
menu_shader_manager_clear_num_passes_internal(shader, false);
command_event(CMD_EVENT_SHADER_PRESET_LOADED, NULL);
return ret;
}
#endif
/**
* action_cycle_thumbnail:
* @mode : menu action (primary/secondary)
*
* Common thumbnail cycler
*
* Returns: 0 on success, -1 on fail.
**/
int action_cycle_thumbnail(unsigned mode)
{
struct menu_state *menu_st = menu_state_get_ptr();
settings_t *settings = config_get_ptr();
if (!settings)
return -1;
if (mode == MENU_ACTION_CYCLE_THUMBNAIL_PRIMARY)
{
uint8_t cur_primary = settings->uints.gfx_thumbnails;
uint8_t cur_secondary = settings->uints.menu_left_thumbnails;
cur_primary++;
/* Prevent dupe image */
if (cur_primary == cur_secondary && cur_secondary)
cur_primary++;
/* Wrap primary to first image type, and skip logo */
if (cur_primary > PLAYLIST_THUMBNAIL_MODE_LAST - PLAYLIST_THUMBNAIL_MODE_OFF - 2)
cur_primary = 1;
/* Final dupe check */
if (cur_primary == cur_secondary && cur_secondary)
cur_primary++;
configuration_set_uint(settings, settings->uints.gfx_thumbnails, cur_primary);
}
else if (mode == MENU_ACTION_CYCLE_THUMBNAIL_SECONDARY)
{
uint8_t cur_primary = settings->uints.gfx_thumbnails;
uint8_t cur_secondary = settings->uints.menu_left_thumbnails;
cur_secondary++;
/* Prevent dupe image */
if (cur_primary == cur_secondary)
cur_secondary++;
/* Wrap secondary to no image, and skip logo.
* If primary disabled, wrap to first image. */
if (cur_secondary > PLAYLIST_THUMBNAIL_MODE_LAST - PLAYLIST_THUMBNAIL_MODE_OFF - 2)
cur_secondary = (cur_primary) ? 0 : 1;
configuration_set_uint(settings, settings->uints.menu_left_thumbnails, cur_secondary);
}
if (menu_st->driver_ctx)
{
if (menu_st->driver_ctx->refresh_thumbnail_image)
menu_st->driver_ctx->refresh_thumbnail_image(menu_st->userdata, menu_st->selection_ptr);
}
return 0;
}
/* Named buttons: Left/Up and Right/Down move the highlight and stop at
* the ends, OK presses it, Back and Info press Cancel (the first) */
static void menu_dialog_buttons_action(struct menu_state *menu_st,
enum menu_action action)
{
menu_dialog_t *d = &menu_st->dialog_st;
switch (action)
{
case MENU_ACTION_LEFT:
case MENU_ACTION_UP:
if (d->button_highlight > 0)
d->button_highlight--;
break;
case MENU_ACTION_RIGHT:
case MENU_ACTION_DOWN:
if (d->button_highlight + 1 < (int)d->button_count)
d->button_highlight++;
break;
case MENU_ACTION_OK:
menu_dialog_press(menu_st, d->button_highlight);
break;
case MENU_ACTION_CANCEL:
case MENU_ACTION_INFO:
menu_dialog_press(menu_st, 0);
break;
default:
break;
}
}
/**
* menu_iterate:
* @input : input sample for this frame
* @old_input : input sample of the previous frame
* @trigger_input : difference' input sample - difference
* between 'input' and 'old_input'
*
* Runs RetroArch menu for one frame.
*
* Returns: 0 on success, -1 if we need to quit out of the loop.
**/
static int generic_menu_iterate(
struct menu_state *menu_st,
gfx_display_t *p_disp,
gfx_animation_t *p_anim,
settings_t *settings,
menu_handle_t *menu,
void *userdata, enum menu_action action,
retro_time_t current_time)
{
#ifdef HAVE_ACCESSIBILITY
static enum action_iterate_type
last_iterate_type = ITERATE_TYPE_DEFAULT;
access_state_t *access_st = access_state_get_ptr();
bool accessibility_enable = settings->bools.accessibility_enable;
unsigned accessibility_narrator_speech_speed = settings->uints.accessibility_narrator_speech_speed;
#endif
enum action_iterate_type iterate_type;
int ret = 0;
const char *label = NULL;
file_list_t *list = MENU_LIST_GET(menu_st->entries.list, 0);
if (list && list->size)
label = list->list[list->size - 1].label;
menu->menu_state_msg[0] = '\0';
menu->menu_state_text = NULL;
/* buttons held on an empty combination row: what it is to do is
* picked next, from a list opened over this one */
{
unsigned number, pad;
if (input_entry_capture_take_fresh(&number, &pad))
menu_setting_entry_target_pick(number, pad);
/* the response time's list, after what it does was picked */
menu_setting_entry_pending();
if (list && list->size)
label = list->list[list->size - 1].label;
}
iterate_type = action_iterate_type(label, menu_st);
menu_st->flags &= ~MENU_ST_FLAG_IS_BINDING;
if ( action != MENU_ACTION_NOOP
|| MENU_ENTRIES_NEEDS_REFRESH(menu_st)
|| GFX_DISPLAY_GET_UPDATE_PENDING(p_anim, p_disp))
{
BIT64_SET(menu->state, MENU_STATE_RENDER_FRAMEBUFFER);
}
switch (iterate_type)
{
case ITERATE_TYPE_HELP:
ret = menu_dialog_iterate(
&menu_st->dialog_st, settings,
menu->menu_state_msg, sizeof(menu->menu_state_msg),
current_time);
#ifdef HAVE_ACCESSIBILITY
if ( (iterate_type != last_iterate_type)
&& is_accessibility_enabled(
accessibility_enable,
access_st->enabled))
accessibility_speak_priority(
accessibility_enable,
accessibility_narrator_speech_speed,
menu->menu_state_msg, 10);
#endif
BIT64_SET(menu->state, MENU_STATE_RENDER_MESSAGEBOX);
BIT64_SET(menu->state, MENU_STATE_POST_ITERATE);
if ( (ret == 1)
|| (action == MENU_ACTION_OK)
|| (action == MENU_ACTION_CANCEL)
)
BIT64_SET(menu->state, MENU_STATE_POP_STACK);
break;
case ITERATE_TYPE_CONFIRM:
#ifdef HAVE_CORE_SIGNATURES
if (menu_st->dialog_st.button_count)
menu->menu_state_text = menu_core_warning_text();
else
#endif
menu->menu_state_text = menu_st->dialog_st.confirm_text
? menu_st->dialog_st.confirm_text
: msg_hash_to_str(
(enum msg_hash_enums)menu_st->dialog_st.confirm_msg);
#ifdef HAVE_ACCESSIBILITY
/* Also when the warning comes up after a confirm or a newer
* request replaces it: the type stays CONFIRM */
if ( ( (iterate_type != last_iterate_type)
|| menu_st->dialog_st.button_announce)
&& is_accessibility_enabled(
accessibility_enable,
access_st->enabled))
accessibility_speak_priority(
accessibility_enable,
accessibility_narrator_speech_speed,
MENU_STATE_TEXT(menu), 10);
#endif
menu_st->dialog_st.button_announce = false;
BIT64_SET(menu->state, MENU_STATE_RENDER_MESSAGEBOX);
BIT64_SET(menu->state, MENU_STATE_POST_ITERATE);
/* Named buttons answer for themselves, and the box is no
* screen of the stack's */
if (menu_st->dialog_st.button_count)
{
#ifdef HAVE_ACCESSIBILITY
int was = menu_st->dialog_st.button_highlight;
#endif
menu_dialog_buttons_action(menu_st, action);
#ifdef HAVE_ACCESSIBILITY
if ( menu_st->dialog_st.button_count
&& menu_st->dialog_st.button_highlight != was
&& is_accessibility_enabled(
accessibility_enable,
access_st->enabled))
accessibility_speak_priority(
accessibility_enable,
accessibility_narrator_speech_speed,
msg_hash_to_str((enum msg_hash_enums)
menu_st->dialog_st.button_label[
menu_st->dialog_st.button_highlight]), 10);
#endif
break;
}
if ( action == MENU_ACTION_OK
|| action == MENU_ACTION_SELECT
|| action == MENU_ACTION_CANCEL
|| action == MENU_ACTION_INFO)
BIT64_SET(menu->state, MENU_STATE_POP_STACK);
if (menu_st->dialog_st.confirm_cmd)
{
if (action == MENU_ACTION_OK || action == MENU_ACTION_SELECT)
menu_dialog_confirm(menu_st);
else if (action == MENU_ACTION_CANCEL || action == MENU_ACTION_INFO)
menu_dialog_confirm_clear(menu_st);
}
break;
case ITERATE_TYPE_BIND:
{
menu_input_ctx_bind_t bind;
menu_st->flags |= MENU_ST_FLAG_IS_BINDING;
bind.s = menu->menu_state_msg;
bind.len = sizeof(menu->menu_state_msg);
if (menu_input_key_bind_iterate(settings, &bind, current_time))
{
size_t selection = menu_st->selection_ptr;
menu_entries_pop_stack(&selection, 0, 0);
menu_st->selection_ptr = selection;
}
else
BIT64_SET(menu->state, MENU_STATE_RENDER_MESSAGEBOX);
}
break;
case ITERATE_TYPE_ENTRY_CAPTURE:
/* the menu takes none of what is pressed meanwhile: the
* capture has had it (it runs in the input pass, ahead of
* this) */
menu_st->flags |= MENU_ST_FLAG_IS_BINDING;
if (!menu_input_entry_capture_iterate(menu->menu_state_msg,
sizeof(menu->menu_state_msg)))
BIT64_SET(menu->state, MENU_STATE_RENDER_MESSAGEBOX);
break;
case ITERATE_TYPE_REMAP_FIND:
/* the menu's own controls are left alone meanwhile */
menu_st->flags |= MENU_ST_FLAG_IS_BINDING;
if (menu_input_remap_find_iterate(menu->menu_state_msg,
sizeof(menu->menu_state_msg), current_time))
{
size_t selection = menu_st->selection_ptr;
menu_list_t *menu_list = menu_st->entries.list;
file_list_t *entries;
menu_entries_pop_stack(&selection, 0, 0);
menu_st->selection_ptr = selection;
/* onto the entry of the button that was pressed: the same
* list, with the pad's entries or the keyboard's */
entries = menu_list
? MENU_LIST_GET_SELECTION(menu_list, (unsigned)0) : NULL;
if (entries && menu_remap_find.found >= 0)
{
size_t k;
unsigned at = (menu_remap_find.port * RARCH_ANALOG_BIND_LIST_END)
+ (unsigned)menu_remap_find.found;
for (k = 0; k < entries->size; k++)
if ( entries->list[k].type
== MENU_SETTINGS_INPUT_DESC_BEGIN + at
|| entries->list[k].type
== MENU_SETTINGS_INPUT_DESC_KBD_BEGIN + at)
{
menu_st->selection_ptr = k;
/* and the list scrolled to it: a stick's entry
* is past the first screen of them */
if ( menu_st->driver_ctx
&& menu_st->driver_ctx->navigation_set)
menu_st->driver_ctx->navigation_set(
menu_st->userdata, true);
break;
}
}
}
else
BIT64_SET(menu->state, MENU_STATE_RENDER_MESSAGEBOX);
break;
case ITERATE_TYPE_INFO:
{
menu_list_t *menu_list = menu_st->entries.list;
file_list_t *selection_buf = menu_list ? MENU_LIST_GET_SELECTION(menu_list, (unsigned)0) : NULL;
size_t selection = menu_st->selection_ptr;
menu_file_list_cbs_t *cbs = selection_buf ?
(menu_file_list_cbs_t*)selection_buf->list[selection].actiondata
: NULL;
if (cbs && cbs->enum_idx != MSG_UNKNOWN)
{
/* Core updater/manager entries require special treatment */
switch (cbs->enum_idx)
{
#ifdef HAVE_NETWORKING
case MENU_ENUM_LABEL_CORE_UPDATER_ENTRY:
{
core_updater_list_t *core_list =
core_updater_list_get_cached();
const core_updater_list_entry_t *entry = NULL;
const char *path =
selection_buf->list[selection].path;
/* Search for specified core */
if (
core_list
&& path
&& core_updater_list_get_filename(core_list,
path, &entry)
&& (entry->description && *entry->description)
)
strlcpy(menu->menu_state_msg, entry->description,
sizeof(menu->menu_state_msg));
else
menu->menu_state_text = msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_NO_INFORMATION_AVAILABLE);
ret = 0;
}
break;
#endif
case MENU_ENUM_LABEL_CORE_MANAGER_ENTRY:
case MENU_ENUM_LABEL_CONTENTLESS_CORE:
{
core_info_t *core_info = NULL;
const char *path = selection_buf->list[selection].path;
/* Search for specified core */
if ( path
&& core_info_find(path, &core_info)
&& (core_info->description && *core_info->description))
strlcpy(menu->menu_state_msg,
core_info->description,
sizeof(menu->menu_state_msg));
else
menu->menu_state_text = msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_NO_INFORMATION_AVAILABLE);
ret = 0;
}
break;
default:
ret = msg_hash_get_help_enum(cbs->enum_idx,
menu->menu_state_msg, sizeof(menu->menu_state_msg));
if (string_is_equal(menu->menu_state_msg,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NO_INFORMATION_AVAILABLE)))
{
menu_driver_get_current_menu_sublabel(
menu_st,
menu->menu_state_msg, sizeof(menu->menu_state_msg));
if (!*menu->menu_state_msg)
menu->menu_state_text = msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_NO_INFORMATION_AVAILABLE);
}
break;
}
#ifdef HAVE_ACCESSIBILITY
if ( (iterate_type != last_iterate_type) &&
is_accessibility_enabled(
accessibility_enable,
access_st->enabled))
{
if (string_is_equal(MENU_STATE_TEXT(menu),
msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_NO_INFORMATION_AVAILABLE)))
{
char current_sublabel[NAME_MAX_LENGTH];
menu_driver_get_current_menu_sublabel(
menu_st,
current_sublabel, sizeof(current_sublabel));
if (string_is_equal(current_sublabel, ""))
accessibility_speak_priority(
accessibility_enable,
accessibility_narrator_speech_speed,
MENU_STATE_TEXT(menu), 10);
else
accessibility_speak_priority(
accessibility_enable,
accessibility_narrator_speech_speed,
current_sublabel, 10);
}
else
accessibility_speak_priority(
accessibility_enable,
accessibility_narrator_speech_speed,
MENU_STATE_TEXT(menu), 10);
}
#endif
}
else
{
enum msg_hash_enums enum_idx = MSG_UNKNOWN;
size_t selection = menu_st->selection_ptr;
unsigned type = selection_buf->list[selection].type;
switch (type)
{
case FILE_TYPE_FONT:
case FILE_TYPE_VIDEO_FONT:
enum_idx = MENU_ENUM_LABEL_FILE_BROWSER_FONT;
break;
case FILE_TYPE_RDB:
enum_idx = MENU_ENUM_LABEL_FILE_BROWSER_RDB;
break;
case FILE_TYPE_OVERLAY:
enum_idx = MENU_ENUM_LABEL_FILE_BROWSER_OVERLAY;
break;
case FILE_TYPE_CHEAT:
enum_idx = MENU_ENUM_LABEL_FILE_BROWSER_CHEAT;
break;
case FILE_TYPE_SHADER_PRESET:
enum_idx = MENU_ENUM_LABEL_FILE_BROWSER_SHADER_PRESET;
break;
case FILE_TYPE_SHADER:
enum_idx = MENU_ENUM_LABEL_FILE_BROWSER_SHADER;
break;
case FILE_TYPE_REMAP:
enum_idx = MENU_ENUM_LABEL_FILE_BROWSER_REMAP;
break;
case FILE_TYPE_RECORD_CONFIG:
enum_idx = MENU_ENUM_LABEL_FILE_BROWSER_RECORD_CONFIG;
break;
case FILE_TYPE_CONFIG:
enum_idx = MENU_ENUM_LABEL_FILE_BROWSER_CONFIG;
break;
case FILE_TYPE_CARCHIVE:
enum_idx = MENU_ENUM_LABEL_FILE_BROWSER_COMPRESSED_ARCHIVE;
break;
case FILE_TYPE_DIRECTORY:
enum_idx = MENU_ENUM_LABEL_FILE_BROWSER_DIRECTORY;
break;
case FILE_TYPE_VIDEOFILTER: /* TODO/FIXME */
case FILE_TYPE_AUDIOFILTER: /* TODO/FIXME */
case FILE_TYPE_SHADER_SLANG: /* TODO/FIXME */
case FILE_TYPE_SHADER_GLSL: /* TODO/FIXME */
case FILE_TYPE_SHADER_HLSL: /* TODO/FIXME */
case FILE_TYPE_SHADER_CG: /* TODO/FIXME */
case FILE_TYPE_SHADER_PRESET_GLSLP: /* TODO/FIXME */
case FILE_TYPE_SHADER_PRESET_HLSLP: /* TODO/FIXME */
case FILE_TYPE_SHADER_PRESET_CGP: /* TODO/FIXME */
case FILE_TYPE_SHADER_PRESET_SLANGP: /* TODO/FIXME */
case FILE_TYPE_PLAIN:
enum_idx = MENU_ENUM_LABEL_FILE_BROWSER_PLAIN_FILE;
break;
default:
break;
}
if (enum_idx != MSG_UNKNOWN)
ret = msg_hash_get_help_enum(enum_idx,
menu->menu_state_msg, sizeof(menu->menu_state_msg));
else
{
/* Special handling for input and remap items */
if ( ( type >= MENU_SETTINGS_REMAPPING_PORT_BEGIN
&& type <= MENU_SETTINGS_REMAPPING_PORT_END)
|| type == MENU_SETTINGS_INPUT_LIBRETRO_DEVICE
|| type == MENU_SETTINGS_INPUT_INPUT_REMAP_PORT)
{
menu_driver_get_current_menu_sublabel(
menu_st,
menu->menu_state_msg, sizeof(menu->menu_state_msg));
ret = 0;
}
/* Use detailed help text for 'Analog to Digital', which
* is the fourth item in global input settings, otherwise
* use sublabels as usual. */
else if (type == MENU_SETTINGS_INPUT_ANALOG_DPAD_MODE
|| type == MENU_SETTINGS_INPUT_BEGIN + 3)
ret = msg_hash_get_help_enum(MENU_ENUM_LABEL_VALUE_INPUT_ADC_TYPE,
menu->menu_state_msg, sizeof(menu->menu_state_msg));
else if (type >= MENU_SETTINGS_INPUT_BEGIN
&& type < MENU_SETTINGS_INPUT_BINDS_BEGIN)
{
menu_driver_get_current_menu_sublabel(
menu_st,
menu->menu_state_msg, sizeof(menu->menu_state_msg));
ret = 0;
}
else
{
menu->menu_state_text = msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_NO_INFORMATION_AVAILABLE);
ret = 0;
}
}
}
}
BIT64_SET(menu->state, MENU_STATE_RENDER_MESSAGEBOX);
BIT64_SET(menu->state, MENU_STATE_POST_ITERATE);
if ( action == MENU_ACTION_OK
|| action == MENU_ACTION_CANCEL
|| action == MENU_ACTION_INFO)
BIT64_SET(menu->state, MENU_STATE_POP_STACK);
break;
case ITERATE_TYPE_DEFAULT:
{
menu_entry_t *entry = &menu_st->iterate_entry;
menu_list_t *menu_list = menu_st->entries.list;
size_t selection = menu_st->selection_ptr;
size_t menu_list_size = menu_st->entries.list ? MENU_LIST_GET_SELECTION(menu_st->entries.list, 0)->size : 0;
/* FIXME: Crappy hack, needed for mouse controls
* to not be completely broken in case we press back.
*
* We need to fix this entire mess, mouse controls
* should not rely on a hack like this in order to work. */
selection = MAX(MIN(selection, (menu_list_size - 1)), 0);
MENU_ENTRY_INITIALIZE((*entry));
/* NOTE: If menu_entry_action() is modified,
* will have to verify that these parameters
* remain unused... */
entry->flags |= MENU_ENTRY_FLAG_PATH_ENABLED
| MENU_ENTRY_FLAG_LABEL_ENABLED;
menu_entry_get(entry, 0, selection, NULL, false);
if ((ret = menu_entry_action(entry,
selection, (enum menu_action)action)))
return -1;
/* menu_entry_action() can tear down and recreate the menu
* (a menu driver change, or any CMD_EVENT_REINIT dispatched
* from an entry's action handler): the handle and the
* entries list cached above are stale once it returns.
* Re-fetch both before writing through them - on 64-bit
* heaps the stale menu->state write lands in freed memory
* silently instead of faulting, so this corrupts quietly
* everywhere the fault does not reproduce. */
if (!(menu = menu_st->driver_data))
return 0;
menu_list = menu_st->entries.list;
BIT64_SET(menu->state, MENU_STATE_POST_ITERATE);
/* Have to defer it so we let settings refresh. */
if (menu_st->dialog_st.pending_push)
{
menu_displaylist_info_t info;
menu_displaylist_info_init(&info);
info.list = menu_list ? MENU_LIST_GET(menu_list, (unsigned)0) : NULL;
info.enum_idx = MENU_ENUM_LABEL_HELP;
/* Set the label string, if it exists. */
info.label = strdup(MENU_ENUM_LABEL_HELP_STR);
menu_displaylist_ctl(DISPLAYLIST_HELP, &info, settings);
}
}
break;
}
#ifdef HAVE_ACCESSIBILITY
if ((last_iterate_type == ITERATE_TYPE_HELP
|| last_iterate_type == ITERATE_TYPE_INFO)
&& last_iterate_type != iterate_type
&& is_accessibility_enabled(
accessibility_enable,
access_st->enabled))
accessibility_speak_priority(
accessibility_enable,
accessibility_narrator_speech_speed,
"Closed dialog.", 10);
last_iterate_type = iterate_type;
#endif
BIT64_SET(menu->state, MENU_STATE_BLIT);
if (BIT64_GET(menu->state, MENU_STATE_POP_STACK))
{
size_t selection = menu_st->selection_ptr;
size_t new_selection_ptr = selection;
menu_entries_pop_stack(&new_selection_ptr, 0, 0);
menu_st->selection_ptr = selection;
/* Play sound for closing the info box */
#ifdef HAVE_AUDIOMIXER
{
bool audio_enable_menu = settings->bools.audio_enable_menu;
bool audio_enable_menu_notice = settings->bools.audio_enable_menu_notice;
if (audio_enable_menu && audio_enable_menu_notice &&
string_is_equal(label, MENU_ENUM_LABEL_INFO_SCREEN_STR))
audio_driver_mixer_play_menu_sound(AUDIO_MIXER_SYSTEM_SLOT_NOTICE_BACK);
}
#endif
}
if (BIT64_GET(menu->state, MENU_STATE_POST_ITERATE))
{
menu_input_t *menu_input = &menu_st->input_state;
/* If pointer devices are disabled, just ensure mouse
* cursor is hidden */
if (menu_input->pointer.type == MENU_POINTER_DISABLED)
{
/* menu_input_post_iterate() is skipped here, so its
* 'last_*_pressed' edge detectors stop being updated
* while the hardware press flags are left untouched.
* Clear the flags so that the two cannot desynchronise
* and emit a phantom press/release once pointer input
* is re-enabled */
menu_st->input_pointer_hw_state.flags &=
~(MENU_INP_PTR_FLG_PRESS_SELECT
| MENU_INP_PTR_FLG_PRESS_CANCEL
| MENU_INP_PTR_FLG_PRESS_LEFT
| MENU_INP_PTR_FLG_PRESS_RIGHT);
ret = 0;
}
else
ret = menu_input_post_iterate(p_disp, menu_st, action,
current_time);
menu_input_set_pointer_visibility(
&menu_st->input_pointer_hw_state,
menu_input, current_time);
}
if (ret)
return -1;
return 0;
}
int generic_menu_entry_action(
void *userdata, menu_entry_t *entry, size_t i,
enum menu_action action)
{
int ret = 0;
struct menu_state *menu_st = &menu_driver_state;
const menu_ctx_driver_t
*menu_driver_ctx = menu_st->driver_ctx;
settings_t *settings = config_get_ptr();
void *menu_userdata = menu_st->userdata;
bool wraparound_enable = settings->bools.menu_navigation_wraparound_enable;
bool scroll_mode = menu_st->scroll.mode;
size_t scroll_accel = menu_st->scroll.acceleration;
menu_list_t *menu_list = menu_st->entries.list;
file_list_t *selection_buf = menu_list ? MENU_LIST_GET_SELECTION(menu_list, (unsigned)0) : NULL;
file_list_t *menu_stack = menu_list ? MENU_LIST_GET(menu_list, (unsigned)0) : NULL;
size_t entries_size = menu_list ? MENU_LIST_GET_SELECTION(menu_list, 0)->size : 0;
size_t selection_buf_size = selection_buf ? selection_buf->size : 0;
menu_file_list_cbs_t *cbs = selection_buf && selection_buf->size ?
(menu_file_list_cbs_t*)selection_buf->list[i].actiondata : NULL;
#ifdef HAVE_ACCESSIBILITY
bool accessibility_enable = settings->bools.accessibility_enable;
unsigned accessibility_narrator_speech_speed = settings->uints.accessibility_narrator_speech_speed;
access_state_t *access_st = access_state_get_ptr();
#endif
switch (action)
{
case MENU_ACTION_UP:
if (selection_buf_size > 0)
{
/* a repeat moves the entries its clock made due (see
* menu_event()); a press moves one */
unsigned scroll_speed = menu_st->scroll.steps
? menu_st->scroll.steps
: (unsigned)((MAX(scroll_accel, 2) - 2) / 4 + 1);
if (!(menu_st->selection_ptr == 0 && !wraparound_enable))
{
size_t idx = 0;
if (menu_st->selection_ptr >= scroll_speed)
idx = menu_st->selection_ptr - scroll_speed;
else
{
idx = selection_buf_size - 1;
if (!wraparound_enable)
idx = 0;
}
menu_st->selection_ptr = idx;
if (menu_st->driver_ctx->navigation_set)
menu_st->driver_ctx->navigation_set(menu_st->userdata, true);
if (menu_driver_ctx->navigation_decrement)
menu_driver_ctx->navigation_decrement(menu_userdata);
#ifdef HAVE_AUDIOMIXER
if (entries_size != 1)
audio_driver_mixer_play_scroll_sound(true);
#endif
}
}
break;
case MENU_ACTION_DOWN:
if (selection_buf_size > 0)
{
/* a repeat moves the entries its clock made due (see
* menu_event()); a press moves one */
unsigned scroll_speed = menu_st->scroll.steps
? menu_st->scroll.steps
: (unsigned)((MAX(scroll_accel, 2) - 2) / 4 + 1);
if (!(menu_st->selection_ptr >= selection_buf_size - 1
&& !wraparound_enable))
{
if ((menu_st->selection_ptr + scroll_speed) < selection_buf_size)
{
size_t idx = menu_st->selection_ptr + scroll_speed;
menu_st->selection_ptr = idx;
if (menu_st->driver_ctx->navigation_set)
menu_st->driver_ctx->navigation_set(menu_st->userdata, true);
}
else
{
if (wraparound_enable)
{
bool pending_push = false;
menu_driver_ctl(MENU_NAVIGATION_CTL_CLEAR, &pending_push);
}
else
{
size_t menu_list_size = menu_st->entries.list ? MENU_LIST_GET_SELECTION(menu_st->entries.list, 0)->size : 0;
size_t new_selection = menu_list_size - 1;
menu_st->selection_ptr = new_selection;
if (menu_st->driver_ctx->navigation_set_last)
menu_st->driver_ctx->navigation_set_last(menu_st->userdata);
}
}
if (menu_driver_ctx->navigation_increment)
menu_driver_ctx->navigation_increment(menu_userdata);
#ifdef HAVE_AUDIOMIXER
if (entries_size != 1)
audio_driver_mixer_play_scroll_sound(false);
#endif
}
}
break;
case MENU_ACTION_SCROLL_UP:
if (scroll_mode == MENU_SCROLL_PAGE)
{
if (selection_buf_size > 0)
{
unsigned scroll_speed = (unsigned)((MAX(scroll_accel, 2) - 2) / 4 + 10);
#ifdef HAVE_AUDIOMIXER
if (menu_st->selection_ptr != 0)
audio_driver_mixer_play_scroll_sound(true);
#endif
if (!(menu_st->selection_ptr == 0 && !wraparound_enable))
{
size_t idx = 0;
if (menu_st->selection_ptr >= scroll_speed)
idx = menu_st->selection_ptr - scroll_speed;
menu_st->selection_ptr = idx;
if (menu_st->driver_ctx->navigation_set)
menu_st->driver_ctx->navigation_set(menu_st->userdata, true);
if (menu_driver_ctx->navigation_decrement)
menu_driver_ctx->navigation_decrement(menu_userdata);
}
}
}
else /* MENU_SCROLL_START_LETTER */
{
#ifdef HAVE_AUDIOMIXER
size_t selection_old = menu_st->selection_ptr;
#endif
if (
menu_st->scroll.index_size
&& menu_st->selection_ptr != 0
)
{
size_t l = menu_st->scroll.index_size - 1;
while (l
&& menu_st->scroll.index_list[l - 1]
>= menu_st->selection_ptr)
l--;
if (l > 0)
menu_st->selection_ptr = menu_st->scroll.index_list[l - 1];
if (menu_driver_ctx->navigation_descend_alphabet)
menu_driver_ctx->navigation_descend_alphabet(
menu_userdata, &menu_st->selection_ptr);
}
#ifdef HAVE_AUDIOMIXER
if (menu_st->selection_ptr != selection_old)
audio_driver_mixer_play_scroll_sound(true);
#endif
}
break;
case MENU_ACTION_SCROLL_DOWN:
if (scroll_mode == MENU_SCROLL_PAGE)
{
if (selection_buf_size > 0)
{
unsigned scroll_speed = (unsigned)((MAX(scroll_accel, 2) - 2) / 4 + 10);
#ifdef HAVE_AUDIOMIXER
if (menu_st->selection_ptr != entries_size - 1)
audio_driver_mixer_play_scroll_sound(false);
#endif
if (!(menu_st->selection_ptr >= selection_buf_size - 1
&& !wraparound_enable))
{
if ((menu_st->selection_ptr + scroll_speed) < selection_buf_size)
{
size_t idx = menu_st->selection_ptr + scroll_speed;
menu_st->selection_ptr = idx;
if (menu_st->driver_ctx->navigation_set)
menu_st->driver_ctx->navigation_set(menu_st->userdata, true);
}
else
{
size_t menu_list_size = menu_st->entries.list ? MENU_LIST_GET_SELECTION(menu_st->entries.list, 0)->size : 0;
size_t new_selection = menu_list_size - 1;
menu_st->selection_ptr = new_selection;
if (menu_st->driver_ctx->navigation_set_last)
menu_st->driver_ctx->navigation_set_last(menu_st->userdata);
}
if (menu_driver_ctx->navigation_increment)
menu_driver_ctx->navigation_increment(menu_userdata);
}
}
}
else /* MENU_SCROLL_START_LETTER */
{
if (menu_st->scroll.index_size)
{
#ifdef HAVE_AUDIOMIXER
size_t selection_old = menu_st->selection_ptr;
#endif
if (menu_st->selection_ptr == menu_st->scroll.index_list[menu_st->scroll.index_size - 1])
menu_st->selection_ptr = selection_buf_size - 1;
else
{
size_t l = 0;
while (l < menu_st->scroll.index_size - 1
&& menu_st->scroll.index_list[l + 1] <= menu_st->selection_ptr)
l++;
menu_st->selection_ptr = menu_st->scroll.index_list[l + 1];
if (menu_st->selection_ptr >= selection_buf_size)
menu_st->selection_ptr = selection_buf_size - 1;
}
if (menu_driver_ctx->navigation_ascend_alphabet)
menu_driver_ctx->navigation_ascend_alphabet(
menu_userdata, &menu_st->selection_ptr);
#ifdef HAVE_AUDIOMIXER
if (menu_st->selection_ptr != selection_old)
audio_driver_mixer_play_scroll_sound(false);
#endif
}
}
break;
case MENU_ACTION_SCROLL_HOME:
{
#ifdef HAVE_AUDIOMIXER
size_t selection_old = menu_st->selection_ptr;
#endif
menu_st->selection_ptr = 0;
if (menu_st->driver_ctx->navigation_set)
menu_st->driver_ctx->navigation_set(menu_st->userdata, true);
#ifdef HAVE_AUDIOMIXER
if (menu_st->selection_ptr != selection_old)
audio_driver_mixer_play_scroll_sound(true);
#endif
break;
}
case MENU_ACTION_SCROLL_END:
{
#ifdef HAVE_AUDIOMIXER
size_t selection_old = menu_st->selection_ptr;
#endif
menu_st->selection_ptr = entries_size ? entries_size - 1 : 0;
if (menu_st->driver_ctx->navigation_set)
menu_st->driver_ctx->navigation_set(menu_st->userdata, true);
#ifdef HAVE_AUDIOMIXER
if (menu_st->selection_ptr != selection_old)
audio_driver_mixer_play_scroll_sound(false);
#endif
break;
}
case MENU_ACTION_CYCLE_THUMBNAIL_PRIMARY:
case MENU_ACTION_CYCLE_THUMBNAIL_SECONDARY:
action_cycle_thumbnail(action);
break;
case MENU_ACTION_CANCEL:
if (cbs && cbs->action_cancel)
ret = cbs->action_cancel(entry->path,
entry->label, entry->type, i);
break;
case MENU_ACTION_OK:
if (cbs && cbs->action_ok)
ret = cbs->action_ok(entry->path,
entry->label, entry->type, i, entry->entry_idx);
break;
case MENU_ACTION_START:
if (cbs && cbs->action_start)
ret = cbs->action_start(entry->path,
entry->label, entry->type, i, entry->entry_idx);
break;
case MENU_ACTION_LEFT:
if (cbs && cbs->action_left)
ret = cbs->action_left(entry->type, entry->label, false);
break;
case MENU_ACTION_RIGHT:
if (cbs && cbs->action_right)
ret = cbs->action_right(entry->type, entry->label, false);
break;
case MENU_ACTION_INFO:
if (cbs && cbs->action_info)
ret = cbs->action_info(entry->type, entry->label);
break;
case MENU_ACTION_SELECT:
if (cbs && cbs->action_select)
ret = cbs->action_select(entry->path,
entry->label, entry->type, i, entry->entry_idx);
break;
case MENU_ACTION_SEARCH:
menu_input_dialog_start_search();
break;
case MENU_ACTION_SCAN:
if (cbs && cbs->action_scan)
ret = cbs->action_scan(entry->path,
entry->label, entry->type, i);
break;
case MENU_ACTION_RESUME:
command_event(CMD_EVENT_MENU_TOGGLE, NULL);
break;
default:
break;
}
if (MENU_ENTRIES_NEEDS_REFRESH(menu_st) && selection_buf_size)
{
/* The action handler dispatched above is free to tear the menu
* down and build it again from scratch - a menu or video reinit,
* a driver switch, a language change and 'close content' all end
* up in RARCH_MENU_CTL_DEINIT, which menu_list_free()s every
* menu_stack[i] / selection_buf[i] before menu_list_new()
* allocates replacements.
*
* The pointers captured on entry to this function are dangling in
* that case, so re-fetch them from menu_st before handing them to
* the displaylist push. */
menu_list = menu_st->entries.list;
selection_buf = menu_list
? MENU_LIST_GET_SELECTION(menu_list, (unsigned)0) : NULL;
menu_stack = menu_list
? MENU_LIST_GET(menu_list, (unsigned)0) : NULL;
if (selection_buf && menu_stack)
menu_driver_displaylist_push(
menu_st,
settings,
selection_buf,
menu_stack);
menu_st->flags &= ~MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
}
#ifdef HAVE_ACCESSIBILITY
if ( action != 0
&& is_accessibility_enabled(
accessibility_enable,
access_st->enabled)
&& !menu_input_dialog_get_display_kb())
{
char current_value[128];
char title_name[NAME_MAX_LENGTH];
char speak_string[512];
title_name [0] = '\0';
get_current_menu_value(menu_st,
current_value, sizeof(current_value));
switch (action)
{
case MENU_ACTION_ACCESSIBILITY_SPEAK_TITLE:
menu_entries_get_title(title_name, sizeof(title_name));
break;
case MENU_ACTION_START:
/* if equal to '..' we break, else we fall-through */
if (string_starts_with_size(current_value, "...",
STRLEN_CONST("...")))
break;
/* fall-through */
case MENU_ACTION_ACCESSIBILITY_SPEAK_TITLE_LABEL:
case MENU_ACTION_OK:
case MENU_ACTION_LEFT:
case MENU_ACTION_RIGHT:
case MENU_ACTION_CANCEL:
menu_entries_get_title(title_name, sizeof(title_name));
/* fall-through */
case MENU_ACTION_UP:
case MENU_ACTION_DOWN:
case MENU_ACTION_SCROLL_UP:
case MENU_ACTION_SCROLL_DOWN:
case MENU_ACTION_SELECT:
case MENU_ACTION_SEARCH:
case MENU_ACTION_ACCESSIBILITY_SPEAK_LABEL:
break;
case MENU_ACTION_SCAN:
case MENU_ACTION_INFO:
default:
break;
}
if (*title_name)
{
size_t _len = strlcpy(speak_string,
title_name, sizeof(speak_string));
speak_string[ _len] = ' ';
speak_string[++_len] = '\0';
_len += menu_driver_get_current_menu_label(
menu_st,
speak_string + _len,
sizeof(speak_string) - _len);
if (!string_is_equal(current_value, "..."))
{
speak_string[ _len] = ' ';
speak_string[++_len] = '\0';
strlcpy(speak_string + _len,
current_value,
sizeof(speak_string) - _len);
}
}
else
{
size_t _len = menu_driver_get_current_menu_label(
menu_st,
speak_string,
sizeof(speak_string));
if (!string_is_equal(current_value, "..."))
{
speak_string[ _len] = ' ';
speak_string[++_len] = '\0';
strlcpy(speak_string + _len,
current_value,
sizeof(speak_string) - _len);
}
}
if (*speak_string)
accessibility_speak_priority(
accessibility_enable,
accessibility_narrator_speech_speed,
speak_string, 10);
}
#endif
return ret;
}
/* Iterate the menu driver for one frame. */
/* Advance work a displaylist left unfinished, and rebuild the list
* when it completes.
*
* ENTRIES_NEED_REFRESH is otherwise consumed only in
* generic_menu_entry_action(), i.e. when the user presses something.
* Anything that yields mid-build therefore sat unfinished until the
* next keypress: a playlist read that did not fit its first slice
* showed an empty list, and populated only after backing out and
* re-entering. This is the pump that was missing - it runs every
* frame, with no input required.
*
* Only a genuinely pending parse does any work here, so a menu with
* nothing outstanding costs one predictable branch. */
static void menu_driver_pump_pending(struct menu_state *menu_st,
settings_t *settings)
{
nbio_budget_t b;
menu_list_t *menu_list;
file_list_t *selection_buf;
file_list_t *menu_stack;
int r;
if (!playlist_init_cached_pending())
return;
/* One slice of the shared per-frame I/O window, the same budget
* the directory walks and the scanner draw from. */
task_nbio_slice_open(&b);
r = playlist_init_cached_continue(menu_playlist_within_budget, &b);
task_nbio_slice_close(&b);
if (r == 0)
return; /* still reading; come back next frame */
if (r > 0)
playlist_init_cached_finish();
/* Ready (or failed): rebuild the list now rather than waiting for
* the user to press something. */
if (!(menu_list = menu_st->entries.list))
return;
selection_buf = MENU_LIST_GET_SELECTION(menu_list, 0);
menu_stack = MENU_LIST_GET(menu_list, 0);
if (selection_buf && menu_stack)
menu_driver_displaylist_push(menu_st, settings,
selection_buf, menu_stack);
menu_st->flags &= ~MENU_ST_FLAG_ENTRIES_NEED_REFRESH;
}
int menu_driver_media_startup_token(void)
{
return retro_atomic_load_acquire_int(&menu_media_startup);
}
void menu_driver_media_startup_ready(int token)
{
if (token & 1)
retro_atomic_cas_int(&menu_media_startup, token, 0);
}
static void menu_driver_media_launch_clear(void)
{
free(menu_media_launch_path);
menu_media_launch_path = NULL;
menu_media_launch_browsing = false;
}
bool menu_driver_media_open(const char *path)
{
settings_t *settings = config_get_ptr();
const menu_ctx_driver_t *driver;
const char *ident;
enum image_type_enum type;
char *copy;
if (!path)
{
menu_driver_media_launch_clear();
menu_media_launch_activate = false;
retro_atomic_store_release_int(&menu_media_startup, 0);
return false;
}
driver = menu_driver_find_driver(settings, "menu", false);
ident = driver ? driver->ident : NULL;
type = image_texture_get_type(path);
if (type == IMAGE_TYPE_NONE || type == IMAGE_TYPE_DDS
|| strstr(path, "://") || path_contains_compressed_file(path)
|| ((type == IMAGE_TYPE_MP4 || type == IMAGE_TYPE_WEBM)
? !settings->bools.multimedia_builtin_mediaplayer_enable
: !settings->bools.multimedia_builtin_imageviewer_enable)
|| (!string_is_equal(ident, "ozone") && !string_is_equal(ident, "xmb")
&& !string_is_equal(ident, "rgui") && !string_is_equal(ident, "glui")))
return false;
copy = (char *)malloc(PATH_MAX_LENGTH);
if (!copy)
return false;
if (strlcpy(copy, path, PATH_MAX_LENGTH) >= PATH_MAX_LENGTH
|| !path_resolve_realpath(copy, PATH_MAX_LENGTH, false))
{
free(copy);
return false;
}
free(menu_media_launch_path);
menu_media_launch_path = copy;
menu_media_launch_browsing = menu_media_launch_activate = false;
menu_media_launch_serial = menu_media_launch_serial >= 0x3fffffffU
? 1 : menu_media_launch_serial + 1;
retro_atomic_store_release_int(&menu_media_startup,
(int)(menu_media_launch_serial << 1));
return true;
}
static enum menu_action menu_driver_media_launch(struct menu_state *menu_st,
enum menu_action action)
{
file_list_t *list;
size_t i;
if (action != MENU_ACTION_NOOP && menu_driver_media_startup_token())
{
menu_driver_media_open(NULL);
return action;
}
if (!menu_media_launch_path || !menu_st->driver_data
|| !menu_st->entries.list || task_content_load_pending())
return action;
if (action != MENU_ACTION_NOOP)
{
menu_driver_media_open(NULL);
return action;
}
if (!menu_media_launch_browsing)
{
char *directory = strdup(menu_media_launch_path);
int result;
if (!directory)
{
menu_driver_media_open(NULL);
return action;
}
path_basedir(directory);
menu_st->flags &= ~(MENU_ST_FLAG_PENDING_QUICK_MENU
| MENU_ST_FLAG_PENDING_STARTUP_PAGE);
result = generic_action_ok_displaylist_push(NULL, directory,
MENU_ENUM_LABEL_FAVORITES_STR, FILE_TYPE_DIRECTORY, 0, 0,
ACTION_OK_DL_CONTENT_LIST);
free(directory);
if (result != 0)
menu_driver_media_open(NULL);
else
menu_media_launch_browsing = true;
return MENU_ACTION_NOOP;
}
if (menu_dirwalk_pending() || MENU_ENTRIES_NEEDS_REFRESH(menu_st))
return action;
if (menu_st->driver_ctx->populate_entries)
{
file_list_t *stack = MENU_LIST_GET(menu_st->entries.list, 0);
if (stack && stack->size)
{
struct item_file *entry = &stack->list[stack->size - 1];
/* The asynchronous refresh may have retained the previous page. */
menu_st->flags &= ~MENU_ST_FLAG_PREVENT_POPULATE;
menu_st->driver_ctx->populate_entries(menu_st->userdata,
entry->path, entry->label, entry->type);
}
}
list = MENU_LIST_GET_SELECTION(menu_st->entries.list, 0);
for (i = 0; list && i < list->size; i++)
if (string_is_equal(list->list[i].path, path_basename(menu_media_launch_path)))
{
menu_st->selection_ptr = i;
if (menu_st->driver_ctx->navigation_set)
menu_st->driver_ctx->navigation_set(menu_st->userdata, true);
menu_driver_media_launch_clear();
menu_media_launch_activate = true;
return MENU_ACTION_OK;
}
menu_driver_media_open(NULL);
return action;
}
bool menu_driver_iterate(
struct menu_state *menu_st,
gfx_display_t *p_disp,
gfx_animation_t *p_anim,
settings_t *settings,
enum menu_action action,
retro_time_t current_time)
{
menu_driver_pump_pending(menu_st, settings);
action = menu_driver_media_launch(menu_st, action);
#ifdef RETRO_ATOMIC_HAS_PTR
if (retro_atomic_load_relaxed_ptr(&menu_drop_pending))
{
struct string_list *files = (struct string_list*)
retro_atomic_exchange_ptr(&menu_drop_pending, NULL);
if (files)
{
menu_entry_drop(menu_st->selection_ptr, files);
string_list_free(files);
}
}
#endif
{
bool result = menu_st->driver_data
&& generic_menu_iterate(menu_st, p_disp, p_anim, settings,
menu_st->driver_data, menu_st->userdata, action,
current_time) != -1;
if (menu_media_launch_activate)
{
int token = menu_driver_media_startup_token();
menu_media_launch_activate = false;
if (token)
retro_atomic_cas_int(&menu_media_startup, token, token | 1);
}
return result;
}
}
bool menu_input_dialog_start_search(void)
{
settings_t *settings = config_get_ptr();
#ifdef HAVE_ACCESSIBILITY
bool accessibility_enable = settings->bools.accessibility_enable;
unsigned accessibility_narrator_speech_speed = settings->uints.accessibility_narrator_speech_speed;
access_state_t *access_st = access_state_get_ptr();
#endif
struct menu_state *menu_st = &menu_driver_state;
menu_handle_t *menu = menu_st->driver_data;
if (!menu || settings->bools.menu_disable_search_button)
return false;
#ifdef HAVE_MIST
steam_open_osk();
#endif
menu_st->flags |= MENU_ST_FLAG_INP_DLG_KB_DISPLAY;
menu_st->input_dialog_kb_text_type = MENU_INPUT_DIALOG_KB_TYPE_TEXT;
strlcpy(menu_st->input_dialog_kb_label,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SEARCH),
sizeof(menu_st->input_dialog_kb_label));
#ifdef HAVE_ACCESSIBILITY
if (is_accessibility_enabled(
accessibility_enable,
access_st->enabled))
accessibility_speak_priority(
accessibility_enable,
accessibility_narrator_speech_speed,
(char*)msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SEARCH), 10);
#endif
menu_st->input_dialog_keyboard_buffer =
input_driver_text_entry_open(menu,
menu_input_search_cb,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SEARCH),
INPUT_TEXT_TYPE_TEXT, NULL);
return true;
}
/* The menu's text types are handed to the frontend as its own; they
* have to stay in the same order. */
enum
{
MENU_TEXT_TYPE_CHECK_TEXT = MENU_INPUT_DIALOG_KB_TYPE_TEXT,
MENU_TEXT_TYPE_CHECK_PASSWORD = MENU_INPUT_DIALOG_KB_TYPE_PASSWORD,
MENU_TEXT_TYPE_CHECK_NUMBER = MENU_INPUT_DIALOG_KB_TYPE_NUMBER,
INPUT_TEXT_TYPE_CHECK_TEXT = INPUT_TEXT_TYPE_TEXT,
INPUT_TEXT_TYPE_CHECK_PASSWORD = INPUT_TEXT_TYPE_PASSWORD,
INPUT_TEXT_TYPE_CHECK_NUMBER = INPUT_TEXT_TYPE_NUMBER
};
typedef char menu_text_type_matches_input[
( MENU_TEXT_TYPE_CHECK_TEXT == INPUT_TEXT_TYPE_CHECK_TEXT
&& MENU_TEXT_TYPE_CHECK_PASSWORD == INPUT_TEXT_TYPE_CHECK_PASSWORD
&& MENU_TEXT_TYPE_CHECK_NUMBER == INPUT_TEXT_TYPE_CHECK_NUMBER)
? 1 : -1];
bool menu_input_dialog_start(menu_input_ctx_line_t *line)
{
#ifdef HAVE_ACCESSIBILITY
settings_t *settings = config_get_ptr();
bool accessibility_enable = settings->bools.accessibility_enable;
unsigned accessibility_narrator_speech_speed = settings->uints.accessibility_narrator_speech_speed;
access_state_t *access_st = access_state_get_ptr();
#endif
struct menu_state *menu_st = &menu_driver_state;
menu_handle_t *menu = menu_st->driver_data;
if (!line || !menu)
return false;
#ifdef HAVE_COCOATOUCH
/* Prevent reopening keyboard if it's already active
* This can happen when return key events trigger menu OK actions */
if (menu_st->flags & MENU_ST_FLAG_INP_DLG_KB_DISPLAY)
return false;
#endif
#ifdef ANDROID
if ( config_get_ptr()->bools.input_android_system_keyboard
&& (menu_st->flags & MENU_ST_FLAG_INP_DLG_KB_DISPLAY))
return false;
#endif
#ifdef HAVE_MIST
steam_open_osk();
#endif
menu_st->flags |= MENU_ST_FLAG_INP_DLG_KB_DISPLAY;
/* Only copy over the menu label and setting if they exist. */
if (line->label)
strlcpy(menu_st->input_dialog_kb_label,
line->label,
sizeof(menu_st->input_dialog_kb_label));
if (line->label_setting)
strlcpy(menu_st->input_dialog_kb_label_setting,
line->label_setting,
sizeof(menu_st->input_dialog_kb_label_setting));
menu_st->input_dialog_kb_idx = line->idx;
menu_st->input_dialog_kb_text_type = line->text_type;
#ifdef HAVE_ACCESSIBILITY
if (is_accessibility_enabled(
accessibility_enable,
access_st->enabled))
accessibility_speak_priority(
accessibility_enable,
accessibility_narrator_speech_speed,
"Keyboard input:", 10);
#endif
menu_st->input_dialog_keyboard_buffer =
input_driver_text_entry_open(menu,
line->cb,
line->label,
(enum input_text_type)line->text_type,
line->dialog_type == MENU_INPUT_DIALOG_TYPE_EDIT
? line->initial_text : NULL);
return true;
}
size_t menu_update_fullscreen_thumbnail_label(
char *s, size_t len,
bool is_quick_menu, const char *title)
{
menu_entry_t selected_entry;
struct menu_state *menu_st = &menu_driver_state;
/* > Get menu entry */
MENU_ENTRY_INITIALIZE(selected_entry);
selected_entry.flags |= MENU_ENTRY_FLAG_PATH_ENABLED
| MENU_ENTRY_FLAG_LABEL_ENABLED
| MENU_ENTRY_FLAG_RICH_LABEL_ENABLED;
menu_entry_get(&selected_entry, 0, menu_st->selection_ptr, NULL, true);
/* > Get entry label */
if (*selected_entry.rich_label)
return strlcpy(s, selected_entry.rich_label, len);
/* > State slot label */
else if ( is_quick_menu
&& (
string_is_equal(selected_entry.label, MENU_ENUM_LABEL_STATE_SLOT_STR)
|| string_is_equal(selected_entry.label, MENU_ENUM_LABEL_LOAD_STATE_STR)
|| string_is_equal(selected_entry.label, MENU_ENUM_LABEL_SAVE_STATE_STR)
)
)
{
int slot = config_get_ptr()->ints.state_slot;
size_t _len = strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_STATE_SLOT),
len);
if (slot < 0)
_len += snprintf(s + _len, len - _len, " %s", "Auto");
else
_len += snprintf(s + _len, len - _len, " %d", slot);
return _len;
}
else if (string_to_unsigned(selected_entry.label) == MENU_ENUM_LABEL_STATE_SLOT)
{
size_t _len = strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_STATE_SLOT),
len);
_len += snprintf(s + _len, len - _len, " %s", selected_entry.path);
return _len;
}
/* > Quick Menu playlist label */
else if (is_quick_menu && title)
{
if (title && *title)
return strlcpy(s, title, len);
}
else
{
if (*selected_entry.path)
return strlcpy(s, selected_entry.path, len);
}
return 0;
}
bool menu_is_running_quick_menu(void)
{
menu_entry_t entry;
MENU_ENTRY_INITIALIZE(entry);
entry.flags |= MENU_ENTRY_FLAG_LABEL_ENABLED
| MENU_ENTRY_FLAG_RICH_LABEL_ENABLED;
menu_entry_get(&entry, 0, 0, NULL, true);
/* memcmp() reads its full count whatever the label's length is, so
* a label shorter than the literal is read past its end. */
return string_is_equal(entry.label, "resume_content")
|| string_is_equal(entry.label, "state_slot");
}
#ifdef HAVE_RUNAHEAD
/**
* menu_update_runahead_mode
*
* Updates the menu runahead mode to match current settings for runahead
* and preemptive frames.
*/
void menu_update_runahead_mode(void)
{
struct menu_state *menu_st = menu_state_get_ptr();
settings_t *settings = config_get_ptr();
if (settings->bools.run_ahead_enabled)
{
#if (defined(HAVE_DYNAMIC) || defined(HAVE_DYLIB))
if (settings->bools.run_ahead_secondary_instance)
menu_st->runahead_mode = MENU_RUNAHEAD_MODE_SECOND_INSTANCE;
else
#endif
menu_st->runahead_mode = MENU_RUNAHEAD_MODE_SINGLE_INSTANCE;
}
else if (settings->bools.preemptive_frames_enable)
menu_st->runahead_mode = MENU_RUNAHEAD_MODE_PREEMPTIVE_FRAMES;
else
menu_st->runahead_mode = MENU_RUNAHEAD_MODE_OFF;
}
#endif
/* Common method for ignoring specifics while picking random playlist items. */
size_t menu_playlist_random_selection(size_t selection, bool is_explore_list)
{
struct menu_state *menu_st = menu_state_get_ptr();
size_t selection_start = 0;
size_t selection_total = menu_st->entries.list ? MENU_LIST_GET_SELECTION(menu_st->entries.list, 0)->size : 0;
size_t new_selection = selection;
/* Skip header items (Search Name + Add Additional Filter + Save as View + Delete this View) */
if (is_explore_list)
{
menu_entry_t entry;
MENU_ENTRY_INITIALIZE(entry);
menu_entry_get(&entry, 0, 0, NULL, true);
if (entry.type == MENU_SETTINGS_LAST + 1 || entry.type == FILE_TYPE_PLAIN)
selection_start = 1;
else if (entry.type == FILE_TYPE_RDB)
selection_start = 2;
}
new_selection = random_range((unsigned)(selection_start), (unsigned)(selection_total - 1));
while (new_selection == selection && selection_start != selection_total - 1)
new_selection = random_range((unsigned)(selection_start), (unsigned)(selection_total - 1));
return new_selection;
}
/* Picks a random playlist on the Playlists tab other than the one at
* @selection, each with equal chance. Returns @selection when there
* is no other playlist to land on, so the caller can tell nothing
* moved. */
size_t menu_playlists_tab_random_selection(size_t selection)
{
struct menu_state *menu_st = menu_state_get_ptr();
file_list_t *selection_buf = MENU_LIST_GET_SELECTION(menu_st->entries.list, 0);
size_t selection_total = selection_buf ? selection_buf->size : 0;
size_t candidates = 0;
size_t pick;
size_t i;
for (i = 0; i < selection_total; i++)
if ( i != selection
&& selection_buf->list[i].type == FILE_TYPE_PLAYLIST_COLLECTION)
candidates++;
if (!candidates)
return selection;
pick = random_range(0, (unsigned)(candidates - 1));
for (i = 0; i < selection_total; i++)
{
if ( i == selection
|| selection_buf->list[i].type != FILE_TYPE_PLAYLIST_COLLECTION)
continue;
if (!pick--)
return i;
}
return selection;
}
/* Modal dialog handling */
/* Wrapped path lines precede the bracketed availability badge. */
const char *menu_dialog_firmware_path_line(const struct menu_state *menu_st,
const char *line, unsigned *state)
{
const char *path = line;
if (menu_st->dialog_st.confirm_msg != MSG_CORE_FIRMWARE_MISSING || !line)
return NULL;
if (!*line)
{
*state = 0;
return NULL;
}
if (*state == 0)
{
while (*path >= '0' && *path <= '9')
path++;
if (path != line && *path == '.' && (path[1] == ' ' || !path[1]))
{
path++;
if (*path == ' ')
path++;
*state = 1;
return *path ? path : NULL;
}
*state = 2;
}
else if (*state == 1)
{
if (*line != '[')
return line;
*state = 2;
}
return NULL;
}
/* A path's underline as last measured: the line it is under and the
* font it was measured in, so the box's frames after the first draw it
* without measuring again. Only the thread that draws the menu's frames
* reads and writes these. */
#define MENU_PATH_UNDERLINES 4
typedef struct
{
font_data_t *font;
size_t len;
float size;
uint32_t font_generation;
int x; /* from the line's start */
int width;
char line[128];
} menu_path_underline_t;
static menu_path_underline_t menu_path_underlines[MENU_PATH_UNDERLINES];
static unsigned menu_path_underline_next;
void menu_dialog_draw_path_underline(const struct menu_state *menu_st,
unsigned *state, const char *line, font_data_t *font,
gfx_display_t *p_disp, void *userdata, unsigned video_dims,
float x, float baseline, uint32_t color)
{
unsigned i;
float rgba[16];
int width;
size_t len;
uint32_t gen;
menu_path_underline_t *u = NULL;
const char *path = menu_dialog_firmware_path_line(menu_st, line, state);
if (!path)
return;
len = strlen(line);
gen = font_driver_get_generation();
for (i = 0; i < MENU_PATH_UNDERLINES; i++)
{
menu_path_underline_t *c = &menu_path_underlines[i];
if ( c->font == font && c->font_generation == gen
&& c->size == font->size
&& c->len == len && !memcmp(c->line, line, len))
{
u = c;
break;
}
}
if (!u)
{
int at;
if ( (width = font_driver_get_message_width(font, path,
strlen(path), 1.0f)) <= 0
|| (at = font_driver_get_message_width(font, line,
path - line, 1.0f)) < 0)
return;
if (len < sizeof(u->line))
{
u = &menu_path_underlines[menu_path_underline_next];
menu_path_underline_next = (menu_path_underline_next + 1)
% MENU_PATH_UNDERLINES;
memcpy(u->line, line, len);
u->font = font;
u->size = font->size;
u->font_generation = gen;
u->len = len;
u->x = at;
u->width = width;
}
x += at;
}
else
{
width = u->width;
x += u->x;
}
for (i = 0; i < 4; i++)
{
rgba[i * 4] = FONT_COLOR_GET_RED(color) / 255.0f;
rgba[i * 4 + 1] = FONT_COLOR_GET_GREEN(color) / 255.0f;
rgba[i * 4 + 2] = FONT_COLOR_GET_BLUE(color) / 255.0f;
rgba[i * 4 + 3] = FONT_COLOR_GET_ALPHA(color) / 255.0f;
}
gfx_display_blend_begin(p_disp->dispctx, userdata);
gfx_display_draw_quad(p_disp, userdata, video_dims, x, baseline + 1,
VIDEO_SCALE_PACK(width, 1), video_dims, rgba, NULL);
gfx_display_blend_end(p_disp->dispctx, userdata);
}
static void menu_dialog_confirm_reset(struct menu_state *menu_st)
{
menu_st->dialog_st.confirm_text = NULL;
menu_st->dialog_st.confirm_msg = MSG_UNKNOWN;
menu_st->dialog_st.confirm_cmd = CMD_EVENT_NONE;
menu_st->dialog_st.confirm_hover_ok = false;
menu_st->dialog_st.confirm_hover_back = false;
menu_st->dialog_st.button_count = 0;
menu_st->dialog_st.button_highlight = -1;
menu_st->dialog_st.button_hover = -1;
BIT64_CLEAR(menu_st->driver_data->state, MENU_STATE_RENDER_MESSAGEBOX);
menu_st->driver_data->menu_state_msg[0] = '\0';
menu_st->driver_data->menu_state_text = NULL;
}
void menu_dialog_confirm_set(struct menu_state *menu_st, unsigned msg, unsigned cmd)
{
/* A Cancel/OK confirm takes over from named buttons; the warning
* waits for it and comes back */
menu_st->dialog_st.button_count = 0;
menu_st->dialog_st.button_highlight = -1;
menu_st->dialog_st.button_hover = -1;
menu_st->dialog_st.confirm_hover_ok = false;
menu_st->dialog_st.confirm_hover_back = false;
menu_st->dialog_st.confirm_text = NULL;
menu_st->dialog_st.button_epoch++;
menu_st->dialog_st.confirm_msg = msg;
menu_st->dialog_st.confirm_cmd = cmd;
menu_st->dialog_st.pending_cmd = CMD_EVENT_NONE;
}
void menu_dialog_confirm_clear(struct menu_state *menu_st)
{
menu_st->dialog_st.pending_cmd = CMD_EVENT_NONE;
menu_dialog_confirm_reset(menu_st);
}
void menu_dialog_confirm(struct menu_state *menu_st)
{
menu_st->dialog_st.pending_cmd = menu_st->dialog_st.confirm_cmd;
menu_dialog_confirm_reset(menu_st);
}
void menu_dialog_get_buttons(struct menu_state *menu_st,
menu_dialog_buttons_t *b)
{
menu_dialog_t *d = &menu_st->dialog_st;
unsigned i;
d->button_drawn = d->button_epoch;
b->count = d->button_count;
b->highlight = -1;
b->hover = -1;
b->classic = false;
if (d->button_count)
{
/* Cancel (the first) always shows its icon, the others OK's,
* dimmed unless highlighted */
for (i = 0; i < b->count; i++)
{
b->label[i] = d->button_label[i];
b->icon[i] = (i == 0) ? MENU_DIALOG_ICON_CANCEL
: ((int)i == d->button_highlight) ? MENU_DIALOG_ICON_OK
: MENU_DIALOG_ICON_OK_DIM;
}
b->highlight = d->button_highlight;
b->hover = d->button_hover;
return;
}
if (!d->confirm_cmd)
return;
b->count = 2;
b->classic = true;
b->label[0] = MENU_ENUM_LABEL_VALUE_BASIC_MENU_CONTROLS_CANCEL;
b->label[1] = MENU_ENUM_LABEL_VALUE_BASIC_MENU_CONTROLS_OK;
b->icon[0] = MENU_DIALOG_ICON_CANCEL;
b->icon[1] = MENU_DIALOG_ICON_OK;
if (d->confirm_hover_back)
b->hover = 0;
else if (d->confirm_hover_ok)
b->hover = 1;
}
void menu_dialog_set_hover(struct menu_state *menu_st, int idx)
{
menu_dialog_t *d = &menu_st->dialog_st;
/* Measured on a box replaced since (threaded video) */
if (d->button_drawn != d->button_epoch)
return;
if (d->button_count)
d->button_hover = (idx >= 0 && idx < (int)d->button_count) ? idx : -1;
else
{
d->confirm_hover_back = (idx == 0);
d->confirm_hover_ok = (idx == 1);
}
}
int menu_dialog_buttons_layout(const int *w, unsigned count, int gap,
int box_x, int box_w, int *x)
{
unsigned i;
int need = 0;
int extra = 0;
int at = box_x;
for (i = 0; i < count; i++)
need += w[i] + (i ? gap : 0);
if (!x)
return need;
if (box_w > need)
{
if (count > 1)
extra = (box_w - need) / (int)(count - 1);
else
at += (box_w - need) / 2;
}
for (i = 0; i < count; i++)
{
x[i] = at;
at += w[i] + gap + extra;
}
return need;
}
bool menu_input_dialog_text_is(const char *text, size_t len,
const char *label, const char *str)
{
size_t llen = strlen(label);
size_t slen;
if (!str || !*str)
str = " ";
slen = strlen(str);
return len == llen + 1 + slen
&& text[llen] == '\n'
&& !memcmp(text, label, llen)
&& !memcmp(text + llen + 1, str, slen);
}
const int *menu_dialog_label_widths(menu_dialog_label_widths_t *c,
const menu_dialog_buttons_t *b, font_data_t *font, float size)
{
unsigned i;
uint32_t gen = font_driver_get_generation();
bool same = c->font == font && c->size == size
&& c->font_generation == gen;
for (i = 0; i < b->count; i++)
{
/* A language's strings are its own: a new one is a new pointer */
const char *str = msg_hash_to_str((enum msg_hash_enums)b->label[i]);
if (!same || i >= c->count || c->str[i] != str)
{
c->str[i] = str;
c->w[i] = font_driver_get_message_width(font, str,
strlen(str), 1.0f);
}
}
c->font = font;
c->size = size;
c->font_generation = gen;
if (!same || c->count < b->count)
c->count = b->count;
return c->w;
}
#ifdef HAVE_CORE_SIGNATURES
/* core_warning_serial() of the request the box went up for, 0 for none */
static unsigned menu_core_warning_shown = 0;
/* Named buttons, the first highlighted */
static void menu_dialog_buttons_set(struct menu_state *menu_st,
const unsigned *labels, unsigned count)
{
menu_dialog_t *d = &menu_st->dialog_st;
unsigned i;
if (count > MENU_DIALOG_BUTTONS_MAX)
count = MENU_DIALOG_BUTTONS_MAX;
for (i = 0; i < count; i++)
d->button_label[i] = labels[i];
d->confirm_msg = MSG_UNKNOWN;
d->confirm_cmd = CMD_EVENT_NONE;
d->confirm_hover_ok = false;
d->confirm_hover_back = false;
d->button_count = (uint8_t)count;
d->button_highlight = 0;
d->button_hover = -1;
d->button_announce = true;
d->button_epoch++;
}
static unsigned menu_core_warning_label(enum core_warning_choice choice)
{
switch (choice)
{
case CORE_WARNING_INSTALL_AND_TRUST:
return MENU_ENUM_LABEL_VALUE_CORE_WARNING_INSTALL_TRUST;
case CORE_WARNING_LOAD_ONCE:
return MENU_ENUM_LABEL_VALUE_CORE_WARNING_LOAD_ONCE;
case CORE_WARNING_ALWAYS_TRUST:
return MENU_ENUM_LABEL_VALUE_CORE_WARNING_ALWAYS_TRUST;
default:
break;
}
return MENU_ENUM_LABEL_VALUE_CORE_WARNING_CANCEL;
}
/* The Security Warning's text, built again only for another reason or
* another language (whose strings are other pointers) */
static const char *menu_core_warning_text(void)
{
static char text[MENU_LABEL_MAX_LENGTH];
static const char *built_title = NULL;
static int built_result = -1;
enum core_check_result result = CORE_CHECK_ERROR;
const char *title = msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_CORE_WARNING);
size_t _len = 0;
core_warning_pending(NULL, &result);
if ((int)result == built_result && title == built_title)
return text;
built_result = (int)result;
built_title = title;
strlcpy_append(text, sizeof(text), &_len, title);
strlcpy_append(text, sizeof(text), &_len, "\n");
strlcpy_append(text, sizeof(text), &_len,
msg_hash_to_str(core_warning_reason(result)));
strlcpy_append(text, sizeof(text), &_len, "\n\n");
strlcpy_append(text, sizeof(text), &_len,
msg_hash_to_str(MSG_CORE_WARNING_FULL_ACCESS));
strlcpy_append(text, sizeof(text), &_len, "\n\n");
strlcpy_append(text, sizeof(text), &_len,
msg_hash_to_str(MSG_CORE_WARNING_TURN_OFF));
return text;
}
static void menu_core_warning_raise(struct menu_state *menu_st)
{
enum core_warning_choice choices[CORE_WARNING_CHOICES_MAX];
unsigned labels[CORE_WARNING_CHOICES_MAX];
unsigned n = core_warning_choices(choices);
unsigned i;
for (i = 0; i < n; i++)
labels[i] = menu_core_warning_label(choices[i]);
menu_dialog_buttons_set(menu_st, labels, n);
menu_core_warning_shown = core_warning_serial();
/* It can come up mid-hold: what is down now waits to be let go */
menu_event_starts_afresh = true;
input_driver_hold_held_input();
}
/* Brings the box in line with the request pending; true when it went up */
static bool menu_core_warning_sync(struct menu_state *menu_st)
{
menu_dialog_t *d = &menu_st->dialog_st;
if (!core_warning_pending(NULL, NULL))
{
/* Answered away from the box, or at deinit */
if (d->button_count)
{
if (menu_st->driver_data)
menu_dialog_confirm_clear(menu_st);
else
d->button_count = 0;
}
menu_core_warning_shown = 0;
return false;
}
/* It waits for what the user is already answering: an open Cancel/OK
* confirm, the command its OK left for this frame, an entry capture
* or the on-screen keyboard. A driver that shows nothing never gets
* the box. */
if ( !menu_st->driver_data
|| !menu_core_warning_can_open()
|| (d->confirm_cmd && !d->button_count)
|| d->pending_cmd != CMD_EVENT_NONE
|| input_entry_capture_running()
|| (menu_st->flags & MENU_ST_FLAG_INP_DLG_KB_DISPLAY))
return false;
menu_core_warning_raise(menu_st);
return true;
}
static void menu_core_warning_press(struct menu_state *menu_st, int idx)
{
static const char *const names[] = {
"Cancel", "Install and Trust", "Load Once", "Always Trust" };
enum core_warning_choice choices[CORE_WARNING_CHOICES_MAX];
unsigned n;
/* The box shows a request a newer push already cancelled: show the
* newer one and choose nothing */
if (menu_core_warning_shown != core_warning_serial())
{
menu_core_warning_sync(menu_st);
return;
}
n = core_warning_choices(choices);
if (idx < 0 || idx >= (int)n)
idx = 0;
RARCH_LOG("[Core] Security Warning answered: %s.\n", names[choices[idx]]);
/* Down first: the callback may raise another box */
menu_dialog_confirm_clear(menu_st);
menu_core_warning_shown = 0;
core_warning_choose(choices[idx]);
}
void menu_core_warning_open(void)
{
/* Before the menu driver exists it waits for the first menu frame */
if ( !(menu_driver_state.flags & MENU_ST_FLAG_ALIVE)
&& menu_driver_state.driver_data)
retroarch_menu_running();
}
bool menu_core_warning_can_open(void)
{
const char *ident = menu_driver_ident();
/* Before the drivers are chosen: the one the config names */
if (!ident)
ident = config_get_ptr()->arrays.menu_driver;
return !string_is_equal(ident, "null");
}
bool menu_core_warning_user_only(void)
{
return core_warning_pending(NULL, NULL)
|| menu_driver_state.dialog_st.confirm_cmd
== CMD_EVENT_CORE_SIGNATURE_CHECK_OFF;
}
bool menu_core_warning_iterate(struct menu_state *menu_st)
{
unsigned serial = core_warning_serial();
/* Up for the request pending, or nothing pending and nothing up */
if ( menu_core_warning_shown == serial
&& (!serial || menu_st->dialog_st.button_count))
return false;
return menu_core_warning_sync(menu_st);
}
#endif
static void menu_dialog_press(struct menu_state *menu_st, int idx)
{
#ifdef HAVE_CORE_SIGNATURES
menu_core_warning_press(menu_st, idx);
#else
(void)idx;
menu_dialog_confirm_clear(menu_st);
#endif
}