Merge branch 'bug_621_caid_info'
[enigma2.git] / lib / driver / rcsdl.cpp
1 #include <lib/driver/rcsdl.h>
2 //#include <lib/actions/action.h>
3 #include <lib/base/init.h>
4 #include <lib/base/init_num.h>
5 #include <lib/driver/input_fake.h>
6
7 /*
8  * eSDLInputDevice
9  */
10
11 eSDLInputDevice::eSDLInputDevice(eRCDriver *driver) : eRCDevice("SDL", driver), m_escape(false), m_unicode(0)
12 {
13 }
14
15 eSDLInputDevice::~eSDLInputDevice()
16 {
17 }
18
19 void eSDLInputDevice::handleCode(long arg)
20 {
21         const SDL_KeyboardEvent *event = (const SDL_KeyboardEvent *)arg;
22         const SDL_keysym *key = &event->keysym;
23         int km = input->getKeyboardMode();
24         int code, flags;
25
26         if (event->type == SDL_KEYDOWN) {
27                 m_unicode = key->unicode;
28                 flags = eRCKey::flagMake;
29         } else {
30                 flags = eRCKey::flagBreak;
31         }
32
33         if (km == eRCInput::kmNone) {
34                 code = translateKey(key->sym);
35         } else {
36                 eDebug("unicode=%04x scancode=%02x", m_unicode, key->scancode);
37                 if (m_unicode & 0xff80) {
38                         eDebug("SDL: skipping unicode character");
39                         return;
40                 }
41                 code = m_unicode & ~0xff80;
42                 // unicode not set...!? use key symbol
43                 if (code == 0) {
44                         // keysym is ascii
45                         if (key->sym >= 128) {
46                                 eDebug("SDL: cannot emulate ASCII");
47                                 return;
48                         }
49                         eDebug("SDL: emulate ASCII");
50                         code = key->sym;
51                 }
52                 if (km == eRCInput::kmAscii) {
53                         // skip ESC c or ESC '[' c
54                         if (m_escape) {
55                                 if (code != '[')
56                                         m_escape = false;
57                                 return;
58                         }
59
60                         if (code == SDLK_ESCAPE)
61                                 m_escape = true;
62
63                         if ((code < SDLK_SPACE) ||
64                             (code == 0x7e) ||   // really?
65                             (code == SDLK_DELETE))
66                                 return;
67                 }
68                 flags |= eRCKey::flagAscii;
69         }
70
71         eDebug("SDL code=%d flags=%d", code, flags);
72         input->keyPressed(eRCKey(this, code, flags));
73 }
74
75 const char *eSDLInputDevice::getDescription() const
76 {
77         return "SDL";
78 }
79
80 int eSDLInputDevice::translateKey(SDLKey key)
81 {
82         #define P(a)    case SDLK_##a: return KEY_##a
83         #define P2(a,b) case SDLK_##a: return KEY_##b
84
85         switch (key) {
86         P(BACKSPACE);
87         P(TAB);
88         P(CLEAR);
89         P2(RETURN,ENTER);
90         P(PAUSE);
91         P2(ESCAPE,ESC);
92         P(SPACE);
93 #if 0
94         P(EXCLAIM);
95         P(QUOTEDBL);
96         P(HASH);
97 #endif
98         P(DOLLAR);
99 #if 0
100         P(AMPERSAND);
101 #endif
102         P2(QUOTE,APOSTROPHE);
103 #if 0
104         P(LEFTPAREN);
105         P(RIGHTPAREN);
106         P(ASTERISK);
107         P(PLUS);
108 #endif
109         P(COMMA);
110         P(MINUS);
111         P2(PERIOD,DOT);
112         P(SLASH);
113         P(0);
114         P(1);
115         P(2);
116         P(3);
117         P(4);
118         P(5);
119         P(6);
120         P(7);
121         P(8);
122         P(9);
123 #if 0
124         P(COLON);
125 #endif
126         P(SEMICOLON);
127 #if 0
128         P(LESS);
129 #endif
130         P2(EQUALS,EQUAL);
131 #if 0
132         P(GREATER);
133 #endif
134         P(QUESTION);
135 #if 0
136         P(AT);
137 #endif
138         P2(LEFTBRACKET,LEFTBRACE);
139         P(BACKSLASH);
140         P2(RIGHTBRACKET,RIGHTBRACE);
141         P2(CARET,GRAVE);
142 #if 0
143         P(UNDERSCORE);
144         P(BACKQUOTE);
145 #endif
146         P2(a,A);
147         P2(b,B);
148         P2(c,C);
149         P2(d,D);
150         P2(e,E);
151         P2(f,F);
152         P2(g,G);
153         P2(h,H);
154         P2(i,I);
155         P2(j,J);
156         P2(k,K);
157         P2(l,L);
158         P2(m,M);
159         P2(n,N);
160         P2(o,O);
161         P2(p,P);
162         P2(q,Q);
163         P2(r,R);
164         P2(s,S);
165         P2(t,T);
166         P2(u,U);
167         P2(v,V);
168         P2(w,W);
169         P2(x,X);
170         P2(y,Y);
171         P2(z,Z);
172         P(DELETE);
173 #if 0
174         P(WORLD_0);
175         P(WORLD_1);
176         P(WORLD_2);
177         P(WORLD_3);
178         P(WORLD_4);
179         P(WORLD_5);
180         P(WORLD_6);
181         P(WORLD_7);
182         P(WORLD_8);
183         P(WORLD_9);
184         P(WORLD_10);
185         P(WORLD_11);
186         P(WORLD_12);
187         P(WORLD_13);
188         P(WORLD_14);
189         P(WORLD_15);
190         P(WORLD_16);
191         P(WORLD_17);
192         P(WORLD_18);
193         P(WORLD_19);
194         P(WORLD_20);
195         P(WORLD_21);
196         P(WORLD_22);
197         P(WORLD_23);
198         P(WORLD_24);
199         P(WORLD_25);
200         P(WORLD_26);
201         P(WORLD_27);
202         P(WORLD_28);
203         P(WORLD_29);
204         P(WORLD_30);
205         P(WORLD_31);
206         P(WORLD_32);
207         P(WORLD_33);
208         P(WORLD_34);
209         P(WORLD_35);
210         P(WORLD_36);
211         P(WORLD_37);
212         P(WORLD_38);
213         P(WORLD_39);
214         P(WORLD_40);
215         P(WORLD_41);
216         P(WORLD_42);
217         P(WORLD_43);
218         P(WORLD_44);
219         P(WORLD_45);
220         P(WORLD_46);
221         P(WORLD_47);
222         P(WORLD_48);
223         P(WORLD_49);
224         P(WORLD_50);
225         P(WORLD_51);
226         P(WORLD_52);
227         P(WORLD_53);
228         P(WORLD_54);
229         P(WORLD_55);
230         P(WORLD_56);
231         P(WORLD_57);
232         P(WORLD_58);
233         P(WORLD_59);
234         P(WORLD_60);
235         P(WORLD_61);
236         P(WORLD_62);
237         P(WORLD_63);
238         P(WORLD_64);
239         P(WORLD_65);
240         P(WORLD_66);
241         P(WORLD_67);
242         P(WORLD_68);
243         P(WORLD_69);
244         P(WORLD_70);
245         P(WORLD_71);
246         P(WORLD_72);
247         P(WORLD_73);
248         P(WORLD_74);
249         P(WORLD_75);
250         P(WORLD_76);
251         P(WORLD_77);
252         P(WORLD_78);
253         P(WORLD_79);
254         P(WORLD_80);
255         P(WORLD_81);
256         P(WORLD_82);
257         P(WORLD_83);
258         P(WORLD_84);
259         P(WORLD_85);
260         P(WORLD_86);
261         P(WORLD_87);
262         P(WORLD_88);
263         P(WORLD_89);
264         P(WORLD_90);
265         P(WORLD_91);
266         P(WORLD_92);
267         P(WORLD_93);
268         P(WORLD_94);
269         P(WORLD_95);
270 #endif
271         P(KP0);
272         P(KP1);
273         P(KP2);
274         P(KP3);
275         P(KP4);
276         P(KP5);
277         P(KP6);
278         P(KP7);
279         P(KP8);
280         P(KP9);
281         P2(KP_PERIOD,KPDOT);
282         P2(KP_DIVIDE,KPSLASH);
283         P2(KP_MULTIPLY,KPASTERISK);
284         P2(KP_MINUS,KPMINUS);
285         P2(KP_PLUS,KPPLUS);
286         P2(KP_ENTER,KPENTER);
287         P2(KP_EQUALS,KPEQUAL);
288         P(UP);
289         P(DOWN);
290         P(RIGHT);
291         P(LEFT);
292         P(INSERT);
293         P(HOME);
294         P(END);
295         P(PAGEUP);
296         P(PAGEDOWN);
297         P(F1);
298         P(F2);
299         P(F3);
300         P(F4);
301         P(F5);
302         P(F6);
303         P(F7);
304         P(F8);
305         P(F9);
306         P(F10);
307         P(F11);
308         P(F12);
309         P(F13);
310         P(F14);
311         P(F15);
312         P(NUMLOCK);
313         P(CAPSLOCK);
314         P2(SCROLLOCK,SCROLLLOCK);
315         P2(RSHIFT,RIGHTSHIFT);
316         P2(LSHIFT,LEFTSHIFT);
317         P2(RCTRL,RIGHTCTRL);
318         P2(LCTRL,LEFTCTRL);
319         P2(RALT,RIGHTALT);
320         P2(LALT,LEFTALT);
321         P2(RMETA,RIGHTMETA);
322         P2(LMETA,LEFTMETA);
323 #if 0
324         P(LSUPER);
325         P(RSUPER);
326 #endif
327         P(MODE);
328         P(COMPOSE);
329         P(HELP);
330         P(PRINT);
331         P2(SYSREQ,SYSRQ);
332         P(BREAK);
333         P(MENU);
334         P(POWER);
335         P(EURO);
336         P(UNDO);
337         default:
338                 eDebug("unhandled SDL keycode: %d", key);
339                 return KEY_RESERVED;
340         }
341
342         #undef P2
343         #undef P
344 }
345
346 /*
347  * eSDLInputDriver
348  */
349
350 eSDLInputDriver *eSDLInputDriver::instance;
351
352 eSDLInputDriver::eSDLInputDriver() : eRCDriver(eRCInput::getInstance())
353 {
354         ASSERT(instance == 0);
355         instance = this;
356 }
357
358 eSDLInputDriver::~eSDLInputDriver()
359 {
360         instance = 0;
361 }
362
363 void eSDLInputDriver::keyPressed(const SDL_KeyboardEvent *key)
364 {
365         eDebug("km=%d enabled=%d locked=%d",
366                 input->getKeyboardMode(), enabled, input->islocked());
367
368         if (!enabled || input->islocked())
369                 return;
370
371         std::list<eRCDevice*>::iterator i(listeners.begin());
372         while (i != listeners.end()) {
373                 (*i)->handleCode((long)key);
374                 ++i;
375         }
376 }
377
378 class eRCSDLInit
379 {
380 private:
381         eSDLInputDriver driver;
382         eSDLInputDevice device;
383
384 public:
385         eRCSDLInit(): driver(), device(&driver)
386         {
387         }
388 };
389
390 eAutoInitP0<eRCSDLInit> init_rcSDL(eAutoInitNumbers::rc+1, "SDL RC Driver");