2021-04-05 17:58:18 +00:00
/* RetroArch - A frontend for libretro.
* Copyright ( C ) 2010 - 2014 - Hans - Kristian Arntzen
* Copyright ( C ) 2011 - 2017 - Daniel De Matteis
* Copyright ( C ) 2021 - David G . F .
*
* RetroArch is free software : you can redistribute it and / or modify it under the terms
* of the GNU General Public License as published by the Free Software Found -
* ation , either version 3 of the License , or ( at your option ) any later version .
*
* RetroArch is distributed in the hope that it will be useful , but WITHOUT ANY WARRANTY ;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE . See the GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License along with RetroArch .
* If not , see < http : //www.gnu.org/licenses/>.
*/
2021-04-05 17:55:59 +00:00
# include <stdio.h>
# include <stdlib.h>
# include <stddef.h>
# ifdef HAVE_NETWORKING
# include <net/net_compat.h>
# include <net/net_socket.h>
# endif
2021-09-09 14:55:14 +00:00
# include <lists/dir_list.h>
2021-09-30 21:31:54 +00:00
# include <file/file_path.h>
2021-04-05 17:55:59 +00:00
# include <streams/stdin_stream.h>
2021-09-09 14:55:14 +00:00
# include <streams/file_stream.h>
2021-04-05 17:55:59 +00:00
# include <string/stdstring.h>
2021-09-09 14:31:35 +00:00
# ifdef HAVE_CONFIG_H
# include "config.h"
# endif
2021-04-05 17:55:59 +00:00
2021-08-30 14:08:50 +00:00
# ifdef HAVE_CHEEVOS
# include "cheevos/cheevos.h"
# endif
2021-09-09 14:31:35 +00:00
# ifdef HAVE_GFX_WIDGETS
# include "gfx/gfx_widgets.h"
# endif
2021-11-03 23:38:18 +00:00
# ifdef HAVE_MENU
# include "menu/menu_driver.h"
# endif
2021-09-09 14:31:35 +00:00
# ifdef HAVE_NETWORKING
# include "network/netplay/netplay.h"
# endif
2021-10-11 16:01:37 +00:00
# include "audio/audio_driver.h"
2021-10-29 12:13:16 +00:00
# if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_SLANG) || defined(HAVE_HLSL)
# include "gfx/video_shader_parse.h"
# endif
2021-11-03 20:59:58 +00:00
# include "autosave.h"
2021-09-09 14:31:35 +00:00
# include "command.h"
2021-10-13 16:15:35 +00:00
# include "core_info.h"
2021-09-09 14:55:14 +00:00
# include "cheat_manager.h"
2021-09-09 14:31:35 +00:00
# include "content.h"
# include "dynamic.h"
2021-09-09 14:55:14 +00:00
# include "list_special.h"
2021-09-09 14:31:35 +00:00
# include "paths.h"
2021-09-30 21:31:54 +00:00
# include "retroarch.h"
2022-12-29 01:13:31 +00:00
# include "runloop.h"
2021-09-09 14:31:35 +00:00
# include "verbosity.h"
# include "version.h"
# include "version_git.h"
2021-04-05 17:55:59 +00:00
# define CMD_BUF_SIZE 4096
2023-02-24 22:16:30 +00:00
static void command_post_state_loaded ( void )
{
# ifdef HAVE_CHEEVOS
if ( rcheevos_hardcore_active ( ) )
{
rcheevos_pause_hardcore ( ) ;
runloop_msg_queue_push ( msg_hash_to_str ( MSG_CHEEVOS_HARDCORE_MODE_DISABLED ) , 0 , 180 , true , NULL , MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_INFO ) ;
}
# endif
# ifdef HAVE_NETWORKING
netplay_driver_ctl ( RARCH_NETPLAY_CTL_LOAD_SAVESTATE , NULL ) ;
# endif
{
settings_t * settings = config_get_ptr ( ) ;
video_driver_state_t * video_st =
video_state_get_ptr ( ) ;
bool frame_time_counter_reset_after_load_state =
settings - > bools . frame_time_counter_reset_after_load_state ;
if ( frame_time_counter_reset_after_load_state )
video_st - > frame_time_count = 0 ;
}
}
2021-04-05 17:55:59 +00:00
# if defined(HAVE_COMMAND)
/* Generic command parse utilities */
static bool command_get_arg ( const char * tok ,
const char * * arg , unsigned * index )
{
unsigned i ;
for ( i = 0 ; i < ARRAY_SIZE ( map ) ; i + + )
{
if ( string_is_equal ( tok , map [ i ] . str ) )
{
if ( arg )
* arg = NULL ;
if ( index )
* index = i ;
return true ;
}
}
for ( i = 0 ; i < ARRAY_SIZE ( action_map ) ; i + + )
{
const char * str = strstr ( tok , action_map [ i ] . str ) ;
if ( str = = tok )
{
const char * argument = str + strlen ( action_map [ i ] . str ) ;
2022-03-07 18:02:46 +00:00
if ( ! argument )
return false ;
2021-04-05 17:55:59 +00:00
if ( * argument ! = ' ' & & * argument ! = ' \0 ' )
return false ;
if ( arg )
* arg = argument + 1 ;
if ( index )
* index = i ;
return true ;
}
}
return false ;
}
static void command_parse_sub_msg ( command_t * handle , const char * tok )
{
const char * arg = NULL ;
unsigned index = 0 ;
if ( command_get_arg ( tok , & arg , & index ) )
{
if ( arg )
{
if ( ! action_map [ index ] . action ( handle , arg ) )
RARCH_ERR ( " Command \" %s \" failed. \n " , arg ) ;
}
else
handle - > state [ map [ index ] . id ] = true ;
}
else
2021-09-16 17:00:14 +00:00
RARCH_WARN ( msg_hash_to_str ( MSG_UNRECOGNIZED_COMMAND ) , tok ) ;
2021-04-05 17:55:59 +00:00
}
static void command_parse_msg (
command_t * handle , char * buf )
{
2021-04-05 18:02:19 +00:00
char * save = NULL ;
2021-04-05 17:55:59 +00:00
const char * tok = strtok_r ( buf , " \n " , & save ) ;
while ( tok )
{
command_parse_sub_msg ( handle , tok ) ;
tok = strtok_r ( NULL , " \n " , & save ) ;
}
}
# if defined(HAVE_NETWORK_CMD)
typedef struct
{
/* Network socket FD */
int net_fd ;
/* Source address for the command received */
struct sockaddr_storage cmd_source ;
/* Size of the previous structure in use */
socklen_t cmd_source_len ;
} command_network_t ;
static void network_command_reply (
command_t * cmd ,
const char * data , size_t len )
{
command_network_t * netcmd = ( command_network_t * ) cmd - > userptr ;
/* Respond (fire and forget since it's UDP) */
sendto ( netcmd - > net_fd , data , len , 0 ,
( struct sockaddr * ) & netcmd - > cmd_source , netcmd - > cmd_source_len ) ;
}
static void network_command_free ( command_t * handle )
{
command_network_t * netcmd = ( command_network_t * ) handle - > userptr ;
if ( netcmd - > net_fd > = 0 )
socket_close ( netcmd - > net_fd ) ;
free ( netcmd ) ;
free ( handle ) ;
}
static void command_network_poll ( command_t * handle )
{
2022-06-12 05:13:34 +00:00
ssize_t ret ;
char buf [ 2048 ] ;
command_network_t * netcmd = ( command_network_t * ) handle - > userptr ;
2021-04-05 17:55:59 +00:00
if ( netcmd - > net_fd < 0 )
return ;
for ( ; ; )
{
2022-06-12 05:13:34 +00:00
netcmd - > cmd_source_len = sizeof ( netcmd - > cmd_source ) ;
2021-04-05 17:55:59 +00:00
2022-07-22 20:23:54 +00:00
if ( ( ret = recvfrom ( netcmd - > net_fd , buf , sizeof ( buf ) - 1 , 0 ,
( struct sockaddr * ) & netcmd - > cmd_source ,
& netcmd - > cmd_source_len ) ) < = 0 )
2022-06-12 05:13:34 +00:00
return ;
2021-04-05 17:55:59 +00:00
buf [ ret ] = ' \0 ' ;
command_parse_msg ( handle , buf ) ;
}
}
command_t * command_network_new ( uint16_t port )
{
2021-04-05 18:02:19 +00:00
struct addrinfo * res = NULL ;
command_t * cmd = ( command_t * ) calloc ( 1 , sizeof ( command_t ) ) ;
2021-04-05 17:55:59 +00:00
command_network_t * netcmd = ( command_network_t * ) calloc (
1 , sizeof ( command_network_t ) ) ;
2021-04-05 18:02:19 +00:00
int fd = socket_init (
2022-07-31 09:22:28 +00:00
( void * * ) & res , port , NULL , SOCKET_TYPE_DATAGRAM , AF_INET ) ;
2021-04-05 17:55:59 +00:00
2021-12-17 17:04:24 +00:00
RARCH_LOG ( " [NetCMD]: %s %hu. \n " ,
2021-04-05 17:55:59 +00:00
msg_hash_to_str ( MSG_BRINGING_UP_COMMAND_INTERFACE_ON_PORT ) ,
( unsigned short ) port ) ;
if ( fd < 0 )
goto error ;
netcmd - > net_fd = fd ;
2021-04-05 18:02:19 +00:00
cmd - > userptr = netcmd ;
cmd - > poll = command_network_poll ;
cmd - > replier = network_command_reply ;
cmd - > destroy = network_command_free ;
2021-04-05 17:55:59 +00:00
if ( ! socket_nonblock ( netcmd - > net_fd ) )
goto error ;
if ( ! socket_bind ( netcmd - > net_fd , ( void * ) res ) )
{
2021-12-17 17:04:24 +00:00
RARCH_ERR ( " [NetCMD]: %s. \n " ,
2021-04-05 17:55:59 +00:00
msg_hash_to_str ( MSG_FAILED_TO_BIND_SOCKET ) ) ;
goto error ;
}
freeaddrinfo_retro ( res ) ;
return cmd ;
error :
if ( res )
freeaddrinfo_retro ( res ) ;
free ( netcmd ) ;
free ( cmd ) ;
return NULL ;
}
# endif
# if defined(HAVE_STDIN_CMD)
typedef struct
{
/* Buffer and pointer for stdin reads */
size_t stdin_buf_ptr ;
char stdin_buf [ CMD_BUF_SIZE ] ;
} command_stdin_t ;
static void stdin_command_reply (
command_t * cmd ,
const char * data , size_t len )
{
/* Just write to stdout! */
fwrite ( data , 1 , len , stdout ) ;
2023-03-24 21:41:07 +00:00
fflush ( stdout ) ;
2021-04-05 17:55:59 +00:00
}
static void stdin_command_free ( command_t * handle )
{
free ( handle - > userptr ) ;
free ( handle ) ;
}
2021-04-05 18:02:19 +00:00
static void command_stdin_poll ( command_t * handle )
{
2021-04-05 17:55:59 +00:00
ptrdiff_t msg_len ;
2021-04-05 18:02:19 +00:00
char * last_newline = NULL ;
2021-04-05 17:55:59 +00:00
command_stdin_t * stdincmd = ( command_stdin_t * ) handle - > userptr ;
2021-04-05 18:02:19 +00:00
ssize_t ret = read_stdin (
2021-04-05 17:55:59 +00:00
stdincmd - > stdin_buf + stdincmd - > stdin_buf_ptr ,
CMD_BUF_SIZE - stdincmd - > stdin_buf_ptr - 1 ) ;
if ( ret = = 0 )
return ;
stdincmd - > stdin_buf_ptr + = ret ;
stdincmd - > stdin_buf [ stdincmd - > stdin_buf_ptr ] = ' \0 ' ;
last_newline = strrchr ( stdincmd - > stdin_buf , ' \n ' ) ;
if ( ! last_newline )
{
/* We're receiving bogus data in pipe
* ( no terminating newline ) , flush out the buffer . */
if ( stdincmd - > stdin_buf_ptr + 1 > = CMD_BUF_SIZE )
{
stdincmd - > stdin_buf_ptr = 0 ;
stdincmd - > stdin_buf [ 0 ] = ' \0 ' ;
}
return ;
}
* last_newline + + = ' \0 ' ;
msg_len = last_newline - stdincmd - > stdin_buf ;
command_parse_msg ( handle , stdincmd - > stdin_buf ) ;
memmove ( stdincmd - > stdin_buf , last_newline ,
stdincmd - > stdin_buf_ptr - msg_len ) ;
stdincmd - > stdin_buf_ptr - = msg_len ;
}
command_t * command_stdin_new ( void )
{
command_t * cmd ;
command_stdin_t * stdincmd ;
# ifndef _WIN32
# ifdef HAVE_NETWORKING
if ( ! socket_nonblock ( STDIN_FILENO ) )
return NULL ;
# endif
# endif
2021-04-05 18:02:19 +00:00
cmd = ( command_t * ) calloc ( 1 , sizeof ( command_t ) ) ;
stdincmd = ( command_stdin_t * ) calloc ( 1 , sizeof ( command_stdin_t ) ) ;
2022-03-05 13:39:56 +00:00
if ( ! cmd )
return NULL ;
if ( ! stdincmd )
{
free ( cmd ) ;
return NULL ;
}
2021-04-05 17:55:59 +00:00
cmd - > userptr = stdincmd ;
2021-04-05 18:02:19 +00:00
cmd - > poll = command_stdin_poll ;
2021-04-05 17:55:59 +00:00
cmd - > replier = stdin_command_reply ;
cmd - > destroy = stdin_command_free ;
return cmd ;
}
# endif
2021-11-23 16:14:38 +00:00
bool command_get_config_param ( command_t * cmd , const char * arg )
{
2022-09-01 02:17:07 +00:00
size_t _len ;
char reply [ 8192 ] ;
2023-03-24 21:41:07 +00:00
# ifdef HAVE_BSV_MOVIE
char value_dynamic [ 256 ] ;
# endif
const char * value = " unsupported " ;
settings_t * settings = config_get_ptr ( ) ;
bool video_fullscreen = settings - > bools . video_fullscreen ;
const char * dir_runtime_log = settings - > paths . directory_runtime_log ;
const char * log_dir = settings - > paths . log_dir ;
const char * directory_cache = settings - > paths . directory_cache ;
const char * directory_system = settings - > paths . directory_system ;
const char * path_username = settings - > paths . username ;
input_driver_state_t * input_st = input_state_get_ptr ( ) ;
2021-11-23 16:14:38 +00:00
if ( string_is_equal ( arg , " video_fullscreen " ) )
{
if ( video_fullscreen )
value = " true " ;
else
value = " false " ;
}
else if ( string_is_equal ( arg , " savefile_directory " ) )
value = dir_get_ptr ( RARCH_DIR_SAVEFILE ) ;
else if ( string_is_equal ( arg , " savestate_directory " ) )
value = dir_get_ptr ( RARCH_DIR_SAVESTATE ) ;
else if ( string_is_equal ( arg , " runtime_log_directory " ) )
value = dir_runtime_log ;
else if ( string_is_equal ( arg , " log_dir " ) )
value = log_dir ;
else if ( string_is_equal ( arg , " cache_directory " ) )
value = directory_cache ;
else if ( string_is_equal ( arg , " system_directory " ) )
value = directory_system ;
else if ( string_is_equal ( arg , " netplay_nickname " ) )
value = path_username ;
2023-03-24 21:41:07 +00:00
# ifdef HAVE_BSV_MOVIE
else if ( string_is_equal ( arg , " active_replay " ) ) {
value = value_dynamic ;
value_dynamic [ 0 ] = ' \0 ' ;
if ( input_st - > bsv_movie_state_handle )
snprintf ( value_dynamic , sizeof ( value_dynamic ) , " %lld %u " , ( long long ) ( input_st - > bsv_movie_state_handle - > identifier ) , input_st - > bsv_movie_state . flags ) ;
else
snprintf ( value_dynamic , sizeof ( value_dynamic ) , " 0 0 " ) ;
}
# endif
2021-11-23 16:14:38 +00:00
/* TODO: query any string */
2022-09-01 02:17:07 +00:00
strlcpy ( reply , " GET_CONFIG_PARAM " , sizeof ( reply ) ) ;
_len = strlcat ( reply , arg , sizeof ( reply ) ) ;
reply [ _len ] = ' ' ;
reply [ _len + 1 ] = ' \0 ' ;
strlcat ( reply , value , sizeof ( reply ) ) ;
2021-11-23 16:14:38 +00:00
cmd - > replier ( cmd , reply , strlen ( reply ) ) ;
return true ;
}
2021-04-05 17:55:59 +00:00
# if defined(HAVE_LAKKA)
# include <sys/un.h>
# define MAX_USER_CONNECTIONS 4
typedef struct
{
/* File descriptor for the domain socket */
int sfd ;
/* Client sockets */
int userfd [ MAX_USER_CONNECTIONS ] ;
/* Last received user socket */
int last_fd ;
} command_uds_t ;
static void uds_command_reply (
command_t * cmd ,
const char * data , size_t len )
{
command_uds_t * subcmd = ( command_uds_t * ) cmd - > userptr ;
write ( subcmd - > last_fd , data , len ) ;
}
static void uds_command_free ( command_t * handle )
{
int i ;
command_uds_t * udscmd = ( command_uds_t * ) handle - > userptr ;
for ( i = 0 ; i < MAX_USER_CONNECTIONS ; i + + )
if ( udscmd - > userfd [ i ] > = 0 )
socket_close ( udscmd - > userfd [ i ] ) ;
socket_close ( udscmd - > sfd ) ;
free ( handle - > userptr ) ;
free ( handle ) ;
}
2021-04-05 18:02:19 +00:00
static void command_uds_poll ( command_t * handle )
{
2021-04-05 17:55:59 +00:00
int i ;
2022-06-12 05:13:34 +00:00
int fd ;
ssize_t ret ;
char buf [ 2048 ] ;
command_uds_t * udscmd = ( command_uds_t * ) handle - > userptr ;
2021-04-05 17:55:59 +00:00
if ( udscmd - > sfd < 0 )
return ;
2022-06-12 05:13:34 +00:00
/* Read data from clients and process commands */
2021-04-05 17:55:59 +00:00
for ( i = 0 ; i < MAX_USER_CONNECTIONS ; i + + )
{
2022-06-12 05:13:34 +00:00
bool err = false ;
fd = udscmd - > userfd [ i ] ;
if ( fd < 0 )
continue ;
ret = socket_receive_all_nonblocking ( fd , & err , buf , sizeof ( buf ) - 1 ) ;
if ( ! ret )
continue ;
if ( ! err )
2021-04-05 17:55:59 +00:00
{
2022-06-12 05:13:34 +00:00
buf [ ret ] = ' \0 ' ;
udscmd - > last_fd = fd ;
command_parse_msg ( handle , buf ) ;
}
else
{
socket_close ( fd ) ;
udscmd - > userfd [ i ] = - 1 ;
2021-04-05 17:55:59 +00:00
}
}
2022-06-12 05:13:34 +00:00
/* Accepts new connections from clients */
fd = accept ( udscmd - > sfd , NULL , NULL ) ;
if ( fd > = 0 )
2021-04-05 17:55:59 +00:00
{
2022-06-12 05:13:34 +00:00
if ( socket_nonblock ( fd ) )
2021-04-05 17:55:59 +00:00
{
2022-06-12 05:13:34 +00:00
for ( i = 0 ; i < MAX_USER_CONNECTIONS ; i + + )
2021-04-05 17:55:59 +00:00
{
2022-06-12 05:13:34 +00:00
if ( udscmd - > userfd [ i ] < 0 )
2021-04-05 17:55:59 +00:00
{
2022-06-12 05:13:34 +00:00
udscmd - > userfd [ i ] = fd ;
return ;
2021-04-05 17:55:59 +00:00
}
}
}
2022-06-12 05:13:34 +00:00
socket_close ( fd ) ;
2021-04-05 17:55:59 +00:00
}
}
command_t * command_uds_new ( void )
{
int i ;
command_t * cmd ;
command_uds_t * subcmd ;
struct sockaddr_un addr ;
2021-04-05 18:02:19 +00:00
const char * sp = " retroarch/cmd " ;
2021-04-05 17:55:59 +00:00
socklen_t addrsz = offsetof ( struct sockaddr_un , sun_path ) + strlen ( sp ) + 1 ;
2021-04-05 18:02:19 +00:00
int fd = socket ( AF_UNIX , SOCK_STREAM , 0 ) ;
2021-04-05 17:55:59 +00:00
if ( fd < 0 )
return NULL ;
/* use an abstract socket for simplicity */
memset ( & addr , 0 , sizeof ( addr ) ) ;
addr . sun_family = AF_UNIX ;
strcpy ( & addr . sun_path [ 1 ] , sp ) ;
if ( bind ( fd , ( struct sockaddr * ) & addr , addrsz ) < 0 | |
2021-04-05 18:02:19 +00:00
listen ( fd , MAX_USER_CONNECTIONS ) < 0 )
{
2021-04-05 17:55:59 +00:00
socket_close ( fd ) ;
return NULL ;
}
if ( ! socket_nonblock ( fd ) )
{
socket_close ( fd ) ;
return NULL ;
}
2021-04-05 18:02:19 +00:00
cmd = ( command_t * ) calloc ( 1 , sizeof ( command_t ) ) ;
subcmd = ( command_uds_t * ) calloc ( 1 , sizeof ( command_uds_t ) ) ;
subcmd - > sfd = fd ;
2021-04-05 17:55:59 +00:00
subcmd - > last_fd = - 1 ;
for ( i = 0 ; i < MAX_USER_CONNECTIONS ; i + + )
subcmd - > userfd [ i ] = - 1 ;
cmd - > userptr = subcmd ;
2021-04-05 18:02:19 +00:00
cmd - > poll = command_uds_poll ;
2021-04-05 17:55:59 +00:00
cmd - > replier = uds_command_reply ;
cmd - > destroy = uds_command_free ;
return cmd ;
}
# endif
/* Routines used to invoke retroarch command ... */
# ifdef HAVE_NETWORK_CMD
static bool command_verify ( const char * cmd )
{
unsigned i ;
if ( command_get_arg ( cmd , NULL , NULL ) )
return true ;
2021-12-17 17:04:24 +00:00
RARCH_ERR ( " [NetCMD]: Command \" %s \" is not recognized by the program. \n " , cmd ) ;
RARCH_ERR ( " [NetCMD]: \t Valid commands: \n " ) ;
2021-04-05 17:55:59 +00:00
for ( i = 0 ; i < ARRAY_SIZE ( map ) ; i + + )
RARCH_ERR ( " \t \t %s \n " , map [ i ] . str ) ;
for ( i = 0 ; i < ARRAY_SIZE ( action_map ) ; i + + )
RARCH_ERR ( " \t \t %s %s \n " , action_map [ i ] . str , action_map [ i ] . arg_desc ) ;
return false ;
}
2022-08-04 11:22:49 +00:00
static bool udp_send_packet ( const char * host , uint16_t port , const char * msg )
{
char port_buf [ 6 ] ;
const struct addrinfo * tmp_info ;
struct addrinfo * addr = NULL ;
struct addrinfo hints = { 0 } ;
size_t len = strlen ( msg ) ;
bool ret = false ;
snprintf ( port_buf , sizeof ( port_buf ) , " %hu " , ( unsigned short ) port ) ;
hints . ai_socktype = SOCK_DGRAM ;
hints . ai_flags = AI_NUMERICSERV ;
if ( getaddrinfo_retro ( host , port_buf , & hints , & addr ) )
return false ;
if ( ! addr )
return false ;
/* Send to all possible targets. */
tmp_info = addr ;
do
{
int fd = socket ( tmp_info - > ai_family ,
tmp_info - > ai_socktype , tmp_info - > ai_protocol ) ;
if ( fd < 0 )
continue ;
if ( sendto ( fd , msg , len , 0 , tmp_info - > ai_addr , tmp_info - > ai_addrlen ) = =
( ssize_t ) len )
ret = true ;
socket_close ( fd ) ;
} while ( ( tmp_info = tmp_info - > ai_next ) ) ;
freeaddrinfo_retro ( addr ) ;
return ret ;
}
2021-04-05 17:55:59 +00:00
bool command_network_send ( const char * cmd_ )
{
char * command = NULL ;
char * save = NULL ;
const char * cmd = NULL ;
if ( ! network_init ( ) )
return false ;
if ( ! ( command = strdup ( cmd_ ) ) )
return false ;
cmd = strtok_r ( command , " ; " , & save ) ;
if ( cmd )
{
uint16_t port = DEFAULT_NETWORK_CMD_PORT ;
const char * port_ = NULL ;
const char * host = strtok_r ( NULL , " ; " , & save ) ;
if ( host )
port_ = strtok_r ( NULL , " ; " , & save ) ;
else
{
# ifdef _WIN32
host = " 127.0.0.1 " ;
# else
host = " localhost " ;
# endif
}
if ( port_ )
port = strtoul ( port_ , NULL , 0 ) ;
2021-12-17 17:04:24 +00:00
RARCH_LOG ( " [NetCMD]: %s: \" %s \" to %s:%hu \n " ,
2021-04-05 17:55:59 +00:00
msg_hash_to_str ( MSG_SENDING_COMMAND ) ,
cmd , host , ( unsigned short ) port ) ;
if ( command_verify ( cmd ) & & udp_send_packet ( host , port , cmd ) )
{
free ( command ) ;
return true ;
}
}
free ( command ) ;
return false ;
}
# endif
2021-08-30 14:08:50 +00:00
bool command_show_osd_msg ( command_t * cmd , const char * arg )
{
runloop_msg_queue_push ( arg , 1 , 180 , false , NULL ,
MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_INFO ) ;
return true ;
}
2023-02-24 22:08:44 +00:00
2023-02-24 22:04:39 +00:00
bool command_load_state_slot ( command_t * cmd , const char * arg )
{
2023-02-24 22:08:44 +00:00
char state_path [ 16384 ] ;
2023-02-24 22:04:39 +00:00
retro_ctx_size_info_t info ;
char reply [ 128 ] = " " ;
unsigned int slot = ( unsigned int ) strtoul ( arg , NULL , 10 ) ;
bool savestates_enabled = core_info_current_supports_savestate ( ) ;
bool ret = false ;
state_path [ 0 ] = ' \0 ' ;
2023-03-24 21:41:07 +00:00
snprintf ( reply , sizeof ( reply ) - 1 , " LOAD_STATE_SLOT %d " , slot ) ;
2023-02-24 22:04:39 +00:00
if ( savestates_enabled )
{
2023-03-07 23:10:59 +00:00
runloop_get_savestate_path ( state_path , sizeof ( state_path ) , slot ) ;
2023-02-24 22:04:39 +00:00
core_serialize_size ( & info ) ;
savestates_enabled = ( info . size > 0 ) ;
}
if ( savestates_enabled )
{
2023-02-24 22:08:44 +00:00
if ( ( ret = content_load_state ( state_path , false , false ) ) )
2023-02-24 22:04:39 +00:00
command_post_state_loaded ( ) ;
}
else
ret = false ;
cmd - > replier ( cmd , reply , strlen ( reply ) ) ;
return ret ;
}
2023-03-07 23:10:59 +00:00
bool command_play_replay_slot ( command_t * cmd , const char * arg )
{
# ifdef HAVE_BSV_MOVIE
char replay_path [ 16384 ] ;
retro_ctx_size_info_t info ;
char reply [ 128 ] = " " ;
unsigned int slot = ( unsigned int ) strtoul ( arg , NULL , 10 ) ;
bool savestates_enabled = core_info_current_supports_savestate ( ) ;
bool ret = false ;
replay_path [ 0 ] = ' \0 ' ;
if ( savestates_enabled )
{
runloop_get_replay_path ( replay_path , sizeof ( replay_path ) , slot ) ;
core_serialize_size ( & info ) ;
savestates_enabled = ( info . size > 0 ) ;
}
if ( savestates_enabled )
{
ret = movie_start_playback ( input_state_get_ptr ( ) , replay_path ) ;
2023-03-24 21:41:07 +00:00
if ( ret ) {
input_driver_state_t * input_st = input_state_get_ptr ( ) ;
task_queue_wait ( NULL , NULL ) ;
if ( input_st - > bsv_movie_state_handle )
snprintf ( reply , sizeof ( reply ) - 1 , " PLAY_REPLAY_SLOT %lld " , ( long long ) ( input_st - > bsv_movie_state_handle - > identifier ) ) ;
else
snprintf ( reply , sizeof ( reply ) - 1 , " PLAY_REPLAY_SLOT 0 " ) ;
2023-03-07 23:10:59 +00:00
command_post_state_loaded ( ) ;
2023-03-24 21:41:07 +00:00
}
2023-03-07 23:10:59 +00:00
}
else
ret = false ;
cmd - > replier ( cmd , reply , strlen ( reply ) ) ;
return ret ;
# else
return false ;
# endif
}
2023-02-24 22:04:39 +00:00
2021-08-30 14:08:50 +00:00
# if defined(HAVE_CHEEVOS)
bool command_read_ram ( command_t * cmd , const char * arg )
{
unsigned i ;
char * reply = NULL ;
const uint8_t * data = NULL ;
char * reply_at = NULL ;
unsigned int nbytes = 0 ;
unsigned int alloc_size = 0 ;
unsigned int addr = - 1 ;
2022-10-02 01:22:34 +00:00
size_t len = 0 ;
2021-08-30 14:08:50 +00:00
if ( sscanf ( arg , " %x %u " , & addr , & nbytes ) ! = 2 )
return true ;
/* We allocate more than needed, saving 20 bytes is not really relevant */
alloc_size = 40 + nbytes * 3 ;
reply = ( char * ) malloc ( alloc_size ) ;
reply [ 0 ] = ' \0 ' ;
reply_at = reply + snprintf (
reply , alloc_size - 1 , " READ_CORE_RAM " " %x " , addr ) ;
if ( ( data = rcheevos_patch_address ( addr ) ) )
{
for ( i = 0 ; i < nbytes ; i + + )
snprintf ( reply_at + 3 * i , 4 , " %.2X " , data [ i ] ) ;
reply_at [ 3 * nbytes ] = ' \n ' ;
len = reply_at + 3 * nbytes + 1 - reply ;
}
else
{
strlcpy ( reply_at , " -1 \n " , sizeof ( reply ) - strlen ( reply ) ) ;
len = reply_at + STRLEN_CONST ( " -1 \n " ) - reply ;
}
cmd - > replier ( cmd , reply , len ) ;
free ( reply ) ;
return true ;
}
bool command_write_ram ( command_t * cmd , const char * arg )
{
unsigned int addr = ( unsigned int ) strtoul ( arg , ( char * * ) & arg , 16 ) ;
uint8_t * data = ( uint8_t * ) rcheevos_patch_address ( addr ) ;
if ( ! data )
return false ;
if ( rcheevos_hardcore_active ( ) )
{
2021-12-17 17:04:24 +00:00
RARCH_LOG ( " [Command]: Achievements hardcore mode disabled by WRITE_CORE_RAM. \n " ) ;
2021-08-30 14:08:50 +00:00
rcheevos_pause_hardcore ( ) ;
}
while ( * arg )
{
* data = strtoul ( arg , ( char * * ) & arg , 16 ) ;
data + + ;
}
return true ;
}
# endif
2021-09-09 14:31:35 +00:00
bool command_version ( command_t * cmd , const char * arg )
{
2022-09-01 02:17:07 +00:00
char reply [ 256 ] ;
size_t _len = strlcpy ( reply , PACKAGE_VERSION , sizeof ( reply ) ) ;
reply [ _len ] = ' \n ' ;
reply [ _len + 1 ] = ' \0 ' ;
2021-09-09 14:31:35 +00:00
cmd - > replier ( cmd , reply , strlen ( reply ) ) ;
return true ;
}
2022-02-25 17:23:38 +00:00
static const rarch_memory_descriptor_t * command_memory_get_descriptor ( const rarch_memory_map_t * mmap , unsigned address , size_t * offset )
2021-09-09 14:31:35 +00:00
{
const rarch_memory_descriptor_t * desc = mmap - > descriptors ;
const rarch_memory_descriptor_t * end = desc + mmap - > num_descriptors ;
for ( ; desc < end ; desc + + )
{
if ( desc - > core . select = = 0 )
{
/* if select is 0, attempt to explicitly match the address */
if ( address > = desc - > core . start & & address < desc - > core . start + desc - > core . len )
2022-02-25 17:23:38 +00:00
{
* offset = address - desc - > core . start ;
2021-09-09 14:31:35 +00:00
return desc ;
2022-02-25 17:23:38 +00:00
}
2021-09-09 14:31:35 +00:00
}
else
{
/* otherwise, attempt to match the address by matching the select bits */
if ( ( ( desc - > core . start ^ address ) & desc - > core . select ) = = 0 )
{
2022-02-25 17:23:38 +00:00
/* adjust the address to the start of the descriptor */
unsigned desc_offset = address - ( unsigned ) desc - > core . start ;
/* address is unsigned. we only need that much of the disconnect mask */
unsigned mask = ( unsigned ) desc - > core . disconnect ;
/* this magic logic is copied from mmap_reduce. it removes any bits from
* address that are non - zero in the disconnect field . bits above the
* removed bits are shifted down to fill the gap . */
while ( mask )
{
const unsigned tmp = ( mask - 1 ) & ~ mask ;
desc_offset = ( desc_offset & tmp ) | ( ( desc_offset > > 1 ) & ~ tmp ) ;
2023-02-24 22:08:44 +00:00
mask = ( mask & ( mask - 1 ) ) > > 1 ;
2022-02-25 17:23:38 +00:00
}
/* we've calculated the actual offset of the data within the descriptor */
* offset = desc_offset ;
2021-09-09 14:31:35 +00:00
/* sanity check - make sure the descriptor is large enough to hold the target address */
2022-02-25 17:23:38 +00:00
if ( desc_offset < desc - > core . len )
2021-09-09 14:31:35 +00:00
return desc ;
}
}
}
return NULL ;
}
uint8_t * command_memory_get_pointer (
const rarch_system_info_t * system ,
unsigned address ,
unsigned int * max_bytes ,
int for_write ,
char * reply_at ,
size_t len )
{
if ( ! system | | system - > mmaps . num_descriptors = = 0 )
strlcpy ( reply_at , " -1 no memory map defined \n " , len ) ;
else
{
2022-02-25 17:23:38 +00:00
size_t offset ;
const rarch_memory_descriptor_t * desc = command_memory_get_descriptor ( & system - > mmaps , address , & offset ) ;
2021-09-09 14:31:35 +00:00
if ( ! desc )
strlcpy ( reply_at , " -1 no descriptor for address \n " , len ) ;
else if ( ! desc - > core . ptr )
strlcpy ( reply_at , " -1 no data for descriptor \n " , len ) ;
else if ( for_write & & ( desc - > core . flags & RETRO_MEMDESC_CONST ) )
strlcpy ( reply_at , " -1 descriptor data is readonly \n " , len ) ;
else
{
2022-10-02 01:22:34 +00:00
* max_bytes = ( unsigned int ) ( desc - > core . len - offset ) ;
2021-09-09 14:31:35 +00:00
return ( uint8_t * ) desc - > core . ptr + desc - > core . offset + offset ;
}
}
* max_bytes = 0 ;
return NULL ;
}
2021-10-13 16:15:35 +00:00
bool command_get_status ( command_t * cmd , const char * arg )
{
2022-10-09 18:32:39 +00:00
char reply [ 4096 ] ;
uint8_t flags = content_get_flags ( ) ;
2021-10-13 16:15:35 +00:00
2022-10-09 18:32:39 +00:00
if ( flags & CONTENT_ST_FLAG_IS_INITED )
2021-10-13 16:15:35 +00:00
{
2022-10-09 18:32:39 +00:00
/* add some content info */
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
const char * status = " PLAYING " ;
const char * content_name = path_basename ( path_get ( RARCH_PATH_BASENAME ) ) ; /* filename only without ext */
int content_crc32 = content_get_crc ( ) ;
const char * system_id = NULL ;
core_info_t * core_info = NULL ;
reply [ 0 ] = ' \0 ' ;
core_info_get_current_core ( & core_info ) ;
2022-10-10 17:54:54 +00:00
if ( runloop_st - > flags & RUNLOOP_FLAG_PAUSED )
2022-10-09 18:32:39 +00:00
status = " PAUSED " ;
if ( core_info )
system_id = core_info - > system_id ;
if ( ! system_id )
system_id = runloop_st - > system . info . library_name ;
snprintf ( reply , sizeof ( reply ) , " GET_STATUS %s %s,%s,crc32=%x \n " ,
status , system_id , content_name , content_crc32 ) ;
2021-10-13 16:15:35 +00:00
}
2022-10-09 18:32:39 +00:00
else
strlcpy ( reply , " GET_STATUS CONTENTLESS " , sizeof ( reply ) ) ;
2021-10-13 16:15:35 +00:00
cmd - > replier ( cmd , reply , strlen ( reply ) ) ;
return true ;
}
bool command_read_memory ( command_t * cmd , const char * arg )
{
unsigned i ;
char * reply = NULL ;
char * reply_at = NULL ;
const uint8_t * data = NULL ;
unsigned int nbytes = 0 ;
unsigned int alloc_size = 0 ;
unsigned int address = - 1 ;
size_t len = 0 ;
unsigned int max_bytes = 0 ;
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
const rarch_system_info_t * system = & runloop_st - > system ;
if ( sscanf ( arg , " %x %u " , & address , & nbytes ) ! = 2 )
return false ;
/* Ensure large enough to return all requested bytes or an error message */
alloc_size = 64 + nbytes * 3 ;
reply = ( char * ) malloc ( alloc_size ) ;
reply_at = reply + snprintf ( reply , alloc_size - 1 , " READ_CORE_MEMORY %x " , address ) ;
2023-02-24 22:08:44 +00:00
if ( ( data = command_memory_get_pointer (
system , address , & max_bytes ,
0 , reply_at , alloc_size - strlen ( reply ) ) ) )
2021-10-13 16:15:35 +00:00
{
if ( nbytes > max_bytes )
nbytes = max_bytes ;
for ( i = 0 ; i < nbytes ; i + + )
snprintf ( reply_at + 3 * i , 4 , " %02X " , data [ i ] ) ;
reply_at [ 3 * nbytes ] = ' \n ' ;
len = reply_at + 3 * nbytes + 1 - reply ;
}
else
len = strlen ( reply ) ;
cmd - > replier ( cmd , reply , len ) ;
free ( reply ) ;
return true ;
}
bool command_write_memory ( command_t * cmd , const char * arg )
{
unsigned int address = ( unsigned int ) strtoul ( arg , ( char * * ) & arg , 16 ) ;
unsigned int max_bytes = 0 ;
char reply [ 128 ] = " " ;
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
const rarch_system_info_t
* system = & runloop_st - > system ;
char * reply_at = reply + snprintf ( reply , sizeof ( reply ) - 1 , " WRITE_CORE_MEMORY %x " , address ) ;
uint8_t * data = command_memory_get_pointer ( system , address , & max_bytes , 1 , reply_at , sizeof ( reply ) - strlen ( reply ) - 1 ) ;
if ( data )
{
uint8_t * start = data ;
while ( * arg & & max_bytes > 0 )
{
- - max_bytes ;
* data = strtoul ( arg , ( char * * ) & arg , 16 ) ;
data + + ;
}
snprintf ( reply_at , sizeof ( reply ) - strlen ( reply ) - 1 ,
" %u \n " , ( unsigned ) ( data - start ) ) ;
# ifdef HAVE_CHEEVOS
if ( rcheevos_hardcore_active ( ) )
{
2021-12-17 17:04:24 +00:00
RARCH_LOG ( " [Command]: Achievements hardcore mode disabled by WRITE_CORE_MEMORY. \n " ) ;
2021-10-13 16:15:35 +00:00
rcheevos_pause_hardcore ( ) ;
}
# endif
}
cmd - > replier ( cmd , reply , strlen ( reply ) ) ;
return true ;
}
2021-09-09 14:31:35 +00:00
# endif
void command_event_set_volume (
settings_t * settings ,
float gain ,
bool widgets_active ,
bool audio_driver_mute_enable )
{
2022-09-01 02:17:07 +00:00
size_t _len ;
2021-09-09 14:31:35 +00:00
char msg [ 128 ] ;
2022-09-01 02:17:07 +00:00
float new_volume = settings - > floats . audio_volume + gain ;
new_volume = MAX ( new_volume , - 80.0f ) ;
new_volume = MIN ( new_volume , 12.0f ) ;
2021-09-09 14:31:35 +00:00
configuration_set_float ( settings , settings - > floats . audio_volume , new_volume ) ;
2022-09-01 02:17:07 +00:00
_len = strlcpy ( msg , msg_hash_to_str ( MSG_AUDIO_VOLUME ) ,
sizeof ( msg ) ) ;
msg [ _len ] = ' : ' ;
msg [ + + _len ] = ' ' ;
msg [ + + _len ] = ' \0 ' ;
_len + = snprintf ( msg + _len , sizeof ( msg ) - _len , " %.1f " ,
2021-09-09 14:31:35 +00:00
new_volume ) ;
2022-09-01 02:17:07 +00:00
msg [ _len ] = ' ' ;
msg [ + + _len ] = ' d ' ;
msg [ + + _len ] = ' B ' ;
msg [ + + _len ] = ' \0 ' ;
2021-09-09 14:31:35 +00:00
# if defined(HAVE_GFX_WIDGETS)
if ( widgets_active )
gfx_widget_volume_update_and_show ( new_volume ,
audio_driver_mute_enable ) ;
else
# endif
runloop_msg_queue_push ( msg , 1 , 180 , true , NULL ,
MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_INFO ) ;
RARCH_LOG ( " [Audio]: %s \n " , msg ) ;
audio_set_float ( AUDIO_ACTION_VOLUME_GAIN , new_volume ) ;
}
/**
* event_set_mixer_volume :
* @ gain : amount of gain to be applied to current volume level .
*
* Adjusts the current audio volume level .
*
* */
void command_event_set_mixer_volume (
settings_t * settings ,
float gain )
{
2022-09-01 02:17:07 +00:00
size_t _len ;
2021-09-09 14:31:35 +00:00
char msg [ 128 ] ;
2022-09-01 02:17:07 +00:00
float new_volume = settings - > floats . audio_mixer_volume + gain ;
new_volume = MAX ( new_volume , - 80.0f ) ;
new_volume = MIN ( new_volume , 12.0f ) ;
2021-09-09 14:31:35 +00:00
configuration_set_float ( settings , settings - > floats . audio_mixer_volume , new_volume ) ;
2022-09-01 02:17:07 +00:00
_len = strlcpy ( msg , msg_hash_to_str ( MSG_AUDIO_VOLUME ) ,
sizeof ( msg ) ) ;
msg [ _len ] = ' : ' ;
msg [ + + _len ] = ' ' ;
msg [ + + _len ] = ' \0 ' ;
_len + = snprintf ( msg + _len , sizeof ( msg ) - _len , " %.1f " ,
2021-09-09 14:31:35 +00:00
new_volume ) ;
2022-09-01 02:17:07 +00:00
msg [ _len ] = ' ' ;
msg [ + + _len ] = ' d ' ;
msg [ + + _len ] = ' B ' ;
msg [ + + _len ] = ' \0 ' ;
2021-09-09 14:31:35 +00:00
runloop_msg_queue_push ( msg , 1 , 180 , true , NULL , MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_INFO ) ;
RARCH_LOG ( " [Audio]: %s \n " , msg ) ;
audio_set_float ( AUDIO_ACTION_VOLUME_GAIN , new_volume ) ;
}
void command_event_init_controllers ( rarch_system_info_t * info ,
settings_t * settings , unsigned num_active_users )
{
unsigned port ;
unsigned num_core_ports = info - > ports . size ;
for ( port = 0 ; port < num_core_ports ; port + + )
{
unsigned i ;
retro_ctx_controller_info_t pad ;
unsigned device = RETRO_DEVICE_NONE ;
const struct retro_controller_description * desc = NULL ;
/* Check whether current core port is mapped
* to an input device
* > If is not , leave ' device ' set to
* ' RETRO_DEVICE_NONE '
* > For example : if input ports 0 and 1 are
* mapped to core port 0 , core port 1 will
* be unmapped and should be disabled */
for ( i = 0 ; i < num_active_users ; i + + )
{
if ( i > = MAX_USERS )
break ;
if ( port = = settings - > uints . input_remap_ports [ i ] )
{
device = input_config_get_device ( port ) ;
break ;
}
}
desc = libretro_find_controller_description (
& info - > ports . data [ port ] , device ) ;
if ( desc & & ! desc - > desc )
{
/* If we're trying to connect a completely unknown device,
* revert back to JOYPAD . */
if ( device ! = RETRO_DEVICE_JOYPAD & & device ! = RETRO_DEVICE_NONE )
{
/* Do not fix device,
* because any use of dummy core will reset this ,
* which is not a good idea . */
RARCH_WARN ( " [Input]: Input device ID %u is unknown to this "
" libretro implementation. Using RETRO_DEVICE_JOYPAD. \n " ,
device ) ;
device = RETRO_DEVICE_JOYPAD ;
}
}
pad . device = device ;
pad . port = port ;
core_set_controller_port_device ( & pad ) ;
}
}
# ifdef HAVE_CONFIGFILE
bool command_event_save_config (
const char * config_path ,
char * s , size_t len )
{
bool path_exists = ! string_is_empty ( config_path ) ;
const char * str = path_exists ? config_path :
path_get ( RARCH_PATH_CONFIG ) ;
if ( path_exists & & config_save_file ( config_path ) )
{
snprintf ( s , len , " %s \" %s \" . " ,
msg_hash_to_str ( MSG_SAVED_NEW_CONFIG_TO ) ,
config_path ) ;
RARCH_LOG ( " [Config]: %s \n " , s ) ;
return true ;
}
if ( ! string_is_empty ( str ) )
{
snprintf ( s , len , " %s \" %s \" . " ,
msg_hash_to_str ( MSG_FAILED_SAVING_CONFIG_TO ) ,
str ) ;
RARCH_ERR ( " [Config]: %s \n " , s ) ;
}
return false ;
}
# endif
void command_event_undo_save_state ( char * s , size_t len )
{
if ( content_undo_save_buf_is_empty ( ) )
{
strlcpy ( s ,
msg_hash_to_str ( MSG_NO_SAVE_STATE_HAS_BEEN_OVERWRITTEN_YET ) , len ) ;
return ;
}
if ( ! content_undo_save_state ( ) )
{
strlcpy ( s ,
msg_hash_to_str ( MSG_FAILED_TO_UNDO_SAVE_STATE ) , len ) ;
return ;
}
strlcpy ( s ,
msg_hash_to_str ( MSG_UNDOING_SAVE_STATE ) , len ) ;
}
void command_event_undo_load_state ( char * s , size_t len )
{
if ( content_undo_load_buf_is_empty ( ) )
{
strlcpy ( s ,
msg_hash_to_str ( MSG_NO_STATE_HAS_BEEN_LOADED_YET ) ,
len ) ;
return ;
}
if ( ! content_undo_load_state ( ) )
{
snprintf ( s , len , " %s \" %s \" . " ,
msg_hash_to_str ( MSG_FAILED_TO_UNDO_LOAD_STATE ) ,
" RAM " ) ;
return ;
}
# ifdef HAVE_NETWORKING
netplay_driver_ctl ( RARCH_NETPLAY_CTL_LOAD_SAVESTATE , NULL ) ;
# endif
strlcpy ( s ,
msg_hash_to_str ( MSG_UNDID_LOAD_STATE ) , len ) ;
}
bool command_event_resize_windowed_scale ( settings_t * settings ,
unsigned window_scale )
{
unsigned idx = 0 ;
bool video_fullscreen = settings - > bools . video_fullscreen ;
if ( window_scale = = 0 )
return false ;
2022-09-05 02:03:04 +00:00
configuration_set_uint ( settings , settings - > uints . video_scale , window_scale ) ;
2021-09-09 14:31:35 +00:00
if ( ! video_fullscreen )
command_event ( CMD_EVENT_REINIT , NULL ) ;
2021-09-28 10:56:10 +00:00
retroarch_ctl ( RARCH_CTL_SET_WINDOWED_SCALE , & idx ) ;
2021-09-09 14:31:35 +00:00
return true ;
}
bool command_event_save_auto_state (
bool savestate_auto_save ,
const enum rarch_core_type current_core_type )
{
2021-11-09 05:03:00 +00:00
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
2021-09-09 14:31:35 +00:00
char savestate_name_auto [ PATH_MAX_LENGTH ] ;
2021-12-14 13:08:06 +00:00
if ( runloop_st - > entry_state_slot )
return false ;
2021-11-09 05:03:00 +00:00
if ( ! savestate_auto_save )
2021-09-09 14:31:35 +00:00
return false ;
if ( current_core_type = = CORE_TYPE_DUMMY )
return false ;
2022-02-02 15:49:53 +00:00
if ( ! core_info_current_supports_savestate ( ) )
return false ;
2021-09-09 14:31:35 +00:00
if ( string_is_empty ( path_basename ( path_get ( RARCH_PATH_BASENAME ) ) ) )
return false ;
2022-07-11 16:48:02 +00:00
strlcpy ( savestate_name_auto ,
2021-11-09 05:03:00 +00:00
runloop_st - > name . savestate ,
2022-07-11 16:48:02 +00:00
sizeof ( savestate_name_auto ) ) ;
strlcat ( savestate_name_auto ,
" .auto " ,
sizeof ( savestate_name_auto ) ) ;
if ( content_save_state ( ( const char * ) savestate_name_auto , true , true ) )
RARCH_LOG ( " %s \" %s \" %s. \n " ,
msg_hash_to_str ( MSG_AUTO_SAVE_STATE_TO ) ,
savestate_name_auto , " succeeded " ) ;
else
RARCH_LOG ( " %s \" %s \" %s. \n " ,
msg_hash_to_str ( MSG_AUTO_SAVE_STATE_TO ) ,
savestate_name_auto , " failed " ) ;
2021-09-09 14:31:35 +00:00
return true ;
}
2021-09-09 14:55:14 +00:00
# ifdef HAVE_CHEATS
void command_event_init_cheats (
bool apply_cheats_after_load ,
const char * path_cheat_db ,
2021-09-09 14:58:52 +00:00
void * bsv_movie_data )
2021-09-09 14:55:14 +00:00
{
# ifdef HAVE_NETWORKING
bool allow_cheats = ! netplay_driver_ctl (
RARCH_NETPLAY_CTL_IS_DATA_INITED , NULL ) ;
# else
bool allow_cheats = true ;
# endif
# ifdef HAVE_BSV_MOVIE
2023-01-25 06:15:32 +00:00
allow_cheats & = ! ( bsv_movie_data ! = NULL ) ;
2021-09-09 14:55:14 +00:00
# endif
if ( ! allow_cheats )
return ;
cheat_manager_alloc_if_empty ( ) ;
cheat_manager_load_game_specific_cheats ( path_cheat_db ) ;
if ( apply_cheats_after_load )
cheat_manager_apply_cheats ( ) ;
}
# endif
2022-12-30 13:38:21 +00:00
bool command_event_load_entry_state ( settings_t * settings )
2021-12-14 13:08:06 +00:00
{
char entry_state_path [ PATH_MAX_LENGTH ] ;
int entry_path_stats ;
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
bool ret = false ;
2022-02-02 15:49:53 +00:00
if ( ! core_info_current_supports_savestate ( ) )
return false ;
2021-12-14 13:08:06 +00:00
# ifdef HAVE_CHEEVOS
if ( rcheevos_hardcore_active ( ) )
return false ;
# endif
# ifdef HAVE_NETWORKING
if ( netplay_driver_ctl ( RARCH_NETPLAY_CTL_IS_ENABLED , NULL ) )
return false ;
# endif
entry_state_path [ 0 ] = ' \0 ' ;
2022-12-29 01:13:31 +00:00
if ( ! runloop_get_entry_state_path (
2022-07-22 20:23:54 +00:00
entry_state_path , sizeof ( entry_state_path ) ,
runloop_st - > entry_state_slot ) )
2021-12-14 13:08:06 +00:00
return false ;
entry_path_stats = path_stat ( entry_state_path ) ;
if ( ( entry_path_stats & RETRO_VFS_STAT_IS_VALID ) = = 0
| | ( entry_path_stats & RETRO_VFS_STAT_IS_DIRECTORY ) ! = 0 )
return false ;
ret = content_load_state ( entry_state_path , false , true ) ;
2021-12-17 17:04:24 +00:00
RARCH_LOG ( " [State]: %s \" %s \" . \n " ,
2021-12-14 13:08:06 +00:00
msg_hash_to_str ( MSG_FOUND_ENTRY_STATE_IN ) ,
2021-12-17 17:04:24 +00:00
entry_state_path ) ;
RARCH_LOG ( " [State]: %s \" %s \" %s. \n " ,
2021-12-14 13:08:06 +00:00
msg_hash_to_str ( MSG_LOADING_ENTRY_STATE_FROM ) ,
entry_state_path , ret ? " succeeded " : " failed "
) ;
2022-12-30 13:38:21 +00:00
if ( ret )
2023-03-07 23:10:59 +00:00
configuration_set_int ( settings , settings - > ints . state_slot , runloop_st - > entry_state_slot ) ;
2022-12-30 13:38:21 +00:00
2021-12-14 13:08:06 +00:00
return ret ;
}
2021-11-09 05:03:00 +00:00
void command_event_load_auto_state ( void )
2021-09-09 14:55:14 +00:00
{
char savestate_name_auto [ PATH_MAX_LENGTH ] ;
2021-11-09 05:03:00 +00:00
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
2022-02-02 15:49:53 +00:00
if ( ! core_info_current_supports_savestate ( ) )
return ;
2021-09-09 14:55:14 +00:00
# ifdef HAVE_CHEEVOS
if ( rcheevos_hardcore_active ( ) )
return ;
# endif
# ifdef HAVE_NETWORKING
if ( netplay_driver_ctl ( RARCH_NETPLAY_CTL_IS_ENABLED , NULL ) )
return ;
# endif
2022-07-11 16:48:02 +00:00
strlcpy ( savestate_name_auto ,
runloop_st - > name . savestate ,
sizeof ( savestate_name_auto ) ) ;
strlcat ( savestate_name_auto ,
" .auto " ,
sizeof ( savestate_name_auto ) ) ;
2021-09-09 14:55:14 +00:00
if ( ! path_is_valid ( savestate_name_auto ) )
return ;
2021-12-17 17:04:24 +00:00
RARCH_LOG ( " [State]: %s \" %s \" . \n " ,
2021-09-09 14:55:14 +00:00
msg_hash_to_str ( MSG_FOUND_AUTO_SAVESTATE_IN ) ,
2021-12-17 17:04:24 +00:00
savestate_name_auto ) ;
2022-07-11 16:48:02 +00:00
if ( ( content_load_state ( savestate_name_auto , false , true ) ) )
RARCH_LOG ( " [State]: %s \" %s \" %s. \n " ,
msg_hash_to_str ( MSG_AUTOLOADING_SAVESTATE_FROM ) ,
savestate_name_auto , " succeeded " ) ;
else
RARCH_LOG ( " [State]: %s \" %s \" %s. \n " ,
msg_hash_to_str ( MSG_AUTOLOADING_SAVESTATE_FROM ) ,
savestate_name_auto , " failed " ) ;
2021-09-09 14:55:14 +00:00
}
2021-11-09 05:03:00 +00:00
void command_event_set_savestate_auto_index ( settings_t * settings )
2021-09-09 14:55:14 +00:00
{
size_t i ;
2022-06-18 17:07:15 +00:00
char state_base [ 128 ] ;
2021-09-09 14:55:14 +00:00
char state_dir [ PATH_MAX_LENGTH ] ;
struct string_list * dir_list = NULL ;
unsigned max_idx = 0 ;
2021-11-09 05:03:00 +00:00
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
2021-09-09 14:55:14 +00:00
bool savestate_auto_index = settings - > bools . savestate_auto_index ;
bool show_hidden_files = settings - > bools . show_hidden_files ;
2021-11-09 05:03:00 +00:00
if ( ! savestate_auto_index )
2021-09-09 14:55:14 +00:00
return ;
2021-11-09 05:03:00 +00:00
/* Find the file in the same directory as runloop_st->savestate_name
2021-09-09 14:55:14 +00:00
* with the largest numeral suffix .
*
* E . g . / foo / path / content . state , will try to find
* / foo / path / content . state % d , where % d is the largest number available .
*/
2021-11-09 05:03:00 +00:00
fill_pathname_basedir ( state_dir , runloop_st - > name . savestate ,
2021-09-09 14:55:14 +00:00
sizeof ( state_dir ) ) ;
dir_list = dir_list_new_special ( state_dir , DIR_LIST_PLAIN , NULL ,
show_hidden_files ) ;
if ( ! dir_list )
return ;
2021-11-09 05:03:00 +00:00
fill_pathname_base ( state_base , runloop_st - > name . savestate ,
2021-09-09 14:55:14 +00:00
sizeof ( state_base ) ) ;
for ( i = 0 ; i < dir_list - > size ; i + + )
{
unsigned idx ;
char elem_base [ 128 ] = { 0 } ;
const char * end = NULL ;
const char * dir_elem = dir_list - > elems [ i ] . data ;
fill_pathname_base ( elem_base , dir_elem , sizeof ( elem_base ) ) ;
if ( strstr ( elem_base , state_base ) ! = elem_base )
continue ;
end = dir_elem + strlen ( dir_elem ) ;
while ( ( end > dir_elem ) & & ISDIGIT ( ( int ) end [ - 1 ] ) )
end - - ;
idx = ( unsigned ) strtoul ( end , NULL , 0 ) ;
if ( idx > max_idx )
max_idx = idx ;
}
dir_list_free ( dir_list ) ;
configuration_set_int ( settings , settings - > ints . state_slot , max_idx ) ;
2021-12-17 17:04:24 +00:00
RARCH_LOG ( " [State]: %s: #%d \n " ,
2021-09-09 14:55:14 +00:00
msg_hash_to_str ( MSG_FOUND_LAST_STATE_SLOT ) ,
max_idx ) ;
}
void command_event_set_savestate_garbage_collect (
unsigned max_to_keep ,
bool show_hidden_files
)
{
size_t i , cnt = 0 ;
char state_dir [ PATH_MAX_LENGTH ] ;
2022-06-18 17:07:15 +00:00
char state_base [ 128 ] ;
2021-11-09 05:03:00 +00:00
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
2021-09-09 14:55:14 +00:00
struct string_list * dir_list = NULL ;
unsigned min_idx = UINT_MAX ;
const char * oldest_save = NULL ;
/* Similar to command_event_set_savestate_auto_index(),
* this will find the lowest numbered save - state */
2021-11-09 05:03:00 +00:00
fill_pathname_basedir ( state_dir , runloop_st - > name . savestate ,
2021-09-09 14:55:14 +00:00
sizeof ( state_dir ) ) ;
dir_list = dir_list_new_special ( state_dir , DIR_LIST_PLAIN , NULL ,
show_hidden_files ) ;
if ( ! dir_list )
return ;
2021-11-09 05:03:00 +00:00
fill_pathname_base ( state_base , runloop_st - > name . savestate ,
2021-09-09 14:55:14 +00:00
sizeof ( state_base ) ) ;
for ( i = 0 ; i < dir_list - > size ; i + + )
{
unsigned idx ;
char elem_base [ 128 ] ;
const char * ext = NULL ;
const char * end = NULL ;
const char * dir_elem = dir_list - > elems [ i ] . data ;
if ( string_is_empty ( dir_elem ) )
continue ;
fill_pathname_base ( elem_base , dir_elem , sizeof ( elem_base ) ) ;
/* Only consider files with a '.state' extension
* > i . e . Ignore ' . state . auto ' , ' . state . bak ' , etc . */
ext = path_get_extension ( elem_base ) ;
if ( string_is_empty ( ext ) | |
! string_starts_with_size ( ext , " state " , STRLEN_CONST ( " state " ) ) )
continue ;
/* Check whether this file is associated with
* the current content */
if ( ! string_starts_with ( elem_base , state_base ) )
continue ;
/* This looks like a valid save */
cnt + + ;
/* > Get index */
end = dir_elem + strlen ( dir_elem ) ;
while ( ( end > dir_elem ) & & ISDIGIT ( ( int ) end [ - 1 ] ) )
end - - ;
idx = string_to_unsigned ( end ) ;
/* > Check if this is the lowest index so far */
if ( idx < min_idx )
{
min_idx = idx ;
oldest_save = dir_elem ;
}
}
/* Only delete one save state per save action
* > Conservative behaviour , designed to minimise
* the risk of deleting multiple incorrect files
* in case of accident */
if ( ! string_is_empty ( oldest_save ) & & ( cnt > max_to_keep ) )
filestream_delete ( oldest_save ) ;
dir_list_free ( dir_list ) ;
}
2021-10-29 12:13:16 +00:00
Replay UI support (#15048)
* Add bsv replay controls (not yet fully implemented), remove toggle
see notes in task_movie.c, make sure command.c calls the right
functions, check retroarch.c and other todos.
bsv files are also now stored with states, not saves.
* Compilation fixes
* Added command impls for play and record replay, and some code in load state to do the right thing there
* Guard some parts of the new code with HAVE_BSV_MOVIE
* wip, menu fixes
* more menu fixes, osd for movie errors, halt recording properly
* Menu and label fixes
* move bsvs to own file suffix series under savestates, fix recording and playback command validity checks
* Fix replay autoincrement
* fix endif placement, whoops
---------
Co-authored-by: Joseph C. Osborn <jcoa2018@pomona.edu>
2023-03-02 23:52:22 +00:00
void command_event_set_replay_auto_index ( settings_t * settings )
{
size_t i ;
char state_base [ 128 ] ;
char state_dir [ PATH_MAX_LENGTH ] ;
struct string_list * dir_list = NULL ;
unsigned max_idx = 0 ;
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
bool replay_auto_index = settings - > bools . replay_auto_index ;
bool show_hidden_files = settings - > bools . show_hidden_files ;
if ( ! replay_auto_index )
return ;
/* Find the file in the same directory as runloop_st->names.replay
* with the largest numeral suffix .
*
2023-03-03 22:16:48 +00:00
* E . g . / foo / path / content . replay will try to find
* / foo / path / content . replay % d , where % d is the largest number available .
Replay UI support (#15048)
* Add bsv replay controls (not yet fully implemented), remove toggle
see notes in task_movie.c, make sure command.c calls the right
functions, check retroarch.c and other todos.
bsv files are also now stored with states, not saves.
* Compilation fixes
* Added command impls for play and record replay, and some code in load state to do the right thing there
* Guard some parts of the new code with HAVE_BSV_MOVIE
* wip, menu fixes
* more menu fixes, osd for movie errors, halt recording properly
* Menu and label fixes
* move bsvs to own file suffix series under savestates, fix recording and playback command validity checks
* Fix replay autoincrement
* fix endif placement, whoops
---------
Co-authored-by: Joseph C. Osborn <jcoa2018@pomona.edu>
2023-03-02 23:52:22 +00:00
*/
fill_pathname_basedir ( state_dir , runloop_st - > name . replay ,
sizeof ( state_dir ) ) ;
dir_list = dir_list_new_special ( state_dir , DIR_LIST_PLAIN , NULL ,
show_hidden_files ) ;
if ( ! dir_list )
return ;
fill_pathname_base ( state_base , runloop_st - > name . replay ,
sizeof ( state_base ) ) ;
for ( i = 0 ; i < dir_list - > size ; i + + )
{
unsigned idx ;
char elem_base [ 128 ] = { 0 } ;
const char * end = NULL ;
const char * dir_elem = dir_list - > elems [ i ] . data ;
fill_pathname_base ( elem_base , dir_elem , sizeof ( elem_base ) ) ;
if ( strstr ( elem_base , state_base ) ! = elem_base )
continue ;
end = dir_elem + strlen ( dir_elem ) ;
while ( ( end > dir_elem ) & & ISDIGIT ( ( int ) end [ - 1 ] ) )
end - - ;
idx = ( unsigned ) strtoul ( end , NULL , 0 ) ;
if ( idx > max_idx )
max_idx = idx ;
}
dir_list_free ( dir_list ) ;
configuration_set_int ( settings , settings - > ints . replay_slot , max_idx ) ;
RARCH_LOG ( " [Replay]: %s: #%d \n " ,
msg_hash_to_str ( MSG_FOUND_LAST_REPLAY_SLOT ) ,
max_idx ) ;
}
void command_event_set_replay_garbage_collect (
unsigned max_to_keep ,
bool show_hidden_files
)
{
/* TODO: debugme */
size_t i , cnt = 0 ;
char state_dir [ PATH_MAX_LENGTH ] ;
char state_base [ 128 ] ;
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
struct string_list * dir_list = NULL ;
unsigned min_idx = UINT_MAX ;
const char * oldest_save = NULL ;
/* Similar to command_event_set_replay_auto_index(),
* this will find the lowest numbered replay */
fill_pathname_basedir ( state_dir , runloop_st - > name . replay ,
sizeof ( state_dir ) ) ;
dir_list = dir_list_new_special ( state_dir , DIR_LIST_PLAIN , NULL ,
show_hidden_files ) ;
if ( ! dir_list )
return ;
fill_pathname_base ( state_base , runloop_st - > name . replay ,
sizeof ( state_base ) ) ;
for ( i = 0 ; i < dir_list - > size ; i + + )
{
unsigned idx ;
char elem_base [ 128 ] ;
const char * ext = NULL ;
const char * end = NULL ;
const char * dir_elem = dir_list - > elems [ i ] . data ;
if ( string_is_empty ( dir_elem ) )
continue ;
fill_pathname_base ( elem_base , dir_elem , sizeof ( elem_base ) ) ;
2023-03-03 22:16:48 +00:00
/* Only consider files with a '.replayXX' extension
* > i . e . Ignore ' . replay . auto ' , ' . replay . bak ' , etc . */
Replay UI support (#15048)
* Add bsv replay controls (not yet fully implemented), remove toggle
see notes in task_movie.c, make sure command.c calls the right
functions, check retroarch.c and other todos.
bsv files are also now stored with states, not saves.
* Compilation fixes
* Added command impls for play and record replay, and some code in load state to do the right thing there
* Guard some parts of the new code with HAVE_BSV_MOVIE
* wip, menu fixes
* more menu fixes, osd for movie errors, halt recording properly
* Menu and label fixes
* move bsvs to own file suffix series under savestates, fix recording and playback command validity checks
* Fix replay autoincrement
* fix endif placement, whoops
---------
Co-authored-by: Joseph C. Osborn <jcoa2018@pomona.edu>
2023-03-02 23:52:22 +00:00
ext = path_get_extension ( elem_base ) ;
if ( string_is_empty ( ext ) | |
2023-03-03 22:16:48 +00:00
! string_starts_with_size ( ext , " replay " , STRLEN_CONST ( " REPLAY " ) ) )
Replay UI support (#15048)
* Add bsv replay controls (not yet fully implemented), remove toggle
see notes in task_movie.c, make sure command.c calls the right
functions, check retroarch.c and other todos.
bsv files are also now stored with states, not saves.
* Compilation fixes
* Added command impls for play and record replay, and some code in load state to do the right thing there
* Guard some parts of the new code with HAVE_BSV_MOVIE
* wip, menu fixes
* more menu fixes, osd for movie errors, halt recording properly
* Menu and label fixes
* move bsvs to own file suffix series under savestates, fix recording and playback command validity checks
* Fix replay autoincrement
* fix endif placement, whoops
---------
Co-authored-by: Joseph C. Osborn <jcoa2018@pomona.edu>
2023-03-02 23:52:22 +00:00
continue ;
/* Check whether this file is associated with
* the current content */
if ( ! string_starts_with ( elem_base , state_base ) )
continue ;
/* This looks like a valid save */
cnt + + ;
/* > Get index */
end = dir_elem + strlen ( dir_elem ) ;
while ( ( end > dir_elem ) & & ISDIGIT ( ( int ) end [ - 1 ] ) )
end - - ;
idx = string_to_unsigned ( end ) ;
/* > Check if this is the lowest index so far */
if ( idx < min_idx )
{
min_idx = idx ;
oldest_save = dir_elem ;
}
}
/* Only delete one save state per save action
* > Conservative behaviour , designed to minimise
* the risk of deleting multiple incorrect files
* in case of accident */
if ( ! string_is_empty ( oldest_save ) & & ( cnt > max_to_keep ) )
filestream_delete ( oldest_save ) ;
dir_list_free ( dir_list ) ;
}
2021-10-29 12:13:16 +00:00
# if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_SLANG) || defined(HAVE_HLSL)
bool command_set_shader ( command_t * cmd , const char * arg )
{
enum rarch_shader_type type = video_shader_parse_type ( arg ) ;
settings_t * settings = config_get_ptr ( ) ;
if ( ! string_is_empty ( arg ) )
{
if ( ! video_shader_is_supported ( type ) )
return false ;
/* rebase on shader directory */
if ( ! path_is_absolute ( arg ) )
{
2022-06-18 17:07:15 +00:00
char abs_arg [ PATH_MAX_LENGTH ] ;
2021-10-29 12:13:16 +00:00
const char * ref_path = settings - > paths . directory_video_shader ;
2022-08-04 12:18:11 +00:00
fill_pathname_join_special ( abs_arg , ref_path , arg , sizeof ( abs_arg ) ) ;
2022-12-22 20:36:32 +00:00
return video_shader_apply_shader ( settings , type , abs_arg , true ) ;
2021-10-29 12:13:16 +00:00
}
}
2022-12-22 20:36:32 +00:00
return video_shader_apply_shader ( settings , type , arg , true ) ;
2021-10-29 12:13:16 +00:00
}
# endif
2021-11-03 20:59:58 +00:00
# ifdef HAVE_CONFIGFILE
bool command_event_save_core_config (
const char * dir_menu_config ,
const char * rarch_path_config )
{
char msg [ 128 ] ;
2022-06-18 17:07:15 +00:00
char config_name [ 255 ] ;
2021-11-03 20:59:58 +00:00
char config_path [ PATH_MAX_LENGTH ] ;
char config_dir [ PATH_MAX_LENGTH ] ;
2022-06-18 17:07:15 +00:00
bool new_path_available = false ;
2021-11-03 20:59:58 +00:00
bool overrides_active = false ;
const char * core_path = NULL ;
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
msg [ 0 ] = ' \0 ' ;
if ( ! string_is_empty ( dir_menu_config ) )
strlcpy ( config_dir , dir_menu_config , sizeof ( config_dir ) ) ;
else if ( ! string_is_empty ( rarch_path_config ) ) /* Fallback */
fill_pathname_basedir ( config_dir , rarch_path_config ,
sizeof ( config_dir ) ) ;
if ( string_is_empty ( config_dir ) )
{
runloop_msg_queue_push ( msg_hash_to_str ( MSG_CONFIG_DIRECTORY_NOT_SET ) , 1 , 180 , true , NULL , MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_INFO ) ;
RARCH_ERR ( " [Config]: %s \n " , msg_hash_to_str ( MSG_CONFIG_DIRECTORY_NOT_SET ) ) ;
return false ;
}
core_path = path_get ( RARCH_PATH_CORE ) ;
/* Infer file name based on libretro core. */
if ( path_is_valid ( core_path ) )
{
unsigned i ;
2022-07-02 12:08:27 +00:00
char tmp [ PATH_MAX_LENGTH + 8 ] ;
2021-12-17 17:04:24 +00:00
RARCH_LOG ( " [Config]: %s \n " , msg_hash_to_str ( MSG_USING_CORE_NAME_FOR_NEW_CONFIG ) ) ;
2021-11-03 20:59:58 +00:00
2022-07-11 18:13:44 +00:00
fill_pathname_base ( config_name , core_path , sizeof ( config_name ) ) ;
path_remove_extension ( config_name ) ;
2022-08-04 12:18:11 +00:00
fill_pathname_join_special ( config_path , config_dir , config_name ,
2022-06-18 17:07:15 +00:00
sizeof ( config_path ) ) ;
2021-11-03 20:59:58 +00:00
/* In case of collision, find an alternative name. */
for ( i = 0 ; i < 16 ; i + + )
{
2022-09-01 02:17:07 +00:00
size_t _len = strlcpy ( tmp , config_path , sizeof ( tmp ) ) ;
2021-11-03 20:59:58 +00:00
if ( i )
2022-09-01 02:17:07 +00:00
snprintf ( tmp + _len , sizeof ( tmp ) - _len , " -%u " , i ) ;
2022-08-20 17:17:28 +00:00
strlcat ( tmp , " .cfg " , sizeof ( tmp ) ) ;
2021-11-03 20:59:58 +00:00
2022-06-18 17:07:15 +00:00
if ( ! path_is_valid ( tmp ) )
2021-11-03 20:59:58 +00:00
{
2022-06-18 17:07:15 +00:00
new_path_available = true ;
2021-11-03 20:59:58 +00:00
break ;
}
}
}
2022-06-18 17:07:15 +00:00
if ( ! new_path_available )
2021-11-03 20:59:58 +00:00
{
/* Fallback to system time... */
RARCH_WARN ( " [Config]: %s \n " ,
msg_hash_to_str ( MSG_CANNOT_INFER_NEW_CONFIG_PATH ) ) ;
fill_dated_filename ( config_name , " .cfg " , sizeof ( config_name ) ) ;
2022-08-04 12:18:11 +00:00
fill_pathname_join_special ( config_path , config_dir , config_name ,
2021-11-03 20:59:58 +00:00
sizeof ( config_path ) ) ;
}
2022-10-10 02:37:17 +00:00
if ( runloop_st - > flags & RUNLOOP_FLAG_OVERRIDES_ACTIVE )
2021-11-03 20:59:58 +00:00
{
/* Overrides block config file saving,
* make it appear as overrides weren ' t enabled
* for a manual save . */
2022-10-10 02:37:17 +00:00
runloop_st - > flags & = ~ RUNLOOP_FLAG_OVERRIDES_ACTIVE ;
overrides_active = true ;
2021-11-03 20:59:58 +00:00
}
# ifdef HAVE_CONFIGFILE
command_event_save_config ( config_path , msg , sizeof ( msg ) ) ;
# endif
if ( ! string_is_empty ( msg ) )
runloop_msg_queue_push ( msg , 1 , 180 , true , NULL ,
MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_INFO ) ;
2022-10-10 02:37:17 +00:00
if ( overrides_active )
runloop_st - > flags | = RUNLOOP_FLAG_OVERRIDES_ACTIVE ;
else
runloop_st - > flags & = ~ RUNLOOP_FLAG_OVERRIDES_ACTIVE ;
2021-11-03 20:59:58 +00:00
return true ;
}
void command_event_save_current_config ( enum override_type type )
{
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
switch ( type )
{
2023-01-24 00:42:50 +00:00
default :
2021-11-03 20:59:58 +00:00
case OVERRIDE_NONE :
{
2023-01-24 00:42:50 +00:00
char msg [ 256 ] ;
msg [ 0 ] = ' \0 ' ;
2021-11-03 20:59:58 +00:00
if ( path_is_empty ( RARCH_PATH_CONFIG ) )
{
2023-01-28 23:12:08 +00:00
strlcpy ( msg , " Config directory not set, cannot save configuration. " , sizeof ( msg ) ) ;
2021-11-03 20:59:58 +00:00
runloop_msg_queue_push ( msg , 1 , 180 , true , NULL , MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_INFO ) ;
}
else
{
2023-01-28 23:12:08 +00:00
if ( runloop_st - > flags & RUNLOOP_FLAG_OVERRIDES_ACTIVE )
strlcpy ( msg , msg_hash_to_str ( MSG_OVERRIDES_ACTIVE_NOT_SAVING ) , sizeof ( msg ) ) ;
else
command_event_save_config ( path_get ( RARCH_PATH_CONFIG ) , msg , sizeof ( msg ) ) ;
2021-11-03 20:59:58 +00:00
runloop_msg_queue_push ( msg , 1 , 180 , true , NULL , MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_INFO ) ;
}
}
break ;
case OVERRIDE_GAME :
case OVERRIDE_CORE :
case OVERRIDE_CONTENT_DIR :
{
2023-01-24 00:42:50 +00:00
int8_t ret = config_save_overrides ( type , & runloop_st - > system , false ) ;
char msg [ 256 ] ;
msg [ 0 ] = ' \0 ' ;
switch ( ret )
{
case 1 :
strlcpy ( msg , msg_hash_to_str ( MSG_OVERRIDES_SAVED_SUCCESSFULLY ) , sizeof ( msg ) ) ;
/* set overrides to active so the original config can be
restored after closing content */
runloop_st - > flags | = RUNLOOP_FLAG_OVERRIDES_ACTIVE ;
break ;
case - 1 :
strlcpy ( msg , msg_hash_to_str ( MSG_OVERRIDES_NOT_SAVED ) , sizeof ( msg ) ) ;
break ;
default :
case 0 :
strlcpy ( msg , msg_hash_to_str ( MSG_OVERRIDES_ERROR_SAVING ) , sizeof ( msg ) ) ;
break ;
}
RARCH_LOG ( " [Overrides]: %s \n " , msg ) ;
runloop_msg_queue_push ( msg , 1 , 180 , true , NULL , MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_INFO ) ;
# ifdef HAVE_MENU
2021-11-03 20:59:58 +00:00
{
2023-05-10 19:30:09 +00:00
struct menu_state * menu_st = menu_state_get_ptr ( ) ;
menu_st - > flags | = MENU_ST_FLAG_ENTRIES_NEED_REFRESH
| MENU_ST_FLAG_PREVENT_POPULATE ;
2021-11-03 20:59:58 +00:00
}
2023-01-24 00:42:50 +00:00
# endif
}
break ;
}
}
void command_event_remove_current_config ( enum override_type type )
{
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
switch ( type )
{
default :
case OVERRIDE_NONE :
break ;
case OVERRIDE_GAME :
case OVERRIDE_CORE :
case OVERRIDE_CONTENT_DIR :
{
char msg [ 256 ] ;
msg [ 0 ] = ' \0 ' ;
if ( config_save_overrides ( type , & runloop_st - > system , true ) )
strlcpy ( msg , msg_hash_to_str ( MSG_OVERRIDES_REMOVED_SUCCESSFULLY ) , sizeof ( msg ) ) ;
2021-11-03 20:59:58 +00:00
else
2023-01-24 00:42:50 +00:00
strlcpy ( msg , msg_hash_to_str ( MSG_OVERRIDES_ERROR_REMOVING ) , sizeof ( msg ) ) ;
2021-12-17 17:04:24 +00:00
RARCH_LOG ( " [Overrides]: %s \n " , msg ) ;
2021-11-03 20:59:58 +00:00
runloop_msg_queue_push ( msg , 1 , 180 , true , NULL , MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_INFO ) ;
2023-01-24 00:42:50 +00:00
# ifdef HAVE_MENU
{
2023-05-10 19:30:09 +00:00
struct menu_state * menu_st = menu_state_get_ptr ( ) ;
menu_st - > flags | = MENU_ST_FLAG_ENTRIES_NEED_REFRESH
| MENU_ST_FLAG_PREVENT_POPULATE ;
2023-01-24 00:42:50 +00:00
}
# endif
2021-11-03 20:59:58 +00:00
}
break ;
}
}
# endif
bool command_event_main_state ( unsigned cmd )
{
retro_ctx_size_info_t info ;
char msg [ 128 ] ;
char state_path [ 16384 ] ;
settings_t * settings = config_get_ptr ( ) ;
2022-02-02 15:49:53 +00:00
bool savestates_enabled = core_info_current_supports_savestate ( ) ;
2021-11-03 20:59:58 +00:00
bool ret = false ;
bool push_msg = true ;
state_path [ 0 ] = msg [ 0 ] = ' \0 ' ;
2022-02-02 15:49:53 +00:00
if ( savestates_enabled )
{
2022-12-29 01:13:31 +00:00
runloop_get_current_savestate_path ( state_path ,
2022-02-02 15:49:53 +00:00
sizeof ( state_path ) ) ;
2021-11-03 20:59:58 +00:00
2022-02-02 15:49:53 +00:00
core_serialize_size ( & info ) ;
savestates_enabled = ( info . size > 0 ) ;
}
2021-11-03 20:59:58 +00:00
Replay UI support (#15048)
* Add bsv replay controls (not yet fully implemented), remove toggle
see notes in task_movie.c, make sure command.c calls the right
functions, check retroarch.c and other todos.
bsv files are also now stored with states, not saves.
* Compilation fixes
* Added command impls for play and record replay, and some code in load state to do the right thing there
* Guard some parts of the new code with HAVE_BSV_MOVIE
* wip, menu fixes
* more menu fixes, osd for movie errors, halt recording properly
* Menu and label fixes
* move bsvs to own file suffix series under savestates, fix recording and playback command validity checks
* Fix replay autoincrement
* fix endif placement, whoops
---------
Co-authored-by: Joseph C. Osborn <jcoa2018@pomona.edu>
2023-03-02 23:52:22 +00:00
/* TODO: Load state should act in one of three ways:
- [ X ] Not during recording or playback : normally
- [ - ] During playback : If the state is part of this replay , go back to that state and rewind the replay ( not yet implemented ) ; otherwise halt playback and go to that state normally .
- [ - ] During recording : If the state is part of this replay , go back to that state and rewind the replay , clobbering the stuff in between then and now ( not yet implemented ) ; if the state is not part of the replay , do nothing and log a warning .
*/
2022-02-02 15:49:53 +00:00
if ( savestates_enabled )
2021-11-03 20:59:58 +00:00
{
switch ( cmd )
{
case CMD_EVENT_SAVE_STATE :
case CMD_EVENT_SAVE_STATE_TO_RAM :
{
Replay UI support (#15048)
* Add bsv replay controls (not yet fully implemented), remove toggle
see notes in task_movie.c, make sure command.c calls the right
functions, check retroarch.c and other todos.
bsv files are also now stored with states, not saves.
* Compilation fixes
* Added command impls for play and record replay, and some code in load state to do the right thing there
* Guard some parts of the new code with HAVE_BSV_MOVIE
* wip, menu fixes
* more menu fixes, osd for movie errors, halt recording properly
* Menu and label fixes
* move bsvs to own file suffix series under savestates, fix recording and playback command validity checks
* Fix replay autoincrement
* fix endif placement, whoops
---------
Co-authored-by: Joseph C. Osborn <jcoa2018@pomona.edu>
2023-03-02 23:52:22 +00:00
/* TODO: Saving state during recording should associate the state with the replay. */
2021-11-03 20:59:58 +00:00
video_driver_state_t * video_st =
video_state_get_ptr ( ) ;
bool savestate_auto_index =
settings - > bools . savestate_auto_index ;
unsigned savestate_max_keep =
settings - > uints . savestate_max_keep ;
bool frame_time_counter_reset_after_save_state =
settings - > bools . frame_time_counter_reset_after_save_state ;
if ( cmd = = CMD_EVENT_SAVE_STATE )
content_save_state ( state_path , true , false ) ;
else
content_save_state_to_ram ( ) ;
/* Clean up excess savestates if necessary */
if ( savestate_auto_index & & ( savestate_max_keep > 0 ) )
2021-11-09 05:03:00 +00:00
command_event_set_savestate_garbage_collect (
2021-11-03 20:59:58 +00:00
settings - > uints . savestate_max_keep ,
settings - > bools . show_hidden_files
) ;
if ( frame_time_counter_reset_after_save_state )
video_st - > frame_time_count = 0 ;
ret = true ;
push_msg = false ;
}
break ;
case CMD_EVENT_LOAD_STATE :
case CMD_EVENT_LOAD_STATE_FROM_RAM :
{
bool res = false ;
if ( cmd = = CMD_EVENT_LOAD_STATE )
res = content_load_state ( state_path , false , false ) ;
else
res = content_load_state_from_ram ( ) ;
if ( res )
{
2023-02-24 22:04:39 +00:00
command_post_state_loaded ( ) ;
2021-11-03 20:59:58 +00:00
ret = true ;
}
}
push_msg = false ;
break ;
Replay UI support (#15048)
* Add bsv replay controls (not yet fully implemented), remove toggle
see notes in task_movie.c, make sure command.c calls the right
functions, check retroarch.c and other todos.
bsv files are also now stored with states, not saves.
* Compilation fixes
* Added command impls for play and record replay, and some code in load state to do the right thing there
* Guard some parts of the new code with HAVE_BSV_MOVIE
* wip, menu fixes
* more menu fixes, osd for movie errors, halt recording properly
* Menu and label fixes
* move bsvs to own file suffix series under savestates, fix recording and playback command validity checks
* Fix replay autoincrement
* fix endif placement, whoops
---------
Co-authored-by: Joseph C. Osborn <jcoa2018@pomona.edu>
2023-03-02 23:52:22 +00:00
case CMD_EVENT_UNDO_LOAD_STATE :
{
/* TODO: To support this through re-recording would take some care around moving the replay recording forward to the time when the undo happened, which would need undo support for replays. For now, forbid it during recording and halt playback. */
# ifdef HAVE_BSV_MOVIE
input_driver_state_t * input_st = input_state_get_ptr ( ) ;
if ( input_st - > bsv_movie_state . flags & BSV_FLAG_MOVIE_RECORDING )
{
RARCH_ERR ( " [Load] [Movie] Can't undo load state during movie record \n " ) ;
return false ;
}
if ( input_st - > bsv_movie_state . flags & BSV_FLAG_MOVIE_PLAYBACK )
{
RARCH_LOG ( " [Load] [Movie] Undo load state during movie playback, halting playback \n " ) ;
movie_stop ( input_st ) ;
}
# endif
command_event_undo_load_state ( msg , sizeof ( msg ) ) ;
ret = true ;
break ;
}
2021-11-03 20:59:58 +00:00
case CMD_EVENT_UNDO_SAVE_STATE :
command_event_undo_save_state ( msg , sizeof ( msg ) ) ;
ret = true ;
break ;
}
}
else
strlcpy ( msg , msg_hash_to_str (
MSG_CORE_DOES_NOT_SUPPORT_SAVESTATES ) , sizeof ( msg ) ) ;
if ( push_msg )
runloop_msg_queue_push ( msg , 2 , 180 , true , NULL ,
MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_INFO ) ;
if ( ! string_is_empty ( msg ) )
2021-12-17 17:04:24 +00:00
RARCH_LOG ( " [State]: %s \n " , msg ) ;
2021-11-03 20:59:58 +00:00
return ret ;
}
bool command_event_disk_control_append_image (
const char * path )
{
runloop_state_t * runloop_st = runloop_state_get_ptr ( ) ;
rarch_system_info_t * sys_info = runloop_st ? ( rarch_system_info_t * ) & runloop_st - > system : NULL ;
if ( ! sys_info | |
! disk_control_append_image ( & sys_info - > disk_control , path ) )
return false ;
# ifdef HAVE_THREADS
2022-10-10 02:37:17 +00:00
if ( runloop_st - > flags & RUNLOOP_FLAG_USE_SRAM )
2021-11-03 20:59:58 +00:00
autosave_deinit ( ) ;
# endif
/* TODO/FIXME: Need to figure out what to do with subsystems case. */
if ( path_is_empty ( RARCH_PATH_SUBSYSTEM ) )
{
/* Update paths for our new image.
* If we actually use append_image , we assume that we
* started out in a single disk case , and that this way
* of doing it makes the most sense . */
path_set ( RARCH_PATH_NAMES , path ) ;
runloop_path_fill_names ( ) ;
}
command_event ( CMD_EVENT_AUTOSAVE_INIT , NULL ) ;
return true ;
}
2021-11-03 23:38:18 +00:00
void command_event_reinit ( const int flags )
{
settings_t * settings = config_get_ptr ( ) ;
input_driver_state_t * input_st = input_state_get_ptr ( ) ;
video_driver_state_t * video_st = video_state_get_ptr ( ) ;
# ifdef HAVE_MENU
gfx_display_t * p_disp = disp_get_ptr ( ) ;
struct menu_state * menu_st = menu_state_get_ptr ( ) ;
bool video_fullscreen = settings - > bools . video_fullscreen ;
bool adaptive_vsync = settings - > bools . video_adaptive_vsync ;
2022-05-11 10:06:49 +00:00
unsigned swap_interval_config = settings - > uints . video_swap_interval ;
2021-11-03 23:38:18 +00:00
# endif
enum input_game_focus_cmd_type
game_focus_cmd = GAME_FOCUS_CMD_REAPPLY ;
const input_device_driver_t
* joypad = input_st - > primary_joypad ;
# ifdef HAVE_MFI
const input_device_driver_t
* sec_joypad = input_st - > secondary_joypad ;
# else
const input_device_driver_t
* sec_joypad = NULL ;
# endif
video_driver_reinit ( flags ) ;
/* Poll input to avoid possibly stale data to corrupt things. */
if ( joypad & & joypad - > poll )
joypad - > poll ( ) ;
if ( sec_joypad & & sec_joypad - > poll )
sec_joypad - > poll ( ) ;
if ( input_st - > current_driver & &
input_st - > current_driver - > poll )
input_st - > current_driver - > poll ( input_st - > current_data ) ;
command_event ( CMD_EVENT_GAME_FOCUS_TOGGLE , & game_focus_cmd ) ;
# ifdef HAVE_MENU
2022-10-06 22:10:32 +00:00
p_disp - > flags | = GFX_DISP_FLAG_FB_DIRTY ;
2021-11-03 23:38:18 +00:00
if ( video_fullscreen )
video_driver_hide_mouse ( ) ;
2022-10-08 20:52:18 +00:00
if ( ( menu_st - > flags & MENU_ST_FLAG_ALIVE )
2021-11-03 23:38:18 +00:00
& & video_st - > current_video - > set_nonblock_state )
video_st - > current_video - > set_nonblock_state (
video_st - > data , false ,
video_driver_test_all_flags ( GFX_CTX_FLAGS_ADAPTIVE_VSYNC ) & &
adaptive_vsync ,
2022-05-11 10:06:49 +00:00
runloop_get_video_swap_interval ( swap_interval_config ) ) ;
2021-11-03 23:38:18 +00:00
# endif
}