3b5397b1b42f4456d65bdf02539c3328cf4d5d8c
[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=new eSocketNotifier(eApp, handle, eSocketNotifier::Read);
18                 CONNECT(sn->activated, eRCConsoleDriver::keyPressed);
19                 eRCInput::getInstance()->setFile(handle);
20         }
21         
22                 /* set console mode */
23         struct termios t;
24         tcgetattr(handle, &t);
25         ot = t;
26         t.c_lflag &= ~(ECHO | ICANON | ECHOK | ECHOE | ECHONL);
27         tcsetattr(handle, TCSANOW,&t);
28 }
29
30 eRCConsoleDriver::~eRCConsoleDriver()
31 {
32         tcsetattr(handle,TCSANOW, &ot);
33         if (handle>=0)
34                 close(handle);
35         if (sn)
36                 delete sn;
37 }
38
39 void eRCConsoleDriver::keyPressed(int)
40 {
41         char data[16];
42         char *d = data;
43         int num = read(handle, data, 16);
44         int code;
45 #if 0   
46         int km = input->getKeyboardMode();
47
48         if (km == eRCInput::kmNone)
49                 return;
50 #endif
51         while (num--)
52         {
53 #if 0
54                 if (km == eRCInput::kmAll)
55 #endif
56                         code = *d++;
57 #if 0
58                 else
59                 {
60                         if (*d == 27) // escape code
61                         {
62                                 while (num)
63                                 {
64                                         num--;
65                                         if (*++d != '[')
66                                                 break;
67                                 }
68                                 code = -1;
69                         } else
70                                 code = *d;
71                         ++d;
72                         
73                         if (code < 32)                  /* control characters */
74                                 code = -1;
75                         if (code == 0x7F)               /* delete */
76                                 code = -1;
77                 }
78 #endif
79                 if (code != -1)
80                         for (std::list<eRCDevice*>::iterator i(listeners.begin()); i!=listeners.end(); ++i)
81                                 (*i)->handleCode(code | 0x8000);
82         }
83 }
84
85 void eRCConsole::handleCode(int code)
86 {
87         input->keyPressed(eRCKey(this, code, 0));
88 }
89
90 eRCConsole::eRCConsole(eRCDriver *driver)
91                         : eRCDevice("Console", driver)
92 {
93 }
94
95 const char *eRCConsole::getDescription() const
96 {
97         return "Console";
98 }
99
100 const char *eRCConsole::getKeyDescription(const eRCKey &key) const
101 {
102         return 0;
103 }
104
105 int eRCConsole::getKeyCompatibleCode(const eRCKey &key) const
106 {
107         return key.code; // | KEY_ASCII;
108 }
109
110 class eRCConsoleInit
111 {
112         eRCConsoleDriver driver;
113         eRCConsole device;
114 public:
115         eRCConsoleInit(): driver("/dev/vc/0"), device(&driver)
116         {
117         }
118 };
119
120 eAutoInitP0<eRCConsoleInit> init_rcconsole(eAutoInitNumbers::rc+1, "Console RC Driver");