Refactor some command code

This commit is contained in:
twinaphex 2016-05-09 21:23:53 +02:00
parent 2691ab0c7d
commit f15f28ce02
4 changed files with 108 additions and 93 deletions

152
command.c
View File

@ -82,17 +82,9 @@
#include <net/net_compat.h> #include <net/net_compat.h>
#endif #endif
#ifdef HAVE_COMMAND
#define DEFAULT_NETWORK_CMD_PORT 55355 #define DEFAULT_NETWORK_CMD_PORT 55355
#define STDIN_BUF_SIZE 4096 #define STDIN_BUF_SIZE 4096
#define COMMAND_EXT_GLSL 0x7c976537U
#define COMMAND_EXT_GLSLP 0x0f840c87U
#define COMMAND_EXT_CG 0x0059776fU
#define COMMAND_EXT_CGP 0x0b8865bfU
#define COMMAND_EXT_SLANG 0x105ce63aU
#define COMMAND_EXT_SLANGP 0x1bf9adeaU
struct command struct command
{ {
#ifdef HAVE_STDIN_CMD #ifdef HAVE_STDIN_CMD
@ -121,7 +113,47 @@ struct cmd_action_map
const char *arg_desc; const char *arg_desc;
}; };
static bool cmd_set_shader(const char *arg); #ifdef HAVE_COMMAND
#define COMMAND_EXT_GLSL 0x7c976537U
#define COMMAND_EXT_GLSLP 0x0f840c87U
#define COMMAND_EXT_CG 0x0059776fU
#define COMMAND_EXT_CGP 0x0b8865bfU
#define COMMAND_EXT_SLANG 0x105ce63aU
#define COMMAND_EXT_SLANGP 0x1bf9adeaU
static bool cmd_set_shader(const char *arg)
{
char msg[256];
enum rarch_shader_type type = RARCH_SHADER_NONE;
const char *ext = path_get_extension(arg);
uint32_t ext_hash = msg_hash_calculate(ext);
switch (ext_hash)
{
case COMMAND_EXT_GLSL:
case COMMAND_EXT_GLSLP:
type = RARCH_SHADER_GLSL;
break;
case COMMAND_EXT_CG:
case COMMAND_EXT_CGP:
type = RARCH_SHADER_CG;
break;
case COMMAND_EXT_SLANG:
case COMMAND_EXT_SLANGP:
type = RARCH_SHADER_SLANG;
break;
default:
return false;
}
snprintf(msg, sizeof(msg), "Shader: \"%s\"", arg);
runloop_msg_queue_push(msg, 1, 120, true);
RARCH_LOG("%s \"%s\".\n",
msg_hash_to_str(MSG_APPLYING_SHADER),
arg);
return video_driver_set_shader(type, arg);
}
static const struct cmd_action_map action_map[] = { static const struct cmd_action_map action_map[] = {
{ "SET_SHADER", cmd_set_shader, "<shader path>" }, { "SET_SHADER", cmd_set_shader, "<shader path>" },
@ -168,7 +200,7 @@ static const struct cmd_map map[] = {
}; };
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY) #if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
static bool cmd_init_network(command_t *handle, uint16_t port) static bool command_network_init(command_t *handle, uint16_t port)
{ {
int fd; int fd;
struct addrinfo *res = NULL; struct addrinfo *res = NULL;
@ -203,7 +235,7 @@ error:
#endif #endif
#ifdef HAVE_STDIN_CMD #ifdef HAVE_STDIN_CMD
static bool cmd_init_stdin(command_t *handle) static bool command_stdin_init(command_t *handle)
{ {
#ifndef _WIN32 #ifndef _WIN32
#ifdef HAVE_NETPLAY #ifdef HAVE_NETPLAY
@ -217,72 +249,45 @@ static bool cmd_init_stdin(command_t *handle)
} }
#endif #endif
command_t *command_new(bool stdin_enable, command_t *command_new(void)
bool network_enable, uint16_t port)
{ {
command_t *handle = (command_t*)calloc(1, sizeof(*handle)); command_t *handle = (command_t*)calloc(1, sizeof(*handle));
if (!handle) if (!handle)
return NULL; return NULL;
(void)network_enable; return handle;
(void)port; }
(void)stdin_enable;
bool command_network_new(
command_t *handle,
bool stdin_enable,
bool network_enable,
uint16_t port)
{
if (!handle)
return false;
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY) #if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
handle->net_fd = -1; handle->net_fd = -1;
if (network_enable && !cmd_init_network(handle, port)) if (network_enable && !command_network_init(handle, port))
goto error; goto error;
#endif #endif
#ifdef HAVE_STDIN_CMD #ifdef HAVE_STDIN_CMD
handle->stdin_enable = stdin_enable; handle->stdin_enable = stdin_enable;
if (stdin_enable && !cmd_init_stdin(handle)) if (stdin_enable && !command_stdin_init(handle))
goto error; goto error;
#endif #endif
return handle; return true;
#if (defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)) || defined(HAVE_STDIN_CMD) #if (defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)) || defined(HAVE_STDIN_CMD)
error: error:
command_free(handle); command_free(handle);
return NULL; return false;
#endif #endif
} }
static bool cmd_set_shader(const char *arg)
{
char msg[256];
enum rarch_shader_type type = RARCH_SHADER_NONE;
const char *ext = path_get_extension(arg);
uint32_t ext_hash = msg_hash_calculate(ext);
switch (ext_hash)
{
case COMMAND_EXT_GLSL:
case COMMAND_EXT_GLSLP:
type = RARCH_SHADER_GLSL;
break;
case COMMAND_EXT_CG:
case COMMAND_EXT_CGP:
type = RARCH_SHADER_CG;
break;
case COMMAND_EXT_SLANG:
case COMMAND_EXT_SLANGP:
type = RARCH_SHADER_SLANG;
break;
default:
return false;
}
snprintf(msg, sizeof(msg), "Shader: \"%s\"", arg);
runloop_msg_queue_push(msg, 1, 120, true);
RARCH_LOG("%s \"%s\".\n",
msg_hash_to_str(MSG_APPLYING_SHADER),
arg);
return video_driver_set_shader(type, arg);
}
static bool command_get_arg(const char *tok, static bool command_get_arg(const char *tok,
const char **arg, unsigned *index) const char **arg, unsigned *index)
{ {
@ -324,7 +329,7 @@ static bool command_get_arg(const char *tok,
return false; return false;
} }
static void parse_sub_msg(command_t *handle, const char *tok) static void command_parse_sub_msg(command_t *handle, const char *tok)
{ {
const char *arg = NULL; const char *arg = NULL;
unsigned index = 0; unsigned index = 0;
@ -346,20 +351,20 @@ static void parse_sub_msg(command_t *handle, const char *tok)
msg_hash_to_str(MSG_RECEIVED)); msg_hash_to_str(MSG_RECEIVED));
} }
static void parse_msg(command_t *handle, char *buf) static void command_parse_msg(command_t *handle, char *buf)
{ {
char *save = NULL; char *save = NULL;
const char *tok = strtok_r(buf, "\n", &save); const char *tok = strtok_r(buf, "\n", &save);
while (tok) while (tok)
{ {
parse_sub_msg(handle, tok); command_parse_sub_msg(handle, tok);
tok = strtok_r(NULL, "\n", &save); tok = strtok_r(NULL, "\n", &save);
} }
} }
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY) #if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
static void network_cmd_poll(command_t *handle) static void command_network_poll(command_t *handle)
{ {
fd_set fds; fd_set fds;
struct timeval tmp_tv = {0}; struct timeval tmp_tv = {0};
@ -386,7 +391,7 @@ static void network_cmd_poll(command_t *handle)
break; break;
buf[ret] = '\0'; buf[ret] = '\0';
parse_msg(handle, buf); command_parse_msg(handle, buf);
} }
} }
#endif #endif
@ -498,7 +503,7 @@ static size_t read_stdin(char *buf, size_t size)
} }
#endif #endif
static void stdin_cmd_poll(command_t *handle) static void command_stdin_poll(command_t *handle)
{ {
char *last_newline; char *last_newline;
ssize_t ret; ssize_t ret;
@ -533,7 +538,7 @@ static void stdin_cmd_poll(command_t *handle)
*last_newline++ = '\0'; *last_newline++ = '\0';
msg_len = last_newline - handle->stdin_buf; msg_len = last_newline - handle->stdin_buf;
parse_msg(handle, handle->stdin_buf); command_parse_msg(handle, handle->stdin_buf);
memmove(handle->stdin_buf, last_newline, memmove(handle->stdin_buf, last_newline,
handle->stdin_buf_ptr - msg_len); handle->stdin_buf_ptr - msg_len);
@ -611,7 +616,7 @@ static bool verify_command(const char *cmd)
return false; return false;
} }
bool command_send(const char *cmd_) bool command_network_send(const char *cmd_)
{ {
bool ret; bool ret;
char *command = NULL; char *command = NULL;
@ -656,21 +661,31 @@ bool command_send(const char *cmd_)
} }
#endif #endif
#endif
bool command_poll(command_t *handle) bool command_poll(command_t *handle)
{ {
memset(handle->state, 0, sizeof(handle->state)); memset(handle->state, 0, sizeof(handle->state));
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY) #if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
network_cmd_poll(handle); command_network_poll(handle);
#endif #endif
#ifdef HAVE_STDIN_CMD #ifdef HAVE_STDIN_CMD
stdin_cmd_poll(handle); command_stdin_poll(handle);
#endif #endif
return true; return true;
} }
bool command_get(command_handle_t *handle)
{
if (!handle || !handle->handle)
return false;
return handle->id < RARCH_BIND_LIST_END
&& handle->handle->state[handle->id];
}
bool command_set(command_handle_t *handle) bool command_set(command_handle_t *handle)
{ {
if (!handle || !handle->handle) if (!handle || !handle->handle)
@ -692,15 +707,6 @@ bool command_free(command_t *handle)
return true; return true;
} }
bool command_get(command_handle_t *handle)
{
if (!handle || !handle->handle)
return false;
return handle->id < RARCH_BIND_LIST_END
&& handle->handle->state[handle->id];
}
#endif
/** /**
* command_event_disk_control_set_eject: * command_event_disk_control_set_eject:
* @new_state : Eject or close the virtual drive tray. * @new_state : Eject or close the virtual drive tray.

View File

@ -30,6 +30,12 @@ extern "C" {
typedef struct command command_t; typedef struct command command_t;
typedef struct command_handle
{
command_t *handle;
unsigned id;
} command_handle_t;
enum event_command enum event_command
{ {
CMD_EVENT_NONE = 0, CMD_EVENT_NONE = 0,
@ -215,28 +221,27 @@ enum event_command
CMD_EVENT_EXEC CMD_EVENT_EXEC
}; };
typedef struct command_handle
{
command_t *handle;
unsigned id;
} command_handle_t;
#ifdef HAVE_COMMAND #ifdef HAVE_COMMAND
command_t *command_new(bool stdin_enable,
bool network_enable, uint16_t port);
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY) #if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
bool command_send(const char *cmd_); bool command_network_send(const char *cmd_);
#endif #endif
#endif
bool command_network_new(
command_t *handle,
bool stdin_enable,
bool network_enable,
uint16_t port);
command_t *command_new(void);
bool command_poll(command_t *handle); bool command_poll(command_t *handle);
bool command_set(command_handle_t *handle);
bool command_get(command_handle_t *handle); bool command_get(command_handle_t *handle);
bool command_set(command_handle_t *handle);
bool command_free(command_t *handle); bool command_free(command_t *handle);
#endif
/** /**
* command_event: * command_event:

View File

@ -839,7 +839,8 @@ bool input_driver_init_command(void)
{ {
#ifdef HAVE_COMMAND #ifdef HAVE_COMMAND
settings_t *settings = config_get_ptr(); settings_t *settings = config_get_ptr();
if (!settings->stdin_cmd_enable && !settings->network_cmd_enable) if ( !settings->stdin_cmd_enable
&& !settings->network_cmd_enable)
return false; return false;
if (settings->stdin_cmd_enable if (settings->stdin_cmd_enable
@ -849,11 +850,14 @@ bool input_driver_init_command(void)
"Cannot use this command interface.\n"); "Cannot use this command interface.\n");
} }
input_driver_command = command_new(settings->stdin_cmd_enable input_driver_command = command_new();
&& !input_driver_grab_stdin(),
settings->network_cmd_enable, settings->network_cmd_port); if (!command_network_new(
input_driver_command,
if (!input_driver_command) settings->stdin_cmd_enable
&& !input_driver_grab_stdin(),
settings->network_cmd_enable,
settings->network_cmd_port))
{ {
RARCH_ERR("Failed to initialize command interface.\n"); RARCH_ERR("Failed to initialize command interface.\n");
return false; return false;

View File

@ -952,7 +952,7 @@ static void retroarch_parse_input(int argc, char *argv[])
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY) #if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
case RA_OPT_COMMAND: case RA_OPT_COMMAND:
if (command_send((const char*)optarg)) if (command_network_send((const char*)optarg))
exit(0); exit(0);
else else
retroarch_fail(1, "network_cmd_send()"); retroarch_fail(1, "network_cmd_send()");