17 |
|
#include <posix.hpp> |
18 |
|
#include <regex.hpp> |
19 |
|
|
20 |
< |
struct Buttons |
20 |
> |
#include "Audacious.hpp" |
21 |
> |
|
22 |
> |
// XXX: this is probably little endian! |
23 |
> |
enum Button |
24 |
|
{ |
25 |
< |
// XXX: this is probably little endian! |
26 |
< |
enum Mask |
25 |
> |
None = 0xfb00, |
26 |
> |
PlayPause = 0x0101, |
27 |
> |
Previous = 0x1010, |
28 |
> |
Next = 0x0808, |
29 |
> |
VolumeDown = 0x0c04, |
30 |
> |
VolumeUp = 0x0202 |
31 |
> |
} |
32 |
> |
const buttons[] = { PlayPause, Previous, Next, VolumeDown, VolumeUp }; |
33 |
> |
|
34 |
> |
inline std::ostream &operator<<(std::ostream &output, Button button) |
35 |
> |
{ |
36 |
> |
switch (button) |
37 |
|
{ |
38 |
< |
None = 0xfb00, |
39 |
< |
PlayPause = 0x0101, |
40 |
< |
Previous = 0x1010, |
41 |
< |
Next = 0x0808, |
42 |
< |
VolumeDown = 0x0c04, |
43 |
< |
VolumeUp = 0x0202 |
44 |
< |
}; |
38 |
> |
case None: |
39 |
> |
return output << _B("None"); |
40 |
> |
case PlayPause: |
41 |
> |
return output << _B("PlayPause"); |
42 |
> |
case Previous: |
43 |
> |
return output << _B("Previous"); |
44 |
> |
case Next: |
45 |
> |
return output << _B("Next"); |
46 |
> |
case VolumeDown: |
47 |
> |
return output << _B("VolumeDown"); |
48 |
> |
case VolumeUp: |
49 |
> |
return output << _B("VolumeUp"); |
50 |
> |
default: |
51 |
> |
return output << _B("Unknown"); |
52 |
> |
} |
53 |
> |
} |
54 |
|
|
55 |
< |
private: |
56 |
< |
uint16_t buttons; |
55 |
> |
class Buttons |
56 |
> |
{ |
57 |
> |
uint16_t current, previous; |
58 |
> |
mutable std::vector<Button> events; |
59 |
|
|
60 |
< |
public: |
37 |
< |
inline bool Pressed(Mask mask) const |
60 |
> |
inline bool Pressed(Button button, uint16_t buttons) const |
61 |
|
{ |
62 |
< |
return ((buttons ^ None) & mask) == mask; |
62 |
> |
return ((buttons ^ None) & button) == button; |
63 |
|
} |
64 |
|
|
65 |
< |
inline operator uint16_t() const |
65 |
> |
public: |
66 |
> |
Buttons() : current(None) {} |
67 |
> |
|
68 |
> |
const std::vector<Button> &Events() const |
69 |
|
{ |
70 |
< |
return buttons; |
70 |
> |
if (events.empty()) |
71 |
> |
_forall (uint8_t, index, 0, sizeof (buttons) / sizeof (*buttons)) |
72 |
> |
if (!Pressed(buttons[index], current) && Pressed(buttons[index], previous)) |
73 |
> |
events.push_back(buttons[index]); |
74 |
> |
|
75 |
> |
return events; |
76 |
|
} |
77 |
|
|
78 |
|
friend std::istream &operator>>(std::istream &, Buttons &); |
83 |
|
if (!input.ignore(3)) |
84 |
|
return input; |
85 |
|
|
86 |
< |
if (!input.read(reinterpret_cast<char *>(&buttons.buttons), sizeof (buttons.buttons))) |
86 |
> |
buttons.previous = buttons.current; |
87 |
> |
|
88 |
> |
buttons.events.clear(); |
89 |
> |
|
90 |
> |
if (!input.read(reinterpret_cast<char *>(&buttons.current), sizeof (buttons.current))) |
91 |
|
return input; |
92 |
|
|
93 |
|
return input.ignore(3); |
126 |
|
if (device.empty()) |
127 |
|
return Usage(argv[0]); |
128 |
|
|
94 |
– |
// if (!debug) |
95 |
– |
// Posix::CheckError(::daemon(0, 0)); |
96 |
– |
|
129 |
|
ext::ifdstream uhid(Posix::CheckError(::open(device.c_str(), O_RDONLY))); |
130 |
|
Buttons buttons; |
131 |
+ |
Audacious audacious; |
132 |
+ |
|
133 |
+ |
if (!debug) |
134 |
+ |
Posix::CheckError(::daemon(0, 1)); |
135 |
|
|
136 |
|
while (uhid >> buttons) |
137 |
< |
switch (buttons) |
137 |
> |
_foreach (const std::vector<Button>, button, buttons.Events()) |
138 |
|
{ |
139 |
< |
case Buttons::None: |
140 |
< |
break; |
141 |
< |
default: |
142 |
< |
if (buttons.Pressed(Buttons::PlayPause)) |
143 |
< |
std::cout << _B("PlayPause "); |
139 |
> |
if (debug) |
140 |
> |
std::cout << _B("button=") << *button << std::endl; |
141 |
> |
|
142 |
> |
if (audacious.IsRunning()) |
143 |
> |
switch (*button) |
144 |
> |
{ |
145 |
> |
default: |
146 |
> |
break; |
147 |
> |
|
148 |
> |
case PlayPause: |
149 |
> |
audacious.PlayPause(); |
150 |
> |
|
151 |
> |
break; |
152 |
> |
|
153 |
> |
case Previous: |
154 |
> |
audacious.PlaylistPrevious(); |
155 |
|
|
156 |
< |
if (buttons.Pressed(Buttons::Previous)) |
110 |
< |
std::cout << _B("Previous "); |
156 |
> |
break; |
157 |
|
|
158 |
< |
if (buttons.Pressed(Buttons::Next)) |
159 |
< |
std::cout << _B("Next "); |
158 |
> |
case Next: |
159 |
> |
audacious.PlaylistNext(); |
160 |
|
|
161 |
< |
if (buttons.Pressed(Buttons::VolumeDown)) |
116 |
< |
std::cout << _B("VolumeDown "); |
161 |
> |
break; |
162 |
|
|
163 |
< |
if (buttons.Pressed(Buttons::VolumeUp)) |
164 |
< |
std::cout << _B("VolumeUp"); |
163 |
> |
case VolumeDown: |
164 |
> |
case VolumeUp: |
165 |
> |
int volume(audacious.GetMainVolume() + (*button == VolumeUp ? 1 : -1)); |
166 |
|
|
167 |
< |
std::cout << std::endl; |
167 |
> |
audacious.SetMainVolume(volume); |
168 |
> |
} |
169 |
|
} |
170 |
|
|
171 |
|
return 0; |