4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
– |
#include <cassert> |
7 |
|
#include <iostream> |
9 |
– |
#include <set> |
10 |
– |
#include <sstream> |
8 |
|
#include <string> |
9 |
|
|
13 |
– |
#include <dev/usb/usb.h> |
14 |
– |
#include <dev/usb/usbhid.h> |
10 |
|
#include <err.h> |
11 |
|
#include <fcntl.h> |
12 |
|
#include <sys/ioctl.h> |
18 |
– |
#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 |
< |
inline std::ostream &operator <<(std::ostream &output, uint8_t number) |
20 |
> |
struct Buttons |
21 |
|
{ |
22 |
< |
return output << unsigned(number); |
23 |
< |
} |
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 |
< |
std::set<std::string> AirClicks() |
34 |
< |
{ |
30 |
< |
std::set<std::string> airclicks; |
33 |
> |
private: |
34 |
> |
uint16_t buttons; |
35 |
|
|
36 |
< |
_forall (unsigned, number, 0, 10) |
36 |
> |
public: |
37 |
> |
inline bool Pressed(Mask mask) const |
38 |
|
{ |
39 |
< |
std::ostringstream device_; |
40 |
< |
|
36 |
< |
device_ << "/dev/usb" << number; |
39 |
> |
return ((buttons ^ None) & mask) == mask; |
40 |
> |
} |
41 |
|
|
42 |
< |
const std::string &device(device_.str()); |
42 |
> |
inline operator uint16_t() const |
43 |
> |
{ |
44 |
> |
return buttons; |
45 |
> |
} |
46 |
|
|
47 |
< |
try |
48 |
< |
{ |
42 |
< |
int usb(Posix::CheckError(::open(device.c_str(), O_RDONLY))); |
47 |
> |
friend std::istream &operator>>(std::istream &, Buttons &); |
48 |
> |
}; |
49 |
|
|
50 |
< |
_forall (uint8_t, address, 1, USB_MAX_DEVICES) |
51 |
< |
{ |
52 |
< |
usb_device_info info; |
53 |
< |
|
48 |
< |
info.udi_addr = address; |
49 |
< |
|
50 |
< |
try |
51 |
< |
{ |
52 |
< |
Posix::CheckError(::ioctl(usb, USB_DEVICEINFO, &info)); |
53 |
< |
} |
54 |
< |
catch (const Posix::Error &error) |
55 |
< |
{ |
56 |
< |
if (error.GetCode() != ENXIO) |
57 |
< |
::warn("%s: address %u", device.c_str(), address); |
50 |
> |
inline std::istream &operator>>(std::istream &input, Buttons &buttons) |
51 |
> |
{ |
52 |
> |
if (!input.ignore(3)) |
53 |
> |
return input; |
54 |
|
|
55 |
< |
continue; |
56 |
< |
} |
55 |
> |
if (!input.read(reinterpret_cast<char *>(&buttons.buttons), sizeof (buttons.buttons))) |
56 |
> |
return input; |
57 |
|
|
58 |
< |
std::cout << info.udi_product << std::endl; |
59 |
< |
} |
58 |
> |
return input.ignore(3); |
59 |
> |
} |
60 |
|
|
61 |
< |
Posix::CheckError(::close(usb)); |
62 |
< |
} |
63 |
< |
catch (const Posix::Error &error) |
68 |
< |
{ |
69 |
< |
switch (error.GetCode()) |
70 |
< |
{ |
71 |
< |
case ENOENT: |
72 |
< |
case ENXIO: |
73 |
< |
continue; |
74 |
< |
default: |
75 |
< |
::warn("%s", device.c_str()); |
76 |
< |
} |
77 |
< |
} |
78 |
< |
} |
61 |
> |
int Usage(const std::string &program) |
62 |
> |
{ |
63 |
> |
std::cout << _B("Usage: ") << program << _B(" [-debug] [-device=device]") << std::endl; |
64 |
|
|
65 |
< |
return airclicks; |
65 |
> |
return 1; |
66 |
|
} |
67 |
|
|
68 |
|
int main(int argc, char *argv[]) |
69 |
|
{ |
70 |
< |
std::set<std::string> airclicks(AirClicks()); |
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 |
|
} |