Skip to content

Commit e9f1672

Browse files
committed
Initial commit
0 parents  commit e9f1672

File tree

12 files changed

+1353
-0
lines changed

12 files changed

+1353
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Cargo
2+
target/
3+
Cargo.lock

Cargo.toml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "vigem-client"
3+
version = "0.1.0"
4+
authors = ["Casper <[email protected]>"]
5+
edition = "2018"
6+
license = "MIT"
7+
8+
description = "ViGEm client API in pure Rust."
9+
documentation = "https://docs.rs/vigem-client/"
10+
repository = "https://github.com/CasualX/vigem-client"
11+
readme = "readme.md"
12+
categories = ["api-bindings"]
13+
14+
[features]
15+
# Include the unstable DualShock4Wired target
16+
unstable = []
17+
18+
[dependencies]
19+
winapi = { version = "0.3", features = ["std", "handleapi", "setupapi", "fileapi", "winbase", "ioapiset", "synchapi", "errhandlingapi", "xinput", "winerror"] }

examples/readme.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::{thread, time};
2+
3+
fn main() {
4+
// Connect to the ViGEmBus driver
5+
let client = vigem_client::Client::connect().unwrap();
6+
7+
// Create the virtual controller target
8+
let id = vigem_client::TargetId::XBOX360_WIRED;
9+
let mut target = vigem_client::Xbox360Wired::new(client, id);
10+
11+
// Plugin the virtual controller
12+
target.plugin().unwrap();
13+
14+
// Wait for the virtual controller to be ready to accept updates
15+
target.wait_ready().unwrap();
16+
17+
// The input state of the virtual controller
18+
let mut gamepad = vigem_client::XGamepad {
19+
buttons: vigem_client::XButtons!(UP | RIGHT | LB | A | X),
20+
..Default::default()
21+
};
22+
23+
let start = time::Instant::now();
24+
loop {
25+
let elapsed = start.elapsed().as_secs_f64();
26+
27+
// Play for 10 seconds
28+
if elapsed >= 10.0 {
29+
break;
30+
}
31+
32+
// Spin the left thumb stick in circles
33+
gamepad.thumb_lx = (elapsed.cos() * 30000.0) as i16;
34+
gamepad.thumb_ly = (elapsed.sin() * 30000.0) as i16;
35+
36+
// Spin the right thumb stick in circles
37+
gamepad.thumb_rx = -gamepad.thumb_ly;
38+
gamepad.thumb_ry = gamepad.thumb_lx;
39+
40+
// Twiddle the triggers
41+
gamepad.left_trigger = ((((elapsed * 1.5).sin() * 127.0) as i32) + 127) as u8;
42+
gamepad.right_trigger = ((((elapsed * 1.5).cos() * 127.0) as i32) + 127) as u8;
43+
44+
let _ = target.update(&gamepad);
45+
46+
thread::sleep(time::Duration::from_millis(10));
47+
}
48+
}

license.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2021 Casper <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

readme.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
ViGEm client in Rust
2+
====================
3+
4+
[![MIT License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5+
[![crates.io](https://img.shields.io/crates/v/vigem-client.svg)](https://crates.io/crates/vigem-client)
6+
[![docs.rs](https://docs.rs/vigem-client/badge.svg)](https://docs.rs/vigem-client)
7+
8+
[ViGEm](https://vigem.org/) is a Virtual Gamepad Emulation Framework.
9+
This crate implements a client for the [ViGEmBus Driver](https://github.com/ViGEm/ViGEmBus).
10+
The driver must be installed for this library to have any use.
11+
12+
The client is written 100% in Rust, ViGEm's client C library is not used.
13+
Of course it must talk to WinAPI which means it's only available for Windows platforms.
14+
15+
Unlike the competition this library provides an optimized, safe and idiomatic interface.
16+
17+
Usage
18+
-----
19+
20+
This library is available on [crates.io](https://crates.io/crates/vigem-client) and its documentation on [docs.rs](https://docs.rs/vigem-client).
21+
22+
In your `Cargo.toml` add:
23+
24+
```
25+
[dependencies]
26+
vigem-client = "0.1"
27+
```
28+
29+
Examples
30+
--------
31+
32+
Try this example out: `cargo run --example readme`:
33+
34+
```rust
35+
use std::{thread, time};
36+
37+
fn main() {
38+
// Connect to the ViGEmBus driver
39+
let client = vigem_client::Client::connect().unwrap();
40+
41+
// Create the virtual controller target
42+
let id = vigem_client::TargetId::XBOX360_WIRED;
43+
let mut target = vigem_client::Xbox360Wired::new(client, id);
44+
45+
// Plugin the virtual controller
46+
target.plugin().unwrap();
47+
48+
// Wait for the virtual controller to be ready to accept updates
49+
target.wait_ready().unwrap();
50+
51+
// The input state of the virtual controller
52+
let mut gamepad = vigem_client::XGamepad {
53+
buttons: vigem_client::XButtons!(UP | RIGHT | LB | A | X),
54+
..Default::default()
55+
};
56+
57+
let start = time::Instant::now();
58+
loop {
59+
let elapsed = start.elapsed().as_secs_f64();
60+
61+
// Play for 10 seconds
62+
if elapsed >= 10.0 {
63+
break;
64+
}
65+
66+
// Spin the left thumb stick in circles
67+
gamepad.thumb_lx = (elapsed.cos() * 30000.0) as i16;
68+
gamepad.thumb_ly = (elapsed.sin() * 30000.0) as i16;
69+
70+
// Spin the right thumb stick in circles
71+
gamepad.thumb_rx = -gamepad.thumb_ly;
72+
gamepad.thumb_ry = gamepad.thumb_lx;
73+
74+
// Twiddle the triggers
75+
gamepad.left_trigger = ((((elapsed * 1.5).sin() * 127.0) as i32) + 127) as u8;
76+
gamepad.right_trigger = ((((elapsed * 1.5).cos() * 127.0) as i32) + 127) as u8;
77+
78+
let _ = target.update(&gamepad);
79+
80+
thread::sleep(time::Duration::from_millis(10));
81+
}
82+
}
83+
```
84+
85+
License
86+
-------
87+
88+
Licensed under [MIT License](https://opensource.org/licenses/MIT), see [license.txt](license.txt).
89+
90+
### Contribution
91+
92+
Unless you explicitly state otherwise, any contribution intentionally submitted
93+
for inclusion in the work by you, shall be licensed as above, without any additional terms or conditions.

0 commit comments

Comments
 (0)