RetroArch/input/input_remote.c

266 lines
6.4 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
2017-01-22 12:40:32 +00:00
* Copyright (C) 2011-2017 - 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>
2016-09-11 14:31:23 +00:00
#include <stdlib.h>
2015-11-26 04:40:11 +00:00
#include <string.h>
2016-09-08 06:08:42 +00:00
#ifdef _WIN32
#include <direct.h>
#else
2015-11-26 04:40:11 +00:00
#include <unistd.h>
#endif
#include <compat/strl.h>
#include <compat/posix_string.h>
#include <file/file_path.h>
#include <retro_miscellaneous.h>
2017-04-29 11:20:50 +00:00
#include <libretro.h>
2015-11-26 04:40:11 +00:00
#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
2016-09-06 21:59:43 +00:00
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
2016-05-09 15:37:29 +00:00
#include "input_remote.h"
2015-11-26 04:40:11 +00:00
2016-09-05 16:31:32 +00:00
#include "../configuration.h"
2016-06-27 04:38:47 +00:00
#include "../msg_hash.h"
#include "../verbosity.h"
2015-11-26 04:40:11 +00:00
#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
2016-07-27 19:28:33 +00:00
struct remote_message
{
2017-09-27 22:53:13 +00:00
uint16_t state;
int port;
int device;
int index;
int id;
};
2016-05-09 05:19:18 +00:00
struct input_remote
2015-11-26 04:40:11 +00:00
{
2017-09-27 22:53:13 +00:00
bool state[RARCH_BIND_LIST_END];
2016-09-29 19:07:10 +00:00
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
2015-11-27 04:07:30 +00:00
int net_fd[MAX_USERS];
2015-11-26 04:40:11 +00:00
#endif
};
typedef struct input_remote_state
{
/* Left X, Left Y, Right X, Right Y */
int16_t analog[4][MAX_USERS];
2017-09-27 22:53:13 +00:00
/* 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;
}
2015-11-27 19:43:56 +00:00
2016-09-29 19:07:10 +00:00
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
2016-05-09 05:19:18 +00:00
static bool input_remote_init_network(input_remote_t *handle,
2016-02-03 16:07:45 +00:00
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
{
2016-11-08 00:17:10 +00:00
RARCH_ERR("%s\n", msg_hash_to_str(MSG_FAILED_TO_BIND_SOCKET));
2015-11-26 04:40:11 +00:00
goto error;
}
freeaddrinfo_retro(res);
return true;
error:
if (res)
freeaddrinfo_retro(res);
return false;
}
#endif
2017-01-17 18:43:19 +00:00
input_remote_t *input_remote_new(uint16_t port, unsigned max_users)
2015-11-26 04:40:11 +00:00
{
2015-11-27 23:36:22 +00:00
unsigned user;
2016-09-29 19:07:10 +00:00
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
2015-11-27 23:36:22 +00:00
settings_t *settings = config_get_ptr();
2016-01-23 22:41:46 +00:00
#endif
2016-05-09 05:19:18 +00:00
input_remote_t *handle = (input_remote_t*)
2016-02-03 16:07:45 +00:00
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;
2016-09-29 19:07:10 +00:00
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
2017-01-17 18:43:19 +00:00
for(user = 0; user < max_users; user ++)
2015-11-27 04:07:30 +00:00
{
handle->net_fd[user] = -1;
2017-04-28 11:43:47 +00:00
if(settings->bools.network_remote_enable_user[user])
2016-05-09 05:19:18 +00:00
if (!input_remote_init_network(handle, port, user))
2015-11-27 04:07:30 +00:00
goto error;
}
2015-11-26 04:40:11 +00:00
#endif
return handle;
2016-09-29 19:07:10 +00:00
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
2015-11-26 04:40:11 +00:00
error:
2017-01-17 18:43:19 +00:00
input_remote_free(handle, max_users);
2015-11-26 04:40:11 +00:00
return NULL;
#endif
}
2017-01-17 18:43:19 +00:00
void input_remote_free(input_remote_t *handle, unsigned max_users)
2015-11-26 04:40:11 +00:00
{
2015-11-27 23:36:22 +00:00
unsigned user;
2016-09-29 19:07:10 +00:00
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
2015-11-27 23:36:22 +00:00
2017-01-17 18:43:19 +00:00
for(user = 0; user < 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-09-29 19:07:10 +00:00
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
2016-07-27 19:28:33 +00:00
static void input_remote_parse_packet(struct remote_message *msg, unsigned user)
2015-11-26 04:40:11 +00:00
{
2017-09-08 03:30:42 +00:00
input_remote_state_t *input_state = input_remote_get_state_ptr();
/* Parse message */
switch (msg->device)
{
case RETRO_DEVICE_JOYPAD:
2017-09-08 03:30:42 +00:00
input_state->buttons[user] &= ~(1 << msg->id);
if (msg->state)
2017-09-08 03:30:42 +00:00
input_state->buttons[user] |= 1 << msg->id;
break;
case RETRO_DEVICE_ANALOG:
2017-09-08 03:30:42 +00:00
input_state->analog[msg->index * 2 + msg->id][user] = msg->state;
break;
}
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
2016-05-09 05:19:18 +00:00
void input_remote_state(
int16_t *ret,
unsigned port,
unsigned device,
unsigned idx,
unsigned id)
2015-11-26 04:40:11 +00:00
{
switch (device)
{
case RETRO_DEVICE_JOYPAD:
if (input_remote_key_pressed(id, port))
*ret |= 1;
break;
case RETRO_DEVICE_ANALOG:
{
unsigned base = 0;
2017-09-08 03:30:42 +00:00
input_remote_state_t *input_state = input_remote_get_state_ptr();
2017-09-08 03:30:42 +00:00
if (!input_state)
return;
if (idx == RETRO_DEVICE_INDEX_ANALOG_RIGHT)
base = 2;
if (id == RETRO_DEVICE_ID_ANALOG_Y)
base += 1;
2017-09-08 03:30:42 +00:00
if (input_state && input_state->analog[base][port])
*ret = input_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
{
2017-09-08 03:30:42 +00:00
input_remote_state_t *input_state = input_remote_get_state_ptr();
2015-11-27 19:43:56 +00:00
2017-09-08 03:30:42 +00:00
if (!input_state)
return false;
2017-09-08 03:30:42 +00:00
return (input_state->buttons[port] & (UINT64_C(1) << key));
2015-11-27 19:43:56 +00:00
}
2017-01-17 18:43:19 +00:00
void input_remote_poll(input_remote_t *handle, unsigned max_users)
2015-11-27 19:43:56 +00:00
{
2015-11-27 23:36:22 +00:00
unsigned user;
2016-01-23 22:41:46 +00:00
settings_t *settings = config_get_ptr();
2017-09-08 03:30:42 +00:00
input_remote_state_t *input_state = input_remote_get_state_ptr();
2017-01-17 18:43:19 +00:00
for(user = 0; user < max_users; user++)
2015-11-27 19:43:56 +00:00
{
2017-04-28 11:43:47 +00:00
if (settings->bools.network_remote_enable_user[user])
2015-11-27 19:43:56 +00:00
{
2016-09-29 19:07:10 +00:00
#if defined(HAVE_NETWORKING) && defined(HAVE_NETWORKGAMEPAD)
2016-07-27 19:28:33 +00:00
struct remote_message msg;
2015-11-27 23:36:22 +00:00
ssize_t ret;
2015-11-27 19:43:56 +00:00
fd_set fds;
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);
2016-08-26 16:32:54 +00:00
ret = recvfrom(handle->net_fd[user], (char*)&msg,
sizeof(msg), 0, NULL, NULL);
2015-11-27 23:36:22 +00:00
if (ret == sizeof(msg))
input_remote_parse_packet(&msg, user);
2016-07-28 05:05:57 +00:00
else if ((ret != -1) || ((errno != EAGAIN) && (errno != ENOENT)))
2016-01-19 21:33:48 +00:00
#endif
{
2017-09-27 22:53:13 +00:00
input_state->buttons[user] = 0;
2017-09-08 03:30:42 +00:00
input_state->analog[0][user] = 0;
input_state->analog[1][user] = 0;
input_state->analog[2][user] = 0;
input_state->analog[3][user] = 0;
}
2015-11-27 19:43:56 +00:00
}
}
}