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> |
12 |
|
#include <sys/ioctl.h> |
13 |
< |
#include <usbhid.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 |
< |
for (unsigned number(0); true; ++number) |
70 |
> |
bool debug(false); |
71 |
> |
std::string device; |
72 |
> |
|
73 |
|
{ |
74 |
< |
std::ostringstream device; |
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 |
< |
device << "/dev/usb" << number; |
94 |
> |
// if (!debug) |
95 |
> |
// Posix::CheckError(::daemon(0, 0)); |
96 |
|
|
97 |
< |
int usb(::open(device.str().c_str(), O_RDONLY)); |
97 |
> |
ext::ifdstream uhid(Posix::CheckError(::open(device.c_str(), O_RDONLY))); |
98 |
> |
Buttons buttons; |
99 |
|
|
100 |
< |
if (usb == -1) |
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 |
< |
usb_device_info info; |
109 |
> |
if (buttons.Pressed(Buttons::Previous)) |
110 |
> |
std::cout << _B("Previous "); |
111 |
|
|
112 |
< |
info.udi_addr = 1; |
112 |
> |
if (buttons.Pressed(Buttons::Next)) |
113 |
> |
std::cout << _B("Next "); |
114 |
|
|
115 |
< |
assert(::ioctl(usb, USB_DEVICEINFO, &info) != -1); |
115 |
> |
if (buttons.Pressed(Buttons::VolumeDown)) |
116 |
> |
std::cout << _B("VolumeDown "); |
117 |
|
|
118 |
< |
for (unsigned port(0); port != info.udi_nports; ++port) |
119 |
< |
std::cerr << unsigned(info.udi_ports[port]) << std::endl; |
118 |
> |
if (buttons.Pressed(Buttons::VolumeUp)) |
119 |
> |
std::cout << _B("VolumeUp"); |
120 |
|
|
121 |
< |
::close(usb); |
122 |
< |
} |
121 |
> |
std::cout << std::endl; |
122 |
> |
} |
123 |
|
|
124 |
|
return 0; |
125 |
|
} |