eb5aee3db7187cc99cc1c0108fee87f0fb657c38
[openblackhole/openblackhole-enigma2.git] / lib / driver / rcconsole.cpp
1 #include <lib/base/init.h>
2 #include <lib/base/init_num.h>
3 #include <lib/base/eerror.h>
4 #include <lib/driver/rcconsole.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7
8 eRCConsoleDriver::eRCConsoleDriver(const char *filename): eRCDriver(eRCInput::getInstance())
9 {
10         handle=open(filename, O_RDONLY|O_NONBLOCK);
11         if (handle<0)
12         {
13                 eDebug("failed to open %s", filename);
14                 sn=0;
15         } else
16         {
17                 sn=eSocketNotifier::create(eApp, handle, eSocketNotifier::Read);
18                 CONNECT(sn->activated, eRCConsoleDriver::keyPressed);
19         }
20         
21                 /* set console mode */
22         struct termios t;
23         tcgetattr(handle, &t);
24         ot = t;
25         t.c_lflag &= ~(ECHO | ICANON | ECHOK | ECHOE | ECHONL);
26         tcsetattr(handle, TCSANOW,&t);
27 }
28
29 eRCConsoleDriver::~eRCConsoleDriver()
30 {
31         tcsetattr(handle,TCSANOW, &ot);
32         if (handle>=0)
33                 close(handle);
34 }
35
36 void eRCConsoleDriver::keyPressed(int)
37 {
38         unsigned char data[16];
39         unsigned char *d = data;
40         int num = read(handle, data, 16);
41         int code=-1;
42         
43         int km = input->getKeyboardMode();
44
45         if (km == eRCInput::kmNone)
46                 return;
47
48         while (num--)
49         {
50 //              eDebug("console code %08x\n", *d);
51                 if (km == eRCInput::kmAll)
52                         code = *d++;
53                 else
54                 {
55                         if (*d == 27) // escape code
56                         {
57                                 while (num)
58                                 {
59                                         num--;
60                                         if (*++d != '[')
61                                                 break;
62                                 }
63                                 code = -1;
64                         } else
65                                 code = *d;
66                         ++d;
67
68                         if (code < 32)                  /* control characters */
69                                 code = -1;
70                         else switch(code)
71                         {
72                         case 0x7E:  // mute, einfg, entf
73                         case 0x7F:  // backspace
74                         code = -1;
75                         default:
76                                 break;
77                         }
78                 }
79
80                 if (code != -1)
81                         for (std::list<eRCDevice*>::iterator i(listeners.begin()); i!=listeners.end(); ++i)
82                         {
83 //                              eDebug("ascii %08x", code);
84                                 (*i)->handleCode(code);
85                         }
86         }
87 }
88
89 void eRCConsole::handleCode(long code)
90 {
91         input->keyPressed(eRCKey(this, code, eRCKey::flagAscii));
92 }
93
94 eRCConsole::eRCConsole(eRCDriver *driver)
95                         : eRCDevice("Console", driver)
96 {
97 }
98
99 const char *eRCConsole::getDescription() const
100 {
101         return "Console";
102 }
103
104 const char *eRCConsole::getKeyDescription(const eRCKey &key) const
105 {
106         return 0;
107 }
108
109 int eRCConsole::getKeyCompatibleCode(const eRCKey &key) const
110 {
111         return key.code;
112 }
113
114 class eRCConsoleInit
115 {
116         eRCConsoleDriver driver;
117         eRCConsole device;
118 public:
119         eRCConsoleInit(): driver("/dev/vc/0"), device(&driver)
120         {
121         }
122 };
123
124 eAutoInitP0<eRCConsoleInit> init_rcconsole(eAutoInitNumbers::rc+1, "Console RC Driver");