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; |
95 |
|
// Posix::CheckError(::daemon(0, 0)); |
96 |
|
|
97 |
|
ext::ifdstream uhid(Posix::CheckError(::open(device.c_str(), O_RDONLY))); |
98 |
< |
char buffer[8]; |
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 |
< |
while (uhid.read(buffer, sizeof (buffer))) |
122 |
< |
_forall (int, index, 0, 8) |
61 |
< |
std::printf("0x%08x 0x%08x\n", buffer[3], ~buffer[4] - 0x4); |
121 |
> |
std::cout << std::endl; |
122 |
> |
} |
123 |
|
|
124 |
|
return 0; |
125 |
|
} |