RetroArch/input/drivers/switch_input.c

414 lines
9.5 KiB
C
Raw Normal View History

2017-12-23 17:26:58 -08:00
#include <stdint.h>
#include <stdlib.h>
#include <boolean.h>
#include <libretro.h>
#include <retro_miscellaneous.h>
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#ifdef HAVE_LIBNX
#include <switch.h>
2018-10-24 17:23:22 +02:00
#define MULTITOUCH_LIMIT 4 /* supports up to this many fingers at once */
#define TOUCH_AXIS_MAX 0x7fff /* abstraction of pointer coords */
#define SWITCH_NUM_SCANCODES 114 /* size of rarch_key_map_switch */
#define SWITCH_MAX_SCANCODE 0xfb /* see https://switchbrew.github.io/libnx/hid_8h.html */
2019-03-19 09:22:16 -05:00
#define MOUSE_MAX_X 1920
#define MOUSE_MAX_Y 1080
#endif
2017-12-23 17:26:58 -08:00
#include "../input_driver.h"
2019-03-18 17:51:56 -05:00
#include "../input_keymaps.h"
2017-12-23 17:26:58 -08:00
#define MAX_PADS 10
2019-02-03 15:49:35 -08:00
/* TODO/FIXME -
* fix game focus toggle */
2017-12-29 00:44:41 +01:00
typedef struct switch_input
{
const input_device_driver_t *joypad;
bool blocked;
2018-10-01 19:51:56 +02:00
#ifdef HAVE_LIBNX
2018-10-03 18:50:36 +02:00
uint32_t touch_scale_x;
uint32_t touch_scale_y;
uint32_t touch_half_resolution_x;
uint32_t touch_half_resolution_y;
2018-10-24 17:23:22 +02:00
bool touch_state[MULTITOUCH_LIMIT];
uint32_t touch_x[MULTITOUCH_LIMIT];
uint32_t touch_y[MULTITOUCH_LIMIT];
bool keyboard_state[SWITCH_MAX_SCANCODE + 1];
2019-03-19 09:22:16 -05:00
int32_t mouse_x;
int32_t mouse_y;
int32_t mouse_x_delta;
int32_t mouse_y_delta;
int32_t mouse_wheel;
bool mouse_button_left;
bool mouse_button_right;
bool mouse_button_middle;
2018-10-01 19:51:56 +02:00
#endif
2017-12-23 17:26:58 -08:00
} switch_input_t;
2017-12-29 00:44:41 +01:00
static void switch_input_poll(void *data)
{
2018-10-01 19:51:56 +02:00
switch_input_t *sw = (switch_input_t*) data;
if (sw->joypad)
sw->joypad->poll();
#ifdef HAVE_LIBNX
uint32_t touch_count = hidTouchCount();
2019-03-19 09:22:16 -05:00
unsigned int i = 0;
2019-03-18 17:51:56 -05:00
int keySym = 0;
unsigned keyCode = 0;
uint16_t mod = 0;
2019-03-19 09:22:16 -05:00
MousePosition mouse_pos;
2017-12-23 17:26:58 -08:00
2019-03-18 17:51:56 -05:00
for (i = 0; i < MULTITOUCH_LIMIT; i++)
2018-10-01 19:51:56 +02:00
{
2018-10-24 17:23:22 +02:00
sw->touch_state[i] = touch_count > i;
if (sw->touch_state[i])
{
touchPosition touch_position;
hidTouchRead(&touch_position, i);
2018-10-01 19:51:56 +02:00
2018-10-24 17:23:22 +02:00
sw->touch_x[i] = touch_position.px;
sw->touch_y[i] = touch_position.py;
}
2018-10-01 19:51:56 +02:00
}
2019-03-18 17:51:56 -05:00
mod = 0;
if (hidKeyboardHeld(KBD_LEFTALT) || hidKeyboardHeld(KBD_RIGHTALT))
mod |= RETROKMOD_ALT;
if (hidKeyboardHeld(KBD_LEFTCTRL) || hidKeyboardHeld(KBD_RIGHTCTRL))
mod |= RETROKMOD_CTRL;
if (hidKeyboardHeld(KBD_LEFTSHIFT) || hidKeyboardHeld(KBD_RIGHTSHIFT))
mod |= RETROKMOD_SHIFT;
for (i = 0; i < SWITCH_NUM_SCANCODES; i++)
{
keySym = rarch_key_map_switch[i].sym;
keyCode = input_keymaps_translate_keysym_to_rk(keySym);
if (hidKeyboardHeld(keySym) && !(sw->keyboard_state[keySym]))
2019-03-18 17:51:56 -05:00
{
sw->keyboard_state[keySym] = true;
input_keyboard_event(true, keyCode, 0, mod, RETRO_DEVICE_KEYBOARD);
2019-03-18 17:51:56 -05:00
}
else if (!hidKeyboardHeld(keySym) && sw->keyboard_state[keySym])
2019-03-18 17:51:56 -05:00
{
sw->keyboard_state[keySym] = false;
input_keyboard_event(false, keyCode, 0, mod, RETRO_DEVICE_KEYBOARD);
2019-03-18 17:51:56 -05:00
}
}
2019-03-19 09:22:16 -05:00
if (hidMouseButtonsHeld() & MOUSE_LEFT)
{
sw->mouse_button_left = true;
}
else
{
sw->mouse_button_left = false;
}
if (hidMouseButtonsHeld() & MOUSE_RIGHT)
{
sw->mouse_button_right = true;
}
else
{
sw->mouse_button_right = false;
}
if (hidMouseButtonsHeld() & MOUSE_MIDDLE)
{
sw->mouse_button_middle = true;
}
else
{
sw->mouse_button_middle = false;
}
hidMouseRead(&mouse_pos);
sw->mouse_x_delta = mouse_pos.velocityX;
sw->mouse_y_delta = mouse_pos.velocityY;
sw->mouse_x += mouse_pos.velocityX;
sw->mouse_y += mouse_pos.velocityY;
if (sw->mouse_x < 0)
{
sw->mouse_x = 0;
}
else if (sw->mouse_x > MOUSE_MAX_X)
{
sw->mouse_x = MOUSE_MAX_X;
}
if (sw->mouse_y < 0)
{
sw->mouse_y = 0;
}
else if (sw->mouse_y > MOUSE_MAX_Y)
{
sw->mouse_y = MOUSE_MAX_Y;
}
sw->mouse_wheel = mouse_pos.scrollVelocityY;
2018-10-01 19:51:56 +02:00
#endif
2017-12-23 17:26:58 -08:00
}
2018-10-01 19:51:56 +02:00
#ifdef HAVE_LIBNX
2018-10-03 18:50:36 +02:00
void calc_touch_scaling(switch_input_t *sw, uint32_t x, uint32_t y, uint32_t axis_max)
2018-10-01 19:51:56 +02:00
{
2018-10-03 18:50:36 +02:00
sw->touch_half_resolution_x = x/2;
sw->touch_half_resolution_y = y/2;
2018-10-01 19:51:56 +02:00
2018-10-03 18:50:36 +02:00
sw->touch_scale_x = axis_max / sw->touch_half_resolution_x;
sw->touch_scale_y = axis_max / sw->touch_half_resolution_y;
2018-10-01 19:51:56 +02:00
}
2019-02-03 15:49:35 -08:00
static int16_t switch_pointer_device_state(switch_input_t *sw,
2018-10-01 19:51:56 +02:00
unsigned id, unsigned idx)
{
2018-10-24 17:23:22 +02:00
if (idx >= MULTITOUCH_LIMIT)
2018-10-01 19:51:56 +02:00
return 0;
switch (id)
{
case RETRO_DEVICE_ID_POINTER_PRESSED:
2018-10-24 17:23:22 +02:00
return sw->touch_state[idx];
2018-10-01 19:51:56 +02:00
case RETRO_DEVICE_ID_POINTER_X:
2018-10-24 17:23:22 +02:00
return ((sw->touch_x[idx] - sw->touch_half_resolution_x) * sw->touch_scale_x);
2018-10-01 19:51:56 +02:00
case RETRO_DEVICE_ID_POINTER_Y:
2018-10-24 17:23:22 +02:00
return ((sw->touch_y[idx] - sw->touch_half_resolution_y) * sw->touch_scale_y);
2018-10-01 19:51:56 +02:00
}
return 0;
}
2019-03-19 09:22:16 -05:00
static int16_t switch_input_mouse_state(switch_input_t *sw, unsigned id, bool screen)
{
int val = 0;
switch (id)
{
case RETRO_DEVICE_ID_MOUSE_LEFT:
val = sw->mouse_button_left;
break;
case RETRO_DEVICE_ID_MOUSE_RIGHT:
val = sw->mouse_button_right;
break;
case RETRO_DEVICE_ID_MOUSE_MIDDLE:
val = sw->mouse_button_middle;
break;
case RETRO_DEVICE_ID_MOUSE_X:
if (screen)
{
val = sw->mouse_x;
}
else
{
val = sw->mouse_x_delta;
sw->mouse_x_delta = 0; /* flush delta after it has been read */
}
break;
case RETRO_DEVICE_ID_MOUSE_Y:
if (screen)
{
val = sw->mouse_y;
}
else
{
val = sw->mouse_y_delta;
sw->mouse_y_delta = 0; /* flush delta after it has been read */
}
break;
case RETRO_DEVICE_ID_MOUSE_WHEELUP:
if (sw->mouse_wheel > 0)
{
val = sw->mouse_wheel;
sw->mouse_wheel = 0;
}
break;
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
if (sw->mouse_wheel < 0)
{
val = sw->mouse_wheel;
sw->mouse_wheel = 0;
}
break;
}
return val;
}
2018-10-01 19:51:56 +02:00
#endif
2017-12-23 17:26:58 -08:00
static int16_t switch_input_state(void *data,
2017-12-29 00:44:41 +01:00
rarch_joypad_info_t joypad_info,
const struct retro_keybind **binds,
unsigned port, unsigned device,
unsigned idx, unsigned id)
{
switch_input_t *sw = (switch_input_t*) data;
if (port > MAX_PADS-1)
return 0;
switch (device)
{
case RETRO_DEVICE_JOYPAD:
return input_joypad_pressed(sw->joypad,
joypad_info, port, binds[port], id);
break;
case RETRO_DEVICE_ANALOG:
if (binds[port])
return input_joypad_analog(sw->joypad,
joypad_info, port, idx, id, binds[port]);
break;
2018-10-01 19:51:56 +02:00
#ifdef HAVE_LIBNX
2019-03-18 17:51:56 -05:00
case RETRO_DEVICE_KEYBOARD:
return ((id < RETROK_LAST) && sw->keyboard_state[rarch_keysym_lut[(enum retro_key)id]]);
break;
2019-03-19 09:22:16 -05:00
case RETRO_DEVICE_MOUSE:
return switch_input_mouse_state(sw, id, false);
break;
case RARCH_DEVICE_MOUSE_SCREEN:
return switch_input_mouse_state(sw, id, true);
break;
2018-10-01 19:51:56 +02:00
case RETRO_DEVICE_POINTER:
case RARCH_DEVICE_POINTER_SCREEN:
return switch_pointer_device_state(sw, id, idx);
#endif
2017-12-29 00:44:41 +01:00
}
return 0;
2017-12-23 17:26:58 -08:00
}
2017-12-29 00:44:41 +01:00
static void switch_input_free_input(void *data)
{
switch_input_t *sw = (switch_input_t*) data;
2017-12-23 17:26:58 -08:00
2017-12-29 00:44:41 +01:00
if (sw && sw->joypad)
sw->joypad->destroy();
2017-12-23 17:26:58 -08:00
2017-12-29 00:44:41 +01:00
free(sw);
#ifdef HAVE_LIBNX
hidExit();
#endif
2017-12-23 17:26:58 -08:00
}
2017-12-29 00:44:41 +01:00
static void* switch_input_init(const char *joypad_driver)
{
switch_input_t *sw = (switch_input_t*) calloc(1, sizeof(*sw));
if (!sw)
return NULL;
2017-12-23 17:26:58 -08:00
#ifdef HAVE_LIBNX
hidInitialize();
#endif
2017-12-29 00:44:41 +01:00
sw->joypad = input_joypad_init_driver(joypad_driver, sw);
2017-12-23 17:26:58 -08:00
2018-10-03 18:50:36 +02:00
#ifdef HAVE_LIBNX
/*
Here we assume that the touch screen is always 1280x720
Call me back when a Nintendo Switch XL is out
*/
calc_touch_scaling(sw, 1280, 720, TOUCH_AXIS_MAX);
2019-03-18 17:51:56 -05:00
input_keymaps_init_keyboard_lut(rarch_key_map_switch);
2019-03-19 09:22:16 -05:00
unsigned int i;
for (i = 0; i <= SWITCH_MAX_SCANCODE; i++) {
2019-03-18 17:51:56 -05:00
sw->keyboard_state[i] = false;
}
2019-03-19 09:22:16 -05:00
sw->mouse_x = 0;
sw->mouse_y = 0;
2018-10-03 18:50:36 +02:00
#endif
2017-12-29 00:44:41 +01:00
return sw;
2017-12-23 17:26:58 -08:00
}
2017-12-29 00:44:41 +01:00
static uint64_t switch_input_get_capabilities(void *data)
{
(void) data;
2017-12-23 17:26:58 -08:00
2018-10-08 14:34:12 +02:00
uint64_t caps = (1 << RETRO_DEVICE_JOYPAD) | (1 << RETRO_DEVICE_ANALOG);
#ifdef HAVE_LIBNX
2019-03-19 09:22:16 -05:00
caps |= (1 << RETRO_DEVICE_POINTER) | (1 << RETRO_DEVICE_KEYBOARD) | (1 << RETRO_DEVICE_MOUSE);
2018-10-08 14:34:12 +02:00
#endif
return caps;
2017-12-23 17:26:58 -08:00
}
2017-12-29 00:44:41 +01:00
static const input_device_driver_t *switch_input_get_joypad_driver(void *data)
{
switch_input_t *sw = (switch_input_t*) data;
if (sw)
return sw->joypad;
return NULL;
2017-12-23 17:26:58 -08:00
}
2017-12-29 00:44:41 +01:00
static void switch_input_grab_mouse(void *data, bool state)
{
(void)data;
(void)state;
2017-12-23 17:26:58 -08:00
}
static bool switch_input_set_rumble(void *data, unsigned port,
2017-12-29 00:44:41 +01:00
enum retro_rumble_effect effect, uint16_t strength)
{
2019-03-05 21:13:02 -08:00
#ifdef HAVE_LIBNX
switch_input_t *sw = (switch_input_t*) data;
if (!sw)
return false;
return input_joypad_set_rumble(sw->joypad, port, effect, strength);
#else
return false;
#endif
2017-12-23 17:26:58 -08:00
}
2017-12-29 00:44:41 +01:00
static bool switch_input_keyboard_mapping_is_blocked(void *data)
{
switch_input_t *sw = (switch_input_t*) data;
if (!sw)
return false;
return sw->blocked;
2017-12-23 17:26:58 -08:00
}
2017-12-29 00:44:41 +01:00
static void switch_input_keyboard_mapping_set_block(void *data, bool value)
{
switch_input_t *sw = (switch_input_t*) data;
if (!sw)
return;
sw->blocked = value;
2017-12-23 17:26:58 -08:00
}
input_driver_t input_switch = {
switch_input_init,
switch_input_poll,
switch_input_state,
switch_input_free_input,
NULL,
NULL,
switch_input_get_capabilities,
"switch",
switch_input_grab_mouse,
NULL,
switch_input_set_rumble,
switch_input_get_joypad_driver,
NULL,
switch_input_keyboard_mapping_is_blocked,
switch_input_keyboard_mapping_set_block,
};