Multiple Gamepad support for Windows

This commit is contained in:
loki
2020-01-25 01:27:47 +01:00
parent b4d3748d74
commit 8b7c833cd4
3 changed files with 27 additions and 16 deletions

View File

@@ -125,7 +125,7 @@ void passthrough(platf::input_t &input, PNV_SCROLL_PACKET packet) {
} }
void passthrough(std::shared_ptr<input_t> &input, PNV_MULTI_CONTROLLER_PACKET packet) { void passthrough(std::shared_ptr<input_t> &input, PNV_MULTI_CONTROLLER_PACKET packet) {
if(packet->controllerNumber < 0 || packet->controllerNumber > input->gamepads.size()) { if(packet->controllerNumber < 0 || packet->controllerNumber >= input->gamepads.size()) {
BOOST_LOG(warning) << "ControllerNumber out of range ["sv << packet->controllerNumber << ']'; BOOST_LOG(warning) << "ControllerNumber out of range ["sv << packet->controllerNumber << ']';
return; return;

View File

@@ -223,7 +223,7 @@ void keyboard(input_t &input, uint16_t modcode, bool release) {
void gamepad(input_t &input, int controller, const gamepad_state_t &gamepad_state) { void gamepad(input_t &input, int controller, const gamepad_state_t &gamepad_state) {
auto &gamepads = ((input_raw_t*)input.get())->gamepads; auto &gamepads = ((input_raw_t*)input.get())->gamepads;
if(controller < 0 || controller > gamepads.size()) { if(controller < 0 || controller >= gamepads.size()) {
BOOST_LOG(warning) << "Controller number out of range: ["sv << controller << " > "sv << gamepads.size() << ']'; BOOST_LOG(warning) << "Controller number out of range: ["sv << controller << " > "sv << gamepads.size() << ']';
return; return;
} }
@@ -454,7 +454,7 @@ input_t input() {
std::filesystem::remove(gamepad_path); std::filesystem::remove(gamepad_path);
} }
std::filesystem::create_symlink(libevdev_uinput_get_devnode(gp.gamepads[0].first.get()), gamepad_path); std::filesystem::create_symlink(libevdev_uinput_get_devnode(gp.gamepads[x].first.get()), gamepad_path);
} }
return result; return result;

View File

@@ -35,13 +35,15 @@ public:
return -1; return -1;
} }
x360.reset(vigem_target_x360_alloc()); for(auto &x360 : x360s) {
x360.reset(vigem_target_x360_alloc());
status = vigem_target_add(client.get(), x360.get()); status = vigem_target_add(client.get(), x360.get());
if(!VIGEM_SUCCESS(status)) { if(!VIGEM_SUCCESS(status)) {
BOOST_LOG(error) << "Couldn't add Gamepad to ViGEm connection ["sv << util::hex(status).to_string_view() << ']'; BOOST_LOG(error) << "Couldn't add Gamepad to ViGEm connection ["sv << util::hex(status).to_string_view() << ']';
return -1; return -1;
}
} }
return 0; return 0;
@@ -49,10 +51,12 @@ public:
~vigem_t() { ~vigem_t() {
if(client) { if(client) {
if(x360 && vigem_target_is_attached(x360.get())) { for(auto &x360 : x360s) {
auto status = vigem_target_remove(client.get(), x360.get()); if(x360 && vigem_target_is_attached(x360.get())) {
if(!VIGEM_SUCCESS(status)) { auto status = vigem_target_remove(client.get(), x360.get());
BOOST_LOG(warning) << "Couldn't detach gamepad from ViGEm ["sv << util::hex(status).to_string_view() << ']'; if(!VIGEM_SUCCESS(status)) {
BOOST_LOG(warning) << "Couldn't detach gamepad from ViGEm ["sv << util::hex(status).to_string_view() << ']';
}
} }
} }
@@ -60,7 +64,7 @@ public:
} }
} }
target_t x360; std::vector<target_t> x360s;
client_t client; client_t client;
}; };
@@ -258,16 +262,23 @@ void keyboard(input_t &input, uint16_t modcode, bool release) {
} }
} }
void gamepad(input_t &input, const gamepad_state_t &gamepad_state) { void gamepad(input_t &input, int controller, const gamepad_state_t &gamepad_state) {
// If there is no gamepad support // If there is no gamepad support
if(!input) { if(!input) {
return; return;
} }
auto vigem = (vigem_t*)input.get(); 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(controller < 0 && controller >= vigem->x360s.size()) {
BOOST_LOG(warning) << "Controller number out of range: ["sv << controller << " > "sv << vigem->x360s.size() << ']';
return;
}
auto &xusb = *(PXUSB_REPORT)&gamepad_state;
auto &x360 = vigem->x360s[controller];
auto status = vigem_target_x360_update(vigem->client.get(), x360.get(), xusb);
if(!VIGEM_SUCCESS(status)) { if(!VIGEM_SUCCESS(status)) {
BOOST_LOG(fatal) << "Couldn't send gamepad input to ViGEm ["sv << util::hex(status).to_string_view() << ']'; BOOST_LOG(fatal) << "Couldn't send gamepad input to ViGEm ["sv << util::hex(status).to_string_view() << ']';