// // Created by maxmati on 4/18/15. // // DPI change support added by SouceCalve on 27/11/25. #include "Mouse.h" #include #include using std::cout; using std::endl; void Mouse::init() { int ret = libusb_init(&context); if(ret < 0) { cout<<"Init Error "<(libusb_get_device_address(devs[i]), currentDevice)); } } libusb_free_device_list(devs, 1); if(devices.size() == 0){ cout<<"No suitable device found."<second; } bool Mouse::isCompatibleDevice(libusb_device_descriptor &desc) { if(desc.idVendor != A4TECH_VID) return false; for(size_t i = 0; i < COMPATIBLE_PIDS_SIZE; ++i) if(desc.idProduct == COMPATIBLE_PIDS[i]) return true; return false; } Mouse::~Mouse() { for (auto& dev : devices) libusb_close(dev.second); if(context != nullptr) libusb_exit(context); } int Mouse::setBackLightLevel(uint8_t level) { uint8_t data[72] = {A4TECH_MAGIC, BACKLIGHT_OPCODE,0x00,0x00, BACKLIGHT_WRITE,0x00,0x00,0x00, level, 0x00, }; if(level < 0 || level > 3){ return -1; } if(writeToMouse(data, sizeof(data)) < 0) return -2; return 0; } int Mouse::writeToMouse(uint8_t data[], size_t size) { int res = libusb_control_transfer(currentDevice, 0x21, 9, 0x0307, 2, data, size, 10000); switch (res){ case LIBUSB_ERROR_TIMEOUT: cout << "LIBUSB_ERROR_TIMEOUT" << endl; return -1; case LIBUSB_ERROR_PIPE: cout << "LIBUSB_ERROR_PIPE" << endl; return -1; case LIBUSB_ERROR_NO_DEVICE: cout << "LIBUSB_ERROR_NO_DEVICE" << endl; return -1; case LIBUSB_ERROR_BUSY: cout << "LIBUSB_ERROR_BUSY" << endl; return -1; case LIBUSB_ERROR_INVALID_PARAM: cout << "LIBUSB_ERROR_INVALID_PARAM" << endl; default: return 0; } } int Mouse::readFromMouse(uint8_t *request, size_t requestSize, uint8_t *response, size_t responseSize) { if(writeToMouse(request, requestSize) < 0){ return -1; } int res = libusb_control_transfer(currentDevice,0xa1,1, 0x0307,2,response,responseSize,10000); if(res < 0){ cout<<"Unnable to receive data"< 40) dpi=32; uint8_t data[72] = {A4TECH_MAGIC, DPI_SET_OPCODE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1a,0x1a,0x88,0x02,0x00, }; memcpy(data + 10, dpi_binary+dpi*3, 3); return writeToMouse(data, sizeof(data)); } void Mouse::listDevices() { std::cout<<"Available devices:"<