2014-10-04 16:41:23 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2013-2014 - Jason Fetters
|
2015-01-07 16:46:50 +00:00
|
|
|
* Copyright (C) 2011-2015 - Daniel De Matteis
|
2015-04-07 03:03:03 +00:00
|
|
|
*
|
2014-10-04 16:41:23 +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/>.
|
|
|
|
*/
|
|
|
|
|
2015-04-02 15:49:32 +00:00
|
|
|
#include <string.h>
|
2014-10-04 16:41:23 +00:00
|
|
|
#include "joypad_connection.h"
|
2014-10-04 17:07:59 +00:00
|
|
|
|
2015-04-04 21:34:04 +00:00
|
|
|
int pad_connection_find_vacant_pad(joypad_connection_t *joyconn)
|
2014-10-04 17:07:59 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2015-04-04 21:44:02 +00:00
|
|
|
if (!joyconn)
|
|
|
|
return -1;
|
|
|
|
|
2015-01-05 00:58:00 +00:00
|
|
|
for (i = 0; i < MAX_USERS; i++)
|
2014-10-04 17:07:59 +00:00
|
|
|
{
|
2015-04-04 21:44:02 +00:00
|
|
|
joypad_connection_t *conn = &joyconn[i];
|
2015-02-15 02:59:57 +00:00
|
|
|
|
2015-02-15 02:54:38 +00:00
|
|
|
if (conn && !conn->connected)
|
2014-10-05 23:49:00 +00:00
|
|
|
return i;
|
2014-10-04 17:07:59 +00:00
|
|
|
}
|
2015-01-19 04:01:31 +00:00
|
|
|
|
2014-10-04 17:07:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-12 23:40:55 +00:00
|
|
|
joypad_connection_t *pad_connection_init(unsigned pads)
|
2014-10-04 20:32:14 +00:00
|
|
|
{
|
2015-06-25 15:25:09 +00:00
|
|
|
unsigned i;
|
2015-01-19 04:01:31 +00:00
|
|
|
joypad_connection_t *joyconn = (joypad_connection_t*)
|
2015-06-25 16:25:20 +00:00
|
|
|
calloc(pads, sizeof(joypad_connection_t));
|
2015-01-19 04:01:31 +00:00
|
|
|
|
|
|
|
if (!joyconn)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < pads; i++)
|
|
|
|
{
|
|
|
|
joypad_connection_t *conn = (joypad_connection_t*)&joyconn[i];
|
|
|
|
|
2015-02-15 02:59:57 +00:00
|
|
|
if (!conn)
|
|
|
|
continue;
|
|
|
|
|
2015-02-15 02:54:38 +00:00
|
|
|
conn->connected = false;
|
|
|
|
conn->iface = NULL;
|
|
|
|
conn->data = NULL;
|
2015-01-19 04:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return joyconn;
|
2014-10-04 20:32:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 05:59:52 +00:00
|
|
|
int32_t pad_connection_pad_init(joypad_connection_t *joyconn,
|
2015-04-07 03:03:03 +00:00
|
|
|
const char* name, uint16_t vid, uint16_t pid,
|
2015-04-03 21:30:39 +00:00
|
|
|
void *data, send_control_t ptr)
|
2014-10-04 17:07:59 +00:00
|
|
|
{
|
2015-04-02 15:49:32 +00:00
|
|
|
int pad = pad_connection_find_vacant_pad(joyconn);
|
2014-10-04 17:07:59 +00:00
|
|
|
|
2014-10-05 23:49:00 +00:00
|
|
|
if (pad != -1)
|
2014-10-04 17:07:59 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
2014-10-05 23:49:00 +00:00
|
|
|
joypad_connection_t* s = (joypad_connection_t*)&joyconn[pad];
|
2015-04-04 21:44:02 +00:00
|
|
|
|
2014-10-04 17:07:59 +00:00
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
const char* name;
|
2015-04-03 21:30:39 +00:00
|
|
|
uint16_t vid;
|
|
|
|
uint16_t pid;
|
2014-10-04 17:14:04 +00:00
|
|
|
pad_connection_interface_t *iface;
|
2015-04-07 03:03:03 +00:00
|
|
|
} pad_map[] =
|
2014-10-04 17:07:59 +00:00
|
|
|
{
|
2015-04-03 21:30:39 +00:00
|
|
|
{ "Nintendo RVL-CNT-01", 1406, 816, &pad_connection_wii },
|
2014-10-04 18:53:58 +00:00
|
|
|
#if 0
|
2015-04-03 21:30:39 +00:00
|
|
|
{ "Nintendo RVL-CNT-01-UC", 0, 0, &pad_connection_wii_u },
|
2014-10-04 18:53:58 +00:00
|
|
|
#endif
|
2015-04-03 21:30:39 +00:00
|
|
|
{ "Wireless Controller", 1356, 1476, &pad_connection_ps4 },
|
|
|
|
{ "PLAYSTATION(R)3 Controller", 1356, 616, &pad_connection_ps3 },
|
2014-10-04 17:07:59 +00:00
|
|
|
{ 0, 0}
|
|
|
|
};
|
|
|
|
|
2015-02-15 02:59:57 +00:00
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
for (i = 0; name && pad_map[i].name; i++)
|
2014-10-04 17:07:59 +00:00
|
|
|
{
|
2015-06-26 10:38:33 +00:00
|
|
|
const char *name_match = strstr(name, pad_map[i].name);
|
2015-04-04 14:42:38 +00:00
|
|
|
|
|
|
|
if (name_match || (pad_map[i].vid == vid && pad_map[i].pid == pid))
|
|
|
|
{
|
|
|
|
s->iface = pad_map[i].iface;
|
|
|
|
s->data = s->iface->init(data, pad, ptr);
|
|
|
|
s->connected = true;
|
|
|
|
|
|
|
|
return pad;
|
|
|
|
}
|
2014-10-04 17:07:59 +00:00
|
|
|
}
|
2015-02-15 02:59:57 +00:00
|
|
|
}
|
2014-10-04 17:07:59 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 03:03:03 +00:00
|
|
|
return -1;
|
2014-10-04 17:07:59 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 21:44:02 +00:00
|
|
|
void pad_connection_pad_deinit(joypad_connection_t *joyconn, uint32_t pad)
|
2014-10-04 17:07:59 +00:00
|
|
|
{
|
2015-04-04 21:44:02 +00:00
|
|
|
if (!joyconn || !joyconn->connected)
|
2014-10-05 23:49:00 +00:00
|
|
|
return;
|
2015-04-07 03:03:03 +00:00
|
|
|
|
2015-04-04 21:44:02 +00:00
|
|
|
if (joyconn->iface)
|
2015-01-19 04:01:31 +00:00
|
|
|
{
|
2015-04-04 21:44:02 +00:00
|
|
|
joyconn->iface->set_rumble(joyconn->data, RETRO_RUMBLE_STRONG, 0);
|
|
|
|
joyconn->iface->set_rumble(joyconn->data, RETRO_RUMBLE_WEAK, 0);
|
2015-02-15 02:59:57 +00:00
|
|
|
|
2015-04-04 21:44:02 +00:00
|
|
|
if (joyconn->iface->deinit)
|
|
|
|
joyconn->iface->deinit(joyconn->data);
|
2015-01-19 04:01:31 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 21:44:02 +00:00
|
|
|
joyconn->iface = NULL;
|
|
|
|
joyconn->connected = false;
|
2014-10-04 17:07:59 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 21:44:02 +00:00
|
|
|
void pad_connection_packet(joypad_connection_t *joyconn, uint32_t pad,
|
2014-10-04 17:07:59 +00:00
|
|
|
uint8_t* data, uint32_t length)
|
|
|
|
{
|
2015-04-07 03:21:28 +00:00
|
|
|
if (!joyconn || !joyconn->connected)
|
2014-10-05 23:49:00 +00:00
|
|
|
return;
|
2015-04-04 21:44:02 +00:00
|
|
|
if (joyconn->iface && joyconn->data && joyconn->iface->packet_handler)
|
|
|
|
joyconn->iface->packet_handler(joyconn->data, data, length);
|
2014-10-04 17:07:59 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 21:44:02 +00:00
|
|
|
uint64_t pad_connection_get_buttons(joypad_connection_t *joyconn, unsigned pad)
|
2014-10-04 18:53:58 +00:00
|
|
|
{
|
2015-04-04 21:44:02 +00:00
|
|
|
if (!joyconn->iface)
|
2015-01-19 04:01:31 +00:00
|
|
|
return 0;
|
2015-04-04 21:44:02 +00:00
|
|
|
return joyconn->iface->get_buttons(joyconn->data);
|
2014-10-04 18:53:58 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 21:44:02 +00:00
|
|
|
int16_t pad_connection_get_axis(joypad_connection_t *joyconn,
|
2014-10-20 18:32:41 +00:00
|
|
|
unsigned idx, unsigned i)
|
2014-10-04 18:53:58 +00:00
|
|
|
{
|
2015-04-04 21:44:02 +00:00
|
|
|
if (!joyconn->iface)
|
2015-01-19 04:01:31 +00:00
|
|
|
return 0;
|
2015-04-04 21:44:02 +00:00
|
|
|
return joyconn->iface->get_axis(joyconn->data, i);
|
2014-10-04 18:53:58 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 21:44:02 +00:00
|
|
|
bool pad_connection_has_interface(joypad_connection_t *joyconn, unsigned pad)
|
2014-10-04 17:07:59 +00:00
|
|
|
{
|
2015-04-20 10:12:32 +00:00
|
|
|
if (joyconn && pad < MAX_USERS && joyconn[pad].connected && joyconn[pad].iface)
|
2014-10-05 23:49:00 +00:00
|
|
|
return true;
|
2014-10-04 17:07:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-05 23:49:00 +00:00
|
|
|
void pad_connection_destroy(joypad_connection_t *joyconn)
|
2014-10-04 17:07:59 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2015-01-05 00:58:00 +00:00
|
|
|
for (i = 0; i < MAX_USERS; i ++)
|
2015-04-04 21:44:02 +00:00
|
|
|
pad_connection_pad_deinit(&joyconn[i], i);
|
2014-10-04 17:07:59 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 21:44:02 +00:00
|
|
|
bool pad_connection_rumble(joypad_connection_t *joyconn,
|
2014-10-05 23:49:00 +00:00
|
|
|
unsigned pad, enum retro_rumble_effect effect, uint16_t strength)
|
2014-10-04 17:07:59 +00:00
|
|
|
{
|
2015-04-04 21:44:02 +00:00
|
|
|
if (!joyconn->connected)
|
2015-01-19 04:01:31 +00:00
|
|
|
return false;
|
2015-04-04 21:44:02 +00:00
|
|
|
if (!joyconn->iface)
|
2015-01-19 04:01:31 +00:00
|
|
|
return false;
|
2015-04-04 21:44:02 +00:00
|
|
|
if (!joyconn->iface->set_rumble)
|
2015-01-19 04:05:41 +00:00
|
|
|
return false;
|
2015-01-19 04:01:31 +00:00
|
|
|
|
2015-04-04 21:44:02 +00:00
|
|
|
joyconn->iface->set_rumble(joyconn->data, effect, strength);
|
2015-01-19 04:01:31 +00:00
|
|
|
return true;
|
2014-10-04 17:07:59 +00:00
|
|
|
}
|