trunk 20080912
[gitenigma.git] / include / lib / driver / rc.h
blob449b6e4c2cc59602359773e60b7ca5abf41d416e
1 #ifndef __rc_h
2 #define __rc_h
4 #include <config.h>
5 #include <unistd.h>
6 #include <list>
7 #include <map>
9 #include <lib/base/ebase.h>
10 #include <lib/base/estring.h>
11 #include <libsig_comp.h>
12 #include <linux/input.h>
13 #include <lib/driver/input_fake.h>
15 class eRCInput;
16 class eRCDriver;
17 class eRCKey;
19 // the offset of plain ascii codes coming out of the console (only when in keyboard mode)
20 #define KEY_ASCII 0x1000
22 /**
23 * \brief A remote control.
25 * Handles one remote control. Gets codes from a \ref eRCDriver. Produces events in \ref eRCInput.
27 class eRCDevice: public Object
29 protected:
30 eRCInput *input;
31 eRCDriver *driver;
32 eString id;
33 public:
34 /**
35 * \brief Constructs a new remote control.
37 * \param id The identifier of the RC, for use in settings.
38 * \param input The \ref eRCDriver where this remote gets its codes from.
40 eRCDevice(const eString &id, eRCDriver *input);
41 ~eRCDevice();
42 /**
43 * \brief Handles a device specific code.
45 * Generates events in \ref eRCInput. code is highly device- and driver dependant.
46 * For Example, it might be 16bit codes with one bit make/break or special codes
47 * for repeat.
49 virtual void handleCode(int code)=0;
50 /**
51 * \brief Get user readable description.
52 * \result The description.
54 virtual const char *getDescription() const=0;
55 /**
56 * \brief Get a description for a specific key.
57 * \param key The key to get the description for.
58 * \result User readable description of given key.
60 const eString getIdentifier() const { return id; }
61 /**
62 * \brief Get the identifier for this device.
63 * \result User readable identifier of device.
65 virtual const char *getKeyDescription(const eRCKey &key) const=0;
66 /**
67 * \brief Get an input device keycode.
69 * \param key The key to get the input code for.
70 * \result The Linux input device keycode
72 virtual int getKeyCompatibleCode(const eRCKey &key) const;
73 const eRCDriver *getDriver() { return driver; }
76 /**
77 * Receives codes from one or more remote controls.
79 class eRCDriver: public Object
81 protected:
82 std::list<eRCDevice*> listeners;
83 eRCInput *input;
84 int enabled;
85 public:
86 /**
87 * \brief Constructs a driver.
89 * \param input The RCInput to bind this driver to.
91 eRCDriver(eRCInput *input);
92 /**
93 * \brief Get pointer to key-consumer.
95 eRCInput *getInput() const { return input; }
96 /**
97 * \brief Adds a code lister
99 void addCodeListener(eRCDevice *dev)
101 listeners.push_back(dev);
103 void removeCodeListener(eRCDevice *dev)
105 listeners.remove(dev);
107 ~eRCDriver();
109 void enable(int en) { enabled=en; }
111 virtual void flushBuffer() const {};
112 virtual void lock() const {};
113 virtual void unlock() const {};
116 #if HAVE_DVB_API_VERSION < 3
117 class eRCShortDriver: public eRCDriver
119 protected:
120 int handle;
121 eSocketNotifier *sn;
122 void keyPressed(int);
123 public:
124 eRCShortDriver(const char *filename);
125 ~eRCShortDriver();
126 void flushBuffer() const
128 __u16 buf;
129 if (handle != -1)
130 while ( ::read(handle, &buf, 2) == 2 );
132 void lock() const
134 if ( sn )
135 sn->stop();
137 void unlock() const
139 if ( sn )
140 sn->start();
143 #endif
145 class eRCInputEventDriver: public eRCDriver
147 protected:
148 int handle;
149 eSocketNotifier *sn;
150 void keyPressed(int);
151 public:
152 eString getDeviceName();
153 eRCInputEventDriver(const char *filename);
154 ~eRCInputEventDriver();
155 void flushBuffer() const
157 struct input_event ev;
158 if (handle != -1)
159 while ( ::read(handle, &ev, sizeof(struct input_event)) == sizeof(struct input_event) );
161 void lock() const
163 if ( sn )
164 sn->stop();
166 void unlock() const
168 if ( sn )
169 sn->start();
173 class eRCKey
175 public:
176 eRCDevice *producer;
177 int code, flags;
178 eString picture;
180 eRCKey(eRCDevice *producer, int code, int flags, eString picture=""):
181 producer(producer), code(code), flags(flags), picture(picture)
184 enum
186 flagBreak=1,
187 flagRepeat=2
190 bool operator<(const eRCKey &r) const
192 if (producer > r.producer)
193 return 0;
194 if (producer < r.producer)
195 return 1;
197 if (code > r.code)
198 return 0;
199 if (code < r.code)
200 return 1;
202 if (flags > r.flags)
203 return 0;
204 if (flags < r.flags)
205 return 1;
207 return 0;
211 class eRCConfig
213 public:
214 eRCConfig();
215 ~eRCConfig();
216 void reload();
217 void save();
218 void set(int delay, int repeat);
219 int rdelay, // keypress delay after first keypress to begin of repeat (in ms)
220 rrate; // repeat rate (in ms)
223 class eRCInput: public Object
225 int locked;
226 int handle;
227 static eRCInput *instance;
228 int keyboardMode;
229 public:
230 struct lstr
232 bool operator()(const eString &a, const eString &b) const
234 return a<b;
238 protected:
239 std::map<eString,eRCDevice*,lstr> devices;
240 public:
241 Signal1<void, const eRCKey&> keyEvent;
242 eRCInput();
243 ~eRCInput();
245 int lock();
246 void unlock();
247 int islocked() { return locked; }
248 void close();
249 bool open();
251 void setFile(int handle);
254 /* This is only relevant for "keyboard"-styled input devices,
255 i.e. not plain remote controls. It's up to the input device
256 driver to decide wheter an input device is a keyboard or
257 not.
259 kmNone will ignore all Ascii Characters sent from the
260 keyboard/console driver, only give normal keycodes to the
261 application.
263 kmAscii will filter out all keys which produce ascii characters,
264 and send them instead. Note that Modifiers like shift will still
265 be send. Control keys which produce escape codes are send using
266 normal keycodes.
268 kmAll will ignore all keycodes, and send everything as ascii,
269 including escape codes. Pretty much useless, since you should
270 lock the console and pass this as the console fd for making the
271 tc* stuff working.
274 enum { kmNone, kmAscii, kmAll };
275 void setKeyboardMode(int mode);
276 int getKeyboardMode() { return keyboardMode; }
277 void loadKeyboardMapping();
279 void keyPressed(const eRCKey &key)
281 /*emit*/ keyEvent(key);
284 void addDevice(const eString&, eRCDevice *dev);
285 void removeDevice(const eString &id);
286 eRCDevice *getDevice(const eString &id);
287 std::map<eString,eRCDevice*,lstr> &getDevices();
289 static eRCInput *getInstance() { return instance; }
291 eRCConfig config;
294 #endif