@@ -391,15 +391,44 @@ const int report_rate[] = {
391391};
392392const int n_report_rates = 4 ;
393393
394- static int dpi_point (int dpip ) {
395- return dpip * 250 ;
396- }
394+
397395
398396libusb_context * usb_ctx = NULL ;
399397libusb_device_handle * usb_dev_handle = NULL ;
400398libusb_device * usb_device = NULL ;
401399struct libusb_device_descriptor usb_desc ;
402400int usb_interface_index = -1 ;
401+ int mouse_primed = 0 ;
402+
403+
404+
405+ static int dpi_point (int dpip );
406+ static void help_version (void );
407+ static void help_usage (void );
408+ static void keylist_print (void );
409+ static libusb_context * usb_init (void );
410+ static int usb_deinit (void );
411+ static libusb_device_handle * mouse_init (const uint16_t vendor_id , const uint16_t product_id , const char * product_name );
412+ static int mouse_deinit (void );
413+ static void display_mouse_hid (const uint16_t vendor_id , const uint16_t product_id );
414+ int mouse_hid_detach_kernel (int iface );
415+ int mouse_hid_attach_kernel (int iface );
416+ static t_mode change_mode (libusb_device_handle * usb_dev_handle , t_mode mode );
417+ static int mode_load (unsigned char * mode_data , libusb_device_handle * usb_dev_handle , t_mode mode );
418+ static int mode_save (unsigned char * mode_data , libusb_device_handle * usb_dev_handle , const t_mode mode );
419+ static int mode_print (unsigned char * mode_data , int len );
420+ static int set_mode_rate (unsigned char * mode_data , const int rate );
421+ static unsigned char set_mode_colour (unsigned char * mode_data , const t_colour colour );
422+ static int set_mode_button (unsigned char * mode_data , const unsigned char button , const char * keys );
423+ static int mouse_editmode (void );
424+ int mouse_prime (void );
425+ int mouse_unprime (void );
426+
427+
428+
429+ static int dpi_point (int dpip ) {
430+ return dpip * 250 ;
431+ }
403432
404433static void help_version (void ) {
405434 printf ("%s v%s (BUILT: %s)\n" , (APP_NAME ), (APP_VERSION ), (BUILD_DATE ));
@@ -740,7 +769,7 @@ static t_mode change_mode(libusb_device_handle *usb_dev_handle, t_mode mode) {
740769
741770 int ret ;
742771
743- if (!usb_dev_handle || mode >= mode_COUNT ) return mode_COUNT ;
772+ if (!mouse_primed || ! usb_dev_handle || mode >= mode_COUNT ) return mode_COUNT ;
744773
745774 if (mode == mode_f3 ) {
746775 // Top Mode
@@ -791,7 +820,7 @@ static int mode_load(unsigned char *mode_data, libusb_device_handle *usb_dev_han
791820 uint16_t mi ;
792821 int ret ;
793822
794- if (!mode_data || !usb_dev_handle || mode >= mode_COUNT ) return 0 ;
823+ if (!mouse_primed || ! mode_data || !usb_dev_handle || mode >= mode_COUNT ) return 0 ;
795824
796825 if (mode == mode_f3 ) mi = 0xf3 ;
797826 else if (mode == mode_f4 ) mi = 0xf4 ;
@@ -838,7 +867,7 @@ static int mode_save(unsigned char *mode_data, libusb_device_handle *usb_dev_han
838867
839868 unsigned char cmp [255 ];
840869
841- if (!mode_data || !usb_dev_handle || mode >= mode_COUNT ) return 0 ;
870+ if (!mouse_primed || ! mode_data || !usb_dev_handle || mode >= mode_COUNT ) return 0 ;
842871
843872 if (mode == mode_f3 ) mi = 0xf3 ;
844873 else if (mode == mode_f4 ) mi = 0xf4 ;
@@ -1094,6 +1123,8 @@ static int set_mode_button(unsigned char *mode_data, const unsigned char button,
10941123 ++ ky ;
10951124 }
10961125
1126+ // FIXME: QB#125 - Handle Keys containing '+' (currently only "Num+")
1127+
10971128 // If it's not the last element...
10981129 if (* kptr ) {
10991130 // And didn't match a modifier, BAD!
@@ -1148,8 +1179,9 @@ static int set_mode_button(unsigned char *mode_data, const unsigned char button,
11481179
11491180 return 1 ;
11501181}
1182+
11511183static int mouse_editmode (void ) {
1152- if (!usb_dev_handle ) return 0 ;
1184+ if (!mouse_primed || ! usb_dev_handle ) return 0 ;
11531185
11541186 // LAUNCH EDITOR
11551187 // 2117030035 S Co:2:039:0 s 21 09 03f0 0001 0004 4 = f0423900
@@ -1189,21 +1221,8 @@ static int mouse_editmode(void) {
11891221 return 1 ;
11901222}
11911223
1192- int main (int argc , char * argv []) {
1193- t_exit ret = exit_none ;
1194- int c ;
1195-
1196- t_mode mode = mode_COUNT ;
1197-
1198- // Data structures to store loaded and ready to save mode data
1199- unsigned char mode_data_l [255 ];
1200- unsigned char mode_data_s [255 ];
1201-
1202- log_init ();
1203-
1204- help_version ();
1205-
1206- // FIXME: Help and version shouldn't initialise USB
1224+ int mouse_prime (void ) {
1225+ if (mouse_primed ) return exit_none ;
12071226
12081227 // Initialise USB
12091228 usb_init ();
@@ -1239,6 +1258,43 @@ int main (int argc, char *argv[]) {
12391258 return exit_usberr ;
12401259 }
12411260
1261+ mouse_primed = 1 ;
1262+
1263+ return exit_none ;
1264+ }
1265+
1266+ int mouse_unprime (void ) {
1267+ if (!mouse_primed ) return exit_none ;
1268+
1269+ // Re-attach kernel driver
1270+ printf ("Attaching kernel driver...\n" );
1271+ mouse_hid_attach_kernel (usb_interface_index );
1272+
1273+ // De-initialise mouse
1274+ mouse_deinit ();
1275+
1276+ // De-initialise USB
1277+ usb_deinit ();
1278+
1279+ mouse_primed = 0 ;
1280+
1281+ return exit_none ;
1282+ }
1283+
1284+ int main (int argc , char * argv []) {
1285+ t_exit ret = exit_none ;
1286+ int c ;
1287+
1288+ t_mode mode = mode_COUNT ;
1289+
1290+ // Data structures to store loaded and ready to save mode data
1291+ unsigned char mode_data_l [255 ];
1292+ unsigned char mode_data_s [255 ];
1293+
1294+ log_init ();
1295+
1296+ help_version ();
1297+
12421298 while (1 ) {
12431299 int option_index = 0 ;
12441300 static struct option long_options [] = {
@@ -1285,8 +1341,8 @@ int main (int argc, char *argv[]) {
12851341 c = getopt_long (argc , argv , "hVs:p:m:r:c:1:2:3:4:5:6:7:8:9:" ,
12861342 long_options , & option_index );
12871343
1288- if ( c == -1 )
1289- break ;
1344+ // If we've had a previous error, or there's not more options, break
1345+ if ( ret != exit_none || c == -1 ) break ;
12901346
12911347 switch (c ) {
12921348 // Help
@@ -1326,6 +1382,10 @@ int main (int argc, char *argv[]) {
13261382
13271383 printf ("Mode Selection Specified: %s\n" , s_mode [mnew ]);
13281384
1385+ // Initialise USB and mouse, detach kernel driver (if necessary)
1386+ // If we cannot, abort (caught at start of loop)
1387+ if ((ret = mouse_prime ())) continue ;
1388+
13291389 if (mode != mode_COUNT ) {
13301390 // They've been editing another mode, so save
13311391 printf ("Saving Mode: %s\n" , s_mode [mode ]);
@@ -1360,6 +1420,10 @@ int main (int argc, char *argv[]) {
13601420 continue ;
13611421 }
13621422
1423+ // Initialise USB and mouse, detach kernel driver (if necessary)
1424+ // If we cannot, abort (caught at start of loop)
1425+ if ((ret = mouse_prime ())) continue ;
1426+
13631427 if (mode != mode_COUNT ) {
13641428 // They've been editing another mode, so save
13651429 printf ("Saving Mode: %s\n" , s_mode [mode ]);
@@ -1369,8 +1433,9 @@ int main (int argc, char *argv[]) {
13691433
13701434 printf ("Printing Mode: %s\n" , s_mode [mnew ]);
13711435
1372- len = mode_load (& mode_data_p [0 ], usb_dev_handle , mnew );
1373- mode_print (& mode_data_p [0 ], len );
1436+ if ((len = mode_load (& mode_data_p [0 ], usb_dev_handle , mnew )) > 0 ) {
1437+ mode_print (& mode_data_p [0 ], len );
1438+ }
13741439 }
13751440 break ;
13761441
@@ -1379,6 +1444,10 @@ int main (int argc, char *argv[]) {
13791444 {
13801445 t_mode mnew = mode_COUNT ;
13811446
1447+ // Initialise USB and mouse, detach kernel driver (if necessary)
1448+ // If we cannot, abort (caught at start of loop)
1449+ if ((ret = mouse_prime ())) continue ;
1450+
13821451 if (!optarg ) {
13831452 elog ("ERROR: Mode required for modify option\n" );
13841453
@@ -1422,8 +1491,9 @@ int main (int argc, char *argv[]) {
14221491
14231492 mouse_editmode ();
14241493
1425- mode_load (& mode_data_l [0 ], usb_dev_handle , mode );
1426- memcpy (& mode_data_s , & mode_data_l , 255 );
1494+ if (mode_load (& mode_data_l [0 ], usb_dev_handle , mode ) > 0 ) {
1495+ memcpy (& mode_data_s , & mode_data_l , 255 );
1496+ }
14271497 }
14281498 break ;
14291499
@@ -1523,7 +1593,7 @@ int main (int argc, char *argv[]) {
15231593 } // switch (c)
15241594 } // while (1)
15251595
1526- if (optind < argc ) {
1596+ if (ret == exit_none && optind < argc ) {
15271597 char optout [255 ]
15281598 ,* po = & optout [0 ];
15291599
@@ -1542,16 +1612,8 @@ int main (int argc, char *argv[]) {
15421612 mode = mode_COUNT ;
15431613 }
15441614
1545-
1546- // Re-attach kernel driver
1547- printf ("Attaching kernel driver...\n" );
1548- mouse_hid_attach_kernel (usb_interface_index );
1549-
1550- // De-initialise mouse
1551- mouse_deinit ();
1552-
1553- // De-initialise USB
1554- usb_deinit ();
1615+ // Re-attach kernel driver, de-initialise mouse and USB (if necessary)
1616+ mouse_unprime ();
15551617
15561618 log_end ();
15571619
0 commit comments