1 |
// Steering Wheel Remote |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <iostream> |
8 |
#include <string> |
9 |
|
10 |
#include <err.h> |
11 |
#include <fcntl.h> |
12 |
#include <sys/ioctl.h> |
13 |
|
14 |
#include <common.hpp> |
15 |
#include <fdstream.hpp> |
16 |
#include <foreach.hpp> |
17 |
#include <posix.hpp> |
18 |
#include <regex.hpp> |
19 |
|
20 |
int Usage(const std::string &program) |
21 |
{ |
22 |
std::cout << _B("Usage: ") << program << _B(" [-debug] [-device=device]") << std::endl; |
23 |
|
24 |
return 1; |
25 |
} |
26 |
|
27 |
int main(int argc, char *argv[]) |
28 |
{ |
29 |
bool debug(false); |
30 |
std::string device; |
31 |
|
32 |
{ |
33 |
Pcre::RegEx devicePath(_B("^-device=(/.+)$")); |
34 |
Pcre::RegEx deviceNumber(_B("^-device=([0-9]+)$")); |
35 |
Pcre::RegEx deviceName(_B("^-device=(.+)$")); |
36 |
|
37 |
_forall (int, index, 1, argc) |
38 |
if (argv[index] == _B("-debug")) |
39 |
debug = true; |
40 |
else if (Pcre::RegEx::Match match = devicePath(argv[index])) |
41 |
device = match[1]; |
42 |
else if (Pcre::RegEx::Match match = deviceNumber(argv[index])) |
43 |
device = _B("/dev/uhid") + match[1]; |
44 |
else if (Pcre::RegEx::Match match = deviceName(argv[index])) |
45 |
device = _B("/dev/") + match[1]; |
46 |
else |
47 |
return Usage(argv[0]); |
48 |
} |
49 |
|
50 |
if (device.empty()) |
51 |
return Usage(argv[0]); |
52 |
|
53 |
// if (!debug) |
54 |
// Posix::CheckError(::daemon(0, 0)); |
55 |
|
56 |
ext::ifdstream uhid(Posix::CheckError(::open(device.c_str(), O_RDONLY))); |
57 |
char buffer[8]; |
58 |
|
59 |
while (uhid.read(buffer, sizeof (buffer))) |
60 |
_forall (int, index, 0, 8) |
61 |
std::printf("0x%08x 0x%08x\n", buffer[3], ~buffer[4] - 0x4); |
62 |
|
63 |
return 0; |
64 |
} |