Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Simple hack to change dpi on Logitech G400s
- // G400s only support: 400, 800, 1600, 2000 and 4000 dpi, all other are interpolate by software
- //
- // Based on g400_hack from Przemek Aksamit
- // https://bitbucket.org/extaliones/g400_hack/src
- //
- // gcc -lusb-1.0 -o g400s_hack g400s_hack.c
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <libusb-1.0/libusb.h>
- #define G400S_VENDOR_ID 0x046d
- #define G400S_PRODUCT_ID 0xc24c
- #define EXIT_SUCCESS 0
- #define EXIT_ERROR_ARGS 1
- #define EXIT_ERROR_DEVICE 2
- #define EXIT_ERROR_KERNEL 3
- #define EXIT_ERROR_CLAIM 4
- #define EXIT_ERROR_WRITE 5
- libusb_context *ctx = NULL;
- libusb_device_handle *handle = NULL;
- int detached = 0;
- int claimed = 0;
- void usage()
- {
- printf("Usage: ./g400s_hack ( -400 | -800 | -1600 | -2000 | -4000 )\n");
- printf("Highly experimental, use with caution.\n");
- }
- void cleanup()
- {
- if (claimed)
- libusb_release_interface(handle, 1);
- if (detached)
- libusb_attach_kernel_driver(handle, 1);
- libusb_close(handle);
- libusb_exit(ctx);
- }
- int main(int argc, char **argv)
- {
- if (argc != 2 || argv[1][0] != '-') {
- usage();
- return EXIT_ERROR_ARGS;
- }
- //131 => 400dpi, 132 => 800dpi, 133 => 1600dpi, 134 => 2000dpi, 135 => 4000dpi
- uint16_t dpi_idx = 131;
- if (strcmp(&argv[1][1], "400" ) == 0)
- dpi_idx += 0;
- else if (strcmp(&argv[1][1], "800" ) == 0)
- dpi_idx += 1;
- else if (strcmp(&argv[1][1], "1600") == 0)
- dpi_idx += 2;
- else if (strcmp(&argv[1][1], "2000") == 0)
- dpi_idx += 3;
- else if (strcmp(&argv[1][1], "4000") == 0)
- dpi_idx += 4;
- else {
- usage();
- return EXIT_ERROR_ARGS;
- }
- printf("Set new DPI: %s (index %i)\n", &argv[1][1], dpi_idx);
- libusb_init(&ctx);
- handle = libusb_open_device_with_vid_pid(ctx, G400S_VENDOR_ID, G400S_PRODUCT_ID);
- libusb_set_debug(ctx, 3);
- if (!handle) {
- fprintf(stderr, "Logitech G400s not found! (Do you have usb rights? Try with sudo?)\n");
- cleanup();
- return EXIT_ERROR_DEVICE;
- }
- if (libusb_kernel_driver_active(handle, 1) == 1) {
- if (libusb_detach_kernel_driver(handle, 1) != 0) {
- fprintf(stderr, "Can't detach kernel driver.\n");
- cleanup();
- return EXIT_ERROR_KERNEL;
- }
- detached = 1;
- }
- if (libusb_claim_interface(handle, 1) != 0) {
- fprintf(stderr, "Failed to claim interface.\n");
- cleanup();
- return EXIT_ERROR_CLAIM;
- }
- claimed = 1;
- if (libusb_control_transfer (handle, 0x40, 2, 0x008e, dpi_idx, NULL, 0, 1000) != 0) {
- fprintf(stderr, "Error writing to USB device.\n");
- cleanup();
- return EXIT_ERROR_WRITE;
- }
- cleanup();
- printf("Finished.\n");
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment