struct usb_device_info * usb_open_and_wait_for_device(void) { libusb_device **devs; libusb_device **dev; struct usb_device_info * ret = NULL; int i = 0; void (*prev)(int); static char progress[] = {'/','-','\\', '|'}; if ( libusb_init(NULL) < 0 ) { PRINTF_LINE("libusb_init failed"); PRINTF_END(); return NULL; } PRINTF_BACK(); printf("\n"); signal_quit = 0; prev = signal(SIGINT, signal_handler); while ( ! signal_quit ) { PRINTF_LINE("Waiting for USB device... %c", progress[++i%sizeof(progress)]); if ( libusb_get_device_list(NULL, &devs) < 0 ) { PRINTF_LINE("Listing USB devices failed"); PRINTF_END(); break; } for ( dev = devs; *dev != NULL; ++dev ) { ret = usb_device_is_valid(*dev); if ( ret ) break; } libusb_free_device_list(devs, 1); if ( ret ) break; SLEEP(0xc350); // 0.5s } if ( prev != SIG_ERR ) signal(SIGINT, prev); PRINTF_BACK(); printf("\n"); if ( ! ret ) return NULL; return ret; }
struct usb_device_info * usb_open_and_wait_for_device(void) { struct usb_bus * bus; struct usb_device_info * ret = NULL; int i = 0; static char progress[] = {'/','-','\\', '|'}; usb_init(); usb_find_busses(); PRINTF_BACK(); printf("\n"); while ( 1 ) { PRINTF_LINE("Waiting for USB device... %c", progress[++i%sizeof(progress)]); usb_find_devices(); for ( bus = usb_get_busses(); bus; bus = bus->next ) { if ( bus->root_dev ) ret = usb_search_device(bus->root_dev, 0); else { struct usb_device *dev; for ( dev = bus->devices; dev; dev = dev->next ) { ret = usb_search_device(dev, 0); if ( ret ) break; } } if ( ret ) break; } if ( ret ) break; usleep(0xc350); // 0.5s } PRINTF_BACK(); printf("\n"); if ( ! ret ) return NULL; return ret; }
void printf_progressbar(unsigned long long part, unsigned long long total) { char *columns = getenv("COLUMNS"); int pc; int tmp, cols = 80; /* percentage calculation */ pc = total == 0 ? 100 : (int)(part*100/total); ( pc < 0 ) ? pc = 0 : ( pc > 100 ) ? pc = 100 : 0; PRINTF_BACK(); PRINTF_ADD("\x1b[K %3d%% [", pc); if ( columns ) cols = atoi(columns); if ( cols > 115 ) cols = 115; cols-=15; for ( tmp = cols*pc/100; tmp; tmp-- ) PRINTF_ADD("#"); for ( tmp = cols-(cols*pc/100); tmp; tmp-- ) PRINTF_ADD("-"); PRINTF_ADD("]"); if ( part == total ) PRINTF_END(); fflush(stdout); }