Merge commit 'origin/master' into tmbinc/FixTimingBugs
[enigma2.git] / lib / driver / rc.cpp
1 #include <lib/driver/rc.h>
2
3 #include <asm/types.h>
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <linux/input.h>
8
9 #include <lib/base/init.h>
10 #include <lib/base/init_num.h>
11 #include <lib/base/eerror.h>
12
13 /*
14  *  note on the enigma input layer:
15  *  the enigma input layer (rc*) supports n different devices which
16  *  all have completely different interfaces, mapped down to 32bit + 
17  *  make/break/release codes mapped down (via xml files) to "actions".
18  *  this was necessary to support multiple remote controls with proprietary
19  *  interfaces. now everybody is using input devices, and thus adding
20  *  another input layer seems to be a bit overkill. BUT:
21  *  image a remote control with two hundred buttons. each and every function
22  *  in enigma can be bound to a button. no need to use them twice. 
23  *  for example, you would have KEY_MENU assigned to a menu for setup etc.,
24  *  but no audio and video settings, since you have special keys for that,
25  *  and you don't want to display a big menu with entries that are available
26  *  with another single key.
27  *  then image a remote control with ten buttons. do you really want to waste
28  *  KEY_MENU for a simple menu? you need the audio/video settings there too.
29  *  take this just as a (bad) example. another (better) example might be front-
30  *  button-keys. usually you have KEY_UP, KEY_DOWN, KEY_POWER. you don't want
31  *  them to behave like the remote-control-KEY_UP, KEY_DOWN and KEY_POWER,
32  *  don't you? 
33  *  so here we can map same keys of different input devices to different 
34  *  actions. have fun.
35  */
36
37 eRCDevice::eRCDevice(std::string id, eRCDriver *driver): driver(driver), id(id)
38 {
39         input=driver->getInput();
40         driver->addCodeListener(this);
41         eRCInput::getInstance()->addDevice(id, this);
42 }
43
44 eRCDevice::~eRCDevice()
45 {
46         driver->removeCodeListener(this);
47         eRCInput::getInstance()->removeDevice(id.c_str());
48 }
49
50 eRCDriver::eRCDriver(eRCInput *input): input(input), enabled(1)
51 {
52 }
53
54 eRCDriver::~eRCDriver()
55 {
56         for (std::list<eRCDevice*>::iterator i=listeners.begin(); i!=listeners.end(); ++i)
57                 delete *i;
58 }
59
60 void eRCShortDriver::keyPressed(int)
61 {
62         __u16 rccode;
63         while (1)
64         {
65                 if (read(handle, &rccode, 2)!=2)
66                         break;
67                 if (enabled && !input->islocked())
68                         for (std::list<eRCDevice*>::iterator i(listeners.begin()); i!=listeners.end(); ++i)
69                                 (*i)->handleCode(rccode);
70         }
71 }
72
73 eRCShortDriver::eRCShortDriver(const char *filename): eRCDriver(eRCInput::getInstance())
74 {
75         handle=open(filename, O_RDONLY|O_NONBLOCK);
76         if (handle<0)
77         {
78                 eDebug("failed to open %s", filename);
79                 sn=0;
80         } else
81         {
82                 sn=eSocketNotifier::create(eApp, handle, eSocketNotifier::Read);
83                 CONNECT(sn->activated, eRCShortDriver::keyPressed);
84                 eRCInput::getInstance()->setFile(handle);
85         }
86 }
87
88 eRCShortDriver::~eRCShortDriver()
89 {
90         if (handle>=0)
91                 close(handle);
92 }
93
94 void eRCInputEventDriver::keyPressed(int)
95 {
96         struct input_event ev;
97         while (1)
98         {
99                 if (read(handle, &ev, sizeof(struct input_event))!=sizeof(struct input_event))
100                         break;
101                 if (enabled && !input->islocked())
102                         for (std::list<eRCDevice*>::iterator i(listeners.begin()); i!=listeners.end(); ++i)
103                                 (*i)->handleCode((long)&ev);
104         }
105 }
106
107 eRCInputEventDriver::eRCInputEventDriver(const char *filename): eRCDriver(eRCInput::getInstance())
108 {
109         handle=open(filename, O_RDONLY|O_NONBLOCK);
110         if (handle<0)
111         {
112                 eDebug("failed to open %s", filename);
113                 sn=0;
114         } else
115         {
116                 sn=eSocketNotifier::create(eApp, handle, eSocketNotifier::Read);
117                 CONNECT(sn->activated, eRCInputEventDriver::keyPressed);
118                 eRCInput::getInstance()->setFile(handle);
119         }
120 }
121
122 std::string eRCInputEventDriver::getDeviceName()
123 {
124         char name[128]="";
125         if (handle >= 0)
126                 ::ioctl(handle, EVIOCGNAME(128), name);
127         return name;
128 }
129
130 eRCInputEventDriver::~eRCInputEventDriver()
131 {
132         if (handle>=0)
133                 close(handle);
134 }
135
136 eRCConfig::eRCConfig()
137 {
138         reload();
139 }
140
141 eRCConfig::~eRCConfig()
142 {
143         save();
144 }
145
146 void eRCConfig::set( int delay, int repeat )
147 {
148         rdelay = delay;
149         rrate = repeat;
150 }
151
152 void eRCConfig::reload()
153 {
154         rdelay=500;
155         rrate=100;
156 }
157
158 void eRCConfig::save()
159 {
160 }
161
162 eRCInput *eRCInput::instance;
163
164 eRCInput::eRCInput()
165 {
166         ASSERT( !instance);
167         instance=this;
168         handle = -1;
169         locked = 0;
170         keyboardMode = kmNone;
171 }
172
173 eRCInput::~eRCInput()
174 {
175 }
176
177 void eRCInput::close()
178 {
179 }
180
181 bool eRCInput::open()
182 {
183         return false;
184 }
185
186 int eRCInput::lock()
187 {
188         locked=1;
189         return handle;
190 }
191
192 void eRCInput::unlock()
193 {
194         if (locked)
195                 locked=0;
196 }
197
198 void eRCInput::setFile(int newh)
199 {
200         handle=newh;
201 }
202
203 void eRCInput::addDevice(const std::string &id, eRCDevice *dev)
204 {
205         devices.insert(std::pair<std::string,eRCDevice*>(id, dev));
206 }
207
208 void eRCInput::removeDevice(const std::string &id)
209 {
210         devices.erase(id);
211 }
212
213 eRCDevice *eRCInput::getDevice(const std::string &id)
214 {
215         std::map<std::string,eRCDevice*>::iterator i=devices.find(id);
216         if (i == devices.end())
217         {
218                 eDebug("failed, possible choices are:");
219                 for (std::map<std::string,eRCDevice*>::iterator i=devices.begin(); i != devices.end(); ++i)     
220                         eDebug("%s", i->first.c_str());
221                 return 0;
222         }
223         return i->second;
224 }
225
226 std::map<std::string,eRCDevice*,eRCInput::lstr> &eRCInput::getDevices()
227 {
228         return devices;
229 }
230
231 eAutoInitP0<eRCInput> init_rcinput(eAutoInitNumbers::rc, "RC Input layer");