libnx: added touch support

This commit is contained in:
natinusala 2018-10-01 19:51:56 +02:00
parent ead5b2c0f7
commit fef4448ddb
3 changed files with 83 additions and 5 deletions

View File

@ -20,7 +20,9 @@
"xlocbuf": "c",
"xmemory0": "c",
"ios": "c",
"list": "c"
"list": "c",
"input_driver.h": "c",
"video_driver.h": "c"
},
"C_Cpp.dimInactiveRegions": false,
}

View File

@ -253,6 +253,19 @@ static float switch_ctx_get_refresh_rate(void *data)
return ctx_nx->refresh_rate;
}
bool switch_ctx_get_metrics(void *data,
enum display_metric_types type, float *value)
{
switch (type)
{
case DISPLAY_METRIC_DPI:
*value = 236.87; //FIXME: Don't hardcode this value
return true;
default:
return false;
}
}
const gfx_ctx_driver_t switch_ctx = {
switch_ctx_init,
switch_ctx_destroy,
@ -265,7 +278,7 @@ const gfx_ctx_driver_t switch_ctx = {
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
NULL, /* get_metrics */
switch_ctx_get_metrics,
NULL,
NULL, /* update_title */
switch_ctx_check_window,

View File

@ -24,16 +24,74 @@ typedef struct switch_input
{
const input_device_driver_t *joypad;
bool blocked;
#ifdef HAVE_LIBNX
uint32_t touch_x;
uint32_t touch_y;
bool touch_state;
#endif
} switch_input_t;
static void switch_input_poll(void *data)
{
switch_input_t *sw = (switch_input_t*) data;
switch_input_t *sw = (switch_input_t*) data;
if (sw->joypad)
sw->joypad->poll();
if (sw->joypad)
sw->joypad->poll();
#ifdef HAVE_LIBNX
uint32_t touch_count = hidTouchCount();
sw->touch_state = touch_count > 0;
if (sw->touch_state)
{
touchPosition touch_position;
hidTouchRead(&touch_position, 0);
sw->touch_x = touch_position.px;
sw->touch_y = touch_position.py;
}
#endif
}
#ifdef HAVE_LIBNX
static int16_t scale_touch(int16_t from_min, int16_t from_max,
int16_t to_min, int16_t to_max,
int16_t value)
{
int32_t from_range = from_max - from_min;
int32_t to_range = to_max - to_min;
return (((value - from_min) * to_range) / from_range) + to_min;
}
static int16_t switch_pointer_device_state(switch_input_t *sw,
unsigned id, unsigned idx)
{
/*
Here we assume that the touch screen is always 1280x720
Call me back when a Nintendo Switch XL is out
*/
if (idx != 0)
return 0;
switch (id)
{
case RETRO_DEVICE_ID_POINTER_PRESSED:
return sw->touch_state;
case RETRO_DEVICE_ID_POINTER_X:
return scale_touch(0, 1280, -0x7fff, 0x7fff, (uint16_t) sw->touch_x);
case RETRO_DEVICE_ID_POINTER_Y:
return scale_touch(0, 720, -0x7fff, 0x7fff, (uint16_t) sw->touch_y);
}
return 0;
}
#endif
static int16_t switch_input_state(void *data,
rarch_joypad_info_t joypad_info,
const struct retro_keybind **binds,
@ -56,6 +114,11 @@ static int16_t switch_input_state(void *data,
return input_joypad_analog(sw->joypad,
joypad_info, port, idx, id, binds[port]);
break;
#ifdef HAVE_LIBNX
case RETRO_DEVICE_POINTER:
case RARCH_DEVICE_POINTER_SCREEN:
return switch_pointer_device_state(sw, id, idx);
#endif
}
return 0;