Add --bus and --device options

This commit is contained in:
dokutan 2020-02-08 04:34:17 +01:00
parent d7bd82db05
commit c4beee41fb
5 changed files with 210 additions and 14 deletions

View file

@ -61,3 +61,6 @@ example.macro for an example, keymap.md section Keyboard keys/Keys for a list of
- mouse_right
- mouse_middle
## --bus and --device options
With these options the USB bus id and device number can be specified. This is useful if there are multiple devices with the same vendor and product id, or if the particular device has a different vendor or product id that is not expected by this software.

View file

@ -88,6 +88,96 @@ int mouse_m908::open_mouse(){
return res;
}
// init libusb and open mouse by bus and device
int mouse_m908::open_mouse_bus_device( uint8_t bus, uint8_t device ){
//vars
int res = 0;
//libusb init
res = libusb_init( NULL );
if( res < 0 ){
return res;
}
//open device (_handle)
libusb_device **dev_list; // device list
ssize_t num_devs = libusb_get_device_list(NULL, &dev_list); //get device list
if( num_devs < 0 )
return 1;
for( ssize_t i = 0; i < num_devs; i++ ){
// check if correct bus and device
if( bus == libusb_get_bus_number( dev_list[i] ) &&
device == libusb_get_device_address( dev_list[i] ) ){
// open device
if( libusb_open( dev_list[i], &_handle ) != 0 ){
return 1;
} else{
break;
}
}
}
//free device list, unreference devices
libusb_free_device_list( dev_list, 1 );
//detach kernel driver on interface 0 if active
if( libusb_kernel_driver_active( _handle, 0 ) ){
res += libusb_detach_kernel_driver( _handle, 0 );
if( res == 0 ){
_detached_driver_0 = true;
} else{
return res;
}
}
//detach kernel driver on interface 1 if active
if( libusb_kernel_driver_active( _handle, 1 ) ){
res += libusb_detach_kernel_driver( _handle, 1 );
if( res == 0 ){
_detached_driver_1 = true;
} else{
return res;
}
}
//detach kernel driver on interface 2 if active
if( libusb_kernel_driver_active( _handle, 2 ) ){
res += libusb_detach_kernel_driver( _handle, 2 );
if( res == 0 ){
_detached_driver_2 = true;
} else{
return res;
}
}
//claim interface 0
res += libusb_claim_interface( _handle, 0 );
if( res != 0 ){
return res;
}
//claim interface 1
res += libusb_claim_interface( _handle, 1 );
if( res != 0 ){
return res;
}
//claim interface 2
res += libusb_claim_interface( _handle, 2 );
if( res != 0 ){
return res;
}
return res;
}
//close mouse
int mouse_m908::close_mouse(){

View file

@ -97,6 +97,7 @@ class mouse_m908{
//helper functions
int open_mouse();
int open_mouse_bus_device( uint8_t bus, uint8_t device );
int close_mouse();
private:

View file

@ -20,11 +20,13 @@
//prints a help message
void print_help(){
std::cout << "Options:\n";
std::cout << "Options:\n\n";
std::cout << "-h --help\n\tDisplays this message.\n";
std::cout << "-c --config\n\tLoads and applies settings from specified file.\n";
std::cout << "-p --profile\n\tSets currently active profile (1-5).\n";
std::cout << "-m --macro\n\tSelects macro file for sending.\n";
std::cout << "-n --number\n\tSelects macro slot for sending (1-15).\n";
std::cout << "-b --bus\n\tUSB bus id, requires -d.\n";
std::cout << "-d --device\n\tUSB device number, requires -b.\n";
//std::cout << "-r --repeat\n\tSets number of times the macro will be repeated (1-255).\n";
}

View file

@ -23,6 +23,7 @@
#include <string>
#include <iostream>
#include <exception>
#include <regex>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <getopt.h>
@ -42,20 +43,24 @@ int main( int argc, char **argv ){
{"macro", required_argument, 0, 'm'},
{"number", required_argument, 0, 'n'},
//{"repeat", required_argument, 0, 'r'},
{"bus", required_argument, 0, 'b'},
{"device", required_argument, 0, 'd'},
{0, 0, 0, 0}
};
bool flag_config = false, flag_profile = false;
bool flag_macro = false, flag_number = false;
bool flag_bus = false, flag_device = false;
//bool flag_repeat;
std::string string_config, string_profile;
std::string string_macro, string_number;
//std::string string_repeat;
std::string string_bus, string_device;
//parse command line options
int c, option_index = 0;
//while( (c = getopt_long( argc, argv, "hc:p:m:n:r:",
while( (c = getopt_long( argc, argv, "hc:p:m:n:",
while( (c = getopt_long( argc, argv, "hc:p:m:n:b:d:",
long_options, &option_index ) ) != -1 ){
switch( c ){
@ -79,6 +84,14 @@ int main( int argc, char **argv ){
flag_number = true;
string_number = optarg;
break;
case 'b':
flag_bus = true;
string_bus = optarg;
break;
case 'd':
flag_device = true;
string_device = optarg;
break;
//case 'r':
// flag_repeat = true;
// string_repeat = optarg;
@ -388,13 +401,40 @@ int main( int argc, char **argv ){
if( pt.get("profile5.report_rate", "") == "500" ){ m.set_report_rate( mouse_m908::profile_5, mouse_m908::r_500Hz ); }
if( pt.get("profile5.report_rate", "") == "1000" ){ m.set_report_rate( mouse_m908::profile_5, mouse_m908::r_1000Hz ); }
int r;
r = m.open_mouse();
if( r != 0 ){
std::cout << "Couldn't open mouse\n";
// open mouse
if( flag_bus != flag_device ){ // improper arguments
std::cout << "Missing argument, --bus and --device must be used together.\n";
return 1;
} else if( flag_bus && flag_device ){ // open with bus and device
if( !std::regex_match( string_bus, std::regex("[0-9]+") ) ||
!std::regex_match( string_device, std::regex("[0-9]+") ) ){
std::cout << "Wrong argument, expected number.\n";
return 1;
}
int r;
r = m.open_mouse_bus_device( stoi(string_bus), stoi(string_device) );
if( r != 0 ){
std::cout << "Couldn't open mouse\n";
return 1;
}
} else{ // open with vid and pid
int r;
r = m.open_mouse();
if( r != 0 ){
std::cout << "Couldn't open mouse\n";
return 1;
}
}
// write settings
m.write_settings();
m.close_mouse();
@ -405,27 +445,59 @@ int main( int argc, char **argv ){
}
// change active profile
if( flag_profile ){
// set profile
if( string_profile == "1" ){ m.set_profile( mouse_m908::profile_1 ); }
if( string_profile == "2" ){ m.set_profile( mouse_m908::profile_2 ); }
if( string_profile == "3" ){ m.set_profile( mouse_m908::profile_3 ); }
if( string_profile == "4" ){ m.set_profile( mouse_m908::profile_4 ); }
if( string_profile == "5" ){ m.set_profile( mouse_m908::profile_5 ); }
int r;
r = m.open_mouse();
if( r != 0 ){
std::cout << "Couldn't open mouse\n";
// open mouse
if( flag_bus != flag_device ){ // improper arguments
std::cout << "Missing argument, --bus and --device must be used together.\n";
return 1;
} else if( flag_bus && flag_device ){ // open with bus and device
if( !std::regex_match( string_bus, std::regex("[0-9]+") ) ||
!std::regex_match( string_device, std::regex("[0-9]+") ) ){
std::cout << "Wrong argument, expected number.\n";
return 1;
}
int r;
r = m.open_mouse_bus_device( stoi(string_bus), stoi(string_device) );
if( r != 0 ){
std::cout << "Couldn't open mouse\n";
return 1;
}
} else{ // open with vid and pid
int r;
r = m.open_mouse();
if( r != 0 ){
std::cout << "Couldn't open mouse\n";
return 1;
}
}
// write profile
m.write_profile();
m.close_mouse();
}
// send macro
if( flag_macro && flag_number ){
int r;
@ -438,12 +510,40 @@ int main( int argc, char **argv ){
return 1;
}
r = m.open_mouse();
if( r != 0 ){
std::cout << "Couldn't open mouse\n";
// open mouse
if( flag_bus != flag_device ){ // improper arguments
std::cout << "Missing argument, --bus and --device must be used together.\n";
return 1;
} else if( flag_bus && flag_device ){ // open with bus and device
if( !std::regex_match( string_bus, std::regex("[0-9]+") ) ||
!std::regex_match( string_device, std::regex("[0-9]+") ) ){
std::cout << "Wrong argument, expected number.\n";
return 1;
}
int r;
r = m.open_mouse_bus_device( stoi(string_bus), stoi(string_device) );
if( r != 0 ){
std::cout << "Couldn't open mouse\n";
return 1;
}
} else{ // open with vid and pid
int r;
r = m.open_mouse();
if( r != 0 ){
std::cout << "Couldn't open mouse\n";
return 1;
}
}
// write macro
m.write_macro(number);
/*if( flag_repeat ){
@ -459,7 +559,7 @@ int main( int argc, char **argv ){
m.close_mouse();
} else if( flag_macro || flag_number ){
std::cout << "Misssing option\n";
std::cout << "Misssing option, --macro and --number must be used together.\n";
}
return 0;