Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmake/compile_definitions/windows.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ set(PLATFORM_TARGET_FILES
"${CMAKE_SOURCE_DIR}/src/platform/windows/misc.h"
"${CMAKE_SOURCE_DIR}/src/platform/windows/misc.cpp"
"${CMAKE_SOURCE_DIR}/src/platform/windows/input.cpp"
"${CMAKE_SOURCE_DIR}/src/platform/windows/osmspad.h"
"${CMAKE_SOURCE_DIR}/src/platform/windows/osmspad.cpp"
"${CMAKE_SOURCE_DIR}/src/platform/windows/osmspad_ioctl.h"
"${CMAKE_SOURCE_DIR}/src/platform/windows/session_input.h"
"${CMAKE_SOURCE_DIR}/src/platform/windows/session_input.cpp"
"${CMAKE_SOURCE_DIR}/src/platform/windows/display.h"
Expand Down Expand Up @@ -95,6 +98,7 @@ set(OPENSSL_LIBRARIES
list(PREPEND PLATFORM_LIBRARIES
${CURL_STATIC_LIBRARIES}
avrt
cfgmgr32
d3d11
D3DCompiler
dwmapi
Expand Down
1 change: 1 addition & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,7 @@ namespace config {
GAMEPAD_DRIVER_ALL,
GAMEPAD_DRIVER_VIRTUALHID,
GAMEPAD_DRIVER_VIGEMBUS,
GAMEPAD_DRIVER_OSMSPAD,
GAMEPAD_DRIVER_NONE,
});
string_restricted_f(vars, "gamepad"s, input.gamepad, get_supported_gamepad_options());
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace config {
inline constexpr std::string_view GAMEPAD_DRIVER_ALL = "all"; ///< Allow every available Windows virtual gamepad driver.
inline constexpr std::string_view GAMEPAD_DRIVER_VIRTUALHID = "virtualhid"; ///< Use Virtual HID Broker for gamepads on Windows or macOS.
inline constexpr std::string_view GAMEPAD_DRIVER_VIGEMBUS = "vigembus"; ///< Allow only ViGEmBus for Windows gamepads.
inline constexpr std::string_view GAMEPAD_DRIVER_OSMSPAD = "osmspad"; ///< OpenStreamMS OsmsPad driver (Xbox Series pads), ViGEmBus if it is missing.
inline constexpr std::string_view GAMEPAD_DRIVER_NONE = "none"; ///< Disable virtual gamepads on Windows and macOS.

// track modified config options
Expand Down
3 changes: 2 additions & 1 deletion src/platform/virtualhid_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,8 @@ namespace platf::virtualhid {
const std::string_view gamepad_driver,
const bool virtualhid_licensed
) {
return gamepad_driver != config::GAMEPAD_DRIVER_VIGEMBUS && gamepad_driver != config::GAMEPAD_DRIVER_NONE && capabilities.supports_gamepad &&
return gamepad_driver != config::GAMEPAD_DRIVER_VIGEMBUS && gamepad_driver != config::GAMEPAD_DRIVER_OSMSPAD &&
gamepad_driver != config::GAMEPAD_DRIVER_NONE && capabilities.supports_gamepad &&
(!capabilities.requires_installed_driver || virtualhid_licensed);
}

Expand Down
45 changes: 45 additions & 0 deletions src/platform/windows/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
// local includes
#include "input.h"
#include "misc.h"
#include "osmspad.h"
#include "src/config.h"
#include "src/globals.h"
#include "src/logging.h"
Expand Down Expand Up @@ -522,8 +523,27 @@ namespace platf {
struct input_raw_t {
virtualhid::input_context_t virtualhid; ///< libvirtualhid input context.
std::unique_ptr<vigem_t> vigem; ///< ViGEm fallback context.
std::unique_ptr<osmspad_t> osmspad; ///< OsmsPad driver context, when gamepad_driver = osmspad.
};

/**
* @brief Open the OsmsPad driver if it is not already open.
*
* @param raw Platform input context.
* @return True when OsmsPad can create controllers.
*/
bool ensure_osmspad(input_raw_t *raw) {
if (raw->osmspad) {
return true;
}
auto osmspad = std::make_unique<osmspad_t>();
if (!osmspad->init()) {
return false;
}
raw->osmspad = std::move(osmspad);
return true;
}

/**
* @brief Check whether the libvirtualhid runtime should receive gamepad allocations.
*
Expand Down Expand Up @@ -671,6 +691,18 @@ namespace platf {
return 0;
}

// OsmsPad (OpenStreamMS): every controller is an Xbox Series pad on the XInput path. If the
// driver is not installed, fall back to ViGEmBus with the usual type selection.
if (config::input.gamepad_driver == config::GAMEPAD_DRIVER_OSMSPAD) {
if (ensure_osmspad(raw)) {
BOOST_LOG(info) << "Gamepad " << id.globalIndex << " will be Xbox Series controller (OsmsPad)"sv;
if (raw->osmspad->alloc(id, feedback_queue) == 0) {
return 0;
}
}
BOOST_LOG(warning) << "OsmsPad is unavailable; using ViGEmBus for gamepad "sv << id.globalIndex;
}

if (!should_allocate_vigembus_gamepad(use_virtualhid)) {
return -1;
}
Expand Down Expand Up @@ -729,6 +761,10 @@ namespace platf {
int rebind_gamepad(input_t &input, const gamepad_id_t &id, feedback_queue_t feedback_queue) {
auto raw = (input_raw_t *) input.get();

if (raw->osmspad && raw->osmspad->has(id.globalIndex)) {
return raw->osmspad->rebind(id, std::move(feedback_queue));
}

if (virtualhid::has_gamepad(raw->virtualhid, id.globalIndex)) {
return virtualhid::rebind_gamepad(raw->virtualhid, id, std::move(feedback_queue));
}
Expand All @@ -743,6 +779,11 @@ namespace platf {
void free_gamepad(input_t &input, int nr) {
auto raw = (input_raw_t *) input.get();

if (raw->osmspad && raw->osmspad->has(nr)) {
raw->osmspad->free(nr);
return;
}

if (virtualhid::has_gamepad(raw->virtualhid, nr)) {
virtualhid::free_gamepad(raw->virtualhid, nr);
return;
Expand Down Expand Up @@ -1006,6 +1047,10 @@ namespace platf {
*/
void gamepad_update(input_t &input, int nr, const gamepad_state_t &gamepad_state) {
auto raw = (input_raw_t *) input.get();
if (raw->osmspad && raw->osmspad->has(nr)) {
raw->osmspad->update(nr, gamepad_state);
return;
}
if (virtualhid::has_gamepad(raw->virtualhid, nr)) {
virtualhid::gamepad_update(raw->virtualhid, nr, gamepad_state);
return;
Expand Down
243 changes: 243 additions & 0 deletions src/platform/windows/osmspad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/**
* @file src/platform/windows/osmspad.cpp
* @brief Virtual Xbox Series gamepads through OpenStreamMS's OsmsPad driver.
*/
// platform includes
#include <Windows.h>
#include <cfgmgr32.h>
#include <initguid.h>

// standard includes
#include <vector>

// local includes
#include "osmspad.h"
#include "osmspad_ioctl.h"
#include "src/logging.h"

using namespace std::literals;

namespace platf {
namespace {
/**
* @brief Device path of the first present OsmsPad interface.
* @return Path, or an empty string when the driver is not installed or not started.
*/
std::wstring interface_path() {
ULONG length = 0;
auto guid = GUID_DEVINTERFACE_OSMSPAD;
if (CM_Get_Device_Interface_List_SizeW(&length, &guid, nullptr, CM_GET_DEVICE_INTERFACE_LIST_PRESENT) != CR_SUCCESS || length <= 1) {
return {};
}
std::vector<wchar_t> list(length);
if (CM_Get_Device_Interface_ListW(&guid, nullptr, list.data(), length, CM_GET_DEVICE_INTERFACE_LIST_PRESENT) != CR_SUCCESS) {
return {};
}
return list.data();
}

/**
* @brief Moonlight button mask to the XInput-style bits OsmsPad takes.
*
* The low 16 Moonlight bits already use the XInput layout. The miscellaneous and touchpad
* buttons become the Xbox Series Share button.
*/
std::uint16_t to_osmspad_buttons(const std::uint32_t flags) {
auto buttons = static_cast<std::uint16_t>(flags & 0xF7FF);
if (flags & (MISC_BUTTON | TOUCHPAD_BUTTON)) {
buttons |= OSMSPAD_SHARE;
}
return buttons;
}
} // namespace

bool osmspad_t::available() {
return !interface_path().empty();
}

osmspad_t::~osmspad_t() {
if (stop_event_) {
SetEvent(stop_event_);
}
if (device_ != INVALID_HANDLE_VALUE) {
CancelIoEx(device_, nullptr);
}
if (feedback_thread_.joinable()) {
feedback_thread_.join();
}
// Closing the handle removes every controller this instance created.
if (device_ != INVALID_HANDLE_VALUE) {
CloseHandle(device_);
}
if (stop_event_) {
CloseHandle(stop_event_);
}
}

bool osmspad_t::init() {
const auto path = interface_path();
if (path.empty()) {
return false;
}
device_ = CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
if (device_ == INVALID_HANDLE_VALUE) {
BOOST_LOG(warning) << "Couldn't open the OsmsPad driver ["sv << GetLastError() << ']';
return false;
}
stop_event_ = CreateEventW(nullptr, TRUE, FALSE, nullptr);
feedback_thread_ = std::thread {&osmspad_t::feedback_loop, this};
BOOST_LOG(info) << "OsmsPad virtual gamepad driver is available"sv;
return true;
}

bool osmspad_t::ioctl(DWORD code, const void *in, DWORD in_size, void *out, DWORD out_size, DWORD *returned) {
OVERLAPPED overlapped {};
overlapped.hEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
DWORD bytes = 0;
BOOL ok = DeviceIoControl(device_, code, const_cast<void *>(in), in_size, out, out_size, &bytes, &overlapped);
if (!ok && GetLastError() == ERROR_IO_PENDING) {
ok = GetOverlappedResult(device_, &overlapped, &bytes, TRUE);
}
const auto error = GetLastError();
CloseHandle(overlapped.hEvent);
if (returned) {
*returned = bytes;
}
SetLastError(error);
return ok != FALSE;
}

int osmspad_t::alloc(const gamepad_id_t &id, feedback_queue_t feedback_queue) {
if (id.globalIndex < 0 || id.globalIndex >= MAX_GAMEPADS) {
return -1;
}
free(id.globalIndex);

OsmsPadCreateIn request {OSMSPAD_PROTOCOL_VERSION, OSMSPAD_PROFILE_XBOX_SERIES, OSMSPAD_SESSION_CALLER};
OsmsPadCreateOut created {};
if (!ioctl(IOCTL_OSMSPAD_CREATE, &request, sizeof request, &created, sizeof created)) {
BOOST_LOG(error) << "Couldn't create an OsmsPad controller ["sv << GetLastError() << ']';
return -1;
}

std::lock_guard lock {mutex_};
auto &slot = slots_[id.globalIndex];
slot = {};
slot.used = true;
slot.pad = created.Pad;
slot.client_relative_index = id.clientRelativeIndex;
slot.feedback_queue = std::move(feedback_queue);
BOOST_LOG(info) << "OsmsPad controller "sv << created.Pad << " created for gamepad "sv << id.globalIndex;
return 0;
}

int osmspad_t::rebind(const gamepad_id_t &id, feedback_queue_t feedback_queue) {
std::lock_guard lock {mutex_};
if (id.globalIndex < 0 || id.globalIndex >= MAX_GAMEPADS || !slots_[id.globalIndex].used) {
return -1;
}
auto &slot = slots_[id.globalIndex];
slot.client_relative_index = id.clientRelativeIndex;
slot.feedback_queue = std::move(feedback_queue);
slot.last_low = slot.last_high = slot.last_left = slot.last_right = 0;
return 0;
}

void osmspad_t::free(int nr) {
std::uint32_t pad;
{
std::lock_guard lock {mutex_};
if (nr < 0 || nr >= MAX_GAMEPADS || !slots_[nr].used) {
return;
}
pad = slots_[nr].pad;
slots_[nr] = {};
}
OsmsPadDestroyIn request {pad};
if (!ioctl(IOCTL_OSMSPAD_DESTROY, &request, sizeof request, nullptr, 0)) {
BOOST_LOG(warning) << "Couldn't remove OsmsPad controller "sv << pad << " ["sv << GetLastError() << ']';
}
}

bool osmspad_t::has(int nr) const {
std::lock_guard lock {mutex_};
return nr >= 0 && nr < MAX_GAMEPADS && slots_[nr].used;
}

void osmspad_t::update(int nr, const gamepad_state_t &state) {
OsmsPadInput input {};
{
std::lock_guard lock {mutex_};
if (nr < 0 || nr >= MAX_GAMEPADS || !slots_[nr].used) {
return;
}
input.Pad = slots_[nr].pad;
}
input.Buttons = to_osmspad_buttons(state.buttonFlags);
input.LeftTrigger = state.lt;
input.RightTrigger = state.rt;
input.ThumbLX = state.lsX;
input.ThumbLY = state.lsY;
input.ThumbRX = state.rsX;
input.ThumbRY = state.rsY;
if (!ioctl(IOCTL_OSMSPAD_INPUT, &input, sizeof input, nullptr, 0)) {
BOOST_LOG(warning) << "Couldn't send gamepad input to OsmsPad ["sv << GetLastError() << ']';
}
}

// One pending feedback request at a time: the driver completes it when a game asks one of
// our controllers to rumble, and the loop posts it to that controller's client.
void osmspad_t::feedback_loop() {
OVERLAPPED overlapped {};
overlapped.hEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
const HANDLE waits[] {stop_event_, overlapped.hEvent};

while (true) {
OsmsPadFeedback feedback {};
DWORD bytes = 0;
ResetEvent(overlapped.hEvent);
BOOL ok = DeviceIoControl(device_, IOCTL_OSMSPAD_FEEDBACK, nullptr, 0, &feedback, sizeof feedback, &bytes, &overlapped);
if (!ok && GetLastError() == ERROR_IO_PENDING) {
if (WaitForMultipleObjects(2, waits, FALSE, INFINITE) != WAIT_OBJECT_0 + 1) {
CancelIoEx(device_, &overlapped);
GetOverlappedResult(device_, &overlapped, &bytes, TRUE);
break;
}
ok = GetOverlappedResult(device_, &overlapped, &bytes, FALSE);
}
if (!ok) {
if (WaitForSingleObject(stop_event_, 0) == WAIT_OBJECT_0) {
break;
}
BOOST_LOG(warning) << "OsmsPad feedback request failed ["sv << GetLastError() << ']';
if (WaitForSingleObject(stop_event_, 1000) == WAIT_OBJECT_0) {
break;
}
continue;
}
if (bytes < sizeof feedback) {
continue;
}

std::lock_guard lock {mutex_};
for (auto &slot : slots_) {
if (!slot.used || slot.pad != feedback.Pad || !slot.feedback_queue) {
continue;
}
if (feedback.LargeMotor != slot.last_low || feedback.SmallMotor != slot.last_high) {
slot.feedback_queue->raise(gamepad_feedback_msg_t::make_rumble(slot.client_relative_index, feedback.LargeMotor, feedback.SmallMotor));
slot.last_low = feedback.LargeMotor;
slot.last_high = feedback.SmallMotor;
}
if (feedback.LeftTrigger != slot.last_left || feedback.RightTrigger != slot.last_right) {
slot.feedback_queue->raise(gamepad_feedback_msg_t::make_rumble_triggers(slot.client_relative_index, feedback.LeftTrigger, feedback.RightTrigger));
slot.last_left = feedback.LeftTrigger;
slot.last_right = feedback.RightTrigger;
}
break;
}
}
CloseHandle(overlapped.hEvent);
}

} // namespace platf
Loading
Loading