ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/truck/SteeringWheelRemote/SteeringWheelRemote.cpp
Revision: 14
Committed: 2007-05-14T18:24:34-07:00 (15 years, 8 months ago) by douglas
File size: 3369 byte(s)
Log Message:
Switch back from Menes C++ to stl and hacks.

File Contents

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

Properties

Name Value
svn:keywords Id