ef0588c4cb96008ded5749d25862ad2270e3e40b
[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 <string>
10
11 class eRCInput;
12 class eRCDriver;
13 class eRCKey;
14
15 #ifndef SWIG
16
17 /**
18  * \brief A remote control.
19  *
20  * Handles one remote control. Gets codes from a \ref eRCDriver. Produces events in \ref eRCInput.
21  */
22 class eRCDevice: public Object
23 {
24 protected:
25         eRCInput *input;
26         eRCDriver *driver;
27         std::string id;
28 public:
29         /**
30          * \brief Constructs a new remote control.
31          *
32          * \param id The identifier of the RC, for use in settings.
33          * \param input The \ref eRCDriver where this remote gets its codes from.
34          */
35         eRCDevice(std::string id, eRCDriver *input);
36         ~eRCDevice();
37         /**
38          * \brief Handles a device specific code.
39          *
40          * Generates events in \ref eRCInput. code is highly device- and driver dependant.
41          * For Example, it might be 16bit codes with one bit make/break or special codes
42          * for repeat.
43          */
44         virtual void handleCode(long code)=0;
45         /**
46          * \brief Get user readable description.
47          * \result The description.
48          */
49         virtual const char *getDescription() const=0;
50         const std::string getIdentifier() const { return id; }
51         /**
52          * \brief Get a description for a specific key.
53          * \param key The key to get the description for.
54          * \result User readable description of given key.
55          */
56 };
57
58 /**
59  * Receives codes from one or more remote controls.
60  */
61 class eRCDriver: public Object
62 {
63 protected:
64         std::list<eRCDevice*> listeners;
65         eRCInput *input;
66         int enabled;
67 public:
68         /**
69          * \brief Constructs a driver.
70          *
71          * \param input The RCInput to bind this driver to.
72          */
73         eRCDriver(eRCInput *input);
74         /**
75          * \brief Get pointer to key-consumer.
76          */
77         eRCInput *getInput() const { return input; }
78         /**
79          * \brief Adds a code lister
80          */
81         void addCodeListener(eRCDevice *dev)
82         {
83                 listeners.push_back(dev);
84         }
85         void removeCodeListener(eRCDevice *dev)
86         {
87                 listeners.remove(dev);
88         }
89         ~eRCDriver();
90         
91         void enable(int en) { enabled=en; }
92 };
93
94 class eRCShortDriver: public eRCDriver
95 {
96 protected:
97         int handle;
98         ePtr<eSocketNotifier> sn;
99         void keyPressed(int);
100 public:
101         eRCShortDriver(const char *filename);
102         ~eRCShortDriver();
103 };
104
105 class eRCInputEventDriver: public eRCDriver
106 {
107 protected:
108         int handle;
109         ePtr<eSocketNotifier> sn;
110         void keyPressed(int);
111 public:
112         std::string getDeviceName();
113         eRCInputEventDriver(const char *filename);
114         ~eRCInputEventDriver();
115         void setExclusive(bool b); // in exclusive mode data is not carried to console device
116 };
117
118 class eRCKey
119 {
120 public:
121         eRCDevice *producer;
122         int code, flags;
123
124         eRCKey(eRCDevice *producer, int code, int flags): 
125                 producer(producer), code(code), flags(flags)
126         {
127         }
128         enum
129         {
130                         /* there are not really flags.. */
131                 flagMake=0,
132                 flagBreak=1,
133                 flagRepeat=2,
134                 flagLong=3,
135                         /* but this is. */
136                 flagAscii=4,
137         };
138
139         bool operator<(const eRCKey &r) const
140         {
141                 if (r.producer == producer)
142                 {
143                         if (r.code == code)
144                         {
145                                 if (r.flags < flags)
146                                         return 1;
147                                 else
148                                         return 0;
149                         } else if (r.code < code)
150                                 return 1;
151                         else
152                                 return 0;
153                 } else if (r.producer < producer)
154                         return 1;
155                 else
156                         return 0;
157         }
158 };
159
160 class eRCConfig
161 {
162 public:
163         eRCConfig();
164         ~eRCConfig();
165         void reload();
166         void save();
167         void set(int delay, int repeat);
168         int rdelay, // keypress delay after first keypress to begin of repeat (in ms)
169                 rrate;          // repeat rate (in ms)
170 };
171
172 #endif
173
174 class eRCInput: public Object
175 {
176         int locked;     
177         int handle;
178         static eRCInput *instance;
179         int keyboardMode;
180 #ifdef SWIG
181         eRCInput();
182         ~eRCInput();
183 public:
184 #else
185 public:
186         struct lstr
187         {
188                 bool operator()(const std::string &a, const std::string &b) const
189                 {
190                         return a<b;
191                 }
192         };
193 protected:
194         std::map<std::string,eRCDevice*,lstr> devices;
195 public:
196         Signal1<void, const eRCKey&> keyEvent;
197         eRCInput();
198         ~eRCInput();
199
200         void close();
201         bool open();
202
203         void setFile(int handle);
204
205         /* This is only relevant for "keyboard"-styled input devices,
206            i.e. not plain remote controls. It's up to the input device
207            driver to decide wheter an input device is a keyboard or
208            not.
209            
210            kmNone will ignore all Ascii Characters sent from the 
211            keyboard/console driver, only give normal keycodes to the
212            application.
213            
214            kmAscii will filter out all keys which produce ascii characters,
215            and send them instead. Note that Modifiers like shift will still
216            be send. Control keys which produce escape codes are send using
217            normal keycodes. 
218            
219            kmAll will ignore all keycodes, and send everything as ascii,
220            including escape codes. Pretty much useless, since you should
221            lock the console and pass this as the console fd for making the
222            tc* stuff working.
223         */
224
225         void keyPressed(const eRCKey &key)
226         {
227                 /*emit*/ keyEvent(key);
228         }
229         
230         void addDevice(const std::string &id, eRCDevice *dev);
231         void removeDevice(const std::string &id);
232         eRCDevice *getDevice(const std::string &id);
233         std::map<std::string,eRCDevice*,lstr> &getDevices();
234
235         eRCConfig config;
236 #endif
237         enum { kmNone, kmAscii, kmAll };
238         void setKeyboardMode(int mode) { keyboardMode = mode; }
239         int  getKeyboardMode() { return keyboardMode; }
240         static eRCInput *getInstance() { return instance; }
241         int lock();
242         void unlock();
243         int islocked() { return locked; }
244 };
245
246 #endif