activate extensions
[enigma2.git] / 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(int 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         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         eSocketNotifier *sn;
110         void keyPressed(int);
111 public:
112         std::string getDeviceName();
113         eRCInputEventDriver(const char *filename);
114         ~eRCInputEventDriver();
115 };
116
117 class eRCKey
118 {
119 public:
120         eRCDevice *producer;
121         int code, flags;
122
123         eRCKey(eRCDevice *producer, int code, int flags): 
124                 producer(producer), code(code), flags(flags)
125         {
126         }
127         enum
128         {
129                 flagBreak=1,
130                 flagRepeat=2
131         };
132         
133         bool operator<(const eRCKey &r) const
134         {
135                 if (r.producer == producer)
136                 {
137                         if (r.code == code)
138                         {
139                                 if (r.flags < flags)
140                                         return 1;
141                                 else
142                                         return 0;
143                         } else if (r.code < code)
144                                 return 1;
145                         else
146                                 return 0;
147                 } else if (r.producer < producer)
148                         return 1;
149                 else
150                         return 0;
151         }
152 };
153
154 class eRCConfig
155 {
156 public:
157         eRCConfig();
158         ~eRCConfig();
159         void reload();
160         void save();
161         void set(int delay, int repeat);
162         int rdelay, // keypress delay after first keypress to begin of repeat (in ms)
163                 rrate;          // repeat rate (in ms)
164 };
165
166 #endif
167
168 class eRCInput: public Object
169 {
170         int locked;     
171         int handle;
172         static eRCInput *instance;
173         int keyboardMode;
174 #ifdef SWIG
175         eRCInput();
176         ~eRCInput();
177 public:
178 #else
179 public:
180         struct lstr
181         {
182                 bool operator()(const std::string &a, const std::string &b) const
183                 {
184                         return a<b;
185                 }
186         };
187 protected:
188         std::map<std::string,eRCDevice*,lstr> devices;
189 public:
190         Signal1<void, const eRCKey&> keyEvent;
191         eRCInput();
192         ~eRCInput();
193
194         void close();
195         bool open();
196
197         void setFile(int handle);
198
199         /* This is only relevant for "keyboard"-styled input devices,
200            i.e. not plain remote controls. It's up to the input device
201            driver to decide wheter an input device is a keyboard or
202            not.
203            
204            kmNone will ignore all Ascii Characters sent from the 
205            keyboard/console driver, only give normal keycodes to the
206            application.
207            
208            kmAscii will filter out all keys which produce ascii characters,
209            and send them instead. Note that Modifiers like shift will still
210            be send. Control keys which produce escape codes are send using
211            normal keycodes. 
212            
213            kmAll will ignore all keycodes, and send everything as ascii,
214            including escape codes. Pretty much useless, since you should
215            lock the console and pass this as the console fd for making the
216            tc* stuff working.
217         */
218         
219         enum { kmNone, kmAscii, kmAll };
220         void setKeyboardMode(int mode) { keyboardMode = mode; }
221         int  getKeyboardMode() { return keyboardMode; }
222
223         void keyPressed(const eRCKey &key)
224         {
225                 /*emit*/ keyEvent(key);
226         }
227         
228         void addDevice(const std::string &id, eRCDevice *dev);
229         void removeDevice(const std::string &id);
230         eRCDevice *getDevice(const std::string &id);
231         std::map<std::string,eRCDevice*,lstr> &getDevices();
232
233         eRCConfig config;
234 #endif
235         static eRCInput *getInstance() { return instance; }
236         int lock();
237         void unlock();
238         int islocked() { return locked; }
239 };
240
241 #endif