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