From 834d5cb59bfa64c4b3f581a7277ebc2d9e16b20a Mon Sep 17 00:00:00 2001 From: loki Date: Sat, 18 Jan 2020 23:52:22 +0100 Subject: [PATCH] Refactor in preparation of Gamepad support on Windows --- CMakeLists.txt | 6 +- sunshine/input.cpp | 99 +++++--------------- sunshine/input.h | 12 +-- sunshine/platform/common.h | 49 +++++----- sunshine/platform/linux_evdev.cpp | 144 +++++++++++++----------------- sunshine/platform/windows.cpp | 82 ++++++++++++++++- 6 files changed, 194 insertions(+), 198 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 91ef2354..0d01294e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,11 @@ if(WIN32) sunshine/platform/windows.cpp sunshine/platform/windows_dxgi.cpp sunshine/platform/windows_wasapi.cpp - ViGEmClient/src/ViGEmClient.cpp) + ViGEmClient/src/ViGEmClient.cpp + ViGEmClient/include/ViGEm/Client.h + ViGEmClient/include/ViGEm/Common.h + ViGEmClient/include/ViGEm/Util.h + ViGEmClient/include/ViGEm/km/BusShared.h) set(PLATFORM_LIBRARIES winmm ksuser diff --git a/sunshine/input.cpp b/sunshine/input.cpp index cf46cc96..ab393f48 100644 --- a/sunshine/input.cpp +++ b/sunshine/input.cpp @@ -16,22 +16,6 @@ extern "C" { namespace input { using namespace std::literals; -constexpr std::uint16_t DPAD_UP = 0x0001; -constexpr std::uint16_t DPAD_DOWN = 0x0002; -constexpr std::uint16_t DPAD_LEFT = 0x0004; -constexpr std::uint16_t DPAD_RIGHT = 0x0008; -constexpr std::uint16_t START = 0x0010; -constexpr std::uint16_t BACK = 0x0020; -constexpr std::uint16_t LEFT_STICK = 0x0040; -constexpr std::uint16_t RIGHT_STICK = 0x0080; -constexpr std::uint16_t LEFT_BUTTON = 0x0100; -constexpr std::uint16_t RIGHT_BUTTON = 0x0200; -constexpr std::uint16_t HOME = 0x0400; -constexpr std::uint16_t A = 0x1000; -constexpr std::uint16_t B = 0x2000; -constexpr std::uint16_t X = 0x4000; -constexpr std::uint16_t Y = 0x8000; - void print(PNV_MOUSE_MOVE_PACKET packet) { BOOST_LOG(debug) << "--begin mouse move packet--"sv << std::endl @@ -146,7 +130,7 @@ void passthrough(std::shared_ptr &input, PNV_MULTI_CONTROLLER_PACKET pa std::uint16_t bf; std::memcpy(&bf, &packet->buttonFlags, sizeof(std::uint16_t)); - gamepad_state_t gamepad_state { + platf::gamepad_state_t gamepad_state{ bf, packet->leftTrigger, packet->rightTrigger, @@ -159,77 +143,36 @@ void passthrough(std::shared_ptr &input, PNV_MULTI_CONTROLLER_PACKET pa bf = gamepad_state.buttonFlags ^ input->gamepad_state.buttonFlags; auto bf_new = gamepad_state.buttonFlags; - if(bf) { - // up pressed == -1, down pressed == 1, else 0 - if((DPAD_UP | DPAD_DOWN) & bf) { - int val = bf_new & DPAD_UP ? -1 : (bf_new & DPAD_DOWN ? 1 : 0); + // up pressed == -1, down pressed == 1, else 0 + if (platf::BACK & bf) { + if (platf::BACK & bf_new) { + input->back_timeout_id = task_pool.pushDelayed([input]() { + auto &state = input->gamepad_state; - platf::gp::dpad_y(input->input, val); - } + // Release Back button +// state.buttonFlags &= ~platf::BACK; +// platf::gamepad(input->input, state); - if((DPAD_LEFT | DPAD_RIGHT) & bf) { - int val = bf_new & DPAD_LEFT ? -1 : (bf_new & DPAD_RIGHT ? 1 : 0); - platf::gp::dpad_x(input->input, val); - } + // Press Home button + state.buttonFlags |= platf::HOME; + platf::gamepad(input->input, state); - if(START & bf) platf::gp::start(input->input, bf_new & START ? 1 : 0); - if(LEFT_STICK & bf) platf::gp::left_stick(input->input, bf_new & LEFT_STICK ? 1 : 0); - if(RIGHT_STICK & bf) platf::gp::right_stick(input->input, bf_new & RIGHT_STICK ? 1 : 0); - if(LEFT_BUTTON & bf) platf::gp::left_button(input->input, bf_new & LEFT_BUTTON ? 1 : 0); - if(RIGHT_BUTTON & bf) platf::gp::right_button(input->input, bf_new & RIGHT_BUTTON ? 1 : 0); - if(HOME & bf) platf::gp::home(input->input, bf_new & HOME ? 1 : 0); - if(A & bf) platf::gp::a(input->input, bf_new & A ? 1 : 0); - if(B & bf) platf::gp::b(input->input, bf_new & B ? 1 : 0); - if(X & bf) platf::gp::x(input->input, bf_new & X ? 1 : 0); - if(Y & bf) platf::gp::y(input->input, bf_new & Y ? 1 : 0); + // Release Home button + state.buttonFlags &= ~platf::HOME; + platf::gamepad(input->input, state); - if(BACK & bf) { - if(BACK & bf_new) { - platf::gp::back(input->input,1); - input->back_timeout_id = task_pool.pushDelayed([input]() { - platf::gp::back(input->input, 0); - - platf::gp::home(input->input,1); - platf::gp::home(input->input,0); - - input->back_timeout_id = nullptr; - }, config::input.back_button_timeout).task_id; - } - else if(input->back_timeout_id) { - platf::gp::back(input->input, 0); - - task_pool.cancel(input->back_timeout_id); input->back_timeout_id = nullptr; - } + }, config::input.back_button_timeout).task_id; + } + else if (input->back_timeout_id) { + task_pool.cancel(input->back_timeout_id); + input->back_timeout_id = nullptr; } } - if(input->gamepad_state.lt != gamepad_state.lt) { - platf::gp::left_trigger(input->input, gamepad_state.lt); - } - - if(input->gamepad_state.rt != gamepad_state.rt) { - platf::gp::right_trigger(input->input, gamepad_state.rt); - } - - if(input->gamepad_state.lsX != gamepad_state.lsX) { - platf::gp::left_stick_x(input->input, gamepad_state.lsX); - } - - if(input->gamepad_state.lsY != gamepad_state.lsY) { - platf::gp::left_stick_y(input->input, gamepad_state.lsY); - } - - if(input->gamepad_state.rsX != gamepad_state.rsX) { - platf::gp::right_stick_x(input->input, gamepad_state.rsX); - } - - if(input->gamepad_state.rsY != gamepad_state.rsY) { - platf::gp::right_stick_y(input->input, gamepad_state.rsY); - } + platf::gamepad(input->input, gamepad_state); input->gamepad_state = gamepad_state; - platf::gp::sync(input->input); } void passthrough_helper(std::shared_ptr input, std::vector &&input_data) { diff --git a/sunshine/input.h b/sunshine/input.h index c266afbc..18204c0f 100644 --- a/sunshine/input.h +++ b/sunshine/input.h @@ -9,20 +9,10 @@ #include "thread_pool.h" namespace input { -struct gamepad_state_t { - std::uint16_t buttonFlags; - std::uint8_t lt; - std::uint8_t rt; - std::int16_t lsX; - std::int16_t lsY; - std::int16_t rsX; - std::int16_t rsY; -}; - struct input_t { input_t(); - gamepad_state_t gamepad_state; + platf::gamepad_state_t gamepad_state; std::unordered_map key_press; std::array mouse_press; diff --git a/sunshine/platform/common.h b/sunshine/platform/common.h index 3aaf8e50..1f48254c 100644 --- a/sunshine/platform/common.h +++ b/sunshine/platform/common.h @@ -9,6 +9,31 @@ #include "sunshine/utility.h" namespace platf { +constexpr std::uint16_t DPAD_UP = 0x0001; +constexpr std::uint16_t DPAD_DOWN = 0x0002; +constexpr std::uint16_t DPAD_LEFT = 0x0004; +constexpr std::uint16_t DPAD_RIGHT = 0x0008; +constexpr std::uint16_t START = 0x0010; +constexpr std::uint16_t BACK = 0x0020; +constexpr std::uint16_t LEFT_STICK = 0x0040; +constexpr std::uint16_t RIGHT_STICK = 0x0080; +constexpr std::uint16_t LEFT_BUTTON = 0x0100; +constexpr std::uint16_t RIGHT_BUTTON = 0x0200; +constexpr std::uint16_t HOME = 0x0400; +constexpr std::uint16_t A = 0x1000; +constexpr std::uint16_t B = 0x2000; +constexpr std::uint16_t X = 0x4000; +constexpr std::uint16_t Y = 0x8000; + +struct gamepad_state_t { + std::uint16_t buttonFlags; + std::uint8_t lt; + std::uint8_t rt; + std::int16_t lsX; + std::int16_t lsY; + std::int16_t rsX; + std::int16_t rsY; +}; struct img_t { public: @@ -59,29 +84,7 @@ void move_mouse(input_t &input, int deltaX, int deltaY); void button_mouse(input_t &input, int button, bool release); void scroll(input_t &input, int distance); void keyboard(input_t &input, uint16_t modcode, bool release); - -namespace gp { -void dpad_y(input_t &input, int button_state); // up pressed == -1, down pressed == 1, else 0 -void dpad_x(input_t &input, int button_state); // left pressed == -1, right pressed == 1, else 0 -void start(input_t &input, int button_down); -void back(input_t &input, int button_down); -void left_stick(input_t &input, int button_down); -void right_stick(input_t &input, int button_down); -void left_button(input_t &input, int button_down); -void right_button(input_t &input, int button_down); -void home(input_t &input, int button_down); -void a(input_t &input, int button_down); -void b(input_t &input, int button_down); -void x(input_t &input, int button_down); -void y(input_t &input, int button_down); -void left_trigger(input_t &input, std::uint8_t abs_z); -void right_trigger(input_t &input, std::uint8_t abs_z); -void left_stick_x(input_t &input, std::int16_t x); -void left_stick_y(input_t &input, std::int16_t y); -void right_stick_x(input_t &input, std::int16_t x); -void right_stick_y(input_t &input, std::int16_t y); -void sync(input_t &input); -} +void gamepad(input_t &input, const gamepad_state_t &gamepad_state); } #endif //SUNSHINE_COMMON_H diff --git a/sunshine/platform/linux_evdev.cpp b/sunshine/platform/linux_evdev.cpp index c26622b9..12f291af 100644 --- a/sunshine/platform/linux_evdev.cpp +++ b/sunshine/platform/linux_evdev.cpp @@ -36,6 +36,8 @@ struct input_raw_t { uinput_t mouse_input; keyboard_t keyboard; + + gamepad_state_t gamepad_state {}; }; void move_mouse(input_t &input, int deltaX, int deltaY) { @@ -211,89 +213,65 @@ void keyboard(input_t &input, uint16_t modcode, bool release) { XFlush(keyboard.get()); } -namespace gp { -// up pressed == -1, down pressed == 1, else 0 -void dpad_y(input_t &input, int button_state) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_ABS, ABS_HAT0Y, button_state); -} -// left pressed == -1, right pressed == 1, else 0 -void dpad_x(input_t &input, int button_state) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_ABS, ABS_HAT0X, button_state); -} -void start(input_t &input, int button_down) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_KEY, BTN_START, button_down); -} -void back(input_t &input, int button_down) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_KEY, BTN_SELECT, button_down); -} -void left_stick(input_t &input, int button_down) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_KEY, BTN_THUMBL, button_down); -} -void right_stick(input_t &input, int button_down) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_KEY, BTN_THUMBR, button_down); -} -void left_button(input_t &input, int button_down) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_KEY, BTN_TL, button_down); -} -void right_button(input_t &input, int button_down) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_KEY, BTN_TR, button_down); -} -void home(input_t &input, int button_down) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_KEY, BTN_MODE, button_down); -} -void a(input_t &input, int button_down) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_KEY, BTN_SOUTH, button_down); -} -void b(input_t &input, int button_down) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_KEY, BTN_EAST, button_down); -} -void x(input_t &input, int button_down) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_KEY, BTN_NORTH, button_down); -} -void y(input_t &input, int button_down) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_KEY, BTN_WEST, button_down); -} -void left_trigger(input_t &input, std::uint8_t abs_z) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_ABS, ABS_Z, abs_z); -} -void right_trigger(input_t &input, std::uint8_t abs_z) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_ABS, ABS_RZ, abs_z); -} -void left_stick_x(input_t &input, std::int16_t x) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_ABS, ABS_X, x); -} -void left_stick_y(input_t &input, std::int16_t y) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_ABS, ABS_Y, -y); -} -void right_stick_x(input_t &input, std::int16_t x) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_ABS, ABS_RX, x); -} -void right_stick_y(input_t &input, std::int16_t y) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_ABS, ABS_RY, -y); -} -void sync(input_t &input) { - auto &gp = *(input_raw_t*)input.get(); - libevdev_uinput_write_event(gp.gamepad_input.get(), EV_SYN, SYN_REPORT, 0); -} +void gamepad(input_t &input, const gamepad_state_t &gamepad_state) { + auto uinput = (input_raw_t*)input.get(); + + auto bf = gamepad_state.buttonFlags ^ uinput->gamepad_state.buttonFlags; + auto bf_new = gamepad_state.buttonFlags; + + if(bf) { + // up pressed == -1, down pressed == 1, else 0 + if((DPAD_UP | DPAD_DOWN) & bf) { + int button_state = bf_new & DPAD_UP ? -1 : (bf_new & DPAD_DOWN ? 1 : 0); + + libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_ABS, ABS_HAT0Y, button_state); + } + + if((DPAD_LEFT | DPAD_RIGHT) & bf) { + int button_state = bf_new & DPAD_LEFT ? -1 : (bf_new & DPAD_RIGHT ? 1 : 0); + + libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_ABS, ABS_HAT0X, button_state); + } + + if(START & bf) libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_KEY, BTN_START, bf_new & START ? 1 : 0); + if(BACK & bf) libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_KEY, BTN_SELECT, bf_new & BACK ? 1 : 0); + if(LEFT_STICK & bf) libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_KEY, BTN_THUMBL, bf_new & LEFT_STICK ? 1 : 0); + if(RIGHT_STICK & bf) libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_KEY, BTN_THUMBR, bf_new & RIGHT_STICK ? 1 : 0); + if(LEFT_BUTTON & bf) libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_KEY, BTN_TL, bf_new & LEFT_BUTTON ? 1 : 0); + if(RIGHT_BUTTON & bf) libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_KEY, BTN_TR, bf_new & RIGHT_BUTTON ? 1 : 0); + if(HOME & bf) libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_KEY, BTN_MODE, bf_new & HOME ? 1 : 0); + if(A & bf) libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_KEY, BTN_SOUTH, bf_new & A ? 1 : 0); + if(B & bf) libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_KEY, BTN_EAST, bf_new & B ? 1 : 0); + if(X & bf) libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_KEY, BTN_NORTH, bf_new & X ? 1 : 0); + if(Y & bf) libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_KEY, BTN_WEST, bf_new & Y ? 1 : 0); + } + + if(uinput->gamepad_state.lt != gamepad_state.lt) { + libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_ABS, ABS_Z, gamepad_state.lt); + } + + if(uinput->gamepad_state.rt != gamepad_state.rt) { + libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_ABS, ABS_RZ, gamepad_state.rt); + } + + if(uinput->gamepad_state.lsX != gamepad_state.lsX) { + libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_ABS, ABS_X, gamepad_state.lsX); + } + + if(uinput->gamepad_state.lsY != gamepad_state.lsY) { + libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_ABS, ABS_Y, -gamepad_state.lsY); + } + + if(uinput->gamepad_state.rsX != gamepad_state.rsX) { + libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_ABS, ABS_RX, gamepad_state.rsX); + } + + if(uinput->gamepad_state.rsY != gamepad_state.rsY) { + libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_ABS, ABS_RY, -gamepad_state.rsY); + } + + uinput->gamepad_state = gamepad_state; + libevdev_uinput_write_event(uinput->gamepad_input.get(), EV_SYN, SYN_REPORT, 0); } int mouse(input_raw_t &gp) { diff --git a/sunshine/platform/windows.cpp b/sunshine/platform/windows.cpp index 2d4e6206..4192f9b8 100755 --- a/sunshine/platform/windows.cpp +++ b/sunshine/platform/windows.cpp @@ -3,15 +3,71 @@ #include #include +#include + #include "sunshine/main.h" #include "common.h" namespace platf { using namespace std::literals; + +class vigem_t { +public: + using client_t = util::safe_ptr<_VIGEM_CLIENT_T, vigem_free>; + using target_t = util::safe_ptr<_VIGEM_TARGET_T, vigem_target_free>; + + int init() { + VIGEM_ERROR status; + + client.reset(vigem_alloc()); + + status = vigem_connect(client.get()); + if(!VIGEM_SUCCESS(status)) { + BOOST_LOG(warning) << "Couldn't setup connection to ViGEm for gamepad support ["sv << util::hex(status).to_string_view() << ']'; + + return -1; + } + + x360.reset(vigem_target_x360_alloc()); + + status = vigem_target_add(client.get(), x360.get()); + if(!VIGEM_SUCCESS(status)) { + BOOST_LOG(error) << "Couldn't add Gamepad to ViGEm connection ["sv << util::hex(status).to_string_view() << ']'; + + return -1; + } + + return 0; + } + + ~vigem_t() { + if(client) { + if(vigem_target_is_attached(x360.get())) { + auto status = vigem_target_remove(client.get(), x360.get()); + if(!VIGEM_SUCCESS(status)) { + BOOST_LOG(warning) << "Couldn't detach gamepad from ViGEm ["sv << util::hex(status).to_string_view() << ']'; + } + } + + vigem_disconnect(client.get()); + } + } + + target_t x360; + client_t client; +}; + std::string get_local_ip() { return "192.168.0.119"s; } input_t input() { - return nullptr; + input_t result { new vigem_t {} }; + + auto vigem = (vigem_t*)result.get(); + if(vigem->init()) { + return nullptr; + } + + return result; } void move_mouse(input_t &input, int deltaX, int deltaY) { @@ -119,6 +175,24 @@ void keyboard(input_t &input, uint16_t modcode, bool release) { } } +void gamepad(input_t &input, const gamepad_state_t &gamepad_state) { + // If there is no gamepad support + if(!input) { + return; + } + + auto vigem = (vigem_t*)input.get(); + auto &xusb = *(PXUSB_REPORT)&gamepad_state; + + auto status = vigem_target_x360_update(vigem->client.get(), vigem->x360.get(), xusb); + if(!VIGEM_SUCCESS(status)) { + BOOST_LOG(fatal) << "Couldn't send gamepad input to ViGEm ["sv << util::hex(status).to_string_view() << ']'; + + log_flush(); + std::abort(); + } +} + namespace gp { void dpad_y(input_t &input, int button_state) {} // up pressed == -1, down pressed == 1, else 0 void dpad_x(input_t &input, int button_state) {} // left pressed == -1, right pressed == 1, else 0 @@ -142,5 +216,9 @@ void right_stick_y(input_t &input, std::int16_t y) {} void sync(input_t &input) {} } -void freeInput(void*) {} +void freeInput(void *p) { + auto vigem = (vigem_t*)p; + + delete vigem; +} }