4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
– |
#include <cassert> |
7 |
|
#include <iostream> |
9 |
– |
#include <sstream> |
8 |
|
#include <string> |
9 |
|
|
10 |
< |
#include <dev/usb/usb.h> |
13 |
< |
#include <dev/usb/usbhid.h> |
10 |
> |
#include <err.h> |
11 |
|
#include <fcntl.h> |
15 |
– |
#include <sys/ioctl.h> |
12 |
|
#include <usbhid.h> |
13 |
|
|
14 |
< |
int main(int argc, char *argv[]) |
15 |
< |
{ |
16 |
< |
for (unsigned number(0); true; ++number) |
17 |
< |
{ |
22 |
< |
std::ostringstream device; |
14 |
> |
#include <common.hpp> |
15 |
> |
#include <foreach.hpp> |
16 |
> |
#include <posix.hpp> |
17 |
> |
#include <regex.hpp> |
18 |
|
|
19 |
< |
device << "/dev/usb" << number; |
20 |
< |
|
21 |
< |
int usb(::open(device.str().c_str(), O_RDONLY)); |
27 |
< |
|
28 |
< |
if (usb == -1) |
29 |
< |
break; |
19 |
> |
int Usage(const std::string &program) |
20 |
> |
{ |
21 |
> |
std::cout << _B("Usage: ") << program << _B(" [-debug] [-device=device]") << std::endl; |
22 |
|
|
23 |
< |
usb_device_info info; |
23 |
> |
return 1; |
24 |
> |
} |
25 |
|
|
26 |
< |
info.udi_addr = 1; |
26 |
> |
int main(int argc, char *argv[]) |
27 |
> |
{ |
28 |
> |
bool debug(false); |
29 |
> |
std::string device; |
30 |
|
|
31 |
< |
assert(::ioctl(usb, USB_DEVICEINFO, &info) != -1); |
31 |
> |
{ |
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 |
< |
for (unsigned port(0); port != info.udi_nports; ++port) |
50 |
< |
std::cerr << unsigned(info.udi_ports[port]) << std::endl; |
49 |
> |
if (device.empty()) |
50 |
> |
return Usage(argv[0]); |
51 |
|
|
52 |
< |
::close(usb); |
41 |
< |
} |
52 |
> |
std::cerr << device << std::endl; |
53 |
|
|
54 |
|
return 0; |
55 |
|
} |