1 |
// Steering Wheel Remote |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <cxx/standard.hh> |
8 |
|
9 |
#include <api/exename.hpp> |
10 |
#include <api/pcre/regex.hpp> |
11 |
#include <api/files.hpp> |
12 |
#include <app/simple.hpp> |
13 |
#include <ext/helpers.hpp> |
14 |
|
15 |
#include "Audacious.hpp" |
16 |
|
17 |
bool debug(false); |
18 |
|
19 |
enum Button |
20 |
{ |
21 |
None = 000, |
22 |
PlayPause = 001, |
23 |
VolumeUp = 002, |
24 |
VolumeDown = 004, |
25 |
Next = 010, |
26 |
Previous = 020 |
27 |
}; |
28 |
|
29 |
const _V<Button> buttons(PlayPause, VolumeUp, VolumeDown, Next, Previous); |
30 |
|
31 |
inline ios::PrintWriter &operator<<(ios::PrintWriter &output, Button button) |
32 |
{ |
33 |
switch (button) |
34 |
{ |
35 |
case None: |
36 |
return output << _B("None"); |
37 |
case PlayPause: |
38 |
return output << _B("PlayPause"); |
39 |
case Previous: |
40 |
return output << _B("Previous"); |
41 |
case Next: |
42 |
return output << _B("Next"); |
43 |
case VolumeDown: |
44 |
return output << _B("VolumeDown"); |
45 |
case VolumeUp: |
46 |
return output << _B("VolumeUp"); |
47 |
default: |
48 |
return output << _B("Unknown"); |
49 |
} |
50 |
} |
51 |
|
52 |
class Buttons |
53 |
{ |
54 |
byte_t current, previous; |
55 |
_L<Button> events; |
56 |
|
57 |
inline bool Pressed(Button button, bool current_ = true) const |
58 |
{ |
59 |
return ((current_ ? current : previous) & button) == button; |
60 |
} |
61 |
|
62 |
public: |
63 |
inline const _L<Button> &Events() const |
64 |
{ |
65 |
return events; |
66 |
} |
67 |
|
68 |
inline const _L<Button> &Events(byte_t bytes[8]) |
69 |
{ |
70 |
if (debug) |
71 |
{ |
72 |
_assert (bytes[0] == 3); |
73 |
_assert (bytes[1] == 2); |
74 |
_assert (bytes[2] == 0); |
75 |
_assert (bytes[3] == -(bytes[4] - 0373)); |
76 |
_assert (bytes[5] == 0); |
77 |
_assert (bytes[6] == 0); |
78 |
_assert (bytes[7] == 0); |
79 |
} |
80 |
|
81 |
previous = current; |
82 |
current = bytes[3]; |
83 |
|
84 |
events.Clear(); |
85 |
|
86 |
_foreach (const _V<Button>, button, buttons) |
87 |
if (Pressed(*button, false) && !Pressed(*button)) |
88 |
events.InsertLast(*button); |
89 |
|
90 |
return events; |
91 |
} |
92 |
|
93 |
template <Button button> |
94 |
inline bool Pressed() |
95 |
{ |
96 |
return Pressed(button); |
97 |
} |
98 |
}; |
99 |
|
100 |
int Main(const app::Options &options) |
101 |
{ |
102 |
cse::String device; |
103 |
|
104 |
{ |
105 |
api::Pcre::RegEx devicePath(_B("^-device=(/.+)$")); |
106 |
api::Pcre::RegEx deviceNumber(_B("^-device=([0-9]+)$")); |
107 |
api::Pcre::RegEx deviceName(_B("^-device=(.+)$")); |
108 |
|
109 |
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
110 |
{ |
111 |
api::Pcre::RegEx::Match match; |
112 |
|
113 |
if (*arg == _B("-debug")) |
114 |
debug = true; |
115 |
else if (match = devicePath(*arg)) |
116 |
device = match[1]; |
117 |
else if (match = deviceNumber(*arg)) |
118 |
device = _S<ios::String>() << _B("/dev/uhid") << match[1]; |
119 |
else if (match = deviceName(*arg)) |
120 |
device = _S<ios::String>() << _B("/dev/") << match[1]; |
121 |
else |
122 |
goto usage; |
123 |
} |
124 |
} |
125 |
|
126 |
if (device.IsEmpty()) |
127 |
{ |
128 |
usage: api::Cout << _B("Usage: ") << api::GetExecutablePath().GetName() << _B(" [-debug] [-device=device]") << ios::NewLine; |
129 |
|
130 |
return 1; |
131 |
} |
132 |
|
133 |
try |
134 |
{ |
135 |
_S<api::FileReader> uhid(device); |
136 |
byte_t bytes[8]; |
137 |
Buttons buttons; |
138 |
Audacious audacious; |
139 |
|
140 |
_forever |
141 |
{ |
142 |
uhid.ReadFully(bytes, sizeof (bytes)); |
143 |
|
144 |
_foreach (const _L<Button>, button, buttons.Events(bytes)) |
145 |
{ |
146 |
if (debug) |
147 |
api::Cerr << _B("button=") << *button << ios::NewLine; |
148 |
|
149 |
if (audacious.IsRunning()) |
150 |
switch (*button) |
151 |
{ |
152 |
default: |
153 |
break; |
154 |
|
155 |
case PlayPause: |
156 |
audacious.PlayPause(); |
157 |
|
158 |
break; |
159 |
|
160 |
case Previous: |
161 |
audacious.PlaylistPrevious(); |
162 |
|
163 |
break; |
164 |
|
165 |
case Next: |
166 |
audacious.PlaylistNext(); |
167 |
|
168 |
break; |
169 |
|
170 |
case VolumeDown: |
171 |
case VolumeUp: |
172 |
int volume(audacious.GetMainVolume() + (*button == VolumeUp ? 1 : -1)); |
173 |
|
174 |
audacious.SetMainVolume(volume); |
175 |
} |
176 |
} |
177 |
} |
178 |
} |
179 |
catch (const api::Error &error) |
180 |
{ |
181 |
api::Cerr << api::GetExecutablePath().GetName() << _B(": ") << error.GetMessage() << ios::NewLine; |
182 |
|
183 |
return 1; |
184 |
} |
185 |
|
186 |
return 0; |
187 |
} |