trunk 20080912
[gitenigma.git] / lib / driver / rcconsole.cpp
blob58e2a94685b7d65c3c4c07b8b943b16d1ed4ae00
1 #include <lib/system/init.h>
2 #include <lib/system/init_num.h>
3 #include <lib/driver/rcconsole.h>
4 #include <stdio.h>
5 #include <fcntl.h>
7 eRCConsoleDriver::eRCConsoleDriver(const char *filename): eRCDriver(eRCInput::getInstance())
9 handle=open(filename, O_RDONLY|O_NONBLOCK);
10 if (handle<0)
12 eDebug("failed to open %s", filename);
13 sn=0;
14 } else
16 sn=new eSocketNotifier(eApp, handle, eSocketNotifier::Read);
17 CONNECT(sn->activated, eRCConsoleDriver::keyPressed);
18 eRCInput::getInstance()->setFile(handle);
21 /* set console mode */
22 struct termios t,ot;
23 tcgetattr(handle, &t);
24 t.c_lflag &= ~(ECHO | ICANON | ECHOK | ECHOE | ECHONL);
25 ot = t;
26 tcsetattr(handle, TCSANOW,&t);
29 eRCConsoleDriver::~eRCConsoleDriver()
31 tcsetattr(handle,TCSANOW, &ot);
32 if (handle>=0)
33 close(handle);
34 if (sn)
35 delete sn;
38 void eRCConsoleDriver::keyPressed(int)
40 unsigned char data[16];
41 unsigned char *d = data;
42 int num = read(handle, data, 16);
43 int code;
45 int km = input->getKeyboardMode();
47 if (km == eRCInput::kmNone)
48 return;
50 while (num--)
52 if (km == eRCInput::kmAll)
53 code = *d++;
54 else
56 if (*d == 27) // escape code
58 while (num)
60 num--;
61 if (*++d != '[')
62 break;
64 code = -1;
65 } else
66 code = *d;
67 ++d;
69 if (code < 32) /* control characters */
70 code = -1;
71 if (code == 0x7F) /* delete */
72 code = -1;
75 if (code != -1)
76 for (std::list<eRCDevice*>::iterator i(listeners.begin()); i!=listeners.end(); ++i)
77 (*i)->handleCode(code);
81 void eRCConsole::handleCode(int code)
83 // eDebug("console code is %d", code);
84 input->keyPressed(eRCKey(this, code, 0));
87 eRCConsole::eRCConsole(eRCDriver *driver)
88 : eRCDevice("Console", driver)
92 const char *eRCConsole::getDescription() const
94 return "Console";
97 const char *eRCConsole::getKeyDescription(const eRCKey &key) const
99 return 0;
102 int eRCConsole::getKeyCompatibleCode(const eRCKey &key) const
104 return key.code | KEY_ASCII;
107 class eRCConsoleInit
109 eRCConsoleDriver driver;
110 eRCConsole device;
111 public:
112 eRCConsoleInit(): driver("/dev/vc/0"), device(&driver)
117 eAutoInitP0<eRCConsoleInit> init_rcconsole(eAutoInitNumbers::rc+1, "Console RC Driver");