From f6c8a0c5c2eeb862a388a90f73851886b4d539c2 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Fri, 28 Dec 2018 12:29:04 +0100 Subject: [PATCH] Add UWP input driver Keyboard, mouse and touch support: done --- configuration.c | 7 +- gfx/common/d3d_common.c | 16 +++- griffin/griffin.c | 1 + input/drivers/uwp_input.c | 174 ++++++++++++++++++++++++++++++++++++++ input/input_driver.c | 3 + input/input_driver.h | 1 + uwp/uwp_func.h | 7 ++ uwp/uwp_main.cpp | 146 ++++++++++++++++++++++++++++---- uwp/uwp_main.h | 7 +- 9 files changed, 340 insertions(+), 22 deletions(-) create mode 100644 input/drivers/uwp_input.c diff --git a/configuration.c b/configuration.c index 7ab65f20d9..880caedd27 100644 --- a/configuration.c +++ b/configuration.c @@ -227,6 +227,7 @@ enum input_driver_enum INPUT_WII, INPUT_WIIU, INPUT_XINPUT, + INPUT_UWP, INPUT_UDEV, INPUT_LINUXRAW, INPUT_COCOA, @@ -437,7 +438,9 @@ static enum midi_driver_enum MIDI_DEFAULT_DRIVER = MIDI_ALSA; static enum midi_driver_enum MIDI_DEFAULT_DRIVER = MIDI_NULL; #endif -#if defined(XENON) +#ifdef __WINRT__ +static enum input_driver_enum INPUT_DEFAULT_DRIVER = INPUT_UWP; +#elif defined(XENON) static enum input_driver_enum INPUT_DEFAULT_DRIVER = INPUT_XENON360; #elif defined(_XBOX360) || defined(_XBOX) || defined(HAVE_XINPUT2) || defined(HAVE_XINPUT_XBOX1) static enum input_driver_enum INPUT_DEFAULT_DRIVER = INPUT_XINPUT; @@ -867,6 +870,8 @@ const char *config_get_default_input(void) return "xenon360"; case INPUT_XINPUT: return "xinput"; + case INPUT_UWP: + return "uwp"; case INPUT_WII: return "gx"; case INPUT_WIIU: diff --git a/gfx/common/d3d_common.c b/gfx/common/d3d_common.c index 211e68939c..4019584e6c 100644 --- a/gfx/common/d3d_common.c +++ b/gfx/common/d3d_common.c @@ -128,7 +128,21 @@ int32_t d3d_translate_filter(unsigned type) void d3d_input_driver(const char* input_name, const char* joypad_name, const input_driver_t** input, void** input_data) { -#if defined(_XBOX) || defined(__WINRT__) +#if defined(__WINRT__) + /* Plain xinput is supported on UWP, but it supports joypad only (uwp driver was added later) */ + if (string_is_equal(input_name, "xinput")) + { + void *xinput = input_xinput.init(joypad_name); + *input = xinput ? (const input_driver_t*)&input_xinput : NULL; + *input_data = xinput; + } + else + { + void *uwp = input_uwp.init(joypad_name); + *input = uwp ? (const input_driver_t*)&input_uwp : NULL; + *input_data = uwp; + } +#elif defined(_XBOX) void *xinput = input_xinput.init(joypad_name); *input = xinput ? (const input_driver_t*)&input_xinput : NULL; *input_data = xinput; diff --git a/griffin/griffin.c b/griffin/griffin.c index cbe29ef9e7..e58883035a 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -603,6 +603,7 @@ INPUT #include "../input/drivers_joypad/dos_joypad.c" #elif defined(__WINRT__) #include "../input/drivers/xdk_xinput_input.c" +#include "../input/drivers/uwp_input.c" #endif #ifdef HAVE_WAYLAND diff --git a/input/drivers/uwp_input.c b/input/drivers/uwp_input.c new file mode 100644 index 0000000000..4c2f53af19 --- /dev/null +++ b/input/drivers/uwp_input.c @@ -0,0 +1,174 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2018 - Krzysztof HaƂadyn + * + * RetroArch 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. + * + * RetroArch 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 RetroArch. + * If not, see . + */ + +#include +#include + +#ifdef HAVE_CONFIG_H +#include "../../config.h" +#endif + +#include +#include + +#include + +#include "../input_driver.h" + +// TODO: Add support for multiple mice and multiple touch + +typedef struct uwp_input +{ + bool blocked; + const input_device_driver_t *joypad; +} uwp_input_t; + +static void uwp_input_poll(void *data) +{ + uwp_input_t *uwp = (uwp_input_t*)data; + + if (uwp && uwp->joypad) + uwp->joypad->poll(); + + uwp_input_next_frame(); +} + +static int16_t uwp_input_state(void *data, + rarch_joypad_info_t joypad_info, + const struct retro_keybind **binds, + unsigned port, unsigned device, + unsigned index, unsigned id) +{ + uwp_input_t *uwp = (uwp_input_t*)data; + + switch (device) + { + case RETRO_DEVICE_JOYPAD: + return input_joypad_pressed(uwp->joypad, joypad_info, port, binds[port], id); + case RETRO_DEVICE_ANALOG: + if (binds[port]) + return input_joypad_analog(uwp->joypad, joypad_info, port, index, id, binds[port]); + break; + + case RETRO_DEVICE_KEYBOARD: + return (id < RETROK_LAST) && uwp_keyboard_pressed(id); + + case RETRO_DEVICE_MOUSE: + case RARCH_DEVICE_MOUSE_SCREEN: + return uwp_mouse_state(port, id, device == RARCH_DEVICE_MOUSE_SCREEN); + + case RETRO_DEVICE_POINTER: + case RARCH_DEVICE_POINTER_SCREEN: + return uwp_pointer_state(index, id, device == RARCH_DEVICE_POINTER_SCREEN); + } + + return 0; +} + +static void uwp_input_free_input(void *data) +{ + uwp_input_t *uwp = (uwp_input_t*)data; + + if (!uwp) + return; + + if (uwp->joypad) + uwp->joypad->destroy(); + + free(uwp); +} + +static void *uwp_input_init(const char *joypad_driver) +{ + uwp_input_t *uwp = (uwp_input_t*)calloc(1, sizeof(*uwp)); + if (!uwp) + return NULL; + + input_keymaps_init_keyboard_lut(rarch_key_map_uwp); + + uwp->joypad = input_joypad_init_driver(joypad_driver, uwp); + + return uwp; +} + +static uint64_t uwp_input_get_capabilities(void *data) +{ + uint64_t caps = 0; + + caps |= (1 << RETRO_DEVICE_JOYPAD); + caps |= (1 << RETRO_DEVICE_MOUSE); + caps |= (1 << RETRO_DEVICE_KEYBOARD); + caps |= (1 << RETRO_DEVICE_POINTER); + caps |= (1 << RETRO_DEVICE_ANALOG); + + return caps; +} + +static bool uwp_input_set_rumble(void *data, unsigned port, + enum retro_rumble_effect effect, uint16_t strength) +{ + struct uwp_input *uwp = (struct uwp_input*)data; + if (!uwp) + return false; + return input_joypad_set_rumble(uwp->joypad, port, effect, strength); +} + +static const input_device_driver_t *uwp_input_get_joypad_driver(void *data) +{ + uwp_input_t *uwp = (uwp_input_t*)data; + if (!uwp) + return NULL; + return uwp->joypad; +} + +static void uwp_input_grab_mouse(void *data, bool state) +{ + (void)data; + (void)state; +} + +static bool uwp_keyboard_mapping_is_blocked(void *data) +{ + uwp_input_t *uwp = (uwp_input_t*)data; + if (!uwp) + return false; + return uwp->blocked; +} + +static void uwp_keyboard_mapping_set_block(void *data, bool value) +{ + uwp_input_t *uwp = (uwp_input_t*)data; + if (!uwp) + return; + uwp->blocked = value; +} + +input_driver_t input_uwp = { + uwp_input_init, + uwp_input_poll, + uwp_input_state, + uwp_input_free_input, + NULL, + NULL, + uwp_input_get_capabilities, + "uwp", + uwp_input_grab_mouse, + NULL, + uwp_input_set_rumble, + uwp_input_get_joypad_driver, + NULL, + uwp_keyboard_mapping_is_blocked, + uwp_keyboard_mapping_set_block, +}; diff --git a/input/input_driver.c b/input/input_driver.c index 5134c23e76..709fac324b 100644 --- a/input/input_driver.c +++ b/input/input_driver.c @@ -104,6 +104,9 @@ static const input_driver_t *input_drivers[] = { #ifdef HAVE_X11 &input_x, #endif +#ifdef __WINRT__ + &input_uwp, +#endif #ifdef XENON &input_xenon360, #endif diff --git a/input/input_driver.h b/input/input_driver.h index 1ac166478b..11fcc415b8 100644 --- a/input/input_driver.h +++ b/input/input_driver.h @@ -834,6 +834,7 @@ extern input_driver_t input_xenon360; extern input_driver_t input_gx; extern input_driver_t input_wiiu; extern input_driver_t input_xinput; +extern input_driver_t input_uwp; extern input_driver_t input_linuxraw; extern input_driver_t input_udev; extern input_driver_t input_cocoa; diff --git a/uwp/uwp_func.h b/uwp/uwp_func.h index 7a3e98e60e..4b92bc489b 100644 --- a/uwp/uwp_func.h +++ b/uwp/uwp_func.h @@ -28,6 +28,13 @@ extern char uwp_device_family[128]; void* uwp_get_corewindow(void); +void uwp_input_next_frame(void); +bool uwp_keyboard_pressed(unsigned key); +int16_t uwp_mouse_state(unsigned port, unsigned id, bool screen); +int16_t uwp_pointer_state(unsigned idx, unsigned id, bool screen); + +extern const struct rarch_key_map rarch_key_map_uwp[]; + #ifdef __cplusplus } #endif diff --git a/uwp/uwp_main.cpp b/uwp/uwp_main.cpp index 7e3f132228..b79cbc65ef 100644 --- a/uwp/uwp_main.cpp +++ b/uwp/uwp_main.cpp @@ -34,6 +34,7 @@ using namespace Windows::ApplicationModel::Activation; using namespace Windows::UI::Core; using namespace Windows::UI::Input; using namespace Windows::UI::ViewManagement; +using namespace Windows::Devices::Input; using namespace Windows::System; using namespace Windows::System::Profile; using namespace Windows::Foundation; @@ -158,6 +159,20 @@ const struct rarch_key_map rarch_key_map_uwp[] = { { 0, RETROK_UNKNOWN } }; +struct uwp_input_state_t { + short mouse_screen_x = 0, mouse_screen_y = 0; + short mouse_rel_x = 0, mouse_rel_y = 0; + bool mouse_left = false, mouse_right = false, mouse_middle = false; + bool mouse_button4 = false, mouse_button5 = false; + short mouse_wheel_left = 0, mouse_wheel_up = 0; + + short touch_screen_x = 0, touch_screen_y = 0; + short touch_rel_x = 0, touch_rel_y = 0; + bool touch_touched = false; +}; + +struct uwp_input_state_t uwp_current_input, uwp_next_input; + // The main function is only used to initialize our IFrameworkView class. [Platform::MTAThread] int main(Platform::Array^) @@ -225,10 +240,22 @@ void App::SetWindow(CoreWindow^ window) ref new TypedEventHandler(this, &App::OnWindowClosed); window->KeyDown += - ref new TypedEventHandler(this, &App::OnKeyDown); + ref new TypedEventHandler(this, &App::OnKey); window->KeyUp += - ref new TypedEventHandler(this, &App::OnKeyUp); + ref new TypedEventHandler(this, &App::OnKey); + + window->PointerPressed += + ref new TypedEventHandler(this, &App::OnPointer); + + window->PointerReleased += + ref new TypedEventHandler(this, &App::OnPointer); + + window->PointerMoved += + ref new TypedEventHandler(this, &App::OnPointer); + + window->PointerWheelChanged += + ref new TypedEventHandler(this, &App::OnPointer); DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView(); @@ -255,7 +282,6 @@ void App::Load(Platform::String^ entryPoint) CoreApplication::Exit(); return; } - input_keymaps_init_keyboard_lut(rarch_key_map_uwp); // TODO (krzys_h): move this to the input driver m_initialized = true; } @@ -356,17 +382,7 @@ void App::OnWindowActivated(CoreWindow^ sender, WindowActivatedEventArgs^ args) m_windowFocused = args->WindowActivationState != CoreWindowActivationState::Deactivated; } -void App::OnKeyDown(CoreWindow^ sender, KeyEventArgs^ args) -{ - OnKey(sender, args, true); -} - -void App::OnKeyUp(CoreWindow^ sender, KeyEventArgs^ args) -{ - OnKey(sender, args, false); -} - -void App::OnKey(CoreWindow^ sender, KeyEventArgs^ args, bool down) +void App::OnKey(CoreWindow^ sender, KeyEventArgs^ args) { uint16_t mod = 0; if ((sender->GetKeyState(VirtualKey::Shift) & CoreVirtualKeyStates::Locked) == CoreVirtualKeyStates::Locked) @@ -385,7 +401,39 @@ void App::OnKey(CoreWindow^ sender, KeyEventArgs^ args, bool down) unsigned keycode = input_keymaps_translate_keysym_to_rk((unsigned)args->VirtualKey); - input_keyboard_event(down, keycode, 0, mod, RETRO_DEVICE_KEYBOARD); + input_keyboard_event(!args->KeyStatus.IsKeyReleased, keycode, 0, mod, RETRO_DEVICE_KEYBOARD); +} + +void App::OnPointer(CoreWindow^ sender, PointerEventArgs^ args) +{ + if (args->CurrentPoint->PointerDevice->PointerDeviceType == PointerDeviceType::Mouse) + { + uwp_next_input.mouse_left = args->CurrentPoint->Properties->IsLeftButtonPressed; + uwp_next_input.mouse_middle = args->CurrentPoint->Properties->IsMiddleButtonPressed; + uwp_next_input.mouse_right = args->CurrentPoint->Properties->IsRightButtonPressed; + uwp_next_input.mouse_button4 = args->CurrentPoint->Properties->IsXButton1Pressed; + uwp_next_input.mouse_button5 = args->CurrentPoint->Properties->IsXButton2Pressed; + uwp_next_input.mouse_screen_x = args->CurrentPoint->Position.X; + uwp_next_input.mouse_screen_y = args->CurrentPoint->Position.Y; + uwp_next_input.mouse_rel_x = uwp_next_input.mouse_screen_x - uwp_current_input.mouse_screen_x; + uwp_next_input.mouse_rel_y = uwp_next_input.mouse_screen_y - uwp_current_input.mouse_screen_y; + if (args->CurrentPoint->Properties->IsHorizontalMouseWheel) + { + uwp_next_input.mouse_wheel_left += args->CurrentPoint->Properties->MouseWheelDelta; + } + else + { + uwp_next_input.mouse_wheel_up += args->CurrentPoint->Properties->MouseWheelDelta; + } + } + else + { + uwp_next_input.touch_touched = args->CurrentPoint->IsInContact; + uwp_next_input.touch_screen_x = args->CurrentPoint->Position.X; + uwp_next_input.touch_screen_y = args->CurrentPoint->Position.Y; + uwp_next_input.touch_rel_x = uwp_next_input.touch_screen_x - uwp_current_input.touch_screen_x; + uwp_next_input.touch_rel_y = uwp_next_input.touch_screen_y - uwp_current_input.touch_screen_y; + } } void App::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args) @@ -476,4 +524,72 @@ extern "C" { { return (void*)CoreWindow::GetForCurrentThread(); } + + void uwp_input_next_frame(void) + { + uwp_current_input = uwp_next_input; + uwp_next_input.mouse_rel_x = 0; + uwp_next_input.mouse_rel_y = 0; + uwp_next_input.mouse_wheel_up %= WHEEL_DELTA; + uwp_next_input.mouse_wheel_left %= WHEEL_DELTA; + uwp_next_input.touch_rel_x = 0; + uwp_next_input.touch_rel_y = 0; + } + + bool uwp_keyboard_pressed(unsigned key) + { + unsigned sym = rarch_keysym_lut[(enum retro_key)key]; + return (CoreWindow::GetForCurrentThread()->GetKeyState((VirtualKey)sym) & CoreVirtualKeyStates::Down) == CoreVirtualKeyStates::Down; + } + + int16_t uwp_mouse_state(unsigned port, unsigned id, bool screen) + { + int16_t state = 0; + + switch (id) + { + case RETRO_DEVICE_ID_MOUSE_X: + return screen ? uwp_current_input.mouse_screen_x : uwp_current_input.mouse_rel_x; + case RETRO_DEVICE_ID_MOUSE_Y: + return screen ? uwp_current_input.mouse_screen_y : uwp_current_input.mouse_rel_y; + case RETRO_DEVICE_ID_MOUSE_LEFT: + return uwp_current_input.mouse_left; + case RETRO_DEVICE_ID_MOUSE_RIGHT: + return uwp_current_input.mouse_right; + case RETRO_DEVICE_ID_MOUSE_WHEELUP: + return uwp_current_input.mouse_wheel_up > WHEEL_DELTA; + case RETRO_DEVICE_ID_MOUSE_WHEELDOWN: + return uwp_current_input.mouse_wheel_up < -WHEEL_DELTA; + case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP: + return uwp_current_input.mouse_wheel_left > WHEEL_DELTA; + case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN: + return uwp_current_input.mouse_wheel_left < -WHEEL_DELTA; + case RETRO_DEVICE_ID_MOUSE_MIDDLE: + return uwp_current_input.mouse_middle; + case RETRO_DEVICE_ID_MOUSE_BUTTON_4: + return uwp_current_input.mouse_button4; + case RETRO_DEVICE_ID_MOUSE_BUTTON_5: + return uwp_current_input.mouse_button5; + } + + return 0; + } + + // TODO: I don't have any touch-enabled Windows devices to test if this actually works + int16_t uwp_pointer_state(unsigned idx, unsigned id, bool screen) + { + switch (id) + { + case RETRO_DEVICE_ID_POINTER_X: + return screen ? uwp_current_input.touch_screen_x : uwp_current_input.touch_rel_x; + case RETRO_DEVICE_ID_POINTER_Y: + return screen ? uwp_current_input.touch_screen_y : uwp_current_input.touch_rel_y; + case RETRO_DEVICE_ID_POINTER_PRESSED: + return uwp_current_input.touch_touched; + default: + break; + } + + return 0; + } } diff --git a/uwp/uwp_main.h b/uwp/uwp_main.h index 9d359e6e1a..623d922161 100644 --- a/uwp/uwp_main.h +++ b/uwp/uwp_main.h @@ -45,8 +45,8 @@ namespace RetroArchUWP void OnVisibilityChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::VisibilityChangedEventArgs^ args); void OnWindowClosed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CoreWindowEventArgs^ args); void OnWindowActivated(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::WindowActivatedEventArgs^ args); - void OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args); - void OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args); + void OnKey(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args); + void OnPointer(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args); // DisplayInformation event handlers. void OnDpiChanged(Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args); @@ -62,9 +62,6 @@ namespace RetroArchUWP void SetWindowResized() { m_windowResized = true; } static App^ GetInstance() { return m_instance; } - private: - void OnKey(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args, bool down); - private: bool m_initialized; bool m_windowClosed;