taisei/src/gamepad.c

615 lines
14 KiB
C
Raw Normal View History

2012-08-15 02:41:21 +02:00
/*
* This software is licensed under the terms of the MIT-License
* See COPYING for further information.
2012-08-15 02:41:21 +02:00
* ---
2017-09-12 03:28:15 +02:00
* Copyright (c) 2011-2017, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2017, Andrei Alexeyev <akari@alienslab.net>.
2012-08-15 02:41:21 +02:00
*/
#include "gamepad.h"
#include "config.h"
#include "events.h"
2012-08-15 16:36:39 +02:00
#include "global.h"
2012-08-15 02:41:21 +02:00
static struct {
bool initialized;
int current_devnum;
SDL_GameController *device;
SDL_JoystickID instance;
2017-02-27 23:58:47 +01:00
signed char *axis;
struct {
int *id_map;
size_t count;
} devices;
2012-08-15 02:41:21 +02:00
} gamepad;
static int gamepad_load_mappings(const char *vpath, int warn_noexist) {
char *repr = vfs_repr(vpath, true);
char *errstr = NULL;
const char *const_errstr = NULL;
SDL_RWops *mappings = vfs_open(vpath, VFS_MODE_READ | VFS_MODE_SEEKABLE);
int num_loaded = -1;
LogLevel loglvl = LOG_WARN;
// Yay for gotos!
if(!mappings) {
if(!warn_noexist) {
VFSInfo vinfo = vfs_query(vpath);
if(!vinfo.error && !vinfo.exists && !vinfo.is_dir) {
loglvl = LOG_INFO;
const_errstr = errstr = strfmt("Custom mappings file '%s' does not exist (this is ok)", repr);
goto cleanup;
}
}
const_errstr = vfs_get_error();
goto cleanup;
}
if((num_loaded = SDL_GameControllerAddMappingsFromRW(mappings, true)) < 0) {
const_errstr = SDL_GetError();
}
cleanup:
if(const_errstr) {
log_custom(loglvl, "Couldn't load mappings: %s", const_errstr);
} else if(num_loaded >= 0) {
log_info("Loaded %i mappings from '%s'", num_loaded, repr);
}
free(repr);
free(errstr);
return num_loaded;
}
static void gamepad_load_all_mappings(void) {
gamepad_load_mappings("res/gamecontrollerdb.txt", true);
gamepad_load_mappings("storage/gamecontrollerdb.txt", false);
}
static const char* gamepad_devicename_unmapped(int idx) {
const char *name = SDL_GameControllerNameForIndex(idx);
if(!strcasecmp(name, "Xinput Controller")) {
// HACK: let's try to get a more descriptive name...
name = SDL_JoystickNameForIndex(idx);
}
return name;
}
const char* gamepad_devicename(int num) {
if(num < 0 || num >= gamepad.devices.count) {
return NULL;
}
return gamepad_devicename_unmapped(gamepad.devices.id_map[num]);
}
static void gamepad_update_device_list(void) {
int cnt = SDL_NumJoysticks();
log_info("Updating gamepad devices list");
free(gamepad.devices.id_map);
memset(&gamepad.devices, 0, sizeof(gamepad.devices));
if(!cnt) {
log_info("No joysticks attached");
2012-08-15 02:41:21 +02:00
return;
}
int *idmap_ptr = gamepad.devices.id_map = malloc(sizeof(int) * cnt);
for(int i = 0; i < cnt; ++i) {
SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(i);
char guid_str[33] = { 0 };
SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str));
if(!*guid_str) {
log_warn("Failed to read GUID of joystick at index %i: %s", i, SDL_GetError());
continue;
}
if(!SDL_IsGameController(i)) {
log_warn("Joystick at index %i (name: \"%s\"; guid: %s) is not recognized as a game controller by SDL. "
"Most likely it just doesn't have a mapping. See https://git.io/vdvdV for solutions",
i, SDL_JoystickNameForIndex(i), guid_str);
continue;
}
*idmap_ptr = i;
int num = (int)(uintmax_t)(idmap_ptr - gamepad.devices.id_map);
++idmap_ptr;
gamepad.devices.count = (uintptr_t)(idmap_ptr - gamepad.devices.id_map);
log_info("Found device '%s' (#%i): %s", guid_str, num, gamepad_devicename_unmapped(i));
}
if(!gamepad.devices.count) {
log_info("No usable devices");
return;
}
}
static int gamepad_find_device_by_guid(const char *guid_str, char *guid_out, size_t guid_out_sz, int *out_localdevnum) {
for(int i = 0; i < gamepad.devices.count; ++i) {
*guid_out = 0;
SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(gamepad.devices.id_map[i]);
SDL_JoystickGetGUIDString(guid, guid_out, guid_out_sz);
if(!strcasecmp(guid_str, guid_out) || !strcasecmp(guid_str, "default")) {
*out_localdevnum = i;
return gamepad.devices.id_map[i];
}
}
*out_localdevnum = -1;
return -1;
}
static int gamepad_find_device(char *guid_out, size_t guid_out_sz, int *out_localdevnum) {
int dev;
const char *want_guid = config_get_str(CONFIG_GAMEPAD_DEVICE);
dev = gamepad_find_device_by_guid(want_guid, guid_out, guid_out_sz, out_localdevnum);
if(dev >= 0) {
return dev;
}
if(strcasecmp(want_guid, "default")) {
log_warn("Requested device '%s' is not available, trying first usable", want_guid);
dev = gamepad_find_device_by_guid("default", guid_out, guid_out_sz, out_localdevnum);
if(dev >= 0) {
return dev;
}
}
*out_localdevnum = -1;
strcpy(guid_out, want_guid);
return -1;
}
static void gamepad_cfg_enabled_callback(ConfigIndex idx, ConfigValue v) {
config_set_int(idx, v.i);
if(v.i) {
gamepad_init();
} else {
gamepad_shutdown();
}
}
static void gamepad_setup_cfg_callbacks(void) {
static bool done = false;
if(done) {
return;
}
done = true;
config_set_callback(CONFIG_GAMEPAD_ENABLED, gamepad_cfg_enabled_callback);
}
void gamepad_init(void) {
gamepad_setup_cfg_callbacks();
if(!config_get_int(CONFIG_GAMEPAD_ENABLED) || gamepad.initialized) {
2012-08-16 23:35:48 +02:00
return;
}
if(SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) < 0) {
Implemented a simple and consistent logging subsystem The goal of this change is mainly to clean up Taisei's codebase and improve its console output. I've been frustrated by files littered with inconsistent printf/fprintf/warnx/errx calls for a long time, and now I actually did something about it. All the above functions are now considered deprecated and result in a compile-time warning when used. Instead, the following macros should be used: log_debug(format, ...) log_info(format, ...) log_warn(format, ...) log_err(format, ...) As you can see, all of them have the same printf-like interface. But they have different functionality and purpose: log_debug is intended for very verbose and specific information. It does nothing in release builds, much like assert(), so don't use expressions with side-effects in its arguments. log_info is for various status updates that are expected during normal operation of the program. log_warn is for non-critical failures or other things that may be worth investigating, but don't inherently render the program non-functional. log_err is for when the only choice is to give up. Like errx, it also terminates the program. Unlike errx, it actually calls abort(), which means the cleanup functions are not ran -- but on the other hand, you get a debuggable backtrace. However, if you're trying to catch programming errors, consider using assert() instead. All of them produce output that contains a timestamp, the log level identifier, the calling function's name, and the formatted message. The newline at the end of the format string is not required -- no, it is actually *prohibited*. The logging system will take care of the line breaks by itself, don't litter the code with that shit. Internally, the logging system is based on the SDL_RWops abstraction, and may have multiple, configurable destinations. This makes it easily extensible. Currently, log_debug and log_info are set to write to stdout, log_warn and log_err to stderr, and all of them also to the file log.txt in the Taisei config directory. Consequently, the nasty freopen hacks we used to make Taisei write to log files on Windows are no longer needed -- which is a very good thing, considering they probably would break if the configdir path contains UTF-8 characters. SDL_RWFromFile does not suffer this limitation. As an added bonus, it's also thread-safe. Note about printf and fprintf: in very few cases, the logging system is not a good substitute for these functions. That is, when you care about writing exactly to stdout/stderr and about exactly how the output looks. However, I insist on keeping the deprecation warnings on them to not tempt anyone to use them for logging/debugging out of habit and/or laziness. For this reason, I've added a tsfprintf function to util.c. It is functionally identical to fprintf, except it returns void. Yes, the name is deliberately ugly. Avoid using it if possible, but if you must, only use it to write to stdout or stderr. Do not write to actual files with it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
log_warn("SDL_InitSubSystem() failed: %s", SDL_GetError());
2012-08-15 02:41:21 +02:00
return;
}
gamepad_load_all_mappings();
gamepad_update_device_list();
char guid[33];
int dev = gamepad_find_device(guid, sizeof(guid), &gamepad.current_devnum);
if(dev < 0) {
log_warn("Device '%s' is not available", guid);
2012-08-15 02:41:21 +02:00
gamepad_shutdown();
return;
}
gamepad.device = SDL_GameControllerOpen(dev);
2012-08-15 02:41:21 +02:00
if(!gamepad.device) {
log_warn("Failed to open device '%s' (#%i): %s", guid, dev, SDL_GetError());
2012-08-15 02:41:21 +02:00
gamepad_shutdown();
return;
}
SDL_Joystick *joy = SDL_GameControllerGetJoystick(gamepad.device);
gamepad.instance = SDL_JoystickInstanceID(joy);
gamepad.axis = malloc(max(SDL_JoystickNumAxes(joy), 1));
2017-02-27 23:58:47 +01:00
log_info("Using device '%s' (#%i): %s", guid, dev, gamepad_devicename(dev));
SDL_GameControllerEventState(SDL_ENABLE);
config_set_str(CONFIG_GAMEPAD_DEVICE, guid);
gamepad.initialized = true;
2012-08-15 02:41:21 +02:00
}
void gamepad_shutdown(void) {
if(!gamepad.initialized) {
2012-08-17 14:59:06 +02:00
return;
}
log_info("Disabled the gamepad subsystem");
if(gamepad.device) {
SDL_GameControllerClose(gamepad.device);
}
2017-02-27 23:58:47 +01:00
free(gamepad.axis);
free(gamepad.devices.id_map);
2017-02-27 23:58:47 +01:00
SDL_GameControllerEventState(SDL_IGNORE);
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
memset(&gamepad, 0, sizeof(gamepad));
2012-08-15 02:41:21 +02:00
}
void gamepad_restart(void) {
gamepad_shutdown();
gamepad_init();
}
int gamepad_axis2gamekey(SDL_GameControllerAxis id, int val) {
2017-03-06 16:32:51 +01:00
val *= sign(gamepad_axis_sens(id));
if(!val) {
return -1;
}
2017-02-17 17:03:49 +01:00
if(id == config_get_int(CONFIG_GAMEPAD_AXIS_LR)) {
2012-08-15 14:33:32 +02:00
return val == AXISVAL_LEFT ? KEY_LEFT : KEY_RIGHT;
}
2017-02-17 17:03:49 +01:00
if(id == config_get_int(CONFIG_GAMEPAD_AXIS_UD)) {
2012-08-15 14:33:32 +02:00
return val == AXISVAL_UP ? KEY_UP : KEY_DOWN;
}
2012-08-15 14:33:32 +02:00
return -1;
2012-08-15 02:41:21 +02:00
}
SDL_GameControllerAxis gamepad_gamekey2axis(KeyIndex key) {
switch(key) {
case KEY_UP: case KEY_DOWN: return config_get_int(CONFIG_GAMEPAD_AXIS_UD);
case KEY_LEFT: case KEY_RIGHT: return config_get_int(CONFIG_GAMEPAD_AXIS_LR);
default: return SDL_CONTROLLER_AXIS_INVALID;
}
}
int gamepad_gamekey2axisval(KeyIndex key) {
switch(key) {
case KEY_UP: return AXISVAL_UP;
case KEY_DOWN: return AXISVAL_DOWN;
case KEY_LEFT: return AXISVAL_LEFT;
case KEY_RIGHT: return AXISVAL_RIGHT;
default: return AXISVAL_NULL;
}
}
int gamepad_axis2menuevt(SDL_GameControllerAxis id, int val) {
if(id == SDL_CONTROLLER_AXIS_LEFTX || id == SDL_CONTROLLER_AXIS_RIGHTX)
2012-08-15 14:33:32 +02:00
return val == AXISVAL_LEFT ? E_CursorLeft : E_CursorRight;
if(id == SDL_CONTROLLER_AXIS_LEFTY || id == SDL_CONTROLLER_AXIS_RIGHTY)
2012-08-15 14:33:32 +02:00
return val == AXISVAL_UP ? E_CursorUp : E_CursorDown;
2012-08-15 14:33:32 +02:00
return -1;
2012-08-15 02:41:21 +02:00
}
int gamepad_axis2gameevt(SDL_GameControllerAxis id) {
2017-02-17 17:03:49 +01:00
if(id == config_get_int(CONFIG_GAMEPAD_AXIS_LR))
2012-08-15 16:36:39 +02:00
return E_PlrAxisLR;
2017-02-17 17:03:49 +01:00
if(id == config_get_int(CONFIG_GAMEPAD_AXIS_UD))
2012-08-15 16:36:39 +02:00
return E_PlrAxisUD;
2012-08-15 16:36:39 +02:00
return -1;
}
float gamepad_axis_sens(SDL_GameControllerAxis id) {
2017-02-17 17:03:49 +01:00
if(id == config_get_int(CONFIG_GAMEPAD_AXIS_LR))
return config_get_float(CONFIG_GAMEPAD_AXIS_LR_SENS);
2017-02-17 17:03:49 +01:00
if(id == config_get_int(CONFIG_GAMEPAD_AXIS_UD))
return config_get_float(CONFIG_GAMEPAD_AXIS_UD_SENS);
return 1.0;
}
static int gamepad_axis_process_value_deadzone(int raw) {
int val, vsign;
float deadzone = clamp(config_get_float(CONFIG_GAMEPAD_AXIS_DEADZONE), 0.01, 0.999);
int minval = clamp(deadzone, 0, 1) * GAMEPAD_AXIS_MAX;
val = raw;
vsign = sign(val);
val = abs(val);
if(val < minval) {
val = 0;
} else {
val = vsign * clamp((val - minval) / (1.0 - deadzone), 0, GAMEPAD_AXIS_MAX);
}
return val;
}
static int gamepad_axis_process_value(SDL_GameControllerAxis id, int raw) {
double sens = gamepad_axis_sens(id);
int sens_sign = sign(sens);
raw = gamepad_axis_process_value_deadzone(raw);
double x = raw / (double)GAMEPAD_AXIS_MAX;
int in_sign = sign(x);
x = pow(fabs(x), 1.0 / fabs(sens)) * in_sign * sens_sign;
x = x ? x : 0;
x = clamp(x * GAMEPAD_AXIS_MAX, GAMEPAD_AXIS_MIN, GAMEPAD_AXIS_MAX);
return (int)x;
}
int gamepad_get_player_axis_value(GamepadPlrAxis paxis) {
SDL_GameControllerAxis id;
if(!gamepad.initialized) {
return 0;
}
if(paxis == PLRAXIS_LR) {
id = config_get_int(CONFIG_GAMEPAD_AXIS_LR);
} else if(paxis == PLRAXIS_UD) {
id = config_get_int(CONFIG_GAMEPAD_AXIS_UD);
} else {
return INT_MAX;
}
return gamepad_axis_process_value(id, SDL_GameControllerGetAxis(gamepad.device, id));
}
void gamepad_axis(SDL_GameControllerAxis id, int raw, EventHandler handler, EventFlags flags, void *arg) {
2017-02-27 23:58:47 +01:00
signed char *a = gamepad.axis;
signed char val = AXISVAL(gamepad_axis_process_value_deadzone(raw));
2017-02-27 23:58:47 +01:00
bool free = config_get_int(CONFIG_GAMEPAD_AXIS_FREE);
2017-02-27 23:58:47 +01:00
bool menu = flags & EF_Menu;
bool game = flags & EF_Game;
bool gp = flags & EF_Gamepad;
2012-08-15 16:36:39 +02:00
if(game && free) {
int evt = gamepad_axis2gameevt(id);
if(evt >= 0) {
handler(evt, gamepad_axis_process_value(id, raw), arg);
}
2012-08-15 16:36:39 +02:00
}
2012-08-15 02:41:21 +02:00
if(val) { // simulate press
if(!a[id]) {
a[id] = val;
2012-08-15 16:36:39 +02:00
if(game && !free) {
2012-08-15 14:33:32 +02:00
int key = gamepad_axis2gamekey(id, val);
if(key >= 0) {
2012-08-15 14:33:32 +02:00
handler(E_PlrKeyDown, key, arg);
}
2012-08-15 14:33:32 +02:00
}
2012-08-15 14:33:32 +02:00
if(menu) {
int evt = gamepad_axis2menuevt(id, val);
if(evt >= 0) {
2012-08-15 14:33:32 +02:00
handler(evt, 0, arg);
}
2012-08-15 14:33:32 +02:00
}
2012-08-15 02:41:21 +02:00
}
} else if(a[id]) { // simulate release
2012-08-15 14:33:32 +02:00
if(game) {
int key = gamepad_axis2gamekey(id, a[id]);
handler(E_PlrKeyUp, key, arg);
}
2012-08-15 02:41:21 +02:00
a[id] = AXISVAL_NULL;
}
2017-02-27 23:58:47 +01:00
if(gp) {
// we probably need a better way to pass more than an int to the handler...
handler(E_GamepadAxis, id, arg);
handler(E_GamepadAxisValue, raw, arg);
}
2012-08-15 02:41:21 +02:00
}
void gamepad_button(SDL_GameControllerButton button, int state, EventHandler handler, EventFlags flags, void *arg) {
2012-08-15 02:41:21 +02:00
int menu = flags & EF_Menu;
int game = flags & EF_Game;
int gpad = flags & EF_Gamepad;
2017-02-17 17:03:49 +01:00
int gpkey = config_gamepad_key_from_gamepad_button(button);
int key = config_key_from_gamepad_key(gpkey);
2012-08-15 02:41:21 +02:00
if(state == SDL_PRESSED) {
if(game) switch(button) {
case SDL_CONTROLLER_BUTTON_START:
case SDL_CONTROLLER_BUTTON_BACK:
handler(E_Pause, 0, arg); break;
default:
if(key >= 0) {
handler(E_PlrKeyDown, key, arg);
} break;
2012-08-15 02:41:21 +02:00
}
if(menu) switch(button) {
case SDL_CONTROLLER_BUTTON_DPAD_UP: handler(E_CursorUp, 0, arg); break;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN: handler(E_CursorDown, 0, arg); break;
case SDL_CONTROLLER_BUTTON_DPAD_LEFT: handler(E_CursorLeft, 0, arg); break;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: handler(E_CursorRight, 0, arg); break;
case SDL_CONTROLLER_BUTTON_A:
case SDL_CONTROLLER_BUTTON_START:
handler(E_MenuAccept, 0, arg); break;
case SDL_CONTROLLER_BUTTON_B:
case SDL_CONTROLLER_BUTTON_BACK:
handler(E_MenuAbort, 0, arg); break;
default: break;
2012-08-15 02:41:21 +02:00
}
if(gpad) {
handler(E_GamepadKeyDown, button, arg);
}
} else {
if(game && key >= 0) {
handler(E_PlrKeyUp, key, arg);
}
if(gpad) {
handler(E_GamepadKeyUp, button, arg);
}
}
2012-08-15 02:41:21 +02:00
}
void gamepad_event(SDL_Event *event, EventHandler handler, EventFlags flags, void *arg) {
if(!gamepad.initialized)
return;
2012-08-15 02:41:21 +02:00
switch(event->type) {
case SDL_CONTROLLERAXISMOTION:
if(event->caxis.which == gamepad.instance) {
gamepad_axis(event->caxis.axis, event->caxis.value, handler, flags, arg);
2017-03-18 05:41:19 +01:00
}
2012-08-15 02:41:21 +02:00
break;
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
if(event->cbutton.which == gamepad.instance) {
gamepad_button(event->cbutton.button, event->cbutton.state, handler, flags, arg);
}
2012-08-15 02:41:21 +02:00
break;
}
}
2012-08-16 23:35:48 +02:00
int gamepad_devicecount(void) {
return gamepad.devices.count;
2012-08-16 23:35:48 +02:00
}
2017-09-16 08:44:39 +02:00
void gamepad_deviceguid(int num, char *guid_str, size_t guid_str_sz) {
if(num < 0 || num >= gamepad.devices.count) {
return;
}
SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(gamepad.devices.id_map[num]);
SDL_JoystickGetGUIDString(guid, guid_str, guid_str_sz);
}
int gamepad_numfromguid(const char *guid_str) {
for(int i = 0; i < gamepad.devices.count; ++i) {
char guid[33] = {0};
gamepad_deviceguid(i, guid, sizeof(guid));
if(!strcasecmp(guid_str, guid)) {
return i;
}
}
return -1;
}
int gamepad_currentdevice(void) {
if(gamepad.initialized) {
return gamepad.current_devnum;
}
return -1;
2012-08-16 23:35:48 +02:00
}
bool gamepad_buttonpressed(SDL_GameControllerButton btn) {
return SDL_GameControllerGetButton(gamepad.device, btn);
}
2017-02-17 17:03:49 +01:00
bool gamepad_gamekeypressed(KeyIndex key) {
if(!gamepad.initialized)
2017-02-17 17:03:49 +01:00
return false;
if(!config_get_int(CONFIG_GAMEPAD_AXIS_FREE)) {
SDL_GameControllerAxis axis = gamepad_gamekey2axis(key);
if(axis != SDL_CONTROLLER_AXIS_INVALID && gamepad.axis[axis] == gamepad_gamekey2axisval(key)) {
return true;
}
}
2017-02-17 17:03:49 +01:00
int gpkey = config_gamepad_key_from_key(key);
2017-02-27 23:04:30 +01:00
if(gpkey < 0) {
return false;
}
2017-02-17 17:03:49 +01:00
int cfgidx = GPKEYIDX_TO_CFGIDX(gpkey);
int button = config_get_int(cfgidx);
2017-02-17 17:03:49 +01:00
return gamepad_buttonpressed(button);
}
const char* gamepad_button_name(SDL_GameControllerButton btn) {
static const char *const map[] = {
"A",
"B",
"X",
"Y",
"Back",
"Guide",
"Start",
"Left Stick",
"Right Stick",
"Left Bumper",
"Right Bumper",
"Up",
"Down",
"Left",
"Right",
};
if(btn > SDL_CONTROLLER_BUTTON_INVALID && btn < SDL_CONTROLLER_BUTTON_MAX) {
return map[btn];
}
return "Unknown";
}
const char* gamepad_axis_name(SDL_GameControllerAxis axis) {
static const char *const map[] = {
"Left X",
"Left Y",
"Right X",
"Right Y",
"Left Trigger",
"Right Trigger",
};
if(axis > SDL_CONTROLLER_AXIS_INVALID && axis < SDL_CONTROLLER_AXIS_MAX) {
return map[axis];
}
return "Unknown";
2012-08-16 23:35:48 +02:00
}