1
0
mirror of https://github.com/libretro/RetroArch synced 2024-07-03 00:38:44 +00:00

Merg input_remote into retroarch.c

This commit is contained in:
twinaphex 2019-06-18 01:03:33 +02:00
parent 0bb4d0f24f
commit fd17e04bb1
5 changed files with 211 additions and 323 deletions

View File

@ -1817,8 +1817,7 @@ ifeq ($(HAVE_NETWORKING), 1)
endif
ifeq ($(HAVE_NETWORKGAMEPAD), 1)
OBJ += input/input_remote.o \
cores/libretro-net-retropad/net_retropad_core.o
OBJ += cores/libretro-net-retropad/net_retropad_core.o
endif
ifeq ($(HAVE_BUILTINMINIUPNPC), 1)

View File

@ -1355,7 +1355,6 @@ MENU
#endif
#ifdef HAVE_NETWORKGAMEPAD
#include "../input/input_remote.c"
#include "../cores/libretro-net-retropad/net_retropad_core.c"
#endif

View File

@ -1,264 +0,0 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
#include <compat/strl.h>
#include <compat/posix_string.h>
#include <file/file_path.h>
#include <retro_miscellaneous.h>
#include <libretro.h>
#include <net/net_compat.h>
#include <net/net_socket.h>
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include "input_remote.h"
#include "../configuration.h"
#include "../msg_hash.h"
#include "../verbosity.h"
#define DEFAULT_NETWORK_GAMEPAD_PORT 55400
#define UDP_FRAME_PACKETS 16
struct remote_message
{
uint16_t state;
int port;
int device;
int index;
int id;
};
struct input_remote
{
bool state[RARCH_BIND_LIST_END];
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
int net_fd[MAX_USERS];
#endif
};
typedef struct input_remote_state
{
/* Left X, Left Y, Right X, Right Y */
int16_t analog[4][MAX_USERS];
/* This is a bitmask of (1 << key_bind_id). */
uint64_t buttons[MAX_USERS];
} input_remote_state_t;
static input_remote_state_t remote_st_ptr;
static input_remote_state_t *input_remote_get_state_ptr(void)
{
return &remote_st_ptr;
}
#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;
}
#endif
input_remote_t *input_remote_new(uint16_t port, unsigned max_users)
{
unsigned user;
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
settings_t *settings = config_get_ptr();
#endif
input_remote_t *handle = (input_remote_t*)
calloc(1, sizeof(*handle));
if (!handle)
return NULL;
(void)port;
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
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))
goto error;
}
#endif
return handle;
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
error:
input_remote_free(handle, max_users);
return NULL;
#endif
}
void input_remote_free(input_remote_t *handle, unsigned max_users)
{
unsigned user;
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
for(user = 0; user < max_users; user ++)
socket_close(handle->net_fd[user]);
#endif
free(handle);
}
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
static void input_remote_parse_packet(struct remote_message *msg, unsigned user)
{
input_remote_state_t *input_state = input_remote_get_state_ptr();
/* 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
void input_remote_state(
int16_t *ret,
unsigned port,
unsigned device,
unsigned idx,
unsigned id)
{
switch (device)
{
case RETRO_DEVICE_JOYPAD:
if (input_remote_key_pressed(id, port))
*ret |= 1;
break;
case RETRO_DEVICE_ANALOG:
{
unsigned base = 0;
input_remote_state_t *input_state = input_remote_get_state_ptr();
if (!input_state)
return;
if (idx == RETRO_DEVICE_INDEX_ANALOG_RIGHT)
base = 2;
if (id == RETRO_DEVICE_ID_ANALOG_Y)
base += 1;
if (input_state && input_state->analog[base][port])
*ret = input_state->analog[base][port];
}
break;
}
}
bool input_remote_key_pressed(int key, unsigned port)
{
input_remote_state_t *input_state = input_remote_get_state_ptr();
if (!input_state)
return false;
return (input_state->buttons[port] & (UINT64_C(1) << key));
}
void input_remote_poll(input_remote_t *handle, unsigned max_users)
{
unsigned user;
settings_t *settings = config_get_ptr();
input_remote_state_t *input_state = input_remote_get_state_ptr();
for(user = 0; user < max_users; user++)
{
if (settings->bools.network_remote_enable_user[user])
{
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
struct remote_message msg;
ssize_t ret;
fd_set fds;
if (handle->net_fd[user] < 0)
return;
FD_ZERO(&fds);
FD_SET(handle->net_fd[user], &fds);
ret = recvfrom(handle->net_fd[user], (char*)&msg,
sizeof(msg), 0, NULL, NULL);
if (ret == sizeof(msg))
input_remote_parse_packet(&msg, user);
else if ((ret != -1) || ((errno != EAGAIN) && (errno != ENOENT)))
#endif
{
input_state->buttons[user] = 0;
input_state->analog[0][user] = 0;
input_state->analog[1][user] = 0;
input_state->analog[2][user] = 0;
input_state->analog[3][user] = 0;
}
}
}
}

View File

@ -1,50 +0,0 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INPUT_REMOTE_H__
#define INPUT_REMOTE_H__
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdint.h>
#include <boolean.h>
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
typedef struct input_remote input_remote_t;
input_remote_t *input_remote_new(uint16_t port, unsigned max_users);
void input_remote_free(input_remote_t *handle, unsigned max_users);
void input_remote_poll(input_remote_t *handle, unsigned max_users);
bool input_remote_key_pressed(int key, unsigned port);
void input_remote_state(
int16_t *ret,
unsigned port,
unsigned device,
unsigned idx,
unsigned id);
RETRO_END_DECLS
#endif

View File

@ -77,6 +77,11 @@
#include "config.h"
#endif
#ifdef HAVE_NETWORKING
#include <net/net_compat.h>
#include <net/net_socket.h>
#endif
#include <audio/audio_resampler.h>
#ifdef HAVE_MENU
@ -90,10 +95,6 @@
#endif
#endif
#ifdef HAVE_NETWORKGAMEPAD
#include "input/input_remote.h"
#endif
#include "input/input_driver.h"
#include "input/input_mapper.h"
#include "input/input_keymaps.h"
@ -1388,6 +1389,149 @@ void recording_driver_update_streaming_url(void)
}
}
/* Input Remote */
#define DEFAULT_NETWORK_GAMEPAD_PORT 55400
#define UDP_FRAME_PACKETS 16
struct remote_message
{
uint16_t state;
int port;
int device;
int index;
int id;
};
struct input_remote
{
bool state[RARCH_BIND_LIST_END];
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
int net_fd[MAX_USERS];
#endif
};
typedef struct input_remote input_remote_t;
typedef struct input_remote_state
{
/* Left X, Left Y, Right X, Right Y */
int16_t analog[4][MAX_USERS];
/* This is a bitmask of (1 << key_bind_id). */
uint64_t buttons[MAX_USERS];
} input_remote_state_t;
static input_remote_state_t remote_st_ptr;
#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;
}
#endif
static void input_remote_free(input_remote_t *handle, unsigned max_users)
{
unsigned user;
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
for(user = 0; user < max_users; user ++)
socket_close(handle->net_fd[user]);
#endif
free(handle);
}
static input_remote_t *input_remote_new(uint16_t port, unsigned max_users)
{
unsigned user;
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
settings_t *settings = config_get_ptr();
#endif
input_remote_t *handle = (input_remote_t*)
calloc(1, sizeof(*handle));
if (!handle)
return NULL;
(void)port;
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
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))
goto error;
}
#endif
return handle;
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
error:
input_remote_free(handle, max_users);
return NULL;
#endif
}
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
static void input_remote_parse_packet(struct remote_message *msg, unsigned user)
{
input_remote_state_t *input_state = &remote_st_ptr;
/* 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
#define input_remote_key_pressed(key, port) (remote_st_ptr.buttons[(port)] & (UINT64_C(1) << (key)))
/* Input */
static pad_connection_listener_t *pad_connection_listener = NULL;
@ -1958,8 +2102,44 @@ void input_poll(void)
#endif
#ifdef HAVE_NETWORKGAMEPAD
/* Poll remote */
if (input_driver_remote)
input_remote_poll(input_driver_remote, max_users);
{
unsigned user;
for(user = 0; user < max_users; user++)
{
if (settings->bools.network_remote_enable_user[user])
{
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
struct remote_message msg;
ssize_t ret;
fd_set fds;
if (input_driver_remote->net_fd[user] < 0)
return;
FD_ZERO(&fds);
FD_SET(input_driver_remote->net_fd[user], &fds);
ret = recvfrom(input_driver_remote->net_fd[user], (char*)&msg,
sizeof(msg), 0, NULL, NULL);
if (ret == sizeof(msg))
input_remote_parse_packet(&msg, user);
else if ((ret != -1) || ((errno != EAGAIN) && (errno != ENOENT)))
#endif
{
input_remote_state_t *input_state = &remote_st_ptr;
input_state->buttons[user] = 0;
input_state->analog[0][user] = 0;
input_state->analog[1][user] = 0;
input_state->analog[2][user] = 0;
input_state->analog[3][user] = 0;
}
}
}
}
#endif
}
@ -2026,7 +2206,31 @@ int16_t input_state(unsigned port, unsigned device,
#ifdef HAVE_NETWORKGAMEPAD
if (input_driver_remote)
input_remote_state(&res, port, device, idx, id);
{
switch (device)
{
case RETRO_DEVICE_JOYPAD:
if (input_remote_key_pressed(id, port))
res |= 1;
break;
case RETRO_DEVICE_ANALOG:
{
unsigned base = 0;
input_remote_state_t *input_state = &remote_st_ptr;
if (input_state)
{
if (idx == RETRO_DEVICE_INDEX_ANALOG_RIGHT)
base = 2;
if (id == RETRO_DEVICE_ID_ANALOG_Y)
base += 1;
if (input_state->analog[base][port])
res = input_state->analog[base][port];
}
}
break;
}
}
#endif
if (((id < RARCH_FIRST_META_KEY) || (device == RETRO_DEVICE_KEYBOARD)))