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