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 |
struct Buttons |
21 |
{ |
22 |
// XXX: this is probably little endian! |
23 |
enum Mask |
24 |
{ |
25 |
None = 0xfb00, |
26 |
PlayPause = 0x0101, |
27 |
Previous = 0x1010, |
28 |
Next = 0x0808, |
29 |
VolumeDown = 0x0c04, |
30 |
VolumeUp = 0x0202 |
31 |
}; |
32 |
|
33 |
private: |
34 |
uint16_t buttons; |
35 |
|
36 |
public: |
37 |
inline bool Pressed(Mask mask) const |
38 |
{ |
39 |
return ((buttons ^ None) & mask) == mask; |
40 |
} |
41 |
|
42 |
inline operator uint16_t() const |
43 |
{ |
44 |
return buttons; |
45 |
} |
46 |
|
47 |
friend std::istream &operator>>(std::istream &, Buttons &); |
48 |
}; |
49 |
|
50 |
inline std::istream &operator>>(std::istream &input, Buttons &buttons) |
51 |
{ |
52 |
if (!input.ignore(3)) |
53 |
return input; |
54 |
|
55 |
if (!input.read(reinterpret_cast<char *>(&buttons.buttons), sizeof (buttons.buttons))) |
56 |
return input; |
57 |
|
58 |
return input.ignore(3); |
59 |
} |
60 |
|
61 |
int Usage(const std::string &program) |
62 |
{ |
63 |
std::cout << _B("Usage: ") << program << _B(" [-debug] [-device=device]") << std::endl; |
64 |
|
65 |
return 1; |
66 |
} |
67 |
|
68 |
int main(int argc, char *argv[]) |
69 |
{ |
70 |
bool debug(false); |
71 |
std::string device; |
72 |
|
73 |
{ |
74 |
Pcre::RegEx devicePath(_B("^-device=(/.+)$")); |
75 |
Pcre::RegEx deviceNumber(_B("^-device=([0-9]+)$")); |
76 |
Pcre::RegEx deviceName(_B("^-device=(.+)$")); |
77 |
|
78 |
_forall (int, index, 1, argc) |
79 |
if (argv[index] == _B("-debug")) |
80 |
debug = true; |
81 |
else if (Pcre::RegEx::Match match = devicePath(argv[index])) |
82 |
device = match[1]; |
83 |
else if (Pcre::RegEx::Match match = deviceNumber(argv[index])) |
84 |
device = _B("/dev/uhid") + match[1]; |
85 |
else if (Pcre::RegEx::Match match = deviceName(argv[index])) |
86 |
device = _B("/dev/") + match[1]; |
87 |
else |
88 |
return Usage(argv[0]); |
89 |
} |
90 |
|
91 |
if (device.empty()) |
92 |
return Usage(argv[0]); |
93 |
|
94 |
// if (!debug) |
95 |
// Posix::CheckError(::daemon(0, 0)); |
96 |
|
97 |
ext::ifdstream uhid(Posix::CheckError(::open(device.c_str(), O_RDONLY))); |
98 |
Buttons buttons; |
99 |
|
100 |
while (uhid >> buttons) |
101 |
switch (buttons) |
102 |
{ |
103 |
case Buttons::None: |
104 |
break; |
105 |
default: |
106 |
if (buttons.Pressed(Buttons::PlayPause)) |
107 |
std::cout << _B("PlayPause "); |
108 |
|
109 |
if (buttons.Pressed(Buttons::Previous)) |
110 |
std::cout << _B("Previous "); |
111 |
|
112 |
if (buttons.Pressed(Buttons::Next)) |
113 |
std::cout << _B("Next "); |
114 |
|
115 |
if (buttons.Pressed(Buttons::VolumeDown)) |
116 |
std::cout << _B("VolumeDown "); |
117 |
|
118 |
if (buttons.Pressed(Buttons::VolumeUp)) |
119 |
std::cout << _B("VolumeUp"); |
120 |
|
121 |
std::cout << std::endl; |
122 |
} |
123 |
|
124 |
return 0; |
125 |
} |