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