Merge remote branch 'origin/bug_348_dvdburn_residenttask'
[enigma2.git] / lib / driver / rcinput.cpp
1 #include <lib/driver/rcinput.h>
2
3 #include <lib/base/eerror.h>
4
5 #include <sys/ioctl.h>
6 #include <linux/input.h>
7 #include <sys/stat.h>
8
9 #include <lib/base/ebase.h>
10 #include <lib/base/init.h>
11 #include <lib/base/init_num.h>
12 #include <lib/driver/input_fake.h>
13
14 void eRCDeviceInputDev::handleCode(long rccode)
15 {
16         struct input_event *ev = (struct input_event *)rccode;
17         if (ev->type!=EV_KEY)
18                 return;
19
20 //      eDebug("%x %x %x", ev->value, ev->code, ev->type);
21
22         if (ev->type!=EV_KEY)
23                 return;
24
25         int km = iskeyboard ? input->getKeyboardMode() : eRCInput::kmNone;
26
27 //      eDebug("keyboard mode %d", km);
28         
29         if (km == eRCInput::kmAll)
30                 return;
31
32         if (km == eRCInput::kmAscii)
33         {
34 //              eDebug("filtering.. %d", ev->code);
35                 bool filtered = ( ev->code > 0 && ev->code < 61 );
36                 switch (ev->code)
37                 {
38                         case KEY_RESERVED:
39                         case KEY_ESC:
40                         case KEY_TAB:
41                         case KEY_BACKSPACE:
42                         case KEY_ENTER:
43                         case KEY_LEFTCTRL:
44                         case KEY_RIGHTSHIFT:
45                         case KEY_LEFTALT:
46                         case KEY_CAPSLOCK:
47                         case KEY_INSERT:
48                         case KEY_DELETE:
49                         case KEY_MUTE:
50                                 filtered=false;
51                         default:
52                                 break;
53                 }
54                 if (filtered)
55                         return;
56 //              eDebug("passed!");
57         }
58
59         switch (ev->value)
60         {
61         case 0:
62                 /*emit*/ input->keyPressed(eRCKey(this, ev->code, eRCKey::flagBreak));
63                 break;
64         case 1:
65                 /*emit*/ input->keyPressed(eRCKey(this, ev->code, 0));
66                 break;
67         case 2:
68                 /*emit*/ input->keyPressed(eRCKey(this, ev->code, eRCKey::flagRepeat));
69                 break;
70         }
71 }
72
73 eRCDeviceInputDev::eRCDeviceInputDev(eRCInputEventDriver *driver)
74         :eRCDevice(driver->getDeviceName(), driver), iskeyboard(false)
75 {
76         int len=id.length();
77         int idx=0;
78         while(idx <= len-8)
79         {
80                 if (!strncasecmp(&id[idx++], "KEYBOARD", 8))
81                 {
82                         iskeyboard=true;
83                         break;
84                 }
85         }
86         setExclusive(true);
87         eDebug("Input device \"%s\" is %sa keyboard.", id.c_str(), iskeyboard ? "" : "not ");
88 }
89
90 void eRCDeviceInputDev::setExclusive(bool b)
91 {
92         driver->setExclusive(!iskeyboard && b);
93 }
94
95 const char *eRCDeviceInputDev::getDescription() const
96 {
97         return id.c_str();
98 }
99
100 class eInputDeviceInit
101 {
102         ePtrList<eRCInputEventDriver> m_drivers;
103         ePtrList<eRCDeviceInputDev> m_devices;
104 public:
105         eInputDeviceInit()
106         {
107                 int i = 0;
108                 while (1)
109                 {
110                         struct stat s;
111                         char filename[128];
112                         sprintf(filename, "/dev/input/event%d", i);
113                         if (stat(filename, &s))
114                                 break;
115                         eRCInputEventDriver *p;
116                         m_drivers.push_back(p = new eRCInputEventDriver(filename));
117                         m_devices.push_back(new eRCDeviceInputDev(p));
118                         ++i;
119                 }
120                 eDebug("Found %d input devices!", i);
121         }
122         
123         ~eInputDeviceInit()
124         {
125                 while (m_drivers.size())
126                 {
127                         delete m_devices.back();
128                         m_devices.pop_back();
129                         delete m_drivers.back();
130                         m_drivers.pop_back();
131                 }
132         }
133 };
134
135 eAutoInitP0<eInputDeviceInit> init_rcinputdev(eAutoInitNumbers::rc+1, "input device driver");