P
P
pepl2132018-10-11 13:12:16
C++ / C#
pepl213, 2018-10-11 13:12:16

Why is the application freezing?

#include <libusb.h> // for libusb_init
#include <stdlib.h> // for exit
#include <stdio.h> // for printf
#include <X11/Xlib.h>
#include <string.h>

#define VID 0x046d
#define PID 0xc216
#define SENSETIVE 0.01f
void coords (Display *display, int *x, int *y);
void move_to (Display *display, int x, int y)
{
  int cur_x, cur_y;
  coords (display, &cur_x, &cur_y);
  XWarpPointer (display, None, None, 0,0,0,0, -cur_x, -cur_y);
  XWarpPointer (display, None, None, 0,0,0,0, x, y);
  usleep (1);
}
void coords (Display *display, int *x, int *y)
{
  XEvent event;
  XQueryPointer (display, DefaultRootWindow (display),
                 &event.xbutton.root, &event.xbutton.window,
                 &event.xbutton.x_root, &event.xbutton.y_root,
                 &event.xbutton.x, &event.xbutton.y,
                 &event.xbutton.state);
  *x = event.xbutton.x;
  *y = event.xbutton.y;

}

int main()
{
    Display *display = XOpenDisplay (NULL);
    libusb_device **list = NULL;
    ssize_t number_of_devices = 0;
    if(libusb_init(NULL) != 0)
    {
        fprintf(stderr, "%s:%d: unable to initialize libusb\n", __FILE__, __LINE__);
        exit(EXIT_FAILURE);
    }
    number_of_devices = libusb_get_device_list(NULL, &list);
    if(number_of_devices < 0)
    {
        fprintf(stderr, "%s:%d: unable to get the list of USB devices\n", __FILE__, __LINE__);
        libusb_exit(NULL);
        exit(EXIT_FAILURE);
    }
    printf("number_of_devices = %ld\n", number_of_devices);
    int index = 0;
    for(index = 0; index < number_of_devices; ++index)
    {
        libusb_device *device = list[index];
        struct libusb_device_descriptor desc = {0};
        if(libusb_get_device_descriptor(device, &desc) != 0)
        {
            fprintf(stderr, "%s:%d: unable to get the USB device descriptor\n", __FILE__, __LINE__);
            libusb_free_device_list(list, 1);
            libusb_exit(NULL);
            exit(EXIT_FAILURE);
        }
        printf("%d/%ld: idVendor: %04x, idProduct: %04x\n", index + 1, number_of_devices, desc.idVendor, desc.idProduct);
        /*if(desc.idVendor == 0x045e && desc.idProduct == 0x028e)*/
        if(desc.idVendor == VID && desc.idProduct == PID)
        {
            printf("USB device found!\n");
            printf("Opening the USB device...\n");
            libusb_device_handle *handle = NULL;
            if(libusb_open(device, &handle) != 0)
            {
                fprintf(stderr, "%s:%d: unable to open the USB device\n", __FILE__, __LINE__);
                libusb_free_device_list(list, 1);
                libusb_exit(NULL);
                exit(EXIT_FAILURE);
            }
            printf("Done!\n");
            if(libusb_kernel_driver_active(handle, 0) != 0)
            {
                printf("Kernel driver is active!\n");
                printf("Detaching the kernel driver...\n");
                if(libusb_detach_kernel_driver(handle, 0) != 0)
                {
                    fprintf(stderr, "%s:%d: unable to detach the kernel driver from the interface of the USB device\n", __FILE__, __LINE__);
                    libusb_close(handle);
                    libusb_free_device_list(list, 1);
                    libusb_exit(NULL);
                    exit(EXIT_FAILURE);
                }
                printf("Done!\n");
            }
            printf("Claiming the interface...\n");
            if(libusb_claim_interface(handle, 0) != 0)
            {
                fprintf(stderr, "%s:%d: unable to claim the interface of the USB device\n", __FILE__, __LINE__);
                libusb_close(handle);
                libusb_free_device_list(list, 1);
                libusb_exit(NULL);
                exit(EXIT_FAILURE);
            }
            printf("Done!\n");
            unsigned char data[8];
            int actual_length;
            printf("Reading data...\n");
            while(1)
            {
                int x,y;
                coords(display, &x, &y);
                int r = libusb_bulk_transfer(handle, 0x81, data, sizeof(data), &actual_length, 1);
                int coord_x = ((float)data[0]-127)*SENSETIVE;
                int coord_y = ((float)data[1]-127)*SENSETIVE;
                int new_coord_X = x+coord_x;
                int new_coord_Y = y+coord_y;
                move_to(display, new_coord_X, new_coord_Y);
                printf("Data: ");
                for (int i=0; i<sizeof(data)-1;i++)
                    printf("%d ", data[i]);
                printf("\n");
                

            }
            printf("Releasing the interface...\n");
            if(libusb_release_interface(handle, 0) != 0)
            {
                fprintf(stderr, "%s:%d: unable to detach the kernel driver from the interface of the USB device\n", __FILE__, __LINE__);
                libusb_close(handle);
                libusb_free_device_list(list, 1);
                libusb_exit(NULL);
                exit(EXIT_FAILURE);
            }
            printf("Done!\n");
            printf("Closing the USB device...\n");
            libusb_close(handle);
            printf("Done!\n");
        }
    }
    libusb_free_device_list(list, 1);
    libusb_exit(NULL);
    exit(EXIT_SUCCESS);
}

This code reads the value from the joystick and moves the mouse to the same values. BUT
1) the application freezes
2) it is not possible to move the mouse diagonally.
Why?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question