2020-05-07 20:21:22 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
2020-05-08 03:24:42 +02:00
|
|
|
// Analog camera movement by Pathétique (github.com/vrmiguel), y0shin and Mors
|
|
|
|
// Contribute or communicate bugs at github.com/vrmiguel/sm64-analog-camera
|
|
|
|
|
2020-05-07 20:21:22 +02:00
|
|
|
#include <ultra64.h>
|
|
|
|
|
|
|
|
#include "controller_api.h"
|
2020-05-15 20:38:35 +02:00
|
|
|
#include "controller_sdl.h"
|
2020-05-10 18:05:54 +02:00
|
|
|
#include "../configfile.h"
|
2020-06-02 23:10:42 +02:00
|
|
|
#include "../platform.h"
|
2020-06-07 20:05:26 +02:00
|
|
|
#include "../fs/fs.h"
|
2020-05-10 18:05:54 +02:00
|
|
|
|
2020-05-18 03:37:52 +02:00
|
|
|
#include "game/level_update.h"
|
|
|
|
|
2020-05-15 20:38:35 +02:00
|
|
|
// mouse buttons are also in the controller namespace (why), just offset 0x100
|
|
|
|
#define VK_OFS_SDL_MOUSE 0x0100
|
|
|
|
#define VK_BASE_SDL_MOUSE (VK_BASE_SDL_GAMEPAD + VK_OFS_SDL_MOUSE)
|
|
|
|
#define MAX_JOYBINDS 32
|
|
|
|
#define MAX_MOUSEBUTTONS 8 // arbitrary
|
|
|
|
|
2020-05-08 03:24:42 +02:00
|
|
|
extern int16_t rightx;
|
|
|
|
extern int16_t righty;
|
2020-05-10 10:47:38 +02:00
|
|
|
|
|
|
|
#ifdef BETTERCAMERA
|
2020-05-09 07:10:56 +02:00
|
|
|
int mouse_x;
|
|
|
|
int mouse_y;
|
|
|
|
|
|
|
|
extern u8 newcam_mouse;
|
2020-05-10 10:47:38 +02:00
|
|
|
#endif
|
2020-05-07 20:21:22 +02:00
|
|
|
|
|
|
|
static bool init_ok;
|
|
|
|
static SDL_GameController *sdl_cntrl;
|
2020-05-30 13:37:37 +02:00
|
|
|
static SDL_Haptic *sdl_haptic;
|
2020-05-15 20:38:35 +02:00
|
|
|
|
|
|
|
static u32 num_joy_binds = 0;
|
|
|
|
static u32 num_mouse_binds = 0;
|
|
|
|
static u32 joy_binds[MAX_JOYBINDS][2];
|
|
|
|
static u32 mouse_binds[MAX_JOYBINDS][2];
|
|
|
|
|
|
|
|
static bool joy_buttons[SDL_CONTROLLER_BUTTON_MAX ] = { false };
|
|
|
|
static u32 mouse_buttons = 0;
|
|
|
|
static u32 last_mouse = VK_INVALID;
|
|
|
|
static u32 last_joybutton = VK_INVALID;
|
|
|
|
|
|
|
|
static inline void controller_add_binds(const u32 mask, const u32 *btns) {
|
|
|
|
for (u32 i = 0; i < MAX_BINDS; ++i) {
|
|
|
|
if (btns[i] >= VK_BASE_SDL_GAMEPAD && btns[i] <= VK_BASE_SDL_GAMEPAD + VK_SIZE) {
|
|
|
|
if (btns[i] >= VK_BASE_SDL_MOUSE && num_joy_binds < MAX_JOYBINDS) {
|
|
|
|
mouse_binds[num_mouse_binds][0] = btns[i] - VK_BASE_SDL_MOUSE;
|
|
|
|
mouse_binds[num_mouse_binds][1] = mask;
|
|
|
|
++num_mouse_binds;
|
|
|
|
} else if (num_mouse_binds < MAX_JOYBINDS) {
|
|
|
|
joy_binds[num_joy_binds][0] = btns[i] - VK_BASE_SDL_GAMEPAD;
|
|
|
|
joy_binds[num_joy_binds][1] = mask;
|
|
|
|
++num_joy_binds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void controller_sdl_bind(void) {
|
|
|
|
bzero(joy_binds, sizeof(joy_binds));
|
|
|
|
bzero(mouse_binds, sizeof(mouse_binds));
|
|
|
|
num_joy_binds = 0;
|
|
|
|
num_mouse_binds = 0;
|
|
|
|
|
|
|
|
controller_add_binds(A_BUTTON, configKeyA);
|
|
|
|
controller_add_binds(B_BUTTON, configKeyB);
|
|
|
|
controller_add_binds(Z_TRIG, configKeyZ);
|
2020-05-31 23:03:34 +02:00
|
|
|
controller_add_binds(STICK_UP, configKeyStickUp);
|
|
|
|
controller_add_binds(STICK_LEFT, configKeyStickLeft);
|
|
|
|
controller_add_binds(STICK_DOWN, configKeyStickDown);
|
|
|
|
controller_add_binds(STICK_RIGHT, configKeyStickRight);
|
2020-05-15 20:38:35 +02:00
|
|
|
controller_add_binds(U_CBUTTONS, configKeyCUp);
|
|
|
|
controller_add_binds(L_CBUTTONS, configKeyCLeft);
|
|
|
|
controller_add_binds(D_CBUTTONS, configKeyCDown);
|
|
|
|
controller_add_binds(R_CBUTTONS, configKeyCRight);
|
|
|
|
controller_add_binds(L_TRIG, configKeyL);
|
|
|
|
controller_add_binds(R_TRIG, configKeyR);
|
|
|
|
controller_add_binds(START_BUTTON, configKeyStart);
|
|
|
|
}
|
|
|
|
|
2020-05-07 20:21:22 +02:00
|
|
|
static void controller_sdl_init(void) {
|
2020-05-30 13:37:37 +02:00
|
|
|
if (SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS | SDL_INIT_HAPTIC) != 0) {
|
2020-05-07 20:21:22 +02:00
|
|
|
fprintf(stderr, "SDL init error: %s\n", SDL_GetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-02 23:10:42 +02:00
|
|
|
// try loading an external gamecontroller mapping file
|
2020-06-07 20:05:26 +02:00
|
|
|
uint64_t gcsize = 0;
|
|
|
|
void *gcdata = fs_load_file("gamecontrollerdb.txt", &gcsize);
|
|
|
|
if (gcdata && gcsize) {
|
|
|
|
SDL_RWops *rw = SDL_RWFromConstMem(gcdata, gcsize);
|
|
|
|
if (rw) {
|
|
|
|
int nummaps = SDL_GameControllerAddMappingsFromRW(rw, SDL_TRUE);
|
|
|
|
if (nummaps >= 0)
|
|
|
|
printf("loaded %d controller mappings from 'gamecontrollerdb.txt'\n", nummaps);
|
|
|
|
}
|
|
|
|
free(gcdata);
|
2020-06-02 23:10:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 10:47:38 +02:00
|
|
|
#ifdef BETTERCAMERA
|
2020-05-09 07:10:56 +02:00
|
|
|
if (newcam_mouse == 1)
|
|
|
|
SDL_SetRelativeMouseMode(SDL_TRUE);
|
|
|
|
SDL_GetRelativeMouseState(&mouse_x, &mouse_y);
|
2020-05-10 10:47:38 +02:00
|
|
|
#endif
|
2020-05-09 07:10:56 +02:00
|
|
|
|
2020-05-15 20:38:35 +02:00
|
|
|
controller_sdl_bind();
|
|
|
|
|
2020-05-07 20:21:22 +02:00
|
|
|
init_ok = true;
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:39:57 +02:00
|
|
|
static SDL_Haptic *controller_sdl_init_haptics(const int joy) {
|
|
|
|
SDL_Haptic *hap = SDL_HapticOpen(joy);
|
|
|
|
if (!hap) return NULL;
|
|
|
|
|
|
|
|
if (SDL_HapticRumbleSupported(hap) != SDL_TRUE) {
|
|
|
|
SDL_HapticClose(hap);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SDL_HapticRumbleInit(hap) != 0) {
|
|
|
|
SDL_HapticClose(hap);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("controller %s has haptics support, rumble enabled\n", SDL_JoystickNameForIndex(joy));
|
|
|
|
return hap;
|
|
|
|
}
|
|
|
|
|
2020-05-07 20:21:22 +02:00
|
|
|
static void controller_sdl_read(OSContPad *pad) {
|
|
|
|
if (!init_ok) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-10 10:47:38 +02:00
|
|
|
#ifdef BETTERCAMERA
|
2020-05-18 03:37:52 +02:00
|
|
|
if (newcam_mouse == 1 && sCurrPlayMode != 2)
|
2020-05-09 07:10:56 +02:00
|
|
|
SDL_SetRelativeMouseMode(SDL_TRUE);
|
|
|
|
else
|
|
|
|
SDL_SetRelativeMouseMode(SDL_FALSE);
|
|
|
|
|
2020-05-15 20:38:35 +02:00
|
|
|
u32 mouse = SDL_GetRelativeMouseState(&mouse_x, &mouse_y);
|
|
|
|
|
|
|
|
for (u32 i = 0; i < num_mouse_binds; ++i)
|
|
|
|
if (mouse & SDL_BUTTON(mouse_binds[i][0]))
|
|
|
|
pad->button |= mouse_binds[i][1];
|
|
|
|
|
|
|
|
// remember buttons that changed from 0 to 1
|
|
|
|
last_mouse = (mouse_buttons ^ mouse) & mouse;
|
|
|
|
mouse_buttons = mouse;
|
2020-05-10 10:47:38 +02:00
|
|
|
#endif
|
2020-05-09 07:10:56 +02:00
|
|
|
|
2020-05-07 20:21:22 +02:00
|
|
|
SDL_GameControllerUpdate();
|
|
|
|
|
|
|
|
if (sdl_cntrl != NULL && !SDL_GameControllerGetAttached(sdl_cntrl)) {
|
2020-06-04 22:39:57 +02:00
|
|
|
SDL_HapticClose(sdl_haptic);
|
2020-05-07 20:21:22 +02:00
|
|
|
SDL_GameControllerClose(sdl_cntrl);
|
|
|
|
sdl_cntrl = NULL;
|
2020-06-04 22:39:57 +02:00
|
|
|
sdl_haptic = NULL;
|
2020-05-07 20:21:22 +02:00
|
|
|
}
|
2020-06-04 22:39:57 +02:00
|
|
|
|
2020-05-07 20:21:22 +02:00
|
|
|
if (sdl_cntrl == NULL) {
|
|
|
|
for (int i = 0; i < SDL_NumJoysticks(); i++) {
|
|
|
|
if (SDL_IsGameController(i)) {
|
|
|
|
sdl_cntrl = SDL_GameControllerOpen(i);
|
|
|
|
if (sdl_cntrl != NULL) {
|
2020-06-04 22:39:57 +02:00
|
|
|
sdl_haptic = controller_sdl_init_haptics(i);
|
2020-05-07 20:21:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sdl_cntrl == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 20:38:35 +02:00
|
|
|
for (u32 i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
|
|
|
|
const bool new = SDL_GameControllerGetButton(sdl_cntrl, i);
|
|
|
|
const bool pressed = !joy_buttons[i] && new;
|
|
|
|
joy_buttons[i] = new;
|
|
|
|
if (pressed) last_joybutton = i;
|
|
|
|
}
|
|
|
|
|
2020-05-31 23:03:34 +02:00
|
|
|
u32 buttons_down = 0;
|
2020-05-15 20:38:35 +02:00
|
|
|
for (u32 i = 0; i < num_joy_binds; ++i)
|
|
|
|
if (joy_buttons[joy_binds[i][0]])
|
2020-05-31 23:03:34 +02:00
|
|
|
buttons_down |= joy_binds[i][1];
|
|
|
|
|
|
|
|
pad->button |= buttons_down;
|
|
|
|
|
|
|
|
const u32 xstick = buttons_down & STICK_XMASK;
|
|
|
|
const u32 ystick = buttons_down & STICK_YMASK;
|
|
|
|
if (xstick == STICK_LEFT)
|
|
|
|
pad->stick_x = -128;
|
|
|
|
else if (xstick == STICK_RIGHT)
|
|
|
|
pad->stick_x = 127;
|
|
|
|
if (ystick == STICK_DOWN)
|
|
|
|
pad->stick_y = -128;
|
|
|
|
else if (ystick == STICK_UP)
|
|
|
|
pad->stick_y = 127;
|
2020-05-07 20:21:22 +02:00
|
|
|
|
|
|
|
int16_t leftx = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_LEFTX);
|
|
|
|
int16_t lefty = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_LEFTY);
|
2020-05-08 03:24:42 +02:00
|
|
|
rightx = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_RIGHTX);
|
|
|
|
righty = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_RIGHTY);
|
2020-05-07 20:21:22 +02:00
|
|
|
|
|
|
|
int16_t ltrig = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
|
|
|
|
int16_t rtrig = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
|
|
|
|
|
|
|
|
#ifdef TARGET_WEB
|
|
|
|
// Firefox has a bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1606562
|
|
|
|
// It sets down y to 32768.0f / 32767.0f, which is greater than the allowed 1.0f,
|
|
|
|
// which SDL then converts to a int16_t by multiplying by 32767.0f, which overflows into -32768.
|
|
|
|
// Maximum up will hence never become -32768 with the current version of SDL2,
|
|
|
|
// so this workaround should be safe in compliant browsers.
|
|
|
|
if (lefty == -32768) {
|
|
|
|
lefty = 32767;
|
|
|
|
}
|
|
|
|
if (righty == -32768) {
|
|
|
|
righty = 32767;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (rightx < -0x4000) pad->button |= L_CBUTTONS;
|
|
|
|
if (rightx > 0x4000) pad->button |= R_CBUTTONS;
|
|
|
|
if (righty < -0x4000) pad->button |= U_CBUTTONS;
|
|
|
|
if (righty > 0x4000) pad->button |= D_CBUTTONS;
|
|
|
|
|
|
|
|
if (ltrig > 30 * 256) pad->button |= Z_TRIG;
|
|
|
|
if (rtrig > 30 * 256) pad->button |= R_TRIG;
|
|
|
|
|
|
|
|
uint32_t magnitude_sq = (uint32_t)(leftx * leftx) + (uint32_t)(lefty * lefty);
|
2020-05-29 00:40:36 +02:00
|
|
|
uint32_t stickDeadzoneActual = configStickDeadzone * DEADZONE_STEP;
|
|
|
|
if (magnitude_sq > (uint32_t)(stickDeadzoneActual * stickDeadzoneActual)) {
|
2020-05-07 20:21:22 +02:00
|
|
|
pad->stick_x = leftx / 0x100;
|
|
|
|
int stick_y = -lefty / 0x100;
|
|
|
|
pad->stick_y = stick_y == 128 ? 127 : stick_y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:58:18 +02:00
|
|
|
static void controller_sdl_rumble_play(f32 strength, f32 length) {
|
2020-06-04 22:39:57 +02:00
|
|
|
if (sdl_haptic)
|
2020-06-04 22:58:18 +02:00
|
|
|
SDL_HapticRumblePlay(sdl_haptic, strength, (u32)(length * 1000.0f));
|
2020-06-04 22:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void controller_sdl_rumble_stop(void) {
|
|
|
|
if (sdl_haptic)
|
|
|
|
SDL_HapticRumbleStop(sdl_haptic);
|
|
|
|
}
|
|
|
|
|
2020-05-15 20:38:35 +02:00
|
|
|
static u32 controller_sdl_rawkey(void) {
|
|
|
|
if (last_joybutton != VK_INVALID) {
|
|
|
|
const u32 ret = last_joybutton;
|
|
|
|
last_joybutton = VK_INVALID;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_MOUSEBUTTONS; ++i) {
|
|
|
|
if (last_mouse & SDL_BUTTON(i)) {
|
|
|
|
const u32 ret = VK_OFS_SDL_MOUSE + i;
|
|
|
|
last_mouse = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return VK_INVALID;
|
|
|
|
}
|
|
|
|
|
2020-05-16 21:23:23 +02:00
|
|
|
static void controller_sdl_shutdown(void) {
|
|
|
|
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER)) {
|
|
|
|
if (sdl_cntrl) {
|
|
|
|
SDL_GameControllerClose(sdl_cntrl);
|
|
|
|
sdl_cntrl = NULL;
|
|
|
|
}
|
2020-05-30 13:37:37 +02:00
|
|
|
if (sdl_haptic) {
|
|
|
|
SDL_HapticClose(sdl_haptic);
|
|
|
|
sdl_haptic = NULL;
|
|
|
|
}
|
2020-05-16 21:23:23 +02:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
|
|
|
|
}
|
|
|
|
init_ok = false;
|
|
|
|
}
|
|
|
|
|
2020-05-07 20:21:22 +02:00
|
|
|
struct ControllerAPI controller_sdl = {
|
2020-05-15 20:38:35 +02:00
|
|
|
VK_BASE_SDL_GAMEPAD,
|
2020-05-07 20:21:22 +02:00
|
|
|
controller_sdl_init,
|
2020-05-15 20:38:35 +02:00
|
|
|
controller_sdl_read,
|
|
|
|
controller_sdl_rawkey,
|
2020-06-04 22:39:57 +02:00
|
|
|
controller_sdl_rumble_play,
|
|
|
|
controller_sdl_rumble_stop,
|
2020-05-15 20:38:35 +02:00
|
|
|
controller_sdl_bind,
|
2020-05-16 21:23:23 +02:00
|
|
|
controller_sdl_shutdown
|
2020-05-07 20:21:22 +02:00
|
|
|
};
|