@@ -4,6 +4,7 @@ use evdev::{Device, EventType, RelativeAxisCode, uinput::VirtualDevice};
44use log:: { error, info} ;
55use mouse_scroll_daemon:: { AnxiousParams , AnxiousState , process_events} ;
66use std:: path:: PathBuf ;
7+ use std:: path:: Path ;
78
89#[ derive( Parser , Debug ) ]
910#[ command( author, version, about, long_about = None ) ]
@@ -74,6 +75,7 @@ fn find_mouse_device(device_path: Option<PathBuf>) -> Result<Device> {
7475
7576 info ! ( "Searching for mouse devices..." ) ;
7677 let devices = evdev:: enumerate ( ) . collect :: < Vec < _ > > ( ) ;
78+ let mut best: Option < ( Device , u16 , std:: path:: PathBuf ) > = None ;
7779
7880 for ( path, device) in devices {
7981 let name = device. name ( ) . unwrap_or ( "Unknown" ) ;
@@ -87,13 +89,37 @@ fn find_mouse_device(device_path: Option<PathBuf>) -> Result<Device> {
8789 && relative_axes. contains ( RelativeAxisCode :: REL_WHEEL )
8890 && relative_axes. contains ( RelativeAxisCode :: REL_HWHEEL )
8991 {
90- info ! ( "Found mouse device: {} at {}" , name, path. display( ) ) ;
91- return Ok ( device) ;
92+ let input_id = device. input_id ( ) ;
93+ let product = input_id. product ( ) ;
94+ info ! (
95+ "Found mouse device: {} at {} (product: 0x{:04x})" ,
96+ name,
97+ path. display( ) ,
98+ product
99+ ) ;
100+ match & mut best {
101+ None => best = Some ( ( device, product, path) ) ,
102+ Some ( ( _, best_prod, _) ) => {
103+ if product < * best_prod {
104+ best = Some ( ( device, product, path) ) ;
105+ }
106+ }
107+ }
92108 }
93109 }
94110 }
95111 }
96112
113+ if let Some ( ( device, product_id, path) ) = best {
114+ info ! (
115+ "Selected mouse device: {} at {} (product: 0x{:04x})" ,
116+ device. name( ) . unwrap_or( "Unknown" ) ,
117+ path. display( ) ,
118+ product_id
119+ ) ;
120+ return Ok ( device) ;
121+ }
122+
97123 anyhow:: bail!( "No suitable mouse device found. Please specify a device path with --device" )
98124}
99125
0 commit comments