sdl 2.0.8 update

This commit is contained in:
Tim 2018-05-09 23:09:05 +10:00
parent d6f6bc65a5
commit ec8f56b3b0
894 changed files with 66879 additions and 43299 deletions

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -34,7 +34,9 @@
#include "SDL_log.h"
#include "SDL_sysjoystick_c.h"
#include "../SDL_joystick_c.h"
#include "../../events/SDL_keyboard_c.h"
#include "../../core/android/SDL_android.h"
#include "../steam/SDL_steamcontroller.h"
#include "android/keycodes.h"
@ -77,10 +79,9 @@ static int instance_counter = 0;
static int
keycode_to_SDL(int keycode)
{
/* FIXME: If this function gets too unwiedly in the future, replace with a lookup table */
/* FIXME: If this function gets too unwieldy in the future, replace with a lookup table */
int button = 0;
switch(keycode)
{
switch (keycode) {
/* Some gamepad buttons (API 9) */
case AKEYCODE_BUTTON_A:
button = SDL_CONTROLLER_BUTTON_A;
@ -109,6 +110,7 @@ keycode_to_SDL(int keycode)
case AKEYCODE_BUTTON_START:
button = SDL_CONTROLLER_BUTTON_START;
break;
case AKEYCODE_BACK:
case AKEYCODE_BUTTON_SELECT:
button = SDL_CONTROLLER_BUTTON_BACK;
break;
@ -142,7 +144,9 @@ keycode_to_SDL(int keycode)
button = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
break;
case AKEYCODE_DPAD_CENTER:
button = SDL_CONTROLLER_BUTTON_MAX+4; /* Not supported by GameController */
/* This is handled better by applications as the A button */
/*button = SDL_CONTROLLER_BUTTON_MAX+4;*/ /* Not supported by GameController */
button = SDL_CONTROLLER_BUTTON_A;
break;
/* More gamepad buttons (API 12), these get mapped to 20...35*/
@ -167,7 +171,7 @@ keycode_to_SDL(int keycode)
default:
return -1;
break;
/* break; -Wunreachable-code-break */
}
/* This is here in case future generations, probably with six fingers per hand,
@ -175,7 +179,30 @@ keycode_to_SDL(int keycode)
*/
SDL_assert(button < ANDROID_MAX_NBUTTONS);
return button;
}
static SDL_Scancode
button_to_scancode(int button)
{
switch (button) {
case SDL_CONTROLLER_BUTTON_A:
return SDL_SCANCODE_RETURN;
case SDL_CONTROLLER_BUTTON_B:
return SDL_SCANCODE_ESCAPE;
case SDL_CONTROLLER_BUTTON_BACK:
return SDL_SCANCODE_ESCAPE;
case SDL_CONTROLLER_BUTTON_DPAD_UP:
return SDL_SCANCODE_UP;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
return SDL_SCANCODE_DOWN;
case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
return SDL_SCANCODE_LEFT;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
return SDL_SCANCODE_RIGHT;
}
/* Unsupported button */
return SDL_SCANCODE_UNKNOWN;
}
int
@ -187,6 +214,8 @@ Android_OnPadDown(int device_id, int keycode)
item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickButton(item->joystick, button , SDL_PRESSED);
} else {
SDL_SendKeyboardKey(SDL_PRESSED, button_to_scancode(button));
}
return 0;
}
@ -203,6 +232,8 @@ Android_OnPadUp(int device_id, int keycode)
item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
} else {
SDL_SendKeyboardKey(SDL_RELEASED, button_to_scancode(button));
}
return 0;
}
@ -216,7 +247,7 @@ Android_OnJoy(int device_id, int axis, float value)
/* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
SDL_joylist_item *item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value) );
SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value));
}
return 0;
@ -234,7 +265,7 @@ Android_OnHat(int device_id, int hat_id, int x, int y)
if (x >= -1 && x <=1 && y >= -1 && y <= 1) {
SDL_joylist_item *item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickHat(item->joystick, hat_id, position_map[y+1][x+1] );
SDL_PrivateJoystickHat(item->joystick, hat_id, position_map[y+1][x+1]);
}
return 0;
}
@ -244,18 +275,25 @@ Android_OnHat(int device_id, int hat_id, int x, int y)
int
Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
Android_AddJoystick(int device_id, const char *name, const char *desc, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
{
SDL_JoystickGUID guid;
SDL_joylist_item *item;
if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
/* Ignore devices that aren't actually controllers (e.g. remotes), they'll be handled as keyboard input */
if (naxes < 2 && nhats < 1) {
return -1;
}
}
if(JoystickByDeviceId(device_id) != NULL || name == NULL) {
if (JoystickByDeviceId(device_id) != NULL || name == NULL) {
return -1;
}
/* the GUID is just the first 16 chars of the name for now */
SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
SDL_zero(guid);
SDL_memcpy(&guid, desc, SDL_min(sizeof(guid), SDL_strlen(desc)));
item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
if (item == NULL) {
@ -266,7 +304,7 @@ Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer,
item->guid = guid;
item->device_id = device_id;
item->name = SDL_strdup(name);
if ( item->name == NULL ) {
if (item->name == NULL) {
SDL_free(item);
return -1;
}
@ -349,6 +387,79 @@ Android_RemoveJoystick(int device_id)
}
static SDL_bool SteamControllerConnectedCallback(const char *name, SDL_JoystickGUID guid, int *device_instance)
{
SDL_joylist_item *item;
item = (SDL_joylist_item *)SDL_calloc(1, sizeof (SDL_joylist_item));
if (item == NULL) {
return SDL_FALSE;
}
*device_instance = item->device_instance = instance_counter++;
item->device_id = -1;
item->name = SDL_strdup(name);
item->guid = guid;
SDL_GetSteamControllerInputs(&item->nbuttons,
&item->naxes,
&item->nhats);
item->m_bSteamController = SDL_TRUE;
if (SDL_joylist_tail == NULL) {
SDL_joylist = SDL_joylist_tail = item;
} else {
SDL_joylist_tail->next = item;
SDL_joylist_tail = item;
}
/* Need to increment the joystick count before we post the event */
++numjoysticks;
SDL_PrivateJoystickAdded(numjoysticks - 1);
return SDL_TRUE;
}
static void SteamControllerDisconnectedCallback(int device_instance)
{
SDL_joylist_item *item = SDL_joylist;
SDL_joylist_item *prev = NULL;
while (item != NULL) {
if (item->device_instance == device_instance) {
break;
}
prev = item;
item = item->next;
}
if (item == NULL) {
return;
}
if (item->joystick) {
item->joystick->hwdata = NULL;
}
if (prev != NULL) {
prev->next = item->next;
} else {
SDL_assert(SDL_joylist == item);
SDL_joylist = item->next;
}
if (item == SDL_joylist_tail) {
SDL_joylist_tail = prev;
}
/* Need to decrement the joystick count before we post the event */
--numjoysticks;
SDL_PrivateJoystickRemoved(item->device_instance);
SDL_free(item->name);
SDL_free(item);
}
int
SDL_SYS_JoystickInit(void)
{
@ -356,29 +467,36 @@ SDL_SYS_JoystickInit(void)
if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
/* Default behavior, accelerometer as joystick */
Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, SDL_TRUE, 0, 3, 0, 0);
Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, ANDROID_ACCELEROMETER_NAME, SDL_TRUE, 0, 3, 0, 0);
}
SDL_InitSteamControllers(SteamControllerConnectedCallback,
SteamControllerDisconnectedCallback);
return (numjoysticks);
}
int SDL_SYS_NumJoysticks()
int
SDL_SYS_NumJoysticks(void)
{
return numjoysticks;
}
void SDL_SYS_JoystickDetect()
void
SDL_SYS_JoystickDetect(void)
{
/* Support for device connect/disconnect is API >= 16 only,
* so we poll every three seconds
* Ref: http://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
*/
static Uint32 timeout = 0;
if (SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
if (!timeout || SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
timeout = SDL_GetTicks() + 3000;
Android_JNI_PollInputDevices();
}
SDL_UpdateSteamControllers();
}
static SDL_joylist_item *
@ -447,7 +565,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
{
SDL_joylist_item *item = JoystickByDevIndex(device_index);
if (item == NULL ) {
if (item == NULL) {
return SDL_SetError("No such device");
}
@ -475,30 +593,34 @@ SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{
int i;
Sint16 value;
float values[3];
SDL_joylist_item *item = SDL_joylist;
SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
while (item) {
if (item->is_accelerometer) {
if (item->joystick) {
if (Android_JNI_GetAccelerometerValues(values)) {
for ( i = 0; i < 3; i++ ) {
if (values[i] > 1.0f) {
values[i] = 1.0f;
} else if (values[i] < -1.0f) {
values[i] = -1.0f;
}
if (item == NULL) {
return;
}
if (item->m_bSteamController) {
SDL_UpdateSteamController(joystick);
return;
}
value = (Sint16)(values[i] * 32767.0f);
SDL_PrivateJoystickAxis(item->joystick, i, value);
}
if (item->is_accelerometer) {
int i;
Sint16 value;
float values[3];
if (Android_JNI_GetAccelerometerValues(values)) {
for (i = 0; i < 3; i++) {
if (values[i] > 1.0f) {
values[i] = 1.0f;
} else if (values[i] < -1.0f) {
values[i] = -1.0f;
}
value = (Sint16)(values[i] * 32767.0f);
SDL_PrivateJoystickAxis(item->joystick, i, value);
}
break;
}
item = item->next;
}
}
@ -516,6 +638,10 @@ SDL_SYS_JoystickClose(SDL_Joystick * joystick)
void
SDL_SYS_JoystickQuit(void)
{
/* We don't have any way to scan for joysticks at init, so don't wipe the list
* of joysticks here in case this is a reinit.
*/
#if 0
SDL_joylist_item *item = NULL;
SDL_joylist_item *next = NULL;
@ -529,9 +655,12 @@ SDL_SYS_JoystickQuit(void)
numjoysticks = 0;
instance_counter = 0;
#endif /* 0 */
SDL_QuitSteamControllers();
}
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index)
{
return JoystickByDevIndex(device_index)->guid;
}
@ -548,6 +677,11 @@ SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
return guid;
}
SDL_bool SDL_SYS_IsDPAD_DeviceIndex(int device_index)
{
return JoystickByDevIndex(device_index)->naxes == 0;
}
#endif /* SDL_JOYSTICK_ANDROID */
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -22,13 +22,17 @@
#include "../../SDL_internal.h"
#ifdef SDL_JOYSTICK_ANDROID
#ifndef SDL_sysjoystick_c_h_
#define SDL_sysjoystick_c_h_
#include "../SDL_sysjoystick.h"
extern int Android_OnPadDown(int device_id, int keycode);
extern int Android_OnPadUp(int device_id, int keycode);
extern int Android_OnJoy(int device_id, int axisnum, float value);
extern int Android_OnHat(int device_id, int hat_id, int x, int y);
extern int Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs);
extern int Android_AddJoystick(int device_id, const char *name, const char *desc, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs);
extern int Android_RemoveJoystick(int device_id);
/* A linked list of available joysticks */
@ -42,11 +46,16 @@ typedef struct SDL_joylist_item
SDL_Joystick *joystick;
int nbuttons, naxes, nhats, nballs;
/* Steam Controller support */
SDL_bool m_bSteamController;
struct SDL_joylist_item *next;
} SDL_joylist_item;
typedef SDL_joylist_item joystick_hwdata;
#endif /* SDL_sysjoystick_c_h_ */
#endif /* SDL_JOYSTICK_ANDROID */
/* vi: set ts=4 sw=4 expandtab: */