From bfa363f192fb15ccaa5d5f9c571e9704854dd230 Mon Sep 17 00:00:00 2001 From: Saverio Miroddi Date: Wed, 14 Jul 2021 23:50:26 +0200 Subject: [PATCH 2/2] gamepad-rs: Start joystick threads, on Linux, only when the respective devices are present --- gamepad-rs/src/platform/linux/mod.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/gamepad-rs/src/platform/linux/mod.rs b/gamepad-rs/src/platform/linux/mod.rs index e88f137..13c5ed1 100644 --- a/gamepad-rs/src/platform/linux/mod.rs +++ b/gamepad-rs/src/platform/linux/mod.rs @@ -3,7 +3,7 @@ DEFAULT_CONTROLLER_STATE, }; -use std::sync::mpsc; +use std::{path::Path, sync::mpsc}; const JS0_BLOCK_DEVICE: &str = "/dev/input/js0"; const JS1_BLOCK_DEVICE: &str = "/dev/input/js1"; @@ -54,8 +54,14 @@ pub fn new() -> Option { let (tx, rx) = mpsc::channel(); let (tx1, rx1) = mpsc::channel(); - std::thread::spawn(move || joystick_thread(tx, JS0_BLOCK_DEVICE)); - std::thread::spawn(move || joystick_thread(tx1, JS1_BLOCK_DEVICE)); + // If a joystick is not connected, the corresponding block device may not exist. + // + if Path::new(JS0_BLOCK_DEVICE).exists() { + std::thread::spawn(move || joystick_thread(tx, JS0_BLOCK_DEVICE)); + } + if Path::new(JS1_BLOCK_DEVICE).exists() { + std::thread::spawn(move || joystick_thread(tx1, JS1_BLOCK_DEVICE)); + } Some(Self { rx, -- 2.32.0