Move code to input_driver.c

This commit is contained in:
twinaphex 2021-09-07 10:17:06 +02:00
parent 138afafbb3
commit 3a78b46862
4 changed files with 159 additions and 144 deletions

View File

@ -21,6 +21,11 @@
#include "input_driver.h"
#ifdef HAVE_NETWORKING
#include <net/net_compat.h>
#include <net/net_socket.h>
#endif
#include "../retroarch.h"
#include "../verbosity.h"
#include "../configuration.h"
@ -687,3 +692,107 @@ int16_t input_joypad_axis(
return val;
}
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
static bool input_remote_init_network(input_remote_t *handle,
uint16_t port, unsigned user)
{
int fd;
struct addrinfo *res = NULL;
port = port + user;
if (!network_init())
return false;
RARCH_LOG("Bringing up remote interface on port %hu.\n",
(unsigned short)port);
fd = socket_init((void**)&res, port, NULL, SOCKET_TYPE_DATAGRAM);
if (fd < 0)
goto error;
handle->net_fd[user] = fd;
if (!socket_nonblock(handle->net_fd[user]))
goto error;
if (!socket_bind(handle->net_fd[user], res))
{
RARCH_ERR("%s\n", msg_hash_to_str(MSG_FAILED_TO_BIND_SOCKET));
goto error;
}
freeaddrinfo_retro(res);
return true;
error:
if (res)
freeaddrinfo_retro(res);
return false;
}
void input_remote_free(input_remote_t *handle, unsigned max_users)
{
unsigned user;
for (user = 0; user < max_users; user ++)
socket_close(handle->net_fd[user]);
free(handle);
}
static input_remote_t *input_remote_new(
settings_t *settings,
uint16_t port, unsigned max_users)
{
unsigned user;
input_remote_t *handle = (input_remote_t*)
calloc(1, sizeof(*handle));
if (!handle)
return NULL;
for (user = 0; user < max_users; user ++)
{
handle->net_fd[user] = -1;
if (settings->bools.network_remote_enable_user[user])
if (!input_remote_init_network(handle, port, user))
{
input_remote_free(handle, max_users);
return NULL;
}
}
return handle;
}
void input_remote_parse_packet(
input_remote_state_t *input_state,
struct remote_message *msg, unsigned user)
{
/* Parse message */
switch (msg->device)
{
case RETRO_DEVICE_JOYPAD:
input_state->buttons[user] &= ~(1 << msg->id);
if (msg->state)
input_state->buttons[user] |= 1 << msg->id;
break;
case RETRO_DEVICE_ANALOG:
input_state->analog[msg->index * 2 + msg->id][user] = msg->state;
break;
}
}
input_remote_t *input_driver_init_remote(
settings_t *settings,
unsigned num_active_users)
{
unsigned network_remote_base_port = settings->uints.network_remote_base_port;
return input_remote_new(
settings,
network_remote_base_port,
num_active_users);
}
#endif

View File

@ -120,11 +120,49 @@ typedef struct
bool autoconfigured;
} input_device_info_t;
struct remote_message
{
int port;
int device;
int index;
int id;
uint16_t state;
};
struct input_remote
{
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
int net_fd[MAX_USERS];
#endif
bool state[RARCH_BIND_LIST_END];
};
typedef struct
{
char display_name[256];
} input_mouse_info_t;
typedef struct input_remote input_remote_t;
typedef struct input_remote_state
{
/* This is a bitmask of (1 << key_bind_id). */
uint64_t buttons[MAX_USERS];
/* Left X, Left Y, Right X, Right Y */
int16_t analog[4][MAX_USERS];
} input_remote_state_t;
typedef struct input_list_element_t
{
int16_t *state;
unsigned port;
unsigned device;
unsigned index;
unsigned int state_size;
} input_list_element;
/**
* Organizes the functions and data structures of each driver that are accessed
* by other parts of the input code. The input_driver structs are the "interface"
@ -672,6 +710,18 @@ int16_t input_joypad_axis(
const input_device_driver_t *drv,
unsigned port, uint32_t joyaxis, float normal_mag);
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
void input_remote_parse_packet(
input_remote_state_t *input_state,
struct remote_message *msg, unsigned user);
input_remote_t *input_driver_init_remote(
settings_t *settings,
unsigned num_active_users);
void input_remote_free(input_remote_t *handle, unsigned max_users);
#endif
/*****************************************************************************/
const struct retro_keybind *input_config_get_bind_auto(unsigned port, unsigned id);

