fix menu
[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         unsigned char data[16];
42         unsigned char *d = data;
43         int num = read(handle, data, 16);
44         int code=-1;
45         
46         int km = input->getKeyboardMode();
47
48         if (km == eRCInput::kmNone)
49                 return;
50
51         while (num--)
52         {
53 //              eDebug("console code %08x\n", *d);
54                 if (km == eRCInput::kmAll)
55                         code = *d++;
56                 else
57                 {
58                         if (*d == 27) // escape code
59                         {
60                                 while (num)
61                                 {
62                                         num--;
63                                         if (*++d != '[')
64                                                 break;
65                                 }
66                                 code = -1;
67                         } else
68                                 code = *d;
69                         ++d;
70
71                         if (code < 32)                  /* control characters */
72                                 code = -1;
73                         else switch(code)
74                         {
75                         case 0x35:  // mute
76                         case 0x7E:  // mute, einfg, entf
77                         case 0x7F:  // backspace
78                         code = -1;
79                         default:
80                                 break;
81                         }
82                 }
83
84                 if (code != -1)
85                         for (std::list<eRCDevice*>::iterator i(listeners.begin()); i!=listeners.end(); ++i)
86                         {
87 //                              eDebug("ascii %08x", code);
88                                 (*i)->handleCode(code);
89                         }
90         }
91 }
92
93 void eRCConsole::handleCode(int code)
94 {
95         input->keyPressed(eRCKey(this, code, eRCKey::flagAscii));
96 }
97
98 eRCConsole::eRCConsole(eRCDriver *driver)
99                         : eRCDevice("Console", driver)
100 {
101 }
102
103 const char *eRCConsole::getDescription() const
104 {
105         return "Console";
106 }
107
108 const char *eRCConsole::getKeyDescription(const eRCKey &key) const
109 {
110         return 0;
111 }
112
113 int eRCConsole::getKeyCompatibleCode(const eRCKey &key) const
114 {
115         return key.code;
116 }
117
118 class eRCConsoleInit
119 {
120         eRCConsoleDriver driver;
121         eRCConsole device;
122 public:
123         eRCConsoleInit(): driver("/dev/vc/0"), device(&driver)
124         {
125         }
126 };
127
128 eAutoInitP0<eRCConsoleInit> init_rcconsole(eAutoInitNumbers::rc+1, "Console RC Driver");