From fef4448ddbaf7a78180e29fd831d05891a437296 Mon Sep 17 00:00:00 2001 From: natinusala Date: Mon, 1 Oct 2018 19:51:56 +0200 Subject: [PATCH] libnx: added touch support --- .vscode/settings.json | 4 +- gfx/drivers_context/switch_ctx.c | 15 ++++++- input/drivers/switch_input.c | 69 ++++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index efae5a0931..148f4b83ea 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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, } \ No newline at end of file diff --git a/gfx/drivers_context/switch_ctx.c b/gfx/drivers_context/switch_ctx.c index 739f0b2524..8acd0d5b47 100644 --- a/gfx/drivers_context/switch_ctx.c +++ b/gfx/drivers_context/switch_ctx.c @@ -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, diff --git a/input/drivers/switch_input.c b/input/drivers/switch_input.c index 067e355a2f..8ed989afa6 100644 --- a/input/drivers/switch_input.c +++ b/input/drivers/switch_input.c @@ -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;