- removed some OOOOOLD, never required code (last time it was used was in 2001(!))
[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
170 public:
171         struct lstr
172         {
173                 bool operator()(const std::string &a, const std::string &b) const
174                 {
175                         return a<b;
176                 }
177         };
178 protected:
179         std::map<std::string,eRCDevice*,lstr> devices;
180 public:
181         Signal1<void, const eRCKey&> keyEvent;
182         eRCInput();
183         ~eRCInput();
184         
185         int lock();
186         void unlock();
187         int islocked() { return locked; }
188         void close();
189         bool open();
190
191         void setFile(int handle);
192
193         void keyPressed(const eRCKey &key)
194         {
195                 /*emit*/ keyEvent(key);
196         }
197         
198         void addDevice(const std::string &id, eRCDevice *dev);
199         void removeDevice(const std::string &id);
200         eRCDevice *getDevice(const std::string &id);
201         std::map<std::string,eRCDevice*,lstr> &getDevices();
202         
203         static eRCInput *getInstance() { return instance; }
204         
205         eRCConfig config;
206 };
207
208 #endif