Merge branch 'bug_274_disable_fast_winding_for_non_ts'
[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         }
85 }
86
87 eRCShortDriver::~eRCShortDriver()
88 {
89         if (handle>=0)
90                 close(handle);
91 }
92
93 void eRCInputEventDriver::keyPressed(int)
94 {
95         struct input_event ev;
96         while (1)
97         {
98                 if (read(handle, &ev, sizeof(struct input_event))!=sizeof(struct input_event))
99                         break;
100                 if (enabled && !input->islocked())
101                         for (std::list<eRCDevice*>::iterator i(listeners.begin()); i!=listeners.end(); ++i)
102                                 (*i)->handleCode((long)&ev);
103         }
104 }
105
106 eRCInputEventDriver::eRCInputEventDriver(const char *filename): eRCDriver(eRCInput::getInstance())
107 {
108         handle=open(filename, O_RDONLY|O_NONBLOCK);
109         if (handle<0)
110         {
111                 eDebug("failed to open %s", filename);
112                 sn=0;
113         } else
114         {
115                 sn=eSocketNotifier::create(eApp, handle, eSocketNotifier::Read);
116                 CONNECT(sn->activated, eRCInputEventDriver::keyPressed);
117         }
118 }
119
120 std::string eRCInputEventDriver::getDeviceName()
121 {
122         char name[128]="";
123         if (handle >= 0)
124                 ::ioctl(handle, EVIOCGNAME(128), name);
125         return name;
126 }
127
128 void eRCInputEventDriver::setExclusive(bool b)
129 {
130         if (handle >= 0)
131         {
132                 int grab = b;
133                 if (::ioctl(handle, EVIOCGRAB, grab) < 0)
134                         perror("EVIOCGRAB");
135         }
136 }
137
138 eRCInputEventDriver::~eRCInputEventDriver()
139 {
140         if (handle>=0)
141                 close(handle);
142 }
143
144 eRCConfig::eRCConfig()
145 {
146         reload();
147 }
148
149 eRCConfig::~eRCConfig()
150 {
151         save();
152 }
153
154 void eRCConfig::set( int delay, int repeat )
155 {
156         rdelay = delay;
157         rrate = repeat;
158 }
159
160 void eRCConfig::reload()
161 {
162         rdelay=500;
163         rrate=100;
164 }
165
166 void eRCConfig::save()
167 {
168 }
169
170 eRCInput *eRCInput::instance;
171
172 eRCInput::eRCInput()
173 {
174         ASSERT( !instance);
175         instance=this;
176         locked = 0;
177         keyboardMode = kmNone;
178 }
179
180 eRCInput::~eRCInput()
181 {
182 }
183
184 void eRCInput::close()
185 {
186 }
187
188 bool eRCInput::open()
189 {
190         return false;
191 }
192
193 void eRCInput::lock()
194 {
195         locked=1;
196         for (std::map<std::string,eRCDevice*>::iterator i=devices.begin(); i != devices.end(); ++i)
197                 i->second->setExclusive(false);
198 }
199
200 void eRCInput::unlock()
201 {
202         locked=0;
203         for (std::map<std::string,eRCDevice*>::iterator i=devices.begin(); i != devices.end(); ++i)
204                 i->second->setExclusive(true);
205 }
206
207 void eRCInput::addDevice(const std::string &id, eRCDevice *dev)
208 {
209         devices.insert(std::pair<std::string,eRCDevice*>(id, dev));
210 }
211
212 void eRCInput::removeDevice(const std::string &id)
213 {
214         devices.erase(id);
215 }
216
217 eRCDevice *eRCInput::getDevice(const std::string &id)
218 {
219         std::map<std::string,eRCDevice*>::iterator i=devices.find(id);
220         if (i == devices.end())
221         {
222                 eDebug("failed, possible choices are:");
223                 for (std::map<std::string,eRCDevice*>::iterator i=devices.begin(); i != devices.end(); ++i)
224                         eDebug("%s", i->first.c_str());
225                 return 0;
226         }
227         return i->second;
228 }
229
230 std::map<std::string,eRCDevice*,eRCInput::lstr> &eRCInput::getDevices()
231 {
232         return devices;
233 }
234
235 eAutoInitP0<eRCInput> init_rcinput(eAutoInitNumbers::rc, "RC Input layer");