|
| 1 | +ViGEm client in Rust |
| 2 | +==================== |
| 3 | + |
| 4 | +[](https://opensource.org/licenses/MIT) |
| 5 | +[](https://crates.io/crates/vigem-client) |
| 6 | +[](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