From 0deb5433d184bb23e5d5eecc397c9c0591de8d17 Mon Sep 17 00:00:00 2001 From: loki Date: Sun, 22 Dec 2019 21:24:50 +0100 Subject: [PATCH] Move logic away from platform dependend code --- sunshine/input.cpp | 91 +++++++++++++++-- sunshine/input.h | 20 +++- sunshine/platform/common.h | 24 ++++- sunshine/platform/linux_evdev.cpp | 160 +++++++++++++++--------------- sunshine/stream.cpp | 2 +- 5 files changed, 204 insertions(+), 93 deletions(-) diff --git a/sunshine/input.cpp b/sunshine/input.cpp index fb3c14d5..9f200627 100644 --- a/sunshine/input.cpp +++ b/sunshine/input.cpp @@ -15,6 +15,22 @@ 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) { std::cout << "--begin mouse move packet--"sv << std::endl; @@ -116,12 +132,11 @@ void passthrough(platf::input_t &input, PNV_SCROLL_PACKET packet) { platf::scroll(input, util::endian::big(packet->scrollAmt1)); } -void passthrough(platf::input_t &input, PNV_MULTI_CONTROLLER_PACKET packet) { +void passthrough(input_t &input, PNV_MULTI_CONTROLLER_PACKET packet) { std::uint16_t bf; - - static_assert(sizeof(bf) == sizeof(packet->buttonFlags), "Can't memcpy :("); std::memcpy(&bf, &packet->buttonFlags, sizeof(std::uint16_t)); - platf::gamepad_state_t gamepad_state { + + gamepad_state_t gamepad_state { bf, packet->leftTrigger, packet->rightTrigger, @@ -131,27 +146,81 @@ void passthrough(platf::input_t &input, PNV_MULTI_CONTROLLER_PACKET packet) { packet->rightStickY }; - platf::gamepad(input, gamepad_state); + 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); + + platf::gp::dpad_y(input.input, val); + } + + 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); + } + + if(START & bf) platf::gp::start(input.input, bf_new & START ? 1 : 0); + if(BACK & bf) platf::gp::back(input.input, bf_new & BACK ? 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); + } + + 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); + } + + input.gamepad_state = gamepad_state; + platf::gp::sync(input.input); } -void passthrough(platf::input_t &input, void *payload) { +void passthrough(input_t &input, void *payload) { int input_type = util::endian::big(*(int*)payload); switch(input_type) { case PACKET_TYPE_MOUSE_MOVE: - passthrough(input, (PNV_MOUSE_MOVE_PACKET)payload); + passthrough(input.input, (PNV_MOUSE_MOVE_PACKET)payload); break; case PACKET_TYPE_MOUSE_BUTTON: - passthrough(input, (PNV_MOUSE_BUTTON_PACKET)payload); + passthrough(input.input, (PNV_MOUSE_BUTTON_PACKET)payload); break; case PACKET_TYPE_SCROLL_OR_KEYBOARD: { char *tmp_input = (char*)payload + 4; if(tmp_input[0] == 0x0A) { - passthrough(input, (PNV_SCROLL_PACKET)payload); + passthrough(input.input, (PNV_SCROLL_PACKET)payload); } else { - passthrough(input, (PNV_KEYBOARD_PACKET)payload); + passthrough(input.input, (PNV_KEYBOARD_PACKET)payload); } break; @@ -161,4 +230,6 @@ void passthrough(platf::input_t &input, void *payload) { break; } } + +input_t::input_t() : gamepad_state { 0 }, input { platf::input() } {} } diff --git a/sunshine/input.h b/sunshine/input.h index 444d43e1..bb27558c 100644 --- a/sunshine/input.h +++ b/sunshine/input.h @@ -8,9 +8,25 @@ #include "platform/common.h" namespace input { -void print(void *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; +}; -void passthrough(platf::input_t &, void *input); +struct input_t { + input_t(); + + gamepad_state_t gamepad_state; + platf::input_t input; +}; + +void print(void *input); +void passthrough(input_t &, void *input); } #endif //SUNSHINE_INPUT_H diff --git a/sunshine/platform/common.h b/sunshine/platform/common.h index b9be9e21..e8446aa8 100644 --- a/sunshine/platform/common.h +++ b/sunshine/platform/common.h @@ -52,7 +52,29 @@ 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); -void gamepad(input_t &input, const gamepad_state_t &gamepad_state); + +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); +} } #endif //SUNSHINE_COMMON_H diff --git a/sunshine/platform/linux_evdev.cpp b/sunshine/platform/linux_evdev.cpp index d4e0481f..61457f7e 100644 --- a/sunshine/platform/linux_evdev.cpp +++ b/sunshine/platform/linux_evdev.cpp @@ -14,22 +14,6 @@ #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; - using namespace std::literals; using evdev_t = util::safe_ptr; using uinput_t = util::safe_ptr; @@ -217,72 +201,90 @@ void keyboard(input_t &input, uint16_t modcode, bool release) { XFlush(disp.display); } -void gamepad(input_t &input, const gamepad_state_t &gamepad_state) { +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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.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.uinput.get(), EV_ABS, ABS_RY, -y); +} +void sync(input_t &input) { auto &gp = *(input_raw_t*)input.get(); - - auto bf = gamepad_state.buttonFlags ^ gp.gamepad_state.buttonFlags; - auto bf_new = gamepad_state.buttonFlags; - - if(bf) { - // up pressed == -1, down pressed == 1, else 0 - if(DPAD_UP & bf) { - int val = bf_new & DPAD_UP ? -1 : (bf_new & DPAD_DOWN ? 1 : 0); - libevdev_uinput_write_event(gp.uinput.get(), EV_ABS, ABS_HAT0Y, val); - } - else if(DPAD_DOWN & bf) { - int val = bf_new & DPAD_DOWN ? 1 : 0; - libevdev_uinput_write_event(gp.uinput.get(), EV_ABS, ABS_HAT0Y, val); - } - - if(DPAD_LEFT & bf) { - int val = bf_new & DPAD_LEFT ? -1 : (bf_new & DPAD_RIGHT ? 1 : 0); - libevdev_uinput_write_event(gp.uinput.get(), EV_ABS, ABS_HAT0X, val); - } - else if(DPAD_RIGHT & bf) { - int val = bf_new & DPAD_RIGHT ? 1 : 0; - libevdev_uinput_write_event(gp.uinput.get(), EV_ABS, ABS_HAT0X, val); - } - - if(START & bf) libevdev_uinput_write_event(gp.uinput.get(), EV_KEY, BTN_START, bf_new & START ? 1 : 0); - if(BACK & bf) libevdev_uinput_write_event(gp.uinput.get(), EV_KEY, BTN_SELECT, bf_new & BACK ? 1 : 0); - if(LEFT_STICK & bf) libevdev_uinput_write_event(gp.uinput.get(), EV_KEY, BTN_THUMBL, bf_new & LEFT_STICK ? 1 : 0); - if(RIGHT_STICK & bf) libevdev_uinput_write_event(gp.uinput.get(), EV_KEY, BTN_THUMBR, bf_new & RIGHT_STICK ? 1 : 0); - if(LEFT_BUTTON & bf) libevdev_uinput_write_event(gp.uinput.get(), EV_KEY, BTN_TL, bf_new & LEFT_BUTTON ? 1 : 0); - if(RIGHT_BUTTON & bf) libevdev_uinput_write_event(gp.uinput.get(), EV_KEY, BTN_TR, bf_new & RIGHT_BUTTON ? 1 : 0); - if(HOME & bf) libevdev_uinput_write_event(gp.uinput.get(), EV_KEY, BTN_MODE, bf_new & HOME ? 1 : 0); - if(A & bf) libevdev_uinput_write_event(gp.uinput.get(), EV_KEY, BTN_SOUTH, bf_new & A ? 1 : 0); - if(B & bf) libevdev_uinput_write_event(gp.uinput.get(), EV_KEY, BTN_EAST, bf_new & B ? 1 : 0); - if(X & bf) libevdev_uinput_write_event(gp.uinput.get(), EV_KEY, BTN_NORTH, bf_new & X ? 1 : 0); - if(Y & bf) libevdev_uinput_write_event(gp.uinput.get(), EV_KEY, BTN_WEST, bf_new & Y ? 1 : 0); - } - - if(gp.gamepad_state.lt != gamepad_state.lt) { - libevdev_uinput_write_event(gp.uinput.get(), EV_ABS, ABS_Z, gamepad_state.lt); - } - - if(gp.gamepad_state.rt != gamepad_state.rt) { - libevdev_uinput_write_event(gp.uinput.get(), EV_ABS, ABS_RZ, gamepad_state.rt); - } - - if(gp.gamepad_state.lsX != gamepad_state.lsX) { - libevdev_uinput_write_event(gp.uinput.get(), EV_ABS, ABS_X, gamepad_state.lsX); - } - - if(gp.gamepad_state.lsY != gamepad_state.lsY) { - libevdev_uinput_write_event(gp.uinput.get(), EV_ABS, ABS_Y, -gamepad_state.lsY); - } - - if(gp.gamepad_state.rsX != gamepad_state.rsX) { - libevdev_uinput_write_event(gp.uinput.get(), EV_ABS, ABS_RX, gamepad_state.rsX); - } - - if(gp.gamepad_state.rsY != gamepad_state.rsY) { - libevdev_uinput_write_event(gp.uinput.get(), EV_ABS, ABS_RY, -gamepad_state.rsY); - } - - gp.gamepad_state = gamepad_state; libevdev_uinput_write_event(gp.uinput.get(), EV_SYN, SYN_REPORT, 0); } +} input_t input() { input_t result { new input_raw_t() }; diff --git a/sunshine/stream.cpp b/sunshine/stream.cpp index 88b15ce0..c36b84cc 100644 --- a/sunshine/stream.cpp +++ b/sunshine/stream.cpp @@ -468,7 +468,7 @@ void control_server_t::send(const std::string_view & payload) { void controlThread(video::idr_event_t idr_events) { control_server_t server { CONTROL_PORT }; - auto input = platf::input(); + input::input_t input; server.map(packetTypes[IDX_START_A], [](const std::string_view &payload) { session.pingTimeout = std::chrono::steady_clock::now() + config::stream.ping_timeout;