#include "../../SDL_internal.h"
#if SDL_JOYSTICK_VITA
#include <psp2/types.h>
#include <psp2/ctrl.h>
#include <psp2/kernel/threadmgr.h>
#include <stdio.h>
#include <stdlib.h>
#include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h"
#include "SDL_events.h"
#include "SDL_error.h"
#include "SDL_thread.h"
#include "SDL_mutex.h"
#include "SDL_timer.h"
static SceCtrlData pad0 = { .lx = 0, .ly = 0, .rx = 0, .ry = 0, .lt = 0, .rt = 0, .buttons = 0 };
static SceCtrlData pad1 = { .lx = 0, .ly = 0, .rx = 0, .ry = 0, .lt = 0, .rt = 0, .buttons = 0 };
static SceCtrlData pad2 = { .lx = 0, .ly = 0, .rx = 0, .ry = 0, .lt = 0, .rt = 0, .buttons = 0 };
static SceCtrlData pad3 = { .lx = 0, .ly = 0, .rx = 0, .ry = 0, .lt = 0, .rt = 0, .buttons = 0 };
static int ext_port_map[4]= { 1, 2, 3, 4 };
static int SDL_numjoysticks = 1;
static const unsigned int ext_button_map[] = {
SCE_CTRL_TRIANGLE,
SCE_CTRL_CIRCLE,
SCE_CTRL_CROSS,
SCE_CTRL_SQUARE,
SCE_CTRL_L1,
SCE_CTRL_R1,
SCE_CTRL_DOWN,
SCE_CTRL_LEFT,
SCE_CTRL_UP,
SCE_CTRL_RIGHT,
SCE_CTRL_SELECT,
SCE_CTRL_START,
SCE_CTRL_L2,
SCE_CTRL_R2,
SCE_CTRL_L3,
SCE_CTRL_R3
};
static int analog_map[256];
typedef struct
{
int x;
int y;
} point;
static point a = { 0, 0 };
static point b = { 0, 0 };
static point c = { 128, 32767 };
static point d = { 128, 32767 };
static SDL_INLINE void lerp (point *dest, point *first, point *second, float t)
{
dest->x = first->x + (second->x - first->x) * t;
dest->y = first->y + (second->y - first->y) * t;
}
static int calc_bezier_y(float t)
{
point ab, bc, cd, abbc, bccd, dest;
lerp (&ab, &a, &b, t);
lerp (&bc, &b, &c, t);
lerp (&cd, &c, &d, t);
lerp (&abbc, &ab, &bc, t);
lerp (&bccd, &bc, &cd, t);
lerp (&dest, &abbc, &bccd, t);
return dest.y;
}
int VITA_JoystickInit(void)
{
int i;
SceCtrlPortInfo myPortInfo;
sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG_WIDE);
sceCtrlSetSamplingModeExt(SCE_CTRL_MODE_ANALOG_WIDE);
for (i = 0; i < 128; i++)
{
float t = (float)i/127.0f;
analog_map[i+128] = calc_bezier_y(t);
analog_map[127-i] = -1 * analog_map[i+128];
}
SDL_numjoysticks = 1;
SDL_PrivateJoystickAdded(0);
sceCtrlGetControllerPortInfo(&myPortInfo);
for (i=2; i<=4; i++)
{
if (myPortInfo.port[i]!=SCE_CTRL_TYPE_UNPAIRED)
{
SDL_PrivateJoystickAdded(SDL_numjoysticks);
SDL_numjoysticks++;
}
}
return SDL_numjoysticks;
}
int VITA_JoystickGetCount()
{
return SDL_numjoysticks;
}
void VITA_JoystickDetect()
{
}
SDL_JoystickID VITA_JoystickGetDeviceInstanceID(int device_index)
{
return device_index;
}
const char *VITA_JoystickGetDeviceName(int index)
{
if (index == 0)
return "PSVita Controller";
if (index == 1)
return "PSVita Controller";
if (index == 2)
return "PSVita Controller";
if (index == 3)
return "PSVita Controller";
SDL_SetError("No joystick available with that index");
return NULL;
}
const char *VITA_JoystickGetDevicePath(int index)
{
return NULL;
}
static int
VITA_JoystickGetDevicePlayerIndex(int device_index)
{
return -1;
}
static void
VITA_JoystickSetDevicePlayerIndex(int device_index, int player_index)
{
}
int VITA_JoystickOpen(SDL_Joystick *joystick, int device_index)
{
joystick->nbuttons = SDL_arraysize(ext_button_map);
joystick->naxes = 6;
joystick->nhats = 0;
joystick->instance_id = device_index;
return 0;
}
static void VITA_JoystickUpdate(SDL_Joystick *joystick)
{
int i;
unsigned int buttons;
unsigned int changed;
unsigned char lx, ly, rx, ry, lt, rt;
static unsigned int old_buttons[] = { 0, 0, 0, 0 };
static unsigned char old_lx[] = { 0, 0, 0, 0 };
static unsigned char old_ly[] = { 0, 0, 0, 0 };
static unsigned char old_rx[] = { 0, 0, 0, 0 };
static unsigned char old_ry[] = { 0, 0, 0, 0 };
static unsigned char old_lt[] = { 0, 0, 0, 0 };
static unsigned char old_rt[] = { 0, 0, 0, 0 };
SceCtrlData *pad = NULL;
int index = (int) SDL_JoystickInstanceID(joystick);
if (index == 0) pad = &pad0;
else if (index == 1) pad = &pad1;
else if (index == 2) pad = &pad2;
else if (index == 3) pad = &pad3;
else return;
if (index == 0) {
if (sceCtrlPeekBufferPositive2(ext_port_map[index], pad, 1) < 0) {
sceCtrlPeekBufferPositive2(0, pad, 1);
}
} else {
sceCtrlPeekBufferPositive2(ext_port_map[index], pad, 1);
}
buttons = pad->buttons;
lx = pad->lx;
ly = pad->ly;
rx = pad->rx;
ry = pad->ry;
lt = pad->lt;
rt = pad->rt;
if (old_lx[index] != lx) {
SDL_PrivateJoystickAxis(joystick, 0, analog_map[lx]);
old_lx[index] = lx;
}
if (old_ly[index] != ly) {
SDL_PrivateJoystickAxis(joystick, 1, analog_map[ly]);
old_ly[index] = ly;
}
if (old_rx[index] != rx) {
SDL_PrivateJoystickAxis(joystick, 2, analog_map[rx]);
old_rx[index] = rx;
}
if (old_ry[index] != ry) {
SDL_PrivateJoystickAxis(joystick, 3, analog_map[ry]);
old_ry[index] = ry;
}
if (old_lt[index] != lt) {
SDL_PrivateJoystickAxis(joystick, 4, analog_map[lt]);
old_lt[index] = lt;
}
if (old_rt[index] != rt) {
SDL_PrivateJoystickAxis(joystick, 5, analog_map[rt]);
old_rt[index] = rt;
}
changed = old_buttons[index] ^ buttons;
old_buttons[index] = buttons;
if (changed) {
for (i = 0; i < SDL_arraysize(ext_button_map); i++) {
if (changed & ext_button_map[i]) {
SDL_PrivateJoystickButton(
joystick, i,
(buttons & ext_button_map[i]) ?
SDL_PRESSED : SDL_RELEASED);
}
}
}
}
void VITA_JoystickClose(SDL_Joystick *joystick)
{
}
void VITA_JoystickQuit(void)
{
}
SDL_JoystickGUID VITA_JoystickGetDeviceGUID(int device_index)
{
const char *name = VITA_JoystickGetDeviceName(device_index);
return SDL_CreateJoystickGUIDForName(name);
}
static int
VITA_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
{
int index = (int) SDL_JoystickInstanceID(joystick);
SceCtrlActuator act;
SDL_zero(act);
act.small = high_frequency_rumble / 256;
act.large = low_frequency_rumble / 256;
if (sceCtrlSetActuator(ext_port_map[index], &act) < 0) {
return SDL_Unsupported();
}
return 0;
}
static int
VITA_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left, Uint16 right)
{
return SDL_Unsupported();
}
static Uint32
VITA_JoystickGetCapabilities(SDL_Joystick *joystick)
{
return SDL_JOYCAP_LED | SDL_JOYCAP_RUMBLE;
}
static int
VITA_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
int index = (int) SDL_JoystickInstanceID(joystick);
if (sceCtrlSetLightBar(ext_port_map[index], red, green, blue) < 0) {
return SDL_Unsupported();
}
return 0;
}
static int
VITA_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int
VITA_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{
return SDL_Unsupported();
}
static SDL_bool
VITA_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
{
return SDL_FALSE;
}
SDL_JoystickDriver SDL_VITA_JoystickDriver =
{
VITA_JoystickInit,
VITA_JoystickGetCount,
VITA_JoystickDetect,
VITA_JoystickGetDeviceName,
VITA_JoystickGetDevicePath,
VITA_JoystickGetDevicePlayerIndex,
VITA_JoystickSetDevicePlayerIndex,
VITA_JoystickGetDeviceGUID,
VITA_JoystickGetDeviceInstanceID,
VITA_JoystickOpen,
VITA_JoystickRumble,
VITA_JoystickRumbleTriggers,
VITA_JoystickGetCapabilities,
VITA_JoystickSetLED,
VITA_JoystickSendEffect,
VITA_JoystickSetSensorsEnabled,
VITA_JoystickUpdate,
VITA_JoystickClose,
VITA_JoystickQuit,
VITA_JoystickGetGamepadMapping,
};
#endif