Added sum docs

This commit is contained in:
Benjamin Höglinger-Stelzer
2020-08-23 14:56:35 +02:00
parent 465736429b
commit 1e9e45e0ea

128
README.md
View File

@@ -1,18 +1,21 @@
# ViGEm Client Native SDK
Developer SDK for communication with [`ViGEmBus`](https://github.com/ViGEm/ViGEmBus).
C/C++ developer SDK for communication with [`ViGEmBus`](https://github.com/ViGEm/ViGEmBus).
[![Build status](https://ci.appveyor.com/api/projects/status/k806d3m2egjr0j56?svg=true)](https://ci.appveyor.com/project/nefarius/vigemclient) [![Discord](https://img.shields.io/discord/346756263763378176.svg)](https://discord.vigem.org) [![Website](https://img.shields.io/website-up-down-green-red/https/vigem.org.svg?label=ViGEm.org)](https://vigem.org/) [![PayPal Donate](https://img.shields.io/badge/paypal-donate-blue.svg)](<https://paypal.me/NefariusMaximus>) [![Support on Patreon](https://img.shields.io/badge/patreon-donate-orange.svg)](<https://www.patreon.com/nefarius>) [![GitHub followers](https://img.shields.io/github/followers/nefarius.svg?style=social&label=Follow)](https://github.com/nefarius) [![Twitter Follow](https://img.shields.io/twitter/follow/nefariusmaximus.svg?style=social&label=Follow)](https://twitter.com/nefariusmaximus)
[![Build status](https://ci.appveyor.com/api/projects/status/k806d3m2egjr0j56?svg=true)](https://ci.appveyor.com/project/nefarius/vigemclient) [![Discord](https://img.shields.io/discord/346756263763378176.svg)](https://discord.vigem.org)
## About
Soon...
**TL;DR:** use this if you want to create virtual game controllers from your C/C++ application 😊
The `ViGEmClient` provides a small library exposing a simple API for creating and "feeding" (periodically updating it with new input data) virtual game controllers through [`ViGEmBus`](https://github.com/ViGEm/ViGEmBus). The library takes care of discovering a compatible instance of the bus driver on the users system and abstracting away the inner workings of the emulation framework. You can use and distribute it with your project as either a static component (recommended) or a dynamic library (DLL). This library is **not** thread-safe, ensure proper synchronization in a multi-threaded environment.
## How to build
### Prerequisites
- Visual Studio **2019** ([Community Edition](https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=Community&rel=16) is just fine)
- When linking statically, make sure to also link against `setupapi.lib`
## Contribute
@@ -25,3 +28,122 @@ Have an idea for a new feature? Let's have a chat about your request on [Discord
### Questions & Support
Please respect that the GitHub issue tracker isn't a helpdesk. We offer a [Discord server](https://discord.vigem.org) and [forums](https://forums.vigem.org), where you're welcome to check out and engage in discussions!
## How to use
### Integration
Integrating this library into your project is pretty straight-forward, there are no additional 3rd party dependencies. You can either `git submodule` or `git subtree` this repository directly into your source tree or use the provided [`vcpkg`](https://github.com/microsoft/vcpkg) package manager integration [found here](https://github.com/ViGEm/ViGEmClient.vcpkg) (recommended, can be updates with ease). The library tries to handle driver compatibility internally so static linking is recommended to avoid DLL hell 😊
### API usage
For a general overview of the provided types and functions [take a look at the main include file](./include/ViGEm/Client.h).
Now, onwards to a practical example 😉 First, include some basic headers:
```cpp
//
// Windows basic types 'n' fun
//
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
//
// Optional depending on your use case
//
#include <Xinput.h>
//
// The ViGEm API
//
#include <ViGEm/Client.h>
//
// Link against SetupAPI
//
#pragma comment(lib, "setupapi.lib")
```
To initialize the API call `vigem_alloc` which gives you an opaque handle to the underlying driver:
```cpp
const auto client = vigem_alloc();
if (client == nullptr)
{
std::cerr << L"Uh, not enough memory to do that?!" << std::endl;
return -1;
}
```
Establish connection to the driver:
```cpp
const auto retval = vigem_connect(client);
if (!VIGEM_SUCCESS(retval))
{
std::cerr << L"ViGEm Bus connection failed with error code: 0x" << std::hex << retval << std::endl;
return -1;
}
```
👉 Note: this is an "expensive" operation, it's recommended you do this once in your project, not every frame for performance benefits.
---
With this handle we're prepared to spawn (connect) and feed (supply with periodic input updates) one or many emulated controller devices. So let's spawn an Xbox 360 controller:
```cpp
//
// Allocate handle to identify new pad
//
const auto pad = vigem_target_x360_alloc();
//
// Add client to the bus, this equals a plug-in event
//
const auto pir = vigem_target_add(client, pad);
//
// Error handling
//
if (!VIGEM_SUCCESS(pir))
{
std::cerr << L"Target plugin failed with error code: 0x" << std::hex << retval << std::endl;
return -1;
}
XINPUT_STATE state;
//
// Grab the input from a physical X36ß pad in this example
//
XInputGetState(0, &state);
//
// The XINPUT_GAMEPAD structure is identical to the XUSB_REPORT structure
// so we can simply take it "as-is" and cast it.
//
// Call this function on every input state change e.g. in a loop polling
// another joystick or network device or thermometer or... you get the idea.
//
vigem_target_x360_update(client, pad, *reinterpret_cast<XUSB_REPORT*>(&state.Gamepad));
//
// We're done with this pad, free resources (this disconnects the virtual device)
//
vigem_target_remove(client, pad);
vigem_target_free(pad);
```
---
Once ViGEm interaction is no longer required (e.g. the application is about to end) the acquired resources need to be freed properly:
```cpp
vigem_disconnect(client);
vigem_free(client);
```
After that the `client` handle will become invalid and must not be used again.