main.cpp
2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#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;
}