main.cpp 2.57 KB
#include <iostream>
#include <libusb-1.0/libusb.h>
#include <iomanip>
#include <cstdlib>

#define MOUSE_PROTOCOL 2

using namespace std;

void disable_touchpad()
{
   if(system("which synclient > /dev/null") == 0)
   {
       system("synclient TouchpadOff=1 > /dev/null");
   }
   else
   {
       cout << "Synaptics touchpad not detected or Xorg synaptics driver not installed" << endl;
   }
}


void enable_touchpad()
{
    if(system("which synclient > /dev/null") == 0)
    {
        system("synclient TouchpadOff=0 > /dev/null");
    }
    else
    {
        cout << "Synaptics touchpad not detected or Xorg synaptics driver not installed" << endl;
    }
}

int main()
{
    void (*touchpad_operation)() = &enable_touchpad;
    bool mouseFound = false;
    libusb_context *ctx = NULL;
    libusb_device **devs;
    ssize_t cnt;
    int r = libusb_init(&ctx);
    if(r < 0)
    {
        cout << "Init Error " << r << endl;
        return 1;
    }
    libusb_set_debug(ctx, 3);
    cnt = libusb_get_device_list(ctx, &devs);
    for(int i = 0; (i < cnt) && (!mouseFound); i++)
    {
        ios state(NULL);
        state.copyfmt(cout);
        libusb_device_descriptor desc;
        r = libusb_get_device_descriptor(devs[i], &desc);
        if(r < 0)
        {
            cout << "failed to get device descriptor" << endl;
            continue;
        }
        cout << "Scanning ";
        cout << setfill('0') << setw(4) << hex << desc.idVendor;
        cout << ":";
        cout << setfill('0') << setw(4) << hex << desc.idProduct;
        cout << "...";
        cout.copyfmt(state);
        libusb_config_descriptor *config;
        libusb_get_config_descriptor(devs[i], 0, &config);
        const libusb_interface *inter;
        const libusb_interface_descriptor *interdesc;
        for(int j = 0; j < (int)config->bNumInterfaces; j++)
        {
            inter = &config->interface[j];
            for(int k = 0; k < inter->num_altsetting; k++)
            {
                interdesc = &inter->altsetting[k];
                uint8_t protocol = interdesc->bInterfaceProtocol;
                if(protocol == MOUSE_PROTOCOL)
                {
		    mouseFound = true;
                    cout << " Mouse detected! Disabling touchpad...";
                    touchpad_operation = &disable_touchpad;
                }
            }
        }
        libusb_free_config_descriptor(config);
        cout << endl;
    }
    if(!mouseFound)
    {
        cout << "No mouse detected. Enabling touchpad..." << endl;
    }
    (*touchpad_operation)();
    libusb_free_device_list(devs, 1);
    libusb_exit(ctx);
    return 0;
}