4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
– |
#include <cassert> |
7 |
|
#include <iostream> |
9 |
– |
#include <set> |
10 |
– |
#include <sstream> |
8 |
|
#include <string> |
9 |
|
|
13 |
– |
#include <dev/usb/usb.h> |
14 |
– |
#include <dev/usb/usbhid.h> |
10 |
|
#include <err.h> |
11 |
|
#include <fcntl.h> |
17 |
– |
#include <sys/ioctl.h> |
12 |
|
#include <usbhid.h> |
13 |
|
|
14 |
+ |
#include <common.hpp> |
15 |
|
#include <foreach.hpp> |
16 |
|
#include <posix.hpp> |
17 |
+ |
#include <regex.hpp> |
18 |
|
|
19 |
< |
inline std::ostream &operator <<(std::ostream &output, uint8_t number) |
19 |
> |
int Usage(const std::string &program) |
20 |
|
{ |
21 |
< |
return output << unsigned(number); |
21 |
> |
std::cout << _B("Usage: ") << program << _B(" [-debug] [-device=device]") << std::endl; |
22 |
> |
|
23 |
> |
return 1; |
24 |
|
} |
25 |
|
|
26 |
< |
std::set<std::string> AirClicks() |
26 |
> |
int main(int argc, char *argv[]) |
27 |
|
{ |
28 |
< |
std::set<std::string> airclicks; |
28 |
> |
bool debug(false); |
29 |
> |
std::string device; |
30 |
|
|
32 |
– |
_forall (unsigned, number, 0, 10) |
31 |
|
{ |
32 |
< |
std::ostringstream device_; |
33 |
< |
|
34 |
< |
device_ << "/dev/usb" << number; |
35 |
< |
|
36 |
< |
const std::string &device(device_.str()); |
37 |
< |
|
38 |
< |
try |
39 |
< |
{ |
40 |
< |
int usb(Posix::CheckError(::open(device.c_str(), O_RDONLY))); |
41 |
< |
|
42 |
< |
_forall (uint8_t, address, 1, USB_MAX_DEVICES) |
43 |
< |
{ |
44 |
< |
usb_device_info info; |
45 |
< |
|
46 |
< |
info.udi_addr = address; |
49 |
< |
|
50 |
< |
try |
51 |
< |
{ |
52 |
< |
Posix::CheckError(::ioctl(usb, USB_DEVICEINFO, &info)); |
53 |
< |
} |
54 |
< |
catch (const Posix::Error &error) |
55 |
< |
{ |
56 |
< |
if (error.GetCode() != ENXIO) |
57 |
< |
::warn("%s: address %u", device.c_str(), address); |
58 |
< |
|
59 |
< |
continue; |
60 |
< |
} |
61 |
< |
|
62 |
< |
std::cout << info.udi_product << std::endl; |
63 |
< |
} |
64 |
< |
|
65 |
< |
Posix::CheckError(::close(usb)); |
66 |
< |
} |
67 |
< |
catch (const Posix::Error &error) |
68 |
< |
{ |
69 |
< |
switch (error.GetCode()) |
70 |
< |
{ |
71 |
< |
case ENOENT: |
72 |
< |
case ENXIO: |
73 |
< |
continue; |
74 |
< |
default: |
75 |
< |
::warn("%s", device.c_str()); |
76 |
< |
} |
77 |
< |
} |
32 |
> |
Pcre::RegEx devicePath(_B("^-device=(/.+)$")); |
33 |
> |
Pcre::RegEx deviceNumber(_B("^-device=([0-9]+)$")); |
34 |
> |
Pcre::RegEx deviceName(_B("^-device=(.+)$")); |
35 |
> |
|
36 |
> |
_forall (int, index, 1, argc) |
37 |
> |
if (argv[index] == _B("-debug")) |
38 |
> |
debug = true; |
39 |
> |
else if (Pcre::RegEx::Match match = devicePath(argv[index])) |
40 |
> |
device = match[1]; |
41 |
> |
else if (Pcre::RegEx::Match match = deviceNumber(argv[index])) |
42 |
> |
device = _B("/dev/uhid") + match[1]; |
43 |
> |
else if (Pcre::RegEx::Match match = deviceName(argv[index])) |
44 |
> |
device = _B("/dev/") + match[1]; |
45 |
> |
else |
46 |
> |
return Usage(argv[0]); |
47 |
|
} |
48 |
|
|
49 |
< |
return airclicks; |
50 |
< |
} |
49 |
> |
if (device.empty()) |
50 |
> |
return Usage(argv[0]); |
51 |
|
|
52 |
< |
int main(int argc, char *argv[]) |
84 |
< |
{ |
85 |
< |
std::set<std::string> airclicks(AirClicks()); |
52 |
> |
std::cerr << device << std::endl; |
53 |
|
|
54 |
|
return 0; |
55 |
|
} |