1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qkbdtty_qws.h"
43
44 #if !defined(QT_NO_QWS_KEYBOARD) && !defined(QT_NO_QWS_KBD_TTY)
45
46 #include <QSocketNotifier>
47 #include <QStringList>
48
49 #include <qplatformdefs.h>
50 #include <private/qcore_unix_p.h> // overrides QT_OPEN
51
52 #include <errno.h>
53 #include <termios.h>
54
55 #if defined Q_OS_LINUX
56 # include <linux/kd.h>
57 # include <linux/vt.h> //TODO: move vt handling somewhere else (QLinuxFbScreen?)
58
59 # include "qscreen_qws.h"
60 # include "qwindowsystem_qws.h"
61 # include "qapplication.h"
62 # include "private/qwindowsurface_qws_p.h"
63 # include "private/qwssignalhandler_p.h"
64
65 # define VTACQSIG SIGUSR1
66 # define VTRELSIG SIGUSR2
67 #endif
68
69
70 QT_BEGIN_NAMESPACE
71
72 class QWSTtyKbPrivate : public QObject
73 {
74 Q_OBJECT
75 public:
76 QWSTtyKbPrivate(QWSTtyKeyboardHandler *handler, const QString &device);
77 ~QWSTtyKbPrivate();
78
79 private:
80 void switchLed(char, bool);
81 void switchConsole(int vt);
82
83 private Q_SLOTS:
84 void readKeycode();
85 void handleConsoleSwitch(int sig);
86
87 private:
88 QWSTtyKeyboardHandler *m_handler;
89 int m_tty_fd;
90 struct termios m_tty_attr;
91 char m_last_keycode;
92 int m_vt_qws;
93 int m_orig_kbmode;
94 };
95
96
QWSTtyKeyboardHandler(const QString & device)97 QWSTtyKeyboardHandler::QWSTtyKeyboardHandler(const QString &device)
98 : QWSKeyboardHandler(device)
99 {
100 d = new QWSTtyKbPrivate(this, device);
101 }
102
~QWSTtyKeyboardHandler()103 QWSTtyKeyboardHandler::~QWSTtyKeyboardHandler()
104 {
105 delete d;
106 }
107
filterKeycode(char &)108 bool QWSTtyKeyboardHandler::filterKeycode(char &)
109 {
110 return false;
111 }
112
QWSTtyKbPrivate(QWSTtyKeyboardHandler * h,const QString & device)113 QWSTtyKbPrivate::QWSTtyKbPrivate(QWSTtyKeyboardHandler *h, const QString &device)
114 : m_handler(h), m_tty_fd(-1), m_last_keycode(0), m_vt_qws(0), m_orig_kbmode(K_XLATE)
115 {
116 setObjectName(QLatin1String("TTY Keyboard Handler"));
117 #ifndef QT_NO_QWS_SIGNALHANDLER
118 QWSSignalHandler::instance()->addObject(this);
119 #endif
120
121 QString dev = QLatin1String("/dev/tty0");
122 int repeat_delay = -1;
123 int repeat_rate = -1;
124
125 QStringList args = device.split(QLatin1Char(':'));
126 foreach (const QString &arg, args) {
127 if (arg.startsWith(QLatin1String("repeat-delay=")))
128 repeat_delay = arg.mid(13).toInt();
129 else if (arg.startsWith(QLatin1String("repeat-rate=")))
130 repeat_rate = arg.mid(12).toInt();
131 else if (arg.startsWith(QLatin1String("/dev/")))
132 dev = arg;
133 }
134
135 m_tty_fd = QT_OPEN(dev.toLocal8Bit().constData(), O_RDWR, 0);
136 if (m_tty_fd >= 0) {
137 if (repeat_delay > 0 && repeat_rate > 0) {
138 #if defined(Q_OS_LINUX)
139 struct ::kbd_repeat kbdrep = { repeat_delay, repeat_rate };
140 ::ioctl(m_tty_fd, KDKBDREP, &kbdrep);
141 #endif
142 }
143
144 QSocketNotifier *notifier;
145 notifier = new QSocketNotifier(m_tty_fd, QSocketNotifier::Read, this);
146 connect(notifier, SIGNAL(activated(int)), this, SLOT(readKeycode()));
147
148 // save tty config for restore.
149 tcgetattr(m_tty_fd, &m_tty_attr);
150
151 struct ::termios termdata;
152 tcgetattr(m_tty_fd, &termdata);
153
154 #if defined(Q_OS_LINUX)
155 // record the original mode so we can restore it again in the destructor.
156 ::ioctl(m_tty_fd, KDGKBMODE, &m_orig_kbmode);
157
158 // PLEASE NOTE:
159 // the tty keycode interface can only report keycodes 0x01 .. 0x7f
160 // KEY_MAX is however defined to 0x1ff. In practice this is sufficient
161 // for a PC style keyboard though.
162 // we don't support K_RAW anymore - if you need that, you have to add
163 // a scan- to keycode converter yourself.
164 ::ioctl(m_tty_fd, KDSKBMODE, K_MEDIUMRAW);
165 #endif
166
167 // set the tty layer to pass-through
168 termdata.c_iflag = (IGNPAR | IGNBRK) & (~PARMRK) & (~ISTRIP);
169 termdata.c_oflag = 0;
170 termdata.c_cflag = CREAD | CS8;
171 termdata.c_lflag = 0;
172 termdata.c_cc[VTIME]=0;
173 termdata.c_cc[VMIN]=1;
174 cfsetispeed(&termdata, 9600);
175 cfsetospeed(&termdata, 9600);
176 tcsetattr(m_tty_fd, TCSANOW, &termdata);
177
178 #if defined(Q_OS_LINUX)
179 // VT switching is handled via unix signals
180 connect(QApplication::instance(), SIGNAL(unixSignal(int)), this, SLOT(handleConsoleSwitch(int)));
181 QApplication::instance()->watchUnixSignal(VTACQSIG, true);
182 QApplication::instance()->watchUnixSignal(VTRELSIG, true);
183
184 struct ::vt_mode vtMode;
185 if (::ioctl(m_tty_fd, VT_GETMODE, &vtMode) == 0) {
186 vtMode.mode = VT_PROCESS;
187 vtMode.relsig = VTRELSIG;
188 vtMode.acqsig = VTACQSIG;
189
190 if (::ioctl(m_tty_fd, VT_SETMODE, &vtMode) == 0) {
191 struct ::vt_stat vtStat;
192 ::memset(&vtStat, 0, sizeof(vtStat));
193
194 if (::ioctl(m_tty_fd, VT_GETSTATE, &vtStat) == 0 ) {
195 m_vt_qws = vtStat.v_active;
196 }
197 }
198 }
199
200 if (!m_vt_qws)
201 qWarning("Could not initialize virtual console switching");
202 #endif
203 } else {
204 qWarning("Cannot open input device '%s': %s", qPrintable(dev), strerror(errno));
205 return;
206 }
207
208 }
209
~QWSTtyKbPrivate()210 QWSTtyKbPrivate::~QWSTtyKbPrivate()
211 {
212 if (m_tty_fd >= 0) {
213 #if defined(Q_OS_LINUX)
214 ::ioctl(m_tty_fd, KDSKBMODE, m_orig_kbmode);
215 #endif
216 tcsetattr(m_tty_fd, TCSANOW, &m_tty_attr);
217 QT_CLOSE(m_tty_fd);
218 }
219 }
220
221
222
switchLed(char led,bool state)223 void QWSTtyKbPrivate::switchLed(char led, bool state)
224 {
225 #if defined(Q_OS_LINUX)
226 char ledstate;
227
228 ::ioctl(m_tty_fd, KDGETLED, &ledstate);
229 if (state)
230 ledstate |= led;
231 else
232 ledstate &= ~led;
233 ::ioctl(m_tty_fd, KDSETLED, ledstate);
234 #endif
235 }
236
readKeycode()237 void QWSTtyKbPrivate::readKeycode()
238 {
239 char buffer[32];
240 int n = 0;
241
242 forever {
243 n = QT_READ(m_tty_fd, buffer + n, 32 - n);
244
245 if (n == 0) {
246 qWarning("Got EOF from the input device.");
247 return;
248 } else if (n < 0 && (errno != EINTR && errno != EAGAIN)) {
249 qWarning("Could not read from input device: %s", strerror(errno));
250 return;
251 } else {
252 break;
253 }
254 }
255
256 for (int i = 0; i < n; ++i) {
257 if (m_handler->filterKeycode(buffer[i]))
258 continue;
259
260 QWSKeyboardHandler::KeycodeAction ka;
261 ka = m_handler->processKeycode(buffer[i] & 0x7f, (buffer[i] & 0x80) == 0x00, buffer[i] == m_last_keycode);
262 m_last_keycode = buffer[i];
263
264 switch (ka) {
265 case QWSKeyboardHandler::CapsLockOn:
266 case QWSKeyboardHandler::CapsLockOff:
267 switchLed(LED_CAP, ka == QWSKeyboardHandler::CapsLockOn);
268 break;
269
270 case QWSKeyboardHandler::NumLockOn:
271 case QWSKeyboardHandler::NumLockOff:
272 switchLed(LED_NUM, ka == QWSKeyboardHandler::NumLockOn);
273 break;
274
275 case QWSKeyboardHandler::ScrollLockOn:
276 case QWSKeyboardHandler::ScrollLockOff:
277 switchLed(LED_SCR, ka == QWSKeyboardHandler::ScrollLockOn);
278 break;
279
280 case QWSKeyboardHandler::PreviousConsole:
281 switchConsole(qBound(1, m_vt_qws - 1, 10));
282 break;
283
284 case QWSKeyboardHandler::NextConsole:
285 switchConsole(qBound(1, m_vt_qws + 1, 10));
286 break;
287
288 default:
289 if (ka >= QWSKeyboardHandler::SwitchConsoleFirst &&
290 ka <= QWSKeyboardHandler::SwitchConsoleLast) {
291 switchConsole(1 + (ka & QWSKeyboardHandler::SwitchConsoleMask));
292 }
293 //ignore reboot
294 break;
295 }
296 }
297 }
298
299
switchConsole(int vt)300 void QWSTtyKbPrivate::switchConsole(int vt)
301 {
302 #if defined(Q_OS_LINUX)
303 if (m_vt_qws && vt && (m_tty_fd >= 0 ))
304 ::ioctl(m_tty_fd, VT_ACTIVATE, vt);
305 #endif
306 }
307
handleConsoleSwitch(int sig)308 void QWSTtyKbPrivate::handleConsoleSwitch(int sig)
309 {
310 #if defined(Q_OS_LINUX)
311 // received a notification from the kernel that the current VT is
312 // changing: either enable or disable QWS painting accordingly.
313
314 if (sig == VTACQSIG) {
315 if (::ioctl(m_tty_fd, VT_RELDISP, VT_ACKACQ) == 0) {
316 qwsServer->enablePainting(true);
317 qt_screen->restore();
318 qwsServer->resumeMouse();
319 qwsServer->refresh();
320 }
321 } else if (sig == VTRELSIG) {
322 qwsServer->enablePainting(false);
323
324 // Check for reserved surfaces which might still do painting
325 bool allWindowsHidden = true;
326 const QList<QWSWindow*> windows = QWSServer::instance()->clientWindows();
327 for (int i = 0; i < windows.size(); ++i) {
328 const QWSWindow *w = windows.at(i);
329 QWSWindowSurface *s = w->windowSurface();
330 if (s && s->isRegionReserved() && !w->allocatedRegion().isEmpty()) {
331 allWindowsHidden = false;
332 break;
333 }
334 }
335
336 if (!allWindowsHidden) {
337 ::ioctl(m_tty_fd, VT_RELDISP, 0); // abort console switch
338 qwsServer->enablePainting(true);
339 } else if (::ioctl(m_tty_fd, VT_RELDISP, 1) == 0) {
340 qt_screen->save();
341 qwsServer->suspendMouse();
342 } else {
343 qwsServer->enablePainting(true);
344 }
345 }
346 #endif
347 }
348
349 QT_END_NAMESPACE
350
351 #include "qkbdtty_qws.moc"
352
353 #endif // QT_NO_QWS_KEYBOARD || QT_NO_QWS_KBD_TTY
354