5 releases

0.1.4 Aug 1, 2022
0.1.3 Jun 8, 2022
0.1.2 Feb 20, 2022
0.1.1 Aug 26, 2021
0.1.0 Jul 7, 2021

#393 in Hardware support

Download history 15/week @ 2023-12-17 3/week @ 2024-01-07 69/week @ 2024-01-14 88/week @ 2024-01-21 60/week @ 2024-01-28 63/week @ 2024-02-04 97/week @ 2024-02-11 19/week @ 2024-02-18 207/week @ 2024-02-25 70/week @ 2024-03-03 105/week @ 2024-03-10 102/week @ 2024-03-17 19/week @ 2024-03-24 108/week @ 2024-03-31

341 downloads per month

MIT license

48KB
1K SLoC

ViGEm client in Rust

MIT License crates.io docs.rs

ViGEm is a Virtual Gamepad Emulation Framework. This crate implements a client for the ViGEmBus Driver. The driver must be installed for this library to have any use.

The client is written 100% in Rust, ViGEm's client C library is not used. Of course it must talk to WinAPI which means it's only available for Windows platforms.

Unlike the competition this library provides an optimized, safe and idiomatic interface.

Usage

This library is available on crates.io and its documentation on docs.rs.

In your Cargo.toml add:

[dependencies]
vigem-client = "0.1"

Examples

Try this example out: cargo run --example readme:

use std::{thread, time};

fn main() {
	// Connect to the ViGEmBus driver
	let client = vigem_client::Client::connect().unwrap();

	// Create the virtual controller target
	let id = vigem_client::TargetId::XBOX360_WIRED;
	let mut target = vigem_client::Xbox360Wired::new(client, id);

	// Plugin the virtual controller
	target.plugin().unwrap();

	// Wait for the virtual controller to be ready to accept updates
	target.wait_ready().unwrap();

	// The input state of the virtual controller
	let mut gamepad = vigem_client::XGamepad {
		buttons: vigem_client::XButtons!(UP | RIGHT | LB | A | X),
		..Default::default()
	};

	let start = time::Instant::now();
	loop {
		let elapsed = start.elapsed().as_secs_f64();

		// Play for 10 seconds
		if elapsed >= 10.0 {
			break;
		}

		// Spin the left thumb stick in circles
		gamepad.thumb_lx = (elapsed.cos() * 30000.0) as i16;
		gamepad.thumb_ly = (elapsed.sin() * 30000.0) as i16;

		// Spin the right thumb stick in circles
		gamepad.thumb_rx = -gamepad.thumb_ly;
		gamepad.thumb_ry = gamepad.thumb_lx;

		// Twiddle the triggers
		gamepad.left_trigger = ((((elapsed * 1.5).sin() * 127.0) as i32) + 127) as u8;
		gamepad.right_trigger = ((((elapsed * 1.5).cos() * 127.0) as i32) + 127) as u8;

		let _ = target.update(&gamepad);

		thread::sleep(time::Duration::from_millis(10));
	}
}

License

Licensed under MIT License, see license.txt.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, shall be licensed as above, without any additional terms or conditions.

Dependencies

~225KB