View File

@ -19957,101 +19957,6 @@ static void retroarch_overlay_init(struct rarch_state *p_rarch)
}
#endif
/* INPUT REMOTE */
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
static bool input_remote_init_network(input_remote_t *handle,
uint16_t port, unsigned user)
{
int fd;
struct addrinfo *res = NULL;
port = port + user;
if (!network_init())
return false;
RARCH_LOG("Bringing up remote interface on port %hu.\n",
(unsigned short)port);
fd = socket_init((void**)&res, port, NULL, SOCKET_TYPE_DATAGRAM);
if (fd < 0)
goto error;
handle->net_fd[user] = fd;
if (!socket_nonblock(handle->net_fd[user]))
goto error;
if (!socket_bind(handle->net_fd[user], res))
{
RARCH_ERR("%s\n", msg_hash_to_str(MSG_FAILED_TO_BIND_SOCKET));
goto error;
}
freeaddrinfo_retro(res);
return true;
error:
if (res)
freeaddrinfo_retro(res);
return false;
}
static void input_remote_free(input_remote_t *handle, unsigned max_users)
{
unsigned user;
for (user = 0; user < max_users; user ++)
socket_close(handle->net_fd[user]);
free(handle);
}
static input_remote_t *input_remote_new(
settings_t *settings,
uint16_t port, unsigned max_users)
{
unsigned user;
input_remote_t *handle = (input_remote_t*)
calloc(1, sizeof(*handle));
if (!handle)
return NULL;
for (user = 0; user < max_users; user ++)
{
handle->net_fd[user] = -1;
if (settings->bools.network_remote_enable_user[user])
if (!input_remote_init_network(handle, port, user))
{
input_remote_free(handle, max_users);
return NULL;
}
}
return handle;
}
static void input_remote_parse_packet(
input_remote_state_t *input_state,
struct remote_message *msg, unsigned user)
{
/* Parse message */
switch (msg->device)
{
case RETRO_DEVICE_JOYPAD:
input_state->buttons[user] &= ~(1 << msg->id);
if (msg->state)
input_state->buttons[user] |= 1 << msg->id;
break;
case RETRO_DEVICE_ANALOG:
input_state->analog[msg->index * 2 + msg->id][user] = msg->state;
break;
}
}
#endif
/* INPUT */
void set_connection_listener(pad_connection_listener_t *listener)
@ -22671,19 +22576,6 @@ static void input_driver_deinit_command(struct rarch_state *p_rarch)
}
#endif
#ifdef HAVE_NETWORKGAMEPAD
static input_remote_t *input_driver_init_remote(
settings_t *settings,
unsigned num_active_users)
{
unsigned network_remote_base_port = settings->uints.network_remote_base_port;
return input_remote_new(
settings,
network_remote_base_port,
num_active_users);
}
#endif
/**
* config_get_joypad_driver_options:
*

View File

@ -1098,46 +1098,10 @@ typedef struct
enum gfx_ctx_api api;
} gfx_api_gpu_map;
struct remote_message
{
int port;
int device;
int index;
int id;
uint16_t state;
};
struct input_remote
{
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
int net_fd[MAX_USERS];
#endif
bool state[RARCH_BIND_LIST_END];
};
#ifdef HAVE_BSV_MOVIE
typedef struct bsv_movie bsv_movie_t;
#endif
typedef struct input_remote input_remote_t;
typedef struct input_remote_state
{
/* This is a bitmask of (1 << key_bind_id). */
uint64_t buttons[MAX_USERS];
/* Left X, Left Y, Right X, Right Y */
int16_t analog[4][MAX_USERS];
} input_remote_state_t;
typedef struct input_list_element_t
{
int16_t *state;
unsigned port;
unsigned device;
unsigned index;
unsigned int state_size;
} input_list_element;
typedef void *(*constructor_t)(void);
typedef void (*destructor_t )(void*);