Blame view

main.cpp 2.57 KB
Imanol-Mikel Barba Sabariego authored
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <libusb-1.0/libusb.h>
#include <iomanip>
#include <cstdlib>

#define MOUSE_PROTOCOL 2

using namespace std;

void disable_touchpad()
{
12
   if(system("which synclient > /dev/null") == 0)
Imanol-Mikel Barba Sabariego authored
13
   {
14
       system("synclient TouchpadOff=1 > /dev/null");
Imanol-Mikel Barba Sabariego authored
15
16
17
18
19
20
21
22
23
24
   }
   else
   {
       cout << "Synaptics touchpad not detected or Xorg synaptics driver not installed" << endl;
   }
}


void enable_touchpad()
{
25
    if(system("which synclient > /dev/null") == 0)
Imanol-Mikel Barba Sabariego authored
26
    {
27
        system("synclient TouchpadOff=0 > /dev/null");
Imanol-Mikel Barba Sabariego authored
28
29
30
31
32
33
34
35
36
    }
    else
    {
        cout << "Synaptics touchpad not detected or Xorg synaptics driver not installed" << endl;
    }
}

int main()
{
37
38
    void (*touchpad_operation)() = &enable_touchpad;
    bool mouseFound = false;
Imanol-Mikel Barba Sabariego authored
39
40
41
42
43
44
45
46
47
48
49
    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);
50
    for(int i = 0; (i < cnt) && (!mouseFound); i++)
Imanol-Mikel Barba Sabariego authored
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    {
        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)
                {
80
81
82
		    mouseFound = true;
                    cout << " Mouse detected! Disabling touchpad...";
                    touchpad_operation = &disable_touchpad;
Imanol-Mikel Barba Sabariego authored
83
84
85
86
87
88
                }
            }
        }
        libusb_free_config_descriptor(config);
        cout << endl;
    }
89
90
91
92
93
    if(!mouseFound)
    {
        cout << "No mouse detected. Enabling touchpad..." << endl;
    }
    (*touchpad_operation)();
Imanol-Mikel Barba Sabariego authored
94
95
96
    libusb_free_device_list(devs, 1);
    libusb_exit(ctx);
    return 0;
97
98
}