557be5ea845a4df32b7ceca1baae8b7e4ddbc7fd
[vuplus_dvbapp] / lib / driver / rc.h
1 #ifndef __rc_h
2 #define __rc_h
3
4 #include <list>
5 #include <map>
6
7 #include <lib/base/ebase.h>
8 #include <libsig_comp.h>
9 #include <lib/base/estring.h>
10
11 class eRCInput;
12 class eRCDriver;
13 class eRCKey;
14
15 /**
16  * \brief A remote control.
17  *
18  * Handles one remote control. Gets codes from a \ref eRCDriver. Produces events in \ref eRCInput.
19  */
20 class eRCDevice: public Object
21 {
22 protected:
23         eRCInput *input;
24         eRCDriver *driver;
25         eString id;
26 public:
27         /**
28          * \brief Constructs a new remote control.
29          *
30          * \param id The identifier of the RC, for use in settings.
31          * \param input The \ref eRCDriver where this remote gets its codes from.
32          */
33         eRCDevice(eString id, eRCDriver *input);
34         ~eRCDevice();
35         /**
36          * \brief Handles a device specific code.
37          *
38          * Generates events in \ref eRCInput. code is highly device- and driver dependant.
39          * For Example, it might be 16bit codes with one bit make/break or special codes
40          * for repeat.
41          */
42         virtual void handleCode(int code)=0;
43         /**
44          * \brief Get user readable description.
45          * \result The description.
46          */
47         virtual const char *getDescription() const=0;
48         const eString getIdentifier() const { return id; }
49         /**
50          * \brief Get a description for a specific key.
51          * \param key The key to get the description for.
52          * \result User readable description of given key.
53          */
54         virtual const char *getKeyDescription(const eRCKey &key) const=0;
55         /**
56          * \brief Get a dbox2-compatible keycode.
57          *
58          * THIS IS DEPRECATED! DON'T USE IT UNLESS YOU NEED IT!
59          * \param key The key to get the compatible code for.
60          * \result The dbox2-compatible code. (new RC as defined in enum).
61          */
62         virtual int getKeyCompatibleCode(const eRCKey &key) const;
63 };
64
65 /**
66  * Receives codes from one or more remote controls.
67  */
68 class eRCDriver: public Object
69 {
70 protected:
71         std::list<eRCDevice*> listeners;
72         eRCInput *input;
73         int enabled;
74 public:
75         /**
76          * \brief Constructs a driver.
77          *
78          * \param input The RCInput to bind this driver to.
79          */
80         eRCDriver(eRCInput *input);
81         /**
82          * \brief Get pointer to key-consumer.
83          */
84         eRCInput *getInput() const { return input; }
85         /**
86          * \brief Adds a code lister
87          */
88         void addCodeListener(eRCDevice *dev)
89         {
90                 listeners.push_back(dev);
91         }
92         void removeCodeListener(eRCDevice *dev)
93         {
94                 listeners.remove(dev);
95         }
96         ~eRCDriver();
97         
98         void enable(int en) { enabled=en; }
99 };
100
101 class eRCShortDriver: public eRCDriver
102 {
103 protected:
104         int handle;
105         eSocketNotifier *sn;
106         void keyPressed(int);
107 public:
108         eRCShortDriver(const char *filename);
109         ~eRCShortDriver();
110 };
111
112 class eRCInputEventDriver: public eRCDriver
113 {
114 protected:
115         int handle;
116         eSocketNotifier *sn;
117         void keyPressed(int);
118 public:
119         eString getDeviceName();
120         eRCInputEventDriver(const char *filename);
121         ~eRCInputEventDriver();
122 };
123
124 class eRCKey
125 {
126 public:
127         eRCDevice *producer;
128         int code, flags;
129
130         eRCKey(eRCDevice *producer, int code, int flags): 
131                 producer(producer), code(code), flags(flags)
132         {
133         }
134         enum
135         {
136                 flagBreak=1,
137                 flagRepeat=2
138         };
139         
140         bool operator<(const eRCKey &r) const
141         {
142                 if (r.producer == producer)
143                 {
144                         if (r.code == code)
145                         {
146                                 if (r.flags < flags)
147                                         return 1;
148                                 else
149                                         return 0;
150                         } else if (r.code < code)
151                                 return 1;
152                         else
153                                 return 0;
154                 } else if (r.producer < producer)
155                         return 1;
156                 else
157                         return 0;
158         }
159 };
160
161 class eRCConfig
162 {
163 public:
164         eRCConfig();
165         ~eRCConfig();
166         void reload();
167         void save();
168         void set(int delay, int repeat);
169         int rdelay, // keypress delay after first keypress to begin of repeat (in ms)
170                 rrate;          // repeat rate (in ms)
171 };
172
173 class eRCInput: public Object
174 {
175         int locked;     
176         int handle;
177         static eRCInput *instance;
178
179 public:
180         struct lstr
181         {
182                 bool operator()(const eString &a, const eString &b) const
183                 {
184                         return a<b;
185                 }
186         };
187 protected:
188         std::map<eString,eRCDevice*,lstr> devices;
189 public:
190         Signal1<void, const eRCKey&> keyEvent;
191         enum
192         {
193                 RC_0=0, RC_1=0x1, RC_2=0x2, RC_3=0x3, RC_4=0x4, RC_5=0x5, RC_6=0x6, RC_7=0x7,
194                 RC_8=0x8, RC_9=0x9,
195                 RC_RIGHT=10, RC_LEFT=11, RC_UP=12, RC_DOWN=13, RC_OK=14, RC_MUTE=15,
196                 RC_STANDBY=16, RC_GREEN=17, RC_YELLOW=18, RC_RED=19, RC_BLUE=20, RC_PLUS=21, RC_MINUS=22,
197                 RC_HELP=23, RC_DBOX=24,
198                 RC_UP_LEFT=27, RC_UP_RIGHT=28, RC_DOWN_LEFT=29, RC_DOWN_RIGHT=30, RC_HOME=31
199         };
200         eRCInput();
201         ~eRCInput();
202         
203         int lock();
204         void unlock();
205         int islocked() { return locked; }
206         void close();
207         bool open();
208
209         void setFile(int handle);
210
211         void keyPressed(const eRCKey &key)
212         {
213                 /*emit*/ keyEvent(key);
214         }
215         
216         void addDevice(const eString &id, eRCDevice *dev);
217         void removeDevice(const eString &id);
218         eRCDevice *getDevice(const eString &id);
219         std::map<eString,eRCDevice*,lstr> &getDevices();
220         
221         static eRCInput *getInstance() { return instance; }
222         
223         eRCConfig config;
224 };
225
226 #endif