Skip to content

Commit 6913224

Browse files
committed
controller detection rewrite
1 parent d55e8d7 commit 6913224

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

main.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ use std::io::prelude::*;
22
use std::io::{BufReader, Cursor};
33
use std::fs::File;
44
use std::sync::{Arc, Mutex};
5-
use std::thread;
5+
use std::{thread};
66
use std::time::{SystemTime, Duration};
77
use byteorder::{BigEndian, ReadBytesExt};
88
use enigo::*;
99

10-
static MOVE_DIVISOR: f64 = 1638.4; // = (x/32768)*20 // smaller --> faster
10+
static MOVE_DIVISOR: f64 = 1638.4; // = (x/32768)*20 // smaller -> faster
1111
static CLAMP_THRESHOLD: f64 = 0.04;
12-
1312
static SCROLL_MUTLIPLIER: f64 = 1.5;
13+
static CONTROLLER_NAMEPARTS: [&'static str; 2] = ["gamepad", "controller"];
1414

1515
struct State {
1616
running: bool,
@@ -22,6 +22,7 @@ struct State {
2222
rz: i16,
2323
}
2424

25+
2526
fn get_gamepad_handler() -> String {
2627
// 1. open device file
2728
// 2. read 2nd line and then evry 10 lines further to get device name. find a name containing 'gamepad' (case insensitive)
@@ -31,8 +32,21 @@ fn get_gamepad_handler() -> String {
3132
let reader = BufReader::new(f);
3233

3334
let mut lines = reader.lines();
34-
35-
while !lines.next().unwrap().unwrap().to_lowercase().contains("gamepad") {
35+
36+
let mut found_controller = false;
37+
while !found_controller {
38+
let line = lines.next();
39+
if line.is_none() {
40+
println!("No controler found! This could be caused by the controllers name not containing any of the words:");
41+
println!("{:?}", CONTROLLER_NAMEPARTS);
42+
std::process::exit(1);
43+
}
44+
let controller_name = line.unwrap().unwrap().to_lowercase();
45+
for name in CONTROLLER_NAMEPARTS {
46+
if controller_name.contains(name) {
47+
found_controller = true;
48+
}
49+
}
3650
}
3751

3852
let handlers_line = lines.nth(3).unwrap().unwrap();

0 commit comments

Comments
 (0)