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