RetroArch/remote.c

235 lines
5.6 KiB
C
Raw Normal View History

2015-11-26 04:40:11 +00:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2016-01-10 03:06:50 +00:00
* Copyright (C) 2011-2016 - Daniel De Matteis
2015-11-26 04:40:11 +00:00
*
* 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 <string.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <compat/strl.h>
#include <compat/posix_string.h>
#include <file/file_path.h>
#include <retro_miscellaneous.h>
#include <net/net_compat.h>
2016-05-01 21:52:55 +00:00
#include <net/net_socket.h>
2015-11-26 04:40:11 +00:00
#include "remote.h"
2016-02-07 12:18:48 +00:00
#include "msg_hash.h"
2015-11-26 04:40:11 +00:00
#include "runloop.h"
#include "verbosity.h"
#define DEFAULT_NETWORK_GAMEPAD_PORT 55400
2015-11-27 19:43:56 +00:00
#define UDP_FRAME_PACKETS 16
2015-11-26 04:40:11 +00:00
struct rarch_remote
{
#if defined(HAVE_NETWORK_GAMEPAD) && defined(HAVE_NETPLAY)
2015-11-27 04:07:30 +00:00
int net_fd[MAX_USERS];
2015-11-26 04:40:11 +00:00
#endif
bool state[RARCH_BIND_LIST_END];
};
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;
static input_remote_state_t remote_st_ptr;
static input_remote_state_t *input_remote_get_state_ptr(void)
{
return &remote_st_ptr;
}
2015-11-27 19:43:56 +00:00
2015-11-26 04:40:11 +00:00
#if defined(HAVE_NETWORK_GAMEPAD) && defined(HAVE_NETPLAY)
2016-02-03 16:07:45 +00:00
static bool remote_init_network(rarch_remote_t *handle,
uint16_t port, unsigned user)
2015-11-26 04:40:11 +00:00
{
2016-05-01 23:26:19 +00:00
int fd;
2015-11-26 04:40:11 +00:00
struct addrinfo *res = NULL;
2015-11-27 04:07:30 +00:00
port = port + user;
2015-11-26 04:40:11 +00:00
if (!network_init())
return false;
RARCH_LOG("Bringing up remote interface on port %hu.\n",
(unsigned short)port);
2016-05-08 02:20:25 +00:00
fd = socket_init((void**)&res, port, NULL, SOCKET_TYPE_DATAGRAM);
2015-11-26 04:40:11 +00:00
2016-05-01 23:26:19 +00:00
if (fd < 0)
2015-11-26 04:40:11 +00:00
goto error;
2016-05-01 23:26:19 +00:00
handle->net_fd[user] = fd;
2015-11-27 04:07:30 +00:00
if (!socket_nonblock(handle->net_fd[user]))
2015-11-26 04:40:11 +00:00
goto error;
2016-05-01 23:13:25 +00:00
if (!socket_bind(handle->net_fd[user], res))
2015-11-26 04:40:11 +00:00
{
RARCH_ERR("Failed to bind socket.\n");
goto error;
}
freeaddrinfo_retro(res);
return true;
error:
if (res)
freeaddrinfo_retro(res);
return false;
}
#endif
2015-11-27 04:07:30 +00:00
rarch_remote_t *rarch_remote_new(uint16_t port)
2015-11-26 04:40:11 +00:00
{
2015-11-27 23:36:22 +00:00
unsigned user;
2016-01-23 22:41:46 +00:00
#if defined(HAVE_NETWORK_GAMEPAD) && defined(HAVE_NETPLAY)
2015-11-27 23:36:22 +00:00
settings_t *settings = config_get_ptr();
2016-01-23 22:41:46 +00:00
#endif
2016-02-03 16:07:45 +00:00
rarch_remote_t *handle = (rarch_remote_t*)
calloc(1, sizeof(*handle));
2015-11-27 23:36:22 +00:00
2015-11-26 04:40:11 +00:00
if (!handle)
return NULL;
(void)port;
#if defined(HAVE_NETWORK_GAMEPAD) && defined(HAVE_NETPLAY)
2015-11-27 23:36:22 +00:00
for(user = 0; user < settings->input.max_users; user ++)
2015-11-27 04:07:30 +00:00
{
handle->net_fd[user] = -1;
if(settings->network_remote_enable_user[user])
if (!remote_init_network(handle, port, user))
goto error;
}
2015-11-26 04:40:11 +00:00
#endif
return handle;
2016-01-19 21:33:48 +00:00
#if defined(HAVE_NETWORK_GAMEPAD) && defined(HAVE_NETPLAY)
2015-11-26 04:40:11 +00:00
error:
rarch_remote_free(handle);
return NULL;
#endif
}
void rarch_remote_free(rarch_remote_t *handle)
{
2015-11-27 23:36:22 +00:00
unsigned user;
2016-01-23 22:41:46 +00:00
#if defined(HAVE_NETWORK_GAMEPAD) && defined(HAVE_NETPLAY)
settings_t *settings = config_get_ptr();
2015-11-27 23:36:22 +00:00
for(user = 0; user < settings->input.max_users; user ++)
2015-11-27 04:07:30 +00:00
socket_close(handle->net_fd[user]);
2015-11-26 04:40:11 +00:00
#endif
free(handle);
}
2016-01-19 21:33:48 +00:00
#if defined(HAVE_NETWORK_GAMEPAD) && defined(HAVE_NETPLAY)
static void parse_packet(char *buffer, unsigned size, unsigned user)
2015-11-26 04:40:11 +00:00
{
input_remote_state_t *ol_state = input_remote_get_state_ptr();
/* todo implement parsing of input_state from the packet */
ol_state->buttons[user] = atoi(buffer);
2015-11-26 04:40:11 +00:00
}
2016-01-19 21:33:48 +00:00
#endif
2015-11-26 04:40:11 +00:00
void input_state_remote(int16_t *ret,
unsigned port, unsigned device, unsigned idx,
unsigned id)
2015-11-26 04:40:11 +00:00
{
input_remote_state_t *ol_state = input_remote_get_state_ptr();
if (!ol_state)
return;
switch (device)
{
case RETRO_DEVICE_JOYPAD:
if (input_remote_key_pressed(id, port))
*ret |= 1;
break;
case RETRO_DEVICE_ANALOG:
{
unsigned base = 0;
if (idx == RETRO_DEVICE_INDEX_ANALOG_RIGHT)
base = 2;
if (id == RETRO_DEVICE_ID_ANALOG_Y)
base += 1;
if (ol_state && ol_state->analog[base][port])
*ret = ol_state->analog[base][port];
}
break;
}
2015-11-26 04:40:11 +00:00
}
bool input_remote_key_pressed(int key, unsigned port)
2015-11-27 19:43:56 +00:00
{
input_remote_state_t *ol_state = input_remote_get_state_ptr();
2015-11-27 19:43:56 +00:00
if (!ol_state)
return false;
return (ol_state->buttons[port] & (UINT64_C(1) << key));
2015-11-27 19:43:56 +00:00
}
void rarch_remote_poll(rarch_remote_t *handle)
{
2015-11-27 23:36:22 +00:00
unsigned user;
2016-01-23 22:41:46 +00:00
settings_t *settings = config_get_ptr();
input_remote_state_t *ol_state = input_remote_get_state_ptr();
2015-11-27 19:43:56 +00:00
2015-11-27 23:36:22 +00:00
for(user = 0; user < settings->input.max_users; user++)
2015-11-27 19:43:56 +00:00
{
if (settings->network_remote_enable_user[user])
{
2016-01-19 21:33:48 +00:00
#if defined(HAVE_NETWORK_GAMEPAD) && defined(HAVE_NETPLAY)
2015-11-27 23:36:22 +00:00
char buf[8];
ssize_t ret;
2015-11-27 19:43:56 +00:00
fd_set fds;
struct timeval tmp_tv = {0};
2015-11-27 23:36:22 +00:00
2015-11-27 19:43:56 +00:00
if (handle->net_fd[user] < 0)
return;
2015-11-27 23:36:22 +00:00
2015-11-27 19:43:56 +00:00
FD_ZERO(&fds);
FD_SET(handle->net_fd[user], &fds);
2015-11-27 23:36:22 +00:00
ret = recvfrom(handle->net_fd[user], buf,
sizeof(buf) - 1, 0, NULL, NULL);
2015-11-27 19:43:56 +00:00
if (ret > 0)
parse_packet(buf, sizeof(buf), user);
else
2016-01-19 21:33:48 +00:00
#endif
ol_state->buttons[user] = 0;
2015-11-27 19:43:56 +00:00
}
}
}