ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/truck/SteeringWheelRemote/SteeringWheelRemote.cpp
Revision: 5
Committed: 2007-05-06T09:25:28-07:00 (15 years, 7 months ago) by douglas
File size: 2477 byte(s)
Log Message:
Buttons of workyness!

File Contents

# User Rev Content
1 douglas 1 // Steering Wheel Remote
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include <iostream>
8     #include <string>
9    
10 douglas 2 #include <err.h>
11 douglas 1 #include <fcntl.h>
12 douglas 4 #include <sys/ioctl.h>
13 douglas 1
14 douglas 3 #include <common.hpp>
15 douglas 4 #include <fdstream.hpp>
16 douglas 2 #include <foreach.hpp>
17     #include <posix.hpp>
18 douglas 3 #include <regex.hpp>
19 douglas 2
20 douglas 5 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 douglas 3 int Usage(const std::string &program)
62 douglas 1 {
63 douglas 3 std::cout << _B("Usage: ") << program << _B(" [-debug] [-device=device]") << std::endl;
64    
65     return 1;
66 douglas 2 }
67    
68 douglas 3 int main(int argc, char *argv[])
69 douglas 2 {
70 douglas 3 bool debug(false);
71     std::string device;
72 douglas 2
73 douglas 1 {
74 douglas 3 Pcre::RegEx devicePath(_B("^-device=(/.+)$"));
75     Pcre::RegEx deviceNumber(_B("^-device=([0-9]+)$"));
76     Pcre::RegEx deviceName(_B("^-device=(.+)$"));
77 douglas 1
78 douglas 3 _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 douglas 1 }
90    
91 douglas 3 if (device.empty())
92     return Usage(argv[0]);
93 douglas 2
94 douglas 4 // if (!debug)
95     // Posix::CheckError(::daemon(0, 0));
96 douglas 2
97 douglas 4 ext::ifdstream uhid(Posix::CheckError(::open(device.c_str(), O_RDONLY)));
98 douglas 5 Buttons buttons;
99 douglas 4
100 douglas 5 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 douglas 4
109 douglas 5 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 douglas 1 return 0;
125     }

Properties

Name Value
svn:keywords Id