2011-01-06 17:34:11 +00:00
|
|
|
/* SSNES - A Super Ninteno Entertainment System (SNES) Emulator frontend for libsnes.
|
|
|
|
* Copyright (C) 2010 - Hans-Kristian Arntzen
|
|
|
|
*
|
|
|
|
* Some code herein may be based on code found in BSNES.
|
|
|
|
*
|
|
|
|
* SSNES 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.
|
|
|
|
*
|
|
|
|
* SSNES 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 SSNES.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "driver.h"
|
|
|
|
|
2011-01-07 17:11:06 +00:00
|
|
|
#include "SDL.h"
|
2011-01-06 17:34:11 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include "general.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <libsnes.hpp>
|
2011-01-07 17:11:06 +00:00
|
|
|
#include "ssnes_sdl_input.h"
|
2011-01-06 17:34:11 +00:00
|
|
|
|
|
|
|
static void* sdl_input_init(void)
|
|
|
|
{
|
|
|
|
sdl_input_t *sdl = calloc(1, sizeof(*sdl));
|
|
|
|
if (!sdl)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2011-01-08 18:28:49 +00:00
|
|
|
SDL_JoystickEventState(SDL_IGNORE);
|
2011-01-06 17:34:11 +00:00
|
|
|
sdl->num_joysticks = SDL_NumJoysticks();
|
2011-01-10 07:18:03 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < 2; i++)
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2011-01-10 07:18:03 +00:00
|
|
|
if (sdl->num_joysticks > g_settings.input.joypad_map[i])
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2011-01-10 07:18:03 +00:00
|
|
|
sdl->joysticks[i] = SDL_JoystickOpen(g_settings.input.joypad_map[i]);
|
|
|
|
if (!sdl->joysticks[i])
|
|
|
|
{
|
|
|
|
SSNES_ERR("Couldn't open SDL joystick #%u on SNES port %u\n", g_settings.input.joypad_map[i], i + 1);
|
|
|
|
free(sdl);
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-01-06 17:34:11 +00:00
|
|
|
|
2011-01-10 07:18:03 +00:00
|
|
|
SSNES_LOG("Opened Joystick: %s #%u on port %u\n",
|
|
|
|
SDL_JoystickName(g_settings.input.joypad_map[i]), g_settings.input.joypad_map[i], i + 1);
|
|
|
|
sdl->num_axes[i] = SDL_JoystickNumAxes(sdl->joysticks[i]);
|
|
|
|
sdl->num_buttons[i] = SDL_JoystickNumButtons(sdl->joysticks[i]);
|
|
|
|
sdl->num_hats[i] = SDL_JoystickNumHats(sdl->joysticks[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SSNES_WARN("Desired SDL joystick #%u on port %u, but SDL can only detect %u joysticks ...\n",
|
|
|
|
g_settings.input.joypad_map[i], i + 1, sdl->num_joysticks);
|
|
|
|
}
|
2011-01-06 17:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sdl;
|
|
|
|
}
|
|
|
|
|
2011-01-08 17:37:45 +00:00
|
|
|
|
2011-01-08 21:15:02 +00:00
|
|
|
static bool sdl_key_pressed(int key)
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
|
|
|
int num_keys;
|
|
|
|
Uint8 *keymap = SDL_GetKeyState(&num_keys);
|
|
|
|
|
|
|
|
if (key >= num_keys)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return keymap[key];
|
|
|
|
}
|
|
|
|
|
2011-01-08 21:15:02 +00:00
|
|
|
static bool sdl_joykey_pressed(sdl_input_t *sdl, int port_num, uint16_t joykey)
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2011-01-08 21:15:02 +00:00
|
|
|
// Check hat.
|
|
|
|
if (GET_HAT_DIR(joykey))
|
|
|
|
{
|
|
|
|
int hat = GET_HAT(joykey);
|
|
|
|
if (hat < sdl->num_hats[port_num])
|
|
|
|
{
|
|
|
|
Uint8 dir = SDL_JoystickGetHat(sdl->joysticks[port_num], hat);
|
|
|
|
switch (GET_HAT_DIR(joykey))
|
|
|
|
{
|
|
|
|
case HAT_UP_MASK:
|
2011-01-09 13:33:59 +00:00
|
|
|
if (dir & SDL_HAT_UP)
|
2011-01-08 21:15:02 +00:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case HAT_DOWN_MASK:
|
2011-01-09 13:33:59 +00:00
|
|
|
if (dir & SDL_HAT_DOWN)
|
2011-01-08 21:15:02 +00:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case HAT_LEFT_MASK:
|
2011-01-09 13:33:59 +00:00
|
|
|
if (dir & SDL_HAT_LEFT)
|
2011-01-08 21:15:02 +00:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case HAT_RIGHT_MASK:
|
2011-01-09 13:33:59 +00:00
|
|
|
if (dir & SDL_HAT_RIGHT)
|
2011-01-08 21:15:02 +00:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Check the button
|
|
|
|
{
|
|
|
|
if (joykey < sdl->num_buttons[port_num] && SDL_JoystickGetButton(sdl->joysticks[port_num], joykey))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-06 17:34:11 +00:00
|
|
|
|
2011-01-08 21:15:02 +00:00
|
|
|
static bool sdl_axis_pressed(sdl_input_t *sdl, int port_num, uint32_t joyaxis)
|
|
|
|
{
|
|
|
|
if (joyaxis != AXIS_NONE)
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2011-01-08 21:15:02 +00:00
|
|
|
if (AXIS_NEG_GET(joyaxis) < sdl->num_axes[port_num])
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2011-01-08 21:15:02 +00:00
|
|
|
Sint16 val = SDL_JoystickGetAxis(sdl->joysticks[port_num], AXIS_NEG_GET(joyaxis));
|
2011-01-06 17:34:11 +00:00
|
|
|
float scaled = (float)val / 0x8000;
|
|
|
|
if (scaled < -g_settings.input.axis_threshold)
|
|
|
|
return true;
|
|
|
|
}
|
2011-01-08 21:15:02 +00:00
|
|
|
if (AXIS_POS_GET(joyaxis) < sdl->num_axes[port_num])
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2011-01-08 21:15:02 +00:00
|
|
|
Sint16 val = SDL_JoystickGetAxis(sdl->joysticks[port_num], AXIS_POS_GET(joyaxis));
|
2011-01-06 17:34:11 +00:00
|
|
|
float scaled = (float)val / 0x8000;
|
|
|
|
if (scaled > g_settings.input.axis_threshold)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-08 21:15:02 +00:00
|
|
|
static bool sdl_is_pressed(sdl_input_t *sdl, int port_num, const struct snes_keybind *key)
|
|
|
|
{
|
|
|
|
if (sdl_key_pressed(key->key))
|
|
|
|
return true;
|
2011-01-10 07:18:03 +00:00
|
|
|
if (sdl->joysticks[port_num] == NULL)
|
2011-01-08 21:15:02 +00:00
|
|
|
return false;
|
|
|
|
if (sdl_joykey_pressed(sdl, port_num, key->joykey))
|
|
|
|
return true;
|
|
|
|
if (sdl_axis_pressed(sdl, port_num, key->joyaxis))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-08 17:37:45 +00:00
|
|
|
static bool sdl_bind_button_pressed(void *data, int key)
|
|
|
|
{
|
2011-01-08 21:15:02 +00:00
|
|
|
// Only let player 1 use special binds called from main loop.
|
2011-01-08 17:37:45 +00:00
|
|
|
const struct snes_keybind *binds = g_settings.input.binds[0];
|
|
|
|
for (int i = 0; binds[i].id != -1; i++)
|
|
|
|
{
|
|
|
|
if (binds[i].id == key)
|
|
|
|
return sdl_is_pressed(data, 0, &binds[i]);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-10 07:40:44 +00:00
|
|
|
static int16_t sdl_joypad_device_state(sdl_input_t *sdl, const struct snes_keybind **binds,
|
|
|
|
bool port, unsigned device, unsigned index, unsigned id)
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
|
|
|
const struct snes_keybind *snes_keybinds = binds[port == SNES_PORT_1 ? 0 : 1];
|
|
|
|
|
2011-01-08 17:37:45 +00:00
|
|
|
// Checks if button is pressed.
|
2011-01-06 17:34:11 +00:00
|
|
|
int port_num = port == SNES_PORT_1 ? 0 : 1;
|
|
|
|
for (int i = 0; snes_keybinds[i].id != -1; i++)
|
|
|
|
{
|
2011-01-08 17:37:45 +00:00
|
|
|
if (snes_keybinds[i].id == (int)id)
|
|
|
|
return sdl_is_pressed(sdl, port_num, &snes_keybinds[i]);
|
2011-01-06 17:34:11 +00:00
|
|
|
}
|
|
|
|
|
2011-01-08 17:37:45 +00:00
|
|
|
return false;
|
2011-01-06 17:34:11 +00:00
|
|
|
}
|
|
|
|
|
2011-01-10 07:40:44 +00:00
|
|
|
static int16_t sdl_mouse_device_state(sdl_input_t *sdl, const struct snes_keybind **binds,
|
|
|
|
bool port, unsigned device, unsigned index, unsigned id)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case SNES_DEVICE_ID_MOUSE_LEFT:
|
2011-01-10 13:29:00 +00:00
|
|
|
return sdl->mouse_l;
|
2011-01-10 07:40:44 +00:00
|
|
|
case SNES_DEVICE_ID_MOUSE_RIGHT:
|
2011-01-10 13:29:00 +00:00
|
|
|
return sdl->mouse_r;
|
2011-01-10 07:40:44 +00:00
|
|
|
case SNES_DEVICE_ID_MOUSE_X:
|
2011-01-10 13:29:00 +00:00
|
|
|
return sdl->mouse_x;
|
2011-01-10 07:40:44 +00:00
|
|
|
case SNES_DEVICE_ID_MOUSE_Y:
|
2011-01-10 13:29:00 +00:00
|
|
|
return sdl->mouse_y;
|
2011-01-10 07:40:44 +00:00
|
|
|
default:
|
2011-01-10 13:29:00 +00:00
|
|
|
return 0;
|
2011-01-10 07:40:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: :D
|
|
|
|
static int16_t sdl_scope_device_state(sdl_input_t *sdl, const struct snes_keybind **binds,
|
|
|
|
bool port, unsigned device, unsigned index, unsigned id)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int16_t sdl_input_state(void *data, const struct snes_keybind **binds, bool port, unsigned device, unsigned index, unsigned id)
|
|
|
|
{
|
|
|
|
switch (device)
|
|
|
|
{
|
|
|
|
case SNES_DEVICE_JOYPAD:
|
|
|
|
return sdl_joypad_device_state(data, binds, port, device, index, id);
|
|
|
|
case SNES_DEVICE_MOUSE:
|
|
|
|
return sdl_mouse_device_state(data, binds, port, device, index, id);
|
|
|
|
case SNES_DEVICE_SUPER_SCOPE:
|
|
|
|
return sdl_scope_device_state(data, binds, port, device, index, id);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 17:34:11 +00:00
|
|
|
static void sdl_input_free(void *data)
|
|
|
|
{
|
|
|
|
if (data)
|
|
|
|
{
|
2011-01-08 18:22:58 +00:00
|
|
|
// Flush out all pending events.
|
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_PollEvent(&event));
|
|
|
|
|
2011-01-06 17:34:11 +00:00
|
|
|
sdl_input_t *sdl = data;
|
2011-01-10 07:18:03 +00:00
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
if (sdl->joysticks[i])
|
|
|
|
SDL_JoystickClose(sdl->joysticks[i]);
|
|
|
|
}
|
2011-01-06 17:34:11 +00:00
|
|
|
|
|
|
|
free(data);
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-10 13:29:00 +00:00
|
|
|
static void sdl_poll_mouse(sdl_input_t *sdl)
|
|
|
|
{
|
|
|
|
int _x, _y;
|
|
|
|
Uint8 btn = SDL_GetRelativeMouseState(&_x, &_y);
|
|
|
|
sdl->mouse_x = _x;
|
|
|
|
sdl->mouse_y = _y;
|
|
|
|
sdl->mouse_l = SDL_BUTTON(1) & btn ? 1 : 0;
|
|
|
|
sdl->mouse_r = SDL_BUTTON(3) & btn ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2011-01-06 17:34:11 +00:00
|
|
|
static void sdl_input_poll(void *data)
|
|
|
|
{
|
|
|
|
SDL_PumpEvents();
|
|
|
|
SDL_Event event;
|
2011-01-08 18:28:49 +00:00
|
|
|
SDL_JoystickUpdate();
|
2011-01-10 13:29:00 +00:00
|
|
|
sdl_poll_mouse(data);
|
2011-01-06 17:34:11 +00:00
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
sdl_input_t *sdl = data;
|
|
|
|
// Search for events...
|
2011-01-06 17:34:11 +00:00
|
|
|
while (SDL_PollEvent(&event))
|
|
|
|
{
|
2011-01-06 19:01:32 +00:00
|
|
|
switch (event.type)
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
2011-01-06 19:01:32 +00:00
|
|
|
case SDL_QUIT:
|
|
|
|
if (sdl->quitting)
|
|
|
|
{
|
|
|
|
*sdl->quitting = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_VIDEORESIZE:
|
|
|
|
if (sdl->should_resize)
|
|
|
|
{
|
|
|
|
*sdl->new_width = event.resize.w;
|
|
|
|
*sdl->new_height = event.resize.h;
|
|
|
|
*sdl->should_resize = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2011-01-06 17:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const input_driver_t input_sdl = {
|
|
|
|
.init = sdl_input_init,
|
|
|
|
.poll = sdl_input_poll,
|
|
|
|
.input_state = sdl_input_state,
|
2011-01-08 17:37:45 +00:00
|
|
|
.key_pressed = sdl_bind_button_pressed,
|
2011-01-06 17:34:11 +00:00
|
|
|
.free = sdl_input_free,
|
|
|
|
.ident = "sdl"
|
|
|
|
};
|
|
|
|
